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.stringhandling;
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.range.swing.propertyhandling.*;
030: import org.openharmonise.him.metadata.swing.*;
031: import org.openharmonise.vfs.*;
032: import org.openharmonise.vfs.gui.*;
033: import org.openharmonise.vfs.metadata.*;
034: import org.openharmonise.vfs.metadata.range.*;
035: import org.openharmonise.vfs.metadata.value.*;
036:
037: /**
038: * @author Matthew Large
039: *
040: */
041: public class StringRangeDisplay extends AbstractRangeDisplay implements
042: 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 StringRangeDisplay(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: StringRange range = (StringRange) this .getPropertyInstance()
075: .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: StringValuePanel valPanel = null;
118:
119: valPanel = new StringValuePanel(this , this
120: .getPropertyInstance(), ((StringValue) vals
121: .get(i)).getValue());
122: this .add(valPanel);
123: this .m_valuePanels.add(valPanel);
124: this .m_nHeight = this .m_nHeight
125: + valPanel.getPreferredSize().height + 5;
126:
127: } else {
128: StringValuePanel valPanel = null;
129:
130: valPanel = new StringValuePanel(this , this
131: .getPropertyInstance(), "");
132: this .add(valPanel);
133: this .m_valuePanels.add(valPanel);
134: this .m_nHeight = this .m_nHeight
135: + valPanel.getPreferredSize().height + 5;
136:
137: }
138: JButton removeButton = new JButton();
139: String fontName = "Dialog";
140: int fontSize = 13;
141: Font font = new Font(fontName, Font.BOLD, fontSize);
142: removeButton.setFont(font);
143: removeButton.setIcon(IconManager.getInstance().getIcon(
144: "16-command-value-minus.gif"));
145: removeButton.setActionCommand("REMOVE");
146: removeButton.addActionListener(this );
147: removeButton.setMargin(new Insets(2, 2, 2, 2));
148: removeButton.setFocusable(false);
149: this .add(removeButton);
150: this .m_removeButtons.add(removeButton);
151: } catch (ClassCastException cce) {
152:
153: }
154: }
155:
156: if (this .m_nMaxOccurs == -1
157: || 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: m_addButton.setFocusable(false);
171: }
172:
173: this .checkButtonVisibility();
174:
175: return this ;
176: }
177:
178: /* (non-Javadoc)
179: * @see java.awt.Component#getPreferredSize()
180: */
181: public Dimension getPreferredSize() {
182: this .layoutContainer(null);
183: int nWidth = this .getParent().getWidth() - 25;
184: return new Dimension(nWidth, this .m_nHeight);
185: }
186:
187: /* (non-Javadoc)
188: * @see java.awt.LayoutManager#removeLayoutComponent(java.awt.Component)
189: */
190: public void removeLayoutComponent(Component arg0) {
191: }
192:
193: /* (non-Javadoc)
194: * @see java.awt.LayoutManager#layoutContainer(java.awt.Container)
195: */
196: public void layoutContainer(Container arg0) {
197: if (this .getParent() instanceof PropertyValuePanel) {
198: this .m_nMinOccurs = 0;
199: this .m_nMaxOccurs = 1;
200: }
201:
202: this .checkButtonVisibility();
203: int nWidth = this .getParent().getWidth() - 50;
204:
205: int nYPos = 0;
206:
207: int nXPos = 0;
208:
209: Iterator itor = this .m_valuePanels.iterator();
210: int nCount = 0;
211: while (itor.hasNext()) {
212: StringValuePanel valuePanel = (StringValuePanel) itor
213: .next();
214: valuePanel.setSize(
215: valuePanel.getPreferredSize().width - 50,
216: valuePanel.getPreferredSize().height);
217: valuePanel.setLocation(0, nYPos);
218: nXPos = valuePanel.getLocation().x
219: + valuePanel.getSize().width;
220:
221: JButton removeButton = (JButton) this .m_removeButtons
222: .get(nCount);
223: if (removeButton.isVisible()) {
224: removeButton.setSize(20, 20);
225: removeButton.setLocation(nXPos, nYPos);
226: }
227:
228: nYPos = nYPos + valuePanel.getSize().height;
229:
230: nCount++;
231: }
232:
233: if (this .m_addButton != null && this .m_addButton.isVisible()) {
234: this .m_addButton.setSize(20, 20);
235: this .m_addButton.setLocation(nXPos + 30, nYPos - 30);
236: }
237:
238: this .m_nHeight = nYPos - 10;
239:
240: this .repaint();
241: }
242:
243: /* (non-Javadoc)
244: * @see java.awt.LayoutManager#addLayoutComponent(java.lang.String, java.awt.Component)
245: */
246: public void addLayoutComponent(String arg0, Component arg1) {
247: }
248:
249: /* (non-Javadoc)
250: * @see java.awt.LayoutManager#minimumLayoutSize(java.awt.Container)
251: */
252: public Dimension minimumLayoutSize(Container arg0) {
253: return this .getPreferredSize();
254: }
255:
256: /* (non-Javadoc)
257: * @see java.awt.LayoutManager#preferredLayoutSize(java.awt.Container)
258: */
259: public Dimension preferredLayoutSize(Container arg0) {
260: return this .getPreferredSize();
261: }
262:
263: /* (non-Javadoc)
264: * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
265: */
266: public void actionPerformed(ActionEvent ae) {
267: PropertyInstance propInst = this .getPropertyInstance();
268:
269: if (ae.getActionCommand().equals("REMOVE")
270: && this .m_valuePanels.size() > this .m_nMinOccurs) {
271: int nIndex = this .m_removeButtons.indexOf(ae.getSource());
272: this .remove((Component) this .m_removeButtons.get(nIndex));
273: this .m_removeButtons.remove(nIndex);
274: this .m_nHeight = this .m_nHeight
275: - ((Component) this .m_valuePanels.get(nIndex))
276: .getSize().height;
277: this .remove((Component) this .m_valuePanels.get(nIndex));
278: this .m_valuePanels.remove(nIndex);
279: this .valueChanged(null);
280: } else if (ae.getActionCommand().equals("ADD")
281: && this .m_valuePanels.size() < this .m_nMaxOccurs) {
282: StringValuePanel valPanel = new StringValuePanel(this , this
283: .getPropertyInstance(), "");
284: this .add(valPanel);
285: this .m_valuePanels.add(valPanel);
286: this .m_nHeight = this .m_nHeight
287: + valPanel.getPreferredSize().height;
288: JButton removeButton = new JButton();
289: String fontName = "Dialog";
290: int fontSize = 13;
291: Font font = new Font(fontName, Font.BOLD, fontSize);
292: removeButton.setFont(font);
293: removeButton.setIcon(IconManager.getInstance().getIcon(
294: "16-command-value-minus.gif"));
295: removeButton.setActionCommand("REMOVE");
296: removeButton.addActionListener(this );
297: removeButton.setMargin(new Insets(2, 2, 2, 2));
298: this .add(removeButton);
299: this .m_removeButtons.add(removeButton);
300: }
301:
302: this .checkButtonVisibility();
303:
304: Component comp = this .getParent();
305: comp.validate();
306: while (!(comp instanceof MetadataTabs)) {
307: if (comp != null) {
308: comp = comp.getParent();
309: }
310: }
311: if (comp != null && comp instanceof MetadataTabs) {
312: comp.validate();
313: }
314: super .validateTab();
315: }
316:
317: private void checkButtonVisibility() {
318: if (this .m_nMinOccurs == 0 && this .m_nMaxOccurs == 1) {
319: Iterator itor = this .m_removeButtons.iterator();
320: while (itor.hasNext()) {
321: JButton button = (JButton) itor.next();
322: button.setVisible(false);
323: }
324: this .m_addButton.setVisible(false);
325: this .m_bShowingDelButtons = false;
326: return;
327: }
328: if (this .m_bShowingDelButtons
329: && this .m_removeButtons.size() <= this .m_nMinOccurs) {
330: Iterator itor = this .m_removeButtons.iterator();
331: while (itor.hasNext()) {
332: JButton button = (JButton) itor.next();
333: button.setVisible(false);
334: }
335: this .m_bShowingDelButtons = false;
336: } else if (!this .m_bShowingDelButtons
337: && this .m_removeButtons.size() > this .m_nMinOccurs) {
338: Iterator itor = this .m_removeButtons.iterator();
339: while (itor.hasNext()) {
340: JButton button = (JButton) itor.next();
341: button.setVisible(true);
342: }
343: this .m_bShowingDelButtons = true;
344: }
345:
346: if (this .m_addButton != null && this .m_addButton.isVisible()
347: && this .m_valuePanels.size() >= this .m_nMaxOccurs) {
348: this .m_addButton.setVisible(false);
349: this .m_nHeight = this .m_nHeight
350: - this .m_addButton.getSize().height;
351: } else if (this .m_addButton != null
352: && this .m_valuePanels.size() < this .m_nMaxOccurs) {
353: this .m_addButton.setVisible(true);
354: this .m_nHeight = this .m_nHeight
355: + this .m_addButton.getSize().height;
356: }
357: }
358:
359: protected void valueChanged(String sValue) {
360: ArrayList aValues = new ArrayList();
361:
362: Iterator itor = this .m_valuePanels.iterator();
363: while (itor.hasNext()) {
364: String sTempValue = ((StringValuePanel) itor.next())
365: .getValue();
366: if (!sTempValue.equals("")) {
367: StringValue val = (StringValue) this
368: .getPropertyInstance().getNewValueInstance();
369: val.setValue(sTempValue);
370: aValues.add(val);
371: }
372: }
373: if (!this .getPropertyInstance().getValues()
374: .containsAll(aValues)
375: || !aValues.containsAll(this .getPropertyInstance()
376: .getValues())) {
377: this .getPropertyInstance().setValues(aValues);
378: }
379: }
380:
381: /* (non-Javadoc)
382: * @see com.simulacramedia.contentmanager.metadata.range.swing.AbstractRangeDisplay#isValid()
383: */
384: public boolean isMetadataValid() {
385: boolean bValid = true;
386: for (int i = 0; i < this .getComponentCount(); i++) {
387: Component comp = this .getComponent(i);
388: if (comp instanceof AbstractRangeDisplay) {
389: if (!((AbstractRangeDisplay) comp).isMetadataValid()) {
390: bValid = false;
391: }
392: }
393: }
394: return bValid;
395: }
396: }
|