author | dadubrow |
Fri, 06 Aug 2010 08:39:21 -0500 | |
changeset 1783 | 6d8af00d456a |
parent 1782 | cd82069687e4 |
child 1822 | 8fdfd1446ad7 |
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; |
|
1721 | 27 |
import org.eclipse.jface.dialogs.Dialog; |
28 |
import org.eclipse.jface.dialogs.IInputValidator; |
|
29 |
import org.eclipse.jface.dialogs.InputDialog; |
|
1634 | 30 |
import org.eclipse.swt.SWT; |
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); |
1783 | 139 |
} catch (Throwable e1) { |
140 |
try { |
|
141 |
// try creating regular browser |
|
142 |
browser = new Browser(composite, SWT.NONE); |
|
143 |
} catch (Throwable e2) { |
|
144 |
// don't log with Throwable directly because it may display a dialog |
|
145 |
Activator.logError(MessageFormat.format(Messages.AbstractBrowserPortalPageLayer_BrowserCreateError, e2.getMessage()), null); |
|
146 |
} |
|
1634 | 147 |
} |
148 |
||
149 |
return composite; |
|
150 |
} |
|
151 |
||
1721 | 152 |
protected URL getURL() { |
1634 | 153 |
try { |
1721 | 154 |
return new URL(Activator.getFromServerProperties(getClass().getName())); |
1634 | 155 |
} catch (MalformedURLException e) { |
1721 | 156 |
Activator.logError(Messages.AbstractBrowserPortalPageLayer_BadURLError, e); |
1634 | 157 |
} |
158 |
||
159 |
return null; |
|
160 |
} |
|
161 |
||
162 |
@Override |
|
163 |
public void init() { |
|
164 |
if (browser != null) { |
|
1734 | 165 |
Activator.runInUIThreadWhenProxyDataSet(browser, new Runnable() { |
166 |
@Override |
|
167 |
public void run() { |
|
168 |
URL url = getURL(); |
|
169 |
if (url != null) { |
|
170 |
browser.setUrl(url.toString()); |
|
171 |
actionBar.setLoading(true); |
|
172 |
} |
|
173 |
actionBar.hookBrowser(); |
|
174 |
actionBar.update(); |
|
175 |
} |
|
176 |
}); |
|
1634 | 177 |
} |
178 |
} |
|
179 |
||
180 |
@Override |
|
181 |
public IActionBar[] createCommandBars(IEditorPart part, IActionUIUpdater updater) { |
|
182 |
actionBar = new NavigationActionBar(updater); |
|
183 |
return new IActionBar[] { |
|
184 |
actionBar |
|
185 |
}; |
|
186 |
} |
|
187 |
||
188 |
@Override |
|
189 |
public void dispose() { |
|
190 |
} |
|
1653 | 191 |
|
1721 | 192 |
protected Set<IAction> makeActions() { |
193 |
Set<IAction> actions = new LinkedHashSet<IAction>(); |
|
194 |
IAction a = new BrowserAction(Messages.AbstractBrowserPortalPage_BackLabel) { |
|
195 |
@Override |
|
196 |
public void run() { |
|
197 |
if (browser != null) { |
|
198 |
browser.back(); |
|
199 |
} |
|
200 |
} |
|
201 |
||
202 |
@Override |
|
203 |
public boolean isEnabled() { |
|
204 |
return super.isEnabled() && browser.isBackEnabled(); |
|
205 |
} |
|
206 |
}; |
|
207 |
actions.add(a); |
|
208 |
||
209 |
a = new BrowserAction(Messages.AbstractBrowserPortalPageLayer_ForwardLabel) { |
|
210 |
@Override |
|
211 |
public void run() { |
|
212 |
if (browser != null) { |
|
213 |
browser.forward(); |
|
214 |
} |
|
215 |
} |
|
216 |
||
217 |
@Override |
|
218 |
public boolean isEnabled() { |
|
219 |
return super.isEnabled() && browser.isForwardEnabled(); |
|
220 |
} |
|
221 |
}; |
|
222 |
actions.add(a); |
|
223 |
||
224 |
a = new BrowserAction(Messages.AbstractBrowserPortalPage_RefreshLabel) { |
|
225 |
@Override |
|
226 |
public void run() { |
|
227 |
if (browser != null) { |
|
228 |
browser.refresh(); |
|
229 |
actionBar.update(); |
|
230 |
} |
|
231 |
} |
|
232 |
}; |
|
233 |
actions.add(a); |
|
234 |
||
235 |
a = new Action(Messages.AbstractBrowserPortalPage_StopLabel) { |
|
236 |
@Override |
|
237 |
public void run() { |
|
238 |
if (browser != null) { |
|
239 |
browser.stop(); |
|
240 |
actionBar.update(); |
|
241 |
} |
|
242 |
} |
|
243 |
||
244 |
@Override |
|
245 |
public boolean isEnabled() { |
|
246 |
return browser != null && actionBar.isLoading(); |
|
247 |
} |
|
248 |
}; |
|
249 |
actions.add(a); |
|
250 |
||
251 |
a = new BrowserAction(Messages.AbstractBrowserPortalPageLayer_ShowInBrowserLabel) { |
|
252 |
public void run() { |
|
253 |
WorkbenchUtils.showWebPageInExternalBrowser(browser.getUrl()); |
|
254 |
}; |
|
255 |
||
256 |
public boolean isEnabled() { |
|
257 |
return super.isEnabled() && browserHasURL(); |
|
258 |
}; |
|
259 |
}; |
|
260 |
actions.add(a); |
|
261 |
||
262 |
a = new Action(Messages.AbstractBrowserPortalPageLayer_OpenLocationLabel) { |
|
263 |
public void run() { |
|
264 |
InputDialog dlg = new InputDialog(browser.getShell(), |
|
265 |
Messages.AbstractBrowserPortalPageLayer_OpenLocationTitle, Messages.AbstractBrowserPortalPageLayer_URLLabel, null, new IInputValidator() { |
|
266 |
@Override |
|
267 |
public String isValid(String newText) { |
|
268 |
try { |
|
269 |
new URL(newText); |
|
270 |
} catch (MalformedURLException e) { |
|
271 |
return Messages.AbstractBrowserPortalPageLayer_InvalidURLError; |
|
272 |
} |
|
273 |
return null; |
|
274 |
} |
|
275 |
}); |
|
276 |
int result = dlg.open(); |
|
277 |
if (result == Dialog.OK) { |
|
278 |
browser.setUrl(dlg.getValue()); |
|
279 |
actionBar.update(); |
|
280 |
} |
|
281 |
}; |
|
282 |
||
283 |
public boolean isEnabled() { |
|
284 |
return browser != null; |
|
285 |
}; |
|
286 |
}; |
|
287 |
actions.add(a); |
|
288 |
||
289 |
return actions; |
|
290 |
} |
|
291 |
||
292 |
protected boolean browserHasURL() { |
|
293 |
String url = browser.getUrl(); |
|
294 |
return url.matches("^.*://.*"); //$NON-NLS-1$ |
|
295 |
} |
|
1634 | 296 |
} |