KickJava   Java API By Example, From Geeks To Geeks.

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


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  * PaddingWidthField.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.MarginPaddingModel;
29 import org.netbeans.modules.css.visual.model.PropertyWithUnitData;
30 import org.netbeans.modules.css.visual.model.Utils;
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
36 /**
37  * Padding Width field wiith text field and unit combo box
38  * @author Winston Prakash
39  * @version 1.0
40  */

41 public class PaddingWidthField extends javax.swing.JPanel JavaDoc {
42     PropertyWithUnitData borderPaddingData = new PropertyWithUnitData();
43     MarginPaddingModel marginPaddingModel = new MarginPaddingModel();
44
45     /** Creates new form borderMarginField */
46     public PaddingWidthField() {
47         initComponents();
48         borderPaddingCombo.setModel(marginPaddingModel.getPaddingList());
49         borderPaddingUnitCombo.setModel(marginPaddingModel.getPaddingUnitList());
50
51         // Add editor listeners to the padding width combobox
52
final JTextField JavaDoc borderPaddingComboEditor = (JTextField JavaDoc) borderPaddingCombo.getEditor().getEditorComponent();
53         borderPaddingComboEditor.addKeyListener(new java.awt.event.KeyAdapter JavaDoc() {
54             public void keyTyped(java.awt.event.KeyEvent JavaDoc evt) {
55                 SwingUtilities.invokeLater(new Runnable JavaDoc(){
56                     public void run(){
57                        borderPaddingUnitCombo.setEnabled(Utils.isInteger(borderPaddingComboEditor.getText()));
58                     }
59                 });
60             }
61         });
62     }
63
64     private PropertyChangeSupport JavaDoc propertyChangeSupport = new PropertyChangeSupport JavaDoc(this);
65
66     /**
67      * Adds a PropertyChangeListener to the listener list.
68      * @param l The listener to add.
69      */

70     public void addPropertyChangeListener(java.beans.PropertyChangeListener JavaDoc l) {
71         propertyChangeSupport.addPropertyChangeListener(l);
72     }
73
74     public void setPaddingString(String JavaDoc paddingStr){
75         if((paddingStr != null) && !paddingStr.equals("")){
76             if(Utils.isInteger(paddingStr)){
77                 setWidthValue(paddingStr);
78             }else{
79                 String JavaDoc unit = getUnit(paddingStr);
80                 setWidthUnit(unit);
81                 setWidthValue(paddingStr.replaceAll(unit,"").trim());
82             }
83         }else{
84             setWidthValue(null);
85             setWidthUnit(null);
86         }
87     }
88
89     public String JavaDoc getPaddingString(){
90         return borderPaddingData.toString();
91     }
92     private String JavaDoc getUnit(String JavaDoc paddingStr){
93         DefaultComboBoxModel JavaDoc unitList = marginPaddingModel.getPaddingUnitList();
94         for(int i=0; i< unitList.getSize(); i++){
95             String JavaDoc unit = (String JavaDoc)unitList.getElementAt(i);
96             if(paddingStr.trim().endsWith(unit)){
97                 return unit;
98             }
99         }
100         return "";
101     }
102
103     public void setWidthValue(String JavaDoc value){
104         if((value == null) || value.equals("")){
105             borderPaddingCombo.setSelectedIndex(0);
106         }else{
107             borderPaddingCombo.setSelectedItem(value);
108             borderPaddingData.setValue(value);
109         }
110     }
111
112     public void setWidthUnit(String JavaDoc value){
113         if((value == null) || value.equals("")){
114             borderPaddingUnitCombo.setSelectedIndex(marginPaddingModel.getPaddingUnitList().getIndexOf("px")); //NOI18N
115
}else{
116             if(marginPaddingModel.getMarginUnitList().getIndexOf(value) != -1){
117                 borderPaddingUnitCombo.setSelectedIndex(marginPaddingModel.getPaddingUnitList().getIndexOf(value));
118             }else{
119                 borderPaddingUnitCombo.setSelectedIndex(marginPaddingModel.getPaddingUnitList().getIndexOf("px")); //NOI18N
120
}
121             borderPaddingData.setUnit(value);
122         }
123     }
124     
125     /**
126      * Removes a PropertyChangeListener from the listener list.
127      * @param l The listener to remove.
128      */

129     public void removePropertyChangeListener(java.beans.PropertyChangeListener JavaDoc l) {
130         propertyChangeSupport.removePropertyChangeListener(l);
131     }
132     
133     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
134
private void initComponents() {
135         borderPaddingCombo = new javax.swing.JComboBox JavaDoc();
136         borderPaddingUnitCombo = new javax.swing.JComboBox JavaDoc();
137
138         setLayout(new java.awt.BorderLayout JavaDoc(5, 0));
139
140         borderPaddingCombo.setEditable(true);
141         borderPaddingCombo.addActionListener(new java.awt.event.ActionListener JavaDoc() {
142             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
143                 borderPaddingComboActionPerformed(evt);
144             }
145         });
146         borderPaddingCombo.addFocusListener(new java.awt.event.FocusAdapter JavaDoc() {
147             public void focusLost(java.awt.event.FocusEvent JavaDoc evt) {
148                 borderPaddingComboFocusLost(evt);
149             }
150         });
151         borderPaddingCombo.addItemListener(new java.awt.event.ItemListener JavaDoc() {
152             public void itemStateChanged(java.awt.event.ItemEvent JavaDoc evt) {
153                 borderPaddingComboItemStateChanged(evt);
154             }
155         });
156
157         add(borderPaddingCombo, java.awt.BorderLayout.CENTER);
158         borderPaddingCombo.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(PaddingWidthField.class, "PADDIN_WIDTH_ACCESS_NAME"));
159         borderPaddingCombo.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PaddingWidthField.class, "PADDING_WIDTH_FIELD_ACCESS_DESC"));
160
161         borderPaddingUnitCombo.setEnabled(false);
162         borderPaddingUnitCombo.addItemListener(new java.awt.event.ItemListener JavaDoc() {
163             public void itemStateChanged(java.awt.event.ItemEvent JavaDoc evt) {
164                 borderPaddingUnitComboItemStateChanged(evt);
165             }
166         });
167         borderPaddingUnitCombo.addFocusListener(new java.awt.event.FocusAdapter JavaDoc() {
168             public void focusLost(java.awt.event.FocusEvent JavaDoc evt) {
169                 borderPaddingUnitComboFocusLost(evt);
170             }
171         });
172
173         add(borderPaddingUnitCombo, java.awt.BorderLayout.EAST);
174         borderPaddingUnitCombo.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(PaddingWidthField.class, "PADDING_WIDTH_UNIT_ACCESS_NAME"));
175         borderPaddingUnitCombo.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PaddingWidthField.class, "PADDING_WIDTH_UNIT_ACCESS_DESC"));
176
177     }
178     // </editor-fold>//GEN-END:initComponents
179

