1 19 20 package org.netbeans.modules.editor.options; 21 22 import java.awt.Insets ; 23 import java.util.StringTokenizer ; 24 import org.openide.explorer.propertysheet.ExPropertyEditor; 25 import org.openide.explorer.propertysheet.PropertyEnv; 26 27 import org.openide.util.NbBundle; 28 29 36 public class ScrollInsetsEditor extends java.beans.PropertyEditorSupport implements ExPropertyEditor { 37 38 public boolean supportsCustomEditor () { 39 return true; 40 } 41 42 public java.awt.Component getCustomEditor () { 43 return new ScrollInsetsCustomEditor( this, env ); 44 } 45 46 59 65 public String getAsText() { 66 Insets val = (Insets ) getValue(); 67 if (val == null) 68 return null; 69 else { 70 return "[" + int2percent( val.top ) + ',' + int2percent( val.left ) + ',' + int2percent( val.bottom ) + ',' + int2percent( val.right ) + ']'; 72 } 73 } 74 75 81 public void setAsText(String text) throws IllegalArgumentException { 82 int[] newVal = new int[4]; 83 int nextNumber = 0; 84 85 StringTokenizer tuk = new StringTokenizer ( text, "[] ,;", false ); while( tuk.hasMoreTokens() ) { 87 String token = tuk.nextToken(); 88 if( nextNumber >= 4 ) badFormat(); 89 90 try { 91 newVal[nextNumber++] = percent2int( token ); 92 } catch( NumberFormatException e ) { 93 badFormat(); 94 } 95 } 96 97 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 ( newVal[0], newVal[1], newVal[2], newVal[3] ) ); 105 } 106 107 private String getBundleString(String s) { 108 return NbBundle.getMessage(ScrollInsetsEditor.class, s); 109 } 110 111 112 private void badFormat() throws IllegalArgumentException { 113 throw new IllegalArgumentException ( getBundleString("SIE_EXC_BadFormatValue" ) ); } 115 116 private String int2percent( int i ) { 117 if( i < 0 ) return( "" + (-i) + '%' ); 118 else return( "" + i ); 119 } 120 121 private int percent2int( String val ) throws NumberFormatException { 122 val = val.trim(); 123 if( val.endsWith( "%" ) ) { 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 |