1 11 package org.eclipse.ui.views.contentoutline; 12 13 import org.eclipse.core.runtime.ListenerList; 14 import org.eclipse.core.runtime.Platform; 15 import org.eclipse.jface.util.SafeRunnable; 16 import org.eclipse.jface.viewers.ISelection; 17 import org.eclipse.jface.viewers.ISelectionChangedListener; 18 import org.eclipse.jface.viewers.SelectionChangedEvent; 19 import org.eclipse.jface.viewers.StructuredSelection; 20 import org.eclipse.jface.viewers.TreeViewer; 21 import org.eclipse.swt.SWT; 22 import org.eclipse.swt.widgets.Composite; 23 import org.eclipse.swt.widgets.Control; 24 import org.eclipse.ui.part.IPageSite; 25 import org.eclipse.ui.part.Page; 26 27 50 public abstract class ContentOutlinePage extends Page implements 51 IContentOutlinePage, ISelectionChangedListener { 52 private ListenerList selectionChangedListeners = new ListenerList(); 53 54 private TreeViewer treeViewer; 55 56 59 protected ContentOutlinePage() { 60 super(); 61 } 62 63 66 public void addSelectionChangedListener(ISelectionChangedListener listener) { 67 selectionChangedListeners.add(listener); 68 } 69 70 77 public void createControl(Composite parent) { 78 treeViewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL 79 | SWT.V_SCROLL); 80 treeViewer.addSelectionChangedListener(this); 81 } 82 83 88 protected void fireSelectionChanged(ISelection selection) { 89 final SelectionChangedEvent event = new SelectionChangedEvent(this, 91 selection); 92 93 Object [] listeners = selectionChangedListeners.getListeners(); 95 for (int i = 0; i < listeners.length; ++i) { 96 final ISelectionChangedListener l = (ISelectionChangedListener) listeners[i]; 97 Platform.run(new SafeRunnable() { 98 public void run() { 99 l.selectionChanged(event); 100 } 101 }); 102 } 103 } 104 105 108 public Control getControl() { 109 if (treeViewer == null) { 110 return null; 111 } 112 return treeViewer.getControl(); 113 } 114 115 118 public ISelection getSelection() { 119 if (treeViewer == null) { 120 return StructuredSelection.EMPTY; 121 } 122 return treeViewer.getSelection(); 123 } 124 125 131 protected TreeViewer getTreeViewer() { 132 return treeViewer; 133 } 134 135 139 public void init(IPageSite pageSite) { 140 super.init(pageSite); 141 pageSite.setSelectionProvider(this); 142 } 143 144 147 public void removeSelectionChangedListener( 148 ISelectionChangedListener listener) { 149 selectionChangedListeners.remove(listener); 150 } 151 152 156 public void selectionChanged(SelectionChangedEvent event) { 157 fireSelectionChanged(event.getSelection()); 158 } 159 160 163 public void setFocus() { 164 treeViewer.getControl().setFocus(); 165 } 166 167 170 public void setSelection(ISelection selection) { 171 if (treeViewer != null) { 172 treeViewer.setSelection(selection); 173 } 174 } 175 } 176 | Popular Tags |