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: * PaddingWidthField.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.MarginPaddingModel;
051: import org.netbeans.modules.css.visual.model.PropertyWithUnitData;
052: import org.netbeans.modules.css.visual.model.Utils;
053: import java.beans.PropertyChangeSupport;
054: import javax.swing.DefaultComboBoxModel;
055: import javax.swing.JTextField;
056: import javax.swing.SwingUtilities;
057:
058: /**
059: * Padding Width field wiith text field and unit combo box
060: * @author Winston Prakash
061: * @version 1.0
062: */
063: public class PaddingWidthField extends javax.swing.JPanel {
064: PropertyWithUnitData borderPaddingData = new PropertyWithUnitData();
065: MarginPaddingModel marginPaddingModel = new MarginPaddingModel();
066:
067: /** Creates new form borderMarginField */
068: public PaddingWidthField() {
069: initComponents();
070: borderPaddingCombo
071: .setModel(marginPaddingModel.getPaddingList());
072: borderPaddingUnitCombo.setModel(marginPaddingModel
073: .getPaddingUnitList());
074:
075: // Add editor listeners to the padding width combobox
076: final JTextField borderPaddingComboEditor = (JTextField) borderPaddingCombo
077: .getEditor().getEditorComponent();
078: borderPaddingComboEditor
079: .addKeyListener(new java.awt.event.KeyAdapter() {
080: public void keyTyped(java.awt.event.KeyEvent evt) {
081: SwingUtilities.invokeLater(new Runnable() {
082: public void run() {
083: borderPaddingUnitCombo
084: .setEnabled(Utils
085: .isInteger(borderPaddingComboEditor
086: .getText()));
087: }
088: });
089: }
090: });
091: }
092:
093: public void setPaddingString(String paddingStr) {
094: if ((paddingStr != null) && !paddingStr.equals("")) {
095: if (Utils.isInteger(paddingStr)) {
096: setWidthValue(paddingStr);
097: } else {
098: String unit = getUnit(paddingStr);
099: setWidthUnit(unit);
100: setWidthValue(paddingStr.replaceAll(unit, "").trim());
101: }
102: } else {
103: setWidthValue(null);
104: setWidthUnit(null);
105: }
106: }
107:
108: public String getPaddingString() {
109: return borderPaddingData.toString();
110: }
111:
112: private String getUnit(String paddingStr) {
113: DefaultComboBoxModel unitList = marginPaddingModel
114: .getPaddingUnitList();
115: for (int i = 0; i < unitList.getSize(); i++) {
116: String unit = (String) unitList.getElementAt(i);
117: if (paddingStr.trim().endsWith(unit)) {
118: return unit;
119: }
120: }
121: return "";
122: }
123:
124: public void setWidthValue(String value) {
125: if ((value == null) || value.equals("")) {
126: borderPaddingCombo.setSelectedIndex(0);
127: } else {
128: borderPaddingCombo.setSelectedItem(value);
129: borderPaddingData.setValue(value);
130: }
131: }
132:
133: public void setWidthUnit(String value) {
134: if ((value == null) || value.equals("")) {
135: borderPaddingUnitCombo.setSelectedIndex(marginPaddingModel
136: .getPaddingUnitList().getIndexOf("px")); //NOI18N
137: } else {
138: if (marginPaddingModel.getMarginUnitList()
139: .getIndexOf(value) != -1) {
140: borderPaddingUnitCombo
141: .setSelectedIndex(marginPaddingModel
142: .getPaddingUnitList().getIndexOf(value));
143: } else {
144: borderPaddingUnitCombo
145: .setSelectedIndex(marginPaddingModel
146: .getPaddingUnitList().getIndexOf("px")); //NOI18N
147: }
148: borderPaddingData.setUnit(value);
149: }
150: }
151:
152: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
153: private void initComponents() {
154: borderPaddingCombo = new javax.swing.JComboBox();
155: borderPaddingUnitCombo = new javax.swing.JComboBox();
156:
157: setLayout(new java.awt.BorderLayout(5, 0));
158:
159: borderPaddingCombo.setEditable(true);
160: borderPaddingCombo
161: .addActionListener(new java.awt.event.ActionListener() {
162: public void actionPerformed(
163: java.awt.event.ActionEvent evt) {
164: borderPaddingComboActionPerformed(evt);
165: }
166: });
167: borderPaddingCombo
168: .addFocusListener(new java.awt.event.FocusAdapter() {
169: public void focusLost(java.awt.event.FocusEvent evt) {
170: borderPaddingComboFocusLost(evt);
171: }
172: });
173: borderPaddingCombo
174: .addItemListener(new java.awt.event.ItemListener() {
175: public void itemStateChanged(
176: java.awt.event.ItemEvent evt) {
177: borderPaddingComboItemStateChanged(evt);
178: }
179: });
180:
181: add(borderPaddingCombo, java.awt.BorderLayout.CENTER);
182: borderPaddingCombo.getAccessibleContext().setAccessibleName(
183: org.openide.util.NbBundle.getMessage(
184: PaddingWidthField.class,
185: "PADDIN_WIDTH_ACCESS_NAME"));
186: borderPaddingCombo.getAccessibleContext()
187: .setAccessibleDescription(
188: org.openide.util.NbBundle.getMessage(
189: PaddingWidthField.class,
190: "PADDING_WIDTH_FIELD_ACCESS_DESC"));
191:
192: borderPaddingUnitCombo.setEnabled(false);
193: borderPaddingUnitCombo
194: .addItemListener(new java.awt.event.ItemListener() {
195: public void itemStateChanged(
196: java.awt.event.ItemEvent evt) {
197: borderPaddingUnitComboItemStateChanged(evt);
198: }
199: });
200: borderPaddingUnitCombo
201: .addFocusListener(new java.awt.event.FocusAdapter() {
202: public void focusLost(java.awt.event.FocusEvent evt) {
203: borderPaddingUnitComboFocusLost(evt);
204: }
205: });
206:
207: add(borderPaddingUnitCombo, java.awt.BorderLayout.EAST);
208: borderPaddingUnitCombo.getAccessibleContext()
209: .setAccessibleName(
210: org.openide.util.NbBundle.getMessage(
211: PaddingWidthField.class,
212: "PADDING_WIDTH_UNIT_ACCESS_NAME"));
213: borderPaddingUnitCombo.getAccessibleContext()
214: .setAccessibleDescription(
215: org.openide.util.NbBundle.getMessage(
216: PaddingWidthField.class,
217: "PADDING_WIDTH_UNIT_ACCESS_DESC"));
218:
219: }
220:
221: // </editor-fold>//GEN-END:initComponents
222:
223: private void borderPaddingUnitComboFocusLost(
224: java.awt.event.FocusEvent evt) {//GEN-FIRST:event_borderPaddingUnitComboFocusLost
225: setBorderPadding();
226: }//GEN-LAST:event_borderPaddingUnitComboFocusLost
227:
228: private void borderPaddingUnitComboItemStateChanged(
229: java.awt.event.ItemEvent evt) {//GEN-FIRST:event_borderPaddingUnitComboItemStateChanged
230: if (evt.getStateChange() != evt.DESELECTED) {
231: setBorderPadding();
232: }
233: }//GEN-LAST:event_borderPaddingUnitComboItemStateChanged
234:
235: // For accessibility
236: public void setAccessibleName(String comboName, String unitName) {
237: borderPaddingCombo.getAccessibleContext().setAccessibleName(
238: comboName);
239: borderPaddingUnitCombo.getAccessibleContext()
240: .setAccessibleName(unitName);
241: }
242:
243: // For accessibility
244: public void setAccessibleDescription(String comboDesc,
245: String unitDesc) {
246: borderPaddingCombo.getAccessibleContext()
247: .setAccessibleDescription(comboDesc);
248: borderPaddingUnitCombo.getAccessibleContext()
249: .setAccessibleDescription(unitDesc);
250: }
251:
252: private void borderPaddingComboItemStateChanged(
253: java.awt.event.ItemEvent evt) {//GEN-FIRST:event_borderPaddingComboItemStateChanged
254: if (evt.getStateChange() != evt.DESELECTED) {
255: borderPaddingUnitCombo.setEnabled(Utils
256: .isInteger(borderPaddingCombo.getSelectedItem()
257: .toString()));
258: setBorderPadding();
259: }
260: }//GEN-LAST:event_borderPaddingComboItemStateChanged
261:
262: private void borderPaddingComboFocusLost(
263: java.awt.event.FocusEvent evt) {//GEN-FIRST:event_borderPaddingComboFocusLost
264: setBorderPadding();
265: }//GEN-LAST:event_borderPaddingComboFocusLost
266:
267: private void borderPaddingComboActionPerformed(
268: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_borderPaddingComboActionPerformed
269: setBorderPadding();
270: }//GEN-LAST:event_borderPaddingComboActionPerformed
271:
272: private void setBorderPadding() {
273: String oldValue = borderPaddingData.toString();
274: borderPaddingData.setUnit(borderPaddingUnitCombo
275: .getSelectedItem().toString());
276: borderPaddingData.setValue(borderPaddingCombo.getSelectedItem()
277: .toString());
278: firePropertyChange("margin-width", oldValue, borderPaddingData
279: .toString()); //NOI18N
280: }
281:
282: // Variables declaration - do not modify//GEN-BEGIN:variables
283: private javax.swing.JComboBox borderPaddingCombo;
284: private javax.swing.JComboBox borderPaddingUnitCombo;
285: // End of variables declaration//GEN-END:variables
286:
287: }
|