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.floathandling;
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 FloatRangeDisplay 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 FloatRangeDisplay(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: FloatRange range = (FloatRange) this .getPropertyInstance()
083: .getDefinition().getRange();
084: List domains = this .getPropertyInstance().getDefinition()
085: .getDomains();
086: Iterator itor = domains.iterator();
087:
088: while (itor.hasNext()) {
089: Domain domain = (Domain) itor.next();
090: boolean bApplicableDomain = false;
091: String sFilePath = ((VersionedVirtualFile) this
092: .getPropertyInstance().getVirtualFile())
093: .getLogicalPath();
094: Iterator itor2 = domain.getPaths().iterator();
095: while (itor2.hasNext()) {
096: String sDomainPath = (String) itor2.next();
097: if (sFilePath.startsWith(sDomainPath)) {
098: bApplicableDomain = true;
099: }
100: }
101:
102: if (bApplicableDomain) {
103: if (this .m_nMinOccurs < domain.getMinOccurs()) {
104: this .m_nMinOccurs = domain.getMinOccurs();
105: }
106: if (domain.getMaxOccurs() != -1
107: && this .m_nMaxOccurs > domain.getMaxOccurs()) {
108: this .m_nMaxOccurs = domain.getMaxOccurs();
109: }
110: }
111:
112: }
113:
114: PropertyInstance propInst = this .getPropertyInstance();
115: List vals = propInst.getValues();
116:
117: int nCountMax = this .m_nMinOccurs;
118: if (vals.size() > nCountMax) {
119: nCountMax = vals.size();
120: }
121:
122: for (int i = 0; i < nCountMax; i++) {
123:
124: if (i < vals.size()) {
125: FloatValuePanel valPanel = new FloatValuePanel(this ,
126: this .getPropertyInstance(), Float
127: .toString(((FloatValue) vals.get(i))
128: .getValue()));
129: this .add(valPanel);
130: this .m_valuePanels.add(valPanel);
131: this .m_nHeight = this .m_nHeight
132: + valPanel.getPreferredSize().height + 5;
133: } else {
134: FloatValuePanel valPanel = new FloatValuePanel(this ,
135: this .getPropertyInstance(), "");
136: this .add(valPanel);
137: this .m_valuePanels.add(valPanel);
138: this .m_nHeight = this .m_nHeight
139: + valPanel.getPreferredSize().height + 5;
140: }
141: JButton removeButton = new JButton();
142: String fontName = "Dialog";
143: int fontSize = 13;
144: Font font = new Font(fontName, Font.BOLD, fontSize);
145: removeButton.setFont(font);
146: removeButton.setIcon(IconManager.getInstance().getIcon(
147: "16-command-value-minus.gif"));
148: removeButton.setActionCommand("REMOVE");
149: removeButton.addActionListener(this );
150: removeButton.setMargin(new Insets(2, 2, 2, 2));
151: this .add(removeButton);
152: this .m_removeButtons.add(removeButton);
153:
154: }
155:
156: if (this .m_valuePanels.size() < this .m_nMaxOccurs) {
157: this .m_addButton = new JButton();
158: String fontName = "Dialog";
159: int fontSize = 13;
160: Font font = new Font(fontName, Font.BOLD, fontSize);
161: this .m_addButton.setFont(font);
162: this .m_addButton.setIcon(IconManager.getInstance().getIcon(
163: "16-command-value-plus.gif"));
164: this .m_addButton.setActionCommand("ADD");
165: this .m_addButton.setMargin(new Insets(2, 2, 2, 2));
166: this .add(this .m_addButton);
167: this .m_addButton.addActionListener(this );
168: this .m_nHeight = this .m_nHeight + 25;
169: }
170:
171: this .checkButtonVisibility();
172:
173: return this ;
174: }
175:
176: /* (non-Javadoc)
177: * @see java.awt.Component#getPreferredSize()
178: */
179: public Dimension getPreferredSize() {
180: this .layoutContainer(null);
181: int nWidth = this .getParent().getWidth() - 25;
182: return new Dimension(nWidth, this .m_nHeight);
183: }
184:
185: /* (non-Javadoc)
186: * @see java.awt.LayoutManager#removeLayoutComponent(java.awt.Component)
187: */
188: public void removeLayoutComponent(Component arg0) {
189: }
190:
191: /* (non-Javadoc)
192: * @see java.awt.LayoutManager#layoutContainer(java.awt.Container)
193: */
194: public void layoutContainer(Container arg0) {
195: if (this .getParent() instanceof PropertyValuePanel) {
196: this .m_nMinOccurs = 0;
197: this .m_nMaxOccurs = 1;
198: }
199:
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: FloatValuePanel valuePanel = (FloatValuePanel) itor.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: FloatValuePanel valPanel = new FloatValuePanel(this , this
280: .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 sTempValue = ((FloatValuePanel) itor.next())
361: .getValue();
362: if (!sTempValue.equals("")) {
363: FloatValue val = (FloatValue) this
364: .getPropertyInstance().getNewValueInstance();
365: val.setValue(Float.parseFloat(sTempValue));
366: aValues.add(val);
367: }
368: }
369: if (!this .getPropertyInstance().getValues()
370: .containsAll(aValues)
371: || !aValues.containsAll(this .getPropertyInstance()
372: .getValues())) {
373: this .getPropertyInstance().setValues(aValues);
374: }
375: }
376:
377: /* (non-Javadoc)
378: * @see com.simulacramedia.contentmanager.metadata.range.swing.AbstractRangeDisplay#isValid()
379: */
380: public boolean isMetadataValid() {
381: boolean bValid = true;
382: for (int i = 0; i < this .getComponentCount(); i++) {
383: Component comp = this .getComponent(i);
384: if (comp instanceof AbstractRangeDisplay) {
385: if (!((AbstractRangeDisplay) comp).isMetadataValid()) {
386: bValid = false;
387: }
388: }
389: }
390: return bValid;
391: }
392: }
|