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.integerhandling;
020:
021: import java.awt.Component;
022: import java.awt.Container;
023: import java.awt.Dimension;
024: import java.awt.Font;
025: import java.awt.Insets;
026: import java.awt.LayoutManager;
027: import java.awt.event.ActionEvent;
028: import java.awt.event.ActionListener;
029: import java.util.ArrayList;
030: import java.util.Iterator;
031: import java.util.List;
032:
033: import javax.swing.BoxLayout;
034: import javax.swing.JButton;
035: import javax.swing.JPanel;
036:
037: import org.openharmonise.him.metadata.range.swing.*;
038: import org.openharmonise.him.metadata.range.swing.propertyhandling.*;
039: import org.openharmonise.him.metadata.swing.*;
040: import org.openharmonise.vfs.*;
041: import org.openharmonise.vfs.gui.*;
042: import org.openharmonise.vfs.metadata.*;
043: import org.openharmonise.vfs.metadata.range.*;
044: import org.openharmonise.vfs.metadata.value.*;
045:
046: /**
047: * @author Matthew Large
048: *
049: */
050: public class IntegerRangeDisplay extends AbstractRangeDisplay implements
051: RangeDisplay, LayoutManager, ActionListener {
052:
053: private int m_nHeight = 10;
054:
055: private int m_nMinOccurs = 1;
056: private int m_nMaxOccurs = 1000;
057:
058: private JButton m_addButton = null;
059:
060: private ArrayList m_valuePanels = new ArrayList();
061: private ArrayList m_removeButtons = new ArrayList();
062:
063: private boolean m_bShowingDelButtons = true;
064:
065: /**
066: * @param propInstance
067: */
068: public IntegerRangeDisplay(PropertyInstance propInstance) {
069: super (propInstance);
070: this .setup();
071: }
072:
073: private void setup() {
074: BoxLayout layout = new BoxLayout(this , BoxLayout.Y_AXIS);
075: this .setLayout(this );
076: }
077:
078: /* (non-Javadoc)
079: * @see com.simulacramedia.contentmanager.metadata.range.swing.RangeDisplay#getPanel()
080: */
081: public JPanel getPanel() {
082: IntegerRange range = (IntegerRange) this .getPropertyInstance()
083: .getDefinition().getRange();
084:
085: List domains = this .getPropertyInstance().getDefinition()
086: .getDomains();
087: Iterator itor = domains.iterator();
088:
089: while (itor.hasNext()) {
090: Domain domain = (Domain) itor.next();
091: boolean bApplicableDomain = false;
092: String sFilePath = ((VersionedVirtualFile) this
093: .getPropertyInstance().getVirtualFile())
094: .getLogicalPath();
095: Iterator itor2 = domain.getPaths().iterator();
096: while (itor2.hasNext()) {
097: String sDomainPath = (String) itor2.next();
098: if (sFilePath.startsWith(sDomainPath)) {
099: bApplicableDomain = true;
100: }
101: }
102:
103: if (bApplicableDomain) {
104: if (this .m_nMinOccurs < domain.getMinOccurs()) {
105: this .m_nMinOccurs = domain.getMinOccurs();
106: }
107: if (domain.getMaxOccurs() != -1
108: && this .m_nMaxOccurs > domain.getMaxOccurs()) {
109: this .m_nMaxOccurs = domain.getMaxOccurs();
110: }
111: }
112:
113: }
114:
115: PropertyInstance propInst = this .getPropertyInstance();
116: List vals = propInst.getValues();
117:
118: int nCountMax = this .m_nMinOccurs;
119: if (vals.size() > nCountMax) {
120: nCountMax = vals.size();
121: }
122:
123: for (int i = 0; i < nCountMax; i++) {
124:
125: if (i < vals.size()) {
126: IntegerValuePanel valPanel = new IntegerValuePanel(
127: this , this .getPropertyInstance(), Integer
128: .toString(((IntegerValue) vals.get(i))
129: .getValue()));
130: this .add(valPanel);
131: this .m_valuePanels.add(valPanel);
132: this .m_nHeight = this .m_nHeight
133: + valPanel.getPreferredSize().height + 5;
134: } else {
135: IntegerValuePanel valPanel = new IntegerValuePanel(
136: this , this .getPropertyInstance(), "");
137: this .add(valPanel);
138: this .m_valuePanels.add(valPanel);
139: this .m_nHeight = this .m_nHeight
140: + valPanel.getPreferredSize().height + 5;
141: }
142: JButton removeButton = new JButton();
143: String fontName = "Dialog";
144: int fontSize = 13;
145: Font font = new Font(fontName, Font.BOLD, fontSize);
146: removeButton.setFont(font);
147: removeButton.setIcon(IconManager.getInstance().getIcon(
148: "16-command-value-minus.gif"));
149: removeButton.setActionCommand("REMOVE");
150: removeButton.addActionListener(this );
151: removeButton.setMargin(new Insets(2, 2, 2, 2));
152: this .add(removeButton);
153: this .m_removeButtons.add(removeButton);
154:
155: }
156:
157: if (this .m_valuePanels.size() < this .m_nMaxOccurs) {
158: this .m_addButton = new JButton();
159: String fontName = "Dialog";
160: int fontSize = 13;
161: Font font = new Font(fontName, Font.BOLD, fontSize);
162: this .m_addButton.setFont(font);
163: this .m_addButton.setIcon(IconManager.getInstance().getIcon(
164: "16-command-value-plus.gif"));
165: this .m_addButton.setActionCommand("ADD");
166: this .m_addButton.setMargin(new Insets(2, 2, 2, 2));
167: this .add(this .m_addButton);
168: this .m_addButton.addActionListener(this );
169: this .m_nHeight = this .m_nHeight + 25;
170: }
171:
172: return this ;
173: }
174:
175: /* (non-Javadoc)
176: * @see java.awt.Component#getPreferredSize()
177: */
178: public Dimension getPreferredSize() {
179: this .layoutContainer(null);
180: int nWidth = this .getParent().getWidth() - 25;
181: return new Dimension(nWidth, this .m_nHeight);
182: }
183:
184: /* (non-Javadoc)
185: * @see java.awt.LayoutManager#removeLayoutComponent(java.awt.Component)
186: */
187: public void removeLayoutComponent(Component arg0) {
188: }
189:
190: /* (non-Javadoc)
191: * @see java.awt.LayoutManager#layoutContainer(java.awt.Container)
192: */
193: public void layoutContainer(Container arg0) {
194: if (this .getParent() instanceof PropertyValuePanel) {
195: this .m_nMinOccurs = 0;
196: this .m_nMaxOccurs = 1;
197: }
198:
199: this .checkButtonVisibility();
200: int nWidth = this .getParent().getWidth() - 50;
201:
202: int nYPos = 0;
203:
204: int nXPos = 0;
205:
206: Iterator itor = this .m_valuePanels.iterator();
207: int nCount = 0;
208: while (itor.hasNext()) {
209: IntegerValuePanel valuePanel = (IntegerValuePanel) itor
210: .next();
211: valuePanel.setSize(
212: valuePanel.getPreferredSize().width - 50,
213: valuePanel.getPreferredSize().height);
214: valuePanel.setLocation(0, 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(nXPos, nYPos);
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(nXPos + 30, nYPos - 30);
233: }
234:
235: this .m_nHeight = nYPos - 10;
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 .m_valuePanels.remove(nIndex);
276: this .valueChanged(null);
277: } else if (ae.getActionCommand().equals("ADD")
278: && this .m_valuePanels.size() < this .m_nMaxOccurs) {
279: IntegerValuePanel valPanel = new IntegerValuePanel(this ,
280: this .getPropertyInstance(), "");
281: this .add(valPanel);
282: this .m_valuePanels.add(valPanel);
283: this .m_nHeight = this .m_nHeight
284: + valPanel.getPreferredSize().height;
285: JButton removeButton = new JButton();
286: String fontName = "Dialog";
287: int fontSize = 13;
288: Font font = new Font(fontName, Font.BOLD, fontSize);
289: removeButton.setFont(font);
290: removeButton.setIcon(IconManager.getInstance().getIcon(
291: "16-command-value-minus.gif"));
292: removeButton.setActionCommand("REMOVE");
293: removeButton.addActionListener(this );
294: removeButton.setMargin(new Insets(2, 2, 2, 2));
295: this .add(removeButton);
296: this .m_removeButtons.add(removeButton);
297: }
298:
299: this .checkButtonVisibility();
300:
301: Component comp = this .getParent();
302: comp.validate();
303: while (!(comp instanceof MetadataTabs)) {
304: if (comp != null) {
305: comp = comp.getParent();
306: }
307: }
308: if (comp != null) {
309: comp.validate();
310: }
311: }
312:
313: private void checkButtonVisibility() {
314: if (this .m_nMinOccurs == 0 && this .m_nMaxOccurs == 1) {
315: Iterator itor = this .m_removeButtons.iterator();
316: while (itor.hasNext()) {
317: JButton button = (JButton) itor.next();
318: button.setVisible(false);
319: }
320: this .m_addButton.setVisible(false);
321: this .m_bShowingDelButtons = false;
322: return;
323: }
324: if (this .m_bShowingDelButtons
325: && this .m_removeButtons.size() <= this .m_nMinOccurs) {
326: Iterator itor = this .m_removeButtons.iterator();
327: while (itor.hasNext()) {
328: JButton button = (JButton) itor.next();
329: button.setVisible(false);
330: }
331: this .m_bShowingDelButtons = false;
332: } else if (!this .m_bShowingDelButtons
333: && this .m_removeButtons.size() > this .m_nMinOccurs) {
334: Iterator itor = this .m_removeButtons.iterator();
335: while (itor.hasNext()) {
336: JButton button = (JButton) itor.next();
337: button.setVisible(true);
338: }
339: this .m_bShowingDelButtons = true;
340: }
341: if (this .m_addButton != null && this .m_addButton.isVisible()
342: && this .m_valuePanels.size() >= this .m_nMaxOccurs) {
343: this .m_addButton.setVisible(false);
344: this .m_nHeight = this .m_nHeight
345: - this .m_addButton.getSize().height;
346: } else if (this .m_addButton != null
347: && this .m_valuePanels.size() < this .m_nMaxOccurs) {
348: this .m_addButton.setVisible(true);
349: this .m_nHeight = this .m_nHeight
350: + this .m_addButton.getSize().height;
351: }
352:
353: }
354:
355: protected void valueChanged(String sValue) {
356: ArrayList aValues = new ArrayList();
357:
358: Iterator itor = this .m_valuePanels.iterator();
359: while (itor.hasNext()) {
360: String sVal = ((IntegerValuePanel) itor.next()).getValue();
361: if (!sVal.equals("")) {
362: IntegerValue val = (IntegerValue) this
363: .getPropertyInstance().getNewValueInstance();
364: val.setValue(Integer.parseInt(sVal));
365: aValues.add(val);
366: }
367: }
368: if (!this .getPropertyInstance().getValues()
369: .containsAll(aValues)
370: || !aValues.containsAll(this .getPropertyInstance()
371: .getValues())) {
372: this .getPropertyInstance().setValues(aValues);
373: }
374: }
375:
376: /* (non-Javadoc)
377: * @see com.simulacramedia.contentmanager.metadata.range.swing.AbstractRangeDisplay#isValid()
378: */
379: public boolean isMetadataValid() {
380: boolean bValid = true;
381: for (int i = 0; i < this .getComponentCount(); i++) {
382: Component comp = this .getComponent(i);
383: if (comp instanceof AbstractRangeDisplay) {
384: if (!((AbstractRangeDisplay) comp).isMetadataValid()) {
385: bValid = false;
386: }
387: }
388: }
389: return bValid;
390: }
391: }
|