1 package org.wcb.autohome;
2
3 import org.wcb.autohome.interfaces.X10DeviceConstants;
4 import org.wcb.autohome.interfaces.MessageInterface;
5 import org.wcb.autohome.interfaces.I18nConstants;
6 import org.wcb.autohome.exceptions.HomeException;
7 import org.wcb.autohome.implementations.SerialPortBean;
8 import org.wcb.common.UIEditorDialog;
9 import org.wcb.common.GuiLib;
10
11 import javax.swing.*;
12 import java.awt.*;
13 import java.awt.event.ActionListener;
14 import java.awt.event.ActionEvent;
15
16 /***
17 * Copyright (C) 1999 Walter Bogaardt
18 *
19 * This library is free software; you can redistribute it and/or
20 * modify it under the terms of the GNU Lesser General Public
21 * License as published by the Free Software Foundation; either
22 * version 2 of the License, or (at your option) any later version.
23 *
24 * This library is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 * Lesser General Public License for more details.
28 *
29 * You should have received a copy of the GNU Lesser General Public
30 * License along with this library; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
32 *
33 * Project: Alice X10 Home Automation
34 *
35 * GUI element that holds the user changable elements for connection
36 * configuration.
37 *
38 * Date: Oct 9, 2003
39 * Time: 5:22:14 PM
40 *
41 * $Log: SerialConfigurationPanel.java,v $
42 * Revision 1.16 2004/07/22 02:38:37 wbogaardt
43 * fixed serial panel configuration launch.
44 *
45 * Revision 1.15 2004/06/09 18:52:34 wbogaardt
46 * changed enum to enumr to avoid java keyword issue
47 *
48 * Revision 1.14 2004/02/03 21:02:24 wbogaardt
49 * moved DeviceFactory away from rmi creation and simplified interface between gateway
50 *
51 * Revision 1.13 2004/02/03 00:35:06 wbogaardt
52 * reogranized files so that serial port settings are maintained in a bean to interface with config file
53 *
54 * Revision 1.12 2004/02/01 20:03:20 wbogaardt
55 * removed reference to the Form layout and added javadoc comments to privat methods
56 *
57 * Revision 1.11 2003/12/31 01:09:37 wbogaardt
58 * removed dead string reference to jhome.prop
59 *
60 * Revision 1.10 2003/12/30 18:47:40 wbogaardt
61 * made labels so they are internationlized and fixed layout of trigger panel
62 *
63 * Revision 1.9 2003/12/22 20:51:29 wbogaardt
64 * refactored name assignments and formatted code for readability.
65 *
66 * Revision 1.8 2003/12/20 20:13:01 wbogaardt
67 * modified formating and some names for labels
68 *
69 * Revision 1.7 2003/12/20 06:16:00 wbogaardt
70 * moved most buttons text to i18n internationalization.
71 *
72 * Revision 1.6 2003/12/18 00:02:50 wbogaardt
73 * more javadoc comments
74 *
75 * Revision 1.5 2003/12/17 21:27:06 wbogaardt
76 * Improved error traping of CM11A send command to all house
77 *
78 * Revision 1.4 2003/12/12 23:17:33 wbogaardt
79 * javadoc comments refactored methods so they are more descriptive
80 *
81 * Revision 1.3 2003/12/11 23:10:07 wbogaardt
82 * cleaned up exception handeling and logging of system.out messages
83 *
84 * Revision 1.2 2003/10/11 05:06:52 wbogaardt
85 * modified connect and disconnect button to be one button for simplification of ui
86 *
87 * Revision 1.1 2003/10/10 00:50:43 wbogaardt
88 * decoupled the ConfigurationPanel to SerialConfigurationPanel
89 *
90 */
91 public class SerialConfigurationPanel extends JPanel implements X10DeviceConstants {
92
93 private JComboBox interfaceCombo;
94 private JButton jbPortConnection;
95 private JButton jbConfigure;
96 private String[] sDeviceTypeArray = {"CM11A", "CM17A"};
97 private SerialPanel spSerialPanel;
98 private MessageInterface msgInterface;
99
100 /***
101 * Creates and initilizes the configuration panel. The initial settings
102 * are from the parameters object.
103 * @param panel the panel instance where this will sit.
104 */
105 public SerialConfigurationPanel(SerialPanel panel)
106 {
107 msgInterface = AutoHomeAdminSession.getInstance().getMessageInterface();
108 this.spSerialPanel = panel;
109 jbConfigure = new JButton(AutoHomeAdminSession.getInstance().getI18n().getString(I18nConstants.CONFIGURE_BUTTON));
110 if(!AutoHomeAdminSession.getInstance().isX10GatwayConnected())
111 {
112 jbPortConnection = new JButton(AutoHomeAdminSession.getInstance().getI18n().getString(I18nConstants.CONNECT_BUTTON));
113 }
114 else
115 {
116 jbPortConnection = new JButton(AutoHomeAdminSession.getInstance().getI18n().getString(I18nConstants.DISCONNECT_BUTTON));
117 }
118 interfaceCombo = new JComboBox(sDeviceTypeArray);
119 this.setBorder(BorderFactory.
120 createTitledBorder
121 (BorderFactory.createLineBorder(Color.black),AutoHomeAdminSession.getInstance().getI18n().getString(I18nConstants.SERIAL_PORT_TITLE_LABEL)));
122 JPanel configPanel = new JPanel();
123 configPanel.add(jbConfigure);
124 configPanel.add(new JLabel(AutoHomeAdminSession.getInstance().getI18n().getString(I18nConstants.INTERFACE_TYPE_LABEL)));
125 configPanel.add(interfaceCombo);
126 configPanel.add(jbPortConnection);
127 this.setupListeners();
128 this.add(configPanel);
129 }
130
131 /***
132 * mutator to set the combo box for port settings
133 * to the value previously saved by the user
134 */
135 public void setPortValue()
136 {
137 if(AutoHomeAdminSession.getInstance().getX10GatewayType() == CM11A_TRANSMITTER)
138 {
139 interfaceCombo.setSelectedIndex(0);
140 spSerialPanel.timePanelEnable(true);
141 }
142 if(AutoHomeAdminSession.getInstance().getX10GatewayType() == CM17A_TRANSMITTER)
143 {
144 interfaceCombo.setSelectedIndex(1);
145 spSerialPanel.timePanelEnable(false);
146 }
147 }
148
149 /***
150 * Get the currently select interface name from the JComboBox
151 * @return the integer value of the interface type.
152 */
153 public int getInterfaceType()
154 {
155 if (((String)interfaceCombo.getSelectedItem()).startsWith("CM11A"))
156 {
157 return CM11A_TRANSMITTER;
158 }
159 else
160 {
161 return CM17A_TRANSMITTER;
162 }
163 }
164
165 /***
166 * This returns and instance of this panel so that outside components
167 * can have reference to this instance.
168 * @return The instance of this panel.
169 */
170 private SerialConfigurationPanel getPanel(){
171 return this;
172 }
173
174 /***
175 * This setsup the action listeners to the buttons and drop downs
176 * so that the serial port can be enabled based on the user's action.
177 */
178 private void setupListeners()
179 {
180 ActionListener al = new ActionListener(){
181 public void actionPerformed(ActionEvent evt)
182 {
183 Object src = evt.getSource();
184 if (src == jbPortConnection)
185 {
186 if (AutoHomeAdminSession.getInstance().isX10GatwayConnected())
187 {
188 try
189 {
190 printMessage("Closing port");
191 AutoHomeAdminSession.getInstance().closeSerialPort();
192 jbPortConnection.setText(AutoHomeAdminSession.getInstance().getI18n().getString(I18nConstants.CONNECT_BUTTON));
193 }
194 catch(HomeException err)
195 {
196 printMessage("Failed");
197 jbPortConnection.setText(AutoHomeAdminSession.getInstance().getI18n().getString(I18nConstants.CONNECT_BUTTON));
198 printMessage(err.toString());
199 }
200 }
201 else
202 {
203 if(connectToSerialPort())
204 {
205 spSerialPanel.setPort();
206 jbPortConnection.setText(AutoHomeAdminSession.getInstance().getI18n().getString(I18nConstants.DISCONNECT_BUTTON));
207 }
208 }
209 }
210 if (src == interfaceCombo)
211 {
212 if (interfaceCombo.getSelectedIndex() == 0)
213 {
214 spSerialPanel.timePanelEnable(true);
215 }
216 else if (interfaceCombo.getSelectedIndex() == 1)
217 {
218 spSerialPanel.timePanelEnable(false);
219 }
220 }
221 if (src == jbConfigure)
222 {
223 Frame fr = GuiLib.getFrameForComponent(getPanel());
224 UIEditorDialog editor;
225 if ((fr != null) && (fr instanceof JFrame))
226 {
227 JComponent rootC = ((JFrame)fr).getRootPane();
228 editor = new UIEditorDialog(rootC);
229 editor.setTitle("A.L.I.C.E Global Settings");
230 }
231 else
232 {
233 new UIEditorDialog();
234 }
235 editor = new UIEditorDialog();
236 editor.setLocationRelativeTo(SerialConfigurationPanel.this);
237 editor.setVisible(true);
238 }
239 }
240 };
241 jbPortConnection.addActionListener(al);
242 jbConfigure.addActionListener(al);
243 interfaceCombo.addActionListener(al);
244 }
245
246 /***
247 * Connects the session object to the serial port.
248 *
249 * @return true indicates success.
250 */
251 private boolean connectToSerialPort(){
252 SerialPortBean serialBean = AutoHomeAdminSession.getInstance().getSerialPort();
253
254 if (serialBean.getPort() != null)
255 {
256 try
257 {
258 AutoHomeAdminSession.getInstance().connectPortToDevice(this.getInterfaceType());
259 }
260 catch(HomeException err)
261 {
262 JOptionPane.showMessageDialog(null,"There appears to be an issue with \n"+
263 "your X10 interface. Check your port \n"+
264 "settings in the Global Options Menu.",
265 "Missing Port ID",JOptionPane.WARNING_MESSAGE);
266 return false;
267 }
268 }
269 else
270 {
271 JOptionPane.showMessageDialog(null,"There appears to be an issue with \n"+
272 "your X10 interface. Check your port \n"+
273 "settings in the Global Options Menu.",
274 "Missing Port ID",JOptionPane.WARNING_MESSAGE);
275 return false;
276 }
277 return true;
278 }
279
280 /***
281 * sends text information to the application statatus bar
282 * @param info the string to send to interface.
283 */
284 private void printMessage(String info)
285 {
286 if (msgInterface != null)
287 msgInterface.printMessage(info);
288 }
289 }