1 11 package org.eclipse.debug.internal.ui.views; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 17 import org.eclipse.core.runtime.IPath; 18 import org.eclipse.core.runtime.Path; 19 import org.eclipse.debug.core.DebugException; 20 import org.eclipse.debug.internal.ui.viewers.AsynchronousTreeViewer; 21 import org.eclipse.jface.viewers.TreePath; 22 import org.eclipse.jface.viewers.TreeSelection; 23 import org.eclipse.swt.widgets.TreeItem; 24 25 29 public abstract class AbstractViewerState implements Cloneable { 30 31 private List fSavedExpansion = null; 33 private IPath[] fSelection; 34 35 public AbstractViewerState() { 36 } 37 38 41 public AbstractViewerState(AsynchronousTreeViewer viewer) { 42 saveState(viewer); 43 } 44 45 51 public void saveState(AsynchronousTreeViewer viewer) { 52 List expanded = new ArrayList (); 53 fSavedExpansion = null; 54 TreeItem[] items = viewer.getTree().getItems(); 55 try { 56 for (int i = 0; i < items.length; i++) { 57 collectExpandedItems(items[i], expanded); 58 } 59 if (expanded.size() > 0) { 60 fSavedExpansion = expanded; 61 } 62 } catch (DebugException e) { 63 fSavedExpansion = null; 64 } 65 TreeItem[] selection = viewer.getTree().getSelection(); 66 fSelection = new IPath[selection.length]; 67 try { 68 for (int i = 0; i < selection.length; i++) { 69 fSelection[i] = encodeElement(selection[i]); 70 if (fSelection[i] == null) { 71 fSelection = null; 72 return; 73 } 74 } 75 } catch (DebugException e) { 76 fSelection = null; 77 } 78 } 79 80 89 protected boolean collectExpandedItems(TreeItem item, List expanded) throws DebugException { 90 if (item.getExpanded()) { 91 boolean childExpanded = false; 92 TreeItem[] items = item.getItems(); 93 for (int i = 0; i < items.length; i++) { 94 childExpanded = collectExpandedItems(items[i], expanded) || childExpanded; 95 } 96 if (!childExpanded) { 97 IPath path = encodeElement(item); 98 expanded.add(path); 99 } 100 } else { 101 return false; 102 } 103 return true; 104 } 105 106 115 protected abstract IPath encodeElement(TreeItem item) throws DebugException; 116 117 123 public void restoreState(AsynchronousTreeViewer viewer) { 124 boolean expansionComplete = true; 125 if (fSavedExpansion != null && fSavedExpansion.size() > 0) { 126 for (int i = 0; i < fSavedExpansion.size(); i++) { 127 IPath path = (IPath) fSavedExpansion.get(i); 128 if (path != null) { 129 try { 130 TreePath treePath = decodePath(path, viewer); 131 if (treePath != null) { 132 viewer.expand(new TreeSelection(new TreePath[] { treePath })); 133 134 if (treePath.getSegmentCount()-1 != path.segmentCount()) { 135 expansionComplete = false; 136 } 137 } else { 138 expansionComplete =false; 139 } 140 } catch (DebugException e) { 141 } 142 } 143 } 144 if (expansionComplete) { 145 fSavedExpansion = null; 146 } 147 } 148 149 boolean selectionComplete = true; 150 if (fSelection != null && fSelection.length > 0) { 151 List selection = new ArrayList (fSelection.length); 152 for (int i = 0; i < fSelection.length; i++) { 153 IPath path = fSelection[i]; 154 TreePath obj; 155 try { 156 obj = decodePath(path, viewer); 157 if (obj != null && obj.getSegmentCount()-1 == path.segmentCount()) { 158 selection.add(obj); 159 } else { 160 selectionComplete = false; 161 } 162 } catch (DebugException e) { 163 } 164 } 165 if (selection.size() > 0) { 166 TreePath[] treePaths = (TreePath[]) selection.toArray(new TreePath[0]); 167 viewer.setSelection(new TreeSelection(treePaths)); 168 } 169 if (selectionComplete) { 170 fSelection = null; 171 } 172 } 173 } 174 175 184 protected abstract TreePath decodePath(IPath path, AsynchronousTreeViewer viewer) throws DebugException; 185 186 189 public Object clone() { 190 AbstractViewerState clone = null; 191 try { 192 clone = (AbstractViewerState) this.getClass().newInstance(); 193 } catch (InstantiationException e) { 194 return null; 195 } catch (IllegalAccessException e) { 196 return null; 197 } 198 if (fSavedExpansion != null) { 199 clone.fSavedExpansion = new ArrayList (fSavedExpansion.size()); 200 Iterator iterator = fSavedExpansion.iterator(); 201 while (iterator.hasNext()) { 202 IPath path = (IPath) iterator.next(); 203 IPath clonePath = Path.fromPortableString(path.toPortableString()); 204 clone.fSavedExpansion.add(clonePath); 205 } 206 } 207 if (fSelection != null) { 208 clone.fSelection = new IPath[fSelection.length]; 209 for (int i = 0; i < fSelection.length; i++) { 210 clone.fSelection[i] = Path.fromPortableString(fSelection[i].toPortableString()); 211 } 212 } 213 return clone; 214 } 215 } | Popular Tags |