author | stechong |
Wed, 20 Oct 2010 11:19:31 -0500 | |
changeset 2165 | 2a7b5eccb0bc |
parent 2062 | 4d032dc346bf |
permissions | -rw-r--r-- |
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; |
|
1782
cd82069687e4
don't use error directly when logging to avoid dialog
dadubrow
parents:
1744
diff
changeset
|
21 |
import java.text.MessageFormat; |
1721 | 22 |
import java.util.LinkedHashSet; |
23 |
import java.util.Set; |
|
1634 | 24 |
|
25 |
import org.eclipse.jface.action.Action; |
|
26 |
import org.eclipse.jface.action.IAction; |
|
1832 | 27 |
import org.eclipse.jface.action.IStatusLineManager; |
1721 | 28 |
import org.eclipse.jface.dialogs.Dialog; |
29 |
import org.eclipse.jface.dialogs.IInputValidator; |
|
30 |
import org.eclipse.jface.dialogs.InputDialog; |
|
1634 | 31 |
import org.eclipse.swt.SWT; |
32 |
import org.eclipse.swt.browser.Browser; |
|
33 |
import org.eclipse.swt.browser.LocationAdapter; |
|
34 |
import org.eclipse.swt.browser.LocationEvent; |
|
1721 | 35 |
import org.eclipse.swt.browser.OpenWindowListener; |
1634 | 36 |
import org.eclipse.swt.browser.ProgressAdapter; |
37 |
import org.eclipse.swt.browser.ProgressEvent; |
|
1832 | 38 |
import org.eclipse.swt.browser.StatusTextEvent; |
39 |
import org.eclipse.swt.browser.StatusTextListener; |
|
1721 | 40 |
import org.eclipse.swt.browser.WindowEvent; |
1634 | 41 |
import org.eclipse.swt.layout.FillLayout; |
42 |
import org.eclipse.swt.widgets.Composite; |
|
43 |
import org.eclipse.swt.widgets.Control; |
|
44 |
import org.eclipse.ui.IEditorPart; |
|
45 |
||
46 |
import com.nokia.carbide.discovery.ui.Activator; |
|
1661 | 47 |
import com.nokia.carbide.discovery.ui.Messages; |
1721 | 48 |
import com.nokia.cpp.internal.api.utils.ui.WorkbenchUtils; |
1634 | 49 |
|
1721 | 50 |
public abstract class AbstractBrowserPortalPageLayer implements IPortalPageLayer { |
1634 | 51 |
|
1721 | 52 |
protected class BrowserAction extends Action { |
53 |
||
54 |
public BrowserAction(String text) { |
|
55 |
super(text); |
|
56 |
} |
|
57 |
||
58 |
@Override |
|
59 |
public boolean isEnabled() { |
|
60 |
return browser != null && !actionBar.isLoading(); |
|
61 |
} |
|
62 |
} |
|
63 |
||
64 |
protected class NavigationActionBar implements IActionBar { |
|
65 |
private Set<IAction> actions; |
|
1634 | 66 |
private IActionUIUpdater updater; |
67 |
private boolean loading; |
|
68 |
||
69 |
private NavigationActionBar(IActionUIUpdater updater) { |
|
70 |
this.updater = updater; |
|
1721 | 71 |
actions = makeActions(); |
1634 | 72 |
} |
73 |
||
74 |
public void hookBrowser() { |
|
75 |
browser.addLocationListener(new LocationAdapter() { |
|
76 |
@Override |
|
77 |
public void changed(LocationEvent event) { |
|
78 |
NavigationActionBar.this.updater.updateAll(); |
|
79 |
} |
|
80 |
}); |
|
81 |
browser.addProgressListener(new ProgressAdapter() { |
|
82 |
@Override |
|
83 |
public void changed(ProgressEvent event) { |
|
84 |
if (event.total == 0) |
|
85 |
return; |
|
1653 | 86 |
setLoading(event.current != event.total); |
1634 | 87 |
NavigationActionBar.this.updater.updateAll(); |
88 |
} |
|
89 |
@Override |
|
90 |
public void completed(ProgressEvent event) { |
|
1832 | 91 |
getStatusLineManager().setMessage(null); |
1849 | 92 |
if (!failedConnect && !isValidPage()) { |
1832 | 93 |
displayCannotFindServerPage(); |
1856 | 94 |
Activator.logError(Messages.AbstractBrowserPortalPageLayer_URLDisplayError + getURL().toExternalForm(), null); |
1832 | 95 |
failedConnect = true; |
96 |
} |
|
1653 | 97 |
setLoading(false); |
1634 | 98 |
NavigationActionBar.this.updater.updateAll(); |
99 |
} |
|
100 |
}); |
|
1721 | 101 |
browser.addOpenWindowListener(new OpenWindowListener() { |
102 |
@Override |
|
103 |
public void open(WindowEvent event) { |
|
104 |
event.browser = browser; |
|
105 |
} |
|
106 |
}); |
|
1832 | 107 |
browser.addStatusTextListener(new StatusTextListener() { |
108 |
@Override |
|
109 |
public void changed(StatusTextEvent event) { |
|
110 |
getStatusLineManager().setMessage(event.text); |
|
111 |
} |
|
112 |
}); |
|
1634 | 113 |
} |
114 |
||
115 |
@Override |
|
116 |
public String getTitle() { |
|
1661 | 117 |
return Messages.AbstractBrowserPortalPage_NavigationTitle; |
1634 | 118 |
} |
119 |
||
120 |
@Override |
|
121 |
public IAction[] getActions() { |
|
122 |
return (IAction[]) actions.toArray(new IAction[actions.size()]); |
|
123 |
} |
|
124 |
||
125 |
public void update() { |
|
126 |
updater.updateAll(); |
|
127 |
} |
|
128 |
||
1734 | 129 |
public void setLoading(boolean loading) { |
130 |
Activator.setBusyCursor(browser, loading); |
|
1653 | 131 |
this.loading = loading; |
132 |
} |
|
1661 | 133 |
|
134 |
@Override |
|
135 |
public String[] getHighlightedActionIds() { |
|
136 |
return null; |
|
137 |
} |
|
1721 | 138 |
|
139 |
public boolean isLoading() { |
|
140 |
return loading; |
|
141 |
} |
|
1634 | 142 |
} |
143 |
||
1832 | 144 |
protected static final String HTML_BODY_HEADER = "<html><head><title></title><style type=\"text/css\">div.item {font-family : sans-serif; font-size : 12px; margin-bottom : 16px;} div.itemBody {padding-top : 3px; padding-bottom : 3px;} div.itemInfo {background-color : #EEEEEE; color : #333333;} div.feedflare {display: none;} a.itemTitle {font-size : 12px; font-weight : bold;} a.markItemRead {font-size : 10px; color : #333333;}</style></head><body>"; //$NON-NLS-1$ |
145 |
protected static final String HTML_BODY_FOOTER = "</body></html>"; //$NON-NLS-1$ |
|
146 |
||
1721 | 147 |
protected Browser browser; |
148 |
protected NavigationActionBar actionBar; |
|
1832 | 149 |
private IEditorPart part; |
150 |
private boolean failedConnect; |
|
1634 | 151 |
|
152 |
@Override |
|
153 |
public Control createControl(Composite parent, IEditorPart part) { |
|
1832 | 154 |
this.part = part; |
1634 | 155 |
Composite composite = new Composite(parent, SWT.NONE); |
156 |
composite.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND)); |
|
157 |
composite.setLayout(new FillLayout()); |
|
158 |
try { |
|
1637 | 159 |
browser = new Browser(composite, SWT.MOZILLA); |
1783 | 160 |
} catch (Throwable e1) { |
161 |
try { |
|
162 |
// try creating regular browser |
|
163 |
browser = new Browser(composite, SWT.NONE); |
|
164 |
} catch (Throwable e2) { |
|
165 |
// don't log with Throwable directly because it may display a dialog |
|
166 |
Activator.logError(MessageFormat.format(Messages.AbstractBrowserPortalPageLayer_BrowserCreateError, e2.getMessage()), null); |
|
167 |
} |
|
1634 | 168 |
} |
169 |
||
170 |
return composite; |
|
171 |
} |
|
172 |
||
1721 | 173 |
protected URL getURL() { |
1634 | 174 |
try { |
1721 | 175 |
return new URL(Activator.getFromServerProperties(getClass().getName())); |
1634 | 176 |
} catch (MalformedURLException e) { |
1721 | 177 |
Activator.logError(Messages.AbstractBrowserPortalPageLayer_BadURLError, e); |
1634 | 178 |
} |
179 |
||
180 |
return null; |
|
181 |
} |
|
182 |
||
183 |
@Override |
|
184 |
public void init() { |
|
185 |
if (browser != null) { |
|
1734 | 186 |
Activator.runInUIThreadWhenProxyDataSet(browser, new Runnable() { |
187 |
@Override |
|
188 |
public void run() { |
|
189 |
URL url = getURL(); |
|
190 |
if (url != null) { |
|
1849 | 191 |
setUrl(url.toString()); |
1734 | 192 |
} |
193 |
actionBar.hookBrowser(); |
|
194 |
actionBar.update(); |
|
1822 | 195 |
browser.setFocus(); |
1734 | 196 |
} |
197 |
}); |
|
1634 | 198 |
} |
199 |
} |
|
200 |
||
201 |
@Override |
|
202 |
public IActionBar[] createCommandBars(IEditorPart part, IActionUIUpdater updater) { |
|
203 |
actionBar = new NavigationActionBar(updater); |
|
204 |
return new IActionBar[] { |
|
205 |
actionBar |
|
206 |
}; |
|
207 |
} |
|
208 |
||
209 |
@Override |
|
210 |
public void dispose() { |
|
211 |
} |
|
1653 | 212 |
|
1721 | 213 |
protected Set<IAction> makeActions() { |
214 |
Set<IAction> actions = new LinkedHashSet<IAction>(); |
|
215 |
IAction a = new BrowserAction(Messages.AbstractBrowserPortalPage_BackLabel) { |
|
216 |
@Override |
|
217 |
public void run() { |
|
218 |
if (browser != null) { |
|
219 |
browser.back(); |
|
220 |
} |
|
221 |
} |
|
222 |
||
223 |
@Override |
|
224 |
public boolean isEnabled() { |
|
225 |
return super.isEnabled() && browser.isBackEnabled(); |
|
226 |
} |
|
227 |
}; |
|
228 |
actions.add(a); |
|
229 |
||
230 |
a = new BrowserAction(Messages.AbstractBrowserPortalPageLayer_ForwardLabel) { |
|
231 |
@Override |
|
232 |
public void run() { |
|
233 |
if (browser != null) { |
|
234 |
browser.forward(); |
|
235 |
} |
|
236 |
} |
|
237 |
||
238 |
@Override |
|
239 |
public boolean isEnabled() { |
|
240 |
return super.isEnabled() && browser.isForwardEnabled(); |
|
241 |
} |
|
242 |
}; |
|
243 |
actions.add(a); |
|
244 |
||
245 |
a = new BrowserAction(Messages.AbstractBrowserPortalPage_RefreshLabel) { |
|
246 |
@Override |
|
247 |
public void run() { |
|
248 |
if (browser != null) { |
|
1849 | 249 |
if (browserHasURL()) |
250 |
browser.refresh(); |
|
251 |
else { |
|
1832 | 252 |
URL url = getURL(); |
253 |
if (url != null) { |
|
1849 | 254 |
setUrl(url.toString()); |
1832 | 255 |
} |
256 |
} |
|
1721 | 257 |
actionBar.update(); |
258 |
} |
|
259 |
} |
|
260 |
}; |
|
261 |
actions.add(a); |
|
262 |
||
263 |
a = new Action(Messages.AbstractBrowserPortalPage_StopLabel) { |
|
264 |
@Override |
|
265 |
public void run() { |
|
266 |
if (browser != null) { |
|
267 |
browser.stop(); |
|
268 |
actionBar.update(); |
|
269 |
} |
|
270 |
} |
|
271 |
||
272 |
@Override |
|
273 |
public boolean isEnabled() { |
|
274 |
return browser != null && actionBar.isLoading(); |
|
275 |
} |
|
276 |
}; |
|
277 |
actions.add(a); |
|
278 |
||
279 |
a = new BrowserAction(Messages.AbstractBrowserPortalPageLayer_ShowInBrowserLabel) { |
|
280 |
public void run() { |
|
281 |
WorkbenchUtils.showWebPageInExternalBrowser(browser.getUrl()); |
|
282 |
}; |
|
283 |
||
284 |
public boolean isEnabled() { |
|
285 |
return super.isEnabled() && browserHasURL(); |
|
286 |
}; |
|
287 |
}; |
|
288 |
actions.add(a); |
|
289 |
||
290 |
a = new Action(Messages.AbstractBrowserPortalPageLayer_OpenLocationLabel) { |
|
291 |
public void run() { |
|
292 |
InputDialog dlg = new InputDialog(browser.getShell(), |
|
293 |
Messages.AbstractBrowserPortalPageLayer_OpenLocationTitle, Messages.AbstractBrowserPortalPageLayer_URLLabel, null, new IInputValidator() { |
|
294 |
@Override |
|
295 |
public String isValid(String newText) { |
|
296 |
try { |
|
2002 | 297 |
new URL(getLocationURL(newText)); |
1721 | 298 |
} catch (MalformedURLException e) { |
299 |
return Messages.AbstractBrowserPortalPageLayer_InvalidURLError; |
|
300 |
} |
|
301 |
return null; |
|
302 |
} |
|
303 |
}); |
|
304 |
int result = dlg.open(); |
|
305 |
if (result == Dialog.OK) { |
|
2002 | 306 |
String value = dlg.getValue(); |
307 |
setUrl(getLocationURL(value)); |
|
1721 | 308 |
actionBar.update(); |
309 |
} |
|
2002 | 310 |
} |
311 |
||
312 |
private String getLocationURL(String value) { |
|
313 |
try { |
|
314 |
new URL(value); |
|
315 |
} catch (MalformedURLException e) { |
|
2062 | 316 |
value = "http://" + value; //$NON-NLS-1$ |
2002 | 317 |
} |
318 |
return value; |
|
1721 | 319 |
}; |
320 |
||
321 |
public boolean isEnabled() { |
|
322 |
return browser != null; |
|
323 |
}; |
|
324 |
}; |
|
325 |
actions.add(a); |
|
326 |
||
327 |
return actions; |
|
328 |
} |
|
329 |
||
330 |
protected boolean browserHasURL() { |
|
331 |
String url = browser.getUrl(); |
|
332 |
return url.matches("^.*://.*"); //$NON-NLS-1$ |
|
333 |
} |
|
1832 | 334 |
|
1849 | 335 |
protected boolean isValidPage() { |
336 |
return browserHasURL(); |
|
337 |
} |
|
338 |
||
339 |
protected void setUrl(String url) { |
|
340 |
browser.setUrl(url); |
|
341 |
failedConnect = false; |
|
342 |
} |
|
343 |
||
1832 | 344 |
protected void displayCannotFindServerPage() { |
345 |
StringBuilder sb = new StringBuilder(HTML_BODY_HEADER); |
|
346 |
// if we want, we can add html here for failed connect case |
|
347 |
sb.append(HTML_BODY_FOOTER); |
|
348 |
browser.setText(sb.toString()); |
|
349 |
browser.redraw(); |
|
350 |
} |
|
351 |
||
352 |
private IStatusLineManager getStatusLineManager() { |
|
353 |
return part.getEditorSite().getActionBars().getStatusLineManager(); |
|
354 |
} |
|
1634 | 355 |
} |