KickJava   Java API By Example, From Geeks To Geeks.

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


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.Color JavaDoc;
22 import java.awt.Component JavaDoc;
23 import java.awt.Container JavaDoc;
24 import java.awt.Dimension JavaDoc;
25 import java.awt.Font JavaDoc;
26 import java.awt.LayoutManager JavaDoc;
27 import java.awt.event.ActionEvent JavaDoc;
28 import java.awt.event.KeyEvent JavaDoc;
29 import java.awt.event.KeyListener JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32
33 import javax.swing.AbstractAction JavaDoc;
34 import javax.swing.Action JavaDoc;
35 import javax.swing.BorderFactory JavaDoc;
36 import javax.swing.JComponent JavaDoc;
37 import javax.swing.JLabel JavaDoc;
38 import javax.swing.JPanel JavaDoc;
39 import javax.swing.JScrollPane JavaDoc;
40 import javax.swing.JTextArea JavaDoc;
41 import javax.swing.JTextField JavaDoc;
42 import javax.swing.KeyStroke JavaDoc;
43 import javax.swing.border.EtchedBorder JavaDoc;
44 import javax.swing.text.JTextComponent JavaDoc;
45
46 import org.openharmonise.him.metadata.range.swing.*;
47 import org.openharmonise.vfs.*;
48 import org.openharmonise.vfs.gui.*;
49 import org.openharmonise.vfs.metadata.*;
50 import org.openharmonise.vfs.metadata.range.*;
51
52
53 /**
54  * @author Matthew Large
55  *
56  */

