001: /*
002: * (C) Copyright SimulacraMedia 2003. All rights reserved.
003: *
004: * Created on 21-Jan-2004
005: *
006: */
007: package org.openharmonise.him.metadata.range.swing.domain;
008:
009: import java.awt.Color;
010: import java.awt.Component;
011: import java.awt.Container;
012: import java.awt.Dimension;
013: import java.awt.LayoutManager;
014: import java.awt.event.ActionEvent;
015: import java.awt.event.ActionListener;
016: import java.awt.event.MouseEvent;
017: import java.awt.event.MouseListener;
018: import java.util.ArrayList;
019: import java.util.List;
020:
021: import javax.swing.JComboBox;
022: import javax.swing.JLabel;
023: import javax.swing.JPanel;
024: import javax.swing.JSpinner;
025: import javax.swing.SpinnerModel;
026: import javax.swing.SpinnerNumberModel;
027: import javax.swing.event.ChangeEvent;
028: import javax.swing.event.ChangeListener;
029:
030: import org.openharmonise.him.*;
031: import org.openharmonise.swing.FontManager;
032: import org.openharmonise.vfs.*;
033: import org.openharmonise.vfs.metadata.value.*;
034: import org.openharmonise.vfs.servers.ServerList;
035:
036: /**
037: * FIXME - Matthew Large DIDN'T GIVE ME A DESCRIPTION!!
038: * @author Matthew Large
039: * @version $Revision: 1.2 $
040: *
041: */
042: public class DomainSelectionCell extends JPanel implements
043: MouseListener, LayoutManager, ActionListener, ChangeListener {
044:
045: private boolean m_bSelected = false;
046:
047: private Color m_selectedColor = new Color(173, 169, 143);
048:
049: private JLabel m_collectionLabel = null;
050:
051: private JComboBox m_typeCombo = null;
052:
053: private JSpinner m_minOccursSpinner = null;
054: private JSpinner m_maxOccursSpinner = null;
055:
056: private DomainValue m_valueResource = null;
057: private DomainValue m_valueCollection = null;
058:
059: public static String TYPE_COLLECTION = "Collections";
060: public static String TYPE_RESOURCE = "Resources";
061: public static String TYPE_BOTH = "Both";
062:
063: private DomainSelectionList m_list = null;
064:
065: private boolean m_bListenToCombo = false;
066:
067: private boolean m_bOccursSpinnersEnabled = true;
068:
069: /**
070: *
071: */
072: public DomainSelectionCell(DomainSelectionList list,
073: DomainValue value) throws Exception {
074: super ();
075: this .m_list = list;
076: if (value.getResourceType() == DomainValue.RESOURCE) {
077: this .m_valueResource = value;
078: } else if (value.getResourceType() == DomainValue.COLLECTION) {
079: this .m_valueCollection = value;
080: }
081: this .setup();
082: }
083:
084: public void setOccursSpinnersEnabled(boolean bEnabled) {
085: m_bOccursSpinnersEnabled = bEnabled;
086: if (this .m_typeCombo.getSelectedItem().equals(
087: DomainSelectionCell.TYPE_COLLECTION)
088: || this .m_typeCombo.getSelectedItem().equals(
089: DomainSelectionCell.TYPE_BOTH) || !bEnabled) {
090: this .m_minOccursSpinner.setEnabled(false);
091: } else if (this .m_typeCombo.getSelectedItem().equals(
092: DomainSelectionCell.TYPE_RESOURCE)) {
093: this .m_minOccursSpinner.setEnabled(bEnabled);
094: }
095: this .m_maxOccursSpinner.setEnabled(bEnabled);
096: }
097:
098: protected String getPath() {
099: if (this .m_valueResource != null) {
100: return this .m_valueResource.getPath();
101: } else if (this .m_valueCollection != null) {
102: return this .m_valueCollection.getPath();
103: } else {
104: return null;
105: }
106: }
107:
108: protected void setListenToCombo(boolean bListenToCombo) {
109: this .m_bListenToCombo = bListenToCombo;
110: }
111:
112: protected boolean isSelected() {
113: if (this .getBackground() == this .m_selectedColor) {
114: return true;
115: } else {
116: return false;
117: }
118: }
119:
120: private void setup() throws Exception {
121: this .setLayout(this );
122:
123: this .setBackground(Color.WHITE);
124: this .setOpaque(true);
125:
126: AbstractVirtualFileSystem vfs = ServerList.getInstance()
127: .getHarmoniseServer().getVFS();
128: VirtualFile vfFile = null;
129: if (this .m_valueResource != null) {
130: vfFile = vfs.getVirtualFile(this .m_valueResource.getPath())
131: .getResource();
132: } else if (this .m_valueCollection != null) {
133: vfFile = vfs.getVirtualFile(
134: this .m_valueCollection.getPath()).getResource();
135: }
136:
137: int nMin = 0;
138: int nMax = -1;
139:
140: if (vfFile == null) {
141: throw new Exception("Domain resource is null..");
142: }
143: this .m_collectionLabel = new JLabel(vfs
144: .getVirtualFileSystemView().getDisplayName(vfFile));
145: this .m_collectionLabel.setIcon(vfs.getVirtualFileSystemView()
146: .getIcon(vfFile));
147: this .m_collectionLabel.addMouseListener(this );
148: this .m_collectionLabel.setOpaque(false);
149: this .m_collectionLabel.setFont(FontManager.getInstance()
150: .getFont(FontManager.FONT_RESOURCE_TITLE));
151: this .add(m_collectionLabel);
152:
153: String[] sData = new String[] {
154: DomainSelectionCell.TYPE_RESOURCE,
155: DomainSelectionCell.TYPE_COLLECTION,
156: DomainSelectionCell.TYPE_BOTH };
157: this .m_typeCombo = new JComboBox(sData);
158: this .m_typeCombo.setActionCommand("TYPE");
159: this .m_typeCombo.addActionListener(this );
160: this .m_bListenToCombo = false;
161:
162: if (this .m_valueCollection != null
163: && this .m_valueResource == null) {
164: nMin = this .m_valueCollection.getMinOccurs();
165: nMax = this .m_valueCollection.getMaxOccurs();
166: this .m_typeCombo
167: .setSelectedItem(DomainSelectionCell.TYPE_COLLECTION);
168: } else if (this .m_valueCollection == null
169: && this .m_valueResource != null) {
170: nMin = this .m_valueResource.getMinOccurs();
171: nMax = this .m_valueResource.getMaxOccurs();
172: this .m_typeCombo
173: .setSelectedItem(DomainSelectionCell.TYPE_RESOURCE);
174: } else if (this .m_valueCollection != null
175: && this .m_valueResource != null) {
176: if (this .m_valueCollection.getMinOccurs() < this .m_valueResource
177: .getMinOccurs()) {
178: nMin = this .m_valueCollection.getMinOccurs();
179: } else {
180: nMin = this .m_valueResource.getMinOccurs();
181: }
182: if (this .m_valueCollection.getMaxOccurs() < this .m_valueResource
183: .getMaxOccurs()) {
184: nMax = this .m_valueCollection.getMaxOccurs();
185: } else {
186: nMax = this .m_valueResource.getMaxOccurs();
187: }
188: this .m_typeCombo
189: .setSelectedItem(DomainSelectionCell.TYPE_BOTH);
190: }
191: this .m_bListenToCombo = true;
192: this .add(this .m_typeCombo);
193:
194: SpinnerModel model = new SpinnerNumberModel(nMin, 0, 1000, 1);
195: this .m_minOccursSpinner = new JSpinner(model);
196: this .m_minOccursSpinner.addChangeListener(this );
197: this .m_minOccursSpinner.setEditor(new JSpinner.NumberEditor(
198: this .m_minOccursSpinner));
199: this .add(this .m_minOccursSpinner);
200:
201: model = new SpinnerNumberModel(nMax, -1, 1000, 1);
202: this .m_maxOccursSpinner = new JSpinner(model);
203: this .m_maxOccursSpinner.addChangeListener(this );
204: this .m_maxOccursSpinner.setEditor(new JSpinner.NumberEditor(
205: this .m_maxOccursSpinner));
206: this .add(this .m_maxOccursSpinner);
207: if (nMax == -1) {
208: this .setMaxSpinnerCleared(true);
209: }
210:
211: if (this .m_typeCombo.getSelectedItem().equals(
212: DomainSelectionCell.TYPE_COLLECTION)
213: || this .m_typeCombo.getSelectedItem().equals(
214: DomainSelectionCell.TYPE_BOTH)) {
215: this .m_minOccursSpinner.setValue(new Integer(0));
216:
217: if (this .m_valueCollection != null) {
218: this .m_valueCollection.setMinOccurs(0);
219: }
220: if (this .m_valueResource != null) {
221: this .m_valueResource.setMinOccurs(0);
222: }
223:
224: this .m_minOccursSpinner.setEnabled(false);
225: }
226:
227: }
228:
229: public void setType(String sType) {
230: this .m_typeCombo.setSelectedItem(sType);
231: }
232:
233: /* (non-Javadoc)
234: * @see javax.swing.event.ChangeListener#stateChanged(javax.swing.event.ChangeEvent)
235: */
236: public void stateChanged(ChangeEvent ce) {
237: if (ce.getSource() == this .m_maxOccursSpinner) {
238: if (((Integer) this .m_maxOccursSpinner.getValue())
239: .intValue() == -1) {
240: this .setMaxSpinnerCleared(true);
241: if (this .m_valueCollection != null) {
242: this .m_valueCollection.setMaxOccurs(-1);
243: }
244: if (this .m_valueResource != null) {
245: this .m_valueResource.setMaxOccurs(-1);
246: }
247: } else {
248: this .setMaxSpinnerCleared(false);
249: int nMaxVal = ((Integer) ((SpinnerNumberModel) this .m_maxOccursSpinner
250: .getModel()).getValue()).intValue();
251: int nMinVal = ((Integer) ((SpinnerNumberModel) this .m_minOccursSpinner
252: .getModel()).getValue()).intValue();
253: if (nMaxVal < nMinVal) {
254: this .m_minOccursSpinner.setValue(new Integer(
255: nMaxVal));
256: }
257: if (this .m_valueCollection != null) {
258: this .m_valueCollection.setMaxOccurs(nMaxVal);
259: }
260: if (this .m_valueResource != null) {
261: this .m_valueResource.setMaxOccurs(nMaxVal);
262: }
263: }
264: this .m_list.domainValuesChanged();
265: } else if (ce.getSource() == this .m_minOccursSpinner) {
266: this .m_list.domainValuesChanged();
267: int nMaxVal = ((Integer) ((SpinnerNumberModel) this .m_maxOccursSpinner
268: .getModel()).getValue()).intValue();
269: int nMinVal = ((Integer) ((SpinnerNumberModel) this .m_minOccursSpinner
270: .getModel()).getValue()).intValue();
271: if (nMinVal > nMaxVal) {
272: this .m_maxOccursSpinner.setValue(new Integer(nMinVal));
273: }
274: if (this .m_valueCollection != null) {
275: this .m_valueCollection.setMinOccurs(nMinVal);
276: }
277: if (this .m_valueResource != null) {
278: this .m_valueResource.setMinOccurs(nMinVal);
279: }
280: }
281: }
282:
283: private void setMaxSpinnerCleared(boolean bCleared) {
284: if (bCleared) {
285: ((JSpinner.DefaultEditor) ((JSpinner) this .m_maxOccursSpinner)
286: .getEditor()).getTextField().setForeground(
287: Color.WHITE);
288: ((JSpinner.DefaultEditor) ((JSpinner) this .m_maxOccursSpinner)
289: .getEditor()).getTextField().setSelectedTextColor(
290: Color.WHITE);
291: ((JSpinner.DefaultEditor) ((JSpinner) this .m_maxOccursSpinner)
292: .getEditor()).getTextField().setSelectionColor(
293: Color.WHITE);
294: } else {
295: ((JSpinner.DefaultEditor) ((JSpinner) this .m_maxOccursSpinner)
296: .getEditor()).getTextField().setForeground(
297: Color.BLACK);
298: ((JSpinner.DefaultEditor) ((JSpinner) this .m_maxOccursSpinner)
299: .getEditor()).getTextField().setSelectedTextColor(
300: Color.WHITE);
301: ((JSpinner.DefaultEditor) ((JSpinner) this .m_maxOccursSpinner)
302: .getEditor()).getTextField().setSelectionColor(
303: Color.BLUE);
304: }
305: }
306:
307: /* (non-Javadoc)
308: * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
309: */
310: public void mouseClicked(MouseEvent me) {
311: if (me.getSource() == this .m_collectionLabel) {
312: this .m_list.cellSelected(this );
313: }
314: }
315:
316: protected void setCellSelected(boolean bSelected) {
317: if (!bSelected) {
318: this .m_bSelected = false;
319: this .setBackground(Color.WHITE);
320: this .m_collectionLabel.setBackground(Color.WHITE);
321: this .m_typeCombo.setBackground(Color.WHITE);
322: this .m_minOccursSpinner.setBackground(Color.WHITE);
323: this .m_maxOccursSpinner.setBackground(Color.WHITE);
324: } else {
325: this .m_bSelected = true;
326: this .setBackground(this .m_selectedColor);
327: this .m_collectionLabel.setBackground(this .m_selectedColor);
328: this .m_typeCombo.setBackground(this .m_selectedColor);
329: this .m_minOccursSpinner.setBackground(this .m_selectedColor);
330: this .m_maxOccursSpinner.setBackground(this .m_selectedColor);
331: }
332: this .validateTree();
333: }
334:
335: /* (non-Javadoc)
336: * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
337: */
338: public void actionPerformed(ActionEvent ae) {
339: if (ae.getActionCommand().equals("TYPE")
340: && this .m_bListenToCombo) {
341: if (this .m_typeCombo.getSelectedItem().equals(
342: DomainSelectionCell.TYPE_COLLECTION)
343: || this .m_typeCombo.getSelectedItem().equals(
344: DomainSelectionCell.TYPE_BOTH)) {
345: this .m_minOccursSpinner.setValue(new Integer(0));
346:
347: if (this .m_valueCollection != null) {
348: this .m_valueCollection.setMinOccurs(0);
349: }
350: if (this .m_valueResource != null) {
351: this .m_valueResource.setMinOccurs(0);
352: }
353:
354: this .m_minOccursSpinner.setEnabled(false);
355: } else if (this .m_typeCombo.getSelectedItem().equals(
356: DomainSelectionCell.TYPE_RESOURCE)
357: && this .m_bOccursSpinnersEnabled) {
358: this .m_minOccursSpinner.setEnabled(true);
359: this .m_maxOccursSpinner.setEnabled(true);
360: }
361:
362: String sType = (String) this .m_typeCombo.getSelectedItem();
363: if (sType.equals(TYPE_RESOURCE)) {
364: if (this .m_valueResource == null) {
365: this .m_valueResource = this .m_list
366: .getNewValueInstance();
367: this .m_valueResource
368: .setResourceType(DomainValue.RESOURCE);
369: if (this .m_valueCollection != null) {
370: this .m_valueResource
371: .setMaxOccurs(this .m_valueCollection
372: .getMaxOccurs());
373: this .m_valueResource
374: .setMinOccurs(this .m_valueCollection
375: .getMinOccurs());
376: this .m_valueResource
377: .setPath(this .m_valueCollection
378: .getPath());
379: }
380: }
381: this .m_valueCollection = null;
382: } else if (sType.equals(TYPE_COLLECTION)) {
383: if (this .m_valueCollection == null) {
384: this .m_valueCollection = this .m_list
385: .getNewValueInstance();
386: this .m_valueCollection
387: .setResourceType(DomainValue.COLLECTION);
388: if (this .m_valueResource != null) {
389: this .m_valueCollection
390: .setMaxOccurs(this .m_valueResource
391: .getMaxOccurs());
392: this .m_valueCollection
393: .setMinOccurs(this .m_valueResource
394: .getMinOccurs());
395: this .m_valueCollection
396: .setPath(this .m_valueResource.getPath());
397: }
398: }
399: this .m_valueResource = null;
400: } else if (sType.equals(TYPE_BOTH)) {
401: if (this .m_valueResource == null) {
402: this .m_valueResource = this .m_list
403: .getNewValueInstance();
404: this .m_valueResource
405: .setResourceType(DomainValue.RESOURCE);
406: if (this .m_valueCollection != null) {
407: this .m_valueResource
408: .setMaxOccurs(this .m_valueCollection
409: .getMaxOccurs());
410: this .m_valueResource
411: .setMinOccurs(this .m_valueCollection
412: .getMinOccurs());
413: this .m_valueResource
414: .setPath(this .m_valueCollection
415: .getPath());
416: }
417: }
418: if (this .m_valueCollection == null) {
419: this .m_valueCollection = this .m_list
420: .getNewValueInstance();
421: this .m_valueCollection
422: .setResourceType(DomainValue.COLLECTION);
423: if (this .m_valueResource != null) {
424: this .m_valueCollection
425: .setMaxOccurs(this .m_valueResource
426: .getMaxOccurs());
427: this .m_valueCollection
428: .setMinOccurs(this .m_valueResource
429: .getMinOccurs());
430: this .m_valueCollection
431: .setPath(this .m_valueResource.getPath());
432: }
433: }
434: }
435: this .m_list.domainValuesChanged();
436: }
437: }
438:
439: protected List getValues() {
440: ArrayList aValues = new ArrayList();
441:
442: if (this .m_valueCollection != null) {
443: aValues.add(this .m_valueCollection);
444: }
445: if (this .m_valueResource != null) {
446: aValues.add(this .m_valueResource);
447: }
448:
449: return aValues;
450: }
451:
452: /* (non-Javadoc)
453: * @see java.awt.LayoutManager#layoutContainer(java.awt.Container)
454: */
455: public void layoutContainer(Container arg0) {
456: this .m_collectionLabel.setSize(100, 20);
457: this .m_collectionLabel.setLocation(0, 0);
458:
459: this .m_typeCombo.setSize(98, 18);
460: this .m_typeCombo.setLocation(101, 1);
461:
462: this .m_minOccursSpinner.setSize(48, 18);
463: this .m_minOccursSpinner.setLocation(201, 1);
464:
465: this .m_maxOccursSpinner.setSize(48, 18);
466: this .m_maxOccursSpinner.setLocation(251, 1);
467: }
468:
469: /* (non-Javadoc)
470: * @see java.awt.Component#getPreferredSize()
471: */
472: public Dimension getPreferredSize() {
473: return new Dimension(300, 20);
474: }
475:
476: /* (non-Javadoc)
477: * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
478: */
479: public void mouseEntered(MouseEvent arg0) {
480: }
481:
482: /* (non-Javadoc)
483: * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
484: */
485: public void mouseExited(MouseEvent arg0) {
486: }
487:
488: /* (non-Javadoc)
489: * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
490: */
491: public void mousePressed(MouseEvent arg0) {
492: }
493:
494: /* (non-Javadoc)
495: * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
496: */
497: public void mouseReleased(MouseEvent arg0) {
498: }
499:
500: /* (non-Javadoc)
501: * @see java.awt.LayoutManager#removeLayoutComponent(java.awt.Component)
502: */
503: public void removeLayoutComponent(Component arg0) {
504: }
505:
506: /* (non-Javadoc)
507: * @see java.awt.LayoutManager#addLayoutComponent(java.lang.String, java.awt.Component)
508: */
509: public void addLayoutComponent(String arg0, Component arg1) {
510: }
511:
512: /* (non-Javadoc)
513: * @see java.awt.LayoutManager#minimumLayoutSize(java.awt.Container)
514: */
515: public Dimension minimumLayoutSize(Container arg0) {
516: return this .getPreferredSize();
517: }
518:
519: /* (non-Javadoc)
520: * @see java.awt.LayoutManager#preferredLayoutSize(java.awt.Container)
521: */
522: public Dimension preferredLayoutSize(Container arg0) {
523: return this .getPreferredSize();
524: }
525:
526: /**
527: * @param arg0
528: */
529: private DomainSelectionCell(boolean arg0) {
530: super (arg0);
531: }
532:
533: /**
534: * @param arg0
535: */
536: private DomainSelectionCell(LayoutManager arg0) {
537: super (arg0);
538: }
539:
540: /**
541: * @param arg0
542: * @param arg1
543: */
544: private DomainSelectionCell(LayoutManager arg0, boolean arg1) {
545: super(arg0, arg1);
546: }
547:
548: }
|