1 11 package org.eclipse.team.ui; 12 13 import java.lang.reflect.InvocationTargetException ; 14 15 import org.eclipse.compare.*; 16 import org.eclipse.compare.structuremergeviewer.ICompareInput; 17 import org.eclipse.core.runtime.IProgressMonitor; 18 import org.eclipse.jface.action.IToolBarManager; 19 import org.eclipse.jface.action.ToolBarManager; 20 import org.eclipse.jface.operation.IRunnableWithProgress; 21 import org.eclipse.jface.viewers.*; 22 import org.eclipse.swt.SWT; 23 import org.eclipse.swt.widgets.Composite; 24 import org.eclipse.team.internal.ui.Utils; 25 import org.eclipse.team.internal.ui.synchronize.LocalResourceTypedElement; 26 import org.eclipse.ui.PlatformUI; 27 import org.eclipse.ui.part.IPage; 28 import org.eclipse.ui.progress.IProgressService; 29 30 38 public abstract class PageCompareEditorInput extends CompareEditorInput implements IContentChangeListener { 39 40 private CompareViewerPane pagePane; 41 private ICompareInput hookedInput; 42 43 47 protected PageCompareEditorInput(CompareConfiguration configuration) { 48 super(configuration); 49 } 50 51 54 protected CompareViewerPane createStructureInputPane(Composite parent) { 55 pagePane = new CompareViewerPane(parent, SWT.BORDER | SWT.FLAT) { 56 public void selectionChanged(SelectionChangedEvent ev) { 57 ISelection selection = ev.getSelection(); 58 StructuredSelection newSelection = convertSelection(selection, false); 59 SelectionChangedEvent newEv = new SelectionChangedEvent(pagePane, newSelection); 60 super.selectionChanged(newEv); 61 } 62 private StructuredSelection convertSelection(ISelection selection, boolean prepare) { 63 ICompareInput ci = asCompareInput(selection); 64 StructuredSelection newSelection; 65 if (ci != null) { 66 if (prepare) 67 prepareCompareInput(ci); 68 newSelection = new StructuredSelection(ci); 69 } else { 70 newSelection = StructuredSelection.EMPTY; 71 } 72 return newSelection; 73 } 74 public ISelection getSelection() { 75 return convertSelection(getSelectionProvider().getSelection(), false); 76 } 77 public Object getInput() { 78 return PageCompareEditorInput.this.getCompareResult(); 79 } 80 public void open(OpenEvent event) { 81 ISelection selection = event.getSelection(); 82 StructuredSelection newSelection = convertSelection(selection, true); 83 super.open(new OpenEvent((Viewer)event.getSource(), newSelection)); 84 } 85 public void doubleClick(DoubleClickEvent event) { 86 ISelection selection = event.getSelection(); 87 StructuredSelection newSelection = convertSelection(selection, true); 88 super.doubleClick(new DoubleClickEvent((Viewer)event.getSource(), newSelection)); 89 } 90 91 public void setInput(Object input) { 92 super.setInput(input); 93 Composite c = getParent(); 94 if (c instanceof Splitter) 95 ((Splitter)c).setVisible(this, true); 96 layout(true); 97 } 98 }; 99 ToolBarManager toolBarManager = CompareViewerPane.getToolBarManager(pagePane); 100 IPage page = createPage(pagePane, toolBarManager); 101 pagePane.setContent(page.getControl()); 102 if (parent instanceof Splitter) 103 ((Splitter)parent).setVisible(pagePane, false); 104 hookupListeners(); 105 return pagePane; 106 } 107 108 115 protected abstract IPage createPage(CompareViewerPane parent, IToolBarManager toolBarManager); 116 117 123 protected abstract ISelectionProvider getSelectionProvider(); 124 125 130 protected void setPageDescription(String title) { 131 pagePane.setText(title); 132 } 133 134 137 protected void handleDispose() { 138 super.handleDispose(); 139 cleanupListeners(); 140 unhookContentChangeListener(); 141 } 142 143 private void hookupListeners() { 144 ISelectionProvider selectionProvider = getSelectionProvider(); 145 if (selectionProvider != null) 146 selectionProvider.addSelectionChangedListener(pagePane); 147 if (selectionProvider instanceof StructuredViewer) { 148 StructuredViewer sv = (StructuredViewer) selectionProvider; 149 sv.addOpenListener(pagePane); 150 sv.addDoubleClickListener(pagePane); 151 } 152 } 153 154 private void cleanupListeners() { 155 ISelectionProvider selectionProvider = getSelectionProvider(); 156 if (selectionProvider != null) 157 selectionProvider.removeSelectionChangedListener(pagePane); 158 if (selectionProvider instanceof StructuredViewer) { 159 StructuredViewer sv = (StructuredViewer) selectionProvider; 160 sv.removeOpenListener(pagePane); 161 sv.removeDoubleClickListener(pagePane); 162 } 163 } 164 165 private void hookContentChangeListener(ICompareInput node) { 166 if (hookedInput == node) 167 return; 168 unhookContentChangeListener(); 169 hookedInput = node; 170 ITypedElement left = node.getLeft(); 171 if(left instanceof IContentChangeNotifier) { 172 ((IContentChangeNotifier)left).addContentChangeListener(this); 173 } 174 ITypedElement right = node.getRight(); 175 if(right instanceof IContentChangeNotifier) { 176 ((IContentChangeNotifier)right).addContentChangeListener(this); 177 } 178 } 179 180 private void unhookContentChangeListener() { 181 if (hookedInput != null) { 182 ITypedElement left = hookedInput.getLeft(); 183 if(left instanceof IContentChangeNotifier) { 184 ((IContentChangeNotifier)left).addContentChangeListener(this); 185 } 186 ITypedElement right = hookedInput.getRight(); 187 if(right instanceof IContentChangeNotifier) { 188 ((IContentChangeNotifier)right).addContentChangeListener(this); 189 } 190 } 191 } 192 193 202 protected ICompareInput asCompareInput(ISelection selection) { 203 if (selection != null && selection instanceof IStructuredSelection) { 204 IStructuredSelection ss= (IStructuredSelection) selection; 205 if (ss.size() == 1) { 206 Object o = ss.getFirstElement(); 207 if(o instanceof ICompareInput) { 208 return (ICompareInput)o; 209 } 210 } 211 } 212 return null; 213 } 214 215 220 protected final void prepareCompareInput(final ICompareInput input) { 221 if (input == null) 222 return; 223 Object left = input.getLeft(); 225 if (left instanceof LocalResourceTypedElement) { 226 LocalResourceTypedElement lrte = (LocalResourceTypedElement) left; 227 lrte.enableSharedDocument(false); 228 } 229 IProgressService manager = PlatformUI.getWorkbench().getProgressService(); 230 try { 231 manager.busyCursorWhile(new IRunnableWithProgress() { 233 public void run(IProgressMonitor monitor) throws InvocationTargetException , InterruptedException { 234 prepareInput(input, getCompareConfiguration(), monitor); 235 hookContentChangeListener(input); 236 } 237 }); 238 } catch (InvocationTargetException e) { 239 Utils.handle(e); 240 } catch (InterruptedException e) { 241 } 243 } 244 245 248 public void contentChanged(IContentChangeNotifier source) { 249 setDirty(true); 250 } 251 252 255 public boolean canRunAsJob() { 256 return true; 257 } 258 259 268 protected abstract void prepareInput(ICompareInput input, CompareConfiguration configuration, IProgressMonitor monitor) throws InvocationTargetException ; 269 270 271 } 272 | Popular Tags |