KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > css > visual > ui > BorderWidthField


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 /*
21  * BorderWidthField.java
22  *
23  * Created on October 22, 2004, 5:08 PM
24  */

25
26 package org.netbeans.modules.css.visual.ui;
27
28 import org.netbeans.modules.css.visual.model.BorderModel;
29 import org.netbeans.modules.css.visual.model.PropertyWithUnitData;
30 import org.netbeans.modules.css.visual.model.BorderModel;
31 import java.beans.PropertyChangeSupport JavaDoc;
32 import javax.swing.DefaultComboBoxModel JavaDoc;
33 import javax.swing.JTextField JavaDoc;
34 import javax.swing.SwingUtilities JavaDoc;
35 import org.netbeans.modules.css.visual.model.Utils;
36
37 /**
38  * Border Width field wiith text field and unit combo box
39  * @author Winston Prakash
40  * @version 1.0
41  */

42 public class BorderWidthField extends javax.swing.JPanel JavaDoc {
43     PropertyWithUnitData borderWidthData = new PropertyWithUnitData();
44
45     BorderModel borderModel = new BorderModel();
46
47     /** Creates new form BorderWidthField */
48     public BorderWidthField() {
49         initComponents();
50         borderWidthCombo.setModel(borderModel.getWidthList());
51         borderWidthUnitCombo.setModel(borderModel.getWidthUnitList());
52
53         // Add editor listeners to the border width combobox
54
final JTextField JavaDoc borderWidthComboEditor = (JTextField JavaDoc) borderWidthCombo.getEditor().getEditorComponent();
55         borderWidthComboEditor.addKeyListener(new java.awt.event.KeyAdapter JavaDoc() {
56             public void keyTyped(java.awt.event.KeyEvent JavaDoc evt) {
57                 SwingUtilities.invokeLater(new Runnable JavaDoc(){
58                     public void run(){
59                         borderWidthUnitCombo.setEnabled(Utils.isInteger(borderWidthComboEditor.getText()));
60                     }
61                 });
62             }
63         });
64     }
65
66     private PropertyChangeSupport JavaDoc propertyChangeSupport = new PropertyChangeSupport JavaDoc(this);
67
68     /**
69      * Adds a PropertyChangeListener to the listener list.
70      * @param l The listener to add.
71      */

72     public void addPropertyChangeListener(java.beans.PropertyChangeListener JavaDoc l) {
73         propertyChangeSupport.addPropertyChangeListener(l);
74     }
75
76     /**
77      * Removes a PropertyChangeListener from the listener list.
78      * @param l The listener to remove.
79      */

80     public void removePropertyChangeListener(java.beans.PropertyChangeListener JavaDoc l) {
81         propertyChangeSupport.removePropertyChangeListener(l);
82     }
83
84     public void setWidthString(String JavaDoc widthStr){
85         if((widthStr != null) && !widthStr.equals("")){
86             if(Utils.isInteger(widthStr)){
87                 setWidthValue(widthStr);
88             }else{
89                 String JavaDoc unit = getUnit(widthStr);
90                 setWidthUnit(unit);
91                 setWidthValue(widthStr.replaceAll(unit,"").trim());
92             }
93         }else{
94             setWidthValue(null);
95             setWidthUnit(null);
96         }
97     }
98
99     public String JavaDoc getWidthString(){
100         return borderWidthData.toString();
101     }
102
103     private String JavaDoc getUnit(String JavaDoc widthStr){
104         DefaultComboBoxModel JavaDoc unitList = borderModel.getWidthUnitList();
105         for(int i=0; i< unitList.getSize(); i++){
106             String JavaDoc unit = (String JavaDoc)unitList.getElementAt(i);
107             if(widthStr.trim().endsWith(unit)){
108                 return unit;
109             }
110         }
111         return "";
112     }
113
114     public void setWidthValue(String JavaDoc value){
115         if((value == null) || value.equals("")){
116             borderWidthCombo.setSelectedIndex(0);
117         }else{
118             borderWidthCombo.setSelectedItem(value);
119             borderWidthData.setValue(value);
120         }
121     }
122
123     public void setWidthUnit(String JavaDoc value){
124         if((value == null) || value.equals("")){
125             borderWidthUnitCombo.setSelectedIndex(borderModel.getWidthUnitList().getIndexOf("px"));
126         }else{
127             if(borderModel.getWidthUnitList().getIndexOf(value) != -1){
128                 borderWidthUnitCombo.setSelectedIndex(borderModel.getWidthUnitList().getIndexOf(value));
129             }else{
130                 borderWidthUnitCombo.setSelectedIndex(borderModel.getWidthUnitList().getIndexOf("px"));
131             }
132             borderWidthData.setUnit(value);
133         }
134     }
135     
136     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
137
private void initComponents() {
138         borderWidthCombo = new javax.swing.JComboBox JavaDoc();
139         borderWidthUnitCombo = new javax.swing.JComboBox JavaDoc();
140
141         setLayout(new java.awt.BorderLayout JavaDoc(3, 0));
142
143         borderWidthCombo.setEditable(true);
144         borderWidthCombo.addItemListener(new java.awt.event.ItemListener JavaDoc() {
145             public void itemStateChanged(java.awt.event.ItemEvent JavaDoc evt) {
146                 borderWidthComboItemStateChanged(evt);
147             }
148         });
149         borderWidthCombo.addActionListener(new java.awt.event.ActionListener JavaDoc() {
150             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
151                 borderWidthComboActionPerformed(evt);
152             }
153         });
154         borderWidthCombo.addFocusListener(new java.awt.event.FocusAdapter JavaDoc() {
155             public void focusLost(java.awt.event.FocusEvent JavaDoc evt) {
156                 borderWidthComboFocusLost(evt);
157             }
158         });
159
160         add(borderWidthCombo, java.awt.BorderLayout.CENTER);
161         borderWidthCombo.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(BorderWidthField.class, "BORDER_WIDTH_FIELD_ACCESS_NAME"));
162         borderWidthCombo.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(BorderWidthField.class, "BORDER_WIDTH_FIELD_ACCESS_DESC"));
163
164         borderWidthUnitCombo.setEnabled(false);
165         borderWidthUnitCombo.addItemListener(new java.awt.event.ItemListener JavaDoc() {
166             public void itemStateChanged(java.awt.event.ItemEvent JavaDoc evt) {
167                 borderWidthUnitComboItemStateChanged(evt);
168             }
169         });
170         borderWidthUnitCombo.addFocusListener(new java.awt.event.FocusAdapter JavaDoc() {
171             public void focusLost(java.awt.event.FocusEvent JavaDoc evt) {
172                 borderWidthUnitComboFocusLost(evt);
173             }
174         });
175
176         add(borderWidthUnitCombo, java.awt.BorderLayout.EAST);
177         borderWidthUnitCombo.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(BorderWidthField.class, "BORDER_WIDTH_UNIT_ACCESS_NAME"));
178         borderWidthUnitCombo.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(BorderWidthField.class, "BORDER_WIDTH_UNIT_ACCESS_DESC"));
179
180     }
181     // </editor-fold>//GEN-END:initComponents
182

