KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > ecore > sdo > action > EvaluatePathAction


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2003-2004 IBM Corporation and others.
5  * All rights reserved. This program and the accompanying materials
6  * are made available under the terms of the Eclipse Public License v1.0
7  * which accompanies this distribution, and is available at
8  * http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Contributors:
11  * IBM - Initial API and implementation
12  *
13  * </copyright>
14  *
15  * $Id: EvaluatePathAction.java,v 1.2 2005/06/08 06:21:18 nickb Exp $
16  */

17 package org.eclipse.emf.ecore.sdo.action;
18
19
20 import java.io.PrintWriter JavaDoc;
21 import java.io.StringWriter JavaDoc;
22 import java.lang.reflect.InvocationTargetException JavaDoc;
23 import java.lang.reflect.Method JavaDoc;
24 import java.math.BigDecimal JavaDoc;
25 import java.math.BigInteger JavaDoc;
26 import java.util.Date JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
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 /**
62  *
63  */

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 JavaDoc 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 JavaDoc [][] TYPE_DATA =
100       {
101         new Object JavaDoc [] { "BigDecimal", Boolean.FALSE, BigDecimal JavaDoc.class, EcorePackage.eINSTANCE.getEBigDecimal() },
102         new Object JavaDoc [] { "BigInteger", Boolean.FALSE, BigInteger JavaDoc.class, EcorePackage.eINSTANCE.getEBigInteger() },
103         new Object JavaDoc [] { "Boolean", Boolean.FALSE, Boolean.TYPE, EcorePackage.eINSTANCE.getEBoolean() },
104         new Object JavaDoc [] { "Byte", Boolean.FALSE, Byte.TYPE, EcorePackage.eINSTANCE.getEByte() },
105         new Object JavaDoc [] { "Bytes", Boolean.FALSE, byte[].class, EcorePackage.eINSTANCE.getEByteArray() },
106         new Object JavaDoc [] { "Char", Boolean.FALSE, Character.TYPE, EcorePackage.eINSTANCE.getEChar() },
107         new Object JavaDoc [] { "DataObject", Boolean.FALSE, DataObject.class, SDOPackage.eINSTANCE.getDataObject() },
108         new Object JavaDoc [] { "Date", Boolean.FALSE, Date JavaDoc.class, EcorePackage.eINSTANCE.getEDate() },
109         new Object JavaDoc [] { "Double", Boolean.FALSE, Double.TYPE, EcorePackage.eINSTANCE.getEDouble() },
110         new Object JavaDoc [] { "Float", Boolean.FALSE, Float.TYPE, EcorePackage.eINSTANCE.getEFloat() },
111         new Object JavaDoc [] { "Int", Boolean.FALSE, Integer.TYPE, EcorePackage.eINSTANCE.getEInt() },
112         new Object JavaDoc [] { "List", Boolean.FALSE, List JavaDoc.class, EcorePackage.eINSTANCE.getEJavaObject() },
113         new Object JavaDoc [] { "Long", Boolean.FALSE, Long.TYPE, EcorePackage.eINSTANCE.getELong() },
114         new Object JavaDoc [] { "Object", Boolean.TRUE, Object JavaDoc.class, EcorePackage.eINSTANCE.getEJavaObject() },
115         new Object JavaDoc [] { "Sequence", Boolean.FALSE, Sequence.class, SDOPackage.eINSTANCE.getSequence() },
116         new Object JavaDoc [] { "Short", Boolean.FALSE, Short.TYPE, EcorePackage.eINSTANCE.getEShort() },
117         new Object JavaDoc [] { "String", Boolean.FALSE, String JavaDoc.class, EcorePackage.eINSTANCE.getEString() }
118       };
119
120     protected Method JavaDoc [][] 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 JavaDoc [TYPE_DATA.length][];
137         for (int i = 0; i < TYPE_DATA.length; ++i)
138         {
139           methods[i] =
140             new Method JavaDoc []
141             {
142               EDataObject.class.getMethod
143                 ("get" + (TYPE_DATA[i][1] == Boolean.TRUE ? "" : TYPE_DATA[i][0]), new Class JavaDoc [] { String JavaDoc.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 JavaDoc [] { String JavaDoc.class, (Class JavaDoc)TYPE_DATA[i][2] }),
148             };
149          }
150        }
151        catch (NoSuchMethodException JavaDoc 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 JavaDoc [] { EcoreUtil.getURI(eDataObject).toString()}));
163     }
164
165     protected Control createDialogArea(Composite parent)
166     {
167       Composite composite = new Composite(parent, SWT.NONE);
168       // Composite composite = (Composite)super.createDialogArea(parent);
169

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 JavaDoc)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 JavaDoc result = null;
326         switch (operationChoice.getSelectionIndex())
327         {
328           case 0:
329           {
330             result =
331               methods[typeChoice.getSelectionIndex()][0].invoke
332                 (eDataObject, new Object JavaDoc [] { 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 JavaDoc [] { 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 JavaDoc)
378           {
379             StringBuffer JavaDoc text = new StringBuffer JavaDoc();
380             for (Iterator JavaDoc i = ((List JavaDoc)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 JavaDoc exception)
398       {
399         StringWriter JavaDoc writer = new StringWriter JavaDoc();
400         exception.getTargetException().printStackTrace(new PrintWriter JavaDoc(writer));
401         resultText.setText(writer.getBuffer().toString());
402       }
403       catch (Exception JavaDoc exception)
404       {
405         StringWriter JavaDoc writer = new StringWriter JavaDoc();
406         exception.printStackTrace(new PrintWriter JavaDoc(writer));
407         resultText.setText(writer.getBuffer().toString());
408       }
409     }
410   }
411 }
412
Popular Tags