1
|
/*******************************************************************************
|
2
|
* Copyright (c) 2000, 2009 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.navigator.framelist;
|
12
|
|
13
|
import java.util.ArrayList;
|
14
|
import java.util.List;
|
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
|
* @since 3.4
|
31
|
*/
|
32
|
public class TreeFrame extends Frame {
|
33
|
private static final String TAG_SELECTION = "selection"; //$NON-NLS-1$
|
34
|
|
35
|
private static final String TAG_EXPANDED = "expanded"; //$NON-NLS-1$
|
36
|
|
37
|
private static final String TAG_ELEMENT = "element"; //$NON-NLS-1$
|
38
|
|
39
|
private static final String TAG_FRAME_INPUT = "frameInput"; //$NON-NLS-1$
|
40
|
|
41
|
private static final String TAG_FACTORY_ID = "factoryID"; //$NON-NLS-1$
|
42
|
|
43
|
private AbstractTreeViewer viewer;
|
44
|
|
45
|
private Object input;
|
46
|
|
47
|
private ISelection selection;
|
48
|
|
49
|
private Object[] expandedElements;
|
50
|
|
51
|
/**
|
52
|
* Constructs a frame for the specified tree viewer.
|
53
|
* The frame's input, name and tool tip text are not set.
|
54
|
*
|
55
|
* @param viewer the tree viewer
|
56
|
*/
|
57
|
public TreeFrame(AbstractTreeViewer viewer) {
|
58
|
this.viewer = viewer;
|
59
|
}
|
60
|
|
61
|
/**
|
62
|
* Constructs a frame for the specified tree viewer.
|
63
|
* The frame's input element is set to the specified input element.
|
64
|
* The frame's name and tool tip text are set to the text for the input
|
65
|
* element, as provided by the viewer's label provider.
|
66
|
*
|
67
|
* @param viewer the tree viewer
|
68
|
* @param input the input element
|
69
|
*/
|
70
|
public TreeFrame(AbstractTreeViewer viewer, Object input) {
|
71
|
this(viewer);
|
72
|
setInput(input);
|
73
|
ILabelProvider provider = (ILabelProvider) viewer.getLabelProvider();
|
74
|
String name = provider.getText(input);
|
75
|
if(name == null) {
|
76
|
name = "";//$NON-NLS-1$
|
77
|
}
|
78
|
setName(name);
|
79
|
setToolTipText(name);
|
80
|
}
|
81
|
|
82
|
/**
|
83
|
* Returns the expanded elements.
|
84
|
*
|
85
|
* @return the expanded elements
|
86
|
*/
|
87
|
public Object[] getExpandedElements() {
|
88
|
return expandedElements;
|
89
|
}
|
90
|
|
91
|
/**
|
92
|
* Returns the input element.
|
93
|
*
|
94
|
* @return the input element
|
95
|
*/
|
96
|
public Object getInput() {
|
97
|
return input;
|
98
|
}
|
99
|
|
100
|
/**
|
101
|
* Returns the selection.
|
102
|
*
|
103
|
* @return the selection
|
104
|
*/
|
105
|
public ISelection getSelection() {
|
106
|
return selection;
|
107
|
}
|
108
|
|
109
|
/**
|
110
|
* Returns the tree viewer.
|
111
|
*
|
112
|
* @return the tree viewer
|
113
|
*/
|
114
|
public AbstractTreeViewer getViewer() {
|
115
|
return viewer;
|
116
|
}
|
117
|
|
118
|
/**
|
119
|
* Restore IPersistableElements from the specified memento.
|
120
|
*
|
121
|
* @param memento memento to restore elements from
|
122
|
* @return list of restored elements. May be empty.
|
123
|
*/
|
124
|
private List restoreElements(IMemento memento) {
|
125
|
IMemento[] elementMem = memento.getChildren(TAG_ELEMENT);
|
126
|
List elements = new ArrayList(elementMem.length);
|
127
|
|
128
|
for (int i = 0; i < elementMem.length; i++) {
|
129
|
String factoryID = elementMem[i].getString(TAG_FACTORY_ID);
|
130
|
if (factoryID != null) {
|
131
|
IElementFactory factory = PlatformUI.getWorkbench()
|
132
|
.getElementFactory(factoryID);
|
133
|
if (factory != null) {
|
134
|
elements.add(factory.createElement(elementMem[i]));
|
135
|
}
|
136
|
}
|
137
|
}
|
138
|
return elements;
|
139
|
}
|
140
|
|
141
|
/**
|
142
|
* Restore the frame from the specified memento.
|
143
|
*
|
144
|
* @param memento memento to restore frame from
|
145
|
*/
|
146
|
public void restoreState(IMemento memento) {
|
147
|
IMemento childMem = memento.getChild(TAG_FRAME_INPUT);
|
148
|
|
149
|
if (childMem == null) {
|
150
|
return;
|
151
|
}
|
152
|
|
153
|
String factoryID = childMem.getString(TAG_FACTORY_ID);
|
154
|
IAdaptable frameInput = null;
|
155
|
if (factoryID != null) {
|
156
|
IElementFactory factory = PlatformUI.getWorkbench()
|
157
|
.getElementFactory(factoryID);
|
158
|
if (factory != null) {
|
159
|
frameInput = factory.createElement(childMem);
|
160
|
}
|
161
|
}
|
162
|
if (frameInput != null) {
|
163
|
input = frameInput;
|
164
|
}
|
165
|
IMemento expandedMem = memento.getChild(TAG_EXPANDED);
|
166
|
if (expandedMem != null) {
|
167
|
List elements = restoreElements(expandedMem);
|
168
|
expandedElements = elements.toArray(new Object[elements.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.createChild(TAG_ELEMENT);
|
195
|
elementMem.putString(TAG_FACTORY_ID, persistable
|
196
|
.getFactoryId());
|
197
|
persistable.saveState(elementMem);
|
198
|
}
|
199
|
}
|
200
|
}
|
201
|
}
|
202
|
|
203
|
/**
|
204
|
* Save the frame state in the given memento.
|
205
|
*
|
206
|
* @param memento memento to persist the frame state in.
|
207
|
*/
|
208
|
public void saveState(IMemento memento) {
|
209
|
if (!(input instanceof IAdaptable)) {
|
210
|
return;
|
211
|
}
|
212
|
|
213
|
IPersistableElement persistable = (IPersistableElement) ((IAdaptable) input)
|
214
|
.getAdapter(IPersistableElement.class);
|
215
|
if (persistable != null) {
|
216
|
IMemento frameMemento = memento.createChild(TAG_FRAME_INPUT);
|
217
|
|
218
|
frameMemento.putString(TAG_FACTORY_ID, persistable.getFactoryId());
|
219
|
persistable.saveState(frameMemento);
|
220
|
|
221
|
if (expandedElements.length > 0) {
|
222
|
IMemento expandedMem = memento.createChild(TAG_EXPANDED);
|
223
|
saveElements(expandedElements, expandedMem);
|
224
|
}
|
225
|
// always IStructuredSelection since we only deal with tree viewers
|
226
|
if (selection instanceof IStructuredSelection) {
|
227
|
Object[] elements = ((IStructuredSelection) selection)
|
228
|
.toArray();
|
229
|
if (elements.length > 0) {
|
230
|
IMemento selectionMem = memento.createChild(TAG_SELECTION);
|
231
|
saveElements(elements, selectionMem);
|
232
|
}
|
233
|
}
|
234
|
}
|
235
|
}
|
236
|
|
237
|
/**
|
238
|
* Sets the input element.
|
239
|
*
|
240
|
* @param input the input element
|
241
|
*/
|
242
|
public void setInput(Object input) {
|
243
|
this.input = input;
|
244
|
}
|
245
|
|
246
|
/**
|
247
|
* Sets the expanded elements.
|
248
|
*
|
249
|
* @param expandedElements the expanded elements
|
250
|
*/
|
251
|
public void setExpandedElements(Object[] expandedElements) {
|
252
|
this.expandedElements = expandedElements;
|
253
|
}
|
254
|
|
255
|
/**
|
256
|
* Sets the selection.
|
257
|
*
|
258
|
* @param selection the selection
|
259
|
*/
|
260
|
public void setSelection(ISelection selection) {
|
261
|
this.selection = selection;
|
262
|
}
|
263
|
}
|