1 11 package org.eclipse.team.internal.ui.synchronize; 12 13 import java.util.ArrayList ; 14 import java.util.Arrays ; 15 import java.util.HashMap ; 16 import java.util.HashSet ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 import java.util.Map ; 20 import java.util.Set ; 21 22 import org.eclipse.core.runtime.*; 23 import org.eclipse.jface.action.IMenuManager; 24 import org.eclipse.jface.action.IToolBarManager; 25 import org.eclipse.jface.dialogs.*; 26 import org.eclipse.jface.util.IPropertyChangeListener; 27 import org.eclipse.jface.util.PropertyChangeEvent; 28 import org.eclipse.jface.viewers.IBasicPropertyConstants; 29 import org.eclipse.swt.widgets.Composite; 30 import org.eclipse.swt.widgets.Display; 31 import org.eclipse.team.core.TeamException; 32 import org.eclipse.team.internal.ui.*; 33 import org.eclipse.team.internal.ui.synchronize.actions.*; 34 import org.eclipse.team.ui.TeamUI; 35 import org.eclipse.team.ui.synchronize.*; 36 import org.eclipse.ui.*; 37 import org.eclipse.ui.actions.ActionFactory; 38 import org.eclipse.ui.part.*; 39 import org.eclipse.ui.progress.IWorkbenchSiteProgressService; 40 41 44 public class SynchronizeView extends PageBookView implements ISynchronizeView, ISynchronizeParticipantListener, IPropertyChangeListener, ISaveablesSource, ISaveablePart { 45 46 49 public final static int MAX_NAME_LENGTH = 100; 50 51 54 private ISynchronizeParticipant activeParticipantRef = null; 55 56 59 private Map fParticipantToPart; 60 61 64 private Map fPartToParticipant; 65 66 69 private SynchronizePageDropDownAction fPageDropDown; 70 71 74 private PinParticipantAction fPinAction; 75 76 79 private RemoveSynchronizeParticipantAction fRemoveCurrentAction; 80 81 84 private RemoveSynchronizeParticipantAction fRemoveAllAction; 85 86 89 private static final String KEY_LAST_ACTIVE_PARTICIPANT_ID = "lastactiveparticipant_id"; private static final String KEY_LAST_ACTIVE_PARTICIPANT_SECONDARY_ID = "lastactiveparticipant_sec_id"; private static final String KEY_SETTINGS_SECTION= "SynchronizeViewSettings"; 93 94 97 public void propertyChange(PropertyChangeEvent event) { 98 Object source = event.getSource(); 99 if (source instanceof ISynchronizeParticipant) { 100 if (event.getProperty().equals(IBasicPropertyConstants.P_TEXT)) { 101 if (source.equals(getParticipant())) { 102 updateTitle(); 103 } 104 } else if (event.getProperty().equals(ModelSynchronizeParticipant.PROP_DIRTY)) { 105 Display.getDefault().asyncExec(new Runnable () { 106 public void run() { 107 firePropertyChange(PROP_DIRTY); 108 } 109 }); 110 } else if (event.getProperty().equals(ModelSynchronizeParticipant.PROP_ACTIVE_SAVEABLE)) { 111 Saveable oldSaveable = (Saveable)event.getOldValue(); 112 Saveable newSaveable = (Saveable)event.getNewValue(); 113 ISaveablesLifecycleListener listener = (ISaveablesLifecycleListener)getSite().getPage().getWorkbenchWindow() 114 .getService(ISaveablesLifecycleListener.class); 115 if (listener != null && oldSaveable != null) 116 listener.handleLifecycleEvent(new SaveablesLifecycleEvent(this, SaveablesLifecycleEvent.POST_CLOSE, new Saveable[] { oldSaveable }, false)); 117 if (listener != null && newSaveable != null) 118 listener.handleLifecycleEvent(new SaveablesLifecycleEvent(this, SaveablesLifecycleEvent.POST_OPEN, new Saveable[] { oldSaveable }, false)); 119 } else if (event.getProperty().equals(ISynchronizeParticipant.P_CONTENT)) { 120 final IWorkbenchSiteProgressService ps = (IWorkbenchSiteProgressService)getSite().getAdapter(IWorkbenchSiteProgressService.class); 121 if (ps != null) 122 Display.getDefault().asyncExec(new Runnable () { 123 public void run() { 124 ps.warnOfContentChange(); 125 } 126 }); 127 } 128 } 129 if (source instanceof ISynchronizePageConfiguration) { 130 ISynchronizePageConfiguration configuration = (ISynchronizePageConfiguration) source; 131 if (event.getProperty().equals(ISynchronizePageConfiguration.P_PAGE_DESCRIPTION)) { 132 if (configuration.getParticipant().equals(getParticipant())) { 133 updateTitle(); 134 } 135 } 136 } 137 } 138 139 142 public void partClosed(IWorkbenchPart part) { 143 super.partClosed(part); 144 } 145 146 149 public ISynchronizeParticipant getParticipant() { 150 return activeParticipantRef; 151 } 152 153 156 protected void showPageRec(PageRec pageRec) { 157 super.showPageRec(pageRec); 158 activeParticipantRef = (ISynchronizeParticipant)fPartToParticipant.get(pageRec.part); 159 updateActionEnablements(); 160 updateTitle(); 161 } 162 163 166 protected void updateTitle() { 167 ISynchronizeParticipant participant = getParticipant(); 168 if (participant == null) { 169 setContentDescription(""); } else { 171 SynchronizeViewWorkbenchPart part = (SynchronizeViewWorkbenchPart)fParticipantToPart.get(participant); 172 ISynchronizePageConfiguration configuration = part.getConfiguration(); 173 String description = (String )configuration.getProperty(ISynchronizePageConfiguration.P_PAGE_DESCRIPTION); 174 if (description == null) 175 description = part.getParticipant().getName(); 176 setContentDescription(Utils.shortenText(MAX_NAME_LENGTH, description)); 179 } 180 } 181 182 185 protected void doDestroyPage(IWorkbenchPart part, PageRec pageRecord) { 186 IPage page = pageRecord.page; 187 page.dispose(); 188 pageRecord.dispose(); 189 SynchronizeViewWorkbenchPart syncPart = (SynchronizeViewWorkbenchPart) part; 190 ISynchronizeParticipant participant = syncPart.getParticipant(); 191 clearCrossReferenceCache(part, participant); 192 } 193 194 private void clearCrossReferenceCache(IWorkbenchPart part, ISynchronizeParticipant participant) { 195 participant.removePropertyChangeListener(this); 196 if (part == null) 197 return; 198 ISynchronizePageConfiguration configuration = ((SynchronizeViewWorkbenchPart)part).getConfiguration(); 199 if (configuration != null) 200 configuration.removePropertyChangeListener(this); 201 fPartToParticipant.remove(part); 202 fParticipantToPart.remove(participant); 203 } 204 205 208 protected PageRec doCreatePage(IWorkbenchPart dummyPart) { 209 SynchronizeViewWorkbenchPart part = (SynchronizeViewWorkbenchPart)dummyPart; 210 ISynchronizeParticipant participant = part.getParticipant(); 211 participant.addPropertyChangeListener(this); 212 ISynchronizePageConfiguration configuration = participant.createPageConfiguration(); 213 part.setConfiguration(configuration); 214 configuration.addPropertyChangeListener(this); 215 IPageBookViewPage page = participant.createPage(configuration); 216 if(page != null) { 217 initPage(page); 218 initPage(configuration, page); 219 page.createControl(getPageBook()); 220 PageRec rec = new PageRec(dummyPart, page); 221 return rec; 222 } 223 return null; 224 } 225 226 229 protected void initPage(ISynchronizePageConfiguration configuration, IPageBookViewPage page) { 230 ((SynchronizePageConfiguration)configuration).setSite(new WorkbenchPartSynchronizePageSite(this, page.getSite(), getDialogSettings(configuration.getParticipant()))); 233 if (page instanceof ISynchronizePage) { 234 try { 235 ((ISynchronizePage)page).init(configuration.getSite()); 236 } catch (PartInitException e) { 237 TeamUIPlugin.log(IStatus.ERROR, e.getMessage(), e); 238 } 239 } 240 page.getSite().getActionBars().setGlobalActionHandler(ActionFactory.REFRESH.getId(), fPageDropDown); 241 page.getSite().getActionBars().updateActionBars(); 242 } 243 244 247 protected boolean isImportant(IWorkbenchPart part) { 248 return part instanceof SynchronizeViewWorkbenchPart; 249 } 250 251 254 public void dispose() { 255 super.dispose(); 256 TeamUI.getSynchronizeManager().removeSynchronizeParticipantListener(this); 257 fPinAction.dispose(); 259 if(activeParticipantRef != null) { 261 rememberCurrentParticipant(); 262 } 263 fParticipantToPart = null; 264 fPartToParticipant = null; 265 } 266 267 270 private void rememberCurrentParticipant() { 271 IDialogSettings section = getDialogSettings(); 272 section.put(KEY_LAST_ACTIVE_PARTICIPANT_ID, activeParticipantRef.getId()); 273 section.put(KEY_LAST_ACTIVE_PARTICIPANT_SECONDARY_ID, activeParticipantRef.getSecondaryId()); 274 } 275 276 279 protected IPage createDefaultPage(PageBook book) { 280 Page page = new MessagePage(); 281 page.createControl(getPageBook()); 282 initPage(page); 283 return page; 284 } 285 286 289 public void participantsAdded(final ISynchronizeParticipant[] participants) { 290 for (int i = 0; i < participants.length; i++) { 291 ISynchronizeParticipant participant = participants[i]; 292 if (isAvailable() && select(TeamUI.getSynchronizeManager().get(participant.getId(), participant.getSecondaryId()))) { 293 SynchronizeViewWorkbenchPart part = new SynchronizeViewWorkbenchPart(participant, getSite()); 294 fParticipantToPart.put(participant, part); 295 fPartToParticipant.put(part, participant); 296 } 297 } 298 Display.getDefault().asyncExec(new Runnable () { 299 public void run() { 300 firePropertyChange(PROP_DIRTY); 301 } 302 }); 303 } 304 305 308 public void participantsRemoved(final ISynchronizeParticipant[] participants) { 309 if (isAvailable()) { 310 Runnable r = new Runnable () { 311 public void run() { 312 for (int i = 0; i < participants.length; i++) { 313 ISynchronizeParticipant participant = participants[i]; 314 if (isAvailable()) { 315 SynchronizeViewWorkbenchPart part = (SynchronizeViewWorkbenchPart)fParticipantToPart.get(participant); 316 if (part != null) { 317 partClosed(part); 318 clearCrossReferenceCache(part, participant); 319 } 320 removeDialogSettings(participant); 322 if (getParticipant() == null) { 323 ISynchronizeParticipantReference[] available = TeamUI.getSynchronizeManager().getSynchronizeParticipants(); 324 if (available.length > 0) { 325 ISynchronizeParticipant p; 326 try { 327 p = available[available.length - 1].getParticipant(); 328 } catch (TeamException e) { 329 return; 330 } 331 display(p); 332 } 333 } 334 } 335 } 336 firePropertyChange(PROP_DIRTY); 337 } 338 }; 339 asyncExec(r); 340 } 341 } 342 343 346 public SynchronizeView() { 347 super(); 348 fParticipantToPart = new HashMap (); 349 fPartToParticipant = new HashMap (); 350 updateTitle(); 351 } 352 353 357 protected void createActions() { 358 fPageDropDown = new SynchronizePageDropDownAction(this); 359 fPinAction = new PinParticipantAction(); 360 fRemoveCurrentAction = new RemoveSynchronizeParticipantAction(this, false); 361 fRemoveAllAction = new RemoveSynchronizeParticipantAction(this, true); 362 updateActionEnablements(); 363 } 364 365 private void updateActionEnablements() { 366 if (fPinAction != null) { 367 fPinAction.setParticipant(activeParticipantRef); 368 } 369 if (fRemoveAllAction != null) { 370 fRemoveAllAction.setEnabled(getParticipant() != null); 371 } 372 if (fRemoveCurrentAction != null) { 373 fRemoveCurrentAction.setEnabled(getParticipant() != null); 374 } 375 } 376 377 382 protected void configureToolBar(IActionBars bars) { 383 IToolBarManager mgr = bars.getToolBarManager(); 384 mgr.add(fPageDropDown); 385 mgr.add(fPinAction); 386 IMenuManager menu = bars.getMenuManager(); 387 menu.add(fPinAction); 388 menu.add(fRemoveCurrentAction); 389 menu.add(fRemoveAllAction); 390 } 391 392 395 public void display(ISynchronizeParticipant participant) { 396 SynchronizeViewWorkbenchPart part = (SynchronizeViewWorkbenchPart)fParticipantToPart.get(participant); 397 if (part != null) { 398 partActivated(part); 399 fPageDropDown.update(); 400 rememberCurrentParticipant(); 401 } 402 } 403 404 407 protected IWorkbenchPart getBootstrapPart() { 408 return null; 409 } 410 411 416 public void asyncExec(Runnable runnable) { 417 if (isAvailable()) { 418 getPageBook().getDisplay().asyncExec(runnable); 419 } 420 } 421 422 438 public void createPartControl(Composite parent) { 439 super.createPartControl(parent); 440 createActions(); 441 configureToolBar(getViewSite().getActionBars()); 442 updateForExistingParticipants(); 443 getViewSite().getActionBars().updateActionBars(); 444 updateTitle(); 445 446 IWorkbenchSiteProgressService progress = (IWorkbenchSiteProgressService)getSite().getAdapter(IWorkbenchSiteProgressService.class); 447 if(progress != null) { 448 progress.showBusyForFamily(ISynchronizeManager.FAMILY_SYNCHRONIZE_OPERATION); 449 } 450 451 PlatformUI.getWorkbench().getHelpSystem().setHelp(parent, IHelpContextIds.SYNC_VIEW); 452 } 453 454 457 private void updateForExistingParticipants() { 458 ISynchronizeManager manager = TeamUI.getSynchronizeManager(); 459 List participants = Arrays.asList(getParticipants()); 460 boolean errorOccurred = false; 461 for (int i = 0; i < participants.size(); i++) { 462 try { 463 ISynchronizeParticipantReference ref = (ISynchronizeParticipantReference)participants.get(i); 464 participantsAdded(new ISynchronizeParticipant[] {ref.getParticipant()}); 465 } catch (TeamException e) { 466 errorOccurred = true; 467 continue; 468 } 469 470 } 471 if (errorOccurred) { 472 participants = Arrays.asList(getParticipants()); 473 } 474 try { 475 if (participants.size() > 0) { 477 ISynchronizeParticipantReference participantToSelect = (ISynchronizeParticipantReference)participants.get(0); 478 IDialogSettings section = getDialogSettings(); 479 String selectedParticipantId = section.get(KEY_LAST_ACTIVE_PARTICIPANT_ID); 480 String selectedParticipantSecId = section.get(KEY_LAST_ACTIVE_PARTICIPANT_SECONDARY_ID); 481 if(selectedParticipantId != null) { 482 ISynchronizeParticipantReference selectedParticipant = manager.get(selectedParticipantId, selectedParticipantSecId); 483 if(selectedParticipant != null) { 484 participantToSelect = selectedParticipant; 485 } 486 } 487 display(participantToSelect.getParticipant()); 488 } 489 490 manager.addSynchronizeParticipantListener(this); 492 } catch (TeamException e) { 493 Utils.handle(e); 494 } 495 } 496 497 private ISynchronizeParticipantReference[] getParticipants() { 498 ISynchronizeManager manager = TeamUI.getSynchronizeManager(); 499 List participants = new ArrayList (); 501 ISynchronizeParticipantReference[] refs = manager.getSynchronizeParticipants(); 502 for (int i = 0; i < refs.length; i++) { 503 ISynchronizeParticipantReference ref =refs[i]; 504 if(select(ref)) { 505 participants.add(ref); 506 } 507 } 508 return (ISynchronizeParticipantReference[]) participants.toArray(new ISynchronizeParticipantReference[participants.size()]); 509 } 510 511 private boolean isAvailable() { 512 return getPageBook() != null && !getPageBook().isDisposed(); 513 } 514 515 518 public IPage getPage(ISynchronizeParticipant participant) { 519 IWorkbenchPart part = (IWorkbenchPart)fParticipantToPart.get(participant); 520 if (part == null) return null; 521 try { 522 return getPageRec(part).page; 523 } catch (NullPointerException e) { 524 return null; 527 } 528 } 529 530 protected boolean select(ISynchronizeParticipantReference ref) { 531 return true; 532 } 533 534 537 private IDialogSettings getDialogSettings() { 538 IDialogSettings workbenchSettings = TeamUIPlugin.getPlugin().getDialogSettings(); 539 IDialogSettings syncViewSettings = workbenchSettings.getSection(KEY_SETTINGS_SECTION); 540 if (syncViewSettings == null) { 541 syncViewSettings = workbenchSettings.addNewSection(KEY_SETTINGS_SECTION); 542 } 543 return syncViewSettings; 544 } 545 546 private String getSettingsKey(ISynchronizeParticipant participant) { 547 String id = participant.getId(); 548 String secondaryId = participant.getSecondaryId(); 549 return secondaryId == null ? id : id + '.' + secondaryId; 550 } 551 552 private IDialogSettings getDialogSettings(ISynchronizeParticipant participant) { 553 String key = getSettingsKey(participant); 554 IDialogSettings viewsSettings = getDialogSettings(); 555 IDialogSettings settings = viewsSettings.getSection(key); 556 if (settings == null) { 557 settings = viewsSettings.addNewSection(key); 558 } 559 return settings; 560 } 561 562 private void removeDialogSettings(ISynchronizeParticipant participant) { 563 String key = getSettingsKey(participant); 564 IDialogSettings settings = getDialogSettings(); 565 if (settings.getSection(key) != null) { 566 getDialogSettings().addSection(new DialogSettings(key)); 569 } 570 } 571 572 575 public Saveable[] getSaveables() { 576 Set result = new HashSet (); 577 for (Iterator iter = fPartToParticipant.keySet().iterator(); iter.hasNext();) { 578 SynchronizeViewWorkbenchPart part = (SynchronizeViewWorkbenchPart) iter.next(); 579 Saveable saveable = getSaveable(part.getParticipant()); 580 if (saveable != null) { 581 result.add(saveable); 582 } 583 } 584 return (Saveable[]) result.toArray(new Saveable[result.size()]); 585 } 586 587 private Saveable getSaveable(ISynchronizeParticipant participant) { 588 if (participant instanceof ModelSynchronizeParticipant) { 589 ModelSynchronizeParticipant msp = (ModelSynchronizeParticipant) participant; 590 return msp.getActiveSaveable(); 591 } 592 return null; 593 } 594 595 598 public Saveable[] getActiveSaveables() { 599 ISynchronizeParticipant participant = getParticipant(); 600 Saveable s = getSaveable(participant); 601 if (s != null) 602 return new Saveable[] { s }; 603 return new Saveable[0]; 604 } 605 606 609 public void doSave(IProgressMonitor monitor) { 610 Saveable[] saveables = getSaveables(); 611 if (saveables.length == 0) 612 return; 613 monitor.beginTask(null, 100* saveables.length); 614 for (int i = 0; i < saveables.length; i++) { 615 Saveable saveable = saveables[i]; 616 try { 617 saveable.doSave(Policy.subMonitorFor(monitor, 100)); 618 } catch (CoreException e) { 619 ErrorDialog.openError(getSite().getShell(), null, e.getMessage(), e.getStatus()); 620 } 621 Policy.checkCanceled(monitor); 622 } 623 monitor.done(); 624 firePropertyChange(PROP_DIRTY); 625 } 626 627 630 public void doSaveAs() { 631 } 633 634 637 public boolean isDirty() { 638 Saveable[] saveables = getSaveables(); 639 for (int i = 0; i < saveables.length; i++) { 640 Saveable saveable = saveables[i]; 641 if (saveable.isDirty()) 642 return true; 643 } 644 return false; 645 } 646 647 650 public boolean isSaveAsAllowed() { 651 return false; 652 } 653 654 657 public boolean isSaveOnCloseNeeded() { 658 return true; 659 } 660 } 661 | Popular Tags |