001 package dk.deepthought.sidious.gui;
002 
003 import java.awt.BorderLayout;
004 import java.awt.Dimension;
005 import java.awt.event.ActionEvent;
006 import java.awt.event.ActionListener;
007 import java.util.List;
008 
009 import javax.swing.Box;
010 import javax.swing.BoxLayout;
011 import javax.swing.JButton;
012 import javax.swing.JFrame;
013 import javax.swing.JLabel;
014 import javax.swing.JPanel;
015 import javax.swing.JTextArea;
016 import javax.swing.JTextField;
017 import javax.swing.UIManager;
018 
019 import dk.deepthought.sidious.supportsystem.Step;
020 
021 /**
022  * Class represents the gui of the project.
023  
024  @author Deepthought
025  
026  */
027 @SuppressWarnings("serial")//Only needed before Java 1.4!
028 public class Display extends JFrame {
029 
030     /**
031      * The main panel.
032      */
033     private JPanel mainPanel;
034 
035     /**
036      * The cost label.
037      */
038     private SidiousLabel cost = new SidiousLabel("Cost: ");
039 
040     /**
041      * The temperature label.
042      */
043     private SidiousLabel temperature = new SidiousLabel("Temperature: ");
044 
045     /**
046      * The humidity label.
047      */
048     private SidiousLabel humidity = new SidiousLabel("Humidity: ");
049 
050     /**
051      * The CO2 label.
052      */
053     private SidiousLabel co2 = new SidiousLabel("CO2: ");
054 
055     /**
056      * The irradiance label.
057      */
058     private SidiousLabel irradiance = new SidiousLabel("Irradiance: ");
059 
060     /**
061      * The time label.
062      */
063     private SidiousLabel time = new SidiousLabel("Time: ");
064 
065     /**
066      * The area to put the final plan.
067      */
068     private JTextArea finishField;
069 
070     /**
071      * The controller of this.
072      */
073     private transient SidiousController controller;
074 
075     /**
076      * Constructor.
077      */
078     public Display() {
079         initGui();
080     }
081 
082     /**
083      * Init GUI.
084      */
085     private void initGui() {
086         // set look&feel
087         try {
088             UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
089         catch (Exception e) {
090             e.printStackTrace();
091         }
092         // builds the main Panel
093         mainPanel = new JPanel();
094         mainPanel.setLayout(new BorderLayout());
095 
096         JPanel panel = new JPanel();
097         panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
098         panel.add(cost);
099         panel.add(temperature);
100         panel.add(humidity);
101         panel.add(co2);
102         panel.add(irradiance);
103         panel.add(time);
104         mainPanel.add(panel, BorderLayout.CENTER);
105 
106         // Add finish field
107         finishField = new JTextArea();
108         panel.add(finishField);
109 
110         // Add buttons
111         JPanel buttonPanel = new JPanel();
112         // Add start button
113         JButton startButton = new JButton("Start");
114         startButton.addActionListener(new ActionListener() {
115             public void actionPerformed(final ActionEvent e) {
116                 controller.start();
117             }
118         });
119         buttonPanel.add(startButton);
120         // Add stop button
121         JButton stopButton = new JButton("Stop");
122         stopButton.addActionListener(new ActionListener() {
123             public void actionPerformed(final ActionEvent e) {
124                 controller.stop();
125             }
126         });
127         buttonPanel.add(stopButton);
128         // Add buttons to main panel
129         mainPanel.add(buttonPanel, BorderLayout.SOUTH);
130 
131         // Add main panel
132         getContentPane().add(mainPanel);
133         // Finish up
134         setPreferredSize(new Dimension(640480));
135         setDefaultCloseOperation(EXIT_ON_CLOSE);
136         setLocation(00);
137         pack();
138         setVisible(true);
139     }
140 
141     /**
142      * Sets the controller for this
143      
144      @param controller
145      *            the controller for this
146      */
147     public void setController(SidiousController controller) {
148         this.controller = controller;
149     }
150 
151     /**
152      * Sets the final plan.
153      
154      @param steps
155      *            the steps comprising the plan
156      */
157     public void finished(List<Step> steps) {
158         for (Step step : steps) {
159             finishField.append(step.toString());
160         }
161     }
162 
163     /**
164      * Sets the vertex and state values.
165      
166      @param cost
167      *            the cost
168      @param temperature
169      *            the temperature
170      @param humidity
171      *            the humidity
172      @param co2
173      *            the CO2 level
174      @param irradiance
175      *            the irradiance
176      @param time
177      *            the time
178      */
179     public void setVertexAndStateValues(double cost, double temperature,
180             double humidity, double co2, double irradiance, double time) {
181         this.cost.setText(String.valueOf(cost));
182         this.temperature.setText(String.valueOf(temperature));
183         this.humidity.setText(String.valueOf(humidity));
184         this.co2.setText(String.valueOf(co2));
185         this.irradiance.setText(String.valueOf(irradiance));
186         this.time.setText(String.valueOf(time));
187     }
188 
189     /**
190      * Private class to hold a label.
191      
192      @author Deepthought
193      
194      */
195     private static class SidiousLabel extends JPanel {
196 
197         private JLabel label;
198 
199         private JTextField textField;
200 
201         public SidiousLabel(String labelText) {
202             textField = new JTextField();
203             textField.setPreferredSize(new Dimension(8012));
204             label = new JLabel(labelText);
205             label.setPreferredSize(new Dimension(8010));
206             setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
207             add(label);
208             add(Box.createRigidArea(new Dimension(100)));
209             add(textField);
210         }
211 
212         public void setText(String s) {
213             textField.setText(s);
214         }
215 
216     }
217 
218 }