1 11 package org.eclipse.team.ui; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.util.ArrayList ; 15 import java.util.Iterator ; 16 17 import org.eclipse.compare.*; 18 import org.eclipse.compare.contentmergeviewer.IFlushable; 19 import org.eclipse.compare.internal.CompareEditor; 20 import org.eclipse.compare.internal.CompareEditorInputNavigator; 21 import org.eclipse.compare.structuremergeviewer.ICompareInput; 22 import org.eclipse.core.runtime.Assert; 23 import org.eclipse.core.runtime.IProgressMonitor; 24 import org.eclipse.jface.action.ToolBarManager; 25 import org.eclipse.jface.operation.IRunnableWithProgress; 26 import org.eclipse.jface.util.IPropertyChangeListener; 27 import org.eclipse.jface.util.PropertyChangeEvent; 28 import org.eclipse.jface.viewers.*; 29 import org.eclipse.swt.SWT; 30 import org.eclipse.swt.events.DisposeEvent; 31 import org.eclipse.swt.events.DisposeListener; 32 import org.eclipse.swt.layout.GridData; 33 import org.eclipse.swt.layout.GridLayout; 34 import org.eclipse.swt.widgets.*; 35 import org.eclipse.team.internal.ui.TeamUIMessages; 36 import org.eclipse.team.internal.ui.Utils; 37 import org.eclipse.team.internal.ui.synchronize.LocalResourceTypedElement; 38 import org.eclipse.team.internal.ui.synchronize.SynchronizePageConfiguration; 39 import org.eclipse.team.ui.synchronize.ISynchronizePageConfiguration; 40 import org.eclipse.ui.PlatformUI; 41 import org.eclipse.ui.progress.IProgressService; 42 43 53 public abstract class PageSaveablePart extends SaveablePartAdapter implements IContentChangeListener{ 54 55 private CompareConfiguration cc; 56 Shell shell; 57 58 private boolean fDirty= false; 60 private ArrayList fDirtyViewers= new ArrayList (); 61 private IPropertyChangeListener fDirtyStateListener; 62 63 private CompareViewerSwitchingPane fContentPane; 65 private CompareViewerPane fEditionPane; 66 private CompareViewerSwitchingPane fStructuredComparePane; 67 private Control control; 68 69 private boolean showContentPanes = true; 71 72 77 protected PageSaveablePart(Shell shell, CompareConfiguration compareConfiguration){ 78 this.shell = shell; 79 this.cc = compareConfiguration; 80 81 fDirtyStateListener= new IPropertyChangeListener() { 82 public void propertyChange(PropertyChangeEvent e) { 83 String propertyName= e.getProperty(); 84 if (CompareEditorInput.DIRTY_STATE.equals(propertyName)) { 85 boolean changed= false; 86 Object newValue= e.getNewValue(); 87 if (newValue instanceof Boolean ) 88 changed= ((Boolean )newValue).booleanValue(); 89 setDirty(e.getSource(), changed); 90 } 91 } 92 }; 93 } 94 95 98 public boolean isDirty() { 99 return fDirty || fDirtyViewers.size() > 0; 100 } 101 102 105 public void createPartControl(Composite parent) { 106 Composite composite = new Composite(parent, SWT.NULL); 107 GridLayout layout = new GridLayout(); 108 layout.marginHeight = 0; 109 layout.marginWidth = 0; 110 layout.verticalSpacing = 0; 111 GridData data = new GridData(GridData.FILL_BOTH); 112 data.grabExcessHorizontalSpace = true; 113 composite.setLayout(layout); 114 composite.setLayoutData(data); 115 116 shell = parent.getShell(); 117 118 Splitter vsplitter = new Splitter(composite, SWT.VERTICAL); 119 vsplitter.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL | GridData.VERTICAL_ALIGN_FILL | GridData.GRAB_VERTICAL)); 120 Splitter hsplitter = new Splitter(vsplitter, SWT.HORIZONTAL); 122 fEditionPane = new CompareViewerPane(hsplitter, SWT.BORDER | SWT.FLAT); 123 fStructuredComparePane = new CompareViewerSwitchingPane(hsplitter, SWT.BORDER | SWT.FLAT, true) { 124 protected Viewer getViewer(Viewer oldViewer, Object input) { 125 if (input instanceof ICompareInput) 126 return findStructureViewer(this, oldViewer, (ICompareInput)input); 127 return null; 128 } 129 }; 130 fStructuredComparePane.addSelectionChangedListener(new ISelectionChangedListener() { 131 public void selectionChanged(SelectionChangedEvent e) { 132 feedInput2(e.getSelection()); 133 } 134 }); 135 fEditionPane.setText(TeamUIMessages.ParticipantPageSaveablePart_0); 136 fContentPane = new CompareViewerSwitchingPane(vsplitter, SWT.BORDER | SWT.FLAT) { 137 protected Viewer getViewer(Viewer oldViewer, Object input) { 138 if (!(input instanceof ICompareInput)) 139 return null; 140 Viewer newViewer= findContentViewer(this, oldViewer, (ICompareInput)input); 141 boolean isNewViewer= newViewer != oldViewer; 142 if (isNewViewer && newViewer instanceof IPropertyChangeNotifier) { 143 final IPropertyChangeNotifier dsp= (IPropertyChangeNotifier) newViewer; 144 dsp.addPropertyChangeListener(fDirtyStateListener); 145 Control c= newViewer.getControl(); 146 c.addDisposeListener( 147 new DisposeListener() { 148 public void widgetDisposed(DisposeEvent e) { 149 dsp.removePropertyChangeListener(fDirtyStateListener); 150 } 151 } 152 ); 153 hookContentChangeListener((ICompareInput)input); 154 } 155 return newViewer; 156 } 157 }; 158 vsplitter.setWeights(new int[]{30, 70}); 159 160 control = composite; 161 162 ToolBarManager toolBarManager = CompareViewerPane.getToolBarManager(fEditionPane); 163 Control c = createPage(fEditionPane, toolBarManager); 164 fEditionPane.setContent(c); 165 166 if(! showContentPanes) { 167 hsplitter.setMaximizedControl(fEditionPane); 168 } 169 170 getSelectionProvider().addSelectionChangedListener(new ISelectionChangedListener() { 171 public void selectionChanged(SelectionChangedEvent event) { 172 ICompareInput input = getCompareInput(event.getSelection()); 173 if (input != null) 174 prepareCompareInput(input); 175 setInput(input); 176 } 177 }); 178 } 179 180 186 protected abstract ISelectionProvider getSelectionProvider(); 187 188 195 protected abstract Control createPage(Composite parent, ToolBarManager toolBarManager); 196 197 202 protected void setPageDescription(String title) { 203 fEditionPane.setText(title); 204 } 205 206 210 protected void setDirty(boolean dirty) { 211 boolean confirmSave= true; 212 Object o= cc.getProperty(CompareEditor.CONFIRM_SAVE_PROPERTY); 213 if (o instanceof Boolean ) 214 confirmSave= ((Boolean )o).booleanValue(); 215 216 if (!confirmSave) { 217 fDirty= dirty; 218 if (!fDirty) 219 fDirtyViewers.clear(); 220 } 221 } 222 223 private void setDirty(Object source, boolean dirty) { 224 Assert.isNotNull(source); 225 if (dirty) 226 fDirtyViewers.add(source); 227 else 228 fDirtyViewers.remove(source); 229 } 230 231 235 private void setInput(Object input) { 236 CompareViewerPane pane = fContentPane; 237 if (pane != null && !pane.isDisposed()) 238 fContentPane.setInput(input); 239 if (fStructuredComparePane != null && !fStructuredComparePane.isDisposed()) 240 fStructuredComparePane.setInput(input); 241 } 242 243 246 private void feedInput2(ISelection sel) { 247 ICompareInput input = getCompareInput(sel); 248 prepareCompareInput(input); 249 if (input != null) 250 fContentPane.setInput(input); 251 } 252 253 258 protected void prepareCompareInput(final ICompareInput input) { 259 if (input == null) 260 return; 261 Object left = input.getLeft(); 263 if (left instanceof LocalResourceTypedElement) { 264 LocalResourceTypedElement lrte = (LocalResourceTypedElement) left; 265 lrte.enableSharedDocument(false); 266 } 267 IProgressService manager = PlatformUI.getWorkbench().getProgressService(); 268 try { 269 manager.busyCursorWhile(new IRunnableWithProgress() { 271 public void run(IProgressMonitor monitor) throws InvocationTargetException , InterruptedException { 272 prepareInput(input, getCompareConfiguration(), monitor); 273 hookContentChangeListener(input); 274 } 275 }); 276 } catch (InvocationTargetException e) { 277 Utils.handle(e); 278 } catch (InterruptedException e) { 279 } 281 } 282 283 292 protected abstract void prepareInput(ICompareInput input, CompareConfiguration configuration, IProgressMonitor monitor) throws InvocationTargetException ; 293 294 private void hookContentChangeListener(ICompareInput node) { 295 ITypedElement left = node.getLeft(); 297 if(left instanceof IContentChangeNotifier) { 298 ((IContentChangeNotifier)left).addContentChangeListener(this); 299 } 300 ITypedElement right = node.getRight(); 301 if(right instanceof IContentChangeNotifier) { 302 ((IContentChangeNotifier)right).addContentChangeListener(this); 303 } 304 } 305 306 310 protected Shell getShell() { 311 return shell; 312 } 313 314 318 protected void setNavigator(ISynchronizePageConfiguration configuration) { 319 configuration.setProperty(SynchronizePageConfiguration.P_NAVIGATOR, new CompareEditorInputNavigator( 320 new Object [] { 321 configuration.getProperty(SynchronizePageConfiguration.P_ADVISOR), 322 fStructuredComparePane, 323 fContentPane 324 } 325 )); 326 } 327 328 337 private Viewer findStructureViewer(Composite parent, Viewer oldViewer, ICompareInput input) { 338 return CompareUI.findStructureViewer(oldViewer, input, parent, cc); 339 } 340 341 350 private Viewer findContentViewer(Composite parent, Viewer oldViewer, ICompareInput input) { 351 return CompareUI.findContentViewer(oldViewer, input, parent, cc); 352 } 353 354 363 protected ICompareInput getCompareInput(ISelection selection) { 364 if (selection != null && selection instanceof IStructuredSelection) { 365 IStructuredSelection ss= (IStructuredSelection) selection; 366 if (ss.size() == 1) { 367 Object o = ss.getFirstElement(); 368 if(o instanceof ICompareInput) { 369 return (ICompareInput)o; 370 } 371 } 372 } 373 return null; 374 } 375 376 382 public void setShowContentPanes(boolean showContentPanes) { 383 this.showContentPanes = showContentPanes; 384 } 385 386 391 public Control getControl() { 392 return control; 393 } 394 395 399 private CompareConfiguration getCompareConfiguration() { 400 return cc; 401 } 402 403 411 public void doSave(IProgressMonitor monitor) { 412 flushViewers(monitor); 413 } 414 415 private void flushViewers(IProgressMonitor monitor) { 416 Iterator iter = fDirtyViewers.iterator(); 417 418 for (int i=0; i<fDirtyViewers.size(); i++){ 419 Object element = iter.next(); 420 IFlushable flushable = (IFlushable)Utils.getAdapter(element, IFlushable.class); 421 if (flushable != null) 422 flushable.flush(monitor); 423 } 424 } 425 426 } 427 | Popular Tags |