KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > him > metadata > range > swing > stringhandling > StringRangeDisplay


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

42 public class StringRangeDisplay
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 StringRangeDisplay(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         StringRange range = (StringRange)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                     StringValuePanel valPanel = null;
116     
117                     valPanel = new StringValuePanel(this, this.getPropertyInstance(), ((StringValue)vals.get(i)).getValue());
118                     this.add(valPanel);
119                     this.m_valuePanels.add(valPanel);
120                     this.m_nHeight = this.m_nHeight + valPanel.getPreferredSize().height + 5;
121         
122                 } else {
123                     StringValuePanel valPanel = null;
124     
125                     valPanel = new StringValuePanel(this, this.getPropertyInstance(), "");
126                     this.add(valPanel);
127                     this.m_valuePanels.add(valPanel);
128                     this.m_nHeight = this.m_nHeight + valPanel.getPreferredSize().height + 5;
129     
130                 }
131                 JButton removeButton = new JButton();
132                 String JavaDoc fontName = "Dialog";
133                 int fontSize = 13;
134                 Font font = new Font(fontName, Font.BOLD, fontSize);
135                 removeButton.setFont(font);
136                 removeButton.setIcon(IconManager.getInstance().getIcon("16-command-value-minus.gif"));
137                 removeButton.setActionCommand("REMOVE");
138                 removeButton.addActionListener(this);
139                 removeButton.setMargin( new Insets(2,2,2,2) );
140                 removeButton.setFocusable(false);
141                 this.add(removeButton);
142                 this.m_removeButtons.add(removeButton);
143             } catch(ClassCastException JavaDoc cce) {
144
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             m_addButton.setFocusable(false);
161         }
162         
163         this.checkButtonVisibility();
164         
165         return this;
166     }
167     
168     
169
170     /* (non-Javadoc)
171      * @see java.awt.Component#getPreferredSize()
172      */

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

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

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

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

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

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

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

356     public boolean isMetadataValid() {
357         boolean bValid = true;
358         for (int i = 0; i < this.getComponentCount(); i++) {
359             Component comp = this.getComponent(i);
360             if(comp instanceof AbstractRangeDisplay) {
361                 if(!((AbstractRangeDisplay)comp).isMetadataValid()) {
362                     bValid=false;
363                 }
364             }
365         }
366         return bValid;
367     }
368 }
369
Popular Tags