001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.him.metadata.range.swing.booleanhandling;
020:
021: import java.awt.*;
022: import java.awt.event.*;
023:
024: import javax.swing.*;
025:
026: import org.openharmonise.him.metadata.range.swing.*;
027: import org.openharmonise.vfs.gui.*;
028: import org.openharmonise.vfs.metadata.*;
029: import org.openharmonise.vfs.metadata.range.*;
030:
031: /**
032: * Component to display boolean property values in the metadata window.
033: *
034: * @author Matthew Large
035: * @version $Revision: 1.1 $
036: *
037: */
038: public class BooleanValuePanel extends AbstractRangeDisplay implements
039: RangeDisplay, LayoutManager, ActionListener {
040:
041: /**
042: * Height of this component.
043: */
044: protected int m_nHeight = 10;
045:
046: /**
047: * Combobox component containing the yes or no options.
048: */
049: private JComboBox m_comboBox = null;
050:
051: /**
052: * Warnings label for this component.
053: */
054: protected WarningsLabel m_warnings = null;
055:
056: /**
057: * Error label for this component.
058: */
059: protected JLabel m_errorLabel = null;
060:
061: /**
062: * String value for this component.
063: */
064: private String m_sValue = "";
065:
066: /**
067: * Previous string value for this component.
068: */
069: private String m_sPreviousValue = "";
070:
071: /**
072: * Parent range display component.
073: */
074: private BooleanRangeDisplay m_display = null;
075:
076: private String m_sTrueText = "Yes";
077: private String m_sFalseText = "No";
078:
079: /**
080: * Constructs a new boolean value panel.
081: *
082: * @param display Parent range display component
083: * @param propInstance Property instance that this component is displaying a value for
084: * @param sValue String value for this component
085: */
086: public BooleanValuePanel(BooleanRangeDisplay display,
087: PropertyInstance propInstance, String sValue) {
088: super (propInstance);
089: this .m_display = display;
090: this .m_sValue = sValue;
091: if (sValue.equals("true")) {
092: this .m_sPreviousValue = "Yes";
093: } else if (sValue.equals("false")) {
094: this .m_sPreviousValue = "No";
095: } else {
096: this .m_sPreviousValue = "";
097: }
098: this .setup();
099: }
100:
101: /* (non-Javadoc)
102: * @see com.simulacramedia.contentmanager.metadata.range.swing.RangeDisplay#getPanel()
103: */
104: public void setup() {
105: this .setLayout(this );
106: BooleanRange range = (BooleanRange) this .getPropertyInstance()
107: .getDefinition().getRange();
108:
109: this .m_sTrueText = range.getTrueText();
110: this .m_sFalseText = range.getFalseText();
111:
112: if (this .m_sValue.equals("true")) {
113: this .m_sPreviousValue = this .m_sTrueText;
114: } else if (this .m_sValue.equals("false")) {
115: this .m_sPreviousValue = this .m_sFalseText;
116: } else {
117: this .m_sPreviousValue = "";
118: }
119:
120: this .m_errorLabel = new JLabel();
121: this .m_errorLabel.setIcon(IconManager.getInstance().getIcon(
122: "16-message-confirm.gif"));
123: this .add(this .m_errorLabel);
124:
125: String sWarning = "Please select a value.";
126:
127: m_warnings = new WarningsLabel(sWarning);
128: this .add(m_warnings);
129:
130: m_comboBox = new JComboBox(new String[] { "", this .m_sTrueText,
131: this .m_sFalseText });
132: if (this .m_sValue != null && this .m_sValue.equals("true")) {
133: m_comboBox.setSelectedItem(this .m_sTrueText);
134: } else if (this .m_sValue != null
135: && this .m_sValue.equals("false")) {
136: m_comboBox.setSelectedItem(this .m_sFalseText);
137: }
138: m_comboBox.setActionCommand("COMBO");
139: m_comboBox.addActionListener(this );
140: int nHeight = 30;
141: m_nHeight = nHeight + 20;
142: m_comboBox.setPreferredSize(new Dimension(200, 20));
143: this .add(m_comboBox);
144:
145: }
146:
147: /* (non-Javadoc)
148: * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
149: */
150: public void actionPerformed(ActionEvent ae) {
151: if (ae.getActionCommand().equals("COMBO")) {
152: String sCurrentValue = (String) this .m_comboBox
153: .getSelectedItem();
154: if (sCurrentValue.equals("")) {
155: this .m_display.valueChanged("");
156: } else if (sCurrentValue.equals(this .m_sTrueText)) {
157: this .m_display.valueChanged("true");
158: this .m_sPreviousValue = sCurrentValue;
159: } else if (sCurrentValue.equals(this .m_sFalseText)) {
160: this .m_display.valueChanged("false");
161: this .m_sPreviousValue = sCurrentValue;
162: }
163: }
164: }
165:
166: /* (non-Javadoc)
167: * @see java.awt.Component#getPreferredSize()
168: */
169: public Dimension getPreferredSize() {
170: this .layoutContainer(null);
171: int nWidth = this .getParent().getWidth();
172: int nHeight = 10;
173: nHeight = nHeight + this .m_comboBox.getSize().height;
174: return new Dimension(nWidth, nHeight);
175: }
176:
177: /* (non-Javadoc)
178: * @see java.awt.LayoutManager#removeLayoutComponent(java.awt.Component)
179: */
180: public void removeLayoutComponent(Component arg0) {
181: // NO-OP
182: }
183:
184: /* (non-Javadoc)
185: * @see java.awt.LayoutManager#layoutContainer(java.awt.Container)
186: */
187: public void layoutContainer(Container arg0) {
188: int nWidth = this .getParent().getWidth() - 50;
189:
190: Dimension dim = m_comboBox.getPreferredSize();
191: m_comboBox.setPreferredSize(new Dimension(nWidth - 85,
192: 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 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 getValue() {
235: if (this .m_comboBox.getSelectedItem().equals(this .m_sTrueText)) {
236: return "true";
237: } else if (this .m_comboBox.getSelectedItem().equals(
238: this .m_sFalseText)) {
239: return "false";
240: } else {
241: return "";
242: }
243: }
244:
245: /* (non-Javadoc)
246: * @see com.simulacramedia.contentmanager.metadata.range.swing.AbstractRangeDisplay#isValid()
247: */
248: public boolean isMetadataValid() {
249: return !this .m_errorLabel.getIcon().toString().equals(
250: IconManager.getInstance().getIcon(
251: "16-message-error.gif").toString());
252: }
253:
254: /**
255: * Sets whether this component's value is valid.
256: *
257: * @param bValid true to set this component's value as valid
258: */
259: public void setValid(boolean bValid) {
260: if (!bValid) {
261: this .m_comboBox.setBorder(BorderFactory
262: .createLineBorder(Color.RED));
263: this .m_errorLabel.setIcon(IconManager.getInstance()
264: .getIcon("16-message-error.gif"));
265: } else {
266: this .m_comboBox.setBorder(BorderFactory
267: .createLineBorder(Color.BLACK));
268: this .m_errorLabel.setIcon(IconManager.getInstance()
269: .getIcon("16-message-confirm.gif"));
270: }
271: this.revalidate();
272: this.repaint();
273: }
274:
275: }
|