View Javadoc

1   package org.wcb.common;
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:   Home Automation Interface
20   *
21   *  Abstract:  User Interface options dialog box
22   *
23   */ 
24  import javax.swing.*;
25  import java.awt.event.ActionListener;
26  import java.awt.event.ActionEvent;
27  
28  import java.awt.Color;
29  import java.awt.Font;
30  import java.awt.BorderLayout;
31  import java.util.Properties;
32  import java.util.Enumeration;
33  import java.util.StringTokenizer;
34  
35  import org.wcb.autohome.AutoHomeAdminSession;
36  import org.wcb.autohome.interfaces.X10DeviceConstants;
37  import org.wcb.autohome.interfaces.I18nConstants;
38  import org.wcb.common.component.ColorEditorPanel;
39  import org.wcb.common.component.FontEditorPanel;
40  import org.wcb.common.component.EmailPanel;
41  import org.wcb.common.component.GeneralConfigurationPanel;
42  
43  /***
44   *This is a dialog box that allows users to make modifications
45   *to the interface ui, coloring, or the type of x10 device
46   *connected to the application. These variables are stored in a 
47   *file called jhome.prop which is located in the user's home directory
48   */
49  public class UIEditorDialog extends JDialog implements X10DeviceConstants{
50  
51      private UIDefaultsLoader swingProperties = null;
52      private JButton saveButton, cancelButton, resetButton;
53      private JComponent rootComponent = null;
54      private ColorEditorPanel colorPanel;
55      private FontEditorPanel fontPanel;
56      private EmailPanel emailPanel;
57      private GeneralConfigurationPanel configPanel;
58  
59      public UIEditorDialog() {
60          this(null);
61      }
62  
63      public UIEditorDialog(JComponent rootC){
64          super.setModal(true);
65           rootComponent = rootC;
66          /* read the swing properties settings */
67          swingProperties = AutoHomeAdminSession.getInstance().getAppProperties();
68          this.setSystemProperties(swingProperties.getProperties());
69          this.setupComponents();
70          this.setupListeners();
71          if (null == rootComponent) rootComponent = (JComponent)getContentPane();
72      }
73  
74      public void setSystemProperties(Properties propsToSet){
75          if (null == propsToSet) return;
76          Enumeration enumr = propsToSet.keys();
77          Object nextElement;
78          Object value;
79          while (enumr.hasMoreElements()) {
80              nextElement = enumr.nextElement();
81              value = propsToSet.get(nextElement);
82              if (null != value) {
83                  if (isColor(value.toString())) {
84                      Color cValue = convertToColor(value.toString());
85                      if (null != cValue) UIManager.put(nextElement, cValue);
86                  }
87                  if (isFont(value.toString())) {
88                      Font fValue = convertToFont(value.toString());
89                      if(null != fValue) UIManager.put(nextElement, fValue);
90                  }
91              }
92          }
93      }
94  
95      private void setupComponents(){
96          JTabbedPane tabs = new JTabbedPane();
97  
98          /* add different panels to the tabs */
99          configPanel = new GeneralConfigurationPanel();
100         tabs.add(AutoHomeAdminSession.getInstance().getI18n().getString(I18nConstants.GENERAL_PANEL), configPanel);
101         emailPanel = new EmailPanel();
102         tabs.add(AutoHomeAdminSession.getInstance().getI18n().getString(I18nConstants.EMAIL_PANEL), emailPanel);
103         colorPanel = new ColorEditorPanel();
104         tabs.add(AutoHomeAdminSession.getInstance().getI18n().getString(I18nConstants.COLOR_PANEL), colorPanel);
105         fontPanel = new FontEditorPanel();
106         tabs.add(AutoHomeAdminSession.getInstance().getI18n().getString(I18nConstants.FONT_PANEL), fontPanel);
107 
108         /* put it together and what do you have ? */
109         getContentPane().add(tabs, BorderLayout.CENTER);
110         getContentPane().add(this.createSaveButtonsPanel(), BorderLayout.SOUTH);
111         setSize(375,325);
112     }
113 
114     private JPanel createSaveButtonsPanel()
115     {
116         /* bottom button panel */
117         JPanel jpButtonPanel= new JPanel();
118         jpButtonPanel.setLayout(new BoxLayout(jpButtonPanel, BoxLayout.X_AXIS));
119         saveButton = new JButton(AutoHomeAdminSession.getInstance().getI18n().getString(I18nConstants.SAVE_BUTTON));
120         cancelButton = new JButton(AutoHomeAdminSession.getInstance().getI18n().getString(I18nConstants.CANCEL_BUTTON));
121         resetButton = new JButton(AutoHomeAdminSession.getInstance().getI18n().getString(I18nConstants.RESET_BUTTON));
122         jpButtonPanel.add(saveButton);
123         jpButtonPanel.add(cancelButton);
124         jpButtonPanel.add(resetButton);
125         return jpButtonPanel;
126     }
127 
128     private void setupListeners() {
129         ActionListener al = new ActionListener(){
130             public void actionPerformed(ActionEvent evt) {
131                 Object src = evt.getSource();
132                 if (src == saveButton) { saveFile();}
133                 if (src == cancelButton) {dispose();}
134                 if (src == resetButton) {
135                     setGuiToLookAndFeel(AutoHomeAdminSession.getInstance().getLookAndFeel());
136                     configPanel.getLanguage();
137                 }
138             }
139         };
140         saveButton.addActionListener(al);
141         cancelButton.addActionListener(al);
142         resetButton.addActionListener(al);
143     }
144 
145     /***
146      * a basic check of the properties table
147      * if the value pair starts with the name of Color
148      * then it should be a valid color to be parsed
149      */
150     private boolean isColor(String value) 
151     {
152         boolean returnValue = false;
153         if (value.startsWith("Color")) {
154             returnValue = true;
155         }
156         return returnValue;
157     }
158 
159     private boolean isFont(String value){
160         boolean returnValue = false;
161         if(value.startsWith("Font")) {
162             returnValue = true;
163         }
164         return returnValue;
165     }
166 
167     /***
168      * converts the color either based ont the GuiLib class
169      * or changes it based on the hex value
170      */
171     private Color convertToColor(String value) 
172     {
173         Color returnValue = null;
174         value = value.substring(value.indexOf(".")+1);
175         returnValue = GuiLib.getColor(value);
176         if (null == returnValue) {
177             try {
178                 returnValue = Color.decode(value);
179             } catch (NumberFormatException e) {
180                 returnValue = Color.black;
181             }
182         }
183         return returnValue;
184     }
185 
186     private Font convertToFont(String value){
187         Font returnValue = null;
188         String family, sStyle, sSize;
189         int style, size;
190         StringTokenizer fontName=new StringTokenizer(
191                 value.substring(value.indexOf(".")+1),",");
192         family = fontName.nextToken();
193         sStyle = fontName.nextToken();
194         sSize = fontName.nextToken();
195         try {
196             style = Integer.parseInt(sStyle);
197             size = Integer.parseInt(sSize);
198             returnValue=new Font(family, style, size);
199         } catch(NumberFormatException e) {
200             returnValue = new Font("Dialog", 1, 12);
201         }
202         return returnValue;
203     }
204 
205     /***
206      * This method sets the look and feel schema to 
207      * the user selected option
208      */
209     private void setGuiToLookAndFeel(String lf) {
210         try {
211             UIManager.setLookAndFeel(lf);
212 
213         }catch(Exception ex) {
214             JOptionPane.showMessageDialog(this,"Look & feel\n"+lf+
215                     "\n not available for your system",
216                     "Look and Feel", JOptionPane.ERROR_MESSAGE);
217         }
218         SwingUtilities.updateComponentTreeUI(getContentPane());
219     }
220 
221     private void saveFile() {
222         emailPanel.saveSettings();
223         configPanel.saveGeneral(swingProperties);
224         AutoHomeAdminSession.getInstance().saveServerFile();
225 
226         /* update all components after saving to reflect change */
227         if (rootComponent != null) {
228             SwingUtilities.updateComponentTreeUI(rootComponent);
229             rootComponent.invalidate();
230             rootComponent.validate();
231             rootComponent.repaint();
232         } else {
233             JOptionPane.showMessageDialog(this,"You must restart the application for\n"
234                     +"color changes to take effect.");
235         }
236         this.writeOutJHome();
237         AutoHomeAdminSession.getInstance().setAppProperties(swingProperties.getProperties());
238         setVisible(false);
239         dispose();
240     }
241 
242     private void writeOutJHome()
243     {
244         colorPanel.saveUIColors(swingProperties);
245         fontPanel.saveUIFonts(swingProperties);
246         /* write file out to jhome.prop */
247         swingProperties.saveProperties();
248     }
249 }
250