Skip to content

Package: TargetPlatformEcoreSelectionDialog$DetailsContentViewer

TargetPlatformEcoreSelectionDialog$DetailsContentViewer

nameinstructionbranchcomplexitylinemethod
TargetPlatformEcoreSelectionDialog.DetailsContentViewer(TargetPlatformEcoreSelectionDialog, Composite, int)
M: 47 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 10 C: 0
0%
M: 1 C: 0
0%
doRefresh(String, Image)
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
getControl()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getSelection()
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
handleLabelProviderChanged(LabelProviderChangedEvent)
M: 7 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
inputChanged(Object, Object)
M: 11 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
refresh()
M: 23 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
refresh(Object[])
M: 27 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
setSelection(ISelection, boolean)
M: 1 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
setVisible(boolean)
M: 17 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /**
2: * Copyright (c) 2009-2010 Thales Corporate Services S.A.S.
3: * This program and the accompanying materials
4: * are made available under the terms of the Eclipse Public License v2.0
5: * which accompanies this distribution, and is available at
6: * https://www.eclipse.org/legal/epl-v2.0
7: *
8: * SPDX-License-Identifier: EPL-2.0
9: *
10: * Contributors:
11: * Thales Corporate Services S.A.S - initial API and implementation
12: */
13: package org.eclipse.egf.core.ui.dialogs;
14:
15: import java.util.ArrayList;
16: import java.util.List;
17:
18: import org.eclipse.core.commands.AbstractHandler;
19: import org.eclipse.core.commands.ExecutionEvent;
20: import org.eclipse.core.commands.IHandler;
21: import org.eclipse.core.runtime.IStatus;
22: import org.eclipse.core.runtime.Status;
23: import org.eclipse.egf.core.EGFCorePlugin;
24: import org.eclipse.egf.core.epackage.IProxyEClass;
25: import org.eclipse.egf.core.epackage.IProxyEClassifier;
26: import org.eclipse.egf.core.epackage.IProxyEDataType;
27: import org.eclipse.egf.core.epackage.IProxyEObject;
28: import org.eclipse.egf.core.epackage.IProxyEPackage;
29: import org.eclipse.egf.core.epackage.IProxyERoot;
30: import org.eclipse.egf.core.ui.EGFCoreUIPlugin;
31: import org.eclipse.egf.core.ui.IEGFCoreUIImages;
32: import org.eclipse.egf.core.ui.l10n.CoreUIMessages;
33: import org.eclipse.emf.common.util.URI;
34: import org.eclipse.emf.common.util.UniqueEList;
35: import org.eclipse.emf.ecore.provider.EcoreEditPlugin;
36: import org.eclipse.emf.edit.ui.provider.ExtendedImageRegistry;
37: import org.eclipse.jface.action.Action;
38: import org.eclipse.jface.action.IAction;
39: import org.eclipse.jface.action.IMenuManager;
40: import org.eclipse.jface.action.MenuManager;
41: import org.eclipse.jface.dialogs.IDialogSettings;
42: import org.eclipse.jface.viewers.ContentViewer;
43: import org.eclipse.jface.viewers.DoubleClickEvent;
44: import org.eclipse.jface.viewers.IContentProvider;
45: import org.eclipse.jface.viewers.IDoubleClickListener;
46: import org.eclipse.jface.viewers.ILabelProvider;
47: import org.eclipse.jface.viewers.ILabelProviderListener;
48: import org.eclipse.jface.viewers.ISelection;
49: import org.eclipse.jface.viewers.ISelectionChangedListener;
50: import org.eclipse.jface.viewers.ITableLabelProvider;
51: import org.eclipse.jface.viewers.ITreeContentProvider;
52: import org.eclipse.jface.viewers.LabelProvider;
53: import org.eclipse.jface.viewers.LabelProviderChangedEvent;
54: import org.eclipse.jface.viewers.SelectionChangedEvent;
55: import org.eclipse.jface.viewers.StructuredSelection;
56: import org.eclipse.jface.viewers.TreeViewer;
57: import org.eclipse.jface.viewers.Viewer;
58: import org.eclipse.jface.viewers.ViewerComparator;
59: import org.eclipse.jface.viewers.ViewerFilter;
60: import org.eclipse.jface.window.Window;
61: import org.eclipse.osgi.util.NLS;
62: import org.eclipse.swt.SWT;
63: import org.eclipse.swt.custom.CLabel;
64: import org.eclipse.swt.custom.ViewForm;
65: import org.eclipse.swt.events.MouseAdapter;
66: import org.eclipse.swt.events.MouseEvent;
67: import org.eclipse.swt.events.SelectionAdapter;
68: import org.eclipse.swt.events.SelectionEvent;
69: import org.eclipse.swt.graphics.Image;
70: import org.eclipse.swt.graphics.Point;
71: import org.eclipse.swt.graphics.Rectangle;
72: import org.eclipse.swt.layout.GridData;
73: import org.eclipse.swt.layout.GridLayout;
74: import org.eclipse.swt.widgets.Button;
75: import org.eclipse.swt.widgets.Composite;
76: import org.eclipse.swt.widgets.Control;
77: import org.eclipse.swt.widgets.Event;
78: import org.eclipse.swt.widgets.Label;
79: import org.eclipse.swt.widgets.Listener;
80: import org.eclipse.swt.widgets.Menu;
81: import org.eclipse.swt.widgets.Shell;
82: import org.eclipse.swt.widgets.ToolBar;
83: import org.eclipse.swt.widgets.ToolItem;
84: import org.eclipse.swt.widgets.Tree;
85: import org.eclipse.ui.ActiveShellExpression;
86: import org.eclipse.ui.PlatformUI;
87: import org.eclipse.ui.dialogs.SelectionStatusDialog;
88: import org.eclipse.ui.handlers.IHandlerActivation;
89: import org.eclipse.ui.handlers.IHandlerService;
90:
91: /**
92: * @author Xavier Maysonnave
93: *
94: */
95: public class TargetPlatformEcoreSelectionDialog extends SelectionStatusDialog {
96:
97: private static final String DIALOG_SETTINGS = "org.eclipse.egf.core.ui.dialogs.TargetPlatformEcoreSelectionDialog"; //$NON-NLS-1$
98:
99: private static final String SHOW_STATUS_LINE = "ShowStatusLine"; //$NON-NLS-1$
100:
101: private ExtendedImageRegistry registry = ExtendedImageRegistry.INSTANCE;
102:
103: private class ToggleStatusLineAction extends Action {
104:
105: /**
106: * Creates a new instance of the class.
107: */
108: public ToggleStatusLineAction() {
109: super(CoreUIMessages.TargetPlatformEcoreSelectionDialog_toggleStatusAction, IAction.AS_CHECK_BOX);
110: }
111:
112: @Override
113: public void run() {
114: _details.setVisible(isChecked());
115: }
116:
117: }
118:
119: /**
120: * A content provider that does nothing.
121: */
122: private class NullContentProvider implements IContentProvider {
123:
124: /*
125: * (non-Javadoc)
126: *
127: * @see org.eclipse.jface.viewers.IContentProvider#dispose()
128: */
129: public void dispose() {
130: // Nothing to do
131: }
132:
133: /*
134: * (non-Javadoc)
135: *
136: * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer,
137: * java.lang.Object, java.lang.Object)
138: */
139: public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
140: // Nothing to do
141: }
142:
143: }
144:
145: protected class EcoreLabelProvider implements ITableLabelProvider, ILabelProvider {
146:
147: public EcoreLabelProvider() {
148: // Nothing to do
149: }
150:
151: public Image getColumnImage(Object element, int columnIndex) {
152: if (element instanceof IProxyEPackage) {
153: return registry.getImage(EcoreEditPlugin.INSTANCE.getImage("full/obj16/EPackage")); //$NON-NLS-1$
154: } else if (element instanceof IProxyEClass) {
155: return registry.getImage(EcoreEditPlugin.INSTANCE.getImage("full/obj16/EClass")); //$NON-NLS-1$
156: } else if (element instanceof IProxyEDataType) {
157: return registry.getImage(EcoreEditPlugin.INSTANCE.getImage("full/obj16/EDataType")); //$NON-NLS-1$
158: }
159: return null;
160: }
161:
162: public String getColumnText(Object element, int columnIndex) {
163: if (element instanceof IProxyEObject) {
164: return ((IProxyEObject) element).getName();
165: }
166: return ""; //$NON-NLS-1$
167: }
168:
169: public void addListener(ILabelProviderListener listener) {
170: // Nothing to do
171: }
172:
173: public void dispose() {
174: // Nothing to do
175: }
176:
177: public boolean isLabelProperty(Object element, String property) {
178: return false;
179: }
180:
181: public void removeListener(ILabelProviderListener listener) {
182: // Nothing to do
183: }
184:
185: public Image getImage(Object element) {
186: return getColumnImage(element, 0);
187: }
188:
189: public String getText(Object element) {
190: return getColumnText(element, 0);
191: }
192:
193: }
194:
195: protected class EcoreContentProvider implements ITreeContentProvider {
196:
197: public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
198: // Nothing to do
199: }
200:
201: public void dispose() {
202: // Nothing to do
203: }
204:
205: public Object[] getElements(Object inputElement) {
206: return getChildren(inputElement);
207: }
208:
209: public boolean hasChildren(Object element) {
210: return getChildren(element).length > 0;
211: }
212:
213: public Object getParent(Object element) {
214: if (element instanceof IProxyEObject) {
215: return ((IProxyEObject) element).getParent();
216: }
217: return null;
218: }
219:
220: public Object[] getChildren(Object parentElement) {
221: if (parentElement instanceof IProxyERoot) {
222: IProxyERoot wrapper = (IProxyERoot) parentElement;
223: return wrapper.getChildren();
224: } else if (parentElement instanceof IProxyEPackage) {
225: IProxyEPackage wrapper = (IProxyEPackage) parentElement;
226: return wrapper.getChildren();
227: } else if (parentElement instanceof List<?>) {
228: return ((List<?>) parentElement).toArray();
229: }
230: return new Object[0];
231: }
232:
233: }
234:
235: /**
236: * DetailsContentViewer objects are wrappers for labels.
237: * DetailsContentViewer provides means to change label's image and text when
238: * the attached LabelProvider is updated.
239: */
240: private class DetailsContentViewer extends ContentViewer {
241:
242: private CLabel label;
243:
244: /**
245: * Unfortunately, it was impossible to delegate displaying border to
246: * label. The <code>ViewForm</code> is used because
247: * <code>CLabel</code> displays shadow when border is present.
248: */
249: private ViewForm viewForm;
250:
251: /**
252: * Constructs a new instance of this class given its parent and a style
253: * value describing its behavior and appearance.
254: *
255: * @param parent
256: * the parent component
257: * @param style
258: * SWT style bits
259: */
260: public DetailsContentViewer(Composite parent, int style) {
261: viewForm = new ViewForm(parent, style);
262: GridData gd = new GridData(GridData.FILL_HORIZONTAL);
263: gd.horizontalSpan = 2;
264: viewForm.setLayoutData(gd);
265: label = new CLabel(viewForm, SWT.FLAT);
266: label.setFont(parent.getFont());
267: viewForm.setContent(label);
268: hookControl(label);
269: }
270:
271: /**
272: * Shows/hides the content viewer.
273: *
274: * @param visible
275: * if the content viewer should be visible.
276: */
277: public void setVisible(boolean visible) {
278: GridData gd = (GridData) viewForm.getLayoutData();
279:• gd.exclude = !visible;
280: viewForm.getParent().layout();
281: }
282:
283: /*
284: * (non-Javadoc)
285: *
286: * @see org.eclipse.jface.viewers.Viewer#inputChanged(java.lang.Object,
287: * java.lang.Object)
288: */
289: @Override
290: protected void inputChanged(Object input, Object oldInput) {
291:• if (oldInput == null) {
292:• if (input == null) {
293: return;
294: }
295: refresh();
296: return;
297: }
298: refresh();
299: }
300:
301: /*
302: * (non-Javadoc)
303: *
304: * @see org.eclipse.jface.viewers.ContentViewer#handleLabelProviderChanged(org.eclipse.jface.viewers.LabelProviderChangedEvent)
305: */
306: @Override
307: protected void handleLabelProviderChanged(LabelProviderChangedEvent event) {
308:• if (event != null) {
309: refresh(event.getElements());
310: }
311: }
312:
313: /*
314: * (non-Javadoc)
315: *
316: * @see org.eclipse.jface.viewers.Viewer#getControl()
317: */
318: @Override
319: public Control getControl() {
320: return label;
321: }
322:
323: /*
324: * (non-Javadoc)
325: *
326: * @see org.eclipse.jface.viewers.Viewer#getSelection()
327: */
328: @Override
329: public ISelection getSelection() {
330: // not supported
331: return null;
332: }
333:
334: /*
335: * (non-Javadoc)
336: *
337: * @see org.eclipse.jface.viewers.Viewer#refresh()
338: */
339: @Override
340: public void refresh() {
341: Object input = this.getInput();
342:• if (input != null) {
343: ILabelProvider labelProvider = (ILabelProvider) getLabelProvider();
344: doRefresh(labelProvider.getText(input), labelProvider.getImage(input));
345: } else {
346: doRefresh(null, null);
347: }
348: }
349:
350: /**
351: * Sets the given text and image to the label.
352: *
353: * @param text
354: * the new text or null
355: * @param image
356: * the new image
357: */
358: private void doRefresh(String text, Image image) {
359: label.setText(text);
360: label.setImage(image);
361: }
362:
363: /*
364: * (non-Javadoc)
365: *
366: * @see org.eclipse.jface.viewers.Viewer#setSelection(org.eclipse.jface.viewers.ISelection,
367: * boolean)
368: */
369: @Override
370: public void setSelection(ISelection selection, boolean reveal) {
371: // not supported
372: }
373:
374: /**
375: * Refreshes the label if currently chosen element is on the list.
376: *
377: * @param objs
378: * list of changed object
379: */
380: private void refresh(Object[] objs) {
381:• if (objs == null || getInput() == null) {
382: return;
383: }
384: Object input = getInput();
385:• for (int i = 0; i < objs.length; i++) {
386:• if (objs[i].equals(input)) {
387: refresh();
388: break;
389: }
390: }
391: }
392:
393: }
394:
395: private class EcoreDetailsLabelProvider extends LabelProvider {
396:
397: public EcoreDetailsLabelProvider() {
398: // Nothing to do
399: }
400:
401: @Override
402: public Image getImage(Object element) {
403: if (element instanceof IProxyEPackage) {
404: return registry.getImage(EcoreEditPlugin.INSTANCE.getImage("full/obj16/EPackage")); //$NON-NLS-1$
405: } else if (element instanceof IProxyEClass) {
406: return registry.getImage(EcoreEditPlugin.INSTANCE.getImage("full/obj16/EClass")); //$NON-NLS-1$
407: } else if (element instanceof IProxyEDataType) {
408: return registry.getImage(EcoreEditPlugin.INSTANCE.getImage("full/obj16/EDataType")); //$NON-NLS-1$
409: }
410: return null;
411: }
412:
413: @Override
414: public String getText(Object element) {
415: if (element instanceof IProxyEObject) {
416: return ((IProxyEObject) element).getURI().toString();
417: }
418: return ""; //$NON-NLS-1$
419: }
420:
421: }
422:
423: private static final String EMPTY_STRING = ""; //$NON-NLS-1$
424:
425: private MenuManager _menuManager;
426:
427: private ToolBar _toolBar;
428:
429: private ToolItem _toolItem;
430:
431: private boolean _multi;
432:
433: protected TreeViewer _ecoreTypeTreeViewer;
434:
435: private IStatus _status;
436:
437: private Object[] _currentSelection;
438:
439: private DetailsContentViewer _details;
440:
441: private ToggleStatusLineAction _toggleStatusLineAction;
442:
443: private IHandlerActivation _showViewHandler;
444:
445: /**
446: * It is a duplicate of a field in the CLabel class in DetailsContentViewer.
447: * It is maintained, because the <code>setDetailsLabelProvider()</code>
448: * could be called before content area is created.
449: */
450: private ILabelProvider _detailsLabelProvider;
451:
452: private List<ISelectionDialogListener> _selectionListeners = new UniqueEList<ISelectionDialogListener>();
453:
454: /**
455: * Creates a new instance of the class.
456: *
457: * @param shell
458: * shell to parent the dialog on
459: * @param multi
460: * indicates whether dialog allows to select more than one
461: * position in its list of items
462: */
463: public TargetPlatformEcoreSelectionDialog(Shell shell, boolean multi) {
464: super(shell);
465: _multi = multi;
466: }
467:
468: /**
469: * Creates a new instance of the class. Created dialog won't allow to select
470: * more than one item.
471: *
472: * @param shell
473: * shell to parent the dialog on
474: */
475: public TargetPlatformEcoreSelectionDialog(Shell shell) {
476: this(shell, false);
477: }
478:
479: /**
480: * Restores dialog using persisted settings. The default implementation
481: * restores the status of the details line and the selection history.
482: *
483: * @param settings
484: * settings used to restore dialog
485: */
486: protected void restoreDialog(IDialogSettings settings) {
487: boolean toggleStatusLine = true;
488: if (settings.get(SHOW_STATUS_LINE) != null) {
489: toggleStatusLine = settings.getBoolean(SHOW_STATUS_LINE);
490: }
491: _toggleStatusLineAction.setChecked(toggleStatusLine);
492: _details.setVisible(toggleStatusLine);
493: }
494:
495: /**
496: * Stores dialog settings.
497: *
498: * @param settings
499: * settings used to store dialog
500: */
501: protected void storeDialog(IDialogSettings settings) {
502: settings.put(SHOW_STATUS_LINE, _toggleStatusLineAction.isChecked());
503: }
504:
505: protected IDialogSettings getDialogSettings() {
506: IDialogSettings settings = EGFCoreUIPlugin.getDefault().getDialogSettings().getSection(DIALOG_SETTINGS);
507: if (settings == null) {
508: settings = EGFCoreUIPlugin.getDefault().getDialogSettings().addNewSection(DIALOG_SETTINGS);
509: }
510: return settings;
511: }
512:
513: /*
514: * (non-Javadoc)
515: *
516: * @see org.eclipse.jface.window.Window#close()
517: */
518: @Override
519: public boolean close() {
520: if (_showViewHandler != null) {
521: IHandlerService service = (IHandlerService) PlatformUI.getWorkbench().getService(IHandlerService.class);
522: service.deactivateHandler(_showViewHandler);
523: _showViewHandler.getHandler().dispose();
524: _showViewHandler = null;
525: }
526: storeDialog(getDialogSettings());
527: return super.close();
528: }
529:
530: /*
531: * (non-Javadoc)
532: *
533: * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
534: */
535: @Override
536: protected Control createDialogArea(Composite parent) {
537:
538: Composite container = (Composite) super.createDialogArea(parent);
539:
540: Composite content = new Composite(container, SWT.NONE);
541: content.setLayoutData(new GridData(GridData.FILL_BOTH));
542:
543: GridLayout layout = new GridLayout();
544: layout.marginWidth = 5;
545: layout.marginWidth = 0;
546: layout.marginHeight = 0;
547: content.setLayout(layout);
548:
549: createHeader(content);
550:
551: Composite viewer = new Composite(content, SWT.NONE);
552: viewer.setLayoutData(new GridData(GridData.FILL_BOTH));
553:
554: layout = new GridLayout();
555: layout.marginWidth = 5;
556: layout.marginWidth = 0;
557: layout.marginHeight = 0;
558: viewer.setLayout(layout);
559:
560: Tree tree = new Tree(viewer, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER | (_multi ? SWT.MULTI : SWT.SINGLE));
561: GridData gd = new GridData(GridData.FILL_BOTH);
562: gd.heightHint = tree.getItemHeight() * 11;
563: tree.setLayoutData(gd);
564:
565: _ecoreTypeTreeViewer = new TreeViewer(tree);
566: _ecoreTypeTreeViewer.setContentProvider(new EcoreContentProvider());
567: _ecoreTypeTreeViewer.setLabelProvider(new EcoreLabelProvider());
568: _ecoreTypeTreeViewer.setComparator(new ViewerComparator() {
569:
570: @Override
571: public int category(Object element) {
572: if (element instanceof IProxyEObject) {
573: if (element instanceof IProxyEClassifier) {
574: return 2;
575: } else if (element instanceof IProxyEPackage) {
576: return 1;
577: }
578: }
579: return 10;
580: }
581:
582: });
583:
584: _ecoreTypeTreeViewer.addFilter(new ViewerFilter() {
585:
586: @Override
587: public boolean select(Viewer innerViewer, Object parentElement, Object element) {
588: // TODO at present time, we don't support DataType as type for
589: // PatternParameter
590: return element instanceof IProxyERoot || element instanceof IProxyEClass || element instanceof IProxyEPackage;
591: }
592:
593: });
594:
595: _ecoreTypeTreeViewer.addSelectionChangedListener(new ISelectionChangedListener() {
596:
597: public void selectionChanged(SelectionChangedEvent event) {
598: StructuredSelection selection = (StructuredSelection) event.getSelection();
599: handleSelected(selection);
600: }
601:
602: });
603:
604: _ecoreTypeTreeViewer.addDoubleClickListener(new IDoubleClickListener() {
605:
606: public void doubleClick(DoubleClickEvent event) {
607: handleDoubleClick();
608: }
609:
610: });
611:
612: Button button = new Button(viewer, SWT.PUSH);
613: button.setText(CoreUIMessages.TargetPlatformEcoreSelectionDialog_choose_model_button_title);
614: button.addListener(SWT.Selection, new Listener() {
615:
616: public void handleEvent(Event event) {
617: TargetPlatformEcoreDialog chooseModelDialog = new TargetPlatformEcoreDialog(getShell(), true, false, _multi, false, false);
618: if (chooseModelDialog.open() == Window.OK) {
619: searchTypeModel(chooseModelDialog.getURIText());
620: }
621: }
622:
623: });
624:
625: _details = new DetailsContentViewer(content, SWT.BORDER | SWT.FLAT);
626: _details.setVisible(_toggleStatusLineAction.isChecked());
627: _details.setContentProvider(new NullContentProvider());
628: _details.setLabelProvider(getDetailsLabelProvider());
629:
630: applyDialogFont(content);
631:
632: restoreDialog(getDialogSettings());
633:
634: return container;
635:
636: }
637:
638: /**
639: * Create a new header which is labelled by headerLabel.
640: *
641: * @param parent
642: * @return Label the label of the header
643: */
644: private Label createHeader(Composite parent) {
645: Composite header = new Composite(parent, SWT.NONE);
646:
647: GridLayout layout = new GridLayout();
648: layout.numColumns = 2;
649: layout.marginWidth = 0;
650: layout.marginHeight = 0;
651: header.setLayout(layout);
652:
653: Label headerLabel = new Label(header, SWT.NONE);
654: headerLabel.setText((getMessage() != null && getMessage().trim().length() > 0) ? getMessage() : CoreUIMessages.TargetPlatformEcoreSelectionDialog_select);
655: GridData gd = new GridData(GridData.FILL_HORIZONTAL);
656: headerLabel.setLayoutData(gd);
657:
658: createViewMenu(header);
659: header.setLayoutData(gd);
660: return headerLabel;
661: }
662:
663: private void createViewMenu(Composite parent) {
664:
665: _toolBar = new ToolBar(parent, SWT.FLAT);
666: _toolItem = new ToolItem(_toolBar, SWT.PUSH, 0);
667:
668: GridData data = new GridData();
669: data.horizontalAlignment = GridData.END;
670: _toolBar.setLayoutData(data);
671:
672: _toolBar.addMouseListener(new MouseAdapter() {
673:
674: @Override
675: public void mouseDown(MouseEvent e) {
676: showViewMenu();
677: }
678:
679: });
680:
681: _toolItem.setImage(EGFCoreUIPlugin.getDefault().getImage(IEGFCoreUIImages.IMG_VIEW_MENU));
682: _toolItem.setToolTipText(CoreUIMessages.TargetPlatformEcoreSelectionDialog_menu);
683: _toolItem.addSelectionListener(new SelectionAdapter() {
684:
685: @Override
686: public void widgetSelected(SelectionEvent e) {
687: showViewMenu();
688: }
689:
690: });
691:
692: _menuManager = new MenuManager();
693:
694: fillViewMenu(_menuManager);
695:
696: IHandlerService service = (IHandlerService) PlatformUI.getWorkbench().getService(IHandlerService.class);
697: IHandler handler = new AbstractHandler() {
698:
699: public Object execute(ExecutionEvent event) {
700: showViewMenu();
701: return null;
702: }
703:
704: };
705:
706: _showViewHandler = service.activateHandler("org.eclipse.egf.core.ui.dialogs.EcoreSelectionDialog", handler, new ActiveShellExpression(getShell())); //$NON-NLS-1$
707:
708: }
709:
710: /**
711: * Fills the menu of the dialog.
712: *
713: * @param menuManager
714: * the menu manager
715: */
716: protected void fillViewMenu(IMenuManager menuManager) {
717: _toggleStatusLineAction = new ToggleStatusLineAction();
718: menuManager.add(_toggleStatusLineAction);
719: }
720:
721: private void showViewMenu() {
722: Menu menu = _menuManager.createContextMenu(getShell());
723: Rectangle bounds = _toolItem.getBounds();
724: Point topLeft = new Point(bounds.x, bounds.y + bounds.height);
725: topLeft = _toolBar.toDisplay(topLeft);
726: menu.setLocation(topLeft.x, topLeft.y);
727: menu.setVisible(true);
728: }
729:
730: /**
731: * Sets label provider for the details field.
732: *
733: * For a single selection, the element sent to {@link ILabelProvider#getImage(Object)} and {@link ILabelProvider#getText(Object)} is the selected object, for
734: * multiple selection a {@link String} with amount of selected items is the
735: * element.
736: *
737: * @see #getSelectedItems() getSelectedItems() can be used to retrieve
738: * selected items and get the items count.
739: *
740: * @param detailsLabelProvider
741: * the label provider for the details field
742: */
743: public void setDetailsLabelProvider(ILabelProvider detailsLabelProvider) {
744: this._detailsLabelProvider = detailsLabelProvider;
745: if (_details != null) {
746: _details.setLabelProvider(detailsLabelProvider);
747: }
748: }
749:
750: private ILabelProvider getDetailsLabelProvider() {
751: if (_detailsLabelProvider == null) {
752: _detailsLabelProvider = new EcoreDetailsLabelProvider();
753: }
754: return _detailsLabelProvider;
755: }
756:
757: /*
758: * (non-Javadoc)
759: *
760: * @see org.eclipse.ui.dialogs.SelectionStatusDialog#computeResult()
761: */
762: @Override
763: @SuppressWarnings("unchecked")
764: protected void computeResult() {
765: List<Object> selectedElements = ((StructuredSelection) _ecoreTypeTreeViewer.getSelection()).toList();
766: List<Object> objectsToReturn = new ArrayList<Object>();
767: for (Object object : selectedElements) {
768: URI uri = null;
769: if (object instanceof IProxyEObject) {
770: uri = ((IProxyEObject) object).getURI();
771: }
772: if (uri != null && uri.isEmpty() == false && "#//".equals(uri.toString()) == false) { //$NON-NLS-1$
773: objectsToReturn.add(uri.toString());
774: }
775: }
776: setResult(objectsToReturn);
777: }
778:
779: protected void searchTypeModel(String buffer) {
780: if (buffer == null || buffer.trim().length() == 0) {
781: return;
782: }
783: String[] textUris = buffer.split(" "); //$NON-NLS-1$
784: IProxyERoot root = null;
785: for (String textUri : textUris) {
786: try {
787: if (textUri == null || textUri.trim().length() == 0) {
788: continue;
789: }
790: URI uri = URI.createURI(textUri.trim());
791: root = EGFCorePlugin.getTargetPlatformIProxyERoot(uri);
792: if (root != null) {
793: break;
794: }
795: } catch (Throwable t) {
796: EGFCoreUIPlugin.getDefault().logError(NLS.bind(CoreUIMessages.ModelSelection_errorMessage, textUri));
797: }
798: }
799: if (root != null) {
800: _ecoreTypeTreeViewer.setInput(root);
801: _ecoreTypeTreeViewer.expandToLevel(2);
802: }
803: }
804:
805: /**
806: * Handle selection in the items list by updating labels of selected and
807: * unselected items and refresh the details field using the selection.
808: *
809: * @param selection
810: * the new selection
811: */
812: protected void handleSelected(StructuredSelection selection) {
813: IStatus status = new Status(IStatus.OK, EGFCoreUIPlugin.getDefault().getPluginID(), IStatus.OK, EMPTY_STRING, null);
814: Object[] lastSelection = _currentSelection;
815: _currentSelection = selection.toArray();
816: if (selection.size() == 0) {
817: status = new Status(IStatus.ERROR, EGFCoreUIPlugin.getDefault().getPluginID(), IStatus.ERROR, EMPTY_STRING, null);
818: if (lastSelection != null) {
819: _ecoreTypeTreeViewer.update(lastSelection, null);
820: }
821: _currentSelection = null;
822: } else {
823: status = new Status(IStatus.ERROR, EGFCoreUIPlugin.getDefault().getPluginID(), IStatus.ERROR, EMPTY_STRING, null);
824: List<?> items = selection.toList();
825: IStatus tempStatus = null;
826: for (Object item : items) {
827: tempStatus = validateItem(item);
828: if (tempStatus.isOK()) {
829: status = new Status(IStatus.OK, PlatformUI.PLUGIN_ID, IStatus.OK, EMPTY_STRING, null);
830: } else {
831: status = tempStatus;
832: // if any selected element is not valid status is set to
833: // ERROR
834: break;
835: }
836: }
837: notifySelectionListeners(items.toArray());
838: if (lastSelection != null) {
839: _ecoreTypeTreeViewer.update(lastSelection, null);
840: }
841: }
842: refreshDetails();
843: updateStatus(status);
844: }
845:
846: /*
847: * @see org.eclipse.ui.dialogs.SelectionStatusDialog#updateStatus(org.eclipse.core.runtime.IStatus)
848: */
849: @Override
850: protected void updateStatus(IStatus status) {
851: _status = status;
852: super.updateStatus(status);
853: }
854:
855: /*
856: * @see Dialog#okPressed()
857: */
858: @Override
859: protected void okPressed() {
860: if (_status != null && (_status.isOK() || _status.getCode() == IStatus.INFO)) {
861: super.okPressed();
862: }
863: }
864:
865: /**
866: * Hack to use a dialog in a wizard page
867: *
868: * @return the current shell or its parent shell
869: */
870: @Override
871: public Shell getShell() {
872: return super.getShell() != null ? super.getShell() : getParentShell();
873: }
874:
875: /**
876: * This method is a hook for subclasses to override default dialog behavior.
877: * The <code>handleDoubleClick()</code> method handles double clicks on
878: * the list of filtered elements.
879: * <p>
880: * Current implementation makes double-clicking on the list do the same as
881: * pressing <code>OK</code> button on the dialog.
882: */
883: protected void handleDoubleClick() {
884: okPressed();
885: }
886:
887: protected IStatus validateItem(Object item) {
888: return new Status(IStatus.OK, EGFCoreUIPlugin.getDefault().getPluginID(), 0, "", null); //$NON-NLS-1$
889: }
890:
891: /**
892: * Returns the current selection.
893: *
894: * @return the current selection
895: */
896: protected StructuredSelection getSelectedItems() {
897: return new StructuredSelection(((StructuredSelection) _ecoreTypeTreeViewer.getSelection()).toList());
898: }
899:
900: /**
901: * Refreshes the details field according to the current selection in the
902: * items list.
903: */
904: private void refreshDetails() {
905: StructuredSelection selection = getSelectedItems();
906: switch (selection.size()) {
907: case 0:
908: _details.setInput(null);
909: break;
910: case 1:
911: _details.setInput(selection.getFirstElement());
912: break;
913: default:
914: _details.setInput(NLS.bind(CoreUIMessages.TargetPlatformEcoreSelectionDialog_nItemsSelected, new Integer(selection.size())));
915: break;
916: }
917: }
918:
919: /**
920: * Hack to use a dialog in a wizard page
921: *
922: * @return Control
923: *
924: */
925: public Control createPage(Composite parent) {
926: return dialogArea = createDialogArea(parent);
927: }
928:
929: public boolean addSelectionListeners(ISelectionDialogListener listener) {
930: if (listener == null) {
931: return false;
932: }
933: return _selectionListeners.add(listener);
934: }
935:
936: public boolean removeSelectionListeners(ISelectionDialogListener listener) {
937: if (listener == null) {
938: return false;
939: }
940: return _selectionListeners.remove(listener);
941: }
942:
943: public void notifySelectionListeners(Object[] selected) {
944: for (ISelectionDialogListener listener : _selectionListeners) {
945: listener.handleSelected(selected);
946: }
947: }
948:
949: }