001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 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.views.framelist;
011:
012: import java.util.ArrayList;
013: import java.util.List;
014:
015: import org.eclipse.core.runtime.IAdaptable;
016: import org.eclipse.jface.viewers.AbstractTreeViewer;
017: import org.eclipse.jface.viewers.ILabelProvider;
018: import org.eclipse.jface.viewers.ISelection;
019: import org.eclipse.jface.viewers.IStructuredSelection;
020: import org.eclipse.jface.viewers.StructuredSelection;
021: import org.eclipse.ui.IElementFactory;
022: import org.eclipse.ui.IMemento;
023: import org.eclipse.ui.IPersistableElement;
024: import org.eclipse.ui.PlatformUI;
025:
026: /**
027: * Frame for tree viewers. This capture the viewer's input element, selection,
028: * and expanded elements.
029: */
030: public class TreeFrame extends Frame {
031: private static final String TAG_SELECTION = "selection"; //$NON-NLS-1$
032:
033: private static final String TAG_EXPANDED = "expanded"; //$NON-NLS-1$
034:
035: private static final String TAG_ELEMENT = "element"; //$NON-NLS-1$
036:
037: private static final String TAG_FRAME_INPUT = "frameInput"; //$NON-NLS-1$
038:
039: private static final String TAG_FACTORY_ID = "factoryID"; //$NON-NLS-1$
040:
041: private AbstractTreeViewer viewer;
042:
043: private Object input;
044:
045: private ISelection selection;
046:
047: private Object[] expandedElements;
048:
049: /**
050: * Constructs a frame for the specified tree viewer.
051: * The frame's input, name and tool tip text are not set.
052: *
053: * @param viewer the tree viewer
054: */
055: public TreeFrame(AbstractTreeViewer viewer) {
056: this .viewer = viewer;
057: }
058:
059: /**
060: * Constructs a frame for the specified tree viewer.
061: * The frame's input element is set to the specified input element.
062: * The frame's name and tool tip text are set to the text for the input
063: * element, as provided by the viewer's label provider.
064: *
065: * @param viewer the tree viewer
066: * @param input the input element
067: */
068: public TreeFrame(AbstractTreeViewer viewer, Object input) {
069: this (viewer);
070: setInput(input);
071: ILabelProvider provider = (ILabelProvider) viewer
072: .getLabelProvider();
073: String name = provider.getText(input);
074: if (name == null) {
075: name = "";//$NON-NLS-1$
076: }
077: setName(name);
078: setToolTipText(name);
079: }
080:
081: /**
082: * Returns the expanded elements.
083: *
084: * @return the expanded elements
085: */
086: public Object[] getExpandedElements() {
087: return expandedElements;
088: }
089:
090: /**
091: * Returns the input element.
092: *
093: * @return the input element
094: */
095: public Object getInput() {
096: return input;
097: }
098:
099: /**
100: * Returns the selection.
101: *
102: * @return the selection
103: */
104: public ISelection getSelection() {
105: return selection;
106: }
107:
108: /**
109: * Returns the tree viewer.
110: *
111: * @return the tree viewer
112: */
113: public AbstractTreeViewer getViewer() {
114: return viewer;
115: }
116:
117: /**
118: * Restore IPersistableElements from the specified memento.
119: *
120: * @param memento memento to restore elements from
121: * @return list of restored elements. May be empty.
122: */
123: private List restoreElements(IMemento memento) {
124: IMemento[] elementMem = memento.getChildren(TAG_ELEMENT);
125: List elements = new ArrayList(elementMem.length);
126:
127: for (int i = 0; i < elementMem.length; i++) {
128: String factoryID = elementMem[i].getString(TAG_FACTORY_ID);
129: if (factoryID != null) {
130: IElementFactory factory = PlatformUI.getWorkbench()
131: .getElementFactory(factoryID);
132: if (factory != null) {
133: elements.add(factory.createElement(elementMem[i]));
134: }
135: }
136: }
137: return elements;
138: }
139:
140: /**
141: * Restore the frame from the specified memento.
142: *
143: * @param memento memento to restore frame from
144: */
145: public void restoreState(IMemento memento) {
146: IMemento childMem = memento.getChild(TAG_FRAME_INPUT);
147:
148: if (childMem == null) {
149: return;
150: }
151:
152: String factoryID = childMem.getString(TAG_FACTORY_ID);
153: IAdaptable frameInput = null;
154: if (factoryID != null) {
155: IElementFactory factory = PlatformUI.getWorkbench()
156: .getElementFactory(factoryID);
157: if (factory != null) {
158: frameInput = factory.createElement(childMem);
159: }
160: }
161: if (frameInput != null) {
162: input = frameInput;
163: }
164: IMemento expandedMem = memento.getChild(TAG_EXPANDED);
165: if (expandedMem != null) {
166: List elements = restoreElements(expandedMem);
167: expandedElements = elements.toArray(new Object[elements
168: .size()]);
169: } else {
170: expandedElements = new Object[0];
171: }
172: IMemento selectionMem = memento.getChild(TAG_SELECTION);
173: if (selectionMem != null) {
174: List elements = restoreElements(selectionMem);
175: selection = new StructuredSelection(elements);
176: } else {
177: selection = StructuredSelection.EMPTY;
178: }
179: }
180:
181: /**
182: * Save the specified elements to the given memento.
183: * The elements have to be adaptable to IPersistableElement.
184: *
185: * @param elements elements to persist
186: * @param memento memento to persist elements in
187: */
188: private void saveElements(Object[] elements, IMemento memento) {
189: for (int i = 0; i < elements.length; i++) {
190: if (elements[i] instanceof IAdaptable) {
191: IPersistableElement persistable = (IPersistableElement) ((IAdaptable) elements[i])
192: .getAdapter(IPersistableElement.class);
193: if (persistable != null) {
194: IMemento elementMem = memento
195: .createChild(TAG_ELEMENT);
196: elementMem.putString(TAG_FACTORY_ID, persistable
197: .getFactoryId());
198: persistable.saveState(elementMem);
199: }
200: }
201: }
202: }
203:
204: /**
205: * Save the frame state in the given memento.
206: *
207: * @param memento memento to persist the frame state in.
208: */
209: public void saveState(IMemento memento) {
210: if (!(input instanceof IAdaptable)) {
211: return;
212: }
213:
214: IPersistableElement persistable = (IPersistableElement) ((IAdaptable) input)
215: .getAdapter(IPersistableElement.class);
216: if (persistable != null) {
217: IMemento frameMemento = memento
218: .createChild(TAG_FRAME_INPUT);
219:
220: frameMemento.putString(TAG_FACTORY_ID, persistable
221: .getFactoryId());
222: persistable.saveState(frameMemento);
223:
224: if (expandedElements.length > 0) {
225: IMemento expandedMem = memento
226: .createChild(TAG_EXPANDED);
227: saveElements(expandedElements, expandedMem);
228: }
229: // always IStructuredSelection since we only deal with tree viewers
230: if (selection instanceof IStructuredSelection) {
231: Object[] elements = ((IStructuredSelection) selection)
232: .toArray();
233: if (elements.length > 0) {
234: IMemento selectionMem = memento
235: .createChild(TAG_SELECTION);
236: saveElements(elements, selectionMem);
237: }
238: }
239: }
240: }
241:
242: /**
243: * Sets the input element.
244: *
245: * @param input the input element
246: */
247: public void setInput(Object input) {
248: this .input = input;
249: }
250:
251: /**
252: * Sets the expanded elements.
253: *
254: * @param expandedElements the expanded elements
255: */
256: public void setExpandedElements(Object[] expandedElements) {
257: this .expandedElements = expandedElements;
258: }
259:
260: /**
261: * Sets the selection.
262: *
263: * @param selection the selection
264: */
265: public void setSelection(ISelection selection) {
266: this.selection = selection;
267: }
268: }
|