KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > him > metadata > range > swing > propertyhandling > PropertyRangeDisplay


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.propertyhandling;
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.*;
31 import org.openharmonise.vfs.gui.*;
32 import org.openharmonise.vfs.metadata.*;
33 import org.openharmonise.vfs.metadata.range.*;
34 import org.openharmonise.vfs.metadata.value.*;
35
36
37 /**
38  * @author Matthew Large
39  * @version $Revision: 1.1 $
40  *
41  */

42 public class PropertyRangeDisplay
43     extends AbstractRangeDisplay
44     implements RangeDisplay, LayoutManager, ActionListener {
45
46         private int m_nHeight = 0;
47         
48         private int m_nMinOccurs = 1;
49         private int m_nMaxOccurs = 1000;
50         
51         private JButton m_addButton = null;
52         
53         private ArrayList m_valuePanels = new ArrayList();
54         private ArrayList m_removeButtons = new ArrayList();
55         
56         private boolean m_bShowingDelButtons = true;
57
58
59     /**
60      * @param propInstance
61      */

62     public PropertyRangeDisplay(PropertyInstance propInstance) {
63         super(propInstance);
64         this.setup();
65         
66     }
67     
68     private void setup() {
69         BoxLayout layout = new BoxLayout(this, BoxLayout.Y_AXIS);
70         this.setLayout( this );
71     }
72
73     /* (non-Javadoc)
74      * @see com.simulacramedia.contentmanager.metadata.range.swing.RangeDisplay#getPanel()
75      */

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

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

182     public void removeLayoutComponent(Component arg0) {
183     }
184
185     /* (non-Javadoc)
186      * @see java.awt.LayoutManager#layoutContainer(java.awt.Container)
187      */

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

229     public void addLayoutComponent(String JavaDoc arg0, Component arg1) {
230     }
231
232     /* (non-Javadoc)
233      * @see java.awt.LayoutManager#minimumLayoutSize(java.awt.Container)
234      */

235     public Dimension minimumLayoutSize(Container arg0) {
236         return this.getPreferredSize();
237     }
238
239     /* (non-Javadoc)
240      * @see java.awt.LayoutManager#preferredLayoutSize(java.awt.Container)
241      */

242     public Dimension preferredLayoutSize(Container arg0) {
243         return this.getPreferredSize();
244     }
245
246     /* (non-Javadoc)
247      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
248      */

249     public void actionPerformed(ActionEvent ae) {
250         PropertyInstance propInst = this.getPropertyInstance();
251         
252         if(ae.getActionCommand().equals("REMOVE") && this.m_valuePanels.size()>this.m_nMinOccurs) {
253             int nIndex = this.m_removeButtons.indexOf(ae.getSource());
254             this.remove((Component)this.m_removeButtons.get(nIndex));
255             this.m_removeButtons.remove(nIndex);
256             this.m_nHeight = this.m_nHeight-((Component)this.m_valuePanels.get(nIndex)).getSize().height;
257             this.remove((Component)this.m_valuePanels.get(nIndex));
258             this.getPropertyInstance().clearAllValues();
259             this.m_valuePanels.remove(nIndex);
260             Iterator itor = this.m_valuePanels.iterator();
261             while (itor.hasNext()) {
262                 PropertyValuePanel propValuePanel = (PropertyValuePanel) itor.next();
263                 this.getPropertyInstance().addValue(propValuePanel.getValue());
264             }
265         } else if(ae.getActionCommand().equals("ADD") && this.m_valuePanels.size()<this.m_nMaxOccurs) {
266             PropertyValue newValue = (PropertyValue) this.getPropertyInstance().getNewValueInstance();
267             this.getPropertyInstance().addValue(newValue);
268             PropertyValuePanel valPanel = new PropertyValuePanel(this, this.getPropertyInstance(), newValue);
269             this.add(valPanel);
270             this.m_valuePanels.add(valPanel.getPanel());
271             this.m_nHeight = this.m_nHeight + valPanel.getPreferredSize().height;
272             JButton removeButton = new JButton();
273             String JavaDoc fontName = "Dialog";
274             int fontSize = 13;
275             Font font = new Font(fontName, Font.BOLD, fontSize);
276             removeButton.setFont(font);
277             removeButton.setIcon(IconManager.getInstance().getIcon("16-command-value-minus.gif"));
278             removeButton.setActionCommand("REMOVE");
279             removeButton.addActionListener(this);
280             removeButton.setMargin( new Insets(2,2,2,2) );
281             this.add(removeButton);
282             this.m_removeButtons.add(removeButton);
283         }
284         
285         this.checkButtonVisibility();
286         
287         Component comp = this.getParent();
288         while(!(comp instanceof MetadataTabs)) {
289             if(comp instanceof PropertyRangeDisplay) {
290                 comp.validate();
291             }
292             if(comp!=null) {
293                 comp = comp.getParent();
294             }
295         }
296         if(comp!=null) {
297             comp.validate();
298         }
299         super.validateTab();
300     }
301     
302     private void checkButtonVisibility() {
303         if(this.m_bShowingDelButtons && this.m_removeButtons.size()<=this.m_nMinOccurs) {
304             Iterator itor = this.m_removeButtons.iterator();
305             while(itor.hasNext()) {
306                 JButton button = (JButton)itor.next();
307                 button.setVisible(false);
308             }
309             this.m_bShowingDelButtons=false;
310         } else if(!this.m_bShowingDelButtons && this.m_removeButtons.size()>this.m_nMinOccurs) {
311             Iterator itor = this.m_removeButtons.iterator();
312             while(itor.hasNext()) {
313                 JButton button = (JButton)itor.next();
314                 button.setVisible(true);
315             }
316             this.m_bShowingDelButtons=true;
317         }
318         
319         if(this.m_addButton!=null && this.m_addButton.isVisible() && this.m_valuePanels.size()>=this.m_nMaxOccurs) {
320             this.m_addButton.setVisible(false);
321             this.m_nHeight = this.m_nHeight-this.m_addButton.getSize().height;
322         } else if(this.m_addButton!=null && this.m_valuePanels.size()<this.m_nMaxOccurs) {
323             this.m_addButton.setVisible(true);
324             this.m_nHeight = this.m_nHeight+this.m_addButton.getSize().height;
325         }
326     }
327     
328     protected void valueChanged(String JavaDoc sValue) {
329         ArrayList aValues = new ArrayList();
330         
331         Iterator itor = this.m_valuePanels.iterator();
332         while(itor.hasNext()) {
333             PropertyValue tempValue = ((PropertyValuePanel)itor.next()).getValue();
334             if(tempValue!=null) {
335                 aValues.add( tempValue );
336             }
337         }
338         if(!this.getPropertyInstance().getValues().containsAll(aValues)) {
339             this.getPropertyInstance().setValues(aValues);
340         }
341     }
342
343     /* (non-Javadoc)
344      * @see com.simulacramedia.contentmanager.metadata.range.swing.AbstractRangeDisplay#isValid()
345      */

346     public boolean isMetadataValid() {
347         boolean bValid = true;
348         for (int i = 0; i < this.getComponentCount(); i++) {
349             Component comp = this.getComponent(i);
350             if(comp instanceof AbstractRangeDisplay) {
351                 if(!((AbstractRangeDisplay)comp).isMetadataValid()) {
352                     bValid=false;
353                 }
354             }
355         }
356         return bValid;
357     }
358 }
359
Popular Tags