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: import java.util.*;
024: import java.util.List;
025:
026: import javax.swing.*;
027:
028: import org.openharmonise.him.metadata.range.swing.*;
029: import org.openharmonise.him.metadata.swing.*;
030: import org.openharmonise.vfs.gui.*;
031: import org.openharmonise.vfs.metadata.*;
032: import org.openharmonise.vfs.metadata.range.*;
033: import org.openharmonise.vfs.metadata.value.*;
034:
035: /**
036: * Component to display boolean properties in the metadata window.
037: *
038: * @author Matthew Large
039: * @version $Revision: 1.1 $
040: *
041: */
042: public class BooleanRangeDisplay extends AbstractRangeDisplay implements
043: RangeDisplay, LayoutManager, ActionListener {
044:
045: /**
046: * Height of the component.
047: */
048: private int m_nHeight = 10;
049:
050: /**
051: * Minimum value occurances.
052: */
053: private int m_nMinOccurs = 1;
054:
055: /**
056: * Maximum value occurances.
057: */
058: private int m_nMaxOccurs = 1;
059:
060: /**
061: * Button for adding new values.
062: */
063: private JButton m_addButton = null;
064:
065: /**
066: * List of {@link BooleanValuePanel} objects.
067: */
068: private ArrayList m_valuePanels = new ArrayList();
069:
070: /**
071: * List of buttons for removing values.
072: */
073: private ArrayList m_removeButtons = new ArrayList();
074:
075: /**
076: * true if removal buttons shoul dbe showing
077: */
078: private boolean m_bShowingDelButtons = true;
079:
080: /**
081: * Constructs a new boolean range display component.
082: *
083: * @param propInstance Property instance to be displayed
084: */
085: public BooleanRangeDisplay(PropertyInstance propInstance) {
086: super (propInstance);
087: this .setup();
088: }
089:
090: /**
091: * Initialises this component.
092: *
093: */
094: private void setup() {
095: this .setLayout(this );
096: }
097:
098: /* (non-Javadoc)
099: * @see com.simulacramedia.contentmanager.metadata.range.swing.RangeDisplay#getPanel()
100: */
101: public JPanel getPanel() {
102: BooleanRange range = (BooleanRange) this .getPropertyInstance()
103: .getDefinition().getRange();
104: List domains = this .getPropertyInstance().getDefinition()
105: .getDomains();
106: Iterator itor = domains.iterator();
107:
108: while (itor.hasNext()) {
109: Domain domain = (Domain) itor.next();
110: if (this .m_nMinOccurs < domain.getMinOccurs()) {
111: this .m_nMinOccurs = domain.getMinOccurs();
112: }
113:
114: }
115:
116: PropertyInstance propInst = this .getPropertyInstance();
117: List vals = propInst.getValues();
118:
119: int nCountMax = this .m_nMinOccurs;
120: if (vals.size() > nCountMax) {
121: nCountMax = vals.size();
122: }
123:
124: for (int i = 0; i < nCountMax; i++) {
125:
126: if (i < vals.size()) {
127: BooleanValuePanel valPanel = new BooleanValuePanel(
128: this , this .getPropertyInstance(), Boolean
129: .toString(((BooleanValue) vals.get(i))
130: .getValue()));
131: this .add(valPanel);
132: this .m_valuePanels.add(valPanel);
133: this .m_nHeight = this .m_nHeight
134: + valPanel.getPreferredSize().height + 5;
135: } else {
136: BooleanValuePanel valPanel = new BooleanValuePanel(
137: this , this .getPropertyInstance(), "");
138: this .add(valPanel);
139: this .m_valuePanels.add(valPanel);
140: this .m_nHeight = this .m_nHeight
141: + valPanel.getPreferredSize().height + 5;
142: }
143: JButton removeButton = new JButton();
144: String fontName = "Dialog";
145: int fontSize = 13;
146: Font font = new Font(fontName, Font.BOLD, fontSize);
147: removeButton.setFont(font);
148: removeButton.setIcon(IconManager.getInstance().getIcon(
149: "16-command-value-minus.gif"));
150: removeButton.setActionCommand("REMOVE");
151: removeButton.addActionListener(this );
152: removeButton.setMargin(new Insets(2, 2, 2, 2));
153: this .add(removeButton);
154: this .m_removeButtons.add(removeButton);
155:
156: }
157:
158: if (this .m_valuePanels.size() < this .m_nMaxOccurs) {
159: this .m_addButton = new JButton();
160: String fontName = "Dialog";
161: int fontSize = 13;
162: Font font = new Font(fontName, Font.BOLD, fontSize);
163: this .m_addButton.setFont(font);
164: this .m_addButton.setIcon(IconManager.getInstance().getIcon(
165: "16-command-value-plus.gif"));
166: this .m_addButton.setActionCommand("ADD");
167: this .m_addButton.setMargin(new Insets(2, 2, 2, 2));
168:
169: this .add(this .m_addButton);
170: this .m_addButton.addActionListener(this );
171: this .m_nHeight = this .m_nHeight + 25;
172: }
173:
174: this .checkButtonVisibility();
175:
176: return this ;
177: }
178:
179: /* (non-Javadoc)
180: * @see java.awt.Component#getPreferredSize()
181: */
182: public Dimension getPreferredSize() {
183: this .layoutContainer(null);
184: int nWidth = this .getParent().getWidth() - 25;
185:
186: return new Dimension(nWidth, this .m_nHeight);
187:
188: }
189:
190: /* (non-Javadoc)
191: * @see java.awt.LayoutManager#removeLayoutComponent(java.awt.Component)
192: */
193: public void removeLayoutComponent(Component arg0) {
194: // NO-OP
195: }
196:
197: /* (non-Javadoc)
198: * @see java.awt.LayoutManager#layoutContainer(java.awt.Container)
199: */
200: public void layoutContainer(Container arg0) {
201: int nWidth = this .getParent().getWidth() - 50;
202:
203: int nYPos = 0;
204:
205: int nXPos = 0;
206:
207: Iterator itor = this .m_valuePanels.iterator();
208: int nCount = 0;
209: while (itor.hasNext()) {
210: BooleanValuePanel valuePanel = (BooleanValuePanel) itor
211: .next();
212: valuePanel.setSize(
213: valuePanel.getPreferredSize().width - 50,
214: valuePanel.getPreferredSize().height);
215: valuePanel.setLocation(10, nYPos);
216: nXPos = valuePanel.getLocation().x
217: + valuePanel.getSize().width;
218:
219: nYPos = nYPos + valuePanel.getSize().height;
220:
221: JButton removeButton = (JButton) this .m_removeButtons
222: .get(nCount);
223: removeButton.setSize(20, 20);
224: removeButton
225: .setLocation(nXPos
226: - removeButton.getPreferredSize().width
227: - 50, nYPos);
228:
229: nCount++;
230: }
231:
232: if (this .m_addButton != null) {
233: this .m_addButton.setSize(20, 20);
234: this .m_addButton.setLocation(nXPos
235: - (m_addButton.getPreferredSize().width * 2) - 55,
236: nYPos - 3);
237:
238: }
239:
240: this .m_nHeight = nYPos;// + 5;
241:
242: this .repaint();
243: }
244:
245: /* (non-Javadoc)
246: * @see java.awt.LayoutManager#addLayoutComponent(java.lang.String, java.awt.Component)
247: */
248: public void addLayoutComponent(String arg0, Component arg1) {
249: // NO-OP
250: }
251:
252: /* (non-Javadoc)
253: * @see java.awt.LayoutManager#minimumLayoutSize(java.awt.Container)
254: */
255: public Dimension minimumLayoutSize(Container arg0) {
256: return this .getPreferredSize();
257: }
258:
259: /* (non-Javadoc)
260: * @see java.awt.LayoutManager#preferredLayoutSize(java.awt.Container)
261: */
262: public Dimension preferredLayoutSize(Container arg0) {
263: return this .getPreferredSize();
264: }
265:
266: /* (non-Javadoc)
267: * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
268: */
269: public void actionPerformed(ActionEvent ae) {
270: PropertyInstance propInst = this .getPropertyInstance();
271:
272: if (ae.getActionCommand().equals("REMOVE")
273: && this .m_valuePanels.size() > this .m_nMinOccurs) {
274: int nIndex = this .m_removeButtons.indexOf(ae.getSource());
275: this .remove((Component) this .m_removeButtons.get(nIndex));
276: this .m_removeButtons.remove(nIndex);
277: this .m_nHeight = this .m_nHeight
278: - ((Component) this .m_valuePanels.get(nIndex))
279: .getSize().height;
280: this .remove((Component) this .m_valuePanels.get(nIndex));
281: this .m_valuePanels.remove(nIndex);
282: } else if (ae.getActionCommand().equals("ADD")
283: && this .m_valuePanels.size() < this .m_nMaxOccurs) {
284: BooleanValuePanel valPanel = new BooleanValuePanel(this ,
285: this .getPropertyInstance(), "");
286: this .add(valPanel);
287: this .m_valuePanels.add(valPanel);
288: this .m_nHeight = this .m_nHeight
289: + valPanel.getPreferredSize().height;
290: JButton removeButton = new JButton();
291: String fontName = "Dialog";
292: int fontSize = 13;
293: Font font = new Font(fontName, Font.BOLD, fontSize);
294: removeButton.setFont(font);
295: removeButton.setIcon(IconManager.getInstance().getIcon(
296: "16-command-value-minus.gif"));
297: removeButton.setActionCommand("REMOVE");
298: removeButton.addActionListener(this );
299: removeButton.setMargin(new Insets(2, 2, 2, 2));
300: this .add(removeButton);
301: this .m_removeButtons.add(removeButton);
302: }
303:
304: this .checkButtonVisibility();
305:
306: Component comp = this .getParent();
307: while (!(comp instanceof MetadataTabs)) {
308: if (comp != null) {
309: comp = comp.getParent();
310: }
311: }
312: if (comp != null) {
313: comp.validate();
314: }
315: }
316:
317: /**
318: * Checks and sets the visibility of the addition and
319: * removal buttons.
320: *
321: */
322: private void checkButtonVisibility() {
323: if (this .m_bShowingDelButtons
324: && this .m_removeButtons.size() <= this .m_nMinOccurs) {
325: Iterator itor = this .m_removeButtons.iterator();
326: while (itor.hasNext()) {
327: JButton button = (JButton) itor.next();
328: button.setVisible(false);
329: }
330: this .m_bShowingDelButtons = false;
331: } else if (!this .m_bShowingDelButtons
332: && this .m_removeButtons.size() > this .m_nMinOccurs) {
333: Iterator itor = this .m_removeButtons.iterator();
334: while (itor.hasNext()) {
335: JButton button = (JButton) itor.next();
336: button.setVisible(true);
337: }
338: this .m_bShowingDelButtons = true;
339: }
340: if (this .m_addButton != null && this .m_addButton.isVisible()
341: && this .m_valuePanels.size() >= this .m_nMaxOccurs) {
342: this .m_addButton.setVisible(false);
343: this .m_nHeight = this .m_nHeight
344: - this .m_addButton.getSize().height;
345: } else if (this .m_addButton != null
346: && !this .m_addButton.isVisible()
347: && this .m_valuePanels.size() < this .m_nMaxOccurs) {
348: this .m_addButton.setVisible(true);
349: this .m_nHeight = this .m_nHeight
350: + this .m_addButton.getSize().height;
351: }
352:
353: }
354:
355: /**
356: * Method called if a value has been altered.
357: *
358: * @param sValue Value that has been altered
359: */
360: protected void valueChanged(String sValue) {
361: ArrayList aValues = new ArrayList();
362:
363: Iterator itor = this .m_valuePanels.iterator();
364: while (itor.hasNext()) {
365: String sVal = ((BooleanValuePanel) itor.next()).getValue();
366: if (!sVal.equals("")) {
367: BooleanValue val = (BooleanValue) this
368: .getPropertyInstance().getNewValueInstance();
369: val.setValue(Boolean.valueOf(sVal).booleanValue());
370: aValues.add(val);
371: }
372: }
373: if (this .getPropertyInstance().getValues().size() != aValues
374: .size()
375: || !this .getPropertyInstance().getValues().containsAll(
376: aValues)) {
377: this .getPropertyInstance().setValues(aValues);
378: }
379:
380: super .validateTab();
381: }
382:
383: /* (non-Javadoc)
384: * @see com.simulacramedia.contentmanager.metadata.range.swing.AbstractRangeDisplay#isValid()
385: */
386: public boolean isMetadataValid() {
387: boolean bValid = true;
388: if (this .m_nMinOccurs > 0) {
389: for (int i = 0; i < this .getComponentCount(); i++) {
390: Component comp = this .getComponent(i);
391: if (comp instanceof AbstractRangeDisplay) {
392: if (!((AbstractRangeDisplay) comp)
393: .isMetadataValid()) {
394: bValid = false;
395: }
396: }
397: }
398: }
399: return bValid;
400: }
401: }
|