KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ViewFactory


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.Map JavaDoc;
17
18 import org.eclipse.core.runtime.IExtension;
19 import org.eclipse.core.runtime.IStatus;
20 import org.eclipse.core.runtime.MultiStatus;
21 import org.eclipse.core.runtime.SafeRunner;
22 import org.eclipse.core.runtime.Status;
23 import org.eclipse.core.runtime.dynamichelpers.IExtensionChangeHandler;
24 import org.eclipse.core.runtime.dynamichelpers.IExtensionTracker;
25 import org.eclipse.jface.util.SafeRunnable;
26 import org.eclipse.osgi.util.NLS;
27 import org.eclipse.ui.IMemento;
28 import org.eclipse.ui.IViewPart;
29 import org.eclipse.ui.IViewReference;
30 import org.eclipse.ui.IWorkbenchPart3;
31 import org.eclipse.ui.PartInitException;
32 import org.eclipse.ui.PlatformUI;
33 import org.eclipse.ui.views.IViewDescriptor;
34 import org.eclipse.ui.views.IViewRegistry;
35
36 /**
37  * The ViewFactory is used to control the creation and disposal of views.
38  * It implements a reference counting strategy so that one view can be shared
39  * by more than one client.
40  */

41 /*package*/class ViewFactory implements IExtensionChangeHandler {
42
43     private ReferenceCounter counter;
44
45     private HashMap JavaDoc mementoTable = new HashMap JavaDoc();
46
47     WorkbenchPage page;
48
49     IViewRegistry viewReg;
50
51     /**
52      * Separates a view's primary id from its secondary id in view key strings.
53      */

54     static final String JavaDoc ID_SEP = ":"; //$NON-NLS-1$
55

56     /**
57      * Returns a string representing a view with the given id and (optional) secondary id,
58      * suitable for use as a key in a map.
59      *
60      * @param id primary id of the view
61      * @param secondaryId secondary id of the view or <code>null</code>
62      * @return the key
63      */

64     static String JavaDoc getKey(String JavaDoc id, String JavaDoc secondaryId) {
65         return secondaryId == null ? id : id + ID_SEP + secondaryId;
66     }
67
68     /**
69      * Returns a string representing the given view reference, suitable for use as a key in a map.
70      *
71      * @param viewRef the view reference
72      * @return the key
73      */

74     static String JavaDoc getKey(IViewReference viewRef) {
75         return getKey(viewRef.getId(), viewRef.getSecondaryId());
76     }
77
78     /**
79      * Extracts ths primary id portion of a compound id.
80      * @param compoundId a compound id of the form: primaryId [':' secondaryId]
81      * @return the primary id
82      */

83     static String JavaDoc extractPrimaryId(String JavaDoc compoundId) {
84         int i = compoundId.lastIndexOf(ID_SEP);
85         if (i == -1) {
86             return compoundId;
87         }
88         return compoundId.substring(0, i);
89     }
90
91     /**
92      * Extracts ths secondary id portion of a compound id.
93      * @param compoundId a compound id of the form: primaryId [':' secondaryId]
94      * @return the secondary id, or <code>null</code> if none
95      */

96     static String JavaDoc extractSecondaryId(String JavaDoc compoundId) {
97         int i = compoundId.lastIndexOf(ID_SEP);
98         if (i == -1) {
99             return null;
100         }
101         return compoundId.substring(i + 1);
102     }
103
104     /**
105      * Returns whether the given view id contains a wildcard. Wildcards cannot
106      * be used in regular view ids, only placeholders.
107      *
108      * @param viewId the view id
109      * @return <code>true</code> if the given view id contains a wildcard,
110      * <code>false</code> otherwise
111      *
112      * @since 3.1
113      */

114     static boolean hasWildcard(String JavaDoc viewId) {
115         return viewId.indexOf(PartPlaceholder.WILD_CARD) >= 0;
116     }
117     
118     /**
119      * Constructs a new view factory.
120      */

121     public ViewFactory(WorkbenchPage page, IViewRegistry reg) {
122         super();
123         this.page = page;
124         this.viewReg = reg;
125         counter = new ReferenceCounter();
126         page.getExtensionTracker().registerHandler(this, null);
127     }
128     
129     /**
130      * Creates an instance of a view defined by id.
131      *
132      * This factory implements reference counting. The first call to this
133      * method will return a new view. Subsequent calls will return the
134      * first view with an additional reference count. The view is
135      * disposed when releaseView is called an equal number of times
136      * to getView.
137      */

138     public IViewReference createView(final String JavaDoc id) throws PartInitException {
139         return createView(id, null);
140     }
141
142     /**
143      * Creates an instance of a view defined by id and secondary id.
144      *
145      * This factory implements reference counting. The first call to this
146      * method will return a new view. Subsequent calls will return the
147      * first view with an additional reference count. The view is
148      * disposed when releaseView is called an equal number of times
149      * to createView.
150      */

151     public IViewReference createView(String JavaDoc id, String JavaDoc secondaryId)
152             throws PartInitException {
153         IViewDescriptor desc = viewReg.find(id);
154         // ensure that the view id is valid
155
if (desc == null) {
156             throw new PartInitException(NLS.bind(WorkbenchMessages.ViewFactory_couldNotCreate, id ));
157         }
158         // ensure that multiple instances are allowed if a secondary id is given
159
if (secondaryId != null) {
160             if (!desc.getAllowMultiple()) {
161                 throw new PartInitException(NLS.bind(WorkbenchMessages.ViewFactory_noMultiple, id));
162             }
163         }
164         String JavaDoc key = getKey(id, secondaryId);
165         IViewReference ref = (IViewReference) counter.get(key);
166         if (ref == null) {
167             IMemento memento = (IMemento) mementoTable.get(key);
168             ref = new ViewReference(this, id, secondaryId, memento);
169             mementoTable.remove(key);
170             counter.put(key, ref);
171             getWorkbenchPage().partAdded((ViewReference)ref);
172         } else {
173             counter.addRef(key);
174         }
175         return ref;
176     }
177     
178     /**
179      * Returns the set of views being managed by this factory
180      *
181      * @return the set of views being managed by this factory
182      */

183     public IViewReference[] getViewReferences() {
184         List JavaDoc values = counter.values();
185         
186         return (IViewReference[]) values.toArray(new IViewReference[values.size()]);
187     }
188
189     /**
190      * Returns the view with the given id, or <code>null</code> if not found.
191      */

192     public IViewReference getView(String JavaDoc id) {
193         return getView(id, null);
194     }
195
196     /**
197      * Returns the view with the given id and secondary id, or <code>null</code> if not found.
198      */

199     public IViewReference getView(String JavaDoc id, String JavaDoc secondaryId) {
200         String JavaDoc key = getKey(id, secondaryId);
201         return (IViewReference) counter.get(key);
202     }
203
204     /**
205      * @return the <code>IViewRegistry</code> used by this factory.
206      * @since 3.0
207      */

208     public IViewRegistry getViewRegistry() {
209         return viewReg;
210     }
211
212     /**
213      * Returns a list of views which are open.
214      */

215     public IViewReference[] getViews() {
216         List JavaDoc list = counter.values();
217         IViewReference[] array = new IViewReference[list.size()];
218         list.toArray(array);
219         return array;
220     }
221
222     /**
223      * @return the <code>WorkbenchPage</code> used by this factory.
224      * @since 3.0
225      */

226     public WorkbenchPage getWorkbenchPage() {
227         return page;
228     }
229
230     /**
231      *
232      * @param viewRef
233      * @return the current reference count for the given view
234      */

235     public int getReferenceCount(IViewReference viewRef) {
236         String JavaDoc key = getKey(viewRef);
237         IViewReference ref = (IViewReference) counter.get(key);
238         return ref==null ? 0 : counter.getRef(key);
239     }
240     
241     /**
242      * Releases an instance of a view.
243      *
244      * This factory does reference counting. For more info see
245      * getView.
246      */

247     public void releaseView(IViewReference viewRef) {
248         String JavaDoc key = getKey(viewRef);
249         IViewReference ref = (IViewReference) counter.get(key);
250         if (ref == null) {
251             return;
252         }
253         int count = counter.removeRef(key);
254         if (count <= 0) {
255             getWorkbenchPage().partRemoved((ViewReference)ref);
256         }
257     }
258
259     /**
260      * Restore view states.
261      *
262      * @param memento the <code>IMemento</code> to restore from.
263      * @return <code>IStatus</code>
264      */

265     public IStatus restoreState(IMemento memento) {
266         IMemento mem[] = memento.getChildren(IWorkbenchConstants.TAG_VIEW);
267         for (int i = 0; i < mem.length; i++) {
268             //for dynamic UI - add the next line to replace subsequent code that is commented out
269
restoreViewState(mem[i]);
270         }
271         return new Status(IStatus.OK, PlatformUI.PLUGIN_ID, 0, "", null); //$NON-NLS-1$
272
}
273
274     /**
275      * Save view states.
276      *
277      * @param memento the <code>IMemento</code> to save to.
278      * @return <code>IStatus</code>
279      */

280     public IStatus saveState(IMemento memento) {
281         final MultiStatus result = new MultiStatus(PlatformUI.PLUGIN_ID,
282                 IStatus.OK, WorkbenchMessages.ViewFactory_problemsSavingViews, null);
283
284         final IViewReference refs[] = getViews();
285         for (int i = 0; i < refs.length; i++) {
286             //for dynamic UI - add the following line to replace subsequent code which is commented out
287
saveViewState(memento, refs[i], result);
288         }
289         return result;
290     }
291
292     // for dynamic UI
293
public IMemento saveViewState(IMemento memento, IViewReference ref,
294             MultiStatus res) {
295         final MultiStatus result = res;
296         final IMemento viewMemento = memento
297                 .createChild(IWorkbenchConstants.TAG_VIEW);
298         viewMemento.putString(IWorkbenchConstants.TAG_ID, ViewFactory
299                 .getKey(ref));
300         if (ref instanceof ViewReference) {
301             viewMemento.putString(IWorkbenchConstants.TAG_PART_NAME,
302                     ((ViewReference) ref).getPartName());
303         }
304         final IViewReference viewRef = ref;
305         final IViewPart view = (IViewPart) ref.getPart(false);
306         if (view != null) {
307             SafeRunner.run(new SafeRunnable() {
308                 public void run() {
309                     if (view instanceof IWorkbenchPart3) {
310                         Map JavaDoc properties = ((IWorkbenchPart3) view)
311                                 .getPartProperties();
312                         if (!properties.isEmpty()) {
313                             IMemento propBag = viewMemento
314                                     .createChild(IWorkbenchConstants.TAG_PROPERTIES);
315                             Iterator JavaDoc i = properties.entrySet().iterator();
316                             while (i.hasNext()) {
317                                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) i.next();
318                                 IMemento p = propBag.createChild(
319                                         IWorkbenchConstants.TAG_PROPERTY,
320                                         (String JavaDoc) entry.getKey());
321                                 p.putTextData((String JavaDoc) entry.getValue());
322                             }
323                         }
324                     }
325                     view.saveState(viewMemento
326                             .createChild(IWorkbenchConstants.TAG_VIEW_STATE));
327                 }
328
329                 public void handleException(Throwable JavaDoc e) {
330                     result
331                             .add(new Status(
332                                     IStatus.ERROR,
333                                     PlatformUI.PLUGIN_ID,
334                                     0,
335                                     NLS.bind(WorkbenchMessages.ViewFactory_couldNotSave, viewRef.getTitle() ),
336                                     e));
337                 }
338             });
339         } else {
340             IMemento mem = null;
341             IMemento props = null;
342             
343             // if we've created the reference once, any previous workbench
344
// state memento is there. After once, there is no previous
345
// session state, so it should be null.
346
if (ref instanceof ViewReference) {
347                 mem = ((ViewReference) ref).getMemento();
348                 if (mem!=null) {
349                     props = mem.getChild(IWorkbenchConstants.TAG_PROPERTIES);
350                 }
351                 if (mem!=null) {
352                     mem = mem.getChild(IWorkbenchConstants.TAG_VIEW_STATE);
353                 }
354             }
355             if (props != null) {
356                 viewMemento.createChild(IWorkbenchConstants.TAG_PROPERTIES)
357                         .putMemento(props);
358             }
359             if (mem != null) {
360                 IMemento child = viewMemento
361                         .createChild(IWorkbenchConstants.TAG_VIEW_STATE);
362                 child.putMemento(mem);
363             }
364         }
365         return viewMemento;
366     }
367
368     // for dynamic UI
369
public void restoreViewState(IMemento memento) {
370         String JavaDoc compoundId = memento.getString(IWorkbenchConstants.TAG_ID);
371         mementoTable.put(compoundId, memento);
372     }
373
374     IMemento getViewState(String JavaDoc key) {
375         IMemento memento = (IMemento) mementoTable.get(key);
376
377         if (memento == null) {
378             return null;
379         }
380
381         return memento.getChild(IWorkbenchConstants.TAG_VIEW_STATE);
382     }
383
384     /* (non-Javadoc)
385      * @see org.eclipse.core.runtime.dynamicHelpers.IExtensionChangeHandler#removeExtension(org.eclipse.core.runtime.IExtension, java.lang.Object[])
386      */

