View Javadoc

1   package org.wcb.autohome;
2   /***
3    * Copyright (C) 1999  Walter Bogaardt
4    *
5    * This library is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU Lesser General Public
7    * License as published by the Free Software Foundation; either
8    * version 2 of the License, or (at your option) any later version.
9    *
10   * This library is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   * Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public
16   * License along with this library; if not, write to the Free Software
17   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
18   *
19   * Project: Alice X10 Home Automation
20   *
21   *  $Log: ModuleDetailPanel.java,v $
22   *  Revision 1.14  2004/07/22 03:17:54  wbogaardt
23   *  removed deprecated methods
24   *
25   *  Revision 1.13  2004/01/31 07:40:23  wbogaardt
26   *  modified layout managers for the panel so that it uses the java standard layoutsrather than the FormLayout api. The purpose is to reduce dependency on non-standard layout api.
27   *
28   *  Revision 1.12  2004/01/19 17:54:16  wbogaardt
29   *  initial fixed monitor
30   *
31   *  Revision 1.11  2004/01/18 05:27:57  wbogaardt
32   *  fixed adding and deleting function of Monitor panel, and saving information
33   *
34   *  Revision 1.10  2004/01/16 19:50:14  wbogaardt
35   *  refactored, fixed long standing bug with updating macro panels, add error notification to user for improper device codes
36   *
37   *  Revision 1.9  2004/01/16 00:53:34  wbogaardt
38   *  Fixed a very obscure bug with the Macro Panel that it didn't added new
39   *  x10 devices to the drop down of available x10 device for the macro. Modified Macro triggers to change the events to integer verses strings cleaner this way.
40   *
41   *  Revision 1.8  2004/01/15 21:05:17  wbogaardt
42   *  major revamp of Modules and interfaces changes overall structure of how information is stored
43   *
44   *  Revision 1.7  2003/12/30 21:20:16  wbogaardt
45   *  added new file saving dialog to actually save files in .x10 extension.
46   *
47   *  Revision 1.6  2003/12/30 18:47:40  wbogaardt
48   *  made labels so they are internationlized and fixed layout of trigger panel
49   *
50   *  Revision 1.5  2003/12/22 20:51:29  wbogaardt
51   *  refactored name assignments and formatted code for readability.
52   *
53   *  Revision 1.4  2003/12/20 06:16:00  wbogaardt
54   *  moved most buttons text to i18n internationalization.
55   *
56   *  Revision 1.3  2003/12/11 23:10:07  wbogaardt
57   *  cleaned up exception handeling and logging of system.out messages
58   *
59   *  Revision 1.2  2003/10/09 23:55:21  wbogaardt
60   *  code clean up of formating and adding revision history to comments
61   *
62   */
63  
64  import javax.swing.*;
65  import java.awt.*;
66  import java.awt.event.ActionListener;
67  import java.awt.event.ActionEvent;
68  import org.wcb.autohome.implementations.X10Module;
69  import org.wcb.autohome.interfaces.X10DeviceConstants;
70  import org.wcb.autohome.interfaces.IX10Module;
71  import org.wcb.autohome.util.DeviceIDFieldValidator;
72  import org.wcb.autohome.interfaces.I18nConstants;
73  
74  
75  public class ModuleDetailPanel extends JPanel implements ActionListener, X10DeviceConstants{
76  
77      private JButton jbAdd, jbUpdate, jbDelete;
78      private JTextField jtfDescription, jtfName, jtfModuleID;
79      private ModulePanel stepDad;
80      private JComboBox jcbDeviceTypeImage;
81  
82      public ModuleDetailPanel(ModulePanel modPanel) {
83          stepDad = modPanel;
84          setupComponents();
85      }
86  
87      private void setupComponents()
88      {
89          setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black), "Device Detail"));
90  
91         // FormPanel formPanel = new FormPanel(4,4,5,5);
92          this.jcbDeviceTypeImage = new JComboBox();
93          this.jcbDeviceTypeImage.addItem(AutoHomeAdminSession.LIGHTICON);
94          this.jcbDeviceTypeImage.addItem(AutoHomeAdminSession.APPLIANCEICON);
95  
96          this.jbAdd = new JButton(AutoHomeAdminSession.getInstance().getI18n().getString(I18nConstants.ADD_BUTTON));
97          this.jbUpdate = new JButton(AutoHomeAdminSession.getInstance().getI18n().getString(I18nConstants.UPDATE_BUTTON));
98          this.jbDelete = new JButton(AutoHomeAdminSession.getInstance().getI18n().getString(I18nConstants.DELETE_BUTTON));
99          this.jtfDescription = new JTextField(15);
100         this.jtfName = new JTextField(15);
101         this.jtfModuleID = new JTextField(4);
102         this.jtfModuleID.setDocument(new DeviceIDFieldValidator());
103 
104         GridBagConstraints gridBagConstraints;
105 
106         JPanel jpFormPanel = new JPanel();
107         jpFormPanel.setLayout(new GridBagLayout());
108 
109 
110         gridBagConstraints = new GridBagConstraints();
111         gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
112         jpFormPanel.add(this.jcbDeviceTypeImage, gridBagConstraints);
113 
114         gridBagConstraints = new GridBagConstraints();
115         gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
116         jpFormPanel.add(new JLabel("ID"), gridBagConstraints);
117 
118         gridBagConstraints = new GridBagConstraints();
119         gridBagConstraints.anchor = GridBagConstraints.WEST;
120         jpFormPanel.add(this.jtfModuleID, gridBagConstraints);
121 
122         gridBagConstraints = new GridBagConstraints();
123         gridBagConstraints.gridx = 0;
124         gridBagConstraints.gridy = 1;
125         gridBagConstraints.gridwidth = 2;
126         jpFormPanel.add(new JLabel(AutoHomeAdminSession.getInstance().getI18n().getString(I18nConstants.MODULE_ID_COLUMN)), gridBagConstraints);
127 
128         gridBagConstraints = new GridBagConstraints();
129         gridBagConstraints.gridx = 2;
130         gridBagConstraints.gridy = 1;
131         gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
132         jpFormPanel.add(jtfName, gridBagConstraints);
133 
134         gridBagConstraints = new GridBagConstraints();
135         gridBagConstraints.gridx = 0;
136         gridBagConstraints.gridy = 2;
137         gridBagConstraints.gridwidth = 2;
138         gridBagConstraints.anchor = GridBagConstraints.WEST;
139         jpFormPanel.add(new JLabel(AutoHomeAdminSession.getInstance().getI18n().getString(I18nConstants.DESCRIPTION_LABEL)), gridBagConstraints);
140 
141         gridBagConstraints = new GridBagConstraints();
142         gridBagConstraints.gridx = 2;
143         gridBagConstraints.gridy = 2;
144         gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
145         jpFormPanel.add(this.jtfDescription, gridBagConstraints);
146 
147         JPanel jpButtons = new JPanel();
148         jpButtons.add(this.jbAdd);
149         jpButtons.add(this.jbUpdate);
150         jpButtons.add(this.jbDelete);
151 
152         gridBagConstraints = new GridBagConstraints();
153         gridBagConstraints.gridx = 0;
154         gridBagConstraints.gridy = 3;
155         gridBagConstraints.gridwidth = 3;
156         gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
157         jpFormPanel.add(jpButtons, gridBagConstraints);
158         add(BorderLayout.CENTER, jpFormPanel);
159 
160         jbAdd.addActionListener(this);
161         jbUpdate.addActionListener(this);
162         jbDelete.addActionListener(this);
163     }
164 
165     /***
166      * UPdates the ModulePanel with the new installed
167      * x10 device.
168      * @param module X10 Device information that is being updated.
169      */
170     public void updateView(IX10Module module)
171     {
172         clearAllViews();
173         switch(module.getType())
174         {
175             case LAMP_MODULE_ON:
176                 this.jcbDeviceTypeImage.setSelectedIndex(0);
177                 break;
178             case APPLIANCE_MODULE_ON:
179                 this.jcbDeviceTypeImage.setSelectedIndex(1);
180                 break;
181         }
182         this.jtfModuleID.setText(module.getFullDeviceCode());
183         this.jtfDescription.setText(module.getDescription());
184         this.jtfName.setText(module.getName());
185     }
186 
187     /***
188      * Set up action listener to certaion buttons.
189      */
190     public void actionPerformed(ActionEvent evt)
191     {
192 
193         Object src = evt.getSource();
194         if(src == jbAdd)
195         {
196             try
197             {
198                 stepDad.addNewRow(saveDetailData());
199                 clearAllViews();
200             } catch(Exception err)
201             {
202                 //nothing should be thrown
203             }
204         }
205         if(src == jbUpdate)
206         {
207             stepDad.updateRow(saveDetailData());
208         }
209         if(src == jbDelete)
210         {
211             stepDad.deleteRow();
212             clearAllViews();
213         }
214 
215     }
216 
217     /***
218      * Clear the text fields if nothing in
219      * the panel is selected.
220      */
221     private void clearAllViews()
222     {
223         this.jtfDescription.setText("");
224         this.jtfName.setText("");
225         this.jtfModuleID.setText("");
226     }
227 
228     /***
229      * Using a switch statement based on the device
230      * type combo box set the X10Module device type to
231      * either an appliance or lamp module identify.
232      *
233      * @return Interface of X10Module
234      */
235     private IX10Module saveDetailData()
236     {
237         switch(this.jcbDeviceTypeImage.getSelectedIndex()) {
238             case 0:
239                 return new X10Module(getHouseCode(), getDeviceCode(), jtfName.getText(), jtfDescription.getText(), LAMP_MODULE_ON);
240             case 1:
241                 return new X10Module(getHouseCode(), getDeviceCode(), jtfName.getText(), jtfDescription.getText(), APPLIANCE_MODULE_ON);
242             default:
243                 return new X10Module();
244         }
245     }
246 
247     /***
248      * Gets the first position in the DeviceID text field
249      * and returns a Upper Case Character value.
250      * @return defaults to A if not parsed correctly.
251      */
252     private char getHouseCode()
253     {
254         char returnValue = 'A';
255         try
256         {
257             returnValue = jtfModuleID.getText().charAt(0);
258         }
259         catch(IndexOutOfBoundsException ioobe)
260         {
261             returnValue = 'A';
262         }
263         return returnValue;
264     }
265 
266     /***
267      * Gets the last positions in the DeviceID text field
268      * and returns an integer value.  If the entered integers
269      * are greater than 16 then 16 is returned;
270      * @return defaults to 1 if not parsed correctly;
271      */
272     private int getDeviceCode()
273     {
274         int returnValue = 1;
275         try
276         {
277             returnValue = Integer.parseInt(jtfModuleID.getText().substring(1));
278         }
279         catch(NumberFormatException nfe)
280         {
281             returnValue = 1;
282         }
283         if (returnValue > 16)
284         {
285             JOptionPane.showMessageDialog(null,"The maximum number device id can be is 16.\n Either update the module with a " +
286                     "\nlower number or keep what Alice has defaulted.","Bad Device code",JOptionPane.ERROR_MESSAGE);
287             returnValue = 16;
288         }
289         return returnValue;
290     }
291 }
292 
293 
294 
295 
296