001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: /*
043: * BorderWidthField.java
044: *
045: * Created on October 22, 2004, 5:08 PM
046: */
047:
048: package org.netbeans.modules.css.visual.ui;
049:
050: import org.netbeans.modules.css.visual.model.BorderModel;
051: import org.netbeans.modules.css.visual.model.PropertyWithUnitData;
052: import org.netbeans.modules.css.visual.model.BorderModel;
053: import javax.swing.DefaultComboBoxModel;
054: import javax.swing.JTextField;
055: import javax.swing.SwingUtilities;
056: import org.netbeans.modules.css.visual.model.Utils;
057:
058: /**
059: * Border Width field wiith text field and unit combo box
060: * @author Winston Prakash
061: * @version 1.0
062: */
063: public class BorderWidthField extends javax.swing.JPanel {
064: PropertyWithUnitData borderWidthData = new PropertyWithUnitData();
065:
066: BorderModel borderModel = new BorderModel();
067:
068: /** Creates new form BorderWidthField */
069: public BorderWidthField() {
070: initComponents();
071: borderWidthCombo.setModel(borderModel.getWidthList());
072: borderWidthUnitCombo.setModel(borderModel.getWidthUnitList());
073:
074: // Add editor listeners to the border width combobox
075: final JTextField borderWidthComboEditor = (JTextField) borderWidthCombo
076: .getEditor().getEditorComponent();
077: borderWidthComboEditor
078: .addKeyListener(new java.awt.event.KeyAdapter() {
079: public void keyTyped(java.awt.event.KeyEvent evt) {
080: SwingUtilities.invokeLater(new Runnable() {
081: public void run() {
082: borderWidthUnitCombo
083: .setEnabled(Utils
084: .isInteger(borderWidthComboEditor
085: .getText()));
086: }
087: });
088: }
089: });
090: }
091:
092: public void setWidthString(String widthStr) {
093: if ((widthStr != null) && !widthStr.equals("")) {
094: if (Utils.isInteger(widthStr)) {
095: setWidthValue(widthStr);
096: } else {
097: String unit = getUnit(widthStr);
098: setWidthUnit(unit);
099: setWidthValue(widthStr.replaceAll(unit, "").trim());
100: }
101: } else {
102: setWidthValue(null);
103: setWidthUnit(null);
104: }
105: }
106:
107: public String getWidthString() {
108: return borderWidthData.toString();
109: }
110:
111: private String getUnit(String widthStr) {
112: DefaultComboBoxModel unitList = borderModel.getWidthUnitList();
113: for (int i = 0; i < unitList.getSize(); i++) {
114: String unit = (String) unitList.getElementAt(i);
115: if (widthStr.trim().endsWith(unit)) {
116: return unit;
117: }
118: }
119: return "";
120: }
121:
122: public void setWidthValue(String value) {
123: if ((value == null) || value.equals("")) {
124: borderWidthCombo.setSelectedIndex(0);
125: } else {
126: borderWidthCombo.setSelectedItem(value);
127: borderWidthData.setValue(value);
128: }
129: }
130:
131: public void setWidthUnit(String value) {
132: if ((value == null) || value.equals("")) {
133: borderWidthUnitCombo.setSelectedIndex(borderModel
134: .getWidthUnitList().getIndexOf("px"));
135: } else {
136: if (borderModel.getWidthUnitList().getIndexOf(value) != -1) {
137: borderWidthUnitCombo.setSelectedIndex(borderModel
138: .getWidthUnitList().getIndexOf(value));
139: } else {
140: borderWidthUnitCombo.setSelectedIndex(borderModel
141: .getWidthUnitList().getIndexOf("px"));
142: }
143: borderWidthData.setUnit(value);
144: }
145: }
146:
147: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
148: private void initComponents() {
149: borderWidthCombo = new javax.swing.JComboBox();
150: borderWidthUnitCombo = new javax.swing.JComboBox();
151:
152: setLayout(new java.awt.BorderLayout(3, 0));
153:
154: borderWidthCombo.setEditable(true);
155: borderWidthCombo
156: .addItemListener(new java.awt.event.ItemListener() {
157: public void itemStateChanged(
158: java.awt.event.ItemEvent evt) {
159: borderWidthComboItemStateChanged(evt);
160: }
161: });
162: borderWidthCombo
163: .addActionListener(new java.awt.event.ActionListener() {
164: public void actionPerformed(
165: java.awt.event.ActionEvent evt) {
166: borderWidthComboActionPerformed(evt);
167: }
168: });
169: borderWidthCombo
170: .addFocusListener(new java.awt.event.FocusAdapter() {
171: public void focusLost(java.awt.event.FocusEvent evt) {
172: borderWidthComboFocusLost(evt);
173: }
174: });
175:
176: add(borderWidthCombo, java.awt.BorderLayout.CENTER);
177: borderWidthCombo.getAccessibleContext().setAccessibleName(
178: org.openide.util.NbBundle.getMessage(
179: BorderWidthField.class,
180: "BORDER_WIDTH_FIELD_ACCESS_NAME"));
181: borderWidthCombo.getAccessibleContext()
182: .setAccessibleDescription(
183: org.openide.util.NbBundle.getMessage(
184: BorderWidthField.class,
185: "BORDER_WIDTH_FIELD_ACCESS_DESC"));
186:
187: borderWidthUnitCombo.setEnabled(false);
188: borderWidthUnitCombo
189: .addItemListener(new java.awt.event.ItemListener() {
190: public void itemStateChanged(
191: java.awt.event.ItemEvent evt) {
192: borderWidthUnitComboItemStateChanged(evt);
193: }
194: });
195: borderWidthUnitCombo
196: .addFocusListener(new java.awt.event.FocusAdapter() {
197: public void focusLost(java.awt.event.FocusEvent evt) {
198: borderWidthUnitComboFocusLost(evt);
199: }
200: });
201:
202: add(borderWidthUnitCombo, java.awt.BorderLayout.EAST);
203: borderWidthUnitCombo.getAccessibleContext().setAccessibleName(
204: org.openide.util.NbBundle.getMessage(
205: BorderWidthField.class,
206: "BORDER_WIDTH_UNIT_ACCESS_NAME"));
207: borderWidthUnitCombo.getAccessibleContext()
208: .setAccessibleDescription(
209: org.openide.util.NbBundle.getMessage(
210: BorderWidthField.class,
211: "BORDER_WIDTH_UNIT_ACCESS_DESC"));
212:
213: }
214:
215: // </editor-fold>//GEN-END:initComponents
216:
217: private void borderWidthUnitComboFocusLost(
218: java.awt.event.FocusEvent evt) {//GEN-FIRST:event_borderWidthUnitComboFocusLost
219: setBorderWidth();
220: }//GEN-LAST:event_borderWidthUnitComboFocusLost
221:
222: private void borderWidthUnitComboItemStateChanged(
223: java.awt.event.ItemEvent evt) {//GEN-FIRST:event_borderWidthUnitComboItemStateChanged
224: if (evt.getStateChange() != evt.DESELECTED) {
225: setBorderWidth();
226: }
227: }//GEN-LAST:event_borderWidthUnitComboItemStateChanged
228:
229: // For accessibility
230: public void setAccessibleName(String comboName, String unitName) {
231: borderWidthCombo.getAccessibleContext().setAccessibleName(
232: comboName);
233: borderWidthUnitCombo.getAccessibleContext().setAccessibleName(
234: unitName);
235: }
236:
237: // For accessibility
238: public void setAccessibleDescription(String comboDesc,
239: String unitDesc) {
240: borderWidthCombo.getAccessibleContext()
241: .setAccessibleDescription(comboDesc);
242: borderWidthUnitCombo.getAccessibleContext()
243: .setAccessibleDescription(unitDesc);
244: }
245:
246: private void borderWidthComboItemStateChanged(
247: java.awt.event.ItemEvent evt) {//GEN-FIRST:event_borderWidthComboItemStateChanged
248: if (evt.getStateChange() != evt.DESELECTED) {
249: setBorderWidth();
250: borderWidthUnitCombo.setEnabled(Utils
251: .isInteger(borderWidthCombo.getSelectedItem()
252: .toString()));
253: }
254: }//GEN-LAST:event_borderWidthComboItemStateChanged
255:
256: private void borderWidthComboFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_borderWidthComboFocusLost
257: setBorderWidth();
258: }//GEN-LAST:event_borderWidthComboFocusLost
259:
260: private void borderWidthComboActionPerformed(
261: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_borderWidthComboActionPerformed
262: setBorderWidth();
263: }//GEN-LAST:event_borderWidthComboActionPerformed
264:
265: private void setBorderWidth() {
266: String oldValue = borderWidthData.toString();
267: borderWidthData.setUnit(borderWidthUnitCombo.getSelectedItem()
268: .toString());
269: borderWidthData.setValue(borderWidthCombo.getSelectedItem()
270: .toString());
271: firePropertyChange("border-width", oldValue, borderWidthData
272: .toString());//NOI18N
273: }
274:
275: // Variables declaration - do not modify//GEN-BEGIN:variables
276: private javax.swing.JComboBox borderWidthCombo;
277: private javax.swing.JComboBox borderWidthUnitCombo;
278: // End of variables declaration//GEN-END:variables
279:
280: }
|