1 11 package org.eclipse.team.internal.ui.synchronize; 12 13 import java.util.HashMap ; 14 import java.util.Map ; 15 16 import org.eclipse.core.runtime.*; 17 import org.eclipse.jface.action.IMenuManager; 18 import org.eclipse.jface.operation.IRunnableContext; 19 import org.eclipse.jface.util.IPropertyChangeListener; 20 import org.eclipse.jface.util.PropertyChangeEvent; 21 import org.eclipse.jface.viewers.ILabelDecorator; 22 import org.eclipse.team.core.diff.IDiff; 23 import org.eclipse.team.core.diff.IThreeWayDiff; 24 import org.eclipse.team.core.mapping.IResourceDiff; 25 import org.eclipse.team.core.synchronize.SyncInfoSet; 26 import org.eclipse.team.internal.ui.TeamUIPlugin; 27 import org.eclipse.team.internal.ui.mapping.CommonViewerAdvisor; 28 import org.eclipse.team.ui.synchronize.*; 29 import org.eclipse.ui.IActionBars; 30 import org.eclipse.ui.actions.ActionContext; 31 32 39 public class SynchronizePageConfiguration extends SynchronizePageActionGroup implements ISynchronizePageConfiguration { 40 41 47 public static final String P_MODEL = TeamUIPlugin.ID + ".P_MODEL"; 49 56 public static final String P_ADVISOR = TeamUIPlugin.ID + ".P_ADVISOR"; 58 64 public static final String P_NAVIGATOR = TeamUIPlugin.ID + ".P_NAVIGATOR"; 66 71 public static final String P_INPUT_NAVIGATOR = TeamUIPlugin.ID + ".P_INPUT_NAVIGATOR"; 73 79 public static final String P_MODEL_MANAGER = TeamUIPlugin.ID + ".P_MODEL_MANAGER"; 81 86 public static final String P_WORKING_SET_SYNC_INFO_SET = TeamUIPlugin.ID + ".P_WORKING_SET_SYNC_INFO_SET"; 88 93 public static final String P_PARTICIPANT_SYNC_INFO_SET = TeamUIPlugin.ID + ".P_PARTICIPANT_SYNC_INFO_SET"; 95 100 public static final String P_OPEN_ACTION = TeamUIPlugin.ID + ".P_OPEN_ACTION"; 102 105 public static final String P_VIEWER_STYLE = TeamUIPlugin.ID + ".P_VIEWER_STYLE"; 107 public static final int CHECKBOX = TreeViewerAdvisor.CHECKBOX; 108 109 private static final int UNINITIALIZED = 0; 111 private static final int INITIALIZED = 1; 112 private static final int DISPOSED = 2; 113 114 private ISynchronizeParticipant participant; 115 private ISynchronizePageSite site; 116 private ListenerList propertyChangeListeners = new ListenerList(ListenerList.IDENTITY); 117 private ListenerList actionContributions = new ListenerList(ListenerList.IDENTITY); 118 private Map properties = new HashMap (); 119 private int actionState = UNINITIALIZED; 120 private ISynchronizePage page; 121 private IRunnableContext context; 122 123 127 public SynchronizePageConfiguration(ISynchronizeParticipant participant) { 128 this.participant = participant; 129 setProperty(P_CONTEXT_MENU, DEFAULT_CONTEXT_MENU); 130 setProperty(P_TOOLBAR_MENU, DEFAULT_TOOLBAR_MENU); 131 setProperty(P_VIEW_MENU, DEFAULT_VIEW_MENU); 132 setProperty(P_COMPARISON_TYPE, THREE_WAY); 133 } 134 135 138 public ISynchronizeParticipant getParticipant() { 139 return participant; 140 } 141 142 145 public ISynchronizePageSite getSite() { 146 return site; 147 } 148 149 154 public void setSite(ISynchronizePageSite site) { 155 this.site = site; 156 } 157 158 161 public void addPropertyChangeListener(IPropertyChangeListener listener) { 162 synchronized(propertyChangeListeners) { 163 propertyChangeListeners.add(listener); 164 } 165 } 166 167 170 public void removePropertyChangeListener(IPropertyChangeListener listener) { 171 synchronized(propertyChangeListeners) { 172 propertyChangeListeners.remove(listener); 173 } 174 } 175 176 179 public void setProperty(String key, Object newValue) { 180 Object oldValue = properties.get(key); 181 if (page == null || page.aboutToChangeProperty(this, key, newValue)) { 182 properties.put(key, newValue); 183 if (oldValue == null || !oldValue.equals(newValue)) 184 firePropertyChange(key, oldValue, newValue); 185 } 186 } 187 188 191 public Object getProperty(String key) { 192 return properties.get(key); 193 } 194 195 198 public void addActionContribution(SynchronizePageActionGroup contribution) { 199 int currentActionState; 200 synchronized(actionContributions) { 201 currentActionState = actionState; 203 if (currentActionState != DISPOSED) 204 actionContributions.add(contribution); 205 } 206 if (currentActionState == INITIALIZED) { 207 contribution.initialize(this); 212 if (actionState == DISPOSED) { 213 contribution .dispose(); 214 } 215 } else if (currentActionState == DISPOSED) { 216 contribution.dispose(); 217 } 218 } 219 220 223 public void removeActionContribution(SynchronizePageActionGroup contribution) { 224 synchronized(actionContributions) { 225 actionContributions.remove(contribution); 226 } 227 } 228 229 private void firePropertyChange(String key, Object oldValue, Object newValue) { 230 Object [] listeners; 231 synchronized(propertyChangeListeners) { 232 listeners = propertyChangeListeners.getListeners(); 233 } 234 final PropertyChangeEvent event = new PropertyChangeEvent(this, key, oldValue, newValue); 235 for (int i = 0; i < listeners.length; i++) { 236 final IPropertyChangeListener listener = (IPropertyChangeListener)listeners[i]; 237 SafeRunner.run(new ISafeRunnable() { 238 public void handleException(Throwable exception) { 239 } 241 public void run() throws Exception { 242 listener.propertyChange(event); 243 } 244 }); 245 } 246 } 247 248 251 public void initialize(final ISynchronizePageConfiguration configuration) { 252 super.initialize(configuration); 253 final Object [] listeners; 255 synchronized(actionContributions) { 256 if (actionState != UNINITIALIZED) { 257 return; 259 } 260 actionState = INITIALIZED; 261 listeners = actionContributions.getListeners(); 262 } 263 for (int i= 0; i < listeners.length; i++) { 264 final SynchronizePageActionGroup contribution = (SynchronizePageActionGroup)listeners[i]; 265 SafeRunner.run(new ISafeRunnable() { 266 public void handleException(Throwable exception) { 267 } 269 public void run() throws Exception { 270 contribution.initialize(configuration); 271 } 272 }); 273 } 274 } 275 276 279 public void setContext(final ActionContext context) { 280 super.setContext(context); 281 final Object [] listeners; 282 synchronized(actionContributions) { 283 listeners = actionContributions.getListeners(); 284 } 285 for (int i= 0; i < listeners.length; i++) { 286 final SynchronizePageActionGroup contribution = (SynchronizePageActionGroup)listeners[i]; 287 SafeRunner.run(new ISafeRunnable() { 288 public void handleException(Throwable exception) { 289 } 291 public void run() throws Exception { 292 contribution.setContext(context); 293 } 294 }); 295 } 296 } 297 298 303 public void fillContextMenu(final IMenuManager manager) { 304 final Object [] listeners; 305 synchronized(actionContributions) { 306 listeners = actionContributions.getListeners(); 307 } 308 for (int i= 0; i < listeners.length; i++) { 309 final SynchronizePageActionGroup contribution = (SynchronizePageActionGroup)listeners[i]; 310 SafeRunner.run(new ISafeRunnable() { 311 public void handleException(Throwable exception) { 312 } 314 public void run() throws Exception { 315 contribution.fillContextMenu(manager); 316 } 317 }); 318 } 319 } 320 321 325 public void fillActionBars(final IActionBars actionBars) { 326 if (actionState == UNINITIALIZED) { 327 initialize(this); 328 } 329 final Object [] listeners; 330 synchronized(actionContributions) { 331 listeners = actionContributions.getListeners(); 332 } 333 for (int i= 0; i < listeners.length; i++) { 334 final SynchronizePageActionGroup contribution = (SynchronizePageActionGroup)listeners[i]; 335 SafeRunner.run(new ISafeRunnable() { 336 public void handleException(Throwable exception) { 337 } 339 public void run() throws Exception { 340 contribution.fillActionBars(actionBars); 341 } 342 }); 343 } 344 } 345 346 349 public void updateActionBars() { 350 final Object [] listeners; 351 synchronized(actionContributions) { 352 listeners = actionContributions.getListeners(); 353 } 354 for (int i= 0; i < listeners.length; i++) { 355 final SynchronizePageActionGroup contribution = (SynchronizePageActionGroup)listeners[i]; 356 SafeRunner.run(new ISafeRunnable() { 357 public void handleException(Throwable exception) { 358 } 360 public void run() throws Exception { 361 contribution.updateActionBars(); 362 } 363 }); 364 } 365 } 366 367 370 public void modelChanged(final ISynchronizeModelElement root) { 371 final Object [] listeners; 372 synchronized(actionContributions) { 373 listeners = actionContributions.getListeners(); 374 } 375 for (int i= 0; i < listeners.length; i++) { 376 final SynchronizePageActionGroup contribution = (SynchronizePageActionGroup)listeners[i]; 377 SafeRunner.run(new ISafeRunnable() { 378 public void handleException(Throwable exception) { 379 } 381 public void run() throws Exception { 382 contribution.modelChanged(root); 383 } 384 }); 385 } 386 } 387 388 391 public void dispose() { 392 super.dispose(); 393 final Object [] listeners; 394 synchronized(actionContributions) { 395 listeners = actionContributions.getListeners(); 396 actionState = DISPOSED; 397 } 398 for (int i= 0; i < listeners.length; i++) { 399 final SynchronizePageActionGroup contribution = (SynchronizePageActionGroup)listeners[i]; 400 SafeRunner.run(new ISafeRunnable() { 401 public void handleException(Throwable exception) { 402 } 404 public void run() throws Exception { 405 contribution.dispose(); 406 } 407 }); 408 } 409 } 410 411 414 public void setMenuGroups(String menuPropertyId, String [] groups) { 415 setProperty(menuPropertyId, groups); 416 } 417 418 421 public void addMenuGroup(String menuPropertyId, String groupId) { 422 String [] menuGroups = (String [])getProperty(menuPropertyId); 423 if (menuGroups == null) { 424 menuGroups = getDefault(menuPropertyId); 425 } 426 String [] newGroups = new String [menuGroups.length + 1]; 427 System.arraycopy(menuGroups, 0, newGroups, 0, menuGroups.length); 428 newGroups[menuGroups.length] = groupId; 429 setProperty(menuPropertyId, newGroups); 430 } 431 432 435 public boolean hasMenuGroup(String menuPropertyId, String groupId) { 436 String [] groups = (String [])getProperty(menuPropertyId); 437 if (groups == null) { 438 groups = getDefault(menuPropertyId); 439 } 440 for (int i = 0; i < groups.length; i++) { 441 String string = groups[i]; 442 if (string.equals(groupId)) return true; 443 } 444 return false; 445 } 446 447 protected String [] getDefault(String menuPropertyId) { 448 if (menuPropertyId.equals(P_CONTEXT_MENU)) { 449 return DEFAULT_CONTEXT_MENU; 450 } else if (menuPropertyId.equals(P_VIEW_MENU)) { 451 return DEFAULT_VIEW_MENU; 452 } else if (menuPropertyId.equals(P_TOOLBAR_MENU)) { 453 return DEFAULT_TOOLBAR_MENU; 454 } else { 455 return new String [0]; 456 } 457 } 458 459 462 public void addLabelDecorator(ILabelDecorator decorator) { 463 ILabelDecorator[] decorators = (ILabelDecorator[])getProperty(P_LABEL_DECORATORS); 464 if (decorators == null) { 465 decorators = new ILabelDecorator[0]; 466 } 467 for (int i = 0; i < decorators.length; i++) { 469 ILabelDecorator d = decorators[i]; 470 if (d == decorator) { 471 return; 472 } 473 } 474 ILabelDecorator[] newDecorators = new ILabelDecorator[decorators.length + 1]; 475 System.arraycopy(decorators, 0, newDecorators, 0, decorators.length); 476 newDecorators[decorators.length] = decorator; 477 setProperty(P_LABEL_DECORATORS, newDecorators); 478 } 479 480 484 public String getGroupId(String group) { 485 String id = getParticipant().getId(); 486 if (getParticipant().getSecondaryId() != null) { 487 id += "."; id += getParticipant().getSecondaryId(); 489 } 490 return id + "." + group; } 492 493 496 public int getMode() { 497 Object o = getProperty(P_MODE); 498 if (o instanceof Integer ) { 499 return ((Integer )o).intValue(); 500 } 501 return 0; 502 } 503 504 507 public void setMode(int mode) { 508 if (isModeSupported(mode)) 509 setProperty(P_MODE, new Integer (mode)); 510 } 511 512 public boolean isModeSupported(int mode) { 513 return (getSupportedModes() & mode) > 0; 514 } 515 516 public int getSupportedModes() { 517 Object o = getProperty(P_SUPPORTED_MODES); 518 if (o instanceof Integer ) { 519 return ((Integer )o).intValue(); 520 } 521 return 0; 522 } 523 524 527 public void setSupportedModes(int modes) { 528 setProperty(P_SUPPORTED_MODES, new Integer (modes)); 529 } 530 531 534 public ISynchronizePage getPage() { 535 return page; 536 } 537 540 public void setPage(ISynchronizePage page) { 541 this.page = page; 542 } 543 544 547 public int getViewerStyle() { 548 Object o = getProperty(P_VIEWER_STYLE); 549 if (o instanceof Integer ) { 550 return ((Integer )o).intValue(); 551 } 552 return 0; 553 } 554 555 558 public void setViewerStyle(int style) { 559 setProperty(P_VIEWER_STYLE, new Integer (style)); 560 } 561 562 565 public SyncInfoSet getSyncInfoSet() { 566 Object o = getProperty(P_SYNC_INFO_SET); 567 if (o instanceof SyncInfoSet) { 568 return (SyncInfoSet)o; 569 } 570 return null; 571 } 572 573 576 public String getComparisonType() { 577 return (String )getProperty(P_COMPARISON_TYPE); 578 } 579 580 583 public void setComparisonType(String type) { 584 setProperty(P_COMPARISON_TYPE,type); 585 } 586 587 590 public void setRunnableContext(IRunnableContext context) { 591 this.context = context; 592 } 593 594 597 public IRunnableContext getRunnableContext() { 598 return context; 599 } 600 601 public String getViewerId() { 602 String viewerId = (String )getProperty(P_VIEWER_ID); 603 if (viewerId != null) 604 return viewerId; 605 return CommonViewerAdvisor.TEAM_NAVIGATOR_CONTENT; 606 } 607 608 614 public boolean isVisible(IDiff node) { 615 if (getComparisonType() == ISynchronizePageConfiguration.THREE_WAY 616 && node instanceof IThreeWayDiff) { 617 IThreeWayDiff twd = (IThreeWayDiff) node; 618 return includeDirection(twd.getDirection()); 619 } 620 return getComparisonType() == ISynchronizePageConfiguration.TWO_WAY && node instanceof IResourceDiff; 621 } 622 623 632 public boolean includeDirection(int direction) { 633 int mode = getMode(); 634 switch (mode) { 635 case ISynchronizePageConfiguration.BOTH_MODE: 636 return true; 637 case ISynchronizePageConfiguration.CONFLICTING_MODE: 638 return direction == IThreeWayDiff.CONFLICTING; 639 case ISynchronizePageConfiguration.INCOMING_MODE: 640 return direction == IThreeWayDiff.CONFLICTING || direction == IThreeWayDiff.INCOMING; 641 case ISynchronizePageConfiguration.OUTGOING_MODE: 642 return direction == IThreeWayDiff.CONFLICTING || direction == IThreeWayDiff.OUTGOING; 643 default: 644 break; 645 } 646 return true; 647 } 648 649 public ILabelDecorator getLabelDecorator() { 650 ILabelDecorator[] decorators = (ILabelDecorator[])getProperty(ISynchronizePageConfiguration.P_LABEL_DECORATORS); 651 if (decorators == null) { 652 return null; 653 } 654 return new MultiLabelDecorator(decorators); 655 } 656 } 657 | Popular Tags |