KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > him > metadata > range > swing > datehandling > DateRangeDisplay


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.him.metadata.range.swing.datehandling;
20
21 import java.awt.Component JavaDoc;
22 import java.awt.Container JavaDoc;
23 import java.awt.Dimension JavaDoc;
24 import java.awt.Font JavaDoc;
25 import java.awt.Insets JavaDoc;
26 import java.awt.LayoutManager JavaDoc;
27 import java.awt.event.ActionEvent JavaDoc;
28 import java.awt.event.ActionListener JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32
33 import javax.swing.BoxLayout JavaDoc;
34 import javax.swing.JButton JavaDoc;
35 import javax.swing.JPanel JavaDoc;
36
37 import org.openharmonise.him.metadata.range.swing.*;
38 import org.openharmonise.him.metadata.range.swing.propertyhandling.*;
39 import org.openharmonise.him.metadata.swing.*;
40 import org.openharmonise.vfs.*;
41 import org.openharmonise.vfs.gui.*;
42 import org.openharmonise.vfs.metadata.*;
43 import org.openharmonise.vfs.metadata.range.*;
44 import org.openharmonise.vfs.metadata.value.*;
45
46
47 /**
48  * Component to display date properties in the metadata window.
49  *
50  * @author Matthew Large
51  * @version $Revision: 1.1 $
52  *
53  */

