KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 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
12 package org.eclipse.ui.internal;
13
14 import java.util.HashMap JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.Map JavaDoc;
18 import java.util.Set JavaDoc;
19
20 import org.eclipse.core.runtime.IStatus;
21 import org.eclipse.core.runtime.Status;
22 import org.eclipse.ui.IMemento;
23 import org.eclipse.ui.IWorkbenchPage;
24 import org.eclipse.ui.PartInitException;
25 import org.eclipse.ui.views.IStickyViewDescriptor;
26 import org.eclipse.ui.views.IViewRegistry;
27
28 /**
29  * @since 3.3
30  *
31  */

32 class StickyViewManager32 implements IStickyViewManager {
33
34     // a mapping of perspectives to a set of stickyviews that have been activated in that perspective.
35
// this map is persisted across sessions
36
private Map JavaDoc stickyPerspectives = new HashMap JavaDoc(7);
37     
38     private IWorkbenchPage page;
39     
40     public StickyViewManager32(IWorkbenchPage page) {
41         this.page = page;
42     }
43
44     /* (non-Javadoc)
45      * @see org.eclipse.ui.internal.IStickyViewManager#remove(java.lang.String)
46      */

47     public void remove(String JavaDoc pespectiveId) {
48         stickyPerspectives.remove(pespectiveId);
49     }
50
51     /* (non-Javadoc)
52      * @see org.eclipse.ui.internal.IStickyViewManager#add(java.lang.String, java.util.Set)
53      */

54     public void add(String JavaDoc pespectiveId, Set JavaDoc stickyViewList) {
55         stickyPerspectives.put(pespectiveId, stickyViewList);
56     }
57
58     /* (non-Javadoc)
59      * @see org.eclipse.ui.internal.IStickyViewManager#clear()
60      */

61     public void clear() {
62         stickyPerspectives.clear();
63     }
64
65     /* (non-Javadoc)
66      * @see org.eclipse.ui.internal.IStickyViewManager#update(org.eclipse.ui.IWorkbenchPage, org.eclipse.ui.internal.Perspective, org.eclipse.ui.internal.Perspective)
67      */

68     public void update(Perspective oldPersp, Perspective newPersp) {
69         if (newPersp != null && oldPersp != null) {
70             Set JavaDoc activatedStickyViewsInThisPerspective = (Set JavaDoc) stickyPerspectives
71                     .get(newPersp.getDesc().getId());
72             if (activatedStickyViewsInThisPerspective == null) {
73                 activatedStickyViewsInThisPerspective = new HashSet JavaDoc(7);
74                 stickyPerspectives.put(newPersp.getDesc().getId(),
75                         activatedStickyViewsInThisPerspective);
76             }
77             IViewRegistry viewReg = WorkbenchPlugin.getDefault()
78                     .getViewRegistry();
79             IStickyViewDescriptor[] stickyDescs = viewReg.getStickyViews();
80             for (int i = 0; i < stickyDescs.length; i++) {
81                 final String JavaDoc viewId = stickyDescs[i].getId();
82                 try {
83                     // show a sticky view if it was in the last perspective and hasn't already been activated in this one
84
if (oldPersp.findView(viewId) != null
85                             && !activatedStickyViewsInThisPerspective
86                                     .contains(viewId)) {
87                         page.showView(viewId, null, IWorkbenchPage.VIEW_CREATE);
88                         activatedStickyViewsInThisPerspective.add(viewId);
89                     }
90                 } catch (PartInitException e) {
91                     WorkbenchPlugin
92                             .log(
93                                     "Could not open view :" + viewId, new Status(IStatus.ERROR, WorkbenchPlugin.PI_WORKBENCH, IStatus.ERROR, "Could not open view :" + viewId, e)); //$NON-NLS-1$ //$NON-NLS-2$
94
}
95             }
96         }
97     }
98     
99     /* (non-Javadoc)
100      * @see org.eclipse.ui.internal.IStickyViewManager#save(org.eclipse.ui.IMemento)
101      */

102     public void save(IMemento memento) {
103         IMemento stickyState = memento.createChild(IWorkbenchConstants.TAG_STICKY_STATE);
104         Iterator JavaDoc itr = stickyPerspectives.entrySet().iterator();
105         while (itr.hasNext()) {
106             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) itr.next();
107             String JavaDoc perspectiveId = (String JavaDoc) entry.getKey();
108             Set JavaDoc activatedViewIds = (Set JavaDoc) entry.getValue();
109             IMemento perspectiveState = stickyState.createChild(
110                     IWorkbenchConstants.TAG_PERSPECTIVE, perspectiveId);
111             for (Iterator JavaDoc i = activatedViewIds.iterator(); i.hasNext();) {
112                 String JavaDoc viewId = (String JavaDoc) i.next();
113                 perspectiveState.createChild(IWorkbenchConstants.TAG_VIEW,
114                         viewId);
115             }
116         }
117     }
118
119     /* (non-Javadoc)
120      * @see org.eclipse.ui.internal.IStickyViewManager#restore(org.eclipse.ui.IMemento)
121      */

122     public void restore(IMemento memento) {
123         // restore the sticky activation state
124
IMemento stickyState = memento
125                 .getChild(IWorkbenchConstants.TAG_STICKY_STATE);
126
127         if (stickyState != null) {
128             IMemento[] stickyPerspMems = stickyState
129                     .getChildren(IWorkbenchConstants.TAG_PERSPECTIVE);
130             for (int i = 0; i < stickyPerspMems.length; i++) {
131                 String JavaDoc perspectiveId = stickyPerspMems[i].getID();
132                 Set JavaDoc viewState = new HashSet JavaDoc(7);
133                 stickyPerspectives.put(perspectiveId, viewState);
134                 IMemento[] viewStateMementos = stickyPerspMems[i]
135                         .getChildren(IWorkbenchConstants.TAG_VIEW);
136                 for (int j = 0; j < viewStateMementos.length; j++) {
137                     viewState.add(viewStateMementos[j].getID());
138                 }
139             }
140         }
141     }
142 }
143
Popular Tags