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