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