54 public class DateRangeDisplay
55     extends AbstractRangeDisplay
56     implements RangeDisplay, LayoutManager JavaDoc, ActionListener JavaDoc {
57
58     /**
59      * Height of the component.
60      */

61     private int m_nHeight = 10;
62
63     /**
64      * Minimum allowed occurances of values.
65      */

66     private int m_nMinOccurs = 0;
67     
68     /**
69      * Maximum allowed occurances of values.
70      */

71     private int m_nMaxOccurs = 1000;
72
73     /**
74      * Button for adding new values.
75      */

76     private JButton JavaDoc m_addButton = null;
77
78     /**
79      * List of {@link DateValuePanel} components.
80      */

81     private ArrayList JavaDoc m_valuePanels = new ArrayList JavaDoc();
82     
83     /**
84      * List of buttons to remove values.
85      */

86     private ArrayList JavaDoc m_removeButtons = new ArrayList JavaDoc();
87
88     /**
89      * True if removal buttons are showing.
90      */

91     private boolean m_bShowingDelButtons = true;
92
93     /**
94      * Constructs a new date range display component.
95      *
96      * @param propInstance Property instance that this component displays.
97      */

98     public DateRangeDisplay(PropertyInstance propInstance) {
99         super(propInstance);
100         this.setup();
101     }
102
103     /**
104      * Initialises this component.
105      *
106      */

107     private void setup() {
108         BoxLayout JavaDoc layout = new BoxLayout JavaDoc(this, BoxLayout.Y_AXIS);
109         this.setLayout(this);
110     }
111
112     /* (non-Javadoc)
113      * @see com.simulacramedia.contentmanager.metadata.range.swing.RangeDisplay#getPanel()
114      */

115     public JPanel JavaDoc getPanel() {
116         DateRange range =
117             (DateRange) this
118                 .getPropertyInstance()
119                 .getDefinition()
120                 .getRange();
121         List JavaDoc domains = this.getPropertyInstance().getDefinition().getDomains();
122         Iterator JavaDoc itor = domains.iterator();
123
124         while (itor.hasNext()) {
125             Domain domain = (Domain)itor.next();
126             boolean bApplicableDomain = false;
127             String JavaDoc sFilePath = ((VersionedVirtualFile)this.getPropertyInstance().getVirtualFile()).getLogicalPath();
128             Iterator JavaDoc itor2 = domain.getPaths().iterator();
129             while (itor2.hasNext()) {
130                 String JavaDoc sDomainPath = (String JavaDoc) itor2.next();
131                 if(sFilePath.startsWith(sDomainPath)) {
132                     bApplicableDomain=true;
133                 }
134             }
135     
136             if(bApplicableDomain) {
137                 if(this.m_nMinOccurs < domain.getMinOccurs()) {
138                     this.m_nMinOccurs = domain.getMinOccurs();
139                 }
140                 if (domain.getMaxOccurs()!=-1 && this.m_nMaxOccurs > domain.getMaxOccurs()) {
141                     this.m_nMaxOccurs = domain.getMaxOccurs();
142                 }
143             }
144
145         }
146
147         PropertyInstance propInst = this.getPropertyInstance();
148         List JavaDoc vals = propInst.getValues();
149         
150         int nCountMax=this.m_nMinOccurs;
151         if(nCountMax==0) {
152             nCountMax=1;
153         }
154         
155         if(vals.size()>nCountMax) {
156             nCountMax=vals.size();
157         }
158         
159         for(int i=0; i<nCountMax; i++) {
160             boolean bNoValAllowed = true;
161             
162             if(i<m_nMinOccurs) {
163                 bNoValAllowed = false;
164             }
165
166             if (i < vals.size()) {
167                 
168                 DateValuePanel valPanel =
169                     new DateValuePanel(this,
170                         this.getPropertyInstance(),
171                         ((DateValue) vals.get(i)).getValue(),bNoValAllowed);
172                 this.add(valPanel);
173                 this.m_valuePanels.add(valPanel);
174                 this.m_nHeight =
175                     this.m_nHeight + valPanel.getPreferredSize().height + 5;
176             
177                 
178             } else {
179                 DateValuePanel valPanel =
180                     new DateValuePanel(this, this.getPropertyInstance(), "",bNoValAllowed);
181                 this.add(valPanel);
182                 this.m_valuePanels.add(valPanel);
183                 this.m_nHeight =
184                     this.m_nHeight + valPanel.getPreferredSize().height + 5;
185             }
186             JButton JavaDoc removeButton = new JButton JavaDoc();
187             String JavaDoc fontName = "Dialog";
188             int fontSize = 13;
189             Font JavaDoc font = new Font JavaDoc(fontName, Font.BOLD, fontSize);
190             removeButton.setFont(font);
191             removeButton.setIcon(IconManager.getInstance().getIcon("16-command-value-minus.gif"));
192             removeButton.setActionCommand("REMOVE");
193             removeButton.addActionListener(this);
194             removeButton.setMargin( new Insets JavaDoc(2,2,2,2) );
195             this.add(removeButton);
196             this.m_removeButtons.add(removeButton);
197
198         }
199
200         if(this.m_valuePanels.size()<this.m_nMaxOccurs) {
201             this.m_addButton = new JButton JavaDoc();
202             String JavaDoc fontName = "Dialog";
203             int fontSize = 13;
204             Font JavaDoc font = new Font JavaDoc(fontName, Font.BOLD, fontSize);
205             this.m_addButton.setFont(font);
206             this.m_addButton.setIcon(IconManager.getInstance().getIcon("16-command-value-plus.gif"));
207             this.m_addButton.setActionCommand("ADD");
208             this.m_addButton.setMargin( new Insets JavaDoc(2,2,2,2) );
209             this.add(this.m_addButton);
210             this.m_addButton.addActionListener(this);
211             this.m_nHeight = this.m_nHeight + 25;
212         }
213
214         this.checkButtonVisibility();
215
216         return this;
217     }
218
219     /* (non-Javadoc)
220      * @see java.awt.Component#getPreferredSize()
221      */

222     public Dimension JavaDoc getPreferredSize() {
223         this.layoutContainer(null);
224         int nWidth = this.getParent().getWidth()-25;
225
226         return new Dimension JavaDoc(nWidth, this.m_nHeight);
227     }
228
229     /* (non-Javadoc)
230      * @see java.awt.LayoutManager#removeLayoutComponent(java.awt.Component)
231      */

232     public void removeLayoutComponent(Component JavaDoc arg0) {
233     }
234
235     /* (non-Javadoc)
236      * @see java.awt.LayoutManager#layoutContainer(java.awt.Container)
237      */

238     public void layoutContainer(Container JavaDoc arg0) {
239         if(this.getParent() instanceof PropertyValuePanel) {
240             this.m_nMinOccurs=0;
241             this.m_nMaxOccurs=1;
242         }
243         
244         this.checkButtonVisibility();
245         int nWidth = this.getParent().getWidth()-50;
246         
247         int nYPos = 0;
248         
249         int nXPos = 0;
250         
251         Iterator JavaDoc itor = this.m_valuePanels.iterator();
252         int nCount = 0;
253         while(itor.hasNext()) {
254             DateValuePanel valuePanel = (DateValuePanel) itor.next();
255             valuePanel.setSize(valuePanel.getPreferredSize().width-50, valuePanel.getPreferredSize().height);
256             valuePanel.setLocation(10, nYPos);
257             nXPos = valuePanel.getLocation().x + valuePanel.getSize().width;
258             
259             JButton JavaDoc removeButton = (JButton JavaDoc)this.m_removeButtons.get(nCount);
260             if(removeButton.isVisible()) {
261                 removeButton.setSize(20, 20);
262                 removeButton.setLocation(nXPos, nYPos);
263             }
264             
265             nYPos = nYPos + valuePanel.getSize().height;
266
267             nYPos = nYPos + 3;
268             
269             nCount++;
270         }
271         
272         if(this.m_addButton!=null && this.m_addButton.isVisible()) {
273             this.m_addButton.setSize(20, 20);
274             this.m_addButton.setLocation(nXPos+30, nYPos-37);
275         }
276         
277         this.m_nHeight = nYPos-17;
278         
279         this.repaint();
280     }
281
282     /* (non-Javadoc)
283      * @see java.awt.LayoutManager#addLayoutComponent(java.lang.String, java.awt.Component)
284      */

285     public void addLayoutComponent(String JavaDoc arg0, Component JavaDoc arg1) {
286     }
287
288     /* (non-Javadoc)
289      * @see java.awt.LayoutManager#minimumLayoutSize(java.awt.Container)
290      */

291     public Dimension JavaDoc minimumLayoutSize(Container JavaDoc arg0) {
292         return this.getPreferredSize();
293     }
294
295     /* (non-Javadoc)
296      * @see java.awt.LayoutManager#preferredLayoutSize(java.awt.Container)
297      */

298     public Dimension JavaDoc preferredLayoutSize(Container JavaDoc arg0) {
299         return this.getPreferredSize();
300     }
301
302     /* (non-Javadoc)
303      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
304      */

305     public void actionPerformed(ActionEvent JavaDoc ae) {
306         PropertyInstance propInst = this.getPropertyInstance();
307
308         if (ae.getActionCommand().equals("REMOVE")
309             && this.m_valuePanels.size() > this.m_nMinOccurs) {
310             int nIndex = this.m_removeButtons.indexOf(ae.getSource());
311             this.remove((Component JavaDoc) this.m_removeButtons.get(nIndex));
312             this.m_removeButtons.remove(nIndex);
313             this.m_nHeight =
314                 this.m_nHeight
315                     - ((Component JavaDoc) this.m_valuePanels.get(nIndex))
316                         .getSize()
317                         .height;
318             this.remove((Component JavaDoc) this.m_valuePanels.get(nIndex));
319             this.m_valuePanels.remove(nIndex);
320             this.valueChanged(null);
321         } else if (
322             ae.getActionCommand().equals("ADD")
323                 && this.m_valuePanels.size() < this.m_nMaxOccurs) {
324                     DateValuePanel valPanel =
325                 new DateValuePanel(this, this.getPropertyInstance(), "",true);
326             this.add(valPanel);
327             this.m_valuePanels.add(valPanel);
328             this.m_nHeight =
329                 this.m_nHeight + valPanel.getPreferredSize().height;
330                 JButton JavaDoc removeButton = new JButton JavaDoc();
331                 String JavaDoc fontName = "Dialog";
332                 int fontSize = 13;
333                 Font JavaDoc font = new Font JavaDoc(fontName, Font.BOLD, fontSize);
334                 removeButton.setFont(font);
335                 removeButton.setIcon(IconManager.getInstance().getIcon("16-command-value-minus.gif"));
336                 removeButton.setActionCommand("REMOVE");
337                 removeButton.addActionListener(this);
338                 removeButton.setMargin( new Insets JavaDoc(2,2,2,2) );
339                 this.add(removeButton);
340                 this.m_removeButtons.add(removeButton);
341         }
342
343         this.checkButtonVisibility();
344
345         Component JavaDoc comp = this.getParent();
346         comp.validate();
347         while (!(comp instanceof MetadataTabs)) {
348             if (comp != null) {
349                 comp = comp.getParent();
350             }
351         }
352         if (comp != null) {
353             comp.validate();
354         }
355     }
356
357     /**
358      * Checks and sets the visibility of the addition and
359      * removal buttons.
360      *
361      */

362     private void checkButtonVisibility() {
363         if(this.m_nMinOccurs==0 && this.m_nMaxOccurs==1) {
364             Iterator JavaDoc itor = this.m_removeButtons.iterator();
365             while (itor.hasNext()) {
366                 JButton JavaDoc button = (JButton JavaDoc) itor.next();
367                 button.setVisible(false);
368             }
369             if(this.m_addButton!=null) {
370                 this.m_addButton.setVisible(false);
371             }
372             this.m_bShowingDelButtons = false;
373             return;
374         }
375         if (this.m_bShowingDelButtons
376             && this.m_removeButtons.size() <= this.m_nMinOccurs) {
377             Iterator JavaDoc itor = this.m_removeButtons.iterator();
378             while (itor.hasNext()) {
379                 JButton JavaDoc button = (JButton JavaDoc) itor.next();
380                 button.setVisible(false);
381             }
382             this.m_bShowingDelButtons = false;
383         } else if (
384             !this.m_bShowingDelButtons
385                 && this.m_removeButtons.size() > this.m_nMinOccurs) {
386             Iterator JavaDoc itor = this.m_removeButtons.iterator();
387             while (itor.hasNext()) {
388                 JButton JavaDoc button = (JButton JavaDoc) itor.next();
389                 button.setVisible(true);
390             }
391             this.m_bShowingDelButtons = true;
392         }
393         if (this.m_addButton != null
394             && this.m_addButton.isVisible()
395             && this.m_valuePanels.size() >= this.m_nMaxOccurs) {
396             this.m_addButton.setVisible(false);
397             this.m_nHeight = this.m_nHeight - this.m_addButton.getSize().height;
398         } else if (
399             this.m_addButton != null
400                 && this.m_valuePanels.size() < this.m_nMaxOccurs) {
401             this.m_addButton.setVisible(true);
402             this.m_nHeight = this.m_nHeight + this.m_addButton.getSize().height;
403         }
404
405     }
406     
407     /**
408      * Method called if a value has been altered.
409      *
410      * @param sValue Value that has been altered
411      */

412     protected void valueChanged(String JavaDoc sValue) {
413         ArrayList JavaDoc aValues = new ArrayList JavaDoc();
414         
415         Iterator JavaDoc itor = this.m_valuePanels.iterator();
416         while(itor.hasNext()) {
417             DateValue val = (DateValue)this.getPropertyInstance().getNewValueInstance();
418             val.setValue(((DateValuePanel)itor.next()).getValue());
419             aValues.add( val );
420         }
421         if(!this.getPropertyInstance().getValues().containsAll(aValues)
422                 || !aValues.containsAll(this.getPropertyInstance().getValues())) {
423             this.getPropertyInstance().setValues(aValues);
424         }
425     }
426
427     /* (non-Javadoc)
428      * @see com.simulacramedia.contentmanager.metadata.range.swing.AbstractRangeDisplay#isValid()
429      */

430     public boolean isMetadataValid() {
431         boolean bValid = true;
432         for (int i = 0; i < this.getComponentCount(); i++) {
433             Component JavaDoc comp = this.getComponent(i);
434             if(comp instanceof AbstractRangeDisplay) {
435                 if(!((AbstractRangeDisplay)comp).isMetadataValid()) {
436                     bValid=false;
437                 }
438             }
439         }
440         
441         if(bValid == true && m_nMinOccurs > 0) {
442             //check that it is really valid with respect to
443
//actually having values
444

445             List JavaDoc vals = getPropertyInstance().getValues();
446         
447             if(vals.size() < m_nMinOccurs) {
448                 bValid = false;
449             }
450         
451             for(int i=0;i<m_nMinOccurs && bValid == true;i++) {
452                 DateValue val = (DateValue) vals.get(i);
453                 String JavaDoc sVal = val.getValue();
454                 if(sVal == null || sVal.trim().length() == 0) {
455                     bValid = false;
456                 }
457             }
458         }
459         
460         
461         return bValid;
462     }
463 }
Popular Tags