1 11 package org.eclipse.team.ui.synchronize; 12 13 import java.lang.reflect.InvocationTargetException ; 14 15 import org.eclipse.compare.*; 16 import org.eclipse.compare.structuremergeviewer.*; 17 import org.eclipse.core.resources.IResource; 18 import org.eclipse.core.runtime.*; 19 import org.eclipse.jface.action.IToolBarManager; 20 import org.eclipse.jface.util.IPropertyChangeListener; 21 import org.eclipse.jface.util.PropertyChangeEvent; 22 import org.eclipse.jface.viewers.*; 23 import org.eclipse.swt.SWT; 24 import org.eclipse.swt.graphics.Image; 25 import org.eclipse.swt.layout.GridData; 26 import org.eclipse.swt.layout.GridLayout; 27 import org.eclipse.swt.widgets.*; 28 import org.eclipse.team.internal.ui.*; 29 import org.eclipse.team.internal.ui.synchronize.*; 30 import org.eclipse.team.ui.PageCompareEditorInput; 31 import org.eclipse.team.ui.TeamUI; 32 import org.eclipse.team.ui.mapping.ISynchronizationCompareInput; 33 import org.eclipse.team.ui.mapping.SaveableComparison; 34 import org.eclipse.ui.PartInitException; 35 import org.eclipse.ui.part.IPage; 36 import org.eclipse.ui.part.IPageBookViewPage; 37 38 45 public class ParticipantPageCompareEditorInput extends PageCompareEditorInput { 46 47 private ISynchronizeParticipant participant; 48 private ISynchronizePageConfiguration pageConfiguration; 49 50 private Image titleImage; 51 private IPageBookViewPage page; 52 private DialogSynchronizePageSite site; 53 private IPropertyChangeListener listener; 54 private Button rememberParticipantButton; 55 56 67 public ParticipantPageCompareEditorInput( 68 CompareConfiguration configuration, 69 ISynchronizePageConfiguration pageConfiguration, 70 ISynchronizeParticipant participant) { 71 super(configuration); 72 this.pageConfiguration = pageConfiguration; 73 this.participant = participant; 74 pageConfiguration.setRunnableContext(this); 75 } 76 77 80 protected Object prepareInput(IProgressMonitor monitor) 81 throws InvocationTargetException , InterruptedException { 82 setTitle(Utils.shortenText(SynchronizeView.MAX_NAME_LENGTH, participant.getName())); 83 return participant; 84 } 85 86 89 public Image getTitleImage() { 90 if(titleImage == null) { 91 titleImage = participant.getImageDescriptor().createImage(); 92 } 93 return titleImage; 94 } 95 96 99 protected void handleDispose() { 100 if(titleImage != null) { 101 titleImage.dispose(); 102 } 103 if (page != null) 104 page.dispose(); 105 if (site != null) 106 site.dispose(); 107 pageConfiguration.removePropertyChangeListener(listener); 108 super.handleDispose(); 109 } 110 111 114 protected IPage createPage(CompareViewerPane parent, 115 IToolBarManager toolBarManager) { 116 listener = new IPropertyChangeListener() { 117 public void propertyChange(PropertyChangeEvent event) { 118 if (event.getProperty().equals(ISynchronizePageConfiguration.P_PAGE_DESCRIPTION)) { 119 updateDescription(); 120 } 121 } 122 }; 123 pageConfiguration.addPropertyChangeListener(listener); 124 updateDescription(); 125 126 page = participant.createPage(pageConfiguration); 127 site = new DialogSynchronizePageSite(parent.getShell(), true); 128 ((SynchronizePageConfiguration)pageConfiguration).setSite(site); 129 site.createActionBars(toolBarManager); 130 try { 131 ((ISynchronizePage)page).init(pageConfiguration.getSite()); 132 } catch (PartInitException e1) { 133 } 134 135 page.createControl(parent); 136 137 page.setActionBars(site.getActionBars()); 138 toolBarManager.update(true); 139 return page; 140 } 141 142 void updateDescription() { 143 String description = (String )pageConfiguration.getProperty(ISynchronizePageConfiguration.P_PAGE_DESCRIPTION); 144 if (description != null) { 145 setPageDescription(description); 146 } 147 } 148 149 152 protected ISelectionProvider getSelectionProvider() { 153 return ((ISynchronizePage)page).getViewer(); 154 } 155 156 protected ICompareInput asCompareInput(ISelection selection) { 157 ICompareInput compareInput = super.asCompareInput(selection); 158 if (compareInput != null) 159 return compareInput; 160 161 if (selection != null && selection instanceof IStructuredSelection) { 162 IStructuredSelection ss= (IStructuredSelection) selection; 163 if (ss.size() == 1) { 164 Object o = ss.getFirstElement(); 165 if (participant instanceof ModelSynchronizeParticipant) { 166 ModelSynchronizeParticipant msp = (ModelSynchronizeParticipant) participant; 167 return msp.asCompareInput(o); 168 } 169 } 170 } 171 return null; 172 } 173 174 177 protected void prepareInput(ICompareInput input, 178 CompareConfiguration configuration, IProgressMonitor monitor) 179 throws InvocationTargetException { 180 monitor.beginTask(TeamUIMessages.SyncInfoCompareInput_3, 100); 181 monitor.setTaskName(TeamUIMessages.SyncInfoCompareInput_3); 182 try { 183 checkForBufferChange(pageConfiguration.getSite().getShell(), input, false , monitor); 185 if (input instanceof SyncInfoModelElement) { 186 final SyncInfoModelElement node = (SyncInfoModelElement) input; 187 IResource resource = node.getResource(); 188 if (resource != null && resource.getType() == IResource.FILE) { 189 participant.prepareCompareInput(node, configuration, monitor); 190 } 191 } else { 192 ISynchronizationCompareInput adapter = asModelCompareInput(input); 193 if (adapter != null) { 194 adapter.prepareInput(configuration, Policy.subMonitorFor(monitor, 90)); 195 } 196 } 197 } catch (CoreException e) { 198 throw new InvocationTargetException (e); 199 } finally { 200 monitor.done(); 201 } 202 } 203 204 private void checkForBufferChange(Shell shell, final ICompareInput input, boolean cancelAllowed, IProgressMonitor monitor) throws CoreException { 205 ISynchronizeParticipant participant = pageConfiguration.getParticipant(); 206 if (participant instanceof ModelSynchronizeParticipant) { 207 ModelSynchronizeParticipant msp = (ModelSynchronizeParticipant) participant; 208 if (input instanceof ISynchronizationCompareInput) { 209 ISynchronizationCompareInput mci = (ISynchronizationCompareInput) input; 210 msp.checkForBufferChange(shell, mci, cancelAllowed, monitor); 211 } 212 } 213 } 214 215 private ISynchronizationCompareInput asModelCompareInput(ICompareInput input) { 216 return (ISynchronizationCompareInput)Utils.getAdapter(input, ISynchronizationCompareInput.class); 217 } 218 219 222 public boolean isSaveNeeded() { 223 if (participant instanceof ModelSynchronizeParticipant) { 224 ModelSynchronizeParticipant msp = (ModelSynchronizeParticipant) participant; 225 SaveableComparison currentBuffer = msp.getActiveSaveable(); 226 if (currentBuffer != null) { 227 return currentBuffer.isDirty(); 228 } 229 } 230 return super.isSaveNeeded(); 231 } 232 233 236 public void saveChanges(IProgressMonitor monitor) throws CoreException { 237 super.saveChanges(monitor); 238 Object input = ((ISynchronizePage)page).getViewer().getInput(); 239 if (input instanceof ISynchronizeModelElement) { 240 ISynchronizeModelElement root = (ISynchronizeModelElement)input; 241 if (root != null && root instanceof DiffNode) { 242 try { 243 commit(monitor, (DiffNode)root); 244 } catch (CoreException e) { 245 Utils.handle(e); 246 } finally { 247 setDirty(false); 248 } 249 } 250 } 251 } 252 253 private static void commit(IProgressMonitor pm, DiffNode node) throws CoreException { 254 ITypedElement left = node.getLeft(); 255 if (left instanceof LocalResourceTypedElement) 256 ((LocalResourceTypedElement) left).commit(pm); 257 258 ITypedElement right = node.getRight(); 259 if (right instanceof LocalResourceTypedElement) 260 ((LocalResourceTypedElement) right).commit(pm); 261 262 IDiffElement[] children = node.getChildren(); 263 for (int i = 0; i < children.length; i++) { 264 commit(pm, (DiffNode)children[i]); 265 } 266 } 267 268 271 public void contentChanged(IContentChangeNotifier source) { 272 super.contentChanged(source); 273 try { 274 if (source instanceof DiffNode) { 275 commit(new NullProgressMonitor(), (DiffNode) source); 276 } else if (source instanceof LocalResourceTypedElement) { 277 ((LocalResourceTypedElement) source).commit(new NullProgressMonitor()); 278 } 279 } catch (CoreException e) { 280 Utils.handle(e); 281 } 282 } 283 284 289 public ISynchronizePageConfiguration getPageConfiguration() { 290 return pageConfiguration; 291 } 292 293 298 public ISynchronizeParticipant getParticipant() { 299 return participant; 300 } 301 302 305 public Control createContents(Composite parent) { 306 if (shouldCreateRememberButton()) { 307 Composite composite = new Composite(parent, SWT.NONE); 308 composite.setLayout(new GridLayout()); 309 Control control = super.createContents(composite); 310 control.setLayoutData(new GridData(GridData.FILL_BOTH)); 311 rememberParticipantButton = new Button(composite, SWT.CHECK); 312 rememberParticipantButton.setText(TeamUIMessages.ParticipantCompareDialog_1); 313 rememberParticipantButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 314 return composite; 315 } 316 return super.createContents(parent); 317 } 318 319 326 protected boolean isOfferToRememberParticipant() { 327 return true; 328 } 329 330 private boolean shouldCreateRememberButton() { 331 return isOfferToRememberParticipant() && participant != null && ! particantRegisteredWithSynchronizeManager(participant); 332 } 333 334 private boolean isRememberParticipant() { 335 return getParticipant() != null && rememberParticipantButton != null && rememberParticipantButton.getSelection(); 336 } 337 338 private boolean particantRegisteredWithSynchronizeManager(ISynchronizeParticipant participant) { 339 return TeamUI.getSynchronizeManager().get(participant.getId(), participant.getSecondaryId()) != null; 340 } 341 342 private void rememberParticipant() { 343 if(getParticipant() != null) { 344 ISynchronizeManager mgr = TeamUI.getSynchronizeManager(); 345 ISynchronizeView view = mgr.showSynchronizeViewInActivePage(); 346 mgr.addSynchronizeParticipants(new ISynchronizeParticipant[] {getParticipant()}); 347 view.display(participant); 348 } 349 } 350 351 354 public boolean okPressed() { 355 if (isEditable()) { 356 super.okPressed(); 358 return false; 359 } 360 if (isRememberParticipant()) 362 rememberParticipant(); 363 return super.okPressed(); 364 } 365 366 369 public void cancelPressed() { 370 if (isEditable() && isRememberParticipant()) 372 rememberParticipant(); 373 super.cancelPressed(); 374 } 375 376 379 public String getOKButtonLabel() { 380 if (isEditable()) 381 return TeamUIMessages.ParticipantPageCompareEditorInput_0; 382 return TeamUIMessages.ResourceMappingMergeOperation_2; 383 } 384 385 private boolean isEditable() { 386 return getCompareConfiguration().isLeftEditable() 387 || getCompareConfiguration().isRightEditable(); 388 } 389 390 393 public String getCancelButtonLabel() { 394 return TeamUIMessages.ResourceMappingMergeOperation_2; 395 } 396 397 } 398 | Popular Tags |