Skip to main content
summaryrefslogtreecommitdiffstats
blob: f795b6b059a1ffb3f2fb97c0bcccc19ee6faa68f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
/**
 * Copyright (c) 2011, 2015 - Lunifera GmbH (Gross Enzersdorf, Austria), Loetz GmbH&Co.KG (69115 Heidelberg, Germany)
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0 
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *         Florian Pirchner - Initial implementation
 */

package org.eclipse.osbp.ecview.vaadin.ide.preview.parts;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashSet;
import java.util.Set;

import javax.annotation.PreDestroy;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.osbp.ecview.core.common.context.IViewContext;
import org.eclipse.osbp.ecview.core.common.editpart.IExposedActionEditpart;
import org.eclipse.osbp.ecview.vaadin.ide.preview.PreviewActivator;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.DropTarget;
import org.eclipse.swt.dnd.FileTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.IViewSite;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.browser.IWorkbenchBrowserSupport;
import org.eclipse.ui.internal.browser.BrowserViewer;
import org.eclipse.ui.internal.browser.IBrowserViewerContainer;
import org.eclipse.ui.internal.browser.WebBrowserUtil;
import org.eclipse.ui.internal.browser.WebBrowserViewDropAdapter;
import org.eclipse.ui.part.ISetSelectionTarget;
import org.eclipse.ui.part.ViewPart;

import com.google.inject.Inject;

/**
 * Parts copied from org.eclipse.ui.internal.browser.WebBrowserView
 */
