KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > him > metadata > range > swing > domain > DomainSelectionCell


1 /*
2  * (C) Copyright SimulacraMedia 2003. All rights reserved.
3  *
4  * Created on 21-Jan-2004
5  *
6  */

7 package org.openharmonise.him.metadata.range.swing.domain;
8
9 import java.awt.Color JavaDoc;
10 import java.awt.Component JavaDoc;
11 import java.awt.Container JavaDoc;
12 import java.awt.Dimension JavaDoc;
13 import java.awt.LayoutManager JavaDoc;
14 import java.awt.event.ActionEvent JavaDoc;
15 import java.awt.event.ActionListener JavaDoc;
16 import java.awt.event.MouseEvent JavaDoc;
17 import java.awt.event.MouseListener JavaDoc;
18 import java.util.ArrayList JavaDoc;
19 import java.util.List JavaDoc;
20
21 import javax.swing.JComboBox JavaDoc;
22 import javax.swing.JLabel JavaDoc;
23 import javax.swing.JPanel JavaDoc;
24 import javax.swing.JSpinner JavaDoc;
25 import javax.swing.SpinnerModel JavaDoc;
26 import javax.swing.SpinnerNumberModel JavaDoc;
27 import javax.swing.event.ChangeEvent JavaDoc;
28 import javax.swing.event.ChangeListener JavaDoc;
29
30 import org.openharmonise.him.*;
31 import org.openharmonise.swing.FontManager;
32 import org.openharmonise.vfs.*;
33 import org.openharmonise.vfs.metadata.value.*;
34 import org.openharmonise.vfs.servers.ServerList;
35
36
37 /**
38  * FIXME - Matthew Large DIDN'T GIVE ME A DESCRIPTION!!
39  * @author Matthew Large
40  * @version $Revision: 1.2 $
41  *
42  */

