|
Lines 1-112
Link Here
|
| 1 |
/******************************************************************************* |
1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2011 IBM Corporation and others. |
2 |
* Copyright (c) 2011 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
7 |
* |
| 8 |
* Contributors: |
8 |
* Contributors: |
| 9 |
* Dean Roberts, IBM Corporation - initial API and implementation |
9 |
* Dean Roberts, IBM Corporation - initial API and implementation |
| 10 |
*******************************************************************************/ |
10 |
*******************************************************************************/ |
| 11 |
|
11 |
|
| 12 |
package org.eclipse.e4.examples.webintegration.links; |
12 |
package org.eclipse.e4.examples.webintegration.links; |
| 13 |
|
13 |
|
| 14 |
|
14 |
|
| 15 |
import org.eclipse.e4.examples.webintegration.application.Perspective; |
15 |
import org.eclipse.e4.examples.webintegration.application.Perspective; |
| 16 |
import org.eclipse.swt.SWT; |
16 |
import org.eclipse.swt.SWT; |
| 17 |
import org.eclipse.swt.browser.Browser; |
17 |
import org.eclipse.swt.browser.Browser; |
| 18 |
import org.eclipse.swt.browser.LocationEvent; |
18 |
import org.eclipse.swt.browser.LocationEvent; |
| 19 |
import org.eclipse.swt.browser.LocationListener; |
19 |
import org.eclipse.swt.browser.LocationListener; |
| 20 |
import org.eclipse.swt.widgets.Composite; |
20 |
import org.eclipse.swt.widgets.Composite; |
| 21 |
import org.eclipse.ui.IViewPart; |
21 |
import org.eclipse.ui.IViewPart; |
| 22 |
import org.eclipse.ui.PartInitException; |
22 |
import org.eclipse.ui.PartInitException; |
| 23 |
import org.eclipse.ui.PlatformUI; |
23 |
import org.eclipse.ui.PlatformUI; |
| 24 |
import org.eclipse.ui.actions.NewWizardAction; |
24 |
import org.eclipse.ui.actions.NewWizardAction; |
| 25 |
import org.eclipse.ui.part.ViewPart; |
25 |
import org.eclipse.ui.part.ViewPart; |
| 26 |
|
26 |
|
| 27 |
|
27 |
|
| 28 |
/** |
28 |
/** |
| 29 |
* Example of a BrowserWidget that can incercept links and perform Eclipse Workbench |
29 |
* Example of a BrowserWidget that can incercept links and perform Eclipse Workbench |
| 30 |
* actions as desired. |
30 |
* actions as desired. |
| 31 |
* |
31 |
* |
| 32 |
* This example is discussed at http://deanoneclipse.wordpress.com |
32 |
* This example is discussed at http://deanoneclipse.wordpress.com |
| 33 |
*/ |
33 |
*/ |
| 34 |
public class BrowserView extends ViewPart { |
34 |
public class BrowserView extends ViewPart { |
| 35 |
|
35 |
|
| 36 |
private Browser browser; |
36 |
private Browser browser; |
| 37 |
|
37 |
|
| 38 |
public void createPartControl(Composite parent) { |
38 |
public void createPartControl(Composite parent) { |
| 39 |
browser = new Browser(parent, SWT.NONE); |
39 |
browser = new Browser(parent, SWT.NONE); |
| 40 |
browser.setUrl(Perspective.gmailURL); |
40 |
browser.setUrl(Perspective.gmailURL); |
| 41 |
|
41 |
|
| 42 |
// Hooks the link intercept code |
42 |
// Hooks the link intercept code |
| 43 |
browser.addLocationListener(new LinkInterceptListener()); |
43 |
browser.addLocationListener(new LinkInterceptListener()); |
| 44 |
} |
44 |
} |
| 45 |
|
45 |
|
| 46 |
/** |
46 |
/** |
| 47 |
* Implement a LocationListener to intercept links and decide what to do. |
47 |
* Implement a LocationListener to intercept links and decide what to do. |
| 48 |
*/ |
48 |
*/ |
| 49 |
private class LinkInterceptListener implements LocationListener { |
49 |
private class LinkInterceptListener implements LocationListener { |
| 50 |
// method called when the user clicks a link but before the link is opened. |
50 |
// method called when the user clicks a link but before the link is opened. |
| 51 |
public void changing(LocationEvent event) { |
51 |
public void changing(LocationEvent event) { |
| 52 |
try { |
52 |
try { |
| 53 |
// Call user code to process link as desired and return |
53 |
// Call user code to process link as desired and return |
| 54 |
// true if the link should be opened in place. |
54 |
// true if the link should be opened in place. |
| 55 |
boolean shouldOpenLinkInPlace = !openView(event.location); |
55 |
boolean shouldOpenLinkInPlace = !openView(event.location); |
| 56 |
|
56 |
|
| 57 |
// Setting event.doit to false prevents the link from opening in place |
57 |
// Setting event.doit to false prevents the link from opening in place |
| 58 |
event.doit = shouldOpenLinkInPlace; |
58 |
event.doit = shouldOpenLinkInPlace; |
| 59 |
} catch (PartInitException e) { |
59 |
} catch (PartInitException e) { |
| 60 |
e.printStackTrace(); |
60 |
e.printStackTrace(); |
| 61 |
} |
61 |
} |
| 62 |
} |
62 |
} |
| 63 |
|
63 |
|
| 64 |
// method called after the link has been opened in place. |
64 |
// method called after the link has been opened in place. |
| 65 |
public void changed(LocationEvent event) { |
65 |
public void changed(LocationEvent event) { |
| 66 |
// Not used in this example |
66 |
// Not used in this example |
| 67 |
} |
67 |
} |
| 68 |
} |
68 |
} |
| 69 |
|
69 |
|
| 70 |
/** |
70 |
/** |
| 71 |
* User code: |
71 |
* User code: |
| 72 |
* |
72 |
* |
| 73 |
* Examine the link and determine if we wish to intercept it. Perform appropriate actions for intercepted links, do |
73 |
* Examine the link and determine if we wish to intercept it. Perform appropriate actions for intercepted links, do |
| 74 |
* nothing for links we want to be opened in place (default behaviour) |
74 |
* nothing for links we want to be opened in place (default behaviour) |
| 75 |
* |
75 |
* |
| 76 |
* Return true if we intercepted the link. Return false if we did not intercept the link and expect the browser to |
76 |
* Return true if we intercepted the link. Return false if we did not intercept the link and expect the browser to |
| 77 |
* open the link in place. |
77 |
* open the link in place. |
| 78 |
*/ |
78 |
*/ |
| 79 |
private boolean openView(String location) throws PartInitException { |
79 |
private boolean openView(String location) throws PartInitException { |
| 80 |
|
80 |
|
| 81 |
/** |
81 |
/** |
| 82 |
* Certainly the if/else-if construct could be replaced with a more elegant lookup mechanism. |
82 |
* Certainly the if/else-if construct could be replaced with a more elegant lookup mechanism. |
| 83 |
*/ |
83 |
*/ |
| 84 |
|
84 |
|
| 85 |
// Open a view |
85 |
// Open a view |
| 86 |
if (location.equals("http://www.google.com/intl/en_CA/mobile/mail/#utm_source=en_CA-cpp-g4mc-gmhp&utm_medium=cpp&utm_campaign=en_CA")) { |
86 |
if (location.equals("http://www.google.com/intl/en_CA/mobile/mail/#utm_source=en_CA-cpp-g4mc-gmhp&utm_medium=cpp&utm_campaign=en_CA")) { |
| 87 |
IViewPart newView = getViewSite().getPage().showView("url.link.1"); |
87 |
IViewPart newView = getViewSite().getPage().showView("url.link.1"); |
| 88 |
((LinkView) newView).setURL(location); |
88 |
((LinkView) newView).setURL(location); |
| 89 |
|
89 |
|
| 90 |
return true; |
90 |
return true; |
| 91 |
// Open a wizard |
91 |
// Open a wizard |
| 92 |
} else if (location.contains("/accounts/recovery")) { |
92 |
} else if (location.contains("/accounts/recovery")) { |
| 93 |
NewWizardAction a = new NewWizardAction(PlatformUI.getWorkbench().getActiveWorkbenchWindow()); |
93 |
NewWizardAction action = new NewWizardAction(PlatformUI.getWorkbench().getActiveWorkbenchWindow()); |
| 94 |
a.run(); |
94 |
action.run(); |
| 95 |
// commented out to avoid a dependency on org.eclipse.ui.ide: |
95 |
|
| 96 |
// BasicNewFileResourceWizard wizard = new BasicNewFileResourceWizard(); |
96 |
return true; |
| 97 |
// wizard.init(getSite().getWorkbenchWindow().getWorkbench(), new StructuredSelection()); |
97 |
} |
| 98 |
// WizardDialog dialog = new WizardDialog(getSite().getShell(), wizard); |
98 |
|
| 99 |
// dialog.create(); |
99 |
// Do not intercept link. Allow browser widget to open link in place |
| 100 |
// dialog.open(); |
100 |
return false; |
| 101 |
|
101 |
} |
| 102 |
return true; |
102 |
|
| 103 |
} |
103 |
public void setFocus() { |
| 104 |
|
104 |
// Not important for our example. |
| 105 |
// Do not intercept link. Allow browser widget to open link in place |
105 |
} |
| 106 |
return false; |
106 |
} |
| 107 |
} |
|
|
| 108 |
|
| 109 |
public void setFocus() { |
| 110 |
// Not important for our example. |
| 111 |
} |
| 112 |
} |