KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ungoverned > oscar > installer > editor > BooleanStringEditor


1 /*
2  * Oscar - An implementation of the OSGi framework.
3  * Copyright (c) 2004, Richard S. Hall
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  * * Neither the name of the ungoverned.org nor the names of its
17  * contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * Contact: Richard S. Hall (heavy@ungoverned.org)
33  * Contributor(s):
34  *
35 **/

36 package org.ungoverned.oscar.installer.editor;
37
38 import java.awt.GridBagConstraints JavaDoc;
39 import java.awt.GridBagLayout JavaDoc;
40 import java.awt.Insets JavaDoc;
41 import java.awt.event.*;
42
43 import javax.swing.*;
44
45 import org.ungoverned.oscar.installer.BooleanProperty;
46 import org.ungoverned.oscar.installer.Property;
47 import org.ungoverned.oscar.installer.StringProperty;
48
49 public class BooleanStringEditor extends JPanel
50 {
51     private Property m_prop = null;
52     private JCheckBox m_includeButton = null;
53     private JTextField m_textField = null;
54
55     public BooleanStringEditor(Property prop)
56     {
57         if ((prop instanceof BooleanProperty) && (prop instanceof StringProperty))
58         {
59             m_prop = prop;
60         }
61         else
62         {
63             throw new IllegalArgumentException JavaDoc(
64                 "Property must implement both boolean and string property interfaces.");
65         }
66         init();
67     }
68
69     public Property getProperty()
70     {
71         return m_prop;
72     }
73
74     public void setEnabled(boolean b)
75     {
76         m_includeButton.setEnabled(b);
77         m_textField.setEnabled(b && m_includeButton.isSelected());
78     }
79
80     protected void init()
81     {
82         // Set layout.
83
GridBagLayout JavaDoc grid = new GridBagLayout JavaDoc();
84         GridBagConstraints JavaDoc gbc = new GridBagConstraints JavaDoc();
85         gbc.insets = new Insets JavaDoc(0, 2, 0, 2);
86         setLayout(grid);
87
88         // Add button.
89
gbc.gridx = 0;
90         gbc.gridy = 0;
91         gbc.gridheight = 1;
92         gbc.gridwidth = 1;
93         gbc.anchor = GridBagConstraints.WEST;
94         m_includeButton = new JCheckBox("");
95         grid.setConstraints(m_includeButton, gbc);
96         add(m_includeButton);
97
98         // Add field.
99
gbc.gridx = 1;
100         gbc.gridy = 0;
101         gbc.gridheight = 1;
102         gbc.gridwidth = 3;
103         gbc.anchor = GridBagConstraints.WEST;
104         m_textField = new JTextField(30);
105         m_textField.setText(((StringProperty) m_prop).getStringValue());
106         grid.setConstraints(m_textField, gbc);
107         add(m_textField);
108         m_textField.setEnabled(false);
109
110         // Add action listener.
111
m_includeButton.addActionListener(new ActionListener() {
112             public void actionPerformed(ActionEvent event)
113             {
114                 if (m_includeButton.isSelected())
115                 {
116                     ((BooleanProperty) m_prop).setBooleanValue(true);
117                     m_textField.setEnabled(true);
118                 }
119                 else
120                 {
121                     ((BooleanProperty) m_prop).setBooleanValue(false);
122                     m_textField.setEnabled(false);
123                 }
124             }
125         });
126
127         // Add focus listener.
128
m_textField.addFocusListener(new FocusListener() {
129             public void focusGained(FocusEvent event)
130             {
131             }
132             public void focusLost(FocusEvent event)
133             {
134                 if (!event.isTemporary())
135                 {
136                     ((StringProperty) m_prop).setStringValue(m_textField.getText());
137                 }
138             }
139         });
140
141         // Currently, the button is not selected. If the property
142
// is true, then click once to select button.
143
if (((BooleanProperty) m_prop).getBooleanValue())
144         {
145             m_includeButton.doClick();
146         }
147     }
148 }
Popular Tags