package srsim.ui.swing;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultComboBoxModel;
import javax.swing.GroupLayout;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;
import javax.swing.WindowConstants;
import srsim.domain.Room;
import srsim.simulator.ContextChangedEvent;
import srsim.simulator.ControllerActionEvent;
import srsim.simulator.ISimulationController;
import srsim.simulator.ISimulationListener;
import srsim.simulator.SimulationContext;
import srsim.simulator.SimulationContextException;
import srsim.ui.IView;
/**
*
* @author Sita
*/
public class SwingView extends JFrame implements ISimulationListener, IView {
private static final long serialVersionUID = 7666389409605042980L;
private static final int LAYOUT_GAP_SIZE = 10;
private JComboBox<String> roomSelectionComboBox;
private JLabel roomLabel;
private JLabel colorLabel;
private JLabel musicLabel;
private JLabel temperatureLabel;
private JLabel consumptionLabel;
private JLabel intensityLabel;
private JTextField colorTextField;
private JTextField intensityTextField;
private JTextField musicTextField;
private JTextField temperatureTextField;
private JTextField consumptionTextField;
private ISimulationController controller;
/**
* Creates new form room
*/
public SwingView(final ISimulationController controller) {
this.controller = controller;
controller.addSimulationListener(this);
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
private void initComponents() {
roomLabel = new JLabel();
colorLabel = new JLabel();
roomSelectionComboBox = new JComboBox<String>();
musicLabel = new JLabel();
temperatureLabel = new JLabel();
colorTextField = new JTextField();
intensityTextField = new JTextField();
musicTextField = new JTextField();
consumptionLabel = new JLabel();
temperatureTextField = new JTextField();
intensityLabel = new JLabel();
consumptionTextField = new JTextField();
Dimension preferredSize = new Dimension(150, 20);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setTitle("Smartroom Simulator");
setResizable(false);
roomLabel.setText("Room");
DefaultComboBoxModel<String> roomModel = new DefaultComboBoxModel<String>();
for (Room room : controller.getRooms()) {
roomModel.addElement(room.getName());
}
roomSelectionComboBox.setModel(roomModel);
roomSelectionComboBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
roomSelectionBoxActionPerformed(evt);
}
});
colorLabel.setText("Light Color");
colorLabel.setPreferredSize(preferredSize);
colorTextField.setText("#000000");
colorTextField.setPreferredSize(preferredSize);
intensityLabel.setText("Brightness");
intensityLabel.setPreferredSize(preferredSize);
intensityTextField.setText("0.0 lux");
intensityTextField.setPreferredSize(preferredSize);
musicLabel.setText("Music Genre");
musicLabel.setPreferredSize(preferredSize);
musicTextField.setText("none");
musicTextField.setPreferredSize(preferredSize);
temperatureLabel.setText("Temperature");
temperatureLabel.setPreferredSize(preferredSize);
temperatureTextField.setText("0.0 deg C");
temperatureTextField.setPreferredSize(preferredSize);
consumptionLabel.setText("Energy Consumption");
consumptionLabel.setPreferredSize(preferredSize);
consumptionTextField.setText("0.0 W");
consumptionTextField.setPreferredSize(preferredSize);
GroupLayout layout = new GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(layout.createParallelGroup(
GroupLayout.Alignment.LEADING)
.addGroup(
layout.createSequentialGroup()
.addContainerGap()
.addGroup(
layout.createParallelGroup(
GroupLayout.Alignment.LEADING)
.addComponent(consumptionLabel)
.addComponent(temperatureLabel)
.addComponent(musicLabel)
.addComponent(roomLabel)
.addComponent(intensityLabel)
.addComponent(colorLabel))
.addPreferredGap(
LayoutStyle.ComponentPlacement.RELATED)
.addGroup(
layout.createParallelGroup(
GroupLayout.Alignment.LEADING)
.addComponent(
intensityTextField)
.addComponent(musicTextField)
.addComponent(
temperatureTextField)
.addComponent(
roomSelectionComboBox)
.addComponent(colorTextField)
.addComponent(
consumptionTextField))
.addGap(LAYOUT_GAP_SIZE)));
layout.setVerticalGroup(layout.createParallelGroup(
GroupLayout.Alignment.LEADING)
.addGroup(
layout.createSequentialGroup()
.addContainerGap(LAYOUT_GAP_SIZE,
LAYOUT_GAP_SIZE)
.addGroup(
layout.createParallelGroup(
GroupLayout.Alignment.BASELINE)
.addComponent(
roomSelectionComboBox)
.addComponent(roomLabel))
.addGap(LAYOUT_GAP_SIZE)
.addGroup(
layout.createParallelGroup(
GroupLayout.Alignment.BASELINE)
.addComponent(colorLabel)
.addComponent(colorTextField))
.addGap(LAYOUT_GAP_SIZE)
.addGroup(
layout.createParallelGroup(
GroupLayout.Alignment.BASELINE)
.addComponent(intensityLabel)
.addComponent(
intensityTextField))
.addGap(LAYOUT_GAP_SIZE)
.addGroup(
layout.createParallelGroup(
GroupLayout.Alignment.BASELINE)
.addComponent(musicLabel)
.addComponent(musicTextField))
.addGap(LAYOUT_GAP_SIZE)
.addGroup(
layout.createParallelGroup(
GroupLayout.Alignment.BASELINE)
.addComponent(temperatureLabel)
.addComponent(
temperatureTextField))
.addPreferredGap(
LayoutStyle.ComponentPlacement.RELATED)
.addGroup(
layout.createParallelGroup(
GroupLayout.Alignment.LEADING)
.addComponent(consumptionLabel)
.addComponent(
consumptionTextField))
.addGap(LAYOUT_GAP_SIZE)));
pack();
}
private void roomSelectionBoxActionPerformed(ActionEvent evt) {
try {
for (Room room : controller.getRooms()) {
if (room.getName().equals(
(String) roomSelectionComboBox.getSelectedItem())) {
updateValues(room.getLocalContext());
break;
}
}
} catch (SimulationContextException e) {
e.printStackTrace();
}
}
@Override
public void handleContextChange(ContextChangedEvent event)
throws SimulationContextException {
SimulationContext context = event.getContext();
if (context.getRoom().getName()
.equals((String) roomSelectionComboBox.getSelectedItem())) {
updateValues(context);
}
}
private void updateValues(SimulationContext context)
throws SimulationContextException {
temperatureTextField.setText(String.format("%.2f deg C",
context.getTemperature()));
intensityTextField.setText(String.format("%.2f lux",
context.getBrightness()));
consumptionTextField.setText(String.format("%.2f W",
context.getEnergyConsumption()));
String color = "#";
for (int c : context.getLightColor()) {
color += String.format("%02X", c);
}
colorTextField.setText(color);
if (context.getMusicVolume() > 0) {
musicTextField.setText(context.getMusicGenre());
} else {
musicTextField.setText("none");
}
}
@Override
public void handleControllerAction(ControllerActionEvent event) {
}
@Override
public void init() {
initComponents();
}
@Override
public void display() {
EventQueue.invokeLater(new Runnable() {
public void run() {
setVisible(true);
}
});
}
}