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