1634
|
1 |
/*
|
|
2 |
* Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of the License "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description:
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
package com.nokia.carbide.internal.discovery.ui.extension;
|
|
18 |
|
|
19 |
import java.net.MalformedURLException;
|
|
20 |
import java.net.URL;
|
1721
|
21 |
import java.util.LinkedHashSet;
|
|
22 |
import java.util.Set;
|
1634
|
23 |
|
|
24 |
import org.eclipse.jface.action.Action;
|
|
25 |
import org.eclipse.jface.action.IAction;
|
1721
|
26 |
import org.eclipse.jface.dialogs.Dialog;
|
|
27 |
import org.eclipse.jface.dialogs.IInputValidator;
|
|
28 |
import org.eclipse.jface.dialogs.InputDialog;
|
1634
|
29 |
import org.eclipse.swt.SWT;
|
|
30 |
import org.eclipse.swt.SWTError;
|
|
31 |
import org.eclipse.swt.browser.Browser;
|
|
32 |
import org.eclipse.swt.browser.LocationAdapter;
|
|
33 |
import org.eclipse.swt.browser.LocationEvent;
|
1721
|
34 |
import org.eclipse.swt.browser.OpenWindowListener;
|
1634
|
35 |
import org.eclipse.swt.browser.ProgressAdapter;
|
|
36 |
import org.eclipse.swt.browser.ProgressEvent;
|
1721
|
37 |
import org.eclipse.swt.browser.WindowEvent;
|
1634
|
38 |
import org.eclipse.swt.layout.FillLayout;
|
|
39 |
import org.eclipse.swt.widgets.Composite;
|
|
40 |
import org.eclipse.swt.widgets.Control;
|
|
41 |
import org.eclipse.ui.IEditorPart;
|
|
42 |
|
|
43 |
import com.nokia.carbide.discovery.ui.Activator;
|
1661
|
44 |
import com.nokia.carbide.discovery.ui.Messages;
|
1721
|
45 |
import com.nokia.cpp.internal.api.utils.ui.WorkbenchUtils;
|
1634
|
46 |
|
1721
|
47 |
public abstract class AbstractBrowserPortalPageLayer implements IPortalPageLayer {
|
1634
|
48 |
|
1721
|
49 |
protected class BrowserAction extends Action {
|
|
50 |
|
|
51 |
public BrowserAction(String text) {
|
|
52 |
super(text);
|
|
53 |
}
|
|
54 |
|
|
55 |
@Override
|
|
56 |
public boolean isEnabled() {
|
|
57 |
return browser != null && !actionBar.isLoading();
|
|
58 |
}
|
|
59 |
}
|
|
60 |
|
|
61 |
protected class NavigationActionBar implements IActionBar {
|
|
62 |
private Set<IAction> actions;
|
1634
|
63 |
private IActionUIUpdater updater;
|
|
64 |
private boolean loading;
|
|
65 |
|
|
66 |
private NavigationActionBar(IActionUIUpdater updater) {
|
|
67 |
this.updater = updater;
|
1721
|
68 |
actions = makeActions();
|
1634
|
69 |
}
|
|
70 |
|
|
71 |
public void hookBrowser() {
|
|
72 |
browser.addLocationListener(new LocationAdapter() {
|
|
73 |
@Override
|
|
74 |
public void changed(LocationEvent event) {
|
|
75 |
NavigationActionBar.this.updater.updateAll();
|
|
76 |
}
|
|
77 |
});
|
|
78 |
browser.addProgressListener(new ProgressAdapter() {
|
|
79 |
@Override
|
|
80 |
public void changed(ProgressEvent event) {
|
|
81 |
if (event.total == 0)
|
|
82 |
return;
|
1653
|
83 |
setLoading(event.current != event.total);
|
1634
|
84 |
NavigationActionBar.this.updater.updateAll();
|
|
85 |
}
|
|
86 |
@Override
|
|
87 |
public void completed(ProgressEvent event) {
|
1653
|
88 |
setLoading(false);
|
1634
|
89 |
NavigationActionBar.this.updater.updateAll();
|
|
90 |
}
|
|
91 |
});
|
1721
|
92 |
browser.addOpenWindowListener(new OpenWindowListener() {
|
|
93 |
@Override
|
|
94 |
public void open(WindowEvent event) {
|
|
95 |
event.browser = browser;
|
|
96 |
}
|
|
97 |
});
|
1634
|
98 |
}
|
|
99 |
|
|
100 |
@Override
|
|
101 |
public String getTitle() {
|
1661
|
102 |
return Messages.AbstractBrowserPortalPage_NavigationTitle;
|
1634
|
103 |
}
|
|
104 |
|
|
105 |
@Override
|
|
106 |
public IAction[] getActions() {
|
|
107 |
return (IAction[]) actions.toArray(new IAction[actions.size()]);
|
|
108 |
}
|
|
109 |
|
|
110 |
public void update() {
|
|
111 |
updater.updateAll();
|
|
112 |
}
|
|
113 |
|
1734
|
114 |
public void setLoading(boolean loading) {
|
|
115 |
Activator.setBusyCursor(browser, loading);
|
1653
|
116 |
this.loading = loading;
|
|
117 |
}
|
1661
|
118 |
|
|
119 |
@Override
|
|
120 |
public String[] getHighlightedActionIds() {
|
|
121 |
return null;
|
|
122 |
}
|
1721
|
123 |
|
|
124 |
public boolean isLoading() {
|
|
125 |
return loading;
|
|
126 |
}
|
1634
|
127 |
}
|
|
128 |
|
1721
|
129 |
protected Browser browser;
|
|
130 |
protected NavigationActionBar actionBar;
|
1634
|
131 |
|
|
132 |
@Override
|
|
133 |
public Control createControl(Composite parent, IEditorPart part) {
|
|
134 |
Composite composite = new Composite(parent, SWT.NONE);
|
|
135 |
composite.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
|
|
136 |
composite.setLayout(new FillLayout());
|
|
137 |
try {
|
1637
|
138 |
browser = new Browser(composite, SWT.MOZILLA);
|
1634
|
139 |
} catch (SWTError e) {
|
1744
|
140 |
Activator.logError(Messages.AbstractBrowserPortalPageLayer_BrowserCreateError, e);
|
1634
|
141 |
}
|
|
142 |
|
|
143 |
return composite;
|
|
144 |
}
|
|
145 |
|
1721
|
146 |
protected URL getURL() {
|
1634
|
147 |
try {
|
1721
|
148 |
return new URL(Activator.getFromServerProperties(getClass().getName()));
|
1634
|
149 |
} catch (MalformedURLException e) {
|
1721
|
150 |
Activator.logError(Messages.AbstractBrowserPortalPageLayer_BadURLError, e);
|
1634
|
151 |
}
|
|
152 |
|
|
153 |
return null;
|
|
154 |
}
|
|
155 |
|
|
156 |
@Override
|
|
157 |
public void init() {
|
|
158 |
if (browser != null) {
|
1734
|
159 |
Activator.runInUIThreadWhenProxyDataSet(browser, new Runnable() {
|
|
160 |
@Override
|
|
161 |
public void run() {
|
|
162 |
URL url = getURL();
|
|
163 |
if (url != null) {
|
|
164 |
browser.setUrl(url.toString());
|
|
165 |
actionBar.setLoading(true);
|
|
166 |
}
|
|
167 |
actionBar.hookBrowser();
|
|
168 |
actionBar.update();
|
|
169 |
}
|
|
170 |
});
|
1634
|
171 |
}
|
|
172 |
}
|
|
173 |
|
|
174 |
@Override
|
|
175 |
public IActionBar[] createCommandBars(IEditorPart part, IActionUIUpdater updater) {
|
|
176 |
actionBar = new NavigationActionBar(updater);
|
|
177 |
return new IActionBar[] {
|
|
178 |
actionBar
|
|
179 |
};
|
|
180 |
}
|
|
181 |
|
|
182 |
@Override
|
|
183 |
public void dispose() {
|
|
184 |
}
|
1653
|
185 |
|
1721
|
186 |
protected Set<IAction> makeActions() {
|
|
187 |
Set<IAction> actions = new LinkedHashSet<IAction>();
|
|
188 |
IAction a = new BrowserAction(Messages.AbstractBrowserPortalPage_BackLabel) {
|
|
189 |
@Override
|
|
190 |
public void run() {
|
|
191 |
if (browser != null) {
|
|
192 |
browser.back();
|
|
193 |
}
|
|
194 |
}
|
|
195 |
|
|
196 |
@Override
|
|
197 |
public boolean isEnabled() {
|
|
198 |
return super.isEnabled() && browser.isBackEnabled();
|
|
199 |
}
|
|
200 |
};
|
|
201 |
actions.add(a);
|
|
202 |
|
|
203 |
a = new BrowserAction(Messages.AbstractBrowserPortalPageLayer_ForwardLabel) {
|
|
204 |
@Override
|
|
205 |
public void run() {
|
|
206 |
if (browser != null) {
|
|
207 |
browser.forward();
|
|
208 |
}
|
|
209 |
}
|
|
210 |
|
|
211 |
@Override
|
|
212 |
public boolean isEnabled() {
|
|
213 |
return super.isEnabled() && browser.isForwardEnabled();
|
|
214 |
}
|
|
215 |
};
|
|
216 |
actions.add(a);
|
|
217 |
|
|
218 |
a = new BrowserAction(Messages.AbstractBrowserPortalPage_RefreshLabel) {
|
|
219 |
@Override
|
|
220 |
public void run() {
|
|
221 |
if (browser != null) {
|
|
222 |
browser.refresh();
|
|
223 |
actionBar.update();
|
|
224 |
}
|
|
225 |
}
|
|
226 |
};
|
|
227 |
actions.add(a);
|
|
228 |
|
|
229 |
a = new Action(Messages.AbstractBrowserPortalPage_StopLabel) {
|
|
230 |
@Override
|
|
231 |
public void run() {
|
|
232 |
if (browser != null) {
|
|
233 |
browser.stop();
|
|
234 |
actionBar.update();
|
|
235 |
}
|
|
236 |
}
|
|
237 |
|
|
238 |
@Override
|
|
239 |
public boolean isEnabled() {
|
|
240 |
return browser != null && actionBar.isLoading();
|
|
241 |
}
|
|
242 |
};
|
|
243 |
actions.add(a);
|
|
244 |
|
|
245 |
a = new BrowserAction(Messages.AbstractBrowserPortalPageLayer_ShowInBrowserLabel) {
|
|
246 |
public void run() {
|
|
247 |
WorkbenchUtils.showWebPageInExternalBrowser(browser.getUrl());
|
|
248 |
};
|
|
249 |
|
|
250 |
public boolean isEnabled() {
|
|
251 |
return super.isEnabled() && browserHasURL();
|
|
252 |
};
|
|
253 |
};
|
|
254 |
actions.add(a);
|
|
255 |
|
|
256 |
a = new Action(Messages.AbstractBrowserPortalPageLayer_OpenLocationLabel) {
|
|
257 |
public void run() {
|
|
258 |
InputDialog dlg = new InputDialog(browser.getShell(),
|
|
259 |
Messages.AbstractBrowserPortalPageLayer_OpenLocationTitle, Messages.AbstractBrowserPortalPageLayer_URLLabel, null, new IInputValidator() {
|
|
260 |
@Override
|
|
261 |
public String isValid(String newText) {
|
|
262 |
try {
|
|
263 |
new URL(newText);
|
|
264 |
} catch (MalformedURLException e) {
|
|
265 |
return Messages.AbstractBrowserPortalPageLayer_InvalidURLError;
|
|
266 |
}
|
|
267 |
return null;
|
|
268 |
}
|
|
269 |
});
|
|
270 |
int result = dlg.open();
|
|
271 |
if (result == Dialog.OK) {
|
|
272 |
browser.setUrl(dlg.getValue());
|
|
273 |
actionBar.update();
|
|
274 |
}
|
|
275 |
};
|
|
276 |
|
|
277 |
public boolean isEnabled() {
|
|
278 |
return browser != null;
|
|
279 |
};
|
|
280 |
};
|
|
281 |
actions.add(a);
|
|
282 |
|
|
283 |
return actions;
|
|
284 |
}
|
|
285 |
|
|
286 |
protected boolean browserHasURL() {
|
|
287 |
String url = browser.getUrl();
|
|
288 |
return url.matches("^.*://.*"); //$NON-NLS-1$
|
|
289 |
}
|
1634
|
290 |
}
|