KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > him > metadata > range > swing > integerhandling > IntegerRangeDisplay


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

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

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

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

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

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

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

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

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