001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019:
020: package org.netbeans.modules.soa.mappercore.vertexitemeditor;
021:
022: import java.awt.Component;
023: import javax.swing.text.AbstractDocument;
024: import javax.swing.text.AttributeSet;
025: import javax.swing.text.BadLocationException;
026: import javax.swing.text.DocumentFilter;
027: import javax.swing.tree.TreePath;
028: import org.netbeans.modules.soa.mappercore.Mapper;
029: import org.netbeans.modules.soa.mappercore.model.VertexItem;
030: import org.netbeans.modules.soa.mappercore.utils.MetalTextFieldBorder;
031:
032: /**
033: *
034: * @author anjeleevich
035: */
036: public class NumberVertexItemEditor extends
037: AbstractTextVertexItemEditor {
038:
039: public NumberVertexItemEditor() {
040: MetalTextFieldBorder.installIfItIsNeeded(this );
041: setHorizontalAlignment(RIGHT);
042: ((AbstractDocument) getDocument())
043: .setDocumentFilter(new NumberDocumentFilter());
044: }
045:
046: public Component getVertexItemEditorComponent(Mapper mapper,
047: TreePath treePath, VertexItem vertexItem) {
048: Number number = toNumber(vertexItem.getValue());
049: setText((number == null) ? "" : number.toString());
050: return this ;
051: }
052:
053: public Object getVertexItemEditorValue() {
054: return toNumber(getText());
055: }
056:
057: private static class NumberDocumentFilter extends DocumentFilter {
058: public void replace(DocumentFilter.FilterBypass fb, int offset,
059: int length, String text, AttributeSet attrs)
060: throws BadLocationException {
061: text = filterString(text);
062: super .replace(fb, offset, length, text, attrs);
063: }
064:
065: public void insertString(DocumentFilter.FilterBypass fb,
066: int offset, String string, AttributeSet attr)
067: throws BadLocationException {
068: string = filterString(string);
069: super .insertString(fb, offset, string, attr);
070: }
071:
072: }
073:
074: private static String filterString(String string) {
075: StringBuilder builder = new StringBuilder();
076:
077: if (string != null) {
078: for (int i = 0; i < string.length(); i++) {
079: char c = string.charAt(i);
080: if (FILTER_CHARS.indexOf(c) >= 0) {
081: builder.append(c);
082: }
083: }
084: string = builder.toString();
085: }
086:
087: return string;
088: }
089:
090: private static Number toNumber(Object value) {
091: Number number = null;
092:
093: if (value instanceof Number) {
094: number = (Number) value;
095: } else if (value != null) {
096: String text = value.toString();
097:
098: if (text != null) {
099: try {
100: number = Long.parseLong(text);
101: } catch (NumberFormatException ex) {
102: }
103:
104: if (number == null) {
105: try {
106: number = Double.parseDouble(text);
107: } catch (NumberFormatException ex) {
108: }
109: }
110: }
111: }
112:
113: return number;
114: }
115:
116: private static final String FILTER_CHARS = "0123456789+-eE.";
117: }
|