1 | /** |
2 | * |
3 | */ |
4 | package de.uka.ipd.sdq.cip.runtime.runconfig.tabs; |
5 | |
6 | import java.util.ArrayList; |
7 | import java.util.Iterator; |
8 | |
9 | import org.eclipse.core.resources.IResource; |
10 | import org.eclipse.core.resources.ResourcesPlugin; |
11 | import org.eclipse.core.runtime.CoreException; |
12 | import org.eclipse.core.runtime.Path; |
13 | import org.eclipse.debug.core.ILaunchConfiguration; |
14 | import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy; |
15 | import org.eclipse.emf.common.util.URI; |
16 | import org.eclipse.jface.dialogs.IDialogConstants; |
17 | import org.eclipse.jface.viewers.ArrayContentProvider; |
18 | import org.eclipse.jface.viewers.CheckStateChangedEvent; |
19 | import org.eclipse.jface.viewers.CheckboxTableViewer; |
20 | import org.eclipse.jface.viewers.ICheckStateListener; |
21 | import org.eclipse.jface.viewers.ICheckStateProvider; |
22 | import org.eclipse.jface.viewers.ISelectionChangedListener; |
23 | import org.eclipse.jface.viewers.IStructuredSelection; |
24 | import org.eclipse.jface.viewers.ITableLabelProvider; |
25 | import org.eclipse.jface.viewers.LabelProvider; |
26 | import org.eclipse.jface.viewers.SelectionChangedEvent; |
27 | import org.eclipse.jface.viewers.TableViewer; |
28 | import org.eclipse.jface.viewers.TableViewerColumn; |
29 | import org.eclipse.jface.wizard.WizardDialog; |
30 | import org.eclipse.swt.SWT; |
31 | import org.eclipse.swt.events.SelectionAdapter; |
32 | import org.eclipse.swt.events.SelectionEvent; |
33 | import org.eclipse.swt.graphics.Image; |
34 | import org.eclipse.swt.layout.GridData; |
35 | import org.eclipse.swt.layout.GridLayout; |
36 | import org.eclipse.swt.widgets.Button; |
37 | import org.eclipse.swt.widgets.Composite; |
38 | import org.eclipse.swt.widgets.Group; |
39 | import org.eclipse.swt.widgets.Label; |
40 | import org.eclipse.swt.widgets.Shell; |
41 | import org.eclipse.swt.widgets.Table; |
42 | import org.eclipse.ui.dialogs.ElementTreeSelectionDialog; |
43 | import org.eclipse.ui.model.WorkbenchContentProvider; |
44 | import org.eclipse.ui.model.WorkbenchLabelProvider; |
45 | |
46 | import de.uka.ipd.sdq.cip.ConstantsContainer; |
47 | import de.uka.ipd.sdq.cip.configuration.Transformation; |
48 | import de.uka.ipd.sdq.cip.configuration.TransformationType; |
49 | import de.uka.ipd.sdq.cip.runtime.RunConfigImages; |
50 | import de.uka.ipd.sdq.cip.runtime.dialogs.EditCompletionDialog; |
51 | import de.uka.ipd.sdq.cip.runtime.wizards.NewCompletionWizard; |
52 | |
53 | /** |
54 | * @author thomass |
55 | * |
56 | */ |
57 | public class CipCompletionTab extends AbstractCipLaunchConfigurationTab { |
58 | |
59 | ArrayList<Transformation> transformations; |
60 | |
61 | private CheckboxTableViewer completionsViewer; |
62 | |
63 | private Button newbutton; |
64 | |
65 | private Button removebutton; |
66 | |
67 | private Button editbutton; |
68 | |
69 | private Button upbutton; |
70 | |
71 | private Button downbutton; |
72 | |
73 | private Button checkLoggingButton; |
74 | |
75 | /* (non-Javadoc) |
76 | * @see org.eclipse.debug.ui.ILaunchConfigurationTab#createControl(org.eclipse.swt.widgets.Composite) |
77 | */ |
78 | @Override |
79 | public void createControl(Composite parent) { |
80 | |
81 | Composite mainComponent = new Composite(parent,SWT.NONE); |
82 | setControl(mainComponent); |
83 | |
84 | GridLayout layout = new GridLayout(); |
85 | layout.numColumns = 2; |
86 | layout.marginWidth = 5; |
87 | layout.marginHeight = 5; |
88 | |
89 | mainComponent.setFont(parent.getFont()); |
90 | mainComponent.setLayout(layout); |
91 | |
92 | Label label = new Label(mainComponent,SWT.LEFT); |
93 | label.setText("Transformations:"); |
94 | |
95 | GridData gd = new GridData(); |
96 | gd.horizontalSpan = 2; |
97 | label.setLayoutData(gd); |
98 | |
99 | completionsViewer = createTable(mainComponent,new String[]{"Active", "Name","Category", "Configuration File"}); |
100 | //createColumns(completionsViewer); |
101 | |
102 | newbutton = createPushButton(mainComponent, "N&ew...", null); |
103 | newbutton.addSelectionListener(new SelectionAdapter() { |
104 | @Override |
105 | public void widgetSelected(SelectionEvent e) { |
106 | newCompletion(); |
107 | } |
108 | }); |
109 | editbutton = createPushButton(mainComponent, "E&dit...", null); |
110 | editbutton.addSelectionListener(new SelectionAdapter() { |
111 | @Override |
112 | public void widgetSelected(SelectionEvent e) { |
113 | editCompletion(); |
114 | } |
115 | }); |
116 | removebutton = createPushButton(mainComponent, "Rem&ove", null); |
117 | removebutton.addSelectionListener(new SelectionAdapter() { |
118 | @Override |
119 | public void widgetSelected(SelectionEvent e) { |
120 | removeCompletion(); |
121 | } |
122 | }); |
123 | //createSeparator(mainComponent, 1); |
124 | upbutton = createPushButton(mainComponent, "&Up", null); |
125 | upbutton.addSelectionListener(new SelectionAdapter() { |
126 | @Override |
127 | public void widgetSelected(SelectionEvent e) { |
128 | upCompletion(); |
129 | } |
130 | }); |
131 | downbutton = createPushButton(mainComponent, "&Down", null); |
132 | downbutton.addSelectionListener(new SelectionAdapter() { |
133 | @Override |
134 | public void widgetSelected(SelectionEvent e) { |
135 | downCompletion(); |
136 | } |
137 | }); |
138 | |
139 | /** Logging group*/ |
140 | |
141 | final Group loggingGroup = new Group(mainComponent, SWT.NONE); |
142 | gd = new GridData(SWT.FILL, SWT.CENTER, false, false); |
143 | gd.horizontalSpan = 2; |
144 | loggingGroup.setLayoutData(gd); |
145 | loggingGroup.setText("QVT Logging"); |
146 | loggingGroup.setLayout(new GridLayout()); |
147 | checkLoggingButton = new Button(loggingGroup, SWT.CHECK); |
148 | checkLoggingButton.setText("Enable verbose logging"); |
149 | checkLoggingButton.addSelectionListener(new SelectionAdapter() { |
150 | |
151 | /* (non-Javadoc) |
152 | * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent) |
153 | */ |
154 | public void widgetSelected(SelectionEvent e) { |
155 | CipCompletionTab.this.setDirty(true); |
156 | CipCompletionTab.this.updateLaunchConfigurationDialog(); |
157 | } |
158 | }); |
159 | |
160 | } |
161 | |
162 | protected void setTabModified() { |
163 | this.setDirty(true); |
164 | this.updateLaunchConfigurationDialog(); |
165 | updateButtons(); |
166 | |
167 | } |
168 | |
169 | protected void updateButtons() { |
170 | IStructuredSelection selection = (IStructuredSelection) completionsViewer.getSelection(); |
171 | if(!selection.iterator().hasNext()) |
172 | { |
173 | downbutton.setEnabled(false); |
174 | upbutton.setEnabled(false); |
175 | editbutton.setEnabled(false); |
176 | return; |
177 | } |
178 | |
179 | Transformation transformation = (Transformation) selection.iterator().next(); |
180 | int pos = transformations.indexOf(transformation); |
181 | if(pos < transformations.size()-1) |
182 | downbutton.setEnabled(true); |
183 | else |
184 | downbutton.setEnabled(false); |
185 | if(pos > 0) |
186 | upbutton.setEnabled(true); |
187 | else |
188 | upbutton.setEnabled(false); |
189 | |
190 | if(!(transformation.getType() == TransformationType.REGISTERED && transformation.getCompletion().containsCategory(ConstantsContainer.PLAIN_COMPLETION))) |
191 | editbutton.setEnabled(true); |
192 | else |
193 | editbutton.setEnabled(false); |
194 | } |
195 | |
196 | protected void downCompletion() { |
197 | IStructuredSelection selection = (IStructuredSelection) completionsViewer.getSelection(); |
198 | if(!selection.iterator().hasNext()) |
199 | return; |
200 | |
201 | Transformation transformation = (Transformation) selection.iterator().next(); |
202 | int pos = transformations.indexOf(transformation); |
203 | if(pos < transformations.size()) { |
204 | transformations.set(pos, transformations.get(pos+1)); |
205 | transformations.set(pos+1, transformation); |
206 | completionsViewer.refresh(); |
207 | } |
208 | |
209 | } |
210 | |
211 | protected void upCompletion() { |
212 | IStructuredSelection selection = (IStructuredSelection) completionsViewer.getSelection(); |
213 | if(!selection.iterator().hasNext()) |
214 | return; |
215 | |
216 | Transformation transformation = (Transformation) selection.iterator().next(); |
217 | int pos = transformations.indexOf(transformation); |
218 | if(pos > 0) { |
219 | transformations.set(pos, transformations.get(pos-1)); |
220 | transformations.set(pos-1, transformation); |
221 | completionsViewer.refresh(); |
222 | } |
223 | } |
224 | |
225 | protected void editCompletion() { |
226 | IStructuredSelection selection = (IStructuredSelection) completionsViewer.getSelection(); |
227 | if(!selection.iterator().hasNext()) |
228 | return; |
229 | |
230 | Transformation transformation = (Transformation) selection.iterator().next(); |
231 | |
232 | EditCompletionDialog dialog = new EditCompletionDialog(getShell(),transformation); |
233 | if(dialog.open() == EditCompletionDialog.OK) { |
234 | completionsViewer.refresh(); |
235 | } |
236 | |
237 | } |
238 | |
239 | @SuppressWarnings("unchecked") |
240 | protected void removeCompletion() { |
241 | IStructuredSelection selection = (IStructuredSelection) completionsViewer.getSelection(); |
242 | completionsViewer.getControl().setRedraw(false); |
243 | for(Iterator i = selection.iterator();i.hasNext();) { |
244 | Transformation transformation = (Transformation) i.next(); |
245 | transformations.remove(transformation); |
246 | completionsViewer.refresh(); |
247 | } |
248 | setTabModified(); |
249 | completionsViewer.getControl().setRedraw(true); |
250 | |
251 | } |
252 | |
253 | protected void newCompletion() { |
254 | NewCompletionWizard wizard = new NewCompletionWizard(); |
255 | WizardDialog dialog = new WizardDialog(getShell(), wizard); |
256 | dialog.setPageSize(500,500); |
257 | if(dialog.open() == WizardDialog.OK) { |
258 | transformations.add(wizard.getTransformation()); |
259 | completionsViewer.refresh(); |
260 | setTabModified(); |
261 | } |
262 | } |
263 | |
264 | private CheckboxTableViewer createTable(Composite mainComponent, String[] viewerColumnTitles) { |
265 | final CheckboxTableViewer tableviewer; |
266 | |
267 | tableviewer = CheckboxTableViewer.newCheckList(mainComponent, SWT.BORDER | SWT.SINGLE | SWT.FULL_SELECTION); |
268 | createColumns(tableviewer, viewerColumnTitles); |
269 | tableviewer.setContentProvider(new ArrayContentProvider()); |
270 | tableviewer.setLabelProvider(new TransformationsLabelProvider()); |
271 | tableviewer.setCheckStateProvider(new TransformationsCheckBoxProvider()); |
272 | transformations = new ArrayList<Transformation>(); |
273 | |
274 | tableviewer.setInput(transformations); |
275 | GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true); |
276 | gd.verticalSpan = 6; |
277 | tableviewer.getTable().setLayoutData(gd); |
278 | |
279 | tableviewer.addCheckStateListener(new ICheckStateListener() { |
280 | @Override |
281 | public void checkStateChanged(CheckStateChangedEvent event) { |
282 | Transformation entry = (Transformation)event.getElement(); |
283 | entry.setEnabled(event.getChecked()); |
284 | updateLaunchConfigurationDialog(); |
285 | } |
286 | }); |
287 | |
288 | tableviewer.addSelectionChangedListener(new ISelectionChangedListener() { |
289 | @Override |
290 | public void selectionChanged(SelectionChangedEvent event) { |
291 | updateButtons(); |
292 | } |
293 | }); |
294 | |
295 | return tableviewer; |
296 | } |
297 | |
298 | private void createColumns(TableViewer viewer, String[] viewerColumnTitles ) { |
299 | int[] columnWidths = { 50, 200, 200, 200}; |
300 | |
301 | for(int i = 0; i < viewerColumnTitles.length; ++i) { |
302 | TableViewerColumn column = new TableViewerColumn(viewer, SWT.NONE); |
303 | column.getColumn().setText(viewerColumnTitles[i]); |
304 | column.getColumn().setWidth(columnWidths[i]); |
305 | } |
306 | |
307 | Table table = viewer.getTable(); |
308 | table.setHeaderVisible(true); |
309 | table.setLinesVisible(true); |
310 | } |
311 | |
312 | /* (non-Javadoc) |
313 | * @see org.eclipse.debug.ui.ILaunchConfigurationTab#getName() |
314 | */ |
315 | @Override |
316 | public String getName() { |
317 | return "CIP Completions"; |
318 | } |
319 | |
320 | @Override |
321 | public Image getImage() { |
322 | return RunConfigImages.getCompletionTabImage(); |
323 | } |
324 | |
325 | /* (non-Javadoc) |
326 | * @see org.eclipse.debug.ui.ILaunchConfigurationTab#initializeFrom(org.eclipse.debug.core.ILaunchConfiguration) |
327 | */ |
328 | @SuppressWarnings("unchecked") |
329 | @Override |
330 | public void initializeFrom(ILaunchConfiguration configuration) { |
331 | ArrayList<String> config = new ArrayList<String>(); |
332 | transformations.clear(); |
333 | try { |
334 | config = (ArrayList<String>) configuration.getAttribute(ConstantsContainer.COMPLETION_CONFIG,new ArrayList<String>()); |
335 | } catch (CoreException e) { |
336 | e.printStackTrace(); |
337 | } |
338 | for (String transformationconfig : config) { |
339 | Transformation transformation = new Transformation(); |
340 | transformation.readFromDataString(transformationconfig); |
341 | transformations.add(transformation); |
342 | } |
343 | try { |
344 | checkLoggingButton.setSelection(configuration.getAttribute( |
345 | ConstantsContainer.COMPLETION_QVT_VERBOSE_LOGGING, false)); |
346 | } catch (CoreException e) { |
347 | checkLoggingButton.setSelection(false); |
348 | } |
349 | completionsViewer.refresh(); |
350 | updateButtons(); |
351 | } |
352 | |
353 | /* (non-Javadoc) |
354 | * @see org.eclipse.debug.ui.ILaunchConfigurationTab#performApply(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy) |
355 | */ |
356 | @Override |
357 | public void performApply(ILaunchConfigurationWorkingCopy configuration) { |
358 | ArrayList<String> config = new ArrayList<String>(); |
359 | for (Transformation transformation : transformations) { |
360 | config.add(transformation.toDataString()); |
361 | } |
362 | configuration.setAttribute(ConstantsContainer.COMPLETION_CONFIG, config); |
363 | configuration.setAttribute(ConstantsContainer.COMPLETION_QVT_VERBOSE_LOGGING, |
364 | checkLoggingButton.getSelection()); |
365 | } |
366 | |
367 | /* (non-Javadoc) |
368 | * @see org.eclipse.debug.ui.ILaunchConfigurationTab#setDefaults(org.eclipse.debug.core.ILaunchConfigurationWorkingCopy) |
369 | */ |
370 | @Override |
371 | public void setDefaults(ILaunchConfigurationWorkingCopy configuration) { |
372 | } |
373 | |
374 | private class TransformationsLabelProvider extends LabelProvider implements ITableLabelProvider { |
375 | |
376 | @Override |
377 | public Image getColumnImage(Object element, int columnIndex) { |
378 | return null; |
379 | } |
380 | |
381 | @Override |
382 | public String getColumnText(Object element, int columnIndex) { |
383 | Transformation transformation = (Transformation) element; |
384 | |
385 | switch (columnIndex) { |
386 | case 1: |
387 | if(transformation.getType() == TransformationType.REGISTERED) |
388 | return transformation.getCompletion().getName(); |
389 | else if(transformation.getQVTFileURI() != null) |
390 | return new Path(transformation.getQVTFileURI()).lastSegment(); |
391 | else |
392 | return new Path(transformation.getFeatureFileURI()).lastSegment(); |
393 | case 2: |
394 | ArrayList<String> categories = new ArrayList<String>(); |
395 | |
396 | if(transformation.getType() == TransformationType.REGISTERED) { |
397 | categories.add("Registered"); |
398 | categories.addAll(transformation.getCompletion().getCategories()); |
399 | } |
400 | else { |
401 | categories.add("UserDefined"); |
402 | categories.add(transformation.getType().toString()); |
403 | } |
404 | return categories.toString(); |
405 | case 3: |
406 | return transformation.getConfigFileURI(); |
407 | default: |
408 | break; |
409 | } |
410 | return null; |
411 | } |
412 | |
413 | } |
414 | |
415 | private class TransformationsCheckBoxProvider implements ICheckStateProvider { |
416 | |
417 | @Override |
418 | public boolean isChecked(Object element) { |
419 | Transformation transformation = (Transformation) element; |
420 | return transformation.isEnabled(); |
421 | } |
422 | |
423 | @Override |
424 | public boolean isGrayed(Object element) { |
425 | return false; |
426 | } |
427 | |
428 | } |
429 | |
430 | public static String openModelDialog(Shell shell){ |
431 | String ret = null; |
432 | ElementTreeSelectionDialog dialog = new ElementTreeSelectionDialog(shell, new WorkbenchLabelProvider(), new WorkbenchContentProvider()); |
433 | dialog.setTitle("Select a Resource:"); |
434 | dialog.setMessage("Select Models:"); |
435 | dialog.setInput(ResourcesPlugin.getWorkspace().getRoot()); |
436 | //dialog.setComparator(new ResourceComparator(ResourceComparator.NAME)); |
437 | if (dialog.open() == IDialogConstants.OK_ID) { |
438 | Object[] resources = (Object[]) dialog.getResult(); |
439 | StringBuffer arg = new StringBuffer(); |
440 | IResource resource = null; |
441 | for(int i = 0; i < (resources.length - 1); ++i){ |
442 | resource = (IResource) resources[i]; |
443 | arg.append(URI.createPlatformResourceURI(resource.getFullPath().toString(),true)); |
444 | arg.append(" "); |
445 | } |
446 | resource = (IResource)resources[resources.length - 1]; |
447 | arg.append(URI.createPlatformResourceURI(resource.getFullPath().toString(),true)); |
448 | ret = arg.toString(); |
449 | } |
450 | return ret; |
451 | } |
452 | |
453 | } |