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.ToolBarManager; 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.graphics.Image; 24 import org.eclipse.swt.widgets.*; 25 import org.eclipse.team.internal.ui.*; 26 import org.eclipse.team.internal.ui.synchronize.*; 27 import org.eclipse.team.ui.PageCompareEditorInput; 28 import org.eclipse.team.ui.PageSaveablePart; 29 import org.eclipse.team.ui.mapping.ISynchronizationCompareInput; 30 import org.eclipse.team.ui.mapping.SaveableComparison; 31 import org.eclipse.ui.PartInitException; 32 import org.eclipse.ui.part.IPageBookViewPage; 33 34 43 public class ParticipantPageSaveablePart extends PageSaveablePart implements IContentChangeListener { 44 45 private ISynchronizeParticipant participant; 46 private ISynchronizePageConfiguration pageConfiguration; 47 private Image titleImage; 48 49 private IPageBookViewPage page; 50 private DialogSynchronizePageSite site; 51 52 private IPropertyChangeListener listener; 53 private Viewer viewer; 54 55 67 public ParticipantPageSaveablePart(Shell shell, CompareConfiguration cc, ISynchronizePageConfiguration pageConfiguration, ISynchronizeParticipant participant) { 68 super(shell,cc); 69 this.participant = participant; 70 this.pageConfiguration = pageConfiguration; 71 } 72 73 76 public void dispose() { 77 if(titleImage != null) { 78 titleImage.dispose(); 79 } 80 if (page != null) 81 page.dispose(); 82 if (site != null) 83 site.dispose(); 84 pageConfiguration.removePropertyChangeListener(listener); 85 super.dispose(); 86 } 87 88 91 public Image getTitleImage() { 92 if(titleImage == null) { 93 titleImage = participant.getImageDescriptor().createImage(); 94 } 95 return titleImage; 96 } 97 98 101 public String getTitle() { 102 return Utils.shortenText(SynchronizeView.MAX_NAME_LENGTH, participant.getName()); 103 } 104 105 108 public boolean isDirty() { 109 if (participant instanceof ModelSynchronizeParticipant) { 110 ModelSynchronizeParticipant msp = (ModelSynchronizeParticipant) participant; 111 SaveableComparison currentBuffer = msp.getActiveSaveable(); 112 if (currentBuffer != null) { 113 return currentBuffer.isDirty(); 114 } 115 } 116 return super.isDirty(); 117 } 118 119 122 public void contentChanged(IContentChangeNotifier source) { 123 try { 124 if (source instanceof DiffNode) { 125 commit(new NullProgressMonitor(), (DiffNode) source); 126 } else if (source instanceof LocalResourceTypedElement) { 127 ((LocalResourceTypedElement) source).commit(new NullProgressMonitor()); 128 } 129 } catch (CoreException e) { 130 Utils.handle(e); 131 } 132 } 133 134 137 public void doSave(IProgressMonitor pm) { 138 super.doSave(pm); 140 Object input = viewer.getInput(); 141 if (input instanceof ISynchronizeModelElement) { 142 ISynchronizeModelElement root = (ISynchronizeModelElement)input; 143 if (root != null && root instanceof DiffNode) { 144 try { 145 commit(pm, (DiffNode)root); 146 } catch (CoreException e) { 147 Utils.handle(e); 148 } finally { 149 setDirty(false); 150 } 151 } 152 } 153 } 154 155 158 protected Control createPage(Composite parent, ToolBarManager toolBarManager) { 159 listener = new IPropertyChangeListener() { 160 public void propertyChange(PropertyChangeEvent event) { 161 if (event.getProperty().equals(ISynchronizePageConfiguration.P_PAGE_DESCRIPTION)) { 162 updateDescription(); 163 } 164 } 165 }; 166 pageConfiguration.addPropertyChangeListener(listener); 167 updateDescription(); 168 169 page = participant.createPage(pageConfiguration); 170 site = new DialogSynchronizePageSite(getShell(), true); 171 ((SynchronizePageConfiguration)pageConfiguration).setSite(site); 172 site.createActionBars(toolBarManager); 173 try { 174 ((ISynchronizePage)page).init(pageConfiguration.getSite()); 175 } catch (PartInitException e1) { 176 } 177 178 page.createControl(parent); 179 180 initializeDiffViewer(((ISynchronizePage)page).getViewer()); 181 182 page.setActionBars(site.getActionBars()); 183 toolBarManager.update(true); 184 viewer = ((ISynchronizePage)page).getViewer(); 185 186 setNavigator(pageConfiguration); 187 return page.getControl(); 188 } 189 190 193 protected final ISelectionProvider getSelectionProvider() { 194 return ((ISynchronizePage)page).getViewer(); 195 } 196 197 private void updateDescription() { 198 String description = (String )pageConfiguration.getProperty(ISynchronizePageConfiguration.P_PAGE_DESCRIPTION); 199 if (description != null) { 200 setPageDescription(description); 201 } 202 } 203 204 211 private void initializeDiffViewer(Viewer viewer) { 212 if (viewer instanceof StructuredViewer) { 213 ((StructuredViewer) viewer).addOpenListener(new IOpenListener() { 214 public void open(OpenEvent event) { 215 ISelection s = event.getSelection(); 216 final SyncInfoModelElement node = getElement(s); 217 if (node == null) { 218 ICompareInput input = getCompareInput(s); 219 if (input != null) { 220 prepareCompareInput(input); 221 } 222 } else { 223 prepareCompareInput(node); 224 } 225 } 226 }); 227 } 228 } 229 230 234 protected void prepareInput(final ICompareInput input, CompareConfiguration configuration, IProgressMonitor monitor) throws InvocationTargetException { 235 monitor.beginTask(TeamUIMessages.SyncInfoCompareInput_3, 100); 236 monitor.setTaskName(TeamUIMessages.SyncInfoCompareInput_3); 237 try { 238 checkForBufferChange(pageConfiguration.getSite().getShell(), input, false , monitor); 240 if (input instanceof SyncInfoModelElement) { 241 final SyncInfoModelElement node = (SyncInfoModelElement) input; 242 IResource resource = node.getResource(); 243 if (resource != null && resource.getType() == IResource.FILE) { 244 participant.prepareCompareInput(node, configuration, monitor); 245 } 246 } else { 247 ISynchronizationCompareInput adapter = asModelCompareInput(input); 248 if (adapter != null) { 249 adapter.prepareInput(configuration, Policy.subMonitorFor(monitor, 90)); 250 } 251 } 252 } catch (CoreException e) { 253 throw new InvocationTargetException (e); 254 } finally { 255 monitor.done(); 256 } 257 } 258 259 private void checkForBufferChange(Shell shell, final ICompareInput input, boolean cancelAllowed, IProgressMonitor monitor) throws CoreException { 260 ISynchronizeParticipant participant = pageConfiguration.getParticipant(); 261 if (participant instanceof ModelSynchronizeParticipant) { 262 ModelSynchronizeParticipant msp = (ModelSynchronizeParticipant) participant; 263 if (input instanceof ISynchronizationCompareInput) { 264 ISynchronizationCompareInput mci = (ISynchronizationCompareInput) input; 265 msp.checkForBufferChange(shell, mci, cancelAllowed, monitor); 266 } 267 } 268 } 269 270 private ISynchronizationCompareInput asModelCompareInput(ICompareInput input) { 271 return (ISynchronizationCompareInput)Utils.getAdapter(input, ISynchronizationCompareInput.class); 272 } 273 274 private SyncInfoModelElement getElement(ISelection selection) { 275 ICompareInput input = getCompareInput(selection); 276 if(input instanceof SyncInfoModelElement) { 277 return (SyncInfoModelElement)input; 278 } 279 return null; 280 } 281 282 private static void commit(IProgressMonitor pm, DiffNode node) throws CoreException { 283 ITypedElement left = node.getLeft(); 284 if (left instanceof LocalResourceTypedElement) 285 ((LocalResourceTypedElement) left).commit(pm); 286 287 ITypedElement right = node.getRight(); 288 if (right instanceof LocalResourceTypedElement) 289 ((LocalResourceTypedElement) right).commit(pm); 290 291 IDiffElement[] children = node.getChildren(); 292 for (int i = 0; i < children.length; i++) { 293 commit(pm, (DiffNode)children[i]); 294 } 295 } 296 297 302 public ISynchronizePageConfiguration getPageConfiguration() { 303 return pageConfiguration; 304 } 305 306 311 public ISynchronizeParticipant getParticipant() { 312 return participant; 313 } 314 315 319 protected ICompareInput getCompareInput(ISelection selection) { 320 ICompareInput compareInput = super.getCompareInput(selection); 321 if (compareInput != null) 322 return compareInput; 323 324 if (selection != null && selection instanceof IStructuredSelection) { 325 IStructuredSelection ss= (IStructuredSelection) selection; 326 if (ss.size() == 1) { 327 Object o = ss.getFirstElement(); 328 if (participant instanceof ModelSynchronizeParticipant) { 329 ModelSynchronizeParticipant msp = (ModelSynchronizeParticipant) participant; 330 return msp.asCompareInput(o); 331 } 332 } 333 } 334 return null; 335 } 336 337 338 339 } 340 | Popular Tags |