1 11 package org.eclipse.ui.views.framelist; 12 13 import org.eclipse.jface.util.IPropertyChangeListener; 14 import org.eclipse.jface.util.PropertyChangeEvent; 15 import org.eclipse.jface.viewers.AbstractTreeViewer; 16 import org.eclipse.jface.viewers.IStructuredSelection; 17 import org.eclipse.jface.viewers.ITreeContentProvider; 18 19 25 public class TreeViewerFrameSource implements IFrameSource { 26 27 private AbstractTreeViewer viewer; 28 29 34 public TreeViewerFrameSource(AbstractTreeViewer viewer) { 35 this.viewer = viewer; 36 } 37 38 42 public void connectTo(FrameList frameList) { 43 frameList.addPropertyChangeListener(new IPropertyChangeListener() { 44 public void propertyChange(PropertyChangeEvent event) { 45 TreeViewerFrameSource.this.handlePropertyChange(event); 46 } 47 }); 48 } 49 50 56 protected TreeFrame createFrame(Object input) { 57 return new TreeFrame(viewer, input); 58 } 59 60 65 protected void frameChanged(TreeFrame frame) { 66 viewer.getControl().setRedraw(false); 67 viewer.setInput(frame.getInput()); 68 viewer.setExpandedElements(frame.getExpandedElements()); 69 viewer.setSelection(frame.getSelection(), true); 70 viewer.getControl().setRedraw(true); 71 } 72 73 79 protected Frame getCurrentFrame(int flags) { 80 Object input = viewer.getInput(); 81 TreeFrame frame = createFrame(input); 82 if ((flags & IFrameSource.FULL_CONTEXT) != 0) { 83 frame.setSelection(viewer.getSelection()); 84 frame.setExpandedElements(viewer.getExpandedElements()); 85 } 86 return frame; 87 } 88 89 92 public Frame getFrame(int whichFrame, int flags) { 93 switch (whichFrame) { 94 case IFrameSource.CURRENT_FRAME: 95 return getCurrentFrame(flags); 96 case IFrameSource.PARENT_FRAME: 97 return getParentFrame(flags); 98 case IFrameSource.SELECTION_FRAME: 99 return getSelectionFrame(flags); 100 default: 101 return null; 102 } 103 } 104 105 111 protected Frame getParentFrame(int flags) { 112 Object input = viewer.getInput(); 113 ITreeContentProvider provider = (ITreeContentProvider) viewer 114 .getContentProvider(); 115 Object parent = provider.getParent(input); 116 if (parent == null) { 117 return null; 118 } else { 119 TreeFrame frame = createFrame(parent); 120 if ((flags & IFrameSource.FULL_CONTEXT) != 0) { 121 frame.setSelection(viewer.getSelection()); 122 Object [] expanded = viewer.getExpandedElements(); 124 Object [] newExpanded = new Object [expanded.length + 1]; 125 System.arraycopy(expanded, 0, newExpanded, 0, expanded.length); 126 newExpanded[newExpanded.length - 1] = input; 127 frame.setExpandedElements(newExpanded); 128 } 129 return frame; 130 } 131 } 132 133 140 protected Frame getSelectionFrame(int flags) { 141 IStructuredSelection sel = (IStructuredSelection) viewer.getSelection(); 142 if (sel.size() == 1) { 143 Object o = sel.getFirstElement(); 144 if (viewer.isExpandable(o)) { 145 TreeFrame frame = createFrame(o); 146 if ((flags & IFrameSource.FULL_CONTEXT) != 0) { 147 frame.setSelection(viewer.getSelection()); 148 frame.setExpandedElements(viewer.getExpandedElements()); 149 } 150 return frame; 151 } 152 } 153 return null; 154 } 155 156 161 public AbstractTreeViewer getViewer() { 162 return viewer; 163 } 164 165 169 protected void handlePropertyChange(PropertyChangeEvent event) { 170 if (FrameList.P_CURRENT_FRAME.equals(event.getProperty())) { 171 frameChanged((TreeFrame) event.getNewValue()); 172 } 173 } 174 } 175 | Popular Tags |