KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
22 import java.awt.event.*;
23 import java.util.*;
24 import java.util.List JavaDoc;
25
26 import javax.swing.*;
27 import javax.swing.text.*;
28
29 import org.openharmonise.him.metadata.range.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
35 /**
36  * <p>Title: IntegerValuePanel</p>
37  * <p>Description: </p>
38  * <p>Copyright: SimulacraMedia 2003</p>
39  * <p>Company: Simulacra</p>
40  * @author Matthew Large
41  *
42  */

43 public class IntegerValuePanel
44     extends AbstractRangeDisplay
45     implements RangeDisplay, LayoutManager, KeyListener {
46
47     protected int m_nHeight = 10;
48         
49     private int m_nMinOccurs = 0;
50     private int m_nMaxOccurs = 1000;
51     
52     protected JTextComponent m_textComp = null;
53
54     private String JavaDoc m_sPreviousText = "";
55
56     protected WarningsLabel m_warnings = null;
57
58     protected JLabel m_errorLabel = null;
59
60     private String JavaDoc m_sValue = "";
61     
62     private IntegerRangeDisplay m_display = null;
63     
64     /**
65      * @param propInstance
66      */

67     public IntegerValuePanel(IntegerRangeDisplay display, PropertyInstance propInstance, String JavaDoc sValue) {
68         super(propInstance);
69         this.m_display = display;
70         this.m_sValue = sValue;
71         this.setup();
72     }
73
74     /* (non-Javadoc)
75      * @see com.simulacramedia.contentmanager.metadata.range.swing.RangeDisplay#getPanel()
76      */

77     public void setup() {
78         this.setLayout(this);
79         IntegerRange range =
80             (IntegerRange) this
81                 .getPropertyInstance()
82                 .getDefinition()
83                 .getRange();
84
85         List JavaDoc domains = this.getPropertyInstance().getDefinition().getDomains();
86         Iterator itor = domains.iterator();
87         
88         while(itor.hasNext()) {
89             Domain domain = (Domain)itor.next();
90             boolean bApplicableDomain = false;
91             String JavaDoc sFilePath = ((VersionedVirtualFile)this.getPropertyInstance().getVirtualFile()).getLogicalPath();
92             Iterator itor2 = domain.getPaths().iterator();
93             while (itor2.hasNext()) {
94                 String JavaDoc sDomainPath = (String JavaDoc) itor2.next();
95                 if(sFilePath.startsWith(sDomainPath)) {
96                     bApplicableDomain=true;
97                 }
98             }
99     
100             if(bApplicableDomain) {
101                 if(this.m_nMinOccurs<domain.getMinOccurs()) {
102                     this.m_nMinOccurs = domain.getMinOccurs();
103                 }
104                 if(this.m_nMaxOccurs>domain.getMaxOccurs()) {
105                     this.m_nMaxOccurs = domain.getMaxOccurs();
106                 }
107             }
108             
109         }
110
111         this.m_errorLabel = new JLabel();
112         this.m_errorLabel.setIcon(
113             IconManager.getInstance().getIcon("16-message-confirm.gif"));
114         this.add(this.m_errorLabel);
115
116         String JavaDoc sWarning = "";
117
118         if (range.getMinimum() != null && range.getMaximum() != null) {
119             sWarning =
120                 "Please type a number between {"
121                     + range.getMinimum()
122                     + "} and {"
123                     + range.getMaximum()
124                     + "}.";
125         } else if (range.getMaximum() != null) {
126             sWarning =
127                 "Please type a number less than {" + range.getMaximum() + "}.";
128         } else if (range.getMinimum() != null) {
129             sWarning =
130                 "Please type a number greater than {"
131                     + range.getMinimum()
132                     + "}.";
133         } else {
134             sWarning = "Please type a number.";
135         }
136
137         m_warnings = new WarningsLabel(sWarning);
138         this.add(m_warnings);
139
140         m_textComp = new JTextField(this.m_sValue);
141         this.m_sPreviousText = this.m_sValue;
142         m_textComp.addKeyListener(this);
143         m_textComp.setBorder(BorderFactory.createEtchedBorder());
144         int nHeight = 30;
145         m_nHeight = nHeight + 20;
146         m_textComp.setPreferredSize(new Dimension(200, 20));
147         this.add(m_textComp);
148
149         this.keyReleased(null);
150     }
151
152     /* (non-Javadoc)
153      * @see java.awt.Component#getPreferredSize()
154      */

155     public Dimension getPreferredSize() {
156         this.layoutContainer(null);
157         int nWidth = this.getParent().getWidth();
158         int nHeight = 10;
159         nHeight = nHeight + this.m_textComp.getSize().height;
160         return new Dimension(nWidth, nHeight);
161     }
162
163     /* (non-Javadoc)
164      * @see java.awt.LayoutManager#removeLayoutComponent(java.awt.Component)
165      */

166     public void removeLayoutComponent(Component arg0) {
167         // NO-OP
168
}
169
170     /* (non-Javadoc)
171      * @see java.awt.LayoutManager#layoutContainer(java.awt.Container)
172      */

173     public void layoutContainer(Container arg0) {
174         int nWidth = this.getParent().getWidth() - 18;
175
176         Dimension dim = m_textComp.getPreferredSize();
177         m_textComp.setPreferredSize(new Dimension(nWidth - 85, dim.height));
178         m_textComp.setSize(m_textComp.getPreferredSize());
179         m_textComp.setLocation(20, 0);
180
181         this.m_errorLabel.setLocation(0,0);
182         this.m_errorLabel.setSize(15, 15);
183         this.repaint();
184     }
185
186     /* (non-Javadoc)
187      * @see java.awt.LayoutManager#addLayoutComponent(java.lang.String, java.awt.Component)
188      */

189     public void addLayoutComponent(String JavaDoc arg0, Component arg1) {
190         // NO-OP
191
}
192
193     /* (non-Javadoc)
194      * @see java.awt.LayoutManager#minimumLayoutSize(java.awt.Container)
195      */

196     public Dimension minimumLayoutSize(Container arg0) {
197         return this.getPreferredSize();
198     }
199
200     /* (non-Javadoc)
201      * @see java.awt.LayoutManager#preferredLayoutSize(java.awt.Container)
202      */

203     public Dimension preferredLayoutSize(Container arg0) {
204         return this.getPreferredSize();
205     }
206
207     /* (non-Javadoc)
208      * @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
209      */

210     public void keyPressed(KeyEvent arg0) {
211         // NO-OP
212
}
213
214     /* (non-Javadoc)
215      * @see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent)
216      */

217     public void keyReleased(KeyEvent ke) {
218         if (ke != null) {
219             char key = ke.getKeyChar();
220
221             this.m_errorLabel.setIcon(
222                 IconManager.getInstance().getIcon("16-message-confirm.gif"));
223             m_textComp.setBorder(BorderFactory.createEtchedBorder());
224
225             if (!(key == '0'
226                 || key == '1'
227                 || key == '2'
228                 || key == '3'
229                 || key == '4'
230                 || key == '5'
231                 || key == '6'
232                 || key == '7'
233                 || key == '8'
234                 || key == '9'
235                 || key == '-'
236                 || ke.getKeyCode() == KeyEvent.VK_RIGHT
237                 || ke.getKeyCode() == KeyEvent.VK_LEFT
238                 || ke.getKeyCode() == KeyEvent.VK_DELETE
239                 || ke.getKeyCode() == KeyEvent.VK_BACK_SPACE)
240                 || ke.getKeyCode() == KeyEvent.VK_SPACE) {
241                 this.m_textComp.setText(this.m_sPreviousText);
242             } else if (key == '-') {
243                 String JavaDoc sTemp = this.m_textComp.getText();
244                 if (sTemp.lastIndexOf("-") > 0) {
245                     this.m_textComp.setText(this.m_sPreviousText);
246                 } else {
247                     this.m_sPreviousText = this.m_textComp.getText();
248                 }
249             } else {
250                 this.m_sPreviousText = this.m_textComp.getText();
251             }
252         }
253
254         if (!this.m_textComp.getText().equals("")) {
255             int fVal = Integer.parseInt(this.m_textComp.getText());
256             IntegerRange range =
257                 (IntegerRange) this
258                     .getPropertyInstance()
259                     .getDefinition()
260                     .getRange();
261
262             if (range.getMinimum() != null && range.getMaximum() != null) {
263                 if (fVal < range.getMinimum().intValue()) {
264                     this.m_warnings.setHighlight(
265                         range.getMinimum().toString(),
266                         true);
267                     this.m_errorLabel.setIcon(
268                         IconManager.getInstance().getIcon(
269                             "16-message-error.gif"));
270                     m_textComp.setBorder(
271                         BorderFactory.createLineBorder(Color.RED));
272                 } else {
273                     this.m_warnings.setHighlight(
274                         range.getMinimum().toString(),
275                         false);
276                 }
277                 if (fVal > range.getMaximum().intValue()) {
278                     this.m_warnings.setHighlight(
279                         range.getMaximum().toString(),
280                         true);
281                     this.m_errorLabel.setIcon(
282                         IconManager.getInstance().getIcon(
283                             "16-message-error.gif"));
284                     m_textComp.setBorder(
285                         BorderFactory.createLineBorder(Color.RED));
286                 } else {
287                     this.m_warnings.setHighlight(
288                         range.getMaximum().toString(),
289                         false);
290                 }
291             } else if (range.getMinimum() != null) {
292                 if (fVal < range.getMinimum().intValue()) {
293                     this.m_warnings.setHighlight(
294                         range.getMinimum().toString(),
295                         true);
296                     this.m_errorLabel.setIcon(
297                         IconManager.getInstance().getIcon(
298                             "16-message-error.gif"));
299                     m_textComp.setBorder(
300                         BorderFactory.createLineBorder(Color.RED));
301                 } else {
302                     this.m_warnings.setHighlight(
303                         range.getMinimum().toString(),
304                         false);
305                 }
306             } else if (range.getMaximum() != null) {
307                 if (fVal > range.getMaximum().intValue()) {
308                     this.m_warnings.setHighlight(
309                         range.getMaximum().toString(),
310                         true);
311                     this.m_errorLabel.setIcon(
312                         IconManager.getInstance().getIcon(
313                             "16-message-error.gif"));
314                     m_textComp.setBorder(
315                         BorderFactory.createLineBorder(Color.RED));
316                 } else {
317                     this.m_warnings.setHighlight(
318                         range.getMaximum().toString(),
319                         false);
320                 }
321             }
322         } else if(this.m_nMinOccurs>0) {
323             this.m_errorLabel.setIcon(
324                 IconManager.getInstance().getIcon(
325                     "16-message-error.gif"));
326             m_textComp.setBorder(
327                 BorderFactory.createLineBorder(Color.RED));
328         }
329         
330         if(!this.m_errorLabel.getIcon().toString().equals(IconManager.getInstance().getIcon("16-message-error.gif").toString()) && this.getParent()!=null) {
331             this.m_display.valueChanged(this.m_textComp.getText().trim());
332         }
333         super.validateTab();
334
335     }
336
337     /* (non-Javadoc)
338      * @see java.awt.event.KeyListener#keyTyped(java.awt.event.KeyEvent)
339      */

340     public void keyTyped(KeyEvent arg0) {
341         // NO-OP
342
}
343
344     /* (non-Javadoc)
345      * @see com.simulacramedia.contentmanager.metadata.range.swing.RangeDisplay#getPanel()
346      */

347     public JPanel getPanel() {
348         return null;
349     }
350     
351     protected String JavaDoc getValue() {
352         return this.m_textComp.getText().trim();
353     }
354
355     /* (non-Javadoc)
356      * @see com.simulacramedia.contentmanager.metadata.range.swing.AbstractRangeDisplay#isValid()
357      */

358     public boolean isMetadataValid() {
359         return !this.m_errorLabel.getIcon().toString().equals(IconManager.getInstance().getIcon("16-message-error.gif").toString());
360     }
361
362 }
363
Popular Tags