1 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 32 public class JavaPrimitiveValueEditor implements IVariableValueEditor { 33 34 37 private String fSignature= null; 38 39 43 public JavaPrimitiveValueEditor(String signature) { 44 fSignature= signature; 45 } 46 47 50 public boolean editVariable(IVariable variable, Shell shell) { 51 try { 52 String name= variable.getName(); 53 String title= ActionMessages.JavaPrimitiveValueEditor_0; 54 String message= MessageFormat.format(ActionMessages.JavaPrimitiveValueEditor_1, new String [] {name}); 55 String 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 stringValue = dialog.getValue(); 68 if (stringValue.length() > 1 && stringValue.charAt(0) == '\\') { 69 int i= validator.getEscapeValue(stringValue); 71 if (i != Integer.MAX_VALUE) { 72 stringValue= new String (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); } 80 return true; 81 } 82 83 86 public boolean saveVariable(IVariable variable, String expression, Shell shell) { 87 return false; 88 } 89 90 93 protected class PrimitiveValidator implements IInputValidator { 94 97 public String isValid(String newText) { 98 String type= null; 99 switch (fSignature.charAt(0)) { 100 case 'B': 101 try { 102 Byte.parseByte(newText); 103 } catch (NumberFormatException e) { 104 type= "byte"; } 106 break; 107 case 'C': 108 if (newText.length() > 1 && newText.charAt(0) == '\\') { 109 if (isSpecialCharacter(newText) || 111 isOctalEscape(newText) || 112 isUnicode(newText)) { 113 break; 114 } 115 } 116 if (newText.length() != 1) { 117 type="char"; } 119 break; 120 case 'D': 121 try { 122 Double.parseDouble(newText); 123 } catch (NumberFormatException e) { 124 type="double"; } 126 break; 127 case 'F': 128 try { 129 Float.parseFloat(newText); 130 } catch (NumberFormatException e) { 131 type="float"; } 133 break; 134 case 'I': 135 try { 136 Integer.parseInt(newText); 137 } catch (NumberFormatException e) { 138 type="int"; } 140 break; 141 case 'J': 142 try { 143 Long.parseLong(newText); 144 } catch (NumberFormatException e) { 145 type="long"; } 147 break; 148 case 'S': 149 try { 150 Short.parseShort(newText); 151 } catch (NumberFormatException e) { 152 type="short"; } 154 break; 155 case 'Z': 156 if (!("true".equals(newText) || "false".equals(newText))) { type="boolean"; } 159 break; 160 } 161 if (type != null) { 162 return MessageFormat.format(ActionMessages.JavaPrimitiveValueEditor_4, new String [] { type }); 163 } 164 return null; 165 } 166 167 private boolean isUnicode(String 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 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 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 233 protected int getEscapeValue(String 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 |