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