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