387     public void removeExtension(IExtension source, Object JavaDoc[] objects) {
388         for (int i = 0; i < objects.length; i++) {
389             if (objects[i] instanceof IViewPart) {
390                 IViewPart part = (IViewPart) objects[i];
391                 // String primaryViewId = part.getViewSite().getId();
392
// String secondaryViewId = part.getViewSite().getSecondaryId();
393
// IViewReference viewRef = page.findViewReference(
394
// primaryViewId, secondaryViewId);
395
// IPerspectiveDescriptor[] descs =
396
// page.getOpenedPerspectives();
397
// Perspective active = page.getActivePerspective();
398
// for (int i = 0; i < descs.length; i++) {
399
// Perspective nextPerspective = page.findPerspective(descs[i]);
400
//
401
// if (nextPerspective == null || active == nextPerspective)
402
// continue;
403
//
404
// page.hideView(nextPerspective, viewRef);
405
// }
406
page.hideView(part);
407             }
408
409         }
410     }
411     
412     /* (non-Javadoc)
413      * @see org.eclipse.core.runtime.dynamicHelpers.IExtensionChangeHandler#addExtension(org.eclipse.core.runtime.dynamicHelpers.IExtensionTracker, org.eclipse.core.runtime.IExtension)
414      */

415     public void addExtension(IExtensionTracker tracker,IExtension extension) {
416         //Do nothing
417
}
418
419 }
420
421
Popular Tags