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: package org.netbeans.modules.visualweb.faces.dt.converter;
042:
043: import com.sun.rave.designtime.DesignProperty;
044: import java.awt.event.ActionListener;
045: import java.text.DecimalFormat;
046: import java.text.NumberFormat;
047: import javax.swing.JPanel;
048: import org.openide.util.NbBundle;
049:
050: /**
051: * Panel for editing the number pattern
052: *
053: * @author cao
054: */
055: public class NumberPatternPanel extends JPanel {
056:
057: private DesignProperty designProperty;
058: private NumberPatternPropertyEditor propertyEditor;
059:
060: /** Creates new form NumberPatternPanel */
061: public NumberPatternPanel(
062: NumberPatternPropertyEditor propertyEditor,
063: DesignProperty liveProperty) {
064:
065: this .propertyEditor = propertyEditor;
066: this .designProperty = liveProperty;
067: initComponents();
068:
069: // Populate the pattern combo box with some sample patterns
070: NumberFormat form1, form2, form3, form4;
071: form1 = NumberFormat.getInstance();
072: form2 = NumberFormat.getIntegerInstance();
073: form3 = NumberFormat.getCurrencyInstance();
074: form4 = NumberFormat.getPercentInstance();
075: cmbPattern.setModel(new javax.swing.DefaultComboBoxModel(
076: new String[] { "", ((DecimalFormat) form1).toPattern(),
077: ((DecimalFormat) form2).toPattern(),
078: ((DecimalFormat) form3).toPattern(),
079: ((DecimalFormat) form4).toPattern() }));
080:
081: // The Example part of the panel
082: cmbExample.setModel(new javax.swing.DefaultComboBoxModel(
083: new String[] { "1234.56", "-1234.56", "123.4567",
084: "0.123", "01234" }));
085:
086: if (this .designProperty != null) {
087: cmbPattern.setSelectedItem(designProperty.getValue());
088: }
089: }
090:
091: public void addPatternListener(ActionListener listener) {
092: cmbPattern.addActionListener(listener);
093: }
094:
095: // The sample result will be updated based on the selected type or pattern
096: private boolean upateSampleResult() {
097:
098: double sampleNumber = 0;
099:
100: try {
101: if (cmbExample.getSelectedItem() == null
102: || ((String) cmbExample.getSelectedItem()).trim()
103: .length() == 0)
104: return true;
105: else
106: sampleNumber = Double.parseDouble((String) cmbExample
107: .getSelectedItem());
108: } catch (NumberFormatException ne) {
109:
110: txtResults.setText(NbBundle.getMessage(
111: NumberPatternPanel.class, "notANumber",
112: (String) cmbExample.getSelectedItem()));
113: return false;
114: }
115:
116: // Pattern should be used to format the number
117: try {
118: if (cmbPattern.getSelectedItem() == null
119: || ((String) cmbPattern.getSelectedItem()).trim()
120: .length() == 0)
121: return true;
122: else {
123: DecimalFormat decimalFormat = (DecimalFormat) DecimalFormat
124: .getInstance();
125: decimalFormat.applyPattern(((String) cmbPattern
126: .getSelectedItem()).trim());
127: txtResults.setText(decimalFormat.format(sampleNumber));
128: }
129: } catch (IllegalArgumentException ie) {
130: txtResults.setText(NbBundle.getMessage(
131: NumberPatternPanel.class, "badPattern",
132: (String) cmbPattern.getSelectedItem()));
133: return false;
134: }
135:
136: return true;
137: }
138:
139: /** This method is called from within the constructor to
140: * initialize the form.
141: * WARNING: Do NOT modify this code. The content of this method is
142: * always regenerated by the Form Editor.
143: */
144: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
145: private void initComponents() {
146: java.awt.GridBagConstraints gridBagConstraints;
147: java.util.ResourceBundle bundle = java.util.ResourceBundle
148: .getBundle("org/netbeans/modules/visualweb/faces/dt/converter/Bundle"); // NOI18N
149: lblPattern = new javax.swing.JLabel();
150: cmbPattern = new javax.swing.JComboBox();
151: pnlExample = new javax.swing.JPanel();
152: jSeparator1 = new javax.swing.JSeparator();
153: lblExample = new javax.swing.JLabel();
154: txtResults = new javax.swing.JTextField();
155: lblResults = new javax.swing.JLabel();
156: cmbExample = new javax.swing.JComboBox();
157: btnTest = new javax.swing.JButton();
158: txtExampleInstructions = new javax.swing.JTextPane();
159: jPanel1 = new javax.swing.JPanel();
160:
161: setLayout(new java.awt.GridBagLayout());
162:
163: lblPattern.setLabelFor(cmbPattern);
164: lblPattern.setText(bundle.getString("pattern"));
165: gridBagConstraints = new java.awt.GridBagConstraints();
166: gridBagConstraints.gridx = 0;
167: gridBagConstraints.gridy = 0;
168: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
169: gridBagConstraints.insets = new java.awt.Insets(10, 10, 0, 0);
170: add(lblPattern, gridBagConstraints);
171: lblPattern.getAccessibleContext().setAccessibleDescription(
172: bundle.getString("pattern"));
173:
174: cmbPattern.setEditable(true);
175: cmbPattern
176: .addActionListener(new java.awt.event.ActionListener() {
177: public void actionPerformed(
178: java.awt.event.ActionEvent evt) {
179: cmbPatternActionPerformed(evt);
180: }
181: });
182:
183: gridBagConstraints = new java.awt.GridBagConstraints();
184: gridBagConstraints.gridwidth = 2;
185: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
186: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
187: gridBagConstraints.weightx = 1.0;
188: gridBagConstraints.insets = new java.awt.Insets(10, 10, 0, 10);
189: add(cmbPattern, gridBagConstraints);
190: cmbPattern.getAccessibleContext().setAccessibleDescription(
191: bundle.getString("pattern"));
192:
193: pnlExample.setLayout(new java.awt.GridBagLayout());
194:
195: gridBagConstraints = new java.awt.GridBagConstraints();
196: gridBagConstraints.gridwidth = 3;
197: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
198: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
199: gridBagConstraints.weightx = 1.0;
200: gridBagConstraints.insets = new java.awt.Insets(10, 10, 0, 10);
201: pnlExample.add(jSeparator1, gridBagConstraints);
202:
203: lblExample.setDisplayedMnemonic(bundle.getString(
204: "example_mnemonic").charAt(0));
205: lblExample.setLabelFor(cmbExample);
206: lblExample.setText(bundle.getString("example"));
207: gridBagConstraints = new java.awt.GridBagConstraints();
208: gridBagConstraints.gridx = 0;
209: gridBagConstraints.gridy = 2;
210: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
211: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
212: gridBagConstraints.insets = new java.awt.Insets(10, 10, 0, 0);
213: pnlExample.add(lblExample, gridBagConstraints);
214: lblExample.getAccessibleContext().setAccessibleDescription(
215: bundle.getString("example"));
216:
217: txtResults.setEditable(false);
218: txtResults.setText("1234.56");
219: txtResults
220: .addActionListener(new java.awt.event.ActionListener() {
221: public void actionPerformed(
222: java.awt.event.ActionEvent evt) {
223: txtResultsActionPerformed(evt);
224: }
225: });
226:
227: gridBagConstraints = new java.awt.GridBagConstraints();
228: gridBagConstraints.gridx = 1;
229: gridBagConstraints.gridy = 4;
230: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
231: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
232: gridBagConstraints.insets = new java.awt.Insets(10, 10, 0, 10);
233: pnlExample.add(txtResults, gridBagConstraints);
234: txtResults.getAccessibleContext().setAccessibleDescription(
235: bundle.getString("results"));
236:
237: lblResults.setDisplayedMnemonic(bundle.getString(
238: "result_mnemonic").charAt(0));
239: lblResults.setLabelFor(txtResults);
240: lblResults.setText(bundle.getString("results"));
241: gridBagConstraints = new java.awt.GridBagConstraints();
242: gridBagConstraints.gridx = 0;
243: gridBagConstraints.gridy = 4;
244: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
245: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
246: gridBagConstraints.insets = new java.awt.Insets(10, 10, 0, 0);
247: pnlExample.add(lblResults, gridBagConstraints);
248: lblResults.getAccessibleContext().setAccessibleDescription(
249: bundle.getString("results"));
250:
251: cmbExample.setEditable(true);
252: cmbExample
253: .addActionListener(new java.awt.event.ActionListener() {
254: public void actionPerformed(
255: java.awt.event.ActionEvent evt) {
256: cmbExampleActionPerformed(evt);
257: }
258: });
259:
260: gridBagConstraints = new java.awt.GridBagConstraints();
261: gridBagConstraints.gridx = 1;
262: gridBagConstraints.gridy = 2;
263: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
264: gridBagConstraints.weightx = 1.0;
265: gridBagConstraints.insets = new java.awt.Insets(10, 10, 0, 10);
266: pnlExample.add(cmbExample, gridBagConstraints);
267: cmbExample.getAccessibleContext().setAccessibleDescription(
268: bundle.getString("example"));
269: btnTest.setMnemonic(bundle.getString("testBtn_mnemonic")
270: .charAt(0));
271: btnTest.setText(bundle.getString("testText"));
272: btnTest.addActionListener(new java.awt.event.ActionListener() {
273: public void actionPerformed(java.awt.event.ActionEvent evt) {
274: btnTestActionPerformed(evt);
275: }
276: });
277:
278: gridBagConstraints = new java.awt.GridBagConstraints();
279: gridBagConstraints.gridx = 2;
280: gridBagConstraints.gridy = 2;
281: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
282: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
283: gridBagConstraints.insets = new java.awt.Insets(10, 0, 0, 10);
284: pnlExample.add(btnTest, gridBagConstraints);
285: btnTest.getAccessibleContext().setAccessibleDescription(
286: bundle.getString("testText"));
287:
288: txtExampleInstructions.setBackground(getBackground());
289: txtExampleInstructions.setBorder(null);
290: txtExampleInstructions.setEditable(false);
291: txtExampleInstructions.setText(bundle
292: .getString("exampleInstructionsText"));
293: gridBagConstraints = new java.awt.GridBagConstraints();
294: gridBagConstraints.gridx = 0;
295: gridBagConstraints.gridy = 1;
296: gridBagConstraints.gridwidth = 3;
297: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
298: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
299: gridBagConstraints.insets = new java.awt.Insets(10, 10, 0, 11);
300: pnlExample.add(txtExampleInstructions, gridBagConstraints);
301: txtExampleInstructions.getAccessibleContext()
302: .setAccessibleName(
303: bundle.getString("exampleInstructionsText"));
304: txtExampleInstructions.getAccessibleContext()
305: .setAccessibleDescription(
306: bundle.getString("exampleInstructionsText"));
307:
308: gridBagConstraints = new java.awt.GridBagConstraints();
309: gridBagConstraints.gridx = 0;
310: gridBagConstraints.gridy = 1;
311: gridBagConstraints.gridwidth = 3;
312: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
313: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
314: add(pnlExample, gridBagConstraints);
315:
316: gridBagConstraints = new java.awt.GridBagConstraints();
317: gridBagConstraints.gridx = 0;
318: gridBagConstraints.gridy = 2;
319: gridBagConstraints.gridwidth = 3;
320: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
321: gridBagConstraints.weightx = 1.0;
322: gridBagConstraints.weighty = 1.0;
323: add(jPanel1, gridBagConstraints);
324:
325: }// </editor-fold>//GEN-END:initComponents
326:
327: private void txtResultsActionPerformed(
328: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtResultsActionPerformed
329: // TODO add your handling code here:
330: }//GEN-LAST:event_txtResultsActionPerformed
331:
332: private void cmbExampleActionPerformed(
333: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbExampleActionPerformed
334: upateSampleResult();
335: }//GEN-LAST:event_cmbExampleActionPerformed
336:
337: private void btnTestActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTestActionPerformed
338: upateSampleResult();
339: }//GEN-LAST:event_btnTestActionPerformed
340:
341: private void cmbPatternActionPerformed(
342: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmbPatternActionPerformed
343:
344: if (upateSampleResult())
345: propertyEditor.setAsText(((String) cmbPattern
346: .getSelectedItem()));
347: }//GEN-LAST:event_cmbPatternActionPerformed
348:
349: // Variables declaration - do not modify//GEN-BEGIN:variables
350: private javax.swing.JButton btnTest;
351: private javax.swing.JComboBox cmbExample;
352: private javax.swing.JComboBox cmbPattern;
353: private javax.swing.JPanel jPanel1;
354: private javax.swing.JSeparator jSeparator1;
355: private javax.swing.JLabel lblExample;
356: private javax.swing.JLabel lblPattern;
357: private javax.swing.JLabel lblResults;
358: private javax.swing.JPanel pnlExample;
359: private javax.swing.JTextPane txtExampleInstructions;
360: private javax.swing.JTextField txtResults;
361: // End of variables declaration//GEN-END:variables
362:
363: }
|