KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > him > metadata > range > swing > valuehandling > ValueRangeDisplay


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.valuehandling;
20
21 import java.awt.*;
22 import java.util.*;
23 import java.util.List JavaDoc;
24
25 import javax.swing.*;
26
27 import org.openharmonise.him.metadata.range.swing.*;
28 import org.openharmonise.him.swing.resourcetree.*;
29 import org.openharmonise.him.swing.resourcetree.formresourcetree.*;
30 import org.openharmonise.vfs.*;
31 import org.openharmonise.vfs.gui.*;
32 import org.openharmonise.vfs.metadata.*;
33 import org.openharmonise.vfs.metadata.range.*;
34 import org.openharmonise.vfs.metadata.value.*;
35
36
37 /**
38  * @author Matthew Large
39  *
40  */

41 public class ValueRangeDisplay
42     extends AbstractRangeDisplay
43     implements RangeDisplay, LayoutManager, FormResourceTreeListener {
44
45         private int m_nHeight = 30;
46
47         private WarningsLabel m_warnings = null;
48
49         protected JLabel m_errorLabel = null;
50
51         private int m_nMinOccurs = 0;
52         private int m_nMaxOccurs = 1000;
53         
54         private FormResourceTree m_resourceTree = null;
55
56     /**
57      * @param propInstance
58      */

59     public ValueRangeDisplay(PropertyInstance propInstance) {
60         super(propInstance);
61     }
62
63     /* (non-Javadoc)
64      * @see com.simulacramedia.contentmanager.metadata.range.swing.RangeDisplay#getPanel()
65      */

66     public JPanel getPanel() {
67         this.setLayout(this);
68         
69         ValueRange range =
70             (ValueRange) this
71                 .getPropertyInstance()
72                 .getDefinition()
73                 .getRange();
74         List JavaDoc domains = this.getPropertyInstance().getDefinition().getDomains();
75         Iterator itor = domains.iterator();
76
77         while (itor.hasNext()) {
78             Domain domain = (Domain) itor.next();
79             boolean bApplicableDomain = false;
80             String JavaDoc sFilePath = ((VersionedVirtualFile)this.getPropertyInstance().getVirtualFile()).getLogicalPath();
81             Iterator itor2 = domain.getPaths().iterator();
82             while (itor2.hasNext()) {
83                 String JavaDoc sDomainPath = (String JavaDoc) itor2.next();
84                 if(sFilePath.startsWith(sDomainPath)) {
85                     bApplicableDomain=true;
86                 }
87             }
88
89             if(bApplicableDomain) {
90                 if (this.m_nMinOccurs < domain.getMinOccurs()) {
91                     this.m_nMinOccurs = domain.getMinOccurs();
92                 }
93                 if (this.m_nMaxOccurs > domain.getMaxOccurs()) {
94                     this.m_nMaxOccurs = domain.getMaxOccurs();
95                 }
96             }
97
98         }
99         
100         this.m_errorLabel = new JLabel();
101         this.m_errorLabel.setIcon(
102             IconManager.getInstance().getIcon("16-message-confirm.gif"));
103         this.add(this.m_errorLabel);
104
105         String JavaDoc sWarning = "";
106
107         if (this.m_nMinOccurs==1 && this.m_nMaxOccurs==1) {
108             sWarning =
109                 "Please select {1} option.";
110         } else if (this.m_nMinOccurs>0 && this.m_nMaxOccurs>this.m_nMinOccurs) {
111             sWarning =
112                 "Please select between {"
113                     + this.m_nMinOccurs
114                     + "} and {"
115                     + this.m_nMaxOccurs
116                     + "} options.";
117         } else if (this.m_nMinOccurs>0 && this.m_nMinOccurs==this.m_nMinOccurs) {
118             sWarning =
119                 "Please select {"
120                     + this.m_nMinOccurs
121                     + "} options.";
122         } else if (this.m_nMinOccurs>0) {
123             sWarning =
124                 "Please select more than {"
125                     + this.m_nMinOccurs
126                     + "} options.";
127         } else {
128             sWarning = "Please select from the options.";
129         }
130
131         m_warnings = new WarningsLabel(sWarning);
132         this.add(m_warnings);
133                 
134         PropertyInstance propInst = this.getPropertyInstance();
135         List JavaDoc vals = propInst.getValues();
136         
137         List JavaDoc pathValues = new ArrayList();
138         Iterator valItor = vals.iterator();
139         while (valItor.hasNext()) {
140             ValueValue valVal = (ValueValue) valItor.next();
141             pathValues.add(valVal.getValue());
142         }
143         
144         List JavaDoc valGroups = range.getValueGroups();
145         
146         this.m_resourceTree = new FormResourceTree(false, pathValues);
147         this.m_resourceTree.setIsMultiSelect(!(this.m_nMaxOccurs==1));
148         this.m_resourceTree.addFormResourceTreeListener(this);
149         this.m_resourceTree.setResourceFilter(createResourceTreeFilter());
150         
151         itor = valGroups.iterator();
152         while(itor.hasNext()) {
153             ValueGroup group = (ValueGroup)itor.next();
154             if(group!=null) {
155                 String JavaDoc sHREF = group.getHREF();
156                 if(sHREF!=null) {
157                     VirtualFile groupToAdd = this.getPropertyInstance().getVirtualFile().getVFS().getVirtualFile(group.getHREF()).getResource();
158                     if(groupToAdd!=null) {
159                         this.m_resourceTree.addCollection(groupToAdd);
160                     }
161                 }
162             }
163         }
164         
165         this.add(this.m_resourceTree);
166         
167         
168         this.m_nHeight = this.m_nHeight + this.m_resourceTree.getPreferredSize().height;
169         
170         this.m_resourceTree.setVisible(true);
171         
172         this.checkWarnings();
173         
174         return this;
175     }
176
177     /* (non-Javadoc)
178      * @see java.awt.Component#getPreferredSize()
179      */

180     public Dimension getPreferredSize() {
181         int nWidth = this.getParent().getWidth() - 40;
182         int nHeight = 170;
183         if(!this.m_resourceTree.isUsingScrollPane()) {
184             nHeight = this.m_resourceTree.getPreferredSize().height;
185         }
186         return new Dimension(nWidth, nHeight);
187     }
188
189     /* (non-Javadoc)
190      * @see java.awt.LayoutManager#removeLayoutComponent(java.awt.Component)
191      */

192     public void removeLayoutComponent(Component arg0) {
193     }
194
195     /* (non-Javadoc)
196      * @see java.awt.LayoutManager#layoutContainer(java.awt.Container)
197      */

198     public void layoutContainer(Container arg0) {
199         m_resourceTree.setSize(this.getPreferredSize().width-40,this.getPreferredSize().height);
200         m_resourceTree.setLocation(20,0);
201
202         this.m_errorLabel.setSize(15,15);
203         this.m_errorLabel.setLocation(0, 0);
204     }
205
206     /* (non-Javadoc)
207      * @see java.awt.LayoutManager#addLayoutComponent(java.lang.String, java.awt.Component)
208      */

209     public void addLayoutComponent(String JavaDoc arg0, Component arg1) {
210     }
211
212     /* (non-Javadoc)
213      * @see java.awt.LayoutManager#minimumLayoutSize(java.awt.Container)
214      */

215     public Dimension minimumLayoutSize(Container arg0) {
216         return this.getPreferredSize();
217     }
218
219     /* (non-Javadoc)
220      * @see java.awt.LayoutManager#preferredLayoutSize(java.awt.Container)
221      */

222     public Dimension preferredLayoutSize(Container arg0) {
223         return this.getPreferredSize();
224     }
225
226
227     
228     public void checkWarnings() {
229         this.m_warnings.setHighlight(String.valueOf(this.m_nMinOccurs), false);
230         this.m_warnings.setHighlight(String.valueOf(this.m_nMaxOccurs), false);
231         this.m_errorLabel.setIcon(IconManager.getInstance().getIcon("16-message-confirm.gif"));
232         
233         List JavaDoc vals = this.getPropertyInstance().getValues();
234         if(vals.size()<this.m_nMinOccurs) {
235             this.m_warnings.setHighlight(String.valueOf(this.m_nMinOccurs), true);
236             this.m_errorLabel.setIcon(IconManager.getInstance().getIcon("16-message-error.gif"));
237         }
238         if(this.m_nMaxOccurs!=-1 && vals.size()>this.m_nMaxOccurs) {
239             this.m_warnings.setHighlight(String.valueOf(this.m_nMaxOccurs), true);
240             this.m_errorLabel.setIcon(IconManager.getInstance().getIcon("16-message-error.gif"));
241         }
242         super.validateTab();
243     }
244     /* (non-Javadoc)
245      * @see java.awt.Component#setEnabled(boolean)
246      */

247     public void setEnabled(boolean bEnabled) {
248         super.setEnabled(bEnabled);
249     }
250
251     /* (non-Javadoc)
252      * @see com.simulacramedia.contentmanager.metadata.range.swing.AbstractRangeDisplay#isValid()
253      */

254     public boolean isMetadataValid() {
255         return !this.m_errorLabel.getIcon().toString().equals(IconManager.getInstance().getIcon("16-message-error.gif").toString());
256     }
257
258     /* (non-Javadoc)
259      * @see com.simulacramedia.contentmanager.swing.resourcetree.formresourcetree.FormResourceTreeListener#pathValuesChanged(java.util.List)
260      */

261     public void pathValuesChanged(List JavaDoc pathValues) {
262         this.getPropertyInstance().clearAllValues();
263         
264         Iterator itor = pathValues.iterator();
265         while (itor.hasNext()) {
266             String JavaDoc sFullPath = (String JavaDoc) itor.next();
267             ValueValue value = (ValueValue) this.getPropertyInstance().getNewValueInstance();
268             value.setValue(sFullPath);
269             this.getPropertyInstance().addValue(value);
270         }
271         this.checkWarnings();
272     }
273
274     protected AbstractResourceFilter createResourceTreeFilter() {
275         return new BasicResourceFilter();
276     }
277     
278 }
279
Popular Tags