@SuppressWarnings("restriction")
public class ECViewIDEPreviewPart extends ViewPart implements
		IBrowserViewerContainer, ISetSelectionTarget, IUiRenderedListener {

	private static String URL = "http://localhost:%s/idepreview";

	protected BrowserViewer viewer;
	protected ISelectionListener listener;

	@Inject
	private ECViewVaadinSynchronizer synchronizer;

	private Set<Action> exposedActions = new HashSet<Action>();

	public ECViewIDEPreviewPart() {

	}

	public void createPartControl(Composite parent) {
		int style = WebBrowserUtil.decodeStyle(getViewSite().getSecondaryId());
		viewer = new BrowserViewer(parent, style);
		viewer.setContainer(this);
		
		String port = System.getProperty("preview.port", "8099");
		viewer.setURL(String.format(URL, port));

		getViewSite().getActionBars().getMenuManager()
				.add(new LinkWithEditorAction());
		getViewSite().getActionBars().getMenuManager()
				.add(new ShowLayoutBoundsAction());

		/*
		 * PropertyChangeListener propertyChangeListener = new
		 * PropertyChangeListener() { public void
		 * propertyChange(PropertyChangeEvent event) { if
		 * (BrowserViewer.PROPERTY_TITLE.equals(event.getPropertyName())) {
		 * setPartName((String) event.getNewValue()); } } };
		 * viewer.addPropertyChangeListener(propertyChangeListener);
		 */
		initDragAndDrop();
	}

	@Override
	public void init(IViewSite site) throws PartInitException {
		super.init(site);
		synchronizer.start(site);
		PreviewActivator.getIDEPreviewHandler().setPart(this);
	}

	@PreDestroy
	public void predestroy() {
		viewer.setURL("blank");
	}

	public void dispose() {
		PreviewActivator.getIDEPreviewHandler().setPart(null);
		synchronizer.stop(getSite());

		if (viewer != null)
			viewer.setContainer(null);

		if (listener != null)
			removeSelectionListener();

		super.dispose();
	}

	public void setURL(String url) {
		if (viewer != null)
			viewer.setURL(url);
	}

	public void setFocus() {
		viewer.setFocus();
	}

	public boolean close() {
		try {
			viewer.setURL("blank");
			getSite().getPage().hideView(this);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	public IActionBars getActionBars() {
		return getViewSite().getActionBars();
	}

	public void openInExternalBrowser(String url) {
		try {
			URL theURL = new URL(url);
			IWorkbenchBrowserSupport support = PlatformUI.getWorkbench()
					.getBrowserSupport();
			support.getExternalBrowser().openURL(theURL);
		} catch (MalformedURLException e) {
		} catch (PartInitException e) {
		}
	}

	public void addSelectionListener() {
		if (listener != null)
			return;

		listener = new ISelectionListener() {
			public void selectionChanged(IWorkbenchPart part,
					ISelection selection) {
				onSelectionChange(selection);
			}
		};
		getSite().getWorkbenchWindow().getSelectionService()
				.addPostSelectionListener(listener);
	}

	private void onSelectionChange(ISelection selection) {
		if (!(selection instanceof IStructuredSelection))
			return;
		IStructuredSelection sel = (IStructuredSelection) selection;
		Object obj = sel.getFirstElement();
		if (obj instanceof IAdaptable) {
			IAdaptable adapt = (IAdaptable) obj;
			URL url = getURLFromAdaptable(adapt);
			if (url != null)
				setURL(url.toExternalForm());
		}
	}

	private URL getURLFromAdaptable(IAdaptable adapt) {
		// test for path
		IPath path = (IPath) adapt.getAdapter(IPath.class);
		if (path != null) {
			File file = path.toFile();
			if (file.exists() && isWebFile(file.getName()))
				try {
					return file.toURI().toURL();
				} catch (MalformedURLException e) {
					return null;
				}
		}
		return (URL) adapt.getAdapter(URL.class);
	}

	public void removeSelectionListener() {
		if (listener == null)
			return;
		getSite().getWorkbenchWindow().getSelectionService()
				.removePostSelectionListener(listener);
		listener = null;
	}

	/**
	 * Return true if the filename has a "web" extension.
	 * 
	 * @param name
	 * @return
	 */
	protected boolean isWebFile(String name) {
		return name.endsWith("html") || name.endsWith("htm") || name.endsWith("gif") || //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
				name.endsWith("jpg"); //$NON-NLS-1$
	}

	/**
	 * Adds drag and drop support to the view.
	 */
	protected void initDragAndDrop() {
		Transfer[] transfers = new Transfer[] {
		// LocalSelectionTransfer.getInstance(),
		// ResourceTransfer.getInstance(),
		FileTransfer.getInstance() };

		DropTarget dropTarget = new DropTarget(viewer, DND.DROP_COPY
				| DND.DROP_DEFAULT);
		dropTarget.setTransfer(transfers);
		dropTarget.addDropListener(new CustomWebBrowserViewDropAdapter(viewer));
	}

	public void selectReveal(ISelection selection) {
		onSelectionChange(selection);
	}

	public void setBrowserViewName(String name) {
		setPartName(name);
	}

	public void setBrowserViewTooltip(String tip) {
		setTitleToolTip(tip);
	}

	public void notifyNewViewRendered(final IViewContext context) {
		viewer.getDisplay().asyncExec(new Runnable() {
			@Override
			public void run() {
				// remove old actions
				for (Action action : exposedActions) {
					getViewSite().getActionBars().getToolBarManager()
							.remove(action.getId());
				}
				exposedActions.clear();

				// add the new exposed actions
				for (IExposedActionEditpart exposedAction : context
						.getExposedActions()) {
					
					// get the path of the icon
					String theme = PreviewActivator.getIDEPreviewHandler().getThemeName();
					String iconPath = String.format("VAADIN/themes/%s/%s", theme, exposedAction.getIconName());
					URL iconURL = PreviewActivator.getDefault().findResource(iconPath);
					
					Action action = null;
					if (exposedAction.getId().equals(
							"org.eclipse.osbp.actions.load")) {
						action = new ExposedLoadAction(exposedAction.getId(),exposedAction
								.getDescription(), exposedAction.getIconName(), iconURL);
					} else if (exposedAction.getId().equals(
							"org.eclipse.osbp.actions.save")) {
						action = new ExposedSaveAction(exposedAction.getId(),exposedAction
								.getDescription(), exposedAction.getIconName(), iconURL);
					} else if (exposedAction.getId().equals(
							"org.eclipse.osbp.actions.delete")) {
						action = new ExposedDeleteAction(exposedAction.getId(),exposedAction
								.getDescription(), exposedAction.getIconName(), iconURL);
					} else if (exposedAction.getId().equals(
							"org.eclipse.osbp.actions.find")) {
						action = new ExposedFindAction(exposedAction.getId(),exposedAction
								.getDescription(), exposedAction.getIconName(), iconURL);
					} else if (exposedAction.getId().equals(
							"org.eclipse.osbp.actions.create")) {
						action = new ExposedCreateAction(exposedAction.getId(),exposedAction
								.getDescription(), exposedAction.getIconName(), iconURL);
					} else {
						action = new ExposedNullAction(exposedAction.getId(),exposedAction
								.getDescription(), exposedAction.getIconName(), iconURL);
					}
					
					if (action != null) {
						exposedActions.add(action);
						getViewSite().getActionBars().getToolBarManager()
								.add(action);
					}
				}
				getViewSite().getActionBars().updateActionBars();
			}
		});
	}

	private static class CustomWebBrowserViewDropAdapter extends
			WebBrowserViewDropAdapter {

		protected CustomWebBrowserViewDropAdapter(BrowserViewer view) {
			super(view);
		}
	}

	private class LinkWithEditorAction extends Action {

		public LinkWithEditorAction() {
			setText("Link with editor");
			setDescription("Links the selection in preview UI with the associated grammar element.");
			setToolTipText("Links the selection in preview UI with the associated grammar element.");
			setImageDescriptor(PreviewActivator.imageDescriptorFromPlugin(
					PreviewActivator.BUNDLE_ID, "/icons/synced.gif"));
			setChecked(false);
			setEnabled(true);
		}

		@Override
		public void run() {
			PreviewActivator.getIDEPreviewHandler().setLinkedWithEditor(isChecked());
		}
	}

	private class ShowLayoutBoundsAction extends Action {

		public ShowLayoutBoundsAction() {
			setText("Show layout bounds");
			setImageDescriptor(PreviewActivator.imageDescriptorFromPlugin(
					PreviewActivator.BUNDLE_ID, "/icons/bounds.png"));
			setChecked(false);
			setEnabled(true);
		}

		@Override
		public void run() {
			PreviewActivator.getIDEPreviewHandler().setShowLayoutBounds(isChecked());
		}
	}

	private class ExposedLoadAction extends Action {

		public ExposedLoadAction(String id, String text, String icon, URL iconURL) {
			super(text, IAction.AS_PUSH_BUTTON);
			setId(id);
			if(iconURL != null){
				setImageDescriptor(ImageDescriptor.createFromURL(iconURL));
			}else{
				setImageDescriptor(PreviewActivator.imageDescriptorFromPlugin(
						PreviewActivator.BUNDLE_ID, "icons/load.gif"));
			}
			setChecked(false);
			setEnabled(true);
		}

		@Override
		public void run() {

		}
	}

	private class ExposedSaveAction extends Action {

		public ExposedSaveAction(String id, String text, String icon, URL iconURL) {
			super(text, IAction.AS_PUSH_BUTTON);
			setId(id);
			if(iconURL != null){
				setImageDescriptor(ImageDescriptor.createFromURL(iconURL));
			}else{
				setImageDescriptor(PreviewActivator.imageDescriptorFromPlugin(
						PreviewActivator.BUNDLE_ID, "icons/save.gif"));
			}
			setChecked(false);
			setEnabled(true);
		}

		@Override
		public void run() {

		}
	}

	private class ExposedDeleteAction extends Action {

		public ExposedDeleteAction(String id, String text, String icon, URL iconURL) {
			super(text, IAction.AS_PUSH_BUTTON);
			setId(id);
			if(iconURL != null){
				setImageDescriptor(ImageDescriptor.createFromURL(iconURL));
			}else{
				setImageDescriptor(PreviewActivator.imageDescriptorFromPlugin(
						PreviewActivator.BUNDLE_ID, "icons/trash.gif"));
			}
			setChecked(false);
			setEnabled(true);
		}

		@Override
		public void run() {

		}
	}

	private class ExposedFindAction extends Action {

		public ExposedFindAction(String id, String text, String icon, URL iconURL) {
			super(text, IAction.AS_PUSH_BUTTON);
			setId(id);
			if(iconURL != null){
				setImageDescriptor(ImageDescriptor.createFromURL(iconURL));
			}else{
				setImageDescriptor(PreviewActivator.imageDescriptorFromPlugin(
						PreviewActivator.BUNDLE_ID, "icons/search.gif"));
			}
			setChecked(false);
			setEnabled(true);
		}

		@Override
		public void run() {

		}
	}

	private class ExposedCreateAction extends Action {

		public ExposedCreateAction(String id, String text, String icon, URL iconURL) {
			super(text, IAction.AS_PUSH_BUTTON);
			setId(id);
			if(iconURL != null){
				setImageDescriptor(ImageDescriptor.createFromURL(iconURL));
			}else{
				setImageDescriptor(PreviewActivator.imageDescriptorFromPlugin(
						PreviewActivator.BUNDLE_ID, "icons/new.gif"));
			}
			setChecked(false);
			setEnabled(true);
		}

		@Override
		public void run() {

		}
	}
	
	private class ExposedNullAction extends Action {

		public ExposedNullAction(String id, String text, String icon, URL iconURL) {
			super(text, IAction.AS_PUSH_BUTTON);
			setId(id);
			if(iconURL != null){
				setImageDescriptor(ImageDescriptor.createFromURL(iconURL));
			}
			setChecked(false);
			setEnabled(true);
		}

		@Override
		public void run() {

		}
	}

}

Back to the top