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.valuehandling;
020:
021: import java.awt.*;
022: import java.util.*;
023: import java.util.List;
024:
025: import javax.swing.*;
026:
027: import org.openharmonise.him.metadata.range.swing.*;
028: import org.openharmonise.him.swing.resourcetree.*;
029: import org.openharmonise.him.swing.resourcetree.formresourcetree.*;
030: import org.openharmonise.vfs.*;
031: import org.openharmonise.vfs.gui.*;
032: import org.openharmonise.vfs.metadata.*;
033: import org.openharmonise.vfs.metadata.range.*;
034: import org.openharmonise.vfs.metadata.value.*;
035:
036: /**
037: * @author Matthew Large
038: *
039: */
040: public class ValueRangeDisplay extends AbstractRangeDisplay implements
041: RangeDisplay, LayoutManager, FormResourceTreeListener {
042:
043: private int m_nHeight = 30;
044:
045: private WarningsLabel m_warnings = null;
046:
047: protected JLabel m_errorLabel = null;
048:
049: private int m_nMinOccurs = 0;
050: private int m_nMaxOccurs = 1000;
051:
052: private FormResourceTree m_resourceTree = null;
053:
054: /**
055: * @param propInstance
056: */
057: public ValueRangeDisplay(PropertyInstance propInstance) {
058: super (propInstance);
059: }
060:
061: /* (non-Javadoc)
062: * @see com.simulacramedia.contentmanager.metadata.range.swing.RangeDisplay#getPanel()
063: */
064: public JPanel getPanel() {
065: this .setLayout(this );
066:
067: ValueRange range = (ValueRange) this .getPropertyInstance()
068: .getDefinition().getRange();
069: List domains = this .getPropertyInstance().getDefinition()
070: .getDomains();
071: Iterator itor = domains.iterator();
072:
073: while (itor.hasNext()) {
074: Domain domain = (Domain) itor.next();
075: boolean bApplicableDomain = false;
076: String sFilePath = ((VersionedVirtualFile) this
077: .getPropertyInstance().getVirtualFile())
078: .getLogicalPath();
079: Iterator itor2 = domain.getPaths().iterator();
080: while (itor2.hasNext()) {
081: String sDomainPath = (String) itor2.next();
082: if (sFilePath.startsWith(sDomainPath)) {
083: bApplicableDomain = true;
084: }
085: }
086:
087: if (bApplicableDomain) {
088: if (this .m_nMinOccurs < domain.getMinOccurs()) {
089: this .m_nMinOccurs = domain.getMinOccurs();
090: }
091: if (this .m_nMaxOccurs > domain.getMaxOccurs()) {
092: this .m_nMaxOccurs = domain.getMaxOccurs();
093: }
094: }
095:
096: }
097:
098: this .m_errorLabel = new JLabel();
099: this .m_errorLabel.setIcon(IconManager.getInstance().getIcon(
100: "16-message-confirm.gif"));
101: this .add(this .m_errorLabel);
102:
103: String sWarning = "";
104:
105: if (this .m_nMinOccurs == 1 && this .m_nMaxOccurs == 1) {
106: sWarning = "Please select {1} option.";
107: } else if (this .m_nMinOccurs > 0
108: && this .m_nMaxOccurs > this .m_nMinOccurs) {
109: sWarning = "Please select between {" + this .m_nMinOccurs
110: + "} and {" + this .m_nMaxOccurs + "} options.";
111: } else if (this .m_nMinOccurs > 0
112: && this .m_nMinOccurs == this .m_nMinOccurs) {
113: sWarning = "Please select {" + this .m_nMinOccurs
114: + "} options.";
115: } else if (this .m_nMinOccurs > 0) {
116: sWarning = "Please select more than {" + this .m_nMinOccurs
117: + "} options.";
118: } else {
119: sWarning = "Please select from the options.";
120: }
121:
122: m_warnings = new WarningsLabel(sWarning);
123: this .add(m_warnings);
124:
125: PropertyInstance propInst = this .getPropertyInstance();
126: List vals = propInst.getValues();
127:
128: List pathValues = new ArrayList();
129: Iterator valItor = vals.iterator();
130: while (valItor.hasNext()) {
131: ValueValue valVal = (ValueValue) valItor.next();
132: pathValues.add(valVal.getValue());
133: }
134:
135: List valGroups = range.getValueGroups();
136:
137: this .m_resourceTree = new FormResourceTree(false, pathValues);
138: this .m_resourceTree.setIsMultiSelect(!(this .m_nMaxOccurs == 1));
139: this .m_resourceTree.addFormResourceTreeListener(this );
140: this .m_resourceTree
141: .setResourceFilter(createResourceTreeFilter());
142:
143: itor = valGroups.iterator();
144: while (itor.hasNext()) {
145: ValueGroup group = (ValueGroup) itor.next();
146: if (group != null) {
147: String sHREF = group.getHREF();
148: if (sHREF != null) {
149: VirtualFile groupToAdd = this .getPropertyInstance()
150: .getVirtualFile().getVFS().getVirtualFile(
151: group.getHREF()).getResource();
152: if (groupToAdd != null) {
153: this .m_resourceTree.addCollection(groupToAdd);
154: }
155: }
156: }
157: }
158:
159: this .add(this .m_resourceTree);
160:
161: this .m_nHeight = this .m_nHeight
162: + this .m_resourceTree.getPreferredSize().height;
163:
164: this .m_resourceTree.setVisible(true);
165:
166: this .checkWarnings();
167:
168: return this ;
169: }
170:
171: /* (non-Javadoc)
172: * @see java.awt.Component#getPreferredSize()
173: */
174: public Dimension getPreferredSize() {
175: int nWidth = this .getParent().getWidth() - 40;
176: int nHeight = 170;
177: if (!this .m_resourceTree.isUsingScrollPane()) {
178: nHeight = this .m_resourceTree.getPreferredSize().height;
179: }
180: return new Dimension(nWidth, nHeight);
181: }
182:
183: /* (non-Javadoc)
184: * @see java.awt.LayoutManager#removeLayoutComponent(java.awt.Component)
185: */
186: public void removeLayoutComponent(Component arg0) {
187: }
188:
189: /* (non-Javadoc)
190: * @see java.awt.LayoutManager#layoutContainer(java.awt.Container)
191: */
192: public void layoutContainer(Container arg0) {
193: m_resourceTree.setSize(this .getPreferredSize().width - 40, this
194: .getPreferredSize().height);
195: m_resourceTree.setLocation(20, 0);
196:
197: this .m_errorLabel.setSize(15, 15);
198: this .m_errorLabel.setLocation(0, 0);
199: }
200:
201: /* (non-Javadoc)
202: * @see java.awt.LayoutManager#addLayoutComponent(java.lang.String, java.awt.Component)
203: */
204: public void addLayoutComponent(String arg0, Component arg1) {
205: }
206:
207: /* (non-Javadoc)
208: * @see java.awt.LayoutManager#minimumLayoutSize(java.awt.Container)
209: */
210: public Dimension minimumLayoutSize(Container arg0) {
211: return this .getPreferredSize();
212: }
213:
214: /* (non-Javadoc)
215: * @see java.awt.LayoutManager#preferredLayoutSize(java.awt.Container)
216: */
217: public Dimension preferredLayoutSize(Container arg0) {
218: return this .getPreferredSize();
219: }
220:
221: public void checkWarnings() {
222: this .m_warnings.setHighlight(String.valueOf(this .m_nMinOccurs),
223: false);
224: this .m_warnings.setHighlight(String.valueOf(this .m_nMaxOccurs),
225: false);
226: this .m_errorLabel.setIcon(IconManager.getInstance().getIcon(
227: "16-message-confirm.gif"));
228:
229: List vals = this .getPropertyInstance().getValues();
230: if (vals.size() < this .m_nMinOccurs) {
231: this .m_warnings.setHighlight(String
232: .valueOf(this .m_nMinOccurs), true);
233: this .m_errorLabel.setIcon(IconManager.getInstance()
234: .getIcon("16-message-error.gif"));
235: }
236: if (this .m_nMaxOccurs != -1 && vals.size() > this .m_nMaxOccurs) {
237: this .m_warnings.setHighlight(String
238: .valueOf(this .m_nMaxOccurs), true);
239: this .m_errorLabel.setIcon(IconManager.getInstance()
240: .getIcon("16-message-error.gif"));
241: }
242: super .validateTab();
243: }
244:
245: /* (non-Javadoc)
246: * @see java.awt.Component#setEnabled(boolean)
247: */
248: public void setEnabled(boolean bEnabled) {
249: super .setEnabled(bEnabled);
250: }
251:
252: /* (non-Javadoc)
253: * @see com.simulacramedia.contentmanager.metadata.range.swing.AbstractRangeDisplay#isValid()
254: */
255: public boolean isMetadataValid() {
256: return !this .m_errorLabel.getIcon().toString().equals(
257: IconManager.getInstance().getIcon(
258: "16-message-error.gif").toString());
259: }
260:
261: /* (non-Javadoc)
262: * @see com.simulacramedia.contentmanager.swing.resourcetree.formresourcetree.FormResourceTreeListener#pathValuesChanged(java.util.List)
263: */
264: public void pathValuesChanged(List pathValues) {
265: this .getPropertyInstance().clearAllValues();
266:
267: Iterator itor = pathValues.iterator();
268: while (itor.hasNext()) {
269: String sFullPath = (String) itor.next();
270: ValueValue value = (ValueValue) this .getPropertyInstance()
271: .getNewValueInstance();
272: value.setValue(sFullPath);
273: this .getPropertyInstance().addValue(value);
274: }
275: this .checkWarnings();
276: }
277:
278: protected AbstractResourceFilter createResourceTreeFilter() {
279: return new BasicResourceFilter();
280: }
281:
282: }
|