KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > him > metadata > range > swing > booleanhandling > BooleanRangeDisplay


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.booleanhandling;
20
21 import java.awt.*;
22 import java.awt.event.*;
23 import java.util.*;
24 import java.util.List JavaDoc;
25
26 import javax.swing.*;
27
28 import org.openharmonise.him.metadata.range.swing.*;
29 import org.openharmonise.him.metadata.swing.*;
30 import org.openharmonise.vfs.gui.*;
31 import org.openharmonise.vfs.metadata.*;
32 import org.openharmonise.vfs.metadata.range.*;
33 import org.openharmonise.vfs.metadata.value.*;
34
35
36 /**
37  * Component to display boolean properties in the metadata window.
38  *
39  * @author Matthew Large
40  * @version $Revision: 1.1 $
41  *
42  */

43 public class BooleanRangeDisplay
44     extends AbstractRangeDisplay
45     implements RangeDisplay, LayoutManager, ActionListener {
46
47     /**
48      * Height of the component.
49      */

50     private int m_nHeight = 10;
51
52     /**
53      * Minimum value occurances.
54      */

55     private int m_nMinOccurs = 1;
56     
57     /**
58      * Maximum value occurances.
59      */

60     private int m_nMaxOccurs = 1;
61
62     /**
63      * Button for adding new values.
64      */

65     private JButton m_addButton = null;
66
67     /**
68      * List of {@link BooleanValuePanel} objects.
69      */

70     private ArrayList m_valuePanels = new ArrayList();
71     
72     /**
73      * List of buttons for removing values.
74      */

75     private ArrayList m_removeButtons = new ArrayList();
76
77     /**
78      * true if removal buttons shoul dbe showing
79      */

80     private boolean m_bShowingDelButtons = true;
81
82     /**
83      * Constructs a new boolean range display component.
84      *
85      * @param propInstance Property instance to be displayed
86      */

87     public BooleanRangeDisplay(PropertyInstance propInstance) {
88         super(propInstance);
89         this.setup();
90     }
91
92     /**
93      * Initialises this component.
94      *
95      */

96     private void setup() {
97         this.setLayout(this);
98     }
99
100     /* (non-Javadoc)
101      * @see com.simulacramedia.contentmanager.metadata.range.swing.RangeDisplay#getPanel()
102      */

103     public JPanel getPanel() {
104         BooleanRange range =
105             (BooleanRange) this
106                 .getPropertyInstance()
107                 .getDefinition()
108                 .getRange();
109         List JavaDoc domains = this.getPropertyInstance().getDefinition().getDomains();
110         Iterator itor = domains.iterator();
111
112         while (itor.hasNext()) {
113             Domain domain = (Domain) itor.next();
114             if (this.m_nMinOccurs < domain.getMinOccurs()) {
115                 this.m_nMinOccurs = domain.getMinOccurs();
116             }
117
118         }
119
120         PropertyInstance propInst = this.getPropertyInstance();
121         List JavaDoc vals = propInst.getValues();
122         
123         int nCountMax=this.m_nMinOccurs;
124         if(vals.size()>nCountMax) {
125             nCountMax=vals.size();
126         }
127         
128         for(int i=0; i<nCountMax; i++) {
129
130             if (i < vals.size()) {
131                 BooleanValuePanel valPanel =
132                     new BooleanValuePanel(this,
133                         this.getPropertyInstance(),
134                          Boolean.toString( ((BooleanValue) vals.get(i)).getValue() ) );
135                 this.add(valPanel);
136                 this.m_valuePanels.add(valPanel);
137                 this.m_nHeight =
138                     this.m_nHeight + valPanel.getPreferredSize().height + 5;
139             } else {
140                 BooleanValuePanel valPanel =
141                     new BooleanValuePanel(this,this.getPropertyInstance(), "");
142                 this.add(valPanel);
143                 this.m_valuePanels.add(valPanel);
144                 this.m_nHeight =
145                     this.m_nHeight + valPanel.getPreferredSize().height + 5;
146             }
147             JButton removeButton = new JButton();
148             String JavaDoc fontName = "Dialog";
149             int fontSize = 13;
150             Font font = new Font(fontName, Font.BOLD, fontSize);
151             removeButton.setFont(font);
152             removeButton.setIcon(IconManager.getInstance().getIcon("16-command-value-minus.gif"));
153             removeButton.setActionCommand("REMOVE");
154             removeButton.addActionListener(this);
155             removeButton.setMargin( new Insets(2,2,2,2) );
156             this.add(removeButton);
157             this.m_removeButtons.add(removeButton);
158
159         }
160
161         if(this.m_valuePanels.size()<this.m_nMaxOccurs) {
162             this.m_addButton = new JButton();
163             String JavaDoc fontName = "Dialog";
164             int fontSize = 13;
165             Font font = new Font(fontName, Font.BOLD, fontSize);
166             this.m_addButton.setFont(font);
167             this.m_addButton.setIcon(IconManager.getInstance().getIcon("16-command-value-plus.gif"));
168             this.m_addButton.setActionCommand("ADD");
169             this.m_addButton.setMargin( new Insets(2,2,2,2) );
170
171             this.add(this.m_addButton);
172             this.m_addButton.addActionListener(this);
173             this.m_nHeight = this.m_nHeight + 25;
174         }
175
176         this.checkButtonVisibility();
177
178         return this;
179     }
180
181     /* (non-Javadoc)
182      * @see java.awt.Component#getPreferredSize()
183      */

184     public Dimension getPreferredSize() {
185         this.layoutContainer(null);
186         int nWidth = this.getParent().getWidth()-25;
187
188         return new Dimension(nWidth, this.m_nHeight);
189
190     }
191
192     /* (non-Javadoc)
193      * @see java.awt.LayoutManager#removeLayoutComponent(java.awt.Component)
194      */

195     public void removeLayoutComponent(Component arg0) {
196         // NO-OP
197
}
198
199     /* (non-Javadoc)
200      * @see java.awt.LayoutManager#layoutContainer(java.awt.Container)
201      */

202     public void layoutContainer(Container arg0) {
203         int nWidth = this.getParent().getWidth()-50;
204         
205         int nYPos = 0;
206         
207         int nXPos = 0;
208         
209         Iterator itor = this.m_valuePanels.iterator();
210         int nCount = 0;
211         while(itor.hasNext()) {
212             BooleanValuePanel valuePanel = (BooleanValuePanel) itor.next();
213             valuePanel.setSize(valuePanel.getPreferredSize().width-50, valuePanel.getPreferredSize().height);
214             valuePanel.setLocation(10, nYPos);
215             nXPos = valuePanel.getLocation().x + valuePanel.getSize().width;
216             
217             nYPos = nYPos + valuePanel.getSize().height;
218             
219             JButton removeButton = (JButton)this.m_removeButtons.get(nCount);
220             removeButton.setSize(20, 20);
221             removeButton.setLocation(nXPos - removeButton.getPreferredSize().width - 50, nYPos);
222             
223             nCount++;
224         }
225         
226         if(this.m_addButton!=null) {
227             this.m_addButton.setSize(20, 20);
228             this.m_addButton.setLocation(nXPos - (m_addButton.getPreferredSize().width*2) - 55, nYPos-3);
229
230         }
231         
232         this.m_nHeight = nYPos;// + 5;
233

234         this.repaint();
235     }
236
237     /* (non-Javadoc)
238      * @see java.awt.LayoutManager#addLayoutComponent(java.lang.String, java.awt.Component)
239      */

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

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

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

261     public void actionPerformed(ActionEvent ae) {
262         PropertyInstance propInst = this.getPropertyInstance();
263
264         if (ae.getActionCommand().equals("REMOVE")
265             && this.m_valuePanels.size() > this.m_nMinOccurs) {
266             int nIndex = this.m_removeButtons.indexOf(ae.getSource());
267             this.remove((Component) this.m_removeButtons.get(nIndex));
268             this.m_removeButtons.remove(nIndex);
269             this.m_nHeight =
270                 this.m_nHeight
271                     - ((Component) this.m_valuePanels.get(nIndex))
272                         .getSize()
273                         .height;
274             this.remove((Component) this.m_valuePanels.get(nIndex));
275             this.m_valuePanels.remove(nIndex);
276         } else if (
277             ae.getActionCommand().equals("ADD")
278                 && this.m_valuePanels.size() < this.m_nMaxOccurs) {
279                     BooleanValuePanel valPanel =
280                 new BooleanValuePanel(this,this.getPropertyInstance(), "");
281             this.add(valPanel);
282             this.m_valuePanels.add(valPanel);
283             this.m_nHeight =
284                 this.m_nHeight + valPanel.getPreferredSize().height;
285                 JButton removeButton = new JButton();
286                 String JavaDoc fontName = "Dialog";
287                 int fontSize = 13;
288                 Font font = new Font(fontName, Font.BOLD, fontSize);
289                 removeButton.setFont(font);
290                 removeButton.setIcon(IconManager.getInstance().getIcon("16-command-value-minus.gif"));
291                 removeButton.setActionCommand("REMOVE");
292                 removeButton.addActionListener(this);
293                 removeButton.setMargin( new Insets(2,2,2,2) );
294             this.add(removeButton);
295             this.m_removeButtons.add(removeButton);
296         }
297
298         this.checkButtonVisibility();
299
300         Component comp = this.getParent();
301         while (!(comp instanceof MetadataTabs)) {
302             if (comp != null) {
303                 comp = comp.getParent();
304             }
305         }
306         if (comp != null) {
307             comp.validate();
308         }
309     }
310
311     /**
312      * Checks and sets the visibility of the addition and
313      * removal buttons.
314      *
315      */

316     private void checkButtonVisibility() {
317         if (this.m_bShowingDelButtons
318             && this.m_removeButtons.size() <= this.m_nMinOccurs) {
319             Iterator itor = this.m_removeButtons.iterator();
320             while (itor.hasNext()) {
321                 JButton button = (JButton) itor.next();
322                 button.setVisible(false);
323             }
324             this.m_bShowingDelButtons = false;
325         } else if (
326             !this.m_bShowingDelButtons
327                 && this.m_removeButtons.size() > this.m_nMinOccurs) {
328             Iterator itor = this.m_removeButtons.iterator();
329             while (itor.hasNext()) {
330                 JButton button = (JButton) itor.next();
331                 button.setVisible(true);
332             }
333             this.m_bShowingDelButtons = true;
334         }
335         if (this.m_addButton != null
336             && this.m_addButton.isVisible()
337             && this.m_valuePanels.size() >= this.m_nMaxOccurs) {
338             this.m_addButton.setVisible(false);
339             this.m_nHeight = this.m_nHeight - this.m_addButton.getSize().height;
340         } else if (
341             this.m_addButton != null
342                 && !this.m_addButton.isVisible()
343                 && this.m_valuePanels.size() < this.m_nMaxOccurs) {
344             this.m_addButton.setVisible(true);
345             this.m_nHeight = this.m_nHeight + this.m_addButton.getSize().height;
346         }
347
348     }
349     
350     /**
351      * Method called if a value has been altered.
352      *
353      * @param sValue Value that has been altered
354      */

355     protected void valueChanged(String JavaDoc sValue) {
356         ArrayList aValues = new ArrayList();
357         
358         Iterator itor = this.m_valuePanels.iterator();
359         while(itor.hasNext()) {
360             String JavaDoc sVal = ((BooleanValuePanel)itor.next()).getValue();
361             if(!sVal.equals("")) {
362                 BooleanValue val = (BooleanValue)this.getPropertyInstance().getNewValueInstance();
363                 val.setValue( Boolean.valueOf( sVal ).booleanValue() );
364                 aValues.add( val );
365             }
366         }
367         if(this.getPropertyInstance().getValues().size()!=aValues.size() || !this.getPropertyInstance().getValues().containsAll(aValues)) {
368             this.getPropertyInstance().setValues(aValues);
369         }
370
371         super.validateTab();
372     }
373
374     /* (non-Javadoc)
375      * @see com.simulacramedia.contentmanager.metadata.range.swing.AbstractRangeDisplay#isValid()
376      */

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