View Javadoc

1   package org.wcb.autohome.util;
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   *******************************************************************************/
22  
23  import org.wcb.autohome.AutoHomeAdminSession;
24  import org.wcb.autohome.interfaces.I18nConstants;
25  
26  import javax.swing.JComboBox;
27  import javax.swing.JPanel;
28  import javax.swing.JButton;
29  import javax.swing.JFrame;
30  import javax.swing.JDialog;
31  import java.awt.Component;
32  import javax.swing.ImageIcon;
33  import java.awt.BorderLayout;
34  import java.awt.event.ActionListener;
35  import java.awt.event.ActionEvent;
36  
37  public class ModuleChooser extends JPanel {
38  
39      private static String moduleString;
40      public static Component tableButton;	
41      private ImageIcon onBulb = AutoHomeAdminSession.LIGHTICON;
42      private ImageIcon onPlug = AutoHomeAdminSession.APPLIANCEICON;
43      JComboBox imgCb;
44  
45      public ModuleChooser(Component comp) {
46          JPanel display = new JPanel();
47          imgCb = new JComboBox();
48          imgCb.addItem(onBulb);
49          imgCb.addItem(onPlug);
50          moduleString = comp.getName();
51          tableButton = comp;
52          display.add(imgCb);
53          add(BorderLayout.NORTH, display);
54      }
55  
56      public static String getString() {
57          return moduleString;
58      }
59  
60      public void setString(String curString) {
61          moduleString = curString;
62      }
63  
64      /***
65       * Create a module choosing dialog box.
66       *
67       * @param c Main swing component launching this dialog box can be null
68       * @param title The title of the dialog box
69       * @param modal If it is closable or not
70       * @param mc Module chooser content.
71       * @param okListener ok button listener
72       * @param cancelListener cancel button listener
73       * @return  new instance of the dialog box.
74       */
75      public static JDialog createDialog(Component c, String title, boolean modal,
76                                         ModuleChooser mc,
77                                         ActionListener okListener,
78                                         ActionListener cancelListener) {
79          final JDialog chooserDialog = new JDialog((JFrame) null, title, modal);
80          JPanel buttonPanel = new JPanel();
81          JButton okButton = new JButton(AutoHomeAdminSession.getInstance().getI18n().getString(I18nConstants.OK_BUTTON));
82          JButton cancelButton = new JButton(AutoHomeAdminSession.getInstance().getI18n().getString(I18nConstants.CLOSE_BUTTON));
83          buttonPanel.add(okButton);
84          buttonPanel.add(cancelButton);
85          chooserDialog.getContentPane().add(BorderLayout.CENTER, mc);
86          chooserDialog.getContentPane().add(BorderLayout.SOUTH, buttonPanel);
87          okButton.addActionListener(okListener);
88          if (cancelListener == null)
89          {
90              ActionListener cl=new ActionListener() {
91                  public void actionPerformed(ActionEvent ev)
92                  {
93                      chooserDialog.dispose();
94                  }
95              };
96              cancelButton.addActionListener(cl);
97          }
98          else if(cancelListener != null)
99          {
100             cancelButton.addActionListener(cancelListener);
101         }
102         return chooserDialog;
103     }	
104 }