1 11 package org.eclipse.ui.views.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 31 public class TreeFrame extends Frame { 32 private static final String TAG_SELECTION = "selection"; 34 private static final String TAG_EXPANDED = "expanded"; 36 private static final String TAG_ELEMENT = "element"; 38 private static final String TAG_FRAME_INPUT = "frameInput"; 40 private static final String TAG_FACTORY_ID = "factoryID"; 42 private AbstractTreeViewer viewer; 43 44 private Object input; 45 46 private ISelection selection; 47 48 private Object [] expandedElements; 49 50 56 public TreeFrame(AbstractTreeViewer viewer) { 57 this.viewer = viewer; 58 } 59 60 69 public TreeFrame(AbstractTreeViewer viewer, Object input) { 70 this(viewer); 71 setInput(input); 72 ILabelProvider provider = (ILabelProvider) viewer.getLabelProvider(); 73 String name = provider.getText(input); 74 if(name == null) { 75 name = ""; } 77 setName(name); 78 setToolTipText(name); 79 } 80 81 86 public Object [] getExpandedElements() { 87 return expandedElements; 88 } 89 90 95 public Object getInput() { 96 return input; 97 } 98 99 104 public ISelection getSelection() { 105 return selection; 106 } 107 108 113 public AbstractTreeViewer getViewer() { 114 return viewer; 115 } 116 117 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 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.size()]); 168 } else { 169 expandedElements = new Object [0]; 170 } 171 IMemento selectionMem = memento.getChild(TAG_SELECTION); 172 if (selectionMem != null) { 173 List elements = restoreElements(selectionMem); 174 selection = new StructuredSelection(elements); 175 } else { 176 selection = StructuredSelection.EMPTY; 177 } 178 } 179 180 187 private void saveElements(Object [] 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 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 if (selection instanceof IStructuredSelection) { 226 Object [] 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 241 public void setInput(Object input) { 242 this.input = input; 243 } 244 245 250 public void setExpandedElements(Object [] expandedElements) { 251 this.expandedElements = expandedElements; 252 } 253 254 259 public void setSelection(ISelection selection) { 260 this.selection = selection; 261 } 262 } 263 | Popular Tags |