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.awt.event.*;
023: import java.util.*;
024: import java.util.List;
025:
026: import org.openharmonise.him.editors.report.swing.*;
027: import org.openharmonise.vfs.*;
028: import org.openharmonise.vfs.metadata.*;
029: import org.openharmonise.vfs.metadata.range.*;
030: import org.openharmonise.vfs.metadata.value.*;
031:
032: /**
033: *
034: * @author Michael Bell
035: * @version $Revision: 1.1 $
036: *
037: */
038: public class RelatedEventRelationship extends JComboTree {
039:
040: private static final String COMMAND_RECOMBO = "RECOMBO";
041: private static final String INVALID_TEXT = "--Invalid--";
042: private PropertyInstance m_propInst = null;
043:
044: private int m_nMinOccurs = -1;
045: private int m_nMaxOccurs = -1;
046:
047: /**
048: *
049: */
050: public RelatedEventRelationship(PropertyInstance propInst) {
051: super ();
052: m_propInst = propInst;
053: setup(propInst);
054: }
055:
056: /* (non-Javadoc)
057: * @see com.simulacramedia.contentmanager.editors.report.swing.JComboTree#setup()
058: */
059: protected void setup(PropertyInstance propInst) {
060:
061: Property prop = m_propInst.getDefinition();
062:
063: List hrefs = ((ValueRange) prop.getRange()).getHREFs();
064:
065: addCollectionPaths(hrefs);
066:
067: List vals = propInst.getValues();
068:
069: if (vals.size() > 0) {
070: AbstractVirtualFileSystem vfs = propInst.getVirtualFile()
071: .getVFS();
072:
073: List paths = new ArrayList();
074:
075: Iterator iter = vals.iterator();
076:
077: while (iter.hasNext()) {
078: ValueValue val = (ValueValue) iter.next();
079: paths.add(val.getValue());
080: }
081:
082: this .setPaths(vfs, paths);
083: }
084:
085: setActionCommand(COMMAND_RECOMBO);
086: addActionListener(this );
087: }
088:
089: /* (non-Javadoc)
090: * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
091: */
092: public void actionPerformed(ActionEvent ae) {
093:
094: if (ae.getActionCommand().equals(COMMAND_RECOMBO)) {
095: List paths = getPaths();
096: setValues(paths);
097: } else {
098: super .actionPerformed(ae);
099: }
100: }
101:
102: /**
103: * @param paths
104: */
105: private void setValues(List paths) {
106: this .m_propInst.clearAllValues();
107: Iterator iter = paths.iterator();
108:
109: while (iter.hasNext()) {
110: String sPath = (String) iter.next();
111: if (sPath != null && sPath.length() > 0) {
112: ValueValue val = (ValueValue) m_propInst
113: .getNewValueInstance();
114: val.setValue(sPath);
115: m_propInst.addValue(val);
116: }
117: }
118:
119: firePropertyChange(
120: RelatedEventValueDisplay.PROPERTY_METADATA_CHANGE,
121: null, null);
122: }
123:
124: /**
125: * @param sHREF
126: */
127: private void setValue(String sHREF) {
128: List vals = m_propInst.getValues();
129: ValueValue val = null;
130: if (vals.size() > 0) {
131: val = (ValueValue) vals.get(0);
132: } else {
133: val = (ValueValue) m_propInst.getNewValueInstance();
134: m_propInst.setValue(val);
135: }
136:
137: val.setValue(sHREF);
138:
139: }
140:
141: /**
142: * @return
143: */
144: public boolean isMetadataValid() {
145: boolean bIsValid = true;
146:
147: Property prop = m_propInst.getDefinition();
148:
149: ValueRange range = (ValueRange) prop.getRange();
150:
151: List vals = m_propInst.getValues();
152:
153: bIsValid = range.validate(vals).isValid();
154:
155: if (bIsValid == true && m_nMinOccurs > 0) {
156: bIsValid = (vals.size() >= m_nMinOccurs);
157: if (vals.size() == 0) {
158: getLabel().setText(INVALID_TEXT);
159: }
160: }
161:
162: if (bIsValid == true && m_nMaxOccurs > 0) {
163: bIsValid = vals.size() <= m_nMaxOccurs;
164: }
165:
166: if (bIsValid == false) {
167: getLabel().setForeground(Color.RED);
168: } else {
169: getLabel().setForeground(Color.BLACK);
170: }
171:
172: this .revalidate();
173:
174: return bIsValid;
175: }
176:
177: public void setMinOccurs(int nMin) {
178: m_nMinOccurs = nMin;
179: }
180:
181: public void setMaxOccurs(int nMax) {
182: m_nMaxOccurs = nMax;
183: }
184:
185: /* (non-Javadoc)
186: * @see com.simulacramedia.contentmanager.editors.report.swing.JComboTree#clear()
187: */
188: public void clear() {
189: super .clear();
190: if (m_nMinOccurs > 0) {
191: setInvalid();
192: }
193:
194: }
195:
196: void setInvalid() {
197: getLabel().setForeground(Color.RED);
198: getLabel().setText(INVALID_TEXT);
199: revalidate();
200: }
201:
202: }
|