180     private void borderPaddingUnitComboFocusLost(java.awt.event.FocusEvent JavaDoc evt) {//GEN-FIRST:event_borderPaddingUnitComboFocusLost
181
setBorderPadding();
182     }//GEN-LAST:event_borderPaddingUnitComboFocusLost
183

184     private void borderPaddingUnitComboItemStateChanged(java.awt.event.ItemEvent JavaDoc evt) {//GEN-FIRST:event_borderPaddingUnitComboItemStateChanged
185
if (evt.getStateChange() != evt.DESELECTED) {
186             setBorderPadding();
187         }
188     }//GEN-LAST:event_borderPaddingUnitComboItemStateChanged
189
// For accessibility
190
public void setAccessibleName(String JavaDoc comboName, String JavaDoc unitName){
191         borderPaddingCombo.getAccessibleContext().setAccessibleName(comboName);
192         borderPaddingUnitCombo.getAccessibleContext().setAccessibleName(unitName);
193     }
194     
195     // For accessibility
196
public void setAccessibleDescription(String JavaDoc comboDesc, String JavaDoc unitDesc){
197         borderPaddingCombo.getAccessibleContext().setAccessibleDescription(comboDesc);
198         borderPaddingUnitCombo.getAccessibleContext().setAccessibleDescription(unitDesc);
199     }
200     
201     private void borderPaddingComboItemStateChanged(java.awt.event.ItemEvent JavaDoc evt) {//GEN-FIRST:event_borderPaddingComboItemStateChanged
202
if (evt.getStateChange() != evt.DESELECTED) {
203             borderPaddingUnitCombo.setEnabled(Utils.isInteger(borderPaddingCombo.getSelectedItem().toString()));
204             setBorderPadding();
205         }
206     }//GEN-LAST:event_borderPaddingComboItemStateChanged
207

208     private void borderPaddingComboFocusLost(java.awt.event.FocusEvent JavaDoc evt) {//GEN-FIRST:event_borderPaddingComboFocusLost
209
setBorderPadding();
210     }//GEN-LAST:event_borderPaddingComboFocusLost
211

212     private void borderPaddingComboActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_borderPaddingComboActionPerformed
213
setBorderPadding();
214     }//GEN-LAST:event_borderPaddingComboActionPerformed
215

216     private void setBorderPadding(){
217         String JavaDoc oldValue = borderPaddingData.toString();
218         borderPaddingData.setUnit(borderPaddingUnitCombo.getSelectedItem().toString());
219         borderPaddingData.setValue( borderPaddingCombo.getSelectedItem().toString());
220         propertyChangeSupport.firePropertyChange("padding-width", oldValue, borderPaddingData.toString()); //NOI18N
221
}
222     
223     // Variables declaration - do not modify//GEN-BEGIN:variables
224
private javax.swing.JComboBox JavaDoc borderPaddingCombo;
225     private javax.swing.JComboBox JavaDoc borderPaddingUnitCombo;
226     // End of variables declaration//GEN-END:variables
227

228 }
229
Popular Tags