KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > debug > ui > actions > JavaPrimitiveValueEditor


1 /*******************************************************************************
2  * Copyright (c) 2004, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.debug.ui.actions;
12
13 import org.eclipse.debug.core.DebugException;
14 import org.eclipse.debug.core.model.IVariable;
15 import org.eclipse.debug.internal.ui.DebugUIPlugin;
16 import org.eclipse.debug.ui.actions.IVariableValueEditor;
17 import org.eclipse.jdt.internal.debug.ui.IJavaDebugHelpContextIds;
18 import org.eclipse.jface.dialogs.IInputValidator;
19 import org.eclipse.jface.dialogs.InputDialog;
20 import org.eclipse.jface.window.Window;
21 import org.eclipse.swt.widgets.Composite;
22 import org.eclipse.swt.widgets.Control;
23 import org.eclipse.swt.widgets.Shell;
24 import org.eclipse.ui.IWorkbench;
25 import org.eclipse.ui.PlatformUI;
26
27 import com.ibm.icu.text.MessageFormat;
28
29 /**
30  * A variable value editor that prompts the user to set a primitive's value.
31  */

32 public class JavaPrimitiveValueEditor implements IVariableValueEditor {
33     
34     /**
35      * The signature of the edited variable.
36      */

37     private String JavaDoc fSignature= null;
38
39     /**
40      * Creates a new editor for a variable with the given signature
41      * @param signature the signature of the primitive to be edited
42      */

43     public JavaPrimitiveValueEditor(String JavaDoc signature) {
44         fSignature= signature;
45     }
46     
47     /* (non-Javadoc)
48      * @see org.eclipse.debug.ui.actions.IVariableValueEditor#editVariable(org.eclipse.debug.core.model.IVariable, org.eclipse.swt.widgets.Shell)
49      */

50     public boolean editVariable(IVariable variable, Shell shell) {
51         try {
52             String JavaDoc name= variable.getName();
53             String JavaDoc title= ActionMessages.JavaPrimitiveValueEditor_0;
54             String JavaDoc message= MessageFormat.format(ActionMessages.JavaPrimitiveValueEditor_1, new String JavaDoc[] {name});
55             String JavaDoc initialValue= variable.getValue().getValueString();
56             PrimitiveValidator validator= new PrimitiveValidator();
57             InputDialog dialog= new InputDialog(shell, title, message, initialValue, validator){
58                 protected Control createDialogArea(Composite parent) {
59                     IWorkbench workbench = PlatformUI.getWorkbench();
60                     workbench.getHelpSystem().setHelp(
61                             parent,
62                             IJavaDebugHelpContextIds.DEFAULT_INPUT_DIALOG);
63                     return super.createDialogArea(parent);
64                 }
65             };
66             if (dialog.open() == Window.OK) {
67                 String JavaDoc stringValue = dialog.getValue();
68                 if (stringValue.length() > 1 && stringValue.charAt(0) == '\\') {
69                     // Compute value of octal of hexadecimal escape sequence
70
int i= validator.getEscapeValue(stringValue);
71                     if (i != Integer.MAX_VALUE) {
72                         stringValue= new String JavaDoc(new char[] { (char) i });
73                     }
74                 }
75                 variable.setValue(stringValue);
76             }
77         } catch (DebugException e) {
78             DebugUIPlugin.errorDialog(shell, ActionMessages.JavaPrimitiveValueEditor_2, ActionMessages.JavaPrimitiveValueEditor_3, e); //
79
}
80         return true;
81     }
82
83     /* (non-Javadoc)
84      * @see org.eclipse.debug.ui.actions.IVariableValueEditor#saveVariable(org.eclipse.debug.core.model.IVariable, java.lang.String, org.eclipse.swt.widgets.Shell)
85      */

86     public boolean saveVariable(IVariable variable, String JavaDoc expression, Shell shell) {
87         return false;
88     }
89     
90     /**
91      * Input validator for primitive types
92      */

93     protected class PrimitiveValidator implements IInputValidator {
94         /* (non-Javadoc)
95          * @see org.eclipse.jface.dialogs.IInputValidator#isValid(java.lang.String)
96          */

97         public String JavaDoc isValid(String JavaDoc newText) {
98             String JavaDoc type= null;
99             switch (fSignature.charAt(0)) {
100                 case 'B':
101                     try {
102                         Byte.parseByte(newText);
103                     } catch (NumberFormatException JavaDoc e) {
104                         type= "byte"; //$NON-NLS-1$
105
}
106                     break;
107                 case 'C':
108                     if (newText.length() > 1 && newText.charAt(0) == '\\') {
109                         // Possibly an escaped character
110
if (isSpecialCharacter(newText) ||
111                                 isOctalEscape(newText) ||
112                                 isUnicode(newText)) {
113                             break;
114                         }
115                     }
116                     if (newText.length() != 1) {
117                         type="char"; //$NON-NLS-1$
118
}
119                     break;
120                 case 'D':
121                     try {
122                         Double.parseDouble(newText);
123                     } catch (NumberFormatException JavaDoc e) {
124                         type="double"; //$NON-NLS-1$
125
}
126                     break;
127                 case 'F':
128                     try {
129                         Float.parseFloat(newText);
130                     } catch (NumberFormatException JavaDoc e) {
131                         type="float"; //$NON-NLS-1$
132
}
133                     break;
134                 case 'I':
135                     try {
136                         Integer.parseInt(newText);
137                     } catch (NumberFormatException JavaDoc e) {
138                         type="int"; //$NON-NLS-1$
139
}
140                     break;
141                 case 'J':
142                     try {
143                         Long.parseLong(newText);
144                     } catch (NumberFormatException JavaDoc e) {
145                         type="long"; //$NON-NLS-1$
146
}
147                     break;
148                 case 'S':
149                     try {
150                         Short.parseShort(newText);
151                     } catch (NumberFormatException JavaDoc e) {
152                         type="short"; //$NON-NLS-1$
153
}
154                     break;
155                 case 'Z':
156                     if (!("true".equals(newText) || "false".equals(newText))) { //$NON-NLS-1$ //$NON-NLS-2$
157
type="boolean"; //$NON-NLS-1$
158
}
159                     break;
160             }
161             if (type != null) {
162                 return MessageFormat.format(ActionMessages.JavaPrimitiveValueEditor_4, new String JavaDoc[] { type });
163             }
164             return null;
165         }
166
167         private boolean isUnicode(String JavaDoc newText) {
168             if (newText.length() == 6) {
169                 if (newText.charAt(1) == 'u') {
170                     char[] chars = newText.toCharArray();
171                     for (int i = 2; i < chars.length; i++) {
172                         if (!isHexDigit(chars[i])) {
173                             return false;
174                         }
175                     }
176                     return true;
177                 }
178             }
179             return false;
180         }
181         
182         private boolean isOctalEscape(String JavaDoc newText) {
183             char[] chars= newText.toCharArray();
184             if (chars.length < 4) {
185                 for (int i = 1; i < chars.length; i++) {
186                     if (!isOctalDigit(chars[i])) {
187                         return false;
188                     }
189                 }
190                 return true;
191             } else if (chars.length == 4) {
192                 char ch= chars[1];
193                 if (ch < '0' || ch > '3') {
194                     return false;
195                 }
196                 for (int i = 2; i < chars.length; i++) {
197                     if (!isOctalDigit(chars[i])) {
198                         return false;
199                     }
200                 }
201                 return true;
202             }
203             return false;
204         }
205
206         private boolean isSpecialCharacter(String JavaDoc newText) {
207             char ch= newText.charAt(1);
208             return newText.length() == 2 &&
209                 (ch == 'b' ||
210                 ch == 't' ||
211                 ch == 'n' ||
212                 ch == 'f' ||
213                 ch == 'r' ||
214                 ch == '"' ||
215                 ch == '\'' ||
216                 ch == '\\');
217         }
218         
219         private boolean isOctalDigit(char ch) {
220             return Character.digit(ch, 8) != -1;
221         }
222         
223         private boolean isHexDigit(char ch) {
224             return Character.digit(ch, 16) != -1;
225         }
226         
227         /**
228          * Returns the integer value specified by the given string, which
229          * represents an octal or hexadecimal escape sequence. Returns
230          * Integer.MAX_VALUE if the given string is not a valid octal or
231          * hexadecimal escape sequence.
232          */

233         protected int getEscapeValue(String JavaDoc string) {
234             int i= Integer.MAX_VALUE;
235             if (isOctalEscape(string)) {
236                 i= Integer.parseInt(string.substring(1), 8);
237             } else if (isUnicode(string)) {
238                 i= Integer.parseInt(string.substring(2), 16);
239             }
240             return i;
241         }
242     }
243
244 }
245
Popular Tags