KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > him > metadata > range > swing > booleanhandling > BooleanValuePanel


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.him.metadata.range.swing.booleanhandling;
20
21 import java.awt.*;
22 import java.awt.event.*;
23
24 import javax.swing.*;
25
26 import org.openharmonise.him.metadata.range.swing.*;
27 import org.openharmonise.vfs.gui.*;
28 import org.openharmonise.vfs.metadata.*;
29 import org.openharmonise.vfs.metadata.range.*;
30
31
32 /**
33  * Component to display boolean property values in the metadata window.
34  *
35  * @author Matthew Large
36  * @version $Revision: 1.1 $
37  *
38  */

39 public class BooleanValuePanel
40     extends AbstractRangeDisplay
41     implements RangeDisplay, LayoutManager, ActionListener {
42
43     /**
44      * Height of this component.
45      */

46     protected int m_nHeight = 10;
47
48     /**
49      * Combobox component containing the yes or no options.
50      */

51     private JComboBox m_comboBox = null;
52
53     /**
54      * Warnings label for this component.
55      */

56     protected WarningsLabel m_warnings = null;
57
58     /**
59      * Error label for this component.
60      */

61     protected JLabel m_errorLabel = null;
62
63     /**
64      * String value for this component.
65      */

66     private String JavaDoc m_sValue = "";
67     
68     /**
69      * Previous string value for this component.
70      */

71     private String JavaDoc m_sPreviousValue = "";
72
73     /**
74      * Parent range display component.
75      */

76     private BooleanRangeDisplay m_display = null;
77     
78     private String JavaDoc m_sTrueText = "Yes";
79     private String JavaDoc m_sFalseText = "No";
80     
81     /**
82      * Constructs a new boolean value panel.
83      *
84      * @param display Parent range display component
85      * @param propInstance Property instance that this component is displaying a value for
86      * @param sValue String value for this component
87      */

88     public BooleanValuePanel(BooleanRangeDisplay display, PropertyInstance propInstance, String JavaDoc sValue) {
89         super(propInstance);
90         this.m_display = display;
91         this.m_sValue = sValue;
92         if(sValue.equals("true")) {
93             this.m_sPreviousValue = "Yes";
94         } else if(sValue.equals("false")) {
95             this.m_sPreviousValue = "No";
96         } else {
97             this.m_sPreviousValue = "";
98         }
99         this.setup();
100     }
101
102     /* (non-Javadoc)
103      * @see com.simulacramedia.contentmanager.metadata.range.swing.RangeDisplay#getPanel()
104      */

105     public void setup() {
106         this.setLayout(this);
107         BooleanRange range =
108             (BooleanRange) this
109                 .getPropertyInstance()
110                 .getDefinition()
111                 .getRange();
112
113         this.m_sTrueText = range.getTrueText();
114         this.m_sFalseText = range.getFalseText();
115         
116         if(this.m_sValue.equals("true")) {
117             this.m_sPreviousValue = this.m_sTrueText;
118         } else if(this.m_sValue.equals("false")) {
119             this.m_sPreviousValue = this.m_sFalseText;
120         } else {
121             this.m_sPreviousValue = "";
122         }
123         
124         this.m_errorLabel = new JLabel();
125         this.m_errorLabel.setIcon(
126             IconManager.getInstance().getIcon("16-message-confirm.gif"));
127         this.add(this.m_errorLabel);
128
129         String JavaDoc sWarning = "Please select a value.";
130
131         m_warnings = new WarningsLabel(sWarning);
132         this.add(m_warnings);
133
134         m_comboBox = new JComboBox( new String JavaDoc[]{"", this.m_sTrueText, this.m_sFalseText} );
135         if(this.m_sValue!=null && this.m_sValue.equals("true")) {
136             m_comboBox.setSelectedItem(this.m_sTrueText);
137         } else if(this.m_sValue!=null && this.m_sValue.equals("false")) {
138             m_comboBox.setSelectedItem(this.m_sFalseText);
139         }
140         m_comboBox.setActionCommand("COMBO");
141         m_comboBox.addActionListener(this);
142         int nHeight = 30;
143         m_nHeight = nHeight + 20;
144         m_comboBox.setPreferredSize(new Dimension(200, 20));
145         this.add(m_comboBox);
146
147     }
148
149     /* (non-Javadoc)
150      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
151      */

152     public void actionPerformed(ActionEvent ae) {
153         if(ae.getActionCommand().equals("COMBO")) {
154             String JavaDoc sCurrentValue =(String JavaDoc) this.m_comboBox.getSelectedItem();
155             if(sCurrentValue.equals("")) {
156                 this.m_display.valueChanged("");
157             } else if(sCurrentValue.equals( this.m_sTrueText )) {
158                 this.m_display.valueChanged("true");
159                 this.m_sPreviousValue = sCurrentValue;
160             } else if(sCurrentValue.equals( this.m_sFalseText )) {
161                 this.m_display.valueChanged("false");
162                 this.m_sPreviousValue = sCurrentValue;
163             }
164         }
165     }
166
167     /* (non-Javadoc)
168      * @see java.awt.Component#getPreferredSize()
169      */

170     public Dimension getPreferredSize() {
171         this.layoutContainer(null);
172         int nWidth = this.getParent().getWidth();
173         int nHeight = 10;
174         nHeight = nHeight + this.m_comboBox.getSize().height;
175         return new Dimension(nWidth, nHeight);
176     }
177
178     /* (non-Javadoc)
179      * @see java.awt.LayoutManager#removeLayoutComponent(java.awt.Component)
180      */

181     public void removeLayoutComponent(Component arg0) {
182         // NO-OP
183
}
184
185     /* (non-Javadoc)
186      * @see java.awt.LayoutManager#layoutContainer(java.awt.Container)
187      */

188     public void layoutContainer(Container arg0) {
189         int nWidth = this.getParent().getWidth() - 50;
190
191         Dimension dim = m_comboBox.getPreferredSize();
192         m_comboBox.setPreferredSize(new Dimension(nWidth - 85, dim.height));
193         m_comboBox.setSize(m_comboBox.getPreferredSize());
194         m_comboBox.setLocation(20, 0);
195
196         this.m_errorLabel.setLocation(0,0);
197         this.m_errorLabel.setSize(15, 15);
198         this.repaint();
199     }
200
201     /* (non-Javadoc)
202      * @see java.awt.LayoutManager#addLayoutComponent(java.lang.String, java.awt.Component)
203      */

204     public void addLayoutComponent(String JavaDoc arg0, Component arg1) {
205         // NO-OP
206
}
207
208     /* (non-Javadoc)
209      * @see java.awt.LayoutManager#minimumLayoutSize(java.awt.Container)
210      */

211     public Dimension minimumLayoutSize(Container arg0) {
212         return this.getPreferredSize();
213     }
214
215     /* (non-Javadoc)
216      * @see java.awt.LayoutManager#preferredLayoutSize(java.awt.Container)
217      */

218     public Dimension preferredLayoutSize(Container arg0) {
219         return this.getPreferredSize();
220     }
221
222     /* (non-Javadoc)
223      * @see com.simulacramedia.contentmanager.metadata.range.swing.RangeDisplay#getPanel()
224      */

225     public JPanel getPanel() {
226         return null;
227     }
228     
229     /**
230      * Returns the string value for this component.
231      *
232      * @return Value for this component
233      */

234     protected String JavaDoc getValue() {
235         if(this.m_comboBox.getSelectedItem().equals( this.m_sTrueText )) {
236             return "true";
237         } else if(this.m_comboBox.getSelectedItem().equals( this.m_sFalseText )) {
238             return "false";
239         } else {
240             return "";
241         }
242     }
243
244     /* (non-Javadoc)
245      * @see com.simulacramedia.contentmanager.metadata.range.swing.AbstractRangeDisplay#isValid()
246      */

247     public boolean isMetadataValid() {
248         return !this.m_errorLabel.getIcon().toString().equals(IconManager.getInstance().getIcon("16-message-error.gif").toString());
249     }
250     
251     /**
252      * Sets whether this component's value is valid.
253      *
254      * @param bValid true to set this component's value as valid
255      */

256     public void setValid(boolean bValid) {
257         if(!bValid) {
258             this.m_comboBox.setBorder(BorderFactory.createLineBorder(Color.RED));
259             this.m_errorLabel.setIcon(IconManager.getInstance().getIcon("16-message-error.gif"));
260         } else {
261             this.m_comboBox.setBorder(BorderFactory.createLineBorder(Color.BLACK));
262             this.m_errorLabel.setIcon(IconManager.getInstance().getIcon("16-message-confirm.gif"));
263         }
264         this.revalidate();
265         this.repaint();
266     }
267
268 }
269
Popular Tags