1 | /* |
2 | * Copyright 2009, SDQ, IPD, Uni Karlsruhe (TH) |
3 | */ |
4 | package de.uka.ipd.sdq.pcm.gmf.resource.part; |
5 | |
6 | import org.eclipse.emf.ecore.EObject; |
7 | import org.eclipse.emf.ecore.util.FeatureMap; |
8 | import org.eclipse.emf.edit.provider.IWrapperItemProvider; |
9 | import org.eclipse.emf.edit.ui.provider.AdapterFactoryContentProvider; |
10 | import org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider; |
11 | import org.eclipse.jface.viewers.ISelectionChangedListener; |
12 | import org.eclipse.jface.viewers.IStructuredSelection; |
13 | import org.eclipse.jface.viewers.SelectionChangedEvent; |
14 | import org.eclipse.jface.viewers.StructuredSelection; |
15 | import org.eclipse.jface.viewers.TreeViewer; |
16 | import org.eclipse.jface.wizard.WizardPage; |
17 | import org.eclipse.swt.SWT; |
18 | import org.eclipse.swt.layout.GridData; |
19 | import org.eclipse.swt.layout.GridLayout; |
20 | import org.eclipse.swt.widgets.Composite; |
21 | import org.eclipse.swt.widgets.Label; |
22 | |
23 | /** |
24 | * Wizard page that allows to select element from model. |
25 | * @generated |
26 | */ |
27 | public class ModelElementSelectionPage extends WizardPage { |
28 | /** |
29 | * @generated |
30 | */ |
31 | protected EObject selectedModelElement; |
32 | |
33 | /** |
34 | * @generated |
35 | */ |
36 | private TreeViewer modelViewer; |
37 | |
38 | /** |
39 | * @generated |
40 | */ |
41 | public ModelElementSelectionPage(String pageName) { |
42 | super(pageName); |
43 | } |
44 | |
45 | /** |
46 | * @generated |
47 | */ |
48 | public EObject getModelElement() { |
49 | return selectedModelElement; |
50 | } |
51 | |
52 | /** |
53 | * @generated |
54 | */ |
55 | public void setModelElement(EObject modelElement) { |
56 | selectedModelElement = modelElement; |
57 | if (modelViewer != null) { |
58 | if (selectedModelElement != null) { |
59 | modelViewer.setInput(selectedModelElement.eResource()); |
60 | modelViewer.setSelection(new StructuredSelection( |
61 | selectedModelElement)); |
62 | } else { |
63 | modelViewer.setInput(null); |
64 | } |
65 | setPageComplete(validatePage()); |
66 | } |
67 | } |
68 | |
69 | /** |
70 | * @generated |
71 | */ |
72 | public void createControl(Composite parent) { |
73 | initializeDialogUnits(parent); |
74 | |
75 | Composite plate = new Composite(parent, SWT.NONE); |
76 | plate.setLayoutData(new GridData(GridData.FILL_BOTH)); |
77 | GridLayout layout = new GridLayout(); |
78 | layout.marginWidth = 0; |
79 | plate.setLayout(layout); |
80 | setControl(plate); |
81 | |
82 | Label label = new Label(plate, SWT.NONE); |
83 | label.setText(getSelectionTitle()); |
84 | label.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING)); |
85 | |
86 | modelViewer = new TreeViewer(plate, SWT.SINGLE | SWT.H_SCROLL |
87 | | SWT.V_SCROLL | SWT.BORDER); |
88 | GridData layoutData = new GridData(GridData.FILL_BOTH); |
89 | layoutData.heightHint = 300; |
90 | layoutData.widthHint = 300; |
91 | modelViewer.getTree().setLayoutData(layoutData); |
92 | modelViewer.setContentProvider(new AdapterFactoryContentProvider( |
93 | PalladioComponentModelDiagramEditorPlugin.getInstance() |
94 | .getItemProvidersAdapterFactory())); |
95 | modelViewer.setLabelProvider(new AdapterFactoryLabelProvider( |
96 | PalladioComponentModelDiagramEditorPlugin.getInstance() |
97 | .getItemProvidersAdapterFactory())); |
98 | if (selectedModelElement != null) { |
99 | modelViewer.setInput(selectedModelElement.eResource()); |
100 | modelViewer.setSelection(new StructuredSelection( |
101 | selectedModelElement)); |
102 | } |
103 | modelViewer |
104 | .addSelectionChangedListener(new ISelectionChangedListener() { |
105 | public void selectionChanged(SelectionChangedEvent event) { |
106 | ModelElementSelectionPage.this |
107 | .updateSelection((IStructuredSelection) event |
108 | .getSelection()); |
109 | } |
110 | }); |
111 | |
112 | setPageComplete(validatePage()); |
113 | } |
114 | |
115 | /** |
116 | * Override to provide custom model element description. |
117 | * @generated |
118 | */ |
119 | protected String getSelectionTitle() { |
120 | return Messages.ModelElementSelectionPageMessage; |
121 | } |
122 | |
123 | /** |
124 | * @generated |
125 | */ |
126 | protected void updateSelection(IStructuredSelection selection) { |
127 | selectedModelElement = null; |
128 | if (selection.size() == 1) { |
129 | Object selectedElement = selection.getFirstElement(); |
130 | if (selectedElement instanceof IWrapperItemProvider) { |
131 | selectedElement = ((IWrapperItemProvider) selectedElement) |
132 | .getValue(); |
133 | } |
134 | if (selectedElement instanceof FeatureMap.Entry) { |
135 | selectedElement = ((FeatureMap.Entry) selectedElement) |
136 | .getValue(); |
137 | } |
138 | if (selectedElement instanceof EObject) { |
139 | selectedModelElement = (EObject) selectedElement; |
140 | } |
141 | } |
142 | setPageComplete(validatePage()); |
143 | } |
144 | |
145 | /** |
146 | * Override to provide specific validation of the selected model element. |
147 | * @generated |
148 | */ |
149 | protected boolean validatePage() { |
150 | return true; |
151 | } |
152 | |
153 | } |