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.relatedevents;
020:
021: import java.awt.*;
022: import java.beans.*;
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.vfs.metadata.*;
030: import org.openharmonise.vfs.metadata.range.*;
031: import org.openharmonise.vfs.metadata.value.*;
032:
033: /**
034: * @author Michael Bell
035: * @version $Revision: 1.1 $
036: *
037: */
038: public class RelatedEventApproxDate extends AbstractRangeDisplay
039: implements RangeDisplay, LayoutManager {
040:
041: private RelatedEventDate m_dateSpinner = null;
042: private RelatedEventCertainty m_checkbox = null;
043: private JLabel m_checkLabel = null;
044: private int m_nSpacing = 5;
045:
046: private PropertyValue m_propValue = null;
047:
048: /**
049: * @param propInst
050: */
051: public RelatedEventApproxDate(PropertyInstance propInst) {
052: super (propInst);
053:
054: List vals = propInst.getValues();
055:
056: if (vals.size() > 0) {
057: m_propValue = (PropertyValue) vals.get(0);
058: } else {
059: m_propValue = (PropertyValue) propInst
060: .getNewValueInstance();
061: propInst.addValueWithoutFiringVirtualFileEvent(m_propValue);
062: }
063:
064: setup();
065: }
066:
067: /**
068: *
069: */
070: private void setup() {
071: this .setLayout(this );
072:
073: PropertyInstance propInst = getPropertyInstance();
074:
075: PropertyRange range = (PropertyRange) propInst.getDefinition()
076: .getRange();
077:
078: List props = range.getProperties();
079:
080: List propInstances = this .m_propValue.getValue();
081:
082: ArrayList aFillingPropInstances = new ArrayList();
083: aFillingPropInstances.addAll(propInstances);
084:
085: Iterator iter = props.iterator();
086:
087: while (iter.hasNext()) {
088: Property tmpProp = (Property) iter.next();
089: String sPropName = tmpProp.getName();
090: PropertyInstance tmpPropInst = getPropertyInstance(tmpProp);
091:
092: if (tmpPropInst == null) {
093: tmpPropInst = new PropertyInstance(tmpProp
094: .getNamespace(), tmpProp.getName());
095: aFillingPropInstances.add(tmpPropInst);
096:
097: if (propInst.getVirtualFile() == null) {
098: throw new NullPointerException();
099: }
100:
101: tmpPropInst.setVirtualFile(propInst.getVirtualFile());
102:
103: PropertyValue val = (PropertyValue) propInst
104: .getNewValueInstance();
105: }
106:
107: if (sPropName.equals("date")) {
108: m_dateSpinner = new RelatedEventDate(tmpPropInst);
109: this .add(m_dateSpinner);
110: } else if (sPropName.equals("certainty")) {
111: m_checkbox = new RelatedEventCertainty(tmpPropInst);
112: m_checkLabel = new JLabel(tmpPropInst.getName() + ":");
113:
114: this .add(m_checkLabel);
115: this .add(m_checkbox);
116: }
117: }
118:
119: if (aFillingPropInstances.size() > propInstances.size()) {
120: this .m_propValue.setValue(aFillingPropInstances);
121: }
122:
123: }
124:
125: /* (non-Javadoc)
126: * @see java.awt.LayoutManager#removeLayoutComponent(java.awt.Component)
127: */
128: public void removeLayoutComponent(Component comp) {
129: }
130:
131: /* (non-Javadoc)
132: * @see java.awt.LayoutManager#layoutContainer(java.awt.Container)
133: */
134: public void layoutContainer(Container parent) {
135: int nYpos = 0;
136: int nXpos = 0;
137:
138: m_dateSpinner.setLocation(nXpos, nYpos);
139: m_dateSpinner.setSize(m_dateSpinner.getPreferredSize().width,
140: m_dateSpinner.getPreferredSize().height + 3);
141:
142: nYpos = nYpos + m_dateSpinner.getSize().height + m_nSpacing;
143:
144: m_checkLabel.setLocation(nXpos, nYpos);
145: m_checkLabel.setSize(m_checkLabel.getPreferredSize());
146:
147: nXpos = nXpos + m_checkLabel.getSize().width + m_nSpacing;
148:
149: m_checkbox.setLocation(nXpos, nYpos);
150: m_checkbox.setSize(m_checkbox.getPreferredSize());
151:
152: }
153:
154: /* (non-Javadoc)
155: * @see java.awt.LayoutManager#addLayoutComponent(java.lang.String, java.awt.Component)
156: */
157: public void addLayoutComponent(String name, Component comp) {
158: }
159:
160: /* (non-Javadoc)
161: * @see java.awt.LayoutManager#minimumLayoutSize(java.awt.Container)
162: */
163: public Dimension minimumLayoutSize(Container arg0) {
164: return this .getPreferredSize();
165: }
166:
167: /* (non-Javadoc)
168: * @see java.awt.LayoutManager#preferredLayoutSize(java.awt.Container)
169: */
170: public Dimension preferredLayoutSize(Container arg0) {
171: return this .getPreferredSize();
172: }
173:
174: /* (non-Javadoc)
175: * @see java.awt.Component#getPreferredSize()
176: */
177: public Dimension getPreferredSize() {
178: int nHeight = m_dateSpinner.getPreferredSize().height;
179: nHeight = nHeight + m_checkbox.getPreferredSize().height;
180: nHeight = nHeight + m_nSpacing;
181:
182: int nWidth = m_dateSpinner.getPreferredSize().width;
183:
184: return new Dimension(nWidth, nHeight);
185: }
186:
187: /* (non-Javadoc)
188: * @see com.simulacramedia.contentmanager.metadata.range.swing.AbstractRangeDisplay#isValid()
189: */
190: public boolean isMetadataValid() {
191: boolean bIsValid = false;
192:
193: bIsValid = (m_dateSpinner.isMetadataValid() && m_checkbox
194: .isMetadataValid());
195:
196: return bIsValid;
197: }
198:
199: /**
200: * @param prop
201: * @return
202: */
203: private PropertyInstance getPropertyInstance(Property prop) {
204: PropertyInstance propInst = null;
205:
206: List propInstances = this .m_propValue.getValue();
207:
208: Iterator iter = propInstances.iterator();
209:
210: while (iter.hasNext()) {
211: PropertyInstance propInstTemp = (PropertyInstance) iter
212: .next();
213: if (prop.getName().equals(propInstTemp.getName())
214: && prop.getNamespace().equals(
215: propInstTemp.getNamespaceURI())) {
216: propInst = propInstTemp;
217: }
218: }
219:
220: return propInst;
221: }
222:
223: /* (non-Javadoc)
224: * @see com.simulacramedia.contentmanager.metadata.range.swing.RangeDisplay#getPanel()
225: */
226: public JPanel getPanel() {
227:
228: return this ;
229: }
230:
231: /* (non-Javadoc)
232: * @see java.awt.Component#addPropertyChangeListener(java.beans.PropertyChangeListener)
233: */
234: public synchronized void addPropertyChangeListener(
235: PropertyChangeListener listener) {
236: super.addPropertyChangeListener(listener);
237: this.m_dateSpinner.addPropertyChangeListener(listener);
238: this.m_checkbox.addPropertyChangeListener(listener);
239: }
240:
241: }
|