1 17 package org.eclipse.emf.ecore.sdo.action; 18 19 20 import java.io.PrintWriter ; 21 import java.io.StringWriter ; 22 import java.lang.reflect.InvocationTargetException ; 23 import java.lang.reflect.Method ; 24 import java.math.BigDecimal ; 25 import java.math.BigInteger ; 26 import java.util.Date ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 30 import org.eclipse.jface.action.IAction; 31 import org.eclipse.jface.dialogs.Dialog; 32 import org.eclipse.jface.dialogs.IDialogConstants; 33 import org.eclipse.jface.viewers.ISelection; 34 import org.eclipse.jface.viewers.IStructuredSelection; 35 import org.eclipse.swt.SWT; 36 import org.eclipse.swt.events.ModifyEvent; 37 import org.eclipse.swt.events.ModifyListener; 38 import org.eclipse.swt.layout.GridData; 39 import org.eclipse.swt.layout.GridLayout; 40 import org.eclipse.swt.widgets.Combo; 41 import org.eclipse.swt.widgets.Composite; 42 import org.eclipse.swt.widgets.Control; 43 import org.eclipse.swt.widgets.Label; 44 import org.eclipse.swt.widgets.Shell; 45 import org.eclipse.swt.widgets.Text; 46 import org.eclipse.ui.IActionDelegate; 47 import org.eclipse.ui.PlatformUI; 48 import org.eclipse.ui.actions.ActionDelegate; 49 50 import org.eclipse.emf.ecore.EDataType; 51 import org.eclipse.emf.ecore.EcorePackage; 52 import org.eclipse.emf.ecore.sdo.EDataObject; 53 import org.eclipse.emf.ecore.sdo.SDOPackage; 54 import org.eclipse.emf.ecore.sdo.presentation.SDOEditorPlugin; 55 import org.eclipse.emf.ecore.util.EcoreUtil; 56 57 import commonj.sdo.DataObject; 58 import commonj.sdo.Sequence; 59 60 61 64 public class EvaluatePathAction 65 extends ActionDelegate 66 implements IActionDelegate 67 { 68 protected EDataObject eDataObject; 69 70 public EvaluatePathAction() 71 { 72 } 73 74 public void run(IAction action) 75 { 76 EvaluationDialog dialog = new EvaluationDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), eDataObject); 77 dialog.open(); 78 } 79 80 public void selectionChanged(IAction action, ISelection selection) 81 { 82 if (selection instanceof IStructuredSelection) 83 { 84 Object object = ((IStructuredSelection)selection).getFirstElement(); 85 if (object instanceof EDataObject) 86 { 87 eDataObject = (EDataObject)object; 88 89 action.setEnabled(true); 90 return; 91 } 92 } 93 eDataObject = null; 94 action.setEnabled(false); 95 } 96 97 public static class EvaluationDialog extends Dialog 98 { 99 protected static final Object [][] TYPE_DATA = 100 { 101 new Object [] { "BigDecimal", Boolean.FALSE, BigDecimal .class, EcorePackage.eINSTANCE.getEBigDecimal() }, 102 new Object [] { "BigInteger", Boolean.FALSE, BigInteger .class, EcorePackage.eINSTANCE.getEBigInteger() }, 103 new Object [] { "Boolean", Boolean.FALSE, Boolean.TYPE, EcorePackage.eINSTANCE.getEBoolean() }, 104 new Object [] { "Byte", Boolean.FALSE, Byte.TYPE, EcorePackage.eINSTANCE.getEByte() }, 105 new Object [] { "Bytes", Boolean.FALSE, byte[].class, EcorePackage.eINSTANCE.getEByteArray() }, 106 new Object [] { "Char", Boolean.FALSE, Character.TYPE, EcorePackage.eINSTANCE.getEChar() }, 107 new Object [] { "DataObject", Boolean.FALSE, DataObject.class, SDOPackage.eINSTANCE.getDataObject() }, 108 new Object [] { "Date", Boolean.FALSE, Date .class, EcorePackage.eINSTANCE.getEDate() }, 109 new Object [] { "Double", Boolean.FALSE, Double.TYPE, EcorePackage.eINSTANCE.getEDouble() }, 110 new Object [] { "Float", Boolean.FALSE, Float.TYPE, EcorePackage.eINSTANCE.getEFloat() }, 111 new Object [] { "Int", Boolean.FALSE, Integer.TYPE, EcorePackage.eINSTANCE.getEInt() }, 112 new Object [] { "List", Boolean.FALSE, List .class, EcorePackage.eINSTANCE.getEJavaObject() }, 113 new Object [] { "Long", Boolean.FALSE, Long.TYPE, EcorePackage.eINSTANCE.getELong() }, 114 new Object [] { "Object", Boolean.TRUE, Object .class, EcorePackage.eINSTANCE.getEJavaObject() }, 115 new Object [] { "Sequence", Boolean.FALSE, Sequence.class, SDOPackage.eINSTANCE.getSequence() }, 116 new Object [] { "Short", Boolean.FALSE, Short.TYPE, EcorePackage.eINSTANCE.getEShort() }, 117 new Object [] { "String", Boolean.FALSE, String .class, EcorePackage.eINSTANCE.getEString() } 118 }; 119 120 protected Method [][] methods = null; 121 protected EDataObject eDataObject; 122 protected Text pathText; 123 protected Text resultText; 124 protected Combo valueText; 125 protected Combo operationChoice; 126 protected Combo typeChoice; 127 128 public EvaluationDialog(Shell shell, EDataObject eDataObject) 129 { 130 super(shell); 131 setShellStyle(SWT.RESIZE | SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL); 132 this.eDataObject = eDataObject; 133 134 try 135 { 136 methods = new Method [TYPE_DATA.length][]; 137 for (int i = 0; i < TYPE_DATA.length; ++i) 138 { 139 methods[i] = 140 new Method [] 141 { 142 EDataObject.class.getMethod 143 ("get" + (TYPE_DATA[i][1] == Boolean.TRUE ? "" : TYPE_DATA[i][0]), new Class [] { String .class }), 144 "Sequence".equals(TYPE_DATA[i][0]) ? 145 null : 146 EDataObject.class.getMethod 147 ("set" + (TYPE_DATA[i][1] == Boolean.TRUE ? "" : TYPE_DATA[i][0]), new Class [] { String .class, (Class )TYPE_DATA[i][2] }), 148 }; 149 } 150 } 151 catch (NoSuchMethodException exception) 152 { 153 exception.printStackTrace(); 154 } 155 } 156 157 protected void configureShell(Shell newShell) 158 { 159 super.configureShell(newShell); 160 newShell.setText 161 (SDOEditorPlugin.INSTANCE.getString 162 ("_UI_PathEvaluation_title", new Object [] { EcoreUtil.getURI(eDataObject).toString()})); 163 } 164 165 protected Control createDialogArea(Composite parent) 166 { 167 Composite composite = new Composite(parent, SWT.NONE); 168 170 { 171 GridLayout layout = new GridLayout(); 172 layout.numColumns = 1; 173 layout.verticalSpacing = 12; 174 composite.setLayout(layout); 175 176 GridData data = new GridData(); 177 data.verticalAlignment = GridData.FILL; 178 data.grabExcessVerticalSpace = true; 179 data.horizontalAlignment = GridData.FILL; 180 data.grabExcessHorizontalSpace = true; 181 composite.setLayoutData(data); 182 } 183 184 Label pathLabel = new Label(composite, SWT.LEFT); 185 { 186 pathLabel.setText(SDOEditorPlugin.INSTANCE.getString("_UI_Path_label")); 187 GridData data = new GridData(); 188 data.horizontalAlignment = GridData.FILL; 189 pathLabel.setLayoutData(data); 190 } 191 192 pathText = new Text(composite, SWT.SINGLE | SWT.BORDER); 193 { 194 GridData data = new GridData(); 195 data.horizontalAlignment = GridData.FILL; 196 data.grabExcessHorizontalSpace = true; 197 pathText.setLayoutData(data); 198 } 199 200 pathText.addModifyListener 201 (new ModifyListener() 202 { 203 public void modifyText(ModifyEvent event) 204 { 205 resultText.setText(""); 206 } 207 }); 208 209 Label operationLabel = new Label(composite, SWT.LEFT); 210 { 211 operationLabel.setText(SDOEditorPlugin.INSTANCE.getString("_UI_Operation_label")); 212 GridData data = new GridData(); 213 data.horizontalAlignment = GridData.FILL; 214 operationLabel.setLayoutData(data); 215 } 216 217 operationChoice = new Combo(composite, SWT.SINGLE | SWT.BORDER | SWT.READ_ONLY); 218 { 219 GridData data = new GridData(); 220 data.horizontalAlignment = GridData.FILL; 221 data.grabExcessHorizontalSpace = true; 222 operationChoice.setLayoutData(data); 223 } 224 operationChoice.add("get"); 225 operationChoice.add("set"); 226 operationChoice.add("isSet"); 227 operationChoice.add("unset"); 228 operationChoice.select(0); 229 operationChoice.addModifyListener 230 (new ModifyListener() 231 { 232 public void modifyText(ModifyEvent event) 233 { 234 resultText.setText(""); 235 typeChoice.setEnabled(operationChoice.getSelectionIndex() == 0 || operationChoice.getSelectionIndex() == 1); 236 valueText.setEnabled(operationChoice.getSelectionIndex() == 1); 237 } 238 }); 239 240 Label typeLabel = new Label(composite, SWT.LEFT); 241 { 242 typeLabel.setText(SDOEditorPlugin.INSTANCE.getString("_UI_Type_label")); 243 GridData data = new GridData(); 244 data.horizontalAlignment = GridData.FILL; 245 typeLabel.setLayoutData(data); 246 } 247 248 typeChoice = new Combo(composite, SWT.SINGLE | SWT.BORDER | SWT.READ_ONLY); 249 { 250 GridData data = new GridData(); 251 data.horizontalAlignment = GridData.FILL; 252 data.grabExcessHorizontalSpace = true; 253 typeChoice.setLayoutData(data); 254 } 255 for (int i = 0; i < TYPE_DATA.length; ++i) 256 { 257 typeChoice.add((String )TYPE_DATA[i][0]); 258 if (TYPE_DATA[i][1] == Boolean.TRUE) 259 { 260 typeChoice.select(i); 261 } 262 } 263 typeChoice.select(13); 264 265 Label valueLabel = new Label(composite, SWT.LEFT); 266 { 267 valueLabel.setText(SDOEditorPlugin.INSTANCE.getString("_UI_Value_label")); 268 GridData data = new GridData(); 269 data.horizontalAlignment = GridData.FILL; 270 valueLabel.setLayoutData(data); 271 } 272 273 valueText = new Combo(composite, SWT.SINGLE | SWT.BORDER); 274 valueText.setEnabled(false); 275 { 276 GridData data = new GridData(); 277 data.horizontalAlignment = GridData.FILL; 278 data.grabExcessHorizontalSpace = true; 279 valueText.setLayoutData(data); 280 } 281 282 Label resultLabel = new Label(composite, SWT.LEFT); 283 { 284 resultLabel.setText(SDOEditorPlugin.INSTANCE.getString("_UI_Result_label")); 285 GridData data = new GridData(); 286 data.horizontalAlignment = GridData.FILL; 287 resultLabel.setLayoutData(data); 288 } 289 290 resultText = new Text(composite, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL); 291 { 292 GridData data = new GridData(); 293 data.heightHint = 200; 294 data.horizontalAlignment = GridData.FILL; 295 data.grabExcessHorizontalSpace = true; 296 data.widthHint = 500; 297 data.verticalAlignment = GridData.FILL; 298 data.grabExcessVerticalSpace = true; 299 resultText.setLayoutData(data); 300 } 301 return composite; 302 } 303 304 protected void createButtonsForButtonBar(Composite parent) 305 { 306 createButton(parent, IDialogConstants.CLIENT_ID + 1, "Evaluate", true); 307 createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, false); 308 createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false); 309 } 310 311 protected void buttonPressed(int buttonID) 312 { 313 super.buttonPressed(buttonID); 314 if (buttonID == IDialogConstants.CLIENT_ID + 1) 315 { 316 pathText.setFocus(); 317 evaluate(); 318 } 319 } 320 321 protected void evaluate() 322 { 323 try 324 { 325 Object result = null; 326 switch (operationChoice.getSelectionIndex()) 327 { 328 case 0: 329 { 330 result = 331 methods[typeChoice.getSelectionIndex()][0].invoke 332 (eDataObject, new Object [] { pathText.getText() }); 333 break; 334 } 335 case 1: 336 { 337 if (TYPE_DATA[typeChoice.getSelectionIndex()][3] instanceof EDataType) 338 { 339 EDataType eDataType = (EDataType)TYPE_DATA[typeChoice.getSelectionIndex()][3]; 340 if (eDataType.isSerializable()) 341 { 342 result = EcoreUtil.createFromString(eDataType, valueText.getText()); 343 } 344 else 345 { 346 result = valueText.getText(); 347 } 348 } 349 350 methods[typeChoice.getSelectionIndex()][1].invoke 351 (eDataObject, new Object [] { pathText.getText(), result }); 352 break; 353 } 354 case 2: 355 { 356 result = eDataObject.isSet(pathText.getText()) ? Boolean. TRUE : Boolean.FALSE; 357 break; 358 } 359 case 3: 360 { 361 eDataObject.unset(pathText.getText()); 362 break; 363 } 364 } 365 366 if (result == null) 367 { 368 resultText.setText("null"); 369 } 370 else if (TYPE_DATA[typeChoice.getSelectionIndex()][3] instanceof EDataType) 371 { 372 EDataType eDataType = (EDataType)TYPE_DATA[typeChoice.getSelectionIndex()][3]; 373 if (eDataType.isSerializable()) 374 { 375 resultText.setText(EcoreUtil.convertToString(eDataType, result)); 376 } 377 else if (result instanceof List ) 378 { 379 StringBuffer text = new StringBuffer (); 380 for (Iterator i = ((List )result).iterator(); i.hasNext(); ) 381 { 382 text.append(i.next()); 383 text.append("\n"); 384 } 385 resultText.setText(text.toString()); 386 } 387 else 388 { 389 resultText.setText(result.toString()); 390 } 391 } 392 else 393 { 394 resultText.setText(result.toString()); 395 } 396 } 397 catch (InvocationTargetException exception) 398 { 399 StringWriter writer = new StringWriter (); 400 exception.getTargetException().printStackTrace(new PrintWriter (writer)); 401 resultText.setText(writer.getBuffer().toString()); 402 } 403 catch (Exception exception) 404 { 405 StringWriter writer = new StringWriter (); 406 exception.printStackTrace(new PrintWriter (writer)); 407 resultText.setText(writer.getBuffer().toString()); 408 } 409 } 410 } 411 } 412 | Popular Tags |