1609
|
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.editor;
|
|
18 |
|
1616
|
19 |
import java.text.MessageFormat;
|
1609
|
20 |
import java.util.ArrayList;
|
1616
|
21 |
import java.util.Collections;
|
|
22 |
import java.util.Comparator;
|
1609
|
23 |
import java.util.HashMap;
|
|
24 |
import java.util.List;
|
|
25 |
import java.util.Map;
|
|
26 |
|
|
27 |
import org.eclipse.core.runtime.CoreException;
|
|
28 |
import org.eclipse.core.runtime.IConfigurationElement;
|
|
29 |
import org.eclipse.core.runtime.IProgressMonitor;
|
|
30 |
import org.eclipse.core.runtime.Platform;
|
|
31 |
import org.eclipse.jface.layout.GridDataFactory;
|
|
32 |
import org.eclipse.jface.layout.GridLayoutFactory;
|
1618
|
33 |
import org.eclipse.jface.resource.FontDescriptor;
|
1609
|
34 |
import org.eclipse.jface.resource.ImageDescriptor;
|
|
35 |
import org.eclipse.swt.SWT;
|
|
36 |
import org.eclipse.swt.graphics.Font;
|
|
37 |
import org.eclipse.swt.graphics.GC;
|
|
38 |
import org.eclipse.swt.graphics.Image;
|
1618
|
39 |
import org.eclipse.swt.graphics.ImageData;
|
1609
|
40 |
import org.eclipse.swt.graphics.Rectangle;
|
|
41 |
import org.eclipse.swt.graphics.Resource;
|
|
42 |
import org.eclipse.swt.widgets.Composite;
|
|
43 |
import org.eclipse.swt.widgets.Control;
|
|
44 |
import org.eclipse.swt.widgets.Display;
|
|
45 |
import org.eclipse.swt.widgets.Event;
|
|
46 |
import org.eclipse.swt.widgets.Listener;
|
|
47 |
import org.eclipse.ui.IEditorInput;
|
|
48 |
import org.eclipse.ui.IEditorSite;
|
|
49 |
import org.eclipse.ui.IPersistableElement;
|
|
50 |
import org.eclipse.ui.PartInitException;
|
|
51 |
import org.eclipse.ui.PlatformUI;
|
|
52 |
import org.eclipse.ui.part.EditorPart;
|
|
53 |
|
|
54 |
import com.nokia.carbide.discovery.ui.Activator;
|
1615
|
55 |
import com.nokia.carbide.discovery.ui.Messages;
|
1609
|
56 |
import com.nokia.carbide.internal.discovery.ui.extension.IPortalPage;
|
|
57 |
import com.nokia.carbide.internal.discovery.ui.extension.IPortalPage.IActionBar;
|
1616
|
58 |
import com.nokia.cpp.internal.api.utils.core.Pair;
|
1609
|
59 |
import com.nokia.cpp.internal.api.utils.ui.WorkbenchUtils;
|
|
60 |
|
|
61 |
public class PortalEditor extends EditorPart {
|
|
62 |
|
|
63 |
private static final String ID = "com.nokia.carbide.discovery.ui.portalEditor"; //$NON-NLS-1$
|
|
64 |
private static final String CONTEXT_ID = ID + ".context"; //$NON-NLS-1$
|
|
65 |
private static IEditorInput input;
|
|
66 |
private List<IPortalPage> uninitializedPages;
|
|
67 |
private Composite backgroundParent;
|
|
68 |
private Image oldBGImg;
|
|
69 |
private List<Resource> resources;
|
|
70 |
private StackComposite stackComposite;
|
|
71 |
|
|
72 |
private Map<IPortalPage, Control> pageToControlMap;
|
|
73 |
private NavigationBar navigationBar;
|
|
74 |
|
|
75 |
public PortalEditor() {
|
|
76 |
resources = new ArrayList<Resource>();
|
1616
|
77 |
loadPortalPages();
|
1609
|
78 |
pageToControlMap = new HashMap<IPortalPage, Control>();
|
|
79 |
}
|
|
80 |
|
1616
|
81 |
private void loadPortalPages() {
|
|
82 |
List<Pair<IPortalPage, Integer>> pageExtensions = new ArrayList<Pair<IPortalPage,Integer>>();
|
1609
|
83 |
IConfigurationElement[] elements =
|
1615
|
84 |
Platform.getExtensionRegistry().getConfigurationElementsFor(Activator.PLUGIN_ID + ".portalPage"); //$NON-NLS-1$
|
1609
|
85 |
for (IConfigurationElement element : elements) {
|
|
86 |
try {
|
1616
|
87 |
IPortalPage portalPage = (IPortalPage) element.createExecutableExtension("class"); //$NON-NLS-1$
|
|
88 |
String rankString = element.getAttribute("rank"); //$NON-NLS-1$
|
|
89 |
int rank = Integer.MAX_VALUE;
|
|
90 |
if (rankString != null) {
|
|
91 |
try {
|
|
92 |
rank = Integer.parseInt(rankString);
|
|
93 |
}
|
|
94 |
catch (NumberFormatException e) {
|
1661
|
95 |
Activator.logError(MessageFormat.format(Messages.PortalEditor_PageRankError,
|
1616
|
96 |
portalPage.getClass().getName()), e);
|
|
97 |
}
|
|
98 |
}
|
|
99 |
pageExtensions.add(new Pair<IPortalPage, Integer>(portalPage, rank));
|
1609
|
100 |
}
|
|
101 |
catch (CoreException e) {
|
1615
|
102 |
Activator.logError(Messages.PortalEditor_PageLoadError, e);
|
1609
|
103 |
}
|
|
104 |
}
|
1616
|
105 |
Collections.sort(pageExtensions, new Comparator<Pair<IPortalPage, Integer>>() {
|
|
106 |
@Override
|
|
107 |
public int compare(Pair<IPortalPage, Integer> o1, Pair<IPortalPage, Integer> o2) {
|
|
108 |
return o1.second.compareTo(o2.second);
|
|
109 |
}
|
|
110 |
});
|
|
111 |
uninitializedPages = new ArrayList<IPortalPage>();
|
|
112 |
for (Pair<IPortalPage, Integer> pair : pageExtensions) {
|
|
113 |
uninitializedPages.add(pair.first);
|
|
114 |
}
|
1609
|
115 |
}
|
|
116 |
|
|
117 |
@Override
|
|
118 |
public void doSave(IProgressMonitor monitor) {
|
|
119 |
}
|
|
120 |
|
|
121 |
@Override
|
|
122 |
public void doSaveAs() {
|
|
123 |
}
|
|
124 |
|
|
125 |
@Override
|
|
126 |
public boolean isDirty() {
|
|
127 |
return false;
|
|
128 |
}
|
|
129 |
|
|
130 |
@Override
|
|
131 |
public boolean isSaveAsAllowed() {
|
|
132 |
return false;
|
|
133 |
}
|
|
134 |
|
|
135 |
@Override
|
|
136 |
public void init(IEditorSite site, IEditorInput input) throws PartInitException {
|
|
137 |
setSite(site);
|
|
138 |
setInput(input);
|
|
139 |
}
|
|
140 |
|
|
141 |
@Override
|
|
142 |
public void createPartControl(final Composite parent) {
|
|
143 |
Composite body = createBody(parent);
|
|
144 |
navigationBar.initUI();
|
|
145 |
|
|
146 |
PlatformUI.getWorkbench().getHelpSystem().setHelp(body, CONTEXT_ID);
|
|
147 |
}
|
|
148 |
|
|
149 |
private Composite createBody(final Composite parent) {
|
|
150 |
// create background
|
|
151 |
backgroundParent = new Composite(parent, SWT.NONE);
|
|
152 |
applyBG(backgroundParent);
|
1615
|
153 |
GridLayoutFactory.fillDefaults().applyTo(backgroundParent);
|
1609
|
154 |
// create top naviation bar
|
|
155 |
navigationBar = createNavigationBar(backgroundParent);
|
1634
|
156 |
GridDataFactory.swtDefaults().grab(true, false).align(SWT.CENTER, SWT.TOP).indent(10, 10).applyTo(navigationBar);
|
1609
|
157 |
// create stack composite
|
|
158 |
createStackComposite(backgroundParent, navigationBar);
|
|
159 |
return backgroundParent;
|
|
160 |
}
|
|
161 |
|
|
162 |
private void createStackComposite(Composite parent, NavigationBar bar) {
|
|
163 |
stackComposite = new StackComposite(parent, backgroundParent);
|
|
164 |
for (IPortalPage page : uninitializedPages) {
|
|
165 |
Control control = createPage(page);
|
|
166 |
pageToControlMap.put(page, control);
|
|
167 |
}
|
1615
|
168 |
GridDataFactory.fillDefaults().grab(true, true).applyTo(stackComposite);
|
1609
|
169 |
}
|
|
170 |
|
|
171 |
private Control createPage(IPortalPage page) {
|
|
172 |
Composite pageComposite = new SharedBackgroundComposite(stackComposite, backgroundParent);
|
1615
|
173 |
GridLayoutFactory.fillDefaults().numColumns(2).spacing(20, 0).extendedMargins(20, 20, 20, 0).applyTo(pageComposite);
|
1609
|
174 |
ActionUIUpdater updater = new ActionUIUpdater();
|
|
175 |
IActionBar[] commandBars = page.createCommandBars(this, updater);
|
|
176 |
if (commandBars.length > 0) {
|
|
177 |
Composite taskComposite = new SharedBackgroundComposite(pageComposite, backgroundParent);
|
1615
|
178 |
GridLayoutFactory.fillDefaults().applyTo(taskComposite);
|
|
179 |
GridDataFactory.fillDefaults().grab(false, true).applyTo(taskComposite);
|
1609
|
180 |
for (IActionBar actionBar : commandBars) {
|
|
181 |
Control control = createTaskBarControl(taskComposite, actionBar, updater);
|
1634
|
182 |
GridDataFactory.fillDefaults().minSize(150, SWT.DEFAULT).grab(true, false).indent(0, 0).applyTo(control);
|
1609
|
183 |
}
|
|
184 |
}
|
|
185 |
Composite pageControl = new RoundedCornerComposite(pageComposite, backgroundParent,
|
|
186 |
null, pageComposite.getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
|
1615
|
187 |
GridLayoutFactory.fillDefaults().margins(2, 2).applyTo(pageControl);
|
|
188 |
GridDataFactory.fillDefaults().grab(true, true).applyTo(pageControl);
|
1609
|
189 |
Control control = page.createControl(pageControl, this);
|
1615
|
190 |
GridDataFactory.fillDefaults().grab(true, true).applyTo(control);
|
1609
|
191 |
return pageComposite;
|
|
192 |
}
|
|
193 |
|
|
194 |
private Control createTaskBarControl(Composite clientArea, IActionBar actionBar, ActionUIUpdater updater) {
|
|
195 |
TaskBar taskBar = new TaskBar(clientArea, this, actionBar);
|
1615
|
196 |
updater.addTaskBar(taskBar);
|
1609
|
197 |
return taskBar;
|
|
198 |
}
|
|
199 |
|
|
200 |
private NavigationBar createNavigationBar(Composite parent) {
|
|
201 |
NavigationBar bar = new NavigationBar(this, parent);
|
|
202 |
for (IPortalPage page : uninitializedPages) {
|
|
203 |
bar.addNavButton(bar, page);
|
|
204 |
}
|
|
205 |
|
|
206 |
return bar;
|
|
207 |
}
|
|
208 |
|
|
209 |
void showPage(IPortalPage page) {
|
|
210 |
if (uninitializedPages.contains(page)) {
|
|
211 |
uninitializedPages.remove(page);
|
|
212 |
page.init();
|
|
213 |
}
|
|
214 |
stackComposite.showControl(pageToControlMap.get(page));
|
|
215 |
}
|
|
216 |
|
|
217 |
private void applyBG(final Composite composite) {
|
|
218 |
composite.addListener(SWT.Resize, new Listener() {
|
|
219 |
@Override
|
|
220 |
public void handleEvent(Event event) {
|
|
221 |
Rectangle rect = composite.getClientArea();
|
|
222 |
Image newImage = new Image(composite.getDisplay(), rect.width, rect.height);
|
|
223 |
GC gc = new GC(newImage);
|
|
224 |
gc.setForeground(composite.getDisplay().getSystemColor(SWT.COLOR_BLACK));
|
1618
|
225 |
gc.setBackground(composite.getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
|
1609
|
226 |
gc.fillGradientRectangle(0, 0, rect.width, rect.height, true);
|
|
227 |
gc.dispose();
|
|
228 |
composite.setBackgroundImage(newImage);
|
|
229 |
|
|
230 |
if (oldBGImg != null)
|
|
231 |
oldBGImg.dispose();
|
|
232 |
oldBGImg = newImage;
|
1653
|
233 |
backgroundParent.layout();
|
1609
|
234 |
}
|
|
235 |
});
|
|
236 |
}
|
|
237 |
|
|
238 |
@Override
|
|
239 |
public void setFocus() {
|
|
240 |
}
|
|
241 |
|
|
242 |
public static void openPortal() {
|
|
243 |
try {
|
|
244 |
WorkbenchUtils.openEditor(getInput(), ID);
|
|
245 |
} catch (PartInitException e) {
|
1615
|
246 |
Activator.logError(Messages.PortalEditor_PageOpenError, e);
|
1609
|
247 |
}
|
|
248 |
}
|
|
249 |
|
|
250 |
private static IEditorInput getInput() {
|
|
251 |
if (input == null) {
|
|
252 |
input = new IEditorInput() {
|
|
253 |
@SuppressWarnings("rawtypes")
|
|
254 |
@Override
|
|
255 |
public Object getAdapter(Class adapter) {
|
|
256 |
return null;
|
|
257 |
}
|
|
258 |
|
|
259 |
@Override
|
|
260 |
public String getToolTipText() {
|
|
261 |
return getName();
|
|
262 |
}
|
|
263 |
|
|
264 |
@Override
|
|
265 |
public IPersistableElement getPersistable() {
|
|
266 |
return null;
|
|
267 |
}
|
|
268 |
|
|
269 |
@Override
|
|
270 |
public String getName() {
|
1615
|
271 |
return Messages.PortalEditor_Name;
|
1609
|
272 |
}
|
|
273 |
|
|
274 |
@Override
|
|
275 |
public ImageDescriptor getImageDescriptor() {
|
|
276 |
return null;
|
|
277 |
}
|
|
278 |
|
|
279 |
@Override
|
|
280 |
public boolean exists() {
|
|
281 |
return false;
|
|
282 |
}
|
|
283 |
};
|
|
284 |
}
|
|
285 |
|
|
286 |
return input;
|
|
287 |
}
|
|
288 |
|
1618
|
289 |
Image createImage(ImageDescriptor desc, int width, int height) {
|
|
290 |
Image image;
|
|
291 |
ImageData data = desc.getImageData();
|
|
292 |
if (data.width != width || data.height != height) {
|
|
293 |
image = new Image(Display.getCurrent(), desc.getImageData().scaledTo(width, height));
|
|
294 |
}
|
|
295 |
else {
|
|
296 |
image = desc.createImage();
|
|
297 |
}
|
1609
|
298 |
resources.add(image);
|
|
299 |
return image;
|
|
300 |
}
|
|
301 |
|
1618
|
302 |
Font createFont(FontDescriptor desc) {
|
|
303 |
Font font = desc.createFont(Display.getCurrent());
|
1609
|
304 |
resources.add(font);
|
|
305 |
return font;
|
|
306 |
}
|
|
307 |
|
|
308 |
@Override
|
|
309 |
public void dispose() {
|
|
310 |
super.dispose();
|
|
311 |
for (Resource resource : resources) {
|
|
312 |
resource.dispose();
|
|
313 |
}
|
|
314 |
if (oldBGImg != null)
|
|
315 |
oldBGImg.dispose();
|
|
316 |
}
|
|
317 |
|
|
318 |
public Composite getBackgroundParent() {
|
|
319 |
return backgroundParent;
|
|
320 |
}
|
|
321 |
|
|
322 |
}
|