KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > him > metadata > range > swing > datetimehandling > DateTimeRangeDisplay


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.datetimehandling;
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  * @author Matthew Large
49  *
50  */

51 public class DateTimeRangeDisplay
52     extends AbstractRangeDisplay
53     implements RangeDisplay, LayoutManager JavaDoc, ActionListener JavaDoc {
54
55     private int m_nHeight = 10;
56
57     private int m_nMinOccurs = 1;
58     private int m_nMaxOccurs = 1000;
59
60     private JButton JavaDoc m_addButton = null;
61
62     private ArrayList JavaDoc m_valuePanels = new ArrayList JavaDoc();
63     private ArrayList JavaDoc m_removeButtons = new ArrayList JavaDoc();
64
65     private boolean m_bShowingDelButtons = true;
66
67     /**
68      * @param propInstance
69      */

70     public DateTimeRangeDisplay(PropertyInstance propInstance) {
71         super(propInstance);
72         this.setup();
73     }
74
75     private void setup() {
76         BoxLayout JavaDoc layout = new BoxLayout JavaDoc(this, BoxLayout.Y_AXIS);
77         this.setLayout(this);
78     }
79
80     /* (non-Javadoc)
81      * @see com.simulacramedia.contentmanager.metadata.range.swing.RangeDisplay#getPanel()
82      */

83     public JPanel JavaDoc getPanel() {
84         DateTimeRange range =
85             (DateTimeRange) this
86                 .getPropertyInstance()
87                 .getDefinition()
88                 .getRange();
89         List JavaDoc domains = this.getPropertyInstance().getDefinition().getDomains();
90         Iterator JavaDoc itor = domains.iterator();
91
92         while (itor.hasNext()) {
93             Domain domain = (Domain)itor.next();
94             boolean bApplicableDomain = false;
95             String JavaDoc sFilePath = ((VersionedVirtualFile)this.getPropertyInstance().getVirtualFile()).getLogicalPath();
96             Iterator JavaDoc itor2 = domain.getPaths().iterator();
97             while (itor2.hasNext()) {
98                 String JavaDoc sDomainPath = (String JavaDoc) itor2.next();
99                 if(sFilePath.startsWith(sDomainPath)) {
100                     bApplicableDomain=true;
101                 }
102             }
103     
104             if(bApplicableDomain) {
105                 if (this.m_nMinOccurs < domain.getMinOccurs()) {
106                     this.m_nMinOccurs = domain.getMinOccurs();
107                 }
108                 if (domain.getMaxOccurs()!=-1 && this.m_nMaxOccurs > domain.getMaxOccurs()) {
109                     this.m_nMaxOccurs = domain.getMaxOccurs();
110                 }
111             }
112
113         }
114
115         PropertyInstance propInst = this.getPropertyInstance();
116         List JavaDoc vals = propInst.getValues();
117         
118         int nCountMax=this.m_nMinOccurs;
119         if(vals.size()>nCountMax) {
120             nCountMax=vals.size();
121         }
122         
123         for(int i=0; i<nCountMax; i++) {
124
125             if (i < vals.size()) {
126                 DateTimeValuePanel valPanel =
127                     new DateTimeValuePanel(this,
128                         this.getPropertyInstance(),
129                         ((DateTimeValue) vals.get(i)).getValue());
130                 this.add(valPanel);
131                 this.m_valuePanels.add(valPanel);
132                 this.m_nHeight =
133                     this.m_nHeight + valPanel.getPreferredSize().height + 5;
134             } else {
135                 DateTimeValuePanel valPanel =
136                     new DateTimeValuePanel(this, this.getPropertyInstance(), "");
137                 this.add(valPanel);
138                 this.m_valuePanels.add(valPanel);
139                 this.m_nHeight =
140                     this.m_nHeight + valPanel.getPreferredSize().height + 5;
141             }
142             JButton JavaDoc removeButton = new JButton JavaDoc();
143             String JavaDoc fontName = "Dialog";
144             int fontSize = 13;
145             Font JavaDoc font = new Font JavaDoc(fontName, Font.BOLD, fontSize);
146             removeButton.setFont(font);
147             removeButton.setIcon(IconManager.getInstance().getIcon("16-command-value-minus.gif"));
148             removeButton.setActionCommand("REMOVE");
149             removeButton.addActionListener(this);
150             removeButton.setMargin( new Insets JavaDoc(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 JavaDoc();
158             String JavaDoc fontName = "Dialog";
159             int fontSize = 13;
160             Font JavaDoc font = new Font JavaDoc(fontName, Font.BOLD, fontSize);
161             this.m_addButton.setFont(font);
162             this.m_addButton.setIcon(IconManager.getInstance().getIcon("16-command-value-plus.gif"));
163             this.m_addButton.setActionCommand("ADD");
164             this.m_addButton.setMargin( new Insets JavaDoc(2,2,2,2) );
165             this.add(this.m_addButton);
166             this.m_addButton.addActionListener(this);
167             this.m_nHeight = this.m_nHeight + 25;
168         }
169
170         this.checkButtonVisibility();
171
172         return this;
173     }
174
175     /* (non-Javadoc)
176      * @see java.awt.Component#getPreferredSize()
177      */

178     public Dimension JavaDoc getPreferredSize() {
179         this.layoutContainer(null);
180         int nWidth = this.getParent().getWidth()-25;
181         
182         return new Dimension JavaDoc(nWidth, this.m_nHeight);
183     }
184
185     /* (non-Javadoc)
186      * @see java.awt.LayoutManager#removeLayoutComponent(java.awt.Component)
187      */

188     public void removeLayoutComponent(Component JavaDoc arg0) {
189     }
190
191     /* (non-Javadoc)
192      * @see java.awt.LayoutManager#layoutContainer(java.awt.Container)
193      */

194     public void layoutContainer(Container JavaDoc 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 JavaDoc itor = this.m_valuePanels.iterator();
208         int nCount = 0;
209         while(itor.hasNext()) {
210             
211             
212             DateTimeValuePanel valuePanel = (DateTimeValuePanel) itor.next();
213             valuePanel.setSize(valuePanel.getPreferredSize().width-50, valuePanel.getPreferredSize().height);
214             valuePanel.setLocation(10, nYPos);
215             
216             nYPos = nYPos + valuePanel.getSize().height;
217
218             nYPos = nYPos + 3;
219                         
220             nXPos = valuePanel.getLocation().x + valuePanel.getSize().width;
221             
222             JButton JavaDoc removeButton = (JButton JavaDoc)this.m_removeButtons.get(nCount);
223             if(removeButton.isVisible()) {
224                 removeButton.setSize(20, 20);
225                 removeButton.setLocation(nXPos, nYPos - 37);
226             }
227             
228             
229             
230             nCount++;
231         }
232         
233         if(this.m_addButton!=null && this.m_addButton.isVisible()) {
234             this.m_addButton.setSize(20, 20);
235             this.m_addButton.setLocation(nXPos+30, nYPos-37);
236         }
237         
238         this.m_nHeight = nYPos-17;
239         
240         this.repaint();
241     }
242
243     /* (non-Javadoc)
244      * @see java.awt.LayoutManager#addLayoutComponent(java.lang.String, java.awt.Component)
245      */

246     public void addLayoutComponent(String JavaDoc arg0, Component JavaDoc arg1) {
247     }
248
249     /* (non-Javadoc)
250      * @see java.awt.LayoutManager#minimumLayoutSize(java.awt.Container)
251      */

252     public Dimension JavaDoc minimumLayoutSize(Container JavaDoc arg0) {
253         return this.getPreferredSize();
254     }
255
256     /* (non-Javadoc)
257      * @see java.awt.LayoutManager#preferredLayoutSize(java.awt.Container)
258      */

259     public Dimension JavaDoc preferredLayoutSize(Container JavaDoc arg0) {
260         return this.getPreferredSize();
261     }
262
263     /* (non-Javadoc)
264      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
265      */

266     public void actionPerformed(ActionEvent JavaDoc ae) {
267         PropertyInstance propInst = this.getPropertyInstance();
268
269         if (ae.getActionCommand().equals("REMOVE")
270             && this.m_valuePanels.size() > this.m_nMinOccurs) {
271             int nIndex = this.m_removeButtons.indexOf(ae.getSource());
272             this.remove((Component JavaDoc) this.m_removeButtons.get(nIndex));
273             this.m_removeButtons.remove(nIndex);
274             this.m_nHeight =
275                 this.m_nHeight
276                     - ((Component JavaDoc) this.m_valuePanels.get(nIndex))
277                         .getSize()
278                         .height;
279             this.remove((Component JavaDoc) this.m_valuePanels.get(nIndex));
280             this.m_valuePanels.remove(nIndex);
281             this.valueChanged(null);
282         } else if (
283             ae.getActionCommand().equals("ADD")
284                 && this.m_valuePanels.size() < this.m_nMaxOccurs) {
285                     DateTimeValuePanel valPanel =
286                 new DateTimeValuePanel(this, this.getPropertyInstance(), "");
287             this.add(valPanel);
288             this.m_valuePanels.add(valPanel);
289             this.m_nHeight =
290                 this.m_nHeight + valPanel.getPreferredSize().height;
291                 JButton JavaDoc removeButton = new JButton JavaDoc();
292                 String JavaDoc fontName = "Dialog";
293                 int fontSize = 13;
294                 Font JavaDoc font = new Font JavaDoc(fontName, Font.BOLD, fontSize);
295                 removeButton.setFont(font);
296                 removeButton.setIcon(IconManager.getInstance().getIcon("16-command-value-minus.gif"));
297                 removeButton.setActionCommand("REMOVE");
298                 removeButton.addActionListener(this);
299                 removeButton.setMargin( new Insets JavaDoc(2,2,2,2) );
300             this.add(removeButton);
301             this.m_removeButtons.add(removeButton);
302         }
303
304         this.checkButtonVisibility();
305
306         Component JavaDoc comp = this.getParent();
307         comp.validate();
308         while (!(comp instanceof MetadataTabs)) {
309             if (comp != null) {
310                 comp = comp.getParent();
311             }
312         }
313         if (comp != null) {
314             comp.validate();
315         }
316     }
317
318     private void checkButtonVisibility() {
319         if(this.m_nMinOccurs==0 && this.m_nMaxOccurs==1) {
320             Iterator JavaDoc itor = this.m_removeButtons.iterator();
321             while (itor.hasNext()) {
322                 JButton JavaDoc button = (JButton JavaDoc) itor.next();
323                 button.setVisible(false);
324             }
325             this.m_addButton.setVisible(false);
326             this.m_bShowingDelButtons = false;
327             return;
328         }
329         if (this.m_bShowingDelButtons
330             && this.m_removeButtons.size() <= this.m_nMinOccurs) {
331             Iterator JavaDoc itor = this.m_removeButtons.iterator();
332             while (itor.hasNext()) {
333                 JButton JavaDoc button = (JButton JavaDoc) itor.next();
334                 button.setVisible(false);
335             }
336             this.m_bShowingDelButtons = false;
337         } else if (
338             !this.m_bShowingDelButtons
339                 && this.m_removeButtons.size() > this.m_nMinOccurs) {
340             Iterator JavaDoc itor = this.m_removeButtons.iterator();
341             while (itor.hasNext()) {
342                 JButton JavaDoc button = (JButton JavaDoc) itor.next();
343                 button.setVisible(true);
344             }
345             this.m_bShowingDelButtons = true;
346         }
347         if (this.m_addButton != null
348             && this.m_addButton.isVisible()
349             && this.m_valuePanels.size() >= this.m_nMaxOccurs) {
350             this.m_addButton.setVisible(false);
351             this.m_nHeight = this.m_nHeight - this.m_addButton.getSize().height;
352         } else if (
353             this.m_addButton != null
354                 && this.m_valuePanels.size() < this.m_nMaxOccurs) {
355             this.m_addButton.setVisible(true);
356             this.m_nHeight = this.m_nHeight + this.m_addButton.getSize().height;
357         }
358
359     }
360     
361     protected void valueChanged(String JavaDoc sValue) {
362         ArrayList JavaDoc aValues = new ArrayList JavaDoc();
363         
364         Iterator JavaDoc itor = this.m_valuePanels.iterator();
365         while(itor.hasNext()) {
366             DateTimeValue val = (DateTimeValue)this.getPropertyInstance().getNewValueInstance();
367             val.setValue(((DateTimeValuePanel)itor.next()).getValue());
368             aValues.add( val );
369         }
370         if(!this.getPropertyInstance().getValues().containsAll(aValues)
371                 || !aValues.containsAll(this.getPropertyInstance().getValues())) {
372             this.getPropertyInstance().setValues(aValues);
373         }
374     }
375
376     /* (non-Javadoc)
377      * @see com.simulacramedia.contentmanager.metadata.range.swing.AbstractRangeDisplay#isValid()
378      */

379     public boolean isMetadataValid() {
380         boolean bValid = true;
381         for (int i = 0; i < this.getComponentCount(); i++) {
382             Component JavaDoc comp = this.getComponent(i);
383             if(comp instanceof AbstractRangeDisplay) {
384                 if(!((AbstractRangeDisplay)comp).isMetadataValid()) {
385                     bValid=false;
386                 }
387             }
388         }
389         return bValid;
390     }
391     
392     
393 }
Popular Tags