001: /*******************************************************************************
002: * Copyright (c) 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: ******************************************************************************/package org.eclipse.ui.internal;
011:
012: import java.util.HashMap;
013: import java.util.HashSet;
014: import java.util.Iterator;
015: import java.util.Map;
016: import java.util.Set;
017:
018: import org.eclipse.core.runtime.IStatus;
019: import org.eclipse.core.runtime.Status;
020: import org.eclipse.ui.IMemento;
021: import org.eclipse.ui.IWorkbenchPage;
022: import org.eclipse.ui.PartInitException;
023: import org.eclipse.ui.views.IStickyViewDescriptor;
024: import org.eclipse.ui.views.IViewRegistry;
025:
026: /**
027: * @since 3.3
028: *
029: */
030: class StickyViewManager32 implements IStickyViewManager {
031:
032: // a mapping of perspectives to a set of stickyviews that have been activated in that perspective.
033: // this map is persisted across sessions
034: private Map stickyPerspectives = new HashMap(7);
035:
036: private IWorkbenchPage page;
037:
038: public StickyViewManager32(IWorkbenchPage page) {
039: this .page = page;
040: }
041:
042: /* (non-Javadoc)
043: * @see org.eclipse.ui.internal.IStickyViewManager#remove(java.lang.String)
044: */
045: public void remove(String pespectiveId) {
046: stickyPerspectives.remove(pespectiveId);
047: }
048:
049: /* (non-Javadoc)
050: * @see org.eclipse.ui.internal.IStickyViewManager#add(java.lang.String, java.util.Set)
051: */
052: public void add(String pespectiveId, Set stickyViewList) {
053: stickyPerspectives.put(pespectiveId, stickyViewList);
054: }
055:
056: /* (non-Javadoc)
057: * @see org.eclipse.ui.internal.IStickyViewManager#clear()
058: */
059: public void clear() {
060: stickyPerspectives.clear();
061: }
062:
063: /* (non-Javadoc)
064: * @see org.eclipse.ui.internal.IStickyViewManager#update(org.eclipse.ui.IWorkbenchPage, org.eclipse.ui.internal.Perspective, org.eclipse.ui.internal.Perspective)
065: */
066: public void update(Perspective oldPersp, Perspective newPersp) {
067: if (newPersp != null && oldPersp != null) {
068: Set activatedStickyViewsInThisPerspective = (Set) stickyPerspectives
069: .get(newPersp.getDesc().getId());
070: if (activatedStickyViewsInThisPerspective == null) {
071: activatedStickyViewsInThisPerspective = new HashSet(7);
072: stickyPerspectives.put(newPersp.getDesc().getId(),
073: activatedStickyViewsInThisPerspective);
074: }
075: IViewRegistry viewReg = WorkbenchPlugin.getDefault()
076: .getViewRegistry();
077: IStickyViewDescriptor[] stickyDescs = viewReg
078: .getStickyViews();
079: for (int i = 0; i < stickyDescs.length; i++) {
080: final String viewId = stickyDescs[i].getId();
081: try {
082: // show a sticky view if it was in the last perspective and hasn't already been activated in this one
083: if (oldPersp.findView(viewId) != null
084: && !activatedStickyViewsInThisPerspective
085: .contains(viewId)) {
086: page.showView(viewId, null,
087: IWorkbenchPage.VIEW_CREATE);
088: activatedStickyViewsInThisPerspective
089: .add(viewId);
090: }
091: } catch (PartInitException e) {
092: WorkbenchPlugin
093: .log(
094: "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$
095: }
096: }
097: }
098: }
099:
100: /* (non-Javadoc)
101: * @see org.eclipse.ui.internal.IStickyViewManager#save(org.eclipse.ui.IMemento)
102: */
103: public void save(IMemento memento) {
104: IMemento stickyState = memento
105: .createChild(IWorkbenchConstants.TAG_STICKY_STATE);
106: Iterator itr = stickyPerspectives.entrySet().iterator();
107: while (itr.hasNext()) {
108: Map.Entry entry = (Map.Entry) itr.next();
109: String perspectiveId = (String) entry.getKey();
110: Set activatedViewIds = (Set) entry.getValue();
111: IMemento perspectiveState = stickyState.createChild(
112: IWorkbenchConstants.TAG_PERSPECTIVE, perspectiveId);
113: for (Iterator i = activatedViewIds.iterator(); i.hasNext();) {
114: String viewId = (String) i.next();
115: perspectiveState.createChild(
116: IWorkbenchConstants.TAG_VIEW, viewId);
117: }
118: }
119: }
120:
121: /* (non-Javadoc)
122: * @see org.eclipse.ui.internal.IStickyViewManager#restore(org.eclipse.ui.IMemento)
123: */
124: public void restore(IMemento memento) {
125: // restore the sticky activation state
126: IMemento stickyState = memento
127: .getChild(IWorkbenchConstants.TAG_STICKY_STATE);
128:
129: if (stickyState != null) {
130: IMemento[] stickyPerspMems = stickyState
131: .getChildren(IWorkbenchConstants.TAG_PERSPECTIVE);
132: for (int i = 0; i < stickyPerspMems.length; i++) {
133: String perspectiveId = stickyPerspMems[i].getID();
134: Set viewState = new HashSet(7);
135: stickyPerspectives.put(perspectiveId, viewState);
136: IMemento[] viewStateMementos = stickyPerspMems[i]
137: .getChildren(IWorkbenchConstants.TAG_VIEW);
138: for (int j = 0; j < viewStateMementos.length; j++) {
139: viewState.add(viewStateMementos[j].getID());
140: }
141: }
142: }
143: }
144: }
|