author | dadubrow |
Fri, 06 Aug 2010 08:18:06 -0500 | |
changeset 1782 | cd82069687e4 |
parent 1744 | 48b623de4c8e |
child 1783 | 6d8af00d456a |
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.SWTError; |
|
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; |
|
1721 | 38 |
import org.eclipse.swt.browser.WindowEvent; |
1634 | 39 |
import org.eclipse.swt.layout.FillLayout; |
40 |
import org.eclipse.swt.widgets.Composite; |
|
41 |
import org.eclipse.swt.widgets.Control; |
|
42 |
import org.eclipse.ui.IEditorPart; |
|
43 |
||
44 |
import com.nokia.carbide.discovery.ui.Activator; |
|
1661 | 45 |
import com.nokia.carbide.discovery.ui.Messages; |
1721 | 46 |
import com.nokia.cpp.internal.api.utils.ui.WorkbenchUtils; |
1634 | 47 |
|
1721 | 48 |
public abstract class AbstractBrowserPortalPageLayer implements IPortalPageLayer { |
1634 | 49 |
|
1721 | 50 |
protected class BrowserAction extends Action { |
51 |
||
52 |
public BrowserAction(String text) { |
|
53 |
super(text); |
|
54 |
} |
|
55 |
||
56 |
@Override |
|
57 |
public boolean isEnabled() { |
|
58 |
return browser != null && !actionBar.isLoading(); |
|
59 |
} |
|
60 |
} |
|
61 |
||
62 |
protected class NavigationActionBar implements IActionBar { |
|
63 |
private Set<IAction> actions; |
|
1634 | 64 |
private IActionUIUpdater updater; |
65 |
private boolean loading; |
|
66 |
||
67 |
private NavigationActionBar(IActionUIUpdater updater) { |
|
68 |
this.updater = updater; |
|
1721 | 69 |
actions = makeActions(); |
1634 | 70 |
} |
71 |
||
72 |
public void hookBrowser() { |
|
73 |
browser.addLocationListener(new LocationAdapter() { |
|
74 |
@Override |
|
75 |
public void changed(LocationEvent event) { |
|
76 |
NavigationActionBar.this.updater.updateAll(); |
|
77 |
} |
|
78 |
}); |
|
79 |
browser.addProgressListener(new ProgressAdapter() { |
|
80 |
@Override |
|
81 |
public void changed(ProgressEvent event) { |
|
82 |
if (event.total == 0) |
|
83 |
return; |
|
1653 | 84 |
setLoading(event.current != event.total); |
1634 | 85 |
NavigationActionBar.this.updater.updateAll(); |
86 |
} |
|
87 |
@Override |
|
88 |
public void completed(ProgressEvent event) { |
|
1653 | 89 |
setLoading(false); |
1634 | 90 |
NavigationActionBar.this.updater.updateAll(); |
91 |
} |
|
92 |
}); |
|
1721 | 93 |
browser.addOpenWindowListener(new OpenWindowListener() { |
94 |
@Override |
|
95 |
public void open(WindowEvent event) { |
|
96 |
event.browser = browser; |
|
97 |
} |
|
98 |
}); |
|
1634 | 99 |
} |
100 |
||
101 |
@Override |
|
102 |
public String getTitle() { |
|
1661 | 103 |
return Messages.AbstractBrowserPortalPage_NavigationTitle; |
1634 | 104 |
} |
105 |
||
106 |
@Override |
|
107 |
public IAction[] getActions() { |
|
108 |
return (IAction[]) actions.toArray(new IAction[actions.size()]); |
|
109 |
} |
|
110 |
||
111 |
public void update() { |
|
112 |
updater.updateAll(); |
|
113 |
} |
|
114 |
||
1734 | 115 |
public void setLoading(boolean loading) { |
116 |
Activator.setBusyCursor(browser, loading); |
|
1653 | 117 |
this.loading = loading; |
118 |
} |
|
1661 | 119 |
|
120 |
@Override |
|
121 |
public String[] getHighlightedActionIds() { |
|
122 |
return null; |
|
123 |
} |
|
1721 | 124 |
|
125 |
public boolean isLoading() { |
|
126 |
return loading; |
|
127 |
} |
|
1634 | 128 |
} |
129 |
||
1721 | 130 |
protected Browser browser; |
131 |
protected NavigationActionBar actionBar; |
|
1634 | 132 |
|
133 |
@Override |
|
134 |
public Control createControl(Composite parent, IEditorPart part) { |
|
135 |
Composite composite = new Composite(parent, SWT.NONE); |
|
136 |
composite.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND)); |
|
137 |
composite.setLayout(new FillLayout()); |
|
138 |
try { |
|
1637 | 139 |
browser = new Browser(composite, SWT.MOZILLA); |
1634 | 140 |
} catch (SWTError e) { |
1782
cd82069687e4
don't use error directly when logging to avoid dialog
dadubrow
parents:
1744
diff
changeset
|
141 |
// don't log with SWTError as Throwable because it displays a dialog and this may occur and be benign |
cd82069687e4
don't use error directly when logging to avoid dialog
dadubrow
parents:
1744
diff
changeset
|
142 |
Activator.logError(MessageFormat.format(Messages.AbstractBrowserPortalPageLayer_BrowserCreateError, e.getMessage()), null); |
1634 | 143 |
} |
144 |
||
145 |
return composite; |
|
146 |
} |
|
147 |
||
1721 | 148 |
protected URL getURL() { |
1634 | 149 |
try { |
1721 | 150 |
return new URL(Activator.getFromServerProperties(getClass().getName())); |
1634 | 151 |
} catch (MalformedURLException e) { |
1721 | 152 |
Activator.logError(Messages.AbstractBrowserPortalPageLayer_BadURLError, e); |
1634 | 153 |
} |
154 |
||
155 |
return null; |
|
156 |
} |
|
157 |
||
158 |
@Override |
|
159 |
public void init() { |
|
160 |
if (browser != null) { |
|
1734 | 161 |
Activator.runInUIThreadWhenProxyDataSet(browser, new Runnable() { |
162 |
@Override |
|
163 |
public void run() { |
|
164 |
URL url = getURL(); |
|
165 |
if (url != null) { |
|
166 |
browser.setUrl(url.toString()); |
|
167 |
actionBar.setLoading(true); |
|
168 |
} |
|
169 |
actionBar.hookBrowser(); |
|
170 |
actionBar.update(); |
|
171 |
} |
|
172 |
}); |
|
1634 | 173 |
} |
174 |
} |
|
175 |
||
176 |
@Override |
|
177 |
public IActionBar[] createCommandBars(IEditorPart part, IActionUIUpdater updater) { |
|
178 |
actionBar = new NavigationActionBar(updater); |
|
179 |
return new IActionBar[] { |
|
180 |
actionBar |
|
181 |
}; |
|
182 |
} |
|
183 |
||
184 |
@Override |
|
185 |
public void dispose() { |
|
186 |
} |
|
1653 | 187 |
|
1721 | 188 |
protected Set<IAction> makeActions() { |
189 |
Set<IAction> actions = new LinkedHashSet<IAction>(); |
|
190 |
IAction a = new BrowserAction(Messages.AbstractBrowserPortalPage_BackLabel) { |
|
191 |
@Override |
|
192 |
public void run() { |
|
193 |
if (browser != null) { |
|
194 |
browser.back(); |
|
195 |
} |
|
196 |
} |
|
197 |
||
198 |
@Override |
|
199 |
public boolean isEnabled() { |
|
200 |
return super.isEnabled() && browser.isBackEnabled(); |
|
201 |
} |
|
202 |
}; |
|
203 |
actions.add(a); |
|
204 |
||
205 |
a = new BrowserAction(Messages.AbstractBrowserPortalPageLayer_ForwardLabel) { |
|
206 |
@Override |
|
207 |
public void run() { |
|
208 |
if (browser != null) { |
|
209 |
browser.forward(); |
|
210 |
} |
|
211 |
} |
|
212 |
||
213 |
@Override |
|
214 |
public boolean isEnabled() { |
|
215 |
return super.isEnabled() && browser.isForwardEnabled(); |
|
216 |
} |
|
217 |
}; |
|
218 |
actions.add(a); |
|
219 |
||
220 |
a = new BrowserAction(Messages.AbstractBrowserPortalPage_RefreshLabel) { |
|
221 |
@Override |
|
222 |
public void run() { |
|
223 |
if (browser != null) { |
|
224 |
browser.refresh(); |
|
225 |
actionBar.update(); |
|
226 |
} |
|
227 |
} |
|
228 |
}; |
|
229 |
actions.add(a); |
|
230 |
||
231 |
a = new Action(Messages.AbstractBrowserPortalPage_StopLabel) { |
|
232 |
@Override |
|
233 |
public void run() { |
|
234 |
if (browser != null) { |
|
235 |
browser.stop(); |
|
236 |
actionBar.update(); |
|
237 |
} |
|
238 |
} |
|
239 |
||
240 |
@Override |
|
241 |
public boolean isEnabled() { |
|
242 |
return browser != null && actionBar.isLoading(); |
|
243 |
} |
|
244 |
}; |
|
245 |
actions.add(a); |
|
246 |
||
247 |
a = new BrowserAction(Messages.AbstractBrowserPortalPageLayer_ShowInBrowserLabel) { |
|
248 |
public void run() { |
|
249 |
WorkbenchUtils.showWebPageInExternalBrowser(browser.getUrl()); |
|
250 |
}; |
|
251 |
||
252 |
public boolean isEnabled() { |
|
253 |
return super.isEnabled() && browserHasURL(); |
|
254 |
}; |
|
255 |
}; |
|
256 |
actions.add(a); |
|
257 |
||
258 |
a = new Action(Messages.AbstractBrowserPortalPageLayer_OpenLocationLabel) { |
|
259 |
public void run() { |
|
260 |
InputDialog dlg = new InputDialog(browser.getShell(), |
|
261 |
Messages.AbstractBrowserPortalPageLayer_OpenLocationTitle, Messages.AbstractBrowserPortalPageLayer_URLLabel, null, new IInputValidator() { |
|
262 |
@Override |
|
263 |
public String isValid(String newText) { |
|
264 |
try { |
|
265 |
new URL(newText); |
|
266 |
} catch (MalformedURLException e) { |
|
267 |
return Messages.AbstractBrowserPortalPageLayer_InvalidURLError; |
|
268 |
} |
|
269 |
return null; |
|
270 |
} |
|
271 |
}); |
|
272 |
int result = dlg.open(); |
|
273 |
if (result == Dialog.OK) { |
|
274 |
browser.setUrl(dlg.getValue()); |
|
275 |
actionBar.update(); |
|
276 |
} |
|
277 |
}; |
|
278 |
||
279 |
public boolean isEnabled() { |
|
280 |
return browser != null; |
|
281 |
}; |
|
282 |
}; |
|
283 |
actions.add(a); |
|
284 |
||
285 |
return actions; |
|
286 |
} |
|
287 |
||
288 |
protected boolean browserHasURL() { |
|
289 |
String url = browser.getUrl(); |
|
290 |
return url.matches("^.*://.*"); //$NON-NLS-1$ |
|
291 |
} |
|
1634 | 292 |
} |