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.propertyhandling;
020:
021: import java.awt.*;
022: import java.awt.event.*;
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.him.metadata.swing.*;
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: * @version $Revision: 1.1 $
039: *
040: */
041: public class PropertyRangeDisplay extends AbstractRangeDisplay
042: implements RangeDisplay, LayoutManager, ActionListener {
043:
044: private int m_nHeight = 0;
045:
046: private int m_nMinOccurs = 1;
047: private int m_nMaxOccurs = 1000;
048:
049: private JButton m_addButton = null;
050:
051: private ArrayList m_valuePanels = new ArrayList();
052: private ArrayList m_removeButtons = new ArrayList();
053:
054: private boolean m_bShowingDelButtons = true;
055:
056: /**
057: * @param propInstance
058: */
059: public PropertyRangeDisplay(PropertyInstance propInstance) {
060: super (propInstance);
061: this .setup();
062:
063: }
064:
065: private void setup() {
066: BoxLayout layout = new BoxLayout(this , BoxLayout.Y_AXIS);
067: this .setLayout(this );
068: }
069:
070: /* (non-Javadoc)
071: * @see com.simulacramedia.contentmanager.metadata.range.swing.RangeDisplay#getPanel()
072: */
073: public JPanel getPanel() {
074: PropertyRange range = (PropertyRange) this
075: .getPropertyInstance().getDefinition().getRange();
076: List domains = this .getPropertyInstance().getDefinition()
077: .getDomains();
078: Iterator itor = domains.iterator();
079:
080: while (itor.hasNext()) {
081: Domain domain = (Domain) itor.next();
082: boolean bApplicableDomain = false;
083: String sFilePath = ((VersionedVirtualFile) this
084: .getPropertyInstance().getVirtualFile())
085: .getLogicalPath();
086: Iterator itor2 = domain.getPaths().iterator();
087: while (itor2.hasNext()) {
088: String sDomainPath = (String) itor2.next();
089: if (sFilePath.startsWith(sDomainPath)) {
090: bApplicableDomain = true;
091: }
092: }
093:
094: if (bApplicableDomain) {
095: if (this .m_nMinOccurs < domain.getMinOccurs()) {
096: this .m_nMinOccurs = domain.getMinOccurs();
097: }
098: if (domain.getMaxOccurs() != -1
099: && this .m_nMaxOccurs > domain.getMaxOccurs()) {
100: this .m_nMaxOccurs = domain.getMaxOccurs();
101: }
102: }
103:
104: }
105:
106: PropertyInstance propInst = this .getPropertyInstance();
107: List vals = propInst.getValues();
108:
109: int nCountMax = this .m_nMinOccurs;
110: if (vals.size() > nCountMax) {
111: nCountMax = vals.size();
112: }
113:
114: for (int i = 0; i < nCountMax; i++) {
115: try {
116: if (i < vals.size()) {
117: PropertyValuePanel valPanel = null;
118:
119: valPanel = new PropertyValuePanel(this , this
120: .getPropertyInstance(),
121: (PropertyValue) vals.get(i));
122: this .add(valPanel.getPanel());
123: this .m_valuePanels.add(valPanel);
124: this .m_nHeight = this .m_nHeight
125: + valPanel.getPreferredSize().height + 5;
126:
127: } else {
128: PropertyValuePanel valPanel = null;
129:
130: PropertyValue newValue = (PropertyValue) this
131: .getPropertyInstance()
132: .getNewValueInstance();
133: this .getPropertyInstance()
134: .addValueWithoutFiringVirtualFileEvent(
135: newValue);
136: valPanel = new PropertyValuePanel(this , this
137: .getPropertyInstance(), newValue);
138: this .add(valPanel.getPanel());
139: this .m_valuePanels.add(valPanel);
140: this .m_nHeight = this .m_nHeight
141: + valPanel.getPreferredSize().height + 5;
142:
143: }
144: JButton removeButton = new JButton();
145: String fontName = "Dialog";
146: int fontSize = 13;
147: Font font = new Font(fontName, Font.BOLD, fontSize);
148: removeButton.setFont(font);
149: removeButton.setIcon(IconManager.getInstance().getIcon(
150: "16-command-value-minus.gif"));
151: removeButton.setActionCommand("REMOVE");
152: removeButton.addActionListener(this );
153: removeButton.setMargin(new Insets(2, 2, 2, 2));
154: this .add(removeButton);
155: this .m_removeButtons.add(removeButton);
156: } catch (ClassCastException cce) {
157: }
158: }
159:
160: if (this .m_nMaxOccurs == -1
161: || this .m_valuePanels.size() < this .m_nMaxOccurs) {
162: this .m_addButton = new JButton();
163: String fontName = "Dialog";
164: int fontSize = 13;
165: Font font = new Font(fontName, Font.BOLD, fontSize);
166: this .m_addButton.setFont(font);
167: this .m_addButton.setIcon(IconManager.getInstance().getIcon(
168: "16-command-value-plus.gif"));
169: this .m_addButton.setActionCommand("ADD");
170: this .m_addButton.setMargin(new Insets(2, 2, 2, 2));
171: this .add(this .m_addButton);
172: this .m_addButton.addActionListener(this );
173: this .m_nHeight = this .m_nHeight + 25;
174: }
175:
176: this .checkButtonVisibility();
177:
178: return this ;
179: }
180:
181: /* (non-Javadoc)
182: * @see java.awt.Component#getPreferredSize()
183: */
184: public Dimension getPreferredSize() {
185: this .layoutContainer(null);
186: int nWidth = this .getParent().getWidth() - 25;
187: return new Dimension(nWidth, this .m_nHeight);
188: }
189:
190: /* (non-Javadoc)
191: * @see java.awt.LayoutManager#removeLayoutComponent(java.awt.Component)
192: */
193: public void removeLayoutComponent(Component arg0) {
194: }
195:
196: /* (non-Javadoc)
197: * @see java.awt.LayoutManager#layoutContainer(java.awt.Container)
198: */
199: public void layoutContainer(Container arg0) {
200: this .checkButtonVisibility();
201: int nWidth = this .getParent().getWidth() - 50;
202:
203: int nYPos = 0;
204:
205: int nXPos = 0;
206:
207: Iterator itor = this .m_valuePanels.iterator();
208: int nCount = 0;
209: while (itor.hasNext()) {
210: PropertyValuePanel valuePanel = (PropertyValuePanel) itor
211: .next();
212: valuePanel.setSize(valuePanel.getPreferredSize().width,
213: valuePanel.getPreferredSize().height);
214: valuePanel.setLocation(60, nYPos);
215: nXPos = valuePanel.getLocation().x
216: + valuePanel.getSize().width;
217:
218: JButton removeButton = (JButton) this .m_removeButtons
219: .get(nCount);
220: if (removeButton.isVisible()) {
221: removeButton.setSize(20, 20);
222: removeButton.setLocation(5, nYPos - 30);
223: }
224:
225: nYPos = nYPos + valuePanel.getSize().height;
226:
227: nCount++;
228: }
229:
230: if (this .m_addButton != null && this .m_addButton.isVisible()) {
231: this .m_addButton.setSize(20, 20);
232: this .m_addButton.setLocation(30, nYPos - 50);
233: }
234:
235: this .m_nHeight = nYPos;
236:
237: this .repaint();
238: }
239:
240: /* (non-Javadoc)
241: * @see java.awt.LayoutManager#addLayoutComponent(java.lang.String, java.awt.Component)
242: */
243: public void addLayoutComponent(String arg0, Component arg1) {
244: }
245:
246: /* (non-Javadoc)
247: * @see java.awt.LayoutManager#minimumLayoutSize(java.awt.Container)
248: */
249: public Dimension minimumLayoutSize(Container arg0) {
250: return this .getPreferredSize();
251: }
252:
253: /* (non-Javadoc)
254: * @see java.awt.LayoutManager#preferredLayoutSize(java.awt.Container)
255: */
256: public Dimension preferredLayoutSize(Container arg0) {
257: return this .getPreferredSize();
258: }
259:
260: /* (non-Javadoc)
261: * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
262: */
263: public void actionPerformed(ActionEvent ae) {
264: PropertyInstance propInst = this .getPropertyInstance();
265:
266: if (ae.getActionCommand().equals("REMOVE")
267: && this .m_valuePanels.size() > this .m_nMinOccurs) {
268: int nIndex = this .m_removeButtons.indexOf(ae.getSource());
269: this .remove((Component) this .m_removeButtons.get(nIndex));
270: this .m_removeButtons.remove(nIndex);
271: this .m_nHeight = this .m_nHeight
272: - ((Component) this .m_valuePanels.get(nIndex))
273: .getSize().height;
274: this .remove((Component) this .m_valuePanels.get(nIndex));
275: this .getPropertyInstance().clearAllValues();
276: this .m_valuePanels.remove(nIndex);
277: Iterator itor = this .m_valuePanels.iterator();
278: while (itor.hasNext()) {
279: PropertyValuePanel propValuePanel = (PropertyValuePanel) itor
280: .next();
281: this .getPropertyInstance().addValue(
282: propValuePanel.getValue());
283: }
284: } else if (ae.getActionCommand().equals("ADD")
285: && this .m_valuePanels.size() < this .m_nMaxOccurs) {
286: PropertyValue newValue = (PropertyValue) this
287: .getPropertyInstance().getNewValueInstance();
288: this .getPropertyInstance().addValue(newValue);
289: PropertyValuePanel valPanel = new PropertyValuePanel(this ,
290: this .getPropertyInstance(), newValue);
291: this .add(valPanel);
292: this .m_valuePanels.add(valPanel.getPanel());
293: this .m_nHeight = this .m_nHeight
294: + valPanel.getPreferredSize().height;
295: JButton removeButton = new JButton();
296: String fontName = "Dialog";
297: int fontSize = 13;
298: Font font = new Font(fontName, Font.BOLD, fontSize);
299: removeButton.setFont(font);
300: removeButton.setIcon(IconManager.getInstance().getIcon(
301: "16-command-value-minus.gif"));
302: removeButton.setActionCommand("REMOVE");
303: removeButton.addActionListener(this );
304: removeButton.setMargin(new Insets(2, 2, 2, 2));
305: this .add(removeButton);
306: this .m_removeButtons.add(removeButton);
307: }
308:
309: this .checkButtonVisibility();
310:
311: Component comp = this .getParent();
312: while (!(comp instanceof MetadataTabs)) {
313: if (comp instanceof PropertyRangeDisplay) {
314: comp.validate();
315: }
316: if (comp != null) {
317: comp = comp.getParent();
318: }
319: }
320: if (comp != null) {
321: comp.validate();
322: }
323: super .validateTab();
324: }
325:
326: private void checkButtonVisibility() {
327: if (this .m_bShowingDelButtons
328: && this .m_removeButtons.size() <= this .m_nMinOccurs) {
329: Iterator itor = this .m_removeButtons.iterator();
330: while (itor.hasNext()) {
331: JButton button = (JButton) itor.next();
332: button.setVisible(false);
333: }
334: this .m_bShowingDelButtons = false;
335: } else if (!this .m_bShowingDelButtons
336: && this .m_removeButtons.size() > this .m_nMinOccurs) {
337: Iterator itor = this .m_removeButtons.iterator();
338: while (itor.hasNext()) {
339: JButton button = (JButton) itor.next();
340: button.setVisible(true);
341: }
342: this .m_bShowingDelButtons = true;
343: }
344:
345: if (this .m_addButton != null && this .m_addButton.isVisible()
346: && this .m_valuePanels.size() >= this .m_nMaxOccurs) {
347: this .m_addButton.setVisible(false);
348: this .m_nHeight = this .m_nHeight
349: - this .m_addButton.getSize().height;
350: } else if (this .m_addButton != null
351: && this .m_valuePanels.size() < this .m_nMaxOccurs) {
352: this .m_addButton.setVisible(true);
353: this .m_nHeight = this .m_nHeight
354: + this .m_addButton.getSize().height;
355: }
356: }
357:
358: protected void valueChanged(String sValue) {
359: ArrayList aValues = new ArrayList();
360:
361: Iterator itor = this .m_valuePanels.iterator();
362: while (itor.hasNext()) {
363: PropertyValue tempValue = ((PropertyValuePanel) itor.next())
364: .getValue();
365: if (tempValue != null) {
366: aValues.add(tempValue);
367: }
368: }
369: if (!this .getPropertyInstance().getValues()
370: .containsAll(aValues)) {
371: this .getPropertyInstance().setValues(aValues);
372: }
373: }
374:
375: /* (non-Javadoc)
376: * @see com.simulacramedia.contentmanager.metadata.range.swing.AbstractRangeDisplay#isValid()
377: */
378: public boolean isMetadataValid() {
379: boolean bValid = true;
380: for (int i = 0; i < this .getComponentCount(); i++) {
381: Component comp = this .getComponent(i);
382: if (comp instanceof AbstractRangeDisplay) {
383: if (!((AbstractRangeDisplay) comp).isMetadataValid()) {
384: bValid = false;
385: }
386: }
387: }
388: return bValid;
389: }
390: }
|