KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > views > framelist > TreeFrame


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.views.framelist;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.core.runtime.IAdaptable;
17 import org.eclipse.jface.viewers.AbstractTreeViewer;
18 import org.eclipse.jface.viewers.ILabelProvider;
19 import org.eclipse.jface.viewers.ISelection;
20 import org.eclipse.jface.viewers.IStructuredSelection;
21 import org.eclipse.jface.viewers.StructuredSelection;
22 import org.eclipse.ui.IElementFactory;
23 import org.eclipse.ui.IMemento;
24 import org.eclipse.ui.IPersistableElement;
25 import org.eclipse.ui.PlatformUI;
26
27 /**
28  * Frame for tree viewers. This capture the viewer's input element, selection,
29  * and expanded elements.
30  */

31 public class TreeFrame extends Frame {
32     private static final String JavaDoc TAG_SELECTION = "selection"; //$NON-NLS-1$
33

34     private static final String JavaDoc TAG_EXPANDED = "expanded"; //$NON-NLS-1$
35

36     private static final String JavaDoc TAG_ELEMENT = "element"; //$NON-NLS-1$
37

38     private static final String JavaDoc TAG_FRAME_INPUT = "frameInput"; //$NON-NLS-1$
39

40     private static final String JavaDoc TAG_FACTORY_ID = "factoryID"; //$NON-NLS-1$
41

42     private AbstractTreeViewer viewer;
43
44     private Object JavaDoc input;
45
46     private ISelection selection;
47
48     private Object JavaDoc[] expandedElements;
49
50     /**
51      * Constructs a frame for the specified tree viewer.
52      * The frame's input, name and tool tip text are not set.
53      *
54      * @param viewer the tree viewer
55      */

56     public TreeFrame(AbstractTreeViewer viewer) {
57         this.viewer = viewer;
58     }
59
60     /**
61      * Constructs a frame for the specified tree viewer.
62      * The frame's input element is set to the specified input element.
63      * The frame's name and tool tip text are set to the text for the input
64      * element, as provided by the viewer's label provider.
65      *
66      * @param viewer the tree viewer
67      * @param input the input element
68      */

69     public TreeFrame(AbstractTreeViewer viewer, Object JavaDoc input) {
70         this(viewer);
71         setInput(input);
72         ILabelProvider provider = (ILabelProvider) viewer.getLabelProvider();
73         String JavaDoc name = provider.getText(input);
74         if(name == null) {
75             name = "";//$NON-NLS-1$
76
}
77         setName(name);
78         setToolTipText(name);
79     }
80
81     /**
82      * Returns the expanded elements.
83      *
84      * @return the expanded elements
85      */

86     public Object JavaDoc[] getExpandedElements() {
87         return expandedElements;
88     }
89
90     /**
91      * Returns the input element.
92      *
93      * @return the input element
94      */

95     public Object JavaDoc getInput() {
96         return input;
97     }
98
99     /**
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 JavaDoc restoreElements(IMemento memento) {
124         IMemento[] elementMem = memento.getChildren(TAG_ELEMENT);
125         List JavaDoc elements = new ArrayList JavaDoc(elementMem.length);
126
127         for (int i = 0; i < elementMem.length; i++) {
128             String JavaDoc 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 JavaDoc 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 JavaDoc elements = restoreElements(expandedMem);
167             expandedElements = elements.toArray(new Object JavaDoc[elements.size()]);
168         } else {
169             expandedElements = new Object JavaDoc[0];
170         }
171         IMemento selectionMem = memento.getChild(TAG_SELECTION);
172         if (selectionMem != null) {
173             List JavaDoc elements = restoreElements(selectionMem);
174             selection = new StructuredSelection(elements);
175         } else {
176             selection = StructuredSelection.EMPTY;
177         }
178     }
179
180     /**
181      * Save the specified elements to the given memento.
182      * The elements have to be adaptable to IPersistableElement.
183      *
184      * @param elements elements to persist
185      * @param memento memento to persist elements in
186      */

187     private void saveElements(Object JavaDoc[] elements, IMemento memento) {
188         for (int i = 0; i < elements.length; i++) {
189             if (elements[i] instanceof IAdaptable) {
190                 IPersistableElement persistable = (IPersistableElement) ((IAdaptable) elements[i])
191                         .getAdapter(IPersistableElement.class);
192                 if (persistable != null) {
193                     IMemento elementMem = memento.createChild(TAG_ELEMENT);
194                     elementMem.putString(TAG_FACTORY_ID, persistable
195                             .getFactoryId());
196                     persistable.saveState(elementMem);
197                 }
198             }
199         }
200     }
201
202     /**
203      * Save the frame state in the given memento.
204      *
205      * @param memento memento to persist the frame state in.
206      */

207     public void saveState(IMemento memento) {
208         if (!(input instanceof IAdaptable)) {
209             return;
210         }
211
212         IPersistableElement persistable = (IPersistableElement) ((IAdaptable) input)
213                 .getAdapter(IPersistableElement.class);
214         if (persistable != null) {
215             IMemento frameMemento = memento.createChild(TAG_FRAME_INPUT);
216
217             frameMemento.putString(TAG_FACTORY_ID, persistable.getFactoryId());
218             persistable.saveState(frameMemento);
219
220             if (expandedElements.length > 0) {
221                 IMemento expandedMem = memento.createChild(TAG_EXPANDED);
222                 saveElements(expandedElements, expandedMem);
223             }
224             // always IStructuredSelection since we only deal with tree viewers
225
if (selection instanceof IStructuredSelection) {
226                 Object JavaDoc[] elements = ((IStructuredSelection) selection)
227                         .toArray();
228                 if (elements.length > 0) {
229                     IMemento selectionMem = memento.createChild(TAG_SELECTION);
230                     saveElements(elements, selectionMem);
231                 }
232             }
233         }
234     }
235
236     /**
237      * Sets the input element.
238      *
239      * @param input the input element
240      */

241     public void setInput(Object JavaDoc input) {
242         this.input = input;
243     }
244
245     /**
246      * Sets the expanded elements.
247      *
248      * @param expandedElements the expanded elements
249      */

250     public void setExpandedElements(Object JavaDoc[] expandedElements) {
251         this.expandedElements = expandedElements;
252     }
253
254     /**
255      * Sets the selection.
256      *
257      * @param selection the selection
258      */

259     public void setSelection(ISelection selection) {
260         this.selection = selection;
261     }
262 }
263
Popular Tags