1 | package de.uka.ipd.sdq.cip.runtime.dialogs; |
2 | |
3 | |
4 | import java.io.BufferedReader; |
5 | import java.io.FileNotFoundException; |
6 | import java.io.IOException; |
7 | import java.io.InputStreamReader; |
8 | import java.util.Iterator; |
9 | |
10 | import org.eclipse.jface.dialogs.Dialog; |
11 | import org.eclipse.jface.dialogs.TitleAreaDialog; |
12 | import org.eclipse.jface.viewers.ArrayContentProvider; |
13 | import org.eclipse.jface.viewers.ISelectionChangedListener; |
14 | import org.eclipse.jface.viewers.IStructuredSelection; |
15 | import org.eclipse.jface.viewers.SelectionChangedEvent; |
16 | import org.eclipse.jface.viewers.TableViewer; |
17 | import org.eclipse.swt.SWT; |
18 | import org.eclipse.swt.SWTError; |
19 | import org.eclipse.swt.browser.Browser; |
20 | import org.eclipse.swt.browser.BrowserFunction; |
21 | import org.eclipse.swt.events.ModifyEvent; |
22 | import org.eclipse.swt.events.ModifyListener; |
23 | import org.eclipse.swt.events.SelectionAdapter; |
24 | import org.eclipse.swt.events.SelectionEvent; |
25 | import org.eclipse.swt.graphics.Point; |
26 | import org.eclipse.swt.graphics.Rectangle; |
27 | import org.eclipse.swt.layout.FillLayout; |
28 | import org.eclipse.swt.layout.GridData; |
29 | import org.eclipse.swt.layout.GridLayout; |
30 | import org.eclipse.swt.widgets.Button; |
31 | import org.eclipse.swt.widgets.Composite; |
32 | import org.eclipse.swt.widgets.Control; |
33 | import org.eclipse.swt.widgets.Event; |
34 | import org.eclipse.swt.widgets.Label; |
35 | import org.eclipse.swt.widgets.Listener; |
36 | import org.eclipse.swt.widgets.Shell; |
37 | import org.eclipse.swt.widgets.TableItem; |
38 | import org.eclipse.swt.widgets.Text; |
39 | import org.eclipse.swt.widgets.Widget; |
40 | |
41 | import de.uka.ipd.sdq.cip.completions.RegisteredCompletion; |
42 | import de.uka.ipd.sdq.cip.completions.RegisteredCompletions; |
43 | import de.uka.ipd.sdq.cip.configuration.Transformation; |
44 | import de.uka.ipd.sdq.cip.configuration.TransformationType; |
45 | import de.uka.ipd.sdq.cip.runtime.ConstantsContainer; |
46 | |
47 | @Deprecated |
48 | public class EditCompletionDialog_OLD extends TitleAreaDialog { |
49 | |
50 | private Button featureconfig; |
51 | private Text featureconfigLocation; |
52 | private Button annotated; |
53 | private Text annotationLocation; |
54 | private Text annotationCompletionType; |
55 | private Text plainCompletionType; |
56 | private Button plain; |
57 | private Button plainQVTO; |
58 | private Text plainQVTOCompletionType; |
59 | private Transformation transformation; |
60 | |
61 | public EditCompletionDialog_OLD(Shell parentShell,Transformation transformation) { |
62 | super(parentShell); |
63 | this.transformation = transformation; |
64 | setShellStyle(SWT.DIALOG_TRIM | SWT.RESIZE | SWT.APPLICATION_MODAL); |
65 | } |
66 | |
67 | @Override |
68 | protected void okPressed() { |
69 | if(featureconfig.getSelection()) { |
70 | transformation.setType(TransformationType.FEATURE); |
71 | transformation.setFeatureFileURI(featureconfigLocation.getText()); |
72 | } |
73 | else if(annotated.getSelection()) { |
74 | transformation.setType(TransformationType.ANNOTATED); |
75 | transformation.setConfigFileURI(annotationLocation.getText()); |
76 | transformation.setQVTFileURI(annotationCompletionType.getText()); |
77 | } |
78 | else if(plain.getSelection()) { |
79 | transformation.setType(TransformationType.PLAIN); |
80 | transformation.setQVTFileURI(plainCompletionType.getText()); |
81 | } |
82 | /*else if(plainQVTO.getSelection()) { |
83 | transformation.setType(TransformationType.PLAINQVTO); |
84 | transformation.setQVTFileURI(plainQVTOCompletionType.getText()); |
85 | } */ |
86 | |
87 | super.okPressed(); |
88 | } |
89 | |
90 | @Override |
91 | protected Control createDialogArea(Composite parent) { |
92 | setTitle("Select completion type"); |
93 | setMessage("Select one completion type and configure it"); |
94 | Composite container = (Composite) super.createDialogArea(parent); |
95 | GridLayout layout = new GridLayout(1,false); |
96 | container.setLayout(layout); |
97 | |
98 | featureconfig = createRadioButton(container, "Feature Config"); |
99 | featureconfigLocation = createFileLocation(container, "Feature &Config File",new String[] {"*.featureconfig"}); |
100 | |
101 | annotated = createRadioButton(container, "Annotated Completion"); |
102 | annotationCompletionType = createFileLocation(container, "Completion",null); |
103 | annotationLocation = createFileLocation(container, "Annotation File", new String[] {"*.*"}); |
104 | |
105 | plain = createRadioButton(container, "Plain Completion"); |
106 | plainCompletionType = createFileLocation(container, "Completion",null); |
107 | |
108 | plainQVTO = createRadioButton(container, "Plain QVTO Completion"); |
109 | plainQVTOCompletionType = createFileLocation(container, "Completion",null); |
110 | |
111 | featureconfig.setSelection(true); |
112 | activateGroup(featureconfig); |
113 | |
114 | //container.getShell().setSize(500, 400); |
115 | return container; |
116 | } |
117 | |
118 | Button createRadioButton(Composite parent, String caption) { |
119 | Button radio = new Button(parent, SWT.RADIO); |
120 | radio.setText(caption); |
121 | GridData gd = new GridData(SWT.FILL, SWT.FILL, true, false); |
122 | radio.setLayoutData(gd); |
123 | radio.addSelectionListener(new SelectionAdapter() { |
124 | @Override |
125 | public void widgetSelected(SelectionEvent e) { |
126 | activateGroup(e.widget); |
127 | } |
128 | }); |
129 | return radio; |
130 | } |
131 | |
132 | protected void activateGroup(Widget widget) { |
133 | if(widget == featureconfig){ |
134 | featureconfigLocation.getParent().setVisible(true); |
135 | annotationCompletionType.getParent().setVisible(false); |
136 | annotationLocation.getParent().setVisible(false); |
137 | plainCompletionType.getParent().setVisible(false); |
138 | } |
139 | else if(widget == annotated){ |
140 | featureconfigLocation.getParent().setVisible(false); |
141 | annotationCompletionType.getParent().setVisible(true); |
142 | annotationLocation.getParent().setVisible(true); |
143 | plainCompletionType.getParent().setVisible(false); |
144 | } |
145 | else if(widget == plain){ |
146 | featureconfigLocation.getParent().setVisible(false); |
147 | annotationCompletionType.getParent().setVisible(false); |
148 | annotationLocation.getParent().setVisible(false); |
149 | plainCompletionType.getParent().setVisible(true); |
150 | } |
151 | else if(widget == plainQVTO){ |
152 | featureconfigLocation.getParent().setVisible(false); |
153 | annotationCompletionType.getParent().setVisible(false); |
154 | annotationLocation.getParent().setVisible(false); |
155 | plainQVTOCompletionType.getParent().setVisible(true); |
156 | } |
157 | |
158 | } |
159 | |
160 | Text createFileLocation(Composite parent, String caption, final String[] fileExtensions) { |
161 | Composite comp = new Composite(parent, SWT.BORDER); |
162 | GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true); |
163 | gd.horizontalIndent = 20; |
164 | comp.setLayoutData(gd); |
165 | GridLayout layout = new GridLayout(2,false); |
166 | comp.setLayout(layout); |
167 | |
168 | Label label = new Label(comp, SWT.LEFT); |
169 | label.setText(caption); |
170 | gd = new GridData(); |
171 | gd.horizontalSpan = 2; |
172 | label.setLayoutData(gd); |
173 | final Text text = new Text(comp, SWT.SINGLE | SWT.BORDER); |
174 | gd = new GridData(SWT.FILL, SWT.FILL, true, false); |
175 | text.setLayoutData(gd); |
176 | |
177 | Button edit = new Button(comp, SWT.PUSH); |
178 | edit.setText("..."); |
179 | edit.addSelectionListener(new SelectionAdapter() { |
180 | |
181 | @Override |
182 | public void widgetSelected(SelectionEvent e) { |
183 | FileSelectionDialog editDialog = new FileSelectionDialog(getShell(), text.getText(),fileExtensions); |
184 | if(editDialog.open() == FileSelectionDialog.OK) { |
185 | text.setText(editDialog.getFileLocation()); |
186 | } |
187 | } |
188 | }); |
189 | |
190 | return text; |
191 | } |
192 | |
193 | static class FileSelectionDialog extends Dialog { |
194 | |
195 | private String fileLocation; |
196 | private String[] fileExtensions; |
197 | final String tooltipTemplate; |
198 | |
199 | protected FileSelectionDialog(Shell parentShell,String fileLocation, String[] fileExtensions) { |
200 | super(parentShell); |
201 | this.fileLocation = fileLocation; |
202 | this.fileExtensions = fileExtensions; |
203 | StringBuffer contentOfFile = null; |
204 | try { |
205 | BufferedReader br = new BufferedReader( |
206 | new InputStreamReader( |
207 | getClass().getResourceAsStream(ConstantsContainer.HTML_TOOLTIP))); |
208 | contentOfFile = new StringBuffer(); |
209 | String line; |
210 | while ((line = br.readLine()) != null) { |
211 | contentOfFile.append(line); |
212 | } |
213 | } catch (FileNotFoundException e) { |
214 | e.printStackTrace(); |
215 | } catch (IOException e) { |
216 | e.printStackTrace(); |
217 | } |
218 | if(contentOfFile != null) |
219 | tooltipTemplate = contentOfFile.toString(); |
220 | else |
221 | tooltipTemplate = null; |
222 | |
223 | } |
224 | |
225 | @Override |
226 | protected Control createDialogArea(Composite parent) { |
227 | Composite comp = (Composite) super.createDialogArea(parent); |
228 | GridLayout layout = new GridLayout(); |
229 | comp.setLayout(layout); |
230 | |
231 | Composite upperContainer = new Composite(comp, SWT.None); |
232 | GridData gd = new GridData(); |
233 | gd.horizontalAlignment = SWT.FILL; |
234 | gd.grabExcessHorizontalSpace = true; |
235 | gd.verticalAlignment = SWT.FILL; |
236 | gd.grabExcessVerticalSpace = true; |
237 | upperContainer.setLayoutData(gd); |
238 | GridLayout upperLayout = new GridLayout(); |
239 | upperContainer.setLayout(upperLayout); |
240 | |
241 | Label label = new Label(upperContainer, SWT.LEFT); |
242 | label.setText("Completion Library:"); |
243 | gd = new GridData(); |
244 | gd.grabExcessHorizontalSpace = true; |
245 | gd.horizontalAlignment = GridData.FILL; |
246 | label.setLayoutData(gd); |
247 | |
248 | |
249 | final TableViewer tableViewer = new TableViewer(upperContainer); |
250 | gd = new GridData(); |
251 | gd.grabExcessHorizontalSpace = true; |
252 | gd.horizontalAlignment = GridData.FILL; |
253 | gd.grabExcessVerticalSpace = true; |
254 | gd.verticalAlignment = GridData.FILL; |
255 | gd.heightHint = 200; |
256 | gd.widthHint = 350; |
257 | tableViewer.getTable().setLayoutData(gd); |
258 | |
259 | |
260 | Composite lowerContainer = new Composite(comp, SWT.None); |
261 | gd = new GridData(); |
262 | gd.horizontalAlignment = SWT.FILL; |
263 | gd.grabExcessHorizontalSpace = true; |
264 | lowerContainer.setLayoutData(gd); |
265 | GridLayout lowerLayout = new GridLayout(4,false); |
266 | lowerContainer.setLayout(lowerLayout); |
267 | |
268 | final Text text = new Text(parent,SWT.BORDER); |
269 | |
270 | label = new Label(lowerContainer, SWT.LEFT); |
271 | label.setText("Path:"); |
272 | gd = new GridData(); |
273 | label.setLayoutData(gd); |
274 | |
275 | //Button browseWorkspaceButton = DialogHelper.createBrowseWorkspaceButton(lowerContainer, text); |
276 | gd = new GridData(); |
277 | //browseWorkspaceButton.setLayoutData(gd); |
278 | |
279 | |
280 | //Button browseFileSystemButton = DialogHelper.createBrowseFileSystemButton(lowerContainer, text, fileExtensions); |
281 | gd = new GridData(); |
282 | //browseFileSystemButton.setLayoutData(gd); |
283 | |
284 | //text = new Text(comp,SWT.BORDER); |
285 | text.setParent(lowerContainer); |
286 | text.setText(fileLocation); |
287 | gd = new GridData(); |
288 | gd.horizontalSpan = 4; |
289 | gd.horizontalAlignment = SWT.FILL; |
290 | gd.grabExcessHorizontalSpace = true; |
291 | text.setLayoutData(gd); |
292 | |
293 | text.addModifyListener(new ModifyListener() { |
294 | @Override |
295 | public void modifyText(ModifyEvent e) { |
296 | fileLocation = text.getText(); |
297 | } |
298 | }); |
299 | |
300 | tableViewer.setContentProvider(new ArrayContentProvider()); |
301 | tableViewer.setLabelProvider(RegisteredCompletions.getLabelProvider()); |
302 | tableViewer.setInput(RegisteredCompletions.getCompletions()); |
303 | tableViewer.addSelectionChangedListener(new ISelectionChangedListener() { |
304 | |
305 | @Override |
306 | public void selectionChanged(SelectionChangedEvent event) { |
307 | IStructuredSelection selection = (IStructuredSelection) event.getSelection(); |
308 | RegisteredCompletion rc = (RegisteredCompletion) selection.getFirstElement(); |
309 | text.setText(rc.getQvtscript()); |
310 | |
311 | } |
312 | }); |
313 | |
314 | final Listener labelListener = new Listener () { |
315 | |
316 | @Override |
317 | public void handleEvent (Event event) { |
318 | Composite composite = (Composite)event.widget; |
319 | Shell shell = composite.getShell (); |
320 | switch (event.type) { |
321 | case SWT.MouseDown: |
322 | Event e = new Event (); |
323 | e.item = (TableItem) composite.getData ("_TABLEITEM"); |
324 | // Assuming table is single select, set the selection as if |
325 | // the mouse down event went through to the table |
326 | tableViewer.getTable().setSelection (new TableItem [] {(TableItem) e.item}); |
327 | tableViewer.getTable().notifyListeners (SWT.Selection, e); |
328 | shell.dispose (); |
329 | tableViewer.getTable().setFocus(); |
330 | break; |
331 | case SWT.MouseExit: |
332 | shell.dispose (); |
333 | break; |
334 | } |
335 | } |
336 | }; |
337 | |
338 | |
339 | Listener tableListener = new Listener() { |
340 | |
341 | @Override |
342 | public void handleEvent(Event event) { |
343 | Shell tip = null; |
344 | Composite composite = null; |
345 | |
346 | switch (event.type) { |
347 | case SWT.Dispose: |
348 | case SWT.KeyDown: |
349 | case SWT.MouseMove: { |
350 | if (tip == null) break; |
351 | tip.dispose (); |
352 | tip = null; |
353 | composite = null; |
354 | break; |
355 | } |
356 | case SWT.MouseHover: { |
357 | Point coords = new Point(event.x, event.y); |
358 | TableItem item = tableViewer.getTable().getItem(coords); |
359 | if (item != null) { |
360 | int columns = tableViewer.getTable().getColumnCount(); |
361 | |
362 | for (int i = 0; i < columns || i == 0; i++) { |
363 | if (item.getBounds(i).contains(coords)) { |
364 | if (tip != null && !tip.isDisposed ()) tip.dispose (); |
365 | tip = new Shell (tableViewer.getTable().getShell(), SWT.ON_TOP | SWT.NO_FOCUS | SWT.TOOL); |
366 | tip.setBackground (tableViewer.getTable().getDisplay().getSystemColor (SWT.COLOR_INFO_BACKGROUND)); |
367 | FillLayout layout = new FillLayout (); |
368 | layout.marginWidth = 2; |
369 | tip.setLayout (layout); |
370 | composite = new Composite (tip, SWT.NONE); |
371 | composite.setForeground (tableViewer.getTable().getDisplay().getSystemColor (SWT.COLOR_INFO_FOREGROUND)); |
372 | composite.setBackground (tableViewer.getTable().getDisplay().getSystemColor (SWT.COLOR_INFO_BACKGROUND)); |
373 | composite.setData ("_TABLEITEM", item); |
374 | //composite.setText ("Tooltip: " + item.getData()+ " => Column: " + i); |
375 | populateTip(composite,(RegisteredCompletion)item.getData()); |
376 | composite.addListener (SWT.MouseExit, labelListener); |
377 | composite.addListener (SWT.MouseDown, labelListener); |
378 | //Point size = tip.computeSize (SWT.DEFAULT, SWT.DEFAULT); |
379 | //Rectangle rect = item.getBounds (i); |
380 | //Point pt = tableViewer.getTable().toDisplay (rect.x, rect.y); |
381 | //tip.setBounds (pt.x, pt.y, size.x, size.y); |
382 | //tip.setVisible (true); |
383 | break; |
384 | } |
385 | } |
386 | } |
387 | } |
388 | } |
389 | |
390 | |
391 | } |
392 | |
393 | class SizeCallbackFunction extends BrowserFunction { |
394 | SizeCallbackFunction (Browser browser, String name) { |
395 | super (browser, name); |
396 | } |
397 | public Object function (Object[] arguments) { |
398 | int width = ((Double)arguments[0]).intValue(); |
399 | int height = ((Double)arguments[1]).intValue(); |
400 | Shell tip = getBrowser().getShell(); |
401 | TableItem item = (TableItem) getBrowser().getParent().getData ("_TABLEITEM"); |
402 | tip.setSize(new Point(width, height)); |
403 | Point size = tip.computeSize (SWT.DEFAULT, SWT.DEFAULT); |
404 | Rectangle rect = item.getBounds (0); |
405 | Point pt = tableViewer.getTable().toDisplay (rect.x, rect.y); |
406 | tip.setBounds (pt.x, pt.y, size.x, size.y); |
407 | getBrowser().getShell().setVisible (true); |
408 | return null; |
409 | } |
410 | } |
411 | |
412 | |
413 | |
414 | private void populateTip(Composite composite, |
415 | RegisteredCompletion data) { |
416 | composite.setLayout(new GridLayout()); |
417 | Browser browser; |
418 | try { |
419 | browser = new Browser(composite, SWT.NONE); |
420 | } catch (SWTError e) { |
421 | System.out.println("Could not instantiate Browser: " + e.getMessage()); |
422 | return; |
423 | } |
424 | |
425 | GridData gd = new GridData(); |
426 | gd.horizontalAlignment = GridData.FILL; |
427 | gd.verticalAlignment = GridData.FILL; |
428 | gd.grabExcessHorizontalSpace = true; |
429 | gd.grabExcessVerticalSpace = true; |
430 | gd.widthHint = 1000; |
431 | gd.heightHint = 600; |
432 | browser.setLayoutData(gd); |
433 | //browser.setSize(600, height)) |
434 | |
435 | StringBuffer categories = new StringBuffer(); |
436 | Iterator<String> iterator = data.getCategories().iterator(); |
437 | while(iterator.hasNext()) { |
438 | categories.append("<li>"); |
439 | categories.append(iterator.next()); |
440 | categories.append("</li>"); |
441 | } |
442 | |
443 | browser.setText( |
444 | tooltipTemplate |
445 | .replace("{image}", data.getImage()) |
446 | .replace("{caption}", data.getName()) |
447 | .replace("{description}", data.getDescription()) |
448 | .replace("{categories}", categories)); |
449 | |
450 | new SizeCallbackFunction (browser, "setBrowserSize"); |
451 | } |
452 | }; |
453 | |
454 | tableViewer.getTable().addListener (SWT.Dispose, tableListener); |
455 | tableViewer.getTable().addListener (SWT.KeyDown, tableListener); |
456 | |
457 | tableViewer.getTable().addListener (SWT.MouseMove, tableListener); |
458 | tableViewer.getTable().addListener (SWT.MouseHover, tableListener); |
459 | |
460 | |
461 | return comp; |
462 | } |
463 | |
464 | |
465 | public String getFileLocation() { |
466 | return fileLocation; |
467 | } |
468 | |
469 | @Override |
470 | protected boolean isResizable() { |
471 | return true; |
472 | } |
473 | |
474 | |
475 | } |
476 | } |