Sfoglia il codice sorgente

ProfileEditor GUI improvements

Made it more clear how the ProfileEditor works by adding labels, etc.
Tankernn 8 anni fa
parent
commit
f8a63f7a02

+ 2 - 4
src/main/java/eu/tankernn/grid/FanSpeedProfile.java

@@ -3,8 +3,8 @@ package eu.tankernn.grid;
 import java.util.Arrays;
 
 public class FanSpeedProfile {
-	public static final double MAX_TEMP = 80, MIN_TEMP = 30;
 	public static final int STEPS = 5;
+	public static final double MAX_TEMP = 80, MIN_TEMP = 30, STEP_SIZE = (MAX_TEMP - MIN_TEMP) / (double) STEPS;
 
 	public final String name;
 	public final int[] percentages;
@@ -15,15 +15,13 @@ public class FanSpeedProfile {
 	}
 
 	public int getSpeedPercentage(double temp) {
-		double stepSize = (MAX_TEMP - MIN_TEMP) / (double) STEPS;
-
 		double currentTemp = MIN_TEMP;
 
 		for (int i : percentages) {
 			if (temp < currentTemp) {
 				return i;
 			}
-			currentTemp += stepSize;
+			currentTemp += STEP_SIZE;
 		}
 
 		return percentages[percentages.length - 1];

+ 6 - 8
src/main/java/eu/tankernn/grid/frame/GridControlPanel.java

@@ -35,16 +35,13 @@ public class GridControlPanel extends JFrame {
 	private ComputerModel model;
 
 	private JMenuBar menuBar = new JMenuBar();
-	private JMenu fileMenu = new JMenu("File"),
-			settingsMenu = new JMenu("Settings"),
+	private JMenu fileMenu = new JMenu("File"), settingsMenu = new JMenu("Settings"),
 			profileMenu = new JMenu("Profiles");
 	private JMenuItem saveSettings = new JMenuItem("Save settings..."),
-			sensorConf = new JMenuItem("Configure sensors..."),
-			addProfile = new JMenuItem("Add profile...");
+			sensorConf = new JMenuItem("Configure sensors..."), addProfile = new JMenuItem("Add profile...");
 
 	private FanPanel[] fanPanels;
-	private JPanel serialPanel = new JPanel(), gridPanel = new JPanel(),
-			infoPanel = new JPanel();
+	private JPanel serialPanel = new JPanel(), gridPanel = new JPanel(), infoPanel = new JPanel();
 
 	private JSpinner minSpeed = new JSpinner(new SpinnerNumberModel(30, 0, 100, 5)),
 			pollingSpeed = new JSpinner(new SpinnerNumberModel(500, 100, 2000, 100));
@@ -130,7 +127,7 @@ public class GridControlPanel extends JFrame {
 		this.setTitle("JavaGridControl");
 	}
 
-	private JPanel labelledComponent(String labelText, JComponent component) {
+	static JPanel labelledComponent(String labelText, JComponent component) {
 		JPanel panel = new JPanel(new FlowLayout());
 		panel.add(new JLabel(labelText));
 		panel.add(component);
@@ -142,7 +139,8 @@ public class GridControlPanel extends JFrame {
 	 * certain UI elements are updated Finally a pollAndCompute Thread is
 	 * started
 	 *
-	 * @param model the model to set
+	 * @param model
+	 *            the model to set
 	 */
 	private void setModel(ComputerModel model) {
 		this.model = model;

+ 37 - 15
src/main/java/eu/tankernn/grid/frame/ProfileEditor.java

@@ -3,8 +3,11 @@ package eu.tankernn.grid.frame;
 import java.awt.BorderLayout;
 import java.awt.GridLayout;
 import java.util.Arrays;
+import java.util.Dictionary;
+import java.util.Hashtable;
 import java.util.stream.IntStream;
 
+import javax.swing.BoxLayout;
 import javax.swing.JLabel;
 import javax.swing.JOptionPane;
 import javax.swing.JPanel;
@@ -14,37 +17,56 @@ import javax.swing.JTextField;
 import eu.tankernn.grid.FanSpeedProfile;
 
 public class ProfileEditor {
+	
+	private static Dictionary<Integer, JLabel> dictionary = new Hashtable<>();
+	static {
+		dictionary.put(0, new JLabel("0%"));
+		dictionary.put(50, new JLabel("50%"));
+		dictionary.put(100, new JLabel("100%"));
+	}
 
 	public FanSpeedProfile editProfile(FanSpeedProfile profile) {
 		JPanel panel = new JPanel(), sliderPanel = new JPanel();
 		JSlider[] sliders;
-		JTextField nameField = new JTextField();
+		JTextField nameField = new JTextField(20);
 		if (profile != null) {
 			nameField.setText(profile.name);
-			sliders = Arrays.stream(profile.percentages).mapToObj(i -> new JSlider(JSlider.VERTICAL, 0, 100, i)).toArray(JSlider[]::new);
+			sliders = Arrays.stream(profile.percentages).mapToObj(i -> new JSlider(JSlider.VERTICAL, 0, 100, i))
+					.toArray(JSlider[]::new);
 		} else {
-			sliders = IntStream.range(0, FanSpeedProfile.STEPS).mapToObj(i -> new JSlider(JSlider.VERTICAL)).toArray(JSlider[]::new);
+			sliders = IntStream.range(0, FanSpeedProfile.STEPS).mapToObj(i -> new JSlider(JSlider.VERTICAL, 0, 100, 50))
+					.toArray(JSlider[]::new);
 		}
 		
-		for (JSlider s : sliders) {
+		sliderPanel.setLayout(new GridLayout(1, sliders.length));
+		
+		sliders[sliders.length -1].setPaintLabels(true);
+
+		for (int i = 0; i < sliders.length; i++) {
+			JSlider s = sliders[i];
+			s.setPaintTicks(true);
 			s.setSnapToTicks(true);
+			s.setLabelTable(dictionary);
 			s.setMinorTickSpacing(5);
+			s.setMajorTickSpacing(50);
+			JPanel p = new JPanel();
+			p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
+			p.add(s);
+			p.add(new JLabel(FanSpeedProfile.MIN_TEMP + FanSpeedProfile.STEP_SIZE * i + " \u00B0C"));
+			sliderPanel.add(p);
 		}
-		
+
 		panel.setLayout(new BorderLayout());
-		panel.add(new JLabel("Profile name: "));
-		panel.add(nameField, BorderLayout.NORTH);
-		
-		sliderPanel.setLayout(new GridLayout(1, sliders.length));
-		for (JSlider s : sliders)
-			sliderPanel.add(s);
+		panel.add(GridControlPanel.labelledComponent("Profile name: ", nameField), BorderLayout.NORTH);
+
 		panel.add(sliderPanel, BorderLayout.CENTER);
-		
 
-		int response = JOptionPane.showConfirmDialog(null, panel, "Fan Speed Profile Editor", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
+		int response = JOptionPane.showConfirmDialog(null, panel, "Fan Speed Profile Editor",
+				JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
 
 		if (response == JOptionPane.OK_OPTION) {
-			FanSpeedProfile newProfile = new FanSpeedProfile(nameField.getText(), Arrays.stream(sliders).mapToInt(JSlider::getValue).toArray());
+			FanSpeedProfile newProfile = new FanSpeedProfile(nameField.getText(),
+					Arrays.stream(sliders).mapToInt(JSlider::getValue).toArray());
 			if (nameField.getText().isEmpty()) {
 				JOptionPane.showMessageDialog(null, "Please enter a name for the profile.");
 				return editProfile(newProfile);
@@ -54,7 +76,7 @@ public class ProfileEditor {
 			return profile;
 		}
 	}
-	
+
 	public static void main(String[] args) {
 		System.out.println(new ProfileEditor().editProfile(null));
 	}