57 public class StringValuePanel
58     extends AbstractRangeDisplay
59     implements RangeDisplay, KeyListener JavaDoc, LayoutManager JavaDoc {
60
61         private int m_nHeight = 30;
62         
63         private int m_nMinOccurs = 0;
64         private int m_nMaxOccurs = 1000;
65
66     private JTextComponent JavaDoc m_textComp = null;
67     private Counter m_counter = null;
68
69     private WarningsLabel m_warnings = null;
70
71     private JLabel JavaDoc m_errorLabel = null;
72     
73     private String JavaDoc m_sValue = "";
74     
75     private StringRangeDisplay m_display = null;
76     
77     private JScrollPane JavaDoc m_textScroller = null;
78     
79     private int m_nValueHeight = 20;
80
81     /**
82      * @param propInstance
83      */

84     public StringValuePanel(StringRangeDisplay display, PropertyInstance propInstance, String JavaDoc sValue) {
85         super(propInstance);
86         this.m_display = display;
87         this.m_sValue = sValue;
88         this.setup();
89     }
90
91     /* (non-Javadoc)
92      * @see com.simulacramedia.contentmanager.metadata.range.swing.RangeDisplay#getPanel()
93      */

94      private void setup() {
95         this.setLayout(this);
96
97         StringRange range =
98             (StringRange) this.getPropertyInstance().getDefinition().getRange();
99
100         List JavaDoc domains = this.getPropertyInstance().getDefinition().getDomains();
101         Iterator JavaDoc itor = domains.iterator();
102         
103         while(itor.hasNext()) {
104             Domain domain = (Domain)itor.next();
105             boolean bApplicableDomain = false;
106             String JavaDoc sFilePath = ((VersionedVirtualFile)this.getPropertyInstance().getVirtualFile()).getLogicalPath();
107             Iterator JavaDoc itor2 = domain.getPaths().iterator();
108             while (itor2.hasNext()) {
109                 String JavaDoc sDomainPath = (String JavaDoc) itor2.next();
110                 if(sFilePath.startsWith(sDomainPath)) {
111                     bApplicableDomain=true;
112                 }
113             }
114     
115             if(bApplicableDomain) {
116                 if(this.m_nMinOccurs<domain.getMinOccurs()) {
117                     this.m_nMinOccurs = domain.getMinOccurs();
118                 }
119                 if(this.m_nMaxOccurs>domain.getMaxOccurs()) {
120                     this.m_nMaxOccurs = domain.getMaxOccurs();
121                 }
122             }
123             
124         }
125
126         this.m_errorLabel = new JLabel JavaDoc();
127         this.m_errorLabel.setIcon(
128             IconManager.getInstance().getIcon("16-message-confirm.gif"));
129         this.add(this.m_errorLabel);
130
131         String JavaDoc sWarning = "";
132         if (range.getMinLength() != -1 && range.getMaxLength() != -1) {
133             sWarning =
134                 "Please type text between {"
135                     + range.getMinLength()
136                     + "} and {"
137                     + range.getMaxLength()
138                     + "} characters.";
139         } else if (range.getMinLength() != -1) {
140             sWarning =
141                 "Please type text with more than {"
142                     + range.getMinLength()
143                     + "} characters.";
144         } else if (range.getMaxLength() != -1) {
145             sWarning =
146                 "Please type text with less than {"
147                     + range.getMaxLength()
148                     + "} characters.";
149         } else {
150             sWarning = "Please type some text.";
151         }
152
153         m_warnings = new WarningsLabel(sWarning);
154         this.add(m_warnings);
155
156         if (range.getMaxLength() <= StringRange.SIZE_BORDER) {
157             m_textComp = new JTextField JavaDoc(this.m_sValue);
158             m_textComp.setBorder(BorderFactory.createEtchedBorder());
159             m_textComp.setPreferredSize(new Dimension JavaDoc(200, 20));
160             this.m_textComp.addKeyListener(this);
161             this.add(m_textComp);
162             m_counter =
163                 new Counter(
164                     m_textComp,
165                     (StringRange) this
166                         .getPropertyInstance()
167                         .getDefinition()
168                         .getRange());
169         } else {
170             JTextArea JavaDoc textComp = new JTextArea JavaDoc(this.m_sValue);
171             this.m_textComp = textComp;
172             textComp.setWrapStyleWord(true);
173             textComp.setLineWrap(true);
174
175             m_textScroller = new JScrollPane JavaDoc(textComp, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
176             m_textScroller.setPreferredSize(new Dimension JavaDoc(400, 100));
177             m_textScroller.setFocusable(false);
178             m_nValueHeight = 100;
179             textComp.addKeyListener(this);
180             this.add(m_textScroller);
181             m_counter =
182                 new Counter(
183                     textComp,
184                     (StringRange) this
185                         .getPropertyInstance()
186                         .getDefinition()
187                         .getRange());
188             textComp.getInputMap(JComponent.WHEN_FOCUSED).put(
189                     KeyStroke.getKeyStroke("TAB"), nextFocusAction.getValue(Action.NAME));
190             textComp.getInputMap(JComponent.WHEN_FOCUSED).put(
191                     KeyStroke.getKeyStroke("shift TAB"), prevFocusAction.getValue(Action.NAME));
192             textComp.getActionMap().put(nextFocusAction.getValue(Action.NAME),nextFocusAction);
193             textComp.getActionMap().put(prevFocusAction.getValue(Action.NAME),prevFocusAction);
194         }
195
196         this.m_textComp.setFont(new Font JavaDoc("Arial Unicode MS", Font.PLAIN, 11));
197         
198         this.add(m_counter);
199         
200         this.keyReleased(null);
201         
202         if(this.m_display.isReadOnly()){
203             m_textComp.setFocusable(false);
204         }
205     }
206 // The actions
207
public Action JavaDoc nextFocusAction = new AbstractAction JavaDoc("Move Focus Forwards") {
208         public void actionPerformed(ActionEvent JavaDoc evt) {
209             ((Component JavaDoc)evt.getSource()).transferFocus();
210         }
211     };
212     public Action JavaDoc prevFocusAction = new AbstractAction JavaDoc("Move Focus Backwards") {
213         public void actionPerformed(ActionEvent JavaDoc evt) {
214             ((Component JavaDoc)evt.getSource()).transferFocusBackward();
215         }
216     };
217
218     /* (non-Javadoc)
219      * @see java.awt.Component#getPreferredSize()
220      */

221     public Dimension JavaDoc getPreferredSize() {
222         this.layoutContainer(null);
223         int nWidth = this.getParent().getWidth();
224         int nHeight = 10;
225         nHeight = nHeight + m_nValueHeight;
226         return new Dimension JavaDoc(nWidth, nHeight);
227     }
228
229     /* (non-Javadoc)
230      * @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
231      */

232     public void keyPressed(KeyEvent JavaDoc arg0) {
233         // NO-OP-generated method stub
234
}
235
236     /* (non-Javadoc)
237      * @see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent)
238      */

239     public void keyReleased(KeyEvent JavaDoc arg0) {
240
241         this.m_errorLabel.setIcon(
242             IconManager.getInstance().getIcon("16-message-confirm.gif"));
243         if(this.m_textScroller!=null) {
244             m_textScroller.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
245         } else {
246             m_textComp.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
247         }
248
249         StringRange range =
250             (StringRange) this.getPropertyInstance().getDefinition().getRange();
251         int nLength = this.m_textComp.getText().length();
252         if (range.getMinLength() != -1 && range.getMaxLength() != -1) {
253             if (nLength < range.getMinLength()) {
254                 this.m_warnings.setHighlight(
255                     Integer.toString(range.getMinLength()),
256                     true);
257                 this.m_errorLabel.setIcon(
258                     IconManager.getInstance().getIcon("16-message-error.gif"));
259                 if(this.m_textScroller!=null) {
260                     m_textScroller.setBorder(BorderFactory.createLineBorder(Color.RED));
261                 } else {
262                     m_textComp.setBorder(BorderFactory.createLineBorder(Color.RED));
263                 }
264             } else {
265                 this.m_warnings.setHighlight(
266                     Integer.toString(range.getMinLength()),
267                     false);
268             }
269             if (nLength > range.getMaxLength()) {
270                 this.m_warnings.setHighlight(
271                     Integer.toString(range.getMaxLength()),
272                     true);
273                 this.m_errorLabel.setIcon(
274                     IconManager.getInstance().getIcon("16-message-error.gif"));
275                 if(this.m_textScroller!=null) {
276                     m_textScroller.setBorder(BorderFactory.createLineBorder(Color.RED));
277                 } else {
278                     m_textComp.setBorder(BorderFactory.createLineBorder(Color.RED));
279                 }
280             } else {
281                 this.m_warnings.setHighlight(
282                     Integer.toString(range.getMaxLength()),
283                     false);
284             }
285         } else if (range.getMinLength() != -1) {
286             if (nLength < range.getMinLength()) {
287                 this.m_warnings.setHighlight(
288                     Integer.toString(range.getMinLength()),
289                     true);
290                 this.m_errorLabel.setIcon(
291                     IconManager.getInstance().getIcon("16-message-error.gif"));
292                 if(this.m_textScroller!=null) {
293                     m_textScroller.setBorder(BorderFactory.createLineBorder(Color.RED));
294                 } else {
295                     m_textComp.setBorder(BorderFactory.createLineBorder(Color.RED));
296                 }
297             } else {
298                 this.m_warnings.setHighlight(
299                     Integer.toString(range.getMinLength()),
300                     false);
301             }
302         } else if (range.getMaxLength() != -1) {
303             if (nLength > range.getMaxLength()) {
304                 this.m_warnings.setHighlight(
305                     Integer.toString(range.getMaxLength()),
306                     true);
307                 this.m_errorLabel.setIcon(
308                     IconManager.getInstance().getIcon("16-message-error.gif"));
309                 if(this.m_textScroller!=null) {
310                     m_textScroller.setBorder(BorderFactory.createLineBorder(Color.RED));
311                 } else {
312                     m_textComp.setBorder(BorderFactory.createLineBorder(Color.RED));
313                 }
314             } else {
315                 this.m_warnings.setHighlight(
316                     Integer.toString(range.getMaxLength()),
317                     false);
318             }
319         }
320         if(this.m_nMinOccurs>0 && this.m_textComp.getText().trim().length()<1) {
321             this.m_errorLabel.setIcon(
322                 IconManager.getInstance().getIcon("16-message-error.gif"));
323             if(this.m_textScroller!=null) {
324                     m_textScroller.setBorder(BorderFactory.createLineBorder(Color.RED));
325                 } else {
326                     m_textComp.setBorder(BorderFactory.createLineBorder(Color.RED));
327                 }
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     /* (non-Javadoc)
337      * @see java.awt.event.KeyListener#keyTyped(java.awt.event.KeyEvent)
338      */

339     public void keyTyped(KeyEvent JavaDoc arg0) {
340         // NO-OP
341
}
342
343     /* (non-Javadoc)
344      * @see java.awt.LayoutManager#removeLayoutComponent(java.awt.Component)
345      */

346     public void removeLayoutComponent(Component JavaDoc arg0) {
347     }
348
349     /* (non-Javadoc)
350      * @see java.awt.LayoutManager#layoutContainer(java.awt.Container)
351      */

352     public void layoutContainer(Container JavaDoc arg0) {
353         m_nHeight = 30;
354         int nWidth = this.getParent().getWidth() - 18;
355         Dimension JavaDoc dim = null;
356         if(this.m_textScroller!=null) {
357             dim = this.m_textScroller.getPreferredSize();
358             this.m_textScroller.setPreferredSize(
359                 new Dimension JavaDoc(nWidth - 105, dim.height));
360         } else {
361             dim = this.m_textComp.getPreferredSize();
362             this.m_textComp.setPreferredSize(
363                 new Dimension JavaDoc(nWidth - 105, dim.height));
364         }
365
366         this.m_counter.setSize(this.m_counter.getPreferredSize());
367
368         if(this.m_textScroller==null) {
369             this.m_textComp.setSize(this.m_textComp.getPreferredSize());
370             this.m_textComp.setLocation(20, 0);
371             this.m_nHeight = this.m_nHeight + m_nValueHeight;
372             this.m_counter.setLocation(
373                 this.m_textComp.getLocation().x
374                     + this.m_textComp.getSize().width
375                     + 5,
376                 this.m_textComp.getLocation().y);
377         } else {
378             this.m_textScroller.setSize(this.m_textScroller.getPreferredSize());
379             this.m_textScroller.setLocation(20, 0);
380             this.m_nHeight = this.m_nHeight + m_nValueHeight;
381             this.m_counter.setLocation(
382                 this.m_textScroller.getLocation().x
383                     + this.m_textScroller.getSize().width
384                     + 5,
385                 this.m_textScroller.getLocation().y);
386         }
387
388         this.m_errorLabel.setLocation(0,0);
389         this.m_errorLabel.setSize(15, 15);
390         this.repaint();
391     }
392
393     /* (non-Javadoc)
394      * @see java.awt.LayoutManager#addLayoutComponent(java.lang.String, java.awt.Component)
395      */

396     public void addLayoutComponent(String JavaDoc arg0, Component JavaDoc arg1) {
397     }
398
399     /* (non-Javadoc)
400      * @see java.awt.LayoutManager#minimumLayoutSize(java.awt.Container)
401      */

402     public Dimension JavaDoc minimumLayoutSize(Container JavaDoc arg0) {
403         return this.getPreferredSize();
404     }
405
406     /* (non-Javadoc)
407      * @see java.awt.LayoutManager#preferredLayoutSize(java.awt.Container)
408      */

409     public Dimension JavaDoc preferredLayoutSize(Container JavaDoc arg0) {
410         return this.getPreferredSize();
411     }
412
413     /* (non-Javadoc)
414      * @see com.simulacramedia.contentmanager.metadata.range.swing.RangeDisplay#getPanel()
415      */

416     public JPanel JavaDoc getPanel() {
417         return null;
418     }
419     
420     protected String JavaDoc getValue() {
421         return this.m_textComp.getText().trim();
422     }
423
424     /* (non-Javadoc)
425      * @see com.simulacramedia.contentmanager.metadata.range.swing.AbstractRangeDisplay#isValid()
426      */

427     public boolean isMetadataValid() {
428         return !this.m_errorLabel.getIcon().toString().equals(IconManager.getInstance().getIcon("16-message-error.gif").toString());
429     }
430     
431     
432
433     /* (non-Javadoc)
434      * @see java.awt.Component#setEnabled(boolean)
435      */

436     public void setEnabled(boolean bEnabled) {
437         super.setEnabled(bEnabled);
438         if(this.m_textComp instanceof JTextArea JavaDoc) {
439             ((JTextArea JavaDoc)this.m_textComp).setEditable(bEnabled);
440         }
441     }
442
443 }
444
Popular Tags