KickJava   Java API By Example, From Geeks To Geeks.

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


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  * borderMarginField.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  * Margin Width field wiith text field and unit combo box
38  * @author Winston Prakash
39  * @version 1.0
40  */

41 public class MarginWidthField extends javax.swing.JPanel JavaDoc {
42     PropertyWithUnitData borderMarginData = new PropertyWithUnitData();
43     MarginPaddingModel marginPaddingModel = new MarginPaddingModel();
44
45     /** Creates new form borderMarginField */
46     public MarginWidthField() {
47         initComponents();
48         borderMarginCombo.setModel(marginPaddingModel.getMarginList());
49         borderMarginUnitCombo.setModel(marginPaddingModel.getMarginUnitList());
50
51         // Add editor listeners to the margin width combobox
52
final JTextField JavaDoc borderMarginComboEditor = (JTextField JavaDoc) borderMarginCombo.getEditor().getEditorComponent();
53         borderMarginComboEditor.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                         borderMarginUnitCombo.setEnabled(Utils.isInteger(borderMarginComboEditor.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     /**
75      * Removes a PropertyChangeListener from the listener list.
76      * @param l The listener to remove.
77      */

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

181     private void borderMarginUnitComboFocusLost(java.awt.event.FocusEvent JavaDoc evt) {//GEN-FIRST:event_borderMarginUnitComboFocusLost
182
setBorderMargin();
183     }//GEN-LAST:event_borderMarginUnitComboFocusLost
184

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

209     private void borderMarginComboFocusLost(java.awt.event.FocusEvent JavaDoc evt) {//GEN-FIRST:event_borderMarginComboFocusLost
210
setBorderMargin();
211     }//GEN-LAST:event_borderMarginComboFocusLost
212

213     private void borderMarginComboActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_borderMarginComboActionPerformed
214
setBorderMargin();
215     }//GEN-LAST:event_borderMarginComboActionPerformed
216

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

229 }
230
Popular Tags