KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > him > metadata > range > swing > relatedevents > RelatedEventRelationship


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.him.metadata.range.swing.relatedevents;
20
21 import java.awt.*;
22 import java.awt.event.*;
23 import java.util.*;
24 import java.util.List JavaDoc;
25
26 import org.openharmonise.him.editors.report.swing.*;
27 import org.openharmonise.vfs.*;
28 import org.openharmonise.vfs.metadata.*;
29 import org.openharmonise.vfs.metadata.range.*;
30 import org.openharmonise.vfs.metadata.value.*;
31
32
33 /**
34  *
35  * @author Michael Bell
36  * @version $Revision: 1.1 $
37  *
38  */

39 public class RelatedEventRelationship extends JComboTree {
40     
41     private static final String JavaDoc COMMAND_RECOMBO = "RECOMBO";
42     private static final String JavaDoc INVALID_TEXT = "--Invalid--";
43     private PropertyInstance m_propInst = null;
44     
45     private int m_nMinOccurs = -1;
46     private int m_nMaxOccurs = -1;
47
48     /**
49      *
50      */

51     public RelatedEventRelationship(PropertyInstance propInst) {
52         super();
53         m_propInst = propInst;
54         setup(propInst);
55     }
56
57     /* (non-Javadoc)
58      * @see com.simulacramedia.contentmanager.editors.report.swing.JComboTree#setup()
59      */

60     protected void setup(PropertyInstance propInst) {
61         
62         Property prop = m_propInst.getDefinition();
63         
64         List JavaDoc hrefs = ((ValueRange) prop.getRange()).getHREFs();
65                 
66         addCollectionPaths(hrefs);
67         
68         List JavaDoc vals = propInst.getValues();
69         
70         if(vals.size() > 0) {
71             AbstractVirtualFileSystem vfs = propInst.getVirtualFile().getVFS();
72         
73             List JavaDoc paths = new ArrayList();
74             
75             Iterator iter = vals.iterator();
76             
77             while (iter.hasNext()) {
78                 ValueValue val = (ValueValue) iter.next();
79                 paths.add(val.getValue());
80             }
81         
82             this.setPaths(vfs, paths);
83         }
84         
85         setActionCommand(COMMAND_RECOMBO);
86         addActionListener(this);
87     }
88
89     /* (non-Javadoc)
90      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
91      */

92     public void actionPerformed(ActionEvent ae) {
93         
94         if(ae.getActionCommand().equals(COMMAND_RECOMBO)) {
95             List JavaDoc paths = getPaths();
96             setValues(paths);
97         } else {
98             super.actionPerformed(ae);
99         }
100     }
101
102     /**
103      * @param paths
104      */

105     private void setValues(List JavaDoc paths) {
106         this.m_propInst.clearAllValues();
107         Iterator iter = paths.iterator();
108         
109         while (iter.hasNext()) {
110             String JavaDoc sPath = (String JavaDoc) iter.next();
111             if(sPath != null && sPath.length() > 0) {
112                 ValueValue val = (ValueValue)m_propInst.getNewValueInstance();
113                 val.setValue(sPath);
114                 m_propInst.addValue(val);
115             }
116         }
117         
118         firePropertyChange(RelatedEventValueDisplay.PROPERTY_METADATA_CHANGE, null, null);
119     }
120
121     /**
122      * @param sHREF
123      */

124     private void setValue(String JavaDoc sHREF) {
125         List JavaDoc vals = m_propInst.getValues();
126         ValueValue val = null;
127         if(vals.size()>0) {
128             val = (ValueValue) vals.get(0);
129         } else {
130             val = (ValueValue)m_propInst.getNewValueInstance();
131             m_propInst.setValue(val);
132         }
133         
134         val.setValue(sHREF);
135         
136     }
137
138     /**
139      * @return
140      */

141     public boolean isMetadataValid() {
142         boolean bIsValid = true;
143         
144         Property prop = m_propInst.getDefinition();
145
146         ValueRange range = (ValueRange)prop.getRange();
147         
148         List JavaDoc vals = m_propInst.getValues();
149
150         bIsValid = range.validate(vals).isValid();
151         
152         if(bIsValid == true && m_nMinOccurs > 0) {
153             bIsValid = (vals.size() >= m_nMinOccurs);
154             if(vals.size() == 0) {
155                 getLabel().setText(INVALID_TEXT);
156             }
157         }
158         
159         if(bIsValid == true && m_nMaxOccurs > 0) {
160             bIsValid = vals.size() <= m_nMaxOccurs;
161         }
162         
163         if(bIsValid == false) {
164             getLabel().setForeground(Color.RED);
165         } else {
166             getLabel().setForeground(Color.BLACK);
167         }
168         
169         this.revalidate();
170         
171         return bIsValid;
172     }
173     
174     public void setMinOccurs(int nMin) {
175         m_nMinOccurs = nMin;
176     }
177     
178     public void setMaxOccurs(int nMax) {
179         m_nMaxOccurs = nMax;
180     }
181
182     /* (non-Javadoc)
183      * @see com.simulacramedia.contentmanager.editors.report.swing.JComboTree#clear()
184      */

185     public void clear() {
186         super.clear();
187         if(m_nMinOccurs > 0) {
188             setInvalid();
189         }
190         
191     }
192     
193     void setInvalid() {
194         getLabel().setForeground(Color.RED);
195         getLabel().setText(INVALID_TEXT);
196         revalidate();
197     }
198
199 }
200
Popular Tags