KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > edit > ui > celleditor > FeatureEditorDialog


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2002-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: FeatureEditorDialog.java,v 1.7 2005/06/08 06:20:52 nickb Exp $
16  */

17 package org.eclipse.emf.edit.ui.celleditor;
18
19
20 import java.util.Collections JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23
24 import org.eclipse.jface.dialogs.Dialog;
25 import org.eclipse.jface.viewers.IContentProvider;
26 import org.eclipse.jface.viewers.ILabelProvider;
27 import org.eclipse.jface.viewers.IStructuredSelection;
28 import org.eclipse.jface.viewers.StructuredSelection;
29 import org.eclipse.jface.viewers.TableViewer;
30 import org.eclipse.swt.SWT;
31 import org.eclipse.swt.events.KeyAdapter;
32 import org.eclipse.swt.events.KeyEvent;
33 import org.eclipse.swt.events.SelectionAdapter;
34 import org.eclipse.swt.events.SelectionEvent;
35 import org.eclipse.swt.layout.GridData;
36 import org.eclipse.swt.layout.GridLayout;
37 import org.eclipse.swt.widgets.Button;
38 import org.eclipse.swt.widgets.Composite;
39 import org.eclipse.swt.widgets.Control;
40 import org.eclipse.swt.widgets.Display;
41 import org.eclipse.swt.widgets.Label;
42 import org.eclipse.swt.widgets.Shell;
43 import org.eclipse.swt.widgets.Table;
44 import org.eclipse.swt.widgets.Text;
45
46 import org.eclipse.emf.common.notify.AdapterFactory;
47 import org.eclipse.emf.common.notify.impl.AdapterFactoryImpl;
48 import org.eclipse.emf.common.util.BasicEList;
49 import org.eclipse.emf.common.util.EList;
50 import org.eclipse.emf.ecore.EClassifier;
51 import org.eclipse.emf.ecore.EDataType;
52 import org.eclipse.emf.ecore.EObject;
53 import org.eclipse.emf.ecore.EStructuralFeature;
54 import org.eclipse.emf.ecore.util.EcoreUtil;
55 import org.eclipse.emf.edit.provider.ComposedAdapterFactory;
56 import org.eclipse.emf.edit.provider.ItemProvider;
57 import org.eclipse.emf.edit.ui.EMFEditUIPlugin;
58 import org.eclipse.emf.edit.ui.provider.AdapterFactoryContentProvider;
59
60
61 public class FeatureEditorDialog extends Dialog
62 {
63   protected ILabelProvider labelProvider;
64   protected IContentProvider contentProvider;
65   protected Object JavaDoc object;
66   protected EClassifier eClassifier;
67   protected String JavaDoc displayName;
68   protected ItemProvider values;
69   protected List JavaDoc choiceOfValues;
70   protected EList result;
71
72   public FeatureEditorDialog
73     (Shell parent,
74      ILabelProvider labelProvider,
75      Object JavaDoc object,
76      EClassifier eClassifier,
77      List JavaDoc currentValues,
78      String JavaDoc displayName,
79      List JavaDoc choiceOfValues)
80   {
81     super(parent);
82     setShellStyle(getShellStyle() | SWT.RESIZE | SWT.MAX);
83     this.labelProvider = labelProvider;
84     this.object = object;
85     this.eClassifier = eClassifier;
86     this.displayName = displayName;
87     this.choiceOfValues = choiceOfValues;
88
89     AdapterFactory adapterFactory = new ComposedAdapterFactory(Collections.EMPTY_LIST);
90     values = new ItemProvider(adapterFactory, currentValues);
91     contentProvider = new AdapterFactoryContentProvider(adapterFactory);
92   }
93
94   public FeatureEditorDialog
95     (Shell parent,
96      ILabelProvider labelProvider,
97      EObject eObject,
98      EStructuralFeature eStructuralFeature,
99      String JavaDoc displayName,
100      List JavaDoc choiceOfValues)
101   {
102     this(parent,
103          labelProvider,
104          eObject,
105          eStructuralFeature.getEType(),
106          (List JavaDoc)eObject.eGet(eStructuralFeature),
107          displayName,
108          choiceOfValues);
109   }
110
111   protected void configureShell(Shell shell)
112   {
113     super.configureShell(shell);
114     shell.setText
115       (EMFEditUIPlugin.INSTANCE.getString
116          ("_UI_FeatureEditorDialog_title", new Object JavaDoc [] { displayName, labelProvider.getText(object) }));
117     shell.setImage(labelProvider.getImage(object));
118   }
119
120   protected Control createDialogArea(Composite parent)
121   {
122     Composite contents = (Composite)super.createDialogArea(parent);
123
124     GridLayout contentsGridLayout = (GridLayout)contents.getLayout();
125     contentsGridLayout.numColumns = 3;
126
127     GridData contentsGridData = (GridData)contents.getLayoutData();
128     contentsGridData.horizontalAlignment = SWT.FILL;
129     contentsGridData.verticalAlignment = SWT.FILL;
130
131     Composite choiceComposite = new Composite(contents, SWT.NONE);
132     {
133       GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
134       data.horizontalAlignment = SWT.END;
135       choiceComposite.setLayoutData(data);
136
137       GridLayout layout = new GridLayout();
138       data.horizontalAlignment = SWT.FILL;
139       layout.marginHeight = 0;
140       layout.marginWidth = 0;
141       layout.numColumns = 1;
142       choiceComposite.setLayout(layout);
143     }
144
145     Label choiceLabel = new Label(choiceComposite, SWT.NONE);
146     choiceLabel.setText
147       (choiceOfValues == null ?
148          EMFEditUIPlugin.INSTANCE.getString("_UI_Value_label") :
149          EMFEditUIPlugin.INSTANCE.getString("_UI_Choices_label"));
150     GridData choiceLabelGridData = new GridData();
151     choiceLabelGridData.verticalAlignment = SWT.FILL;
152     choiceLabelGridData.horizontalAlignment = SWT.FILL;
153     choiceLabel.setLayoutData(choiceLabelGridData);
154
155     final Table choiceTable = choiceOfValues == null ? null : new Table(choiceComposite, SWT.MULTI | SWT.BORDER);
156     if (choiceTable != null)
157     {
158       GridData choiceTableGridData = new GridData();
159       choiceTableGridData.widthHint = Display.getCurrent().getBounds().width / 5;
160       choiceTableGridData.heightHint = Display.getCurrent().getBounds().height / 3;
161       choiceTableGridData.verticalAlignment = SWT.FILL;
162       choiceTableGridData.horizontalAlignment = SWT.FILL;
163       choiceTableGridData.grabExcessHorizontalSpace= true;
164       choiceTableGridData.grabExcessVerticalSpace= true;
165       choiceTable.setLayoutData(choiceTableGridData);
166     }
167
168     final TableViewer choiceTableViewer = choiceOfValues == null ? null : new TableViewer(choiceTable);
169     if (choiceTableViewer != null)
170     {
171       choiceTableViewer.setContentProvider(new AdapterFactoryContentProvider(new AdapterFactoryImpl()));
172       choiceTableViewer.setLabelProvider(labelProvider);
173       choiceTableViewer.setInput(new ItemProvider(choiceOfValues));
174     }
175
176     final Text choiceText = choiceOfValues == null ? new Text(choiceComposite, SWT.MULTI | SWT.BORDER) : null;
177     if (choiceText != null)
178     {
179       GridData choiceTextGridData = new GridData();
180       choiceTextGridData.widthHint = Display.getCurrent().getBounds().width / 5;
181       choiceTextGridData.verticalAlignment = SWT.BEGINNING;
182       choiceTextGridData.horizontalAlignment = SWT.FILL;
183       choiceTextGridData.grabExcessHorizontalSpace= true;
184       choiceText.setLayoutData(choiceTextGridData);
185     }
186
187     Composite controlButtons = new Composite(contents, SWT.NONE);
188     GridData controlButtonsGridData = new GridData();
189     controlButtonsGridData.verticalAlignment = SWT.FILL;
190     controlButtonsGridData.horizontalAlignment = SWT.FILL;
191     controlButtons.setLayoutData(controlButtonsGridData);
192
193     GridLayout controlsButtonGridLayout = new GridLayout();
194     controlButtons.setLayout(controlsButtonGridLayout);
195
196     new Label(controlButtons, SWT.NONE);
197     
198     final Button addButton = new Button(controlButtons, SWT.PUSH);
199     addButton.setText(EMFEditUIPlugin.INSTANCE.getString("_UI_Add_label"));
200     GridData addButtonGridData = new GridData();
201     addButtonGridData.verticalAlignment = SWT.FILL;
202     addButtonGridData.horizontalAlignment = SWT.FILL;
203     addButton.setLayoutData(addButtonGridData);
204
205     final Button removeButton = new Button(controlButtons, SWT.PUSH);
206     removeButton.setText(EMFEditUIPlugin.INSTANCE.getString("_UI_Remove_label"));
207     GridData removeButtonGridData = new GridData();
208     removeButtonGridData.verticalAlignment = SWT.FILL;
209     removeButtonGridData.horizontalAlignment = SWT.FILL;
210     removeButton.setLayoutData(removeButtonGridData);
211     
212     Label spaceLabel = new Label(controlButtons, SWT.NONE);
213     GridData spaceLabelGridData = new GridData();
214     spaceLabelGridData.verticalSpan = 2;
215     spaceLabel.setLayoutData(spaceLabelGridData);
216     
217     final Button upButton = new Button(controlButtons, SWT.PUSH);
218     upButton.setText(EMFEditUIPlugin.INSTANCE.getString("_UI_Up_label"));
219     GridData upButtonGridData = new GridData();
220     upButtonGridData.verticalAlignment = SWT.FILL;
221     upButtonGridData.horizontalAlignment = SWT.FILL;
222     upButton.setLayoutData(upButtonGridData);
223
224     final Button downButton = new Button(controlButtons, SWT.PUSH);
225     downButton.setText(EMFEditUIPlugin.INSTANCE.getString("_UI_Down_label"));
226     GridData downButtonGridData = new GridData();
227     downButtonGridData.verticalAlignment = SWT.FILL;
228     downButtonGridData.horizontalAlignment = SWT.FILL;
229     downButton.setLayoutData(downButtonGridData);
230
231     Composite featureComposite = new Composite(contents, SWT.NONE);
232     {
233       GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
234       data.horizontalAlignment = SWT.END;
235       featureComposite.setLayoutData(data);
236
237       GridLayout layout = new GridLayout();
238       data.horizontalAlignment = SWT.FILL;
239       layout.marginHeight = 0;
240       layout.marginWidth = 0;
241       layout.numColumns = 1;
242       featureComposite.setLayout(layout);
243     }
244
245     Label featureLabel = new Label(featureComposite,SWT.NONE);
246     featureLabel.setText(EMFEditUIPlugin.INSTANCE.getString("_UI_Feature_label"));
247     GridData featureLabelGridData = new GridData();
248     featureLabelGridData.horizontalSpan = 2;
249     featureLabelGridData.horizontalAlignment = SWT.FILL;
250     featureLabelGridData.verticalAlignment = SWT.FILL;
251     featureLabel.setLayoutData(featureLabelGridData);
252     
253     final Table featureTable = new Table(featureComposite, SWT.MULTI | SWT.BORDER);
254     GridData featureTableGridData = new GridData();
255     featureTableGridData.widthHint = Display.getCurrent().getBounds().width / 5;
256     featureTableGridData.heightHint = Display.getCurrent().getBounds().height / 3;
257     featureTableGridData.verticalAlignment = SWT.FILL;
258     featureTableGridData.horizontalAlignment = SWT.FILL;
259     featureTableGridData.grabExcessHorizontalSpace= true;
260     featureTableGridData.grabExcessVerticalSpace= true;
261     featureTable.setLayoutData(featureTableGridData);
262
263     final TableViewer featureTableViewer = new TableViewer(featureTable);
264     featureTableViewer.setContentProvider(contentProvider);
265     featureTableViewer.setLabelProvider(labelProvider);
266     featureTableViewer.setInput(values);
267     if (!values.getChildren().isEmpty())
268     {
269       featureTableViewer.setSelection(new StructuredSelection(values.getChildren().get(0)));
270     }
271     
272     if (choiceText != null)
273     {
274       choiceText.addKeyListener(
275         new KeyAdapter()
276         {
277           public void keyPressed(KeyEvent event)
278           {
279             if (event.character == '\r' || event.character == '\n')
280             {
281               try
282               {
283                 Object JavaDoc value = EcoreUtil.createFromString((EDataType)eClassifier, choiceText.getText());
284                 values.getChildren().add(value);
285                 choiceText.setText("");
286                 featureTableViewer.setSelection(new StructuredSelection(value));
287                 event.doit = false;
288               }
289               catch (RuntimeException JavaDoc exception)
290               {
291               }
292             }
293             else if (event.character == '\33')
294             {
295               choiceText.setText("");
296               event.doit = false;
297             }
298           }
299         });
300     }
301         
302     upButton.addSelectionListener(
303       new SelectionAdapter()
304       {
305         public void widgetSelected(SelectionEvent event)
306         {
307           IStructuredSelection selection = (IStructuredSelection)featureTableViewer.getSelection();
308           int minIndex = 0;
309           for (Iterator JavaDoc i = selection.iterator(); i.hasNext();)
310           {
311             Object JavaDoc value = i.next();
312             int index = values.getChildren().indexOf(value);
313             values.getChildren().move(Math.max(index - 1, minIndex++), value);
314           }
315         }
316       });
317
318     downButton.addSelectionListener(
319       new SelectionAdapter()
320       {
321         public void widgetSelected(SelectionEvent event)
322         {
323           IStructuredSelection selection = (IStructuredSelection)featureTableViewer.getSelection();
324           int maxIndex = values.getChildren().size() - selection.size();
325           for (Iterator JavaDoc i = selection.iterator(); i.hasNext();)
326           {
327             Object JavaDoc value = i.next();
328             int index = values.getChildren().indexOf(value);
329             values.getChildren().move(Math.min(index + 1, maxIndex++), value);
330           }
331         }
332       });
333
334     addButton.addSelectionListener(
335       new SelectionAdapter()
336       {
337         public void widgetSelected(SelectionEvent event)
338         {
339           if (choiceTableViewer != null)
340           {
341             IStructuredSelection selection = (IStructuredSelection)choiceTableViewer.getSelection();
342             for (Iterator JavaDoc i = selection.iterator(); i.hasNext();)
343             {
344               Object JavaDoc value = i.next();
345               if (!values.getChildren().contains(value))
346               {
347                 values.getChildren().add(value);
348               }
349             }
350             featureTableViewer.setSelection(selection);
351           }
352           else
353           {
354             try
355             {
356               Object JavaDoc value = EcoreUtil.createFromString((EDataType)eClassifier, choiceText.getText());
357               values.getChildren().add(value);
358               choiceText.setText("");
359               featureTableViewer.setSelection(new StructuredSelection(value));
360             }
361             catch (RuntimeException JavaDoc exception)
362             {
363             }
364           }
365         }
366       });
367
368     removeButton.addSelectionListener(
369       new SelectionAdapter()
370       {
371         public void widgetSelected(SelectionEvent event)
372         {
373           IStructuredSelection selection = (IStructuredSelection)featureTableViewer.getSelection();
374           Object JavaDoc firstValue = null;
375           for (Iterator JavaDoc i = selection.iterator(); i.hasNext();)
376           {
377             Object JavaDoc value = i.next();
378             if (firstValue == null)
379             {
380               firstValue = value;
381             }
382             values.getChildren().remove(value);
383           }
384
385           if (!values.getChildren().isEmpty())
386           {
387             featureTableViewer.setSelection(new StructuredSelection(values.getChildren().get(0)));
388           }
389
390           if (choiceTableViewer != null)
391           {
392             choiceTableViewer.setSelection(selection);
393           }
394           else
395           {
396             if (firstValue != null)
397             {
398               String JavaDoc value = EcoreUtil.convertToString((EDataType)eClassifier, firstValue);
399               choiceText.setText(value);
400             }
401           }
402         }
403       });
404         
405     return contents;
406   }
407
408   protected void okPressed()
409   {
410     result = new BasicEList(values.getChildren());
411     super.okPressed();
412   }
413
414   public boolean close()
415   {
416     contentProvider.dispose();
417     return super.close();
418   }
419
420   public EList getResult()
421   {
422     return result;
423   }
424 }
425
Popular Tags