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.datetimehandling;
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: * @author Matthew Large
048: *
049: */
050: public class DateTimeRangeDisplay extends AbstractRangeDisplay
051: implements RangeDisplay, LayoutManager, ActionListener {
052:
053: private int m_nHeight = 10;
054:
055: private int m_nMinOccurs = 1;
056: private int m_nMaxOccurs = 1000;
057:
058: private JButton m_addButton = null;
059:
060: private ArrayList m_valuePanels = new ArrayList();
061: private ArrayList m_removeButtons = new ArrayList();
062:
063: private boolean m_bShowingDelButtons = true;
064:
065: /**
066: * @param propInstance
067: */
068: public DateTimeRangeDisplay(PropertyInstance propInstance) {
069: super (propInstance);
070: this .setup();
071: }
072:
073: private void setup() {
074: BoxLayout layout = new BoxLayout(this , BoxLayout.Y_AXIS);
075: this .setLayout(this );
076: }
077:
078: /* (non-Javadoc)
079: * @see com.simulacramedia.contentmanager.metadata.range.swing.RangeDisplay#getPanel()
080: */
081: public JPanel getPanel() {
082: DateTimeRange range = (DateTimeRange) this
083: .getPropertyInstance().getDefinition().getRange();
084: List domains = this .getPropertyInstance().getDefinition()
085: .getDomains();
086: Iterator itor = domains.iterator();
087:
088: while (itor.hasNext()) {
089: Domain domain = (Domain) itor.next();
090: boolean bApplicableDomain = false;
091: String sFilePath = ((VersionedVirtualFile) this
092: .getPropertyInstance().getVirtualFile())
093: .getLogicalPath();
094: Iterator itor2 = domain.getPaths().iterator();
095: while (itor2.hasNext()) {
096: String sDomainPath = (String) itor2.next();
097: if (sFilePath.startsWith(sDomainPath)) {
098: bApplicableDomain = true;
099: }
100: }
101:
102: if (bApplicableDomain) {
103: if (this .m_nMinOccurs < domain.getMinOccurs()) {
104: this .m_nMinOccurs = domain.getMinOccurs();
105: }
106: if (domain.getMaxOccurs() != -1
107: && this .m_nMaxOccurs > domain.getMaxOccurs()) {
108: this .m_nMaxOccurs = domain.getMaxOccurs();
109: }
110: }
111:
112: }
113:
114: PropertyInstance propInst = this .getPropertyInstance();
115: List vals = propInst.getValues();
116:
117: int nCountMax = this .m_nMinOccurs;
118: if (vals.size() > nCountMax) {
119: nCountMax = vals.size();
120: }
121:
122: for (int i = 0; i < nCountMax; i++) {
123:
124: if (i < vals.size()) {
125: DateTimeValuePanel valPanel = new DateTimeValuePanel(
126: this , this .getPropertyInstance(),
127: ((DateTimeValue) vals.get(i)).getValue());
128: this .add(valPanel);
129: this .m_valuePanels.add(valPanel);
130: this .m_nHeight = this .m_nHeight
131: + valPanel.getPreferredSize().height + 5;
132: } else {
133: DateTimeValuePanel valPanel = new DateTimeValuePanel(
134: this , this .getPropertyInstance(), "");
135: this .add(valPanel);
136: this .m_valuePanels.add(valPanel);
137: this .m_nHeight = this .m_nHeight
138: + valPanel.getPreferredSize().height + 5;
139: }
140: JButton removeButton = new JButton();
141: String fontName = "Dialog";
142: int fontSize = 13;
143: Font font = new Font(fontName, Font.BOLD, fontSize);
144: removeButton.setFont(font);
145: removeButton.setIcon(IconManager.getInstance().getIcon(
146: "16-command-value-minus.gif"));
147: removeButton.setActionCommand("REMOVE");
148: removeButton.addActionListener(this );
149: removeButton.setMargin(new Insets(2, 2, 2, 2));
150: this .add(removeButton);
151: this .m_removeButtons.add(removeButton);
152:
153: }
154:
155: if (this .m_valuePanels.size() < this .m_nMaxOccurs) {
156: this .m_addButton = new JButton();
157: String fontName = "Dialog";
158: int fontSize = 13;
159: Font font = new Font(fontName, Font.BOLD, fontSize);
160: this .m_addButton.setFont(font);
161: this .m_addButton.setIcon(IconManager.getInstance().getIcon(
162: "16-command-value-plus.gif"));
163: this .m_addButton.setActionCommand("ADD");
164: this .m_addButton.setMargin(new Insets(2, 2, 2, 2));
165: this .add(this .m_addButton);
166: this .m_addButton.addActionListener(this );
167: this .m_nHeight = this .m_nHeight + 25;
168: }
169:
170: this .checkButtonVisibility();
171:
172: return this ;
173: }
174:
175: /* (non-Javadoc)
176: * @see java.awt.Component#getPreferredSize()
177: */
178: public Dimension getPreferredSize() {
179: this .layoutContainer(null);
180: int nWidth = this .getParent().getWidth() - 25;
181:
182: return new Dimension(nWidth, this .m_nHeight);
183: }
184:
185: /* (non-Javadoc)
186: * @see java.awt.LayoutManager#removeLayoutComponent(java.awt.Component)
187: */
188: public void removeLayoutComponent(Component arg0) {
189: }
190:
191: /* (non-Javadoc)
192: * @see java.awt.LayoutManager#layoutContainer(java.awt.Container)
193: */
194: public void layoutContainer(Container arg0) {
195: if (this .getParent() instanceof PropertyValuePanel) {
196: this .m_nMinOccurs = 0;
197: this .m_nMaxOccurs = 1;
198: }
199:
200: this .checkButtonVisibility();
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:
211: DateTimeValuePanel valuePanel = (DateTimeValuePanel) itor
212: .next();
213: valuePanel.setSize(
214: valuePanel.getPreferredSize().width - 50,
215: valuePanel.getPreferredSize().height);
216: valuePanel.setLocation(10, nYPos);
217:
218: nYPos = nYPos + valuePanel.getSize().height;
219:
220: nYPos = nYPos + 3;
221:
222: nXPos = valuePanel.getLocation().x
223: + valuePanel.getSize().width;
224:
225: JButton removeButton = (JButton) this .m_removeButtons
226: .get(nCount);
227: if (removeButton.isVisible()) {
228: removeButton.setSize(20, 20);
229: removeButton.setLocation(nXPos, nYPos - 37);
230: }
231:
232: nCount++;
233: }
234:
235: if (this .m_addButton != null && this .m_addButton.isVisible()) {
236: this .m_addButton.setSize(20, 20);
237: this .m_addButton.setLocation(nXPos + 30, nYPos - 37);
238: }
239:
240: this .m_nHeight = nYPos - 17;
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: }
250:
251: /* (non-Javadoc)
252: * @see java.awt.LayoutManager#minimumLayoutSize(java.awt.Container)
253: */
254: public Dimension minimumLayoutSize(Container arg0) {
255: return this .getPreferredSize();
256: }
257:
258: /* (non-Javadoc)
259: * @see java.awt.LayoutManager#preferredLayoutSize(java.awt.Container)
260: */
261: public Dimension preferredLayoutSize(Container arg0) {
262: return this .getPreferredSize();
263: }
264:
265: /* (non-Javadoc)
266: * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
267: */
268: public void actionPerformed(ActionEvent ae) {
269: PropertyInstance propInst = this .getPropertyInstance();
270:
271: if (ae.getActionCommand().equals("REMOVE")
272: && this .m_valuePanels.size() > this .m_nMinOccurs) {
273: int nIndex = this .m_removeButtons.indexOf(ae.getSource());
274: this .remove((Component) this .m_removeButtons.get(nIndex));
275: this .m_removeButtons.remove(nIndex);
276: this .m_nHeight = this .m_nHeight
277: - ((Component) this .m_valuePanels.get(nIndex))
278: .getSize().height;
279: this .remove((Component) this .m_valuePanels.get(nIndex));
280: this .m_valuePanels.remove(nIndex);
281: this .valueChanged(null);
282: } else if (ae.getActionCommand().equals("ADD")
283: && this .m_valuePanels.size() < this .m_nMaxOccurs) {
284: DateTimeValuePanel valPanel = new DateTimeValuePanel(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: comp.validate();
308: while (!(comp instanceof MetadataTabs)) {
309: if (comp != null) {
310: comp = comp.getParent();
311: }
312: }
313: if (comp != null) {
314: comp.validate();
315: }
316: }
317:
318: private void checkButtonVisibility() {
319: if (this .m_nMinOccurs == 0 && this .m_nMaxOccurs == 1) {
320: Iterator itor = this .m_removeButtons.iterator();
321: while (itor.hasNext()) {
322: JButton button = (JButton) itor.next();
323: button.setVisible(false);
324: }
325: this .m_addButton.setVisible(false);
326: this .m_bShowingDelButtons = false;
327: return;
328: }
329: if (this .m_bShowingDelButtons
330: && this .m_removeButtons.size() <= this .m_nMinOccurs) {
331: Iterator itor = this .m_removeButtons.iterator();
332: while (itor.hasNext()) {
333: JButton button = (JButton) itor.next();
334: button.setVisible(false);
335: }
336: this .m_bShowingDelButtons = false;
337: } else if (!this .m_bShowingDelButtons
338: && this .m_removeButtons.size() > this .m_nMinOccurs) {
339: Iterator itor = this .m_removeButtons.iterator();
340: while (itor.hasNext()) {
341: JButton button = (JButton) itor.next();
342: button.setVisible(true);
343: }
344: this .m_bShowingDelButtons = true;
345: }
346: if (this .m_addButton != null && this .m_addButton.isVisible()
347: && this .m_valuePanels.size() >= this .m_nMaxOccurs) {
348: this .m_addButton.setVisible(false);
349: this .m_nHeight = this .m_nHeight
350: - this .m_addButton.getSize().height;
351: } else if (this .m_addButton != null
352: && this .m_valuePanels.size() < this .m_nMaxOccurs) {
353: this .m_addButton.setVisible(true);
354: this .m_nHeight = this .m_nHeight
355: + this .m_addButton.getSize().height;
356: }
357:
358: }
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: DateTimeValue val = (DateTimeValue) this
366: .getPropertyInstance().getNewValueInstance();
367: val.setValue(((DateTimeValuePanel) itor.next()).getValue());
368: aValues.add(val);
369: }
370: if (!this .getPropertyInstance().getValues()
371: .containsAll(aValues)
372: || !aValues.containsAll(this .getPropertyInstance()
373: .getValues())) {
374: this .getPropertyInstance().setValues(aValues);
375: }
376: }
377:
378: /* (non-Javadoc)
379: * @see com.simulacramedia.contentmanager.metadata.range.swing.AbstractRangeDisplay#isValid()
380: */
381: public boolean isMetadataValid() {
382: boolean bValid = true;
383: for (int i = 0; i < this .getComponentCount(); i++) {
384: Component comp = this .getComponent(i);
385: if (comp instanceof AbstractRangeDisplay) {
386: if (!((AbstractRangeDisplay) comp).isMetadataValid()) {
387: bValid = false;
388: }
389: }
390: }
391: return bValid;
392: }
393:
394: }
|