183     private void borderWidthUnitComboFocusLost(java.awt.event.FocusEvent JavaDoc evt) {//GEN-FIRST:event_borderWidthUnitComboFocusLost
184
setBorderWidth();
185     }//GEN-LAST:event_borderWidthUnitComboFocusLost
186

187     private void borderWidthUnitComboItemStateChanged(java.awt.event.ItemEvent JavaDoc evt) {//GEN-FIRST:event_borderWidthUnitComboItemStateChanged
188
if (evt.getStateChange() != evt.DESELECTED) {
189             setBorderWidth();
190         }
191     }//GEN-LAST:event_borderWidthUnitComboItemStateChanged
192

193     // For accessibility
194
public void setAccessibleName(String JavaDoc comboName, String JavaDoc unitName){
195         borderWidthCombo.getAccessibleContext().setAccessibleName(comboName);
196         borderWidthUnitCombo.getAccessibleContext().setAccessibleName(unitName);
197     }
198     
199     // For accessibility
200
public void setAccessibleDescription(String JavaDoc comboDesc, String JavaDoc unitDesc){
201         borderWidthCombo.getAccessibleContext().setAccessibleDescription(comboDesc);
202         borderWidthUnitCombo.getAccessibleContext().setAccessibleDescription(unitDesc);
203     }
204     
205     private void borderWidthComboItemStateChanged(java.awt.event.ItemEvent JavaDoc evt) {//GEN-FIRST:event_borderWidthComboItemStateChanged
206
if (evt.getStateChange() != evt.DESELECTED) {
207             setBorderWidth();
208             borderWidthUnitCombo.setEnabled(Utils.isInteger(borderWidthCombo.getSelectedItem().toString()));
209         }
210     }//GEN-LAST:event_borderWidthComboItemStateChanged
211

212     private void borderWidthComboFocusLost(java.awt.event.FocusEvent JavaDoc evt) {//GEN-FIRST:event_borderWidthComboFocusLost
213
setBorderWidth();
214     }//GEN-LAST:event_borderWidthComboFocusLost
215

216     private void borderWidthComboActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_borderWidthComboActionPerformed
217
setBorderWidth();
218     }//GEN-LAST:event_borderWidthComboActionPerformed
219

220     private void setBorderWidth(){
221         String JavaDoc oldValue = borderWidthData.toString();
222         borderWidthData.setUnit(borderWidthUnitCombo.getSelectedItem().toString());
223         borderWidthData.setValue( borderWidthCombo.getSelectedItem().toString());
224         propertyChangeSupport.firePropertyChange("border-width", oldValue, borderWidthData.toString());//NOI18N
225
}
226     
227     // Variables declaration - do not modify//GEN-BEGIN:variables
228
private javax.swing.JComboBox JavaDoc borderWidthCombo;
229     private javax.swing.JComboBox JavaDoc borderWidthUnitCombo;
230     // End of variables declaration//GEN-END:variables
231

232 }
233
Popular Tags