KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > him > metadata > range > swing > floathandling > FloatRangeDisplay


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.floathandling;
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 FloatRangeDisplay
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 FloatRangeDisplay(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         FloatRange range =
85             (FloatRange) 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                 FloatValuePanel valPanel =
127                     new FloatValuePanel(this,
128                         this.getPropertyInstance(),
129                         Float.toString( ((FloatValue) 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                 FloatValuePanel valPanel =
136                     new FloatValuePanel(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         return new Dimension JavaDoc(nWidth, this.m_nHeight);
182     }
183
184     /* (non-Javadoc)
185      * @see java.awt.LayoutManager#removeLayoutComponent(java.awt.Component)
186      */

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

193     public void layoutContainer(Container JavaDoc arg0) {
194         if(this.getParent() instanceof PropertyValuePanel) {
195             this.m_nMinOccurs=0;
196             this.m_nMaxOccurs=1;
197         }
198         
199         this.checkButtonVisibility();
200         int nWidth = this.getParent().getWidth()-50;
201         
202         int nYPos = 0;
203         
204         int nXPos = 0;
205         
206         Iterator JavaDoc itor = this.m_valuePanels.iterator();
207         int nCount = 0;
208         while(itor.hasNext()) {
209             FloatValuePanel valuePanel = (FloatValuePanel) itor.next();
210             valuePanel.setSize(valuePanel.getPreferredSize().width-50, valuePanel.getPreferredSize().height);
211             valuePanel.setLocation(0, nYPos);
212             nXPos = valuePanel.getLocation().x + valuePanel.getSize().width;
213             
214             JButton JavaDoc removeButton = (JButton JavaDoc)this.m_removeButtons.get(nCount);
215             if(removeButton.isVisible()) {
216                 removeButton.setSize(20, 20);
217                 removeButton.setLocation(nXPos, nYPos);
218             }
219             
220             nYPos = nYPos + valuePanel.getSize().height;
221             
222             nCount++;
223         }
224         
225         if(this.m_addButton!=null && this.m_addButton.isVisible()) {
226             this.m_addButton.setSize(20, 20);
227             this.m_addButton.setLocation(nXPos+30, nYPos-30);
228         }
229         
230         this.m_nHeight = nYPos-10;
231         
232         this.repaint();
233     }
234
235     /* (non-Javadoc)
236      * @see java.awt.LayoutManager#addLayoutComponent(java.lang.String, java.awt.Component)
237      */

238     public void addLayoutComponent(String JavaDoc arg0, Component JavaDoc arg1) {
239     }
240
241     /* (non-Javadoc)
242      * @see java.awt.LayoutManager#minimumLayoutSize(java.awt.Container)
243      */

244     public Dimension JavaDoc minimumLayoutSize(Container JavaDoc arg0) {
245         return this.getPreferredSize();
246     }
247
248     /* (non-Javadoc)
249      * @see java.awt.LayoutManager#preferredLayoutSize(java.awt.Container)
250      */

251     public Dimension JavaDoc preferredLayoutSize(Container JavaDoc arg0) {
252         return this.getPreferredSize();
253     }
254
255     /* (non-Javadoc)
256      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
257      */

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

374     public boolean isMetadataValid() {
375         boolean bValid = true;
376         for (int i = 0; i < this.getComponentCount(); i++) {
377             Component JavaDoc comp = this.getComponent(i);
378             if(comp instanceof AbstractRangeDisplay) {
379                 if(!((AbstractRangeDisplay)comp).isMetadataValid()) {
380                     bValid=false;
381                 }
382             }
383         }
384         return bValid;
385     }
386 }
Popular Tags