KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > options > ScrollInsetsEditor


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.editor.options;
21
22 import java.awt.Insets JavaDoc;
23 import java.util.StringTokenizer JavaDoc;
24 import org.openide.explorer.propertysheet.ExPropertyEditor;
25 import org.openide.explorer.propertysheet.PropertyEnv;
26
27 import org.openide.util.NbBundle;
28
29 /**
30  * A property editor for Insets class allowing to specify per cent value
31  * represented as negative number.
32  *
33  * @author Petr Nejedly
34  * @author Petr Hamernik
35  */

36 public class ScrollInsetsEditor extends java.beans.PropertyEditorSupport JavaDoc implements ExPropertyEditor {
37
38     public boolean supportsCustomEditor () {
39         return true;
40     }
41
42     public java.awt.Component JavaDoc getCustomEditor () {
43         return new ScrollInsetsCustomEditor( this, env );
44     }
45
46     // public Object getValue() {
47
// if( editorPanel != null ) {
48
// try {
49
// return editorPanel.getValue();
50
// } catch( NumberFormatException e ) {
51
// return super.getValue();
52
// // PENDING
53
// }
54
// } else {
55
// return super.getValue();
56
// }
57
// }
58

59     /**
60      * @return The property value as a human editable string.
61      * <p> Returns null if the value can't be expressed as an editable string.
62      * <p> If a non-null value is returned, then the PropertyEditor should
63      * be prepared to parse that string back in setAsText().
64      */

65     public String JavaDoc getAsText() {
66         Insets JavaDoc val = (Insets JavaDoc) getValue();
67         if (val == null)
68             return null;
69         else {
70             return "[" + int2percent( val.top ) + ',' + int2percent( val.left ) + ',' + // NOI18N
71
int2percent( val.bottom ) + ',' + int2percent( val.right ) + ']';
72         }
73     }
74
75     /** Set the property value by parsing a given String. May raise
76     * java.lang.IllegalArgumentException if either the String is
77     * badly formatted or if this kind of property can't be expressed
78     * as text.
79     * @param text The string to be parsed.
80     */

81     public void setAsText(String JavaDoc text) throws IllegalArgumentException JavaDoc {
82         int[] newVal = new int[4];
83         int nextNumber = 0;
84
85         StringTokenizer JavaDoc tuk = new StringTokenizer JavaDoc( text, "[] ,;", false ); // NOI18N
86
while( tuk.hasMoreTokens() ) {
87             String JavaDoc token = tuk.nextToken();
88             if( nextNumber >= 4 ) badFormat();
89
90             try {
91                 newVal[nextNumber++] = percent2int( token );
92             } catch( NumberFormatException JavaDoc e ) {
93                 badFormat();
94             }
95         }
96
97         // if less numbers are entered, copy the last entered number into the rest
98
if( nextNumber != 4 ) {
99             if( nextNumber > 0 ) {
100                 int copyValue = newVal[ nextNumber - 1 ];
101                 for( int i = nextNumber; i < 4; i++ ) newVal[i] = copyValue;
102             }
103         }
104         setValue( new Insets JavaDoc( newVal[0], newVal[1], newVal[2], newVal[3] ) );
105     }
106
107     private String JavaDoc getBundleString(String JavaDoc s) {
108         return NbBundle.getMessage(ScrollInsetsEditor.class, s);
109     }
110     
111     /** Always throws the new exception */
112     private void badFormat() throws IllegalArgumentException JavaDoc {
113         throw new IllegalArgumentException JavaDoc( getBundleString("SIE_EXC_BadFormatValue" ) ); // NOI18N
114
}
115
116     private String JavaDoc int2percent( int i ) {
117         if( i < 0 ) return( "" + (-i) + '%' );
118         else return( "" + i );
119     }
120
121     private int percent2int( String JavaDoc val ) throws NumberFormatException JavaDoc {
122         val = val.trim();
123         if( val.endsWith( "%" ) ) { // NOI18N
124
return -Integer.parseInt( val.substring( 0, val.length() - 1 ) );
125         } else {
126             return Integer.parseInt( val );
127         }
128     }
129
130     private PropertyEnv env;
131     
132     public void attachEnv(PropertyEnv env) {
133         this.env = env;
134     }
135 }
136
Popular Tags