43 public class DomainSelectionCell
44     extends JPanel JavaDoc
45     implements MouseListener JavaDoc, LayoutManager JavaDoc, ActionListener JavaDoc, ChangeListener JavaDoc {
46
47     private boolean m_bSelected = false;
48     
49     private Color JavaDoc m_selectedColor = new Color JavaDoc(173, 169, 143);
50     
51     private JLabel JavaDoc m_collectionLabel = null;
52     
53     private JComboBox JavaDoc m_typeCombo = null;
54     
55     private JSpinner JavaDoc m_minOccursSpinner = null;
56     private JSpinner JavaDoc m_maxOccursSpinner = null;
57
58     private DomainValue m_valueResource = null;
59     private DomainValue m_valueCollection = null;
60
61     public static String JavaDoc TYPE_COLLECTION = "Collections";
62     public static String JavaDoc TYPE_RESOURCE = "Resources";
63     public static String JavaDoc TYPE_BOTH = "Both";
64     
65     private DomainSelectionList m_list = null;
66     
67     private boolean m_bListenToCombo = false;
68     
69     private boolean m_bOccursSpinnersEnabled = true;
70
71     /**
72      *
73      */

74     public DomainSelectionCell(DomainSelectionList list, DomainValue value) throws Exception JavaDoc {
75         super();
76         this.m_list = list;
77         if(value.getResourceType()==DomainValue.RESOURCE) {
78             this.m_valueResource = value;
79         } else if(value.getResourceType()==DomainValue.COLLECTION) {
80             this.m_valueCollection = value;
81         }
82         this.setup();
83     }
84     
85     public void setOccursSpinnersEnabled(boolean bEnabled) {
86         m_bOccursSpinnersEnabled = bEnabled;
87         if(this.m_typeCombo.getSelectedItem().equals(DomainSelectionCell.TYPE_COLLECTION)
88                 || this.m_typeCombo.getSelectedItem().equals(DomainSelectionCell.TYPE_BOTH)
89                 || !bEnabled) {
90             this.m_minOccursSpinner.setEnabled(false);
91         } else if(this.m_typeCombo.getSelectedItem().equals(DomainSelectionCell.TYPE_RESOURCE)) {
92             this.m_minOccursSpinner.setEnabled(bEnabled);
93         }
94         this.m_maxOccursSpinner.setEnabled(bEnabled);
95     }
96     
97     protected String JavaDoc getPath() {
98         if(this.m_valueResource!=null) {
99             return this.m_valueResource.getPath();
100         } else if(this.m_valueCollection!=null) {
101             return this.m_valueCollection.getPath();
102         } else {
103             return null;
104         }
105     }
106     
107     protected void setListenToCombo(boolean bListenToCombo) {
108         this.m_bListenToCombo = bListenToCombo;
109     }
110     
111     protected boolean isSelected() {
112         if(this.getBackground()==this.m_selectedColor) {
113             return true;
114         } else {
115             return false;
116         }
117     }
118     
119     private void setup() throws Exception JavaDoc {
120         this.setLayout(this);
121         
122         this.setBackground(Color.WHITE);
123         this.setOpaque(true);
124         
125         AbstractVirtualFileSystem vfs = ServerList.getInstance().getHarmoniseServer().getVFS();
126         VirtualFile vfFile = null;
127         if(this.m_valueResource!=null) {
128             vfFile = vfs.getVirtualFile(this.m_valueResource.getPath()).getResource();
129         } else if(this.m_valueCollection!=null) {
130             vfFile = vfs.getVirtualFile(this.m_valueCollection.getPath()).getResource();
131         }
132         
133         int nMin = 0;
134         int nMax = -1;
135         
136         if(vfFile==null) {
137             throw new Exception JavaDoc("Domain resource is null..");
138         }
139         this.m_collectionLabel = new JLabel JavaDoc(vfs.getVirtualFileSystemView().getDisplayName(vfFile));
140         this.m_collectionLabel.setIcon( vfs.getVirtualFileSystemView().getIcon(vfFile) );
141         this.m_collectionLabel.addMouseListener(this);
142         this.m_collectionLabel.setOpaque(false);
143         this.m_collectionLabel.setFont( FontManager.getInstance().getFont(FontManager.FONT_RESOURCE_TITLE) );
144         this.add(m_collectionLabel);
145         
146         String JavaDoc[] sData = new String JavaDoc[]{DomainSelectionCell.TYPE_RESOURCE, DomainSelectionCell.TYPE_COLLECTION, DomainSelectionCell.TYPE_BOTH};
147         this.m_typeCombo = new JComboBox JavaDoc(sData);
148         this.m_typeCombo.setActionCommand("TYPE");
149         this.m_typeCombo.addActionListener(this);
150         this.m_bListenToCombo=false;
151         
152         if(this.m_valueCollection!=null && this.m_valueResource==null) {
153             nMin = this.m_valueCollection.getMinOccurs();
154             nMax = this.m_valueCollection.getMaxOccurs();
155             this.m_typeCombo.setSelectedItem(DomainSelectionCell.TYPE_COLLECTION);
156         } else if(this.m_valueCollection==null && this.m_valueResource!=null) {
157             nMin = this.m_valueResource.getMinOccurs();
158             nMax = this.m_valueResource.getMaxOccurs();
159             this.m_typeCombo.setSelectedItem(DomainSelectionCell.TYPE_RESOURCE);
160         } else if(this.m_valueCollection!=null && this.m_valueResource!=null) {
161             if(this.m_valueCollection.getMinOccurs()<this.m_valueResource.getMinOccurs()) {
162                 nMin = this.m_valueCollection.getMinOccurs();
163             } else {
164                 nMin = this.m_valueResource.getMinOccurs();
165             }
166             if(this.m_valueCollection.getMaxOccurs()<this.m_valueResource.getMaxOccurs()) {
167                 nMax = this.m_valueCollection.getMaxOccurs();
168             } else {
169                 nMax = this.m_valueResource.getMaxOccurs();
170             }
171             this.m_typeCombo.setSelectedItem(DomainSelectionCell.TYPE_BOTH);
172         }
173         this.m_bListenToCombo=true;
174         this.add(this.m_typeCombo);
175         
176         SpinnerModel JavaDoc model = new SpinnerNumberModel JavaDoc(nMin, 0, 1000, 1);
177         this.m_minOccursSpinner = new JSpinner JavaDoc(model);
178         this.m_minOccursSpinner.addChangeListener(this);
179         this.m_minOccursSpinner.setEditor(new JSpinner.NumberEditor JavaDoc(this.m_minOccursSpinner));
180         this.add(this.m_minOccursSpinner);
181         
182         model = new SpinnerNumberModel JavaDoc(nMax, -1, 1000, 1);
183         this.m_maxOccursSpinner = new JSpinner JavaDoc(model);
184         this.m_maxOccursSpinner.addChangeListener(this);
185         this.m_maxOccursSpinner.setEditor(new JSpinner.NumberEditor JavaDoc(this.m_maxOccursSpinner));
186         this.add(this.m_maxOccursSpinner);
187         if(nMax==-1) {
188             this.setMaxSpinnerCleared(true);
189         }
190         
191         if(this.m_typeCombo.getSelectedItem().equals(DomainSelectionCell.TYPE_COLLECTION)
192                 || this.m_typeCombo.getSelectedItem().equals(DomainSelectionCell.TYPE_BOTH) ) {
193             this.m_minOccursSpinner.setValue( new Integer JavaDoc(0));
194
195             if(this.m_valueCollection!=null) {
196                 this.m_valueCollection.setMinOccurs(0);
197             }
198             if(this.m_valueResource!=null) {
199                 this.m_valueResource.setMinOccurs(0);
200             }
201             
202             this.m_minOccursSpinner.setEnabled(false);
203         }
204         
205     }
206     
207     public void setType(String JavaDoc sType) {
208         this.m_typeCombo.setSelectedItem(sType);
209     }
210
211     /* (non-Javadoc)
212      * @see javax.swing.event.ChangeListener#stateChanged(javax.swing.event.ChangeEvent)
213      */

214     public void stateChanged(ChangeEvent JavaDoc ce) {
215         if(ce.getSource()==this.m_maxOccursSpinner) {
216             if(((Integer JavaDoc)this.m_maxOccursSpinner.getValue()).intValue()==-1) {
217                 this.setMaxSpinnerCleared(true);
218                 if(this.m_valueCollection!=null) {
219                     this.m_valueCollection.setMaxOccurs(-1);
220                 }
221                 if(this.m_valueResource!=null) {
222                     this.m_valueResource.setMaxOccurs(-1);
223                 }
224             } else {
225                 this.setMaxSpinnerCleared(false);
226                 int nMaxVal = ((Integer JavaDoc)((SpinnerNumberModel JavaDoc)this.m_maxOccursSpinner.getModel()).getValue()).intValue();
227                 int nMinVal = ((Integer JavaDoc)((SpinnerNumberModel JavaDoc)this.m_minOccursSpinner.getModel()).getValue()).intValue();
228                 if( nMaxVal<nMinVal ) {
229                     this.m_minOccursSpinner.setValue(new Integer JavaDoc(nMaxVal));
230                 }
231                 if(this.m_valueCollection!=null) {
232                     this.m_valueCollection.setMaxOccurs(nMaxVal);
233                 }
234                 if(this.m_valueResource!=null) {
235                     this.m_valueResource.setMaxOccurs(nMaxVal);
236                 }
237             }
238             this.m_list.domainValuesChanged();
239         } else if(ce.getSource()==this.m_minOccursSpinner) {
240             this.m_list.domainValuesChanged();
241             int nMaxVal = ((Integer JavaDoc)((SpinnerNumberModel JavaDoc)this.m_maxOccursSpinner.getModel()).getValue()).intValue();
242             int nMinVal = ((Integer JavaDoc)((SpinnerNumberModel JavaDoc)this.m_minOccursSpinner.getModel()).getValue()).intValue();
243             if( nMinVal>nMaxVal ) {
244                 this.m_maxOccursSpinner.setValue(new Integer JavaDoc(nMinVal));
245             }
246             if(this.m_valueCollection!=null) {
247                 this.m_valueCollection.setMinOccurs(nMinVal);
248             }
249             if(this.m_valueResource!=null) {
250                 this.m_valueResource.setMinOccurs(nMinVal);
251             }
252         }
253     }
254     
255     private void setMaxSpinnerCleared(boolean bCleared) {
256         if(bCleared) {
257             ((JSpinner.DefaultEditor JavaDoc)((JSpinner JavaDoc)this.m_maxOccursSpinner).getEditor()).getTextField().setForeground(Color.WHITE);
258             ((JSpinner.DefaultEditor JavaDoc)((JSpinner JavaDoc)this.m_maxOccursSpinner).getEditor()).getTextField().setSelectedTextColor(Color.WHITE);
259             ((JSpinner.DefaultEditor JavaDoc)((JSpinner JavaDoc)this.m_maxOccursSpinner).getEditor()).getTextField().setSelectionColor(Color.WHITE);
260         } else {
261             ((JSpinner.DefaultEditor JavaDoc)((JSpinner JavaDoc)this.m_maxOccursSpinner).getEditor()).getTextField().setForeground(Color.BLACK);
262             ((JSpinner.DefaultEditor JavaDoc)((JSpinner JavaDoc)this.m_maxOccursSpinner).getEditor()).getTextField().setSelectedTextColor(Color.WHITE);
263             ((JSpinner.DefaultEditor JavaDoc)((JSpinner JavaDoc)this.m_maxOccursSpinner).getEditor()).getTextField().setSelectionColor(Color.BLUE);
264         }
265     }
266
267     /* (non-Javadoc)
268      * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
269      */

270     public void mouseClicked(MouseEvent JavaDoc me) {
271         if(me.getSource()==this.m_collectionLabel) {
272             this.m_list.cellSelected(this);
273         }
274     }
275     
276     protected void setCellSelected(boolean bSelected) {
277         if(!bSelected) {
278             this.m_bSelected = false;
279             this.setBackground(Color.WHITE);
280             this.m_collectionLabel.setBackground(Color.WHITE);
281             this.m_typeCombo.setBackground(Color.WHITE);
282             this.m_minOccursSpinner.setBackground(Color.WHITE);
283             this.m_maxOccursSpinner.setBackground(Color.WHITE);
284         } else {
285             this.m_bSelected = true;
286             this.setBackground(this.m_selectedColor);
287             this.m_collectionLabel.setBackground(this.m_selectedColor);
288             this.m_typeCombo.setBackground(this.m_selectedColor);
289             this.m_minOccursSpinner.setBackground(this.m_selectedColor);
290             this.m_maxOccursSpinner.setBackground(this.m_selectedColor);
291         }
292         this.validateTree();
293     }
294
295     /* (non-Javadoc)
296      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
297      */

298     public void actionPerformed(ActionEvent JavaDoc ae) {
299         if(ae.getActionCommand().equals("TYPE") && this.m_bListenToCombo) {
300             if(this.m_typeCombo.getSelectedItem().equals(DomainSelectionCell.TYPE_COLLECTION)
301                     || this.m_typeCombo.getSelectedItem().equals(DomainSelectionCell.TYPE_BOTH)) {
302                 this.m_minOccursSpinner.setValue( new Integer JavaDoc(0));
303
304                 if(this.m_valueCollection!=null) {
305                     this.m_valueCollection.setMinOccurs(0);
306                 }
307                 if(this.m_valueResource!=null) {
308                     this.m_valueResource.setMinOccurs(0);
309                 }
310                 
311                 this.m_minOccursSpinner.setEnabled(false);
312             } else if(this.m_typeCombo.getSelectedItem().equals(DomainSelectionCell.TYPE_RESOURCE) && this.m_bOccursSpinnersEnabled) {
313                 this.m_minOccursSpinner.setEnabled(true);
314                 this.m_maxOccursSpinner.setEnabled(true);
315             }
316             
317             String JavaDoc sType = (String JavaDoc) this.m_typeCombo.getSelectedItem();
318             if(sType.equals(TYPE_RESOURCE)) {
319                 if(this.m_valueResource==null) {
320                     this.m_valueResource = this.m_list.getNewValueInstance();
321                     this.m_valueResource.setResourceType( DomainValue.RESOURCE );
322                     if(this.m_valueCollection!=null) {
323                         this.m_valueResource.setMaxOccurs(this.m_valueCollection.getMaxOccurs());
324                         this.m_valueResource.setMinOccurs(this.m_valueCollection.getMinOccurs());
325                         this.m_valueResource.setPath(this.m_valueCollection.getPath());
326                     }
327                 }
328                 this.m_valueCollection=null;
329             } else if(sType.equals(TYPE_COLLECTION)) {
330                 if(this.m_valueCollection==null) {
331                     this.m_valueCollection = this.m_list.getNewValueInstance();
332                     this.m_valueCollection.setResourceType( DomainValue.COLLECTION );
333                     if(this.m_valueResource!=null) {
334                         this.m_valueCollection.setMaxOccurs(this.m_valueResource.getMaxOccurs());
335                         this.m_valueCollection.setMinOccurs(this.m_valueResource.getMinOccurs());
336                         this.m_valueCollection.setPath(this.m_valueResource.getPath());
337                     }
338                 }
339                 this.m_valueResource=null;
340             } else if(sType.equals(TYPE_BOTH)) {
341                 if(this.m_valueResource==null) {
342                     this.m_valueResource = this.m_list.getNewValueInstance();
343                     this.m_valueResource.setResourceType( DomainValue.RESOURCE );
344                     if(this.m_valueCollection!=null) {
345                         this.m_valueResource.setMaxOccurs(this.m_valueCollection.getMaxOccurs());
346                         this.m_valueResource.setMinOccurs(this.m_valueCollection.getMinOccurs());
347                         this.m_valueResource.setPath(this.m_valueCollection.getPath());
348                     }
349                 }
350                 if(this.m_valueCollection==null) {
351                     this.m_valueCollection = this.m_list.getNewValueInstance();
352                     this.m_valueCollection.setResourceType( DomainValue.COLLECTION );
353                     if(this.m_valueResource!=null) {
354                         this.m_valueCollection.setMaxOccurs(this.m_valueResource.getMaxOccurs());
355                         this.m_valueCollection.setMinOccurs(this.m_valueResource.getMinOccurs());
356                         this.m_valueCollection.setPath(this.m_valueResource.getPath());
357                     }
358                 }
359             }
360             this.m_list.domainValuesChanged();
361         }
362     }
363     
364     protected List JavaDoc getValues() {
365         ArrayList JavaDoc aValues = new ArrayList JavaDoc();
366         
367         if(this.m_valueCollection!=null) {
368             aValues.add(this.m_valueCollection);
369         }
370         if(this.m_valueResource!=null) {
371             aValues.add(this.m_valueResource);
372         }
373         
374         return aValues;
375     }
376
377     /* (non-Javadoc)
378      * @see java.awt.LayoutManager#layoutContainer(java.awt.Container)
379      */

380     public void layoutContainer(Container JavaDoc arg0) {
381         this.m_collectionLabel.setSize(100, 20);
382         this.m_collectionLabel.setLocation(0, 0);
383         
384         this.m_typeCombo.setSize(98, 18);
385         this.m_typeCombo.setLocation(101, 1);
386         
387         this.m_minOccursSpinner.setSize(48, 18);
388         this.m_minOccursSpinner.setLocation(201, 1);
389         
390         this.m_maxOccursSpinner.setSize(48, 18);
391         this.m_maxOccursSpinner.setLocation(251, 1);
392     }
393
394     /* (non-Javadoc)
395      * @see java.awt.Component#getPreferredSize()
396      */

397     public Dimension JavaDoc getPreferredSize() {
398         return new Dimension JavaDoc(300, 20);
399     }
400
401     /* (non-Javadoc)
402      * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
403      */

404     public void mouseEntered(MouseEvent JavaDoc arg0) {
405     }
406
407     /* (non-Javadoc)
408      * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
409      */

410     public void mouseExited(MouseEvent JavaDoc arg0) {
411     }
412
413     /* (non-Javadoc)
414      * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
415      */

416     public void mousePressed(MouseEvent JavaDoc arg0) {
417     }
418
419     /* (non-Javadoc)
420      * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
421      */

422     public void mouseReleased(MouseEvent JavaDoc arg0) {
423     }
424
425     /* (non-Javadoc)
426      * @see java.awt.LayoutManager#removeLayoutComponent(java.awt.Component)
427      */

428     public void removeLayoutComponent(Component JavaDoc arg0) {
429     }
430
431     /* (non-Javadoc)
432      * @see java.awt.LayoutManager#addLayoutComponent(java.lang.String, java.awt.Component)
433      */

434     public void addLayoutComponent(String JavaDoc arg0, Component JavaDoc arg1) {
435     }
436
437     /* (non-Javadoc)
438      * @see java.awt.LayoutManager#minimumLayoutSize(java.awt.Container)
439      */

440     public Dimension JavaDoc minimumLayoutSize(Container JavaDoc arg0) {
441         return this.getPreferredSize();
442     }
443
444     /* (non-Javadoc)
445      * @see java.awt.LayoutManager#preferredLayoutSize(java.awt.Container)
446      */

447     public Dimension JavaDoc preferredLayoutSize(Container JavaDoc arg0) {
448         return this.getPreferredSize();
449     }
450
451     /**
452      * @param arg0
453      */

454     private DomainSelectionCell(boolean arg0) {
455         super(arg0);
456     }
457
458     /**
459      * @param arg0
460      */

461     private DomainSelectionCell(LayoutManager JavaDoc arg0) {
462         super(arg0);
463     }
464
465     /**
466      * @param arg0
467      * @param arg1
468      */

469     private DomainSelectionCell(LayoutManager JavaDoc arg0, boolean arg1) {
470         super(arg0, arg1);
471     }
472
473 }
474
Popular Tags