1 11 package org.eclipse.team.internal.ui.synchronize; 12 13 import org.eclipse.core.resources.IResource; 14 import org.eclipse.jface.action.*; 15 import org.eclipse.jface.util.IPropertyChangeListener; 16 import org.eclipse.jface.util.PropertyChangeEvent; 17 import org.eclipse.jface.viewers.*; 18 import org.eclipse.swt.events.*; 19 import org.eclipse.swt.widgets.Menu; 20 import org.eclipse.team.core.synchronize.SyncInfoSet; 21 import org.eclipse.team.internal.ui.IPreferenceIds; 22 import org.eclipse.team.internal.ui.TeamUIPlugin; 23 import org.eclipse.team.internal.ui.mapping.DiffTreeStatusLineContributionGroup; 24 import org.eclipse.team.ui.synchronize.*; 25 import org.eclipse.ui.*; 26 import org.eclipse.ui.actions.ActionContext; 27 import org.eclipse.ui.actions.ActionGroup; 28 29 64 public abstract class StructuredViewerAdvisor extends AbstractViewerAdvisor { 65 66 private IPropertyChangeListener propertyListener = new IPropertyChangeListener() { 70 public void propertyChange(PropertyChangeEvent event) { 71 if(event.getProperty().equals(IPreferenceIds.SYNCVIEW_VIEW_SYNCINFO_IN_LABEL)) { 73 StructuredViewer viewer = getViewer(); 74 if(viewer != null && !viewer.getControl().isDisposed()) { 75 viewer.refresh(true ); 76 } 77 } 78 } 79 }; 80 private DiffTreeStatusLineContributionGroup statusLine; 81 82 89 public StructuredViewerAdvisor(ISynchronizePageConfiguration configuration) { 90 super(configuration); 91 } 92 93 99 public void initializeViewer(final StructuredViewer viewer) { 100 super.initializeViewer(viewer); 101 initializeListeners(viewer); 102 hookContextMenu(viewer); 103 } 104 105 108 public void dispose() { 109 if (getActionGroup() != null) { 110 getActionGroup().dispose(); 111 } 112 if (statusLine != null) 113 statusLine.dispose(); 114 TeamUIPlugin.getPlugin().getPreferenceStore().removePropertyChangeListener(propertyListener); 115 } 116 117 123 protected void initializeListeners(final StructuredViewer viewer) { 124 viewer.getControl().addDisposeListener(new DisposeListener() { 125 public void widgetDisposed(DisposeEvent e) { 126 StructuredViewerAdvisor.this.dispose(); 127 } 128 }); 129 viewer.addOpenListener(new IOpenListener() { 130 public void open(OpenEvent event) { 131 handleOpen(); 132 } 133 }); 134 viewer.addDoubleClickListener(new IDoubleClickListener() { 135 public void doubleClick(DoubleClickEvent event) { 136 handleDoubleClick(viewer, event); 137 } 138 }); 139 viewer.addSelectionChangedListener(new ISelectionChangedListener() { 140 public void selectionChanged(SelectionChangedEvent event) { 141 updateActionBars((IStructuredSelection)viewer.getSelection()); 143 } 144 }); 145 TeamUIPlugin.getPlugin().getPreferenceStore().addPropertyChangeListener(propertyListener); 146 } 147 148 152 protected boolean handleDoubleClick(StructuredViewer viewer, DoubleClickEvent event) { 153 IStructuredSelection selection = (IStructuredSelection) event.getSelection(); 154 Object node = selection.getFirstElement(); 155 if (node != null && node instanceof SyncInfoModelElement) { 156 SyncInfoModelElement syncNode = (SyncInfoModelElement) node; 157 IResource resource = syncNode.getResource(); 158 if (syncNode != null && resource != null && resource.getType() == IResource.FILE) { 159 return true; 162 } 163 } 164 return false; 165 } 166 167 private void handleOpen() { 168 Object o = getConfiguration().getProperty(SynchronizePageConfiguration.P_OPEN_ACTION); 169 if (o instanceof IAction) { 170 IAction action = (IAction)o; 171 action.run(); 172 } 173 } 174 175 182 public final void setActionBars(IActionBars actionBars) { 183 if(actionBars != null) { 184 IToolBarManager manager = actionBars.getToolBarManager(); 185 186 Object o = getConfiguration().getProperty(ISynchronizePageConfiguration.P_TOOLBAR_MENU); 188 if (!(o instanceof String [])) { 189 o = ISynchronizePageConfiguration.DEFAULT_TOOLBAR_MENU; 190 } 191 String [] groups = (String [])o; 192 for (int i = 0; i < groups.length; i++) { 193 String group = groups[i]; 194 manager.add(new Separator(getGroupId(group))); 196 } 197 198 IMenuManager menu = actionBars.getMenuManager(); 200 if (menu != null) { 201 o = getConfiguration().getProperty(ISynchronizePageConfiguration.P_VIEW_MENU); 203 if (!(o instanceof String [])) { 204 o = ISynchronizePageConfiguration.DEFAULT_VIEW_MENU; 205 } 206 groups = (String []) o; 207 initializeStatusLine(actionBars); 208 for (int i = 0; i < groups.length; i++) { 209 String group = groups[i]; 210 menu.add(new Separator(getGroupId(group))); 213 } 214 } 215 216 fillActionBars(actionBars); 217 } 218 } 219 220 224 protected void initializeStatusLine(IActionBars actionBars) { 225 statusLine = new DiffTreeStatusLineContributionGroup( 226 getConfiguration().getSite().getShell(), 227 getConfiguration()); 228 IStatusLineManager statusLineMgr = actionBars.getStatusLineManager(); 229 if (statusLineMgr != null && statusLine != null) { 230 statusLine.fillActionBars(actionBars); 231 } 232 } 233 234 242 private void hookContextMenu(final StructuredViewer viewer) { 243 String targetID = getContextMenuId(viewer); 244 final MenuManager menuMgr = createContextMenuManager(targetID); 245 246 menuMgr.setRemoveAllWhenShown(true); 247 menuMgr.addMenuListener(new IMenuListener() { 248 public void menuAboutToShow(IMenuManager manager) { 249 fillContextMenu(viewer, manager); 250 } 251 }); 252 Menu menu = menuMgr.createContextMenu(viewer.getControl()); 253 menu.addMenuListener(new MenuListener() { 254 255 public void menuHidden(MenuEvent e) { 256 } 257 258 public void menuShown(MenuEvent e) { 264 IContributionItem[] items = menuMgr.getItems(); 265 for (int i = 0; i < items.length; i++) { 266 IContributionItem item = items[i]; 267 if (item instanceof ActionContributionItem) { 268 IAction actionItem = ((ActionContributionItem) item).getAction(); 269 if (actionItem instanceof SynchronizeModelAction) { 270 ((SynchronizeModelAction) actionItem).selectionChanged(viewer.getSelection()); 271 } 272 } 273 } 274 } 275 }); 276 viewer.getControl().setMenu(menu); 277 registerContextMenu(viewer, menuMgr); 278 } 279 280 287 protected MenuManager createContextMenuManager(String targetID) { 288 return new MenuManager(targetID); 289 } 290 291 296 protected void registerContextMenu(final StructuredViewer viewer, MenuManager menuMgr) { 297 String targetID = getContextMenuId(viewer); 298 if (targetID != null) { 299 IWorkbenchSite workbenchSite = getConfiguration().getSite().getWorkbenchSite(); 300 IWorkbenchPartSite ws = null; 301 if (workbenchSite instanceof IWorkbenchPartSite) 302 ws = (IWorkbenchPartSite)workbenchSite; 303 if (ws != null) { 304 ws.registerContextMenu(targetID, menuMgr, viewer); 305 } 306 } 307 } 308 309 314 protected String getContextMenuId(StructuredViewer viewer) { 315 String targetID; 316 Object o = getConfiguration().getProperty(ISynchronizePageConfiguration.P_OBJECT_CONTRIBUTION_ID); 317 if (o instanceof String ) { 318 targetID = (String )o; 319 } else { 320 targetID = null; 321 } 322 return targetID; 323 } 324 325 334 protected void fillContextMenu(StructuredViewer viewer, final IMenuManager manager) { 335 addContextMenuGroups(manager); 336 getActionGroup().setContext(new ActionContext(viewer.getSelection())); 337 getActionGroup().fillContextMenu(manager); 338 } 339 340 344 protected void addContextMenuGroups(final IMenuManager manager) { 345 Object o = getConfiguration().getProperty(ISynchronizePageConfiguration.P_CONTEXT_MENU); 347 if (!(o instanceof String [])) { 348 o = ISynchronizePageConfiguration.DEFAULT_CONTEXT_MENU; 349 } 350 String [] groups = (String [])o; 351 for (int i = 0; i < groups.length; i++) { 352 String group = groups[i]; 353 manager.add(new Separator(group)); 355 } 356 } 357 358 362 protected void fillActionBars(IActionBars actionBars) { 363 getActionGroup().fillActionBars(actionBars); 364 updateActionBars((IStructuredSelection) getViewer().getSelection()); 365 Object input = getViewer().getInput(); 366 if (input instanceof ISynchronizeModelElement) { 367 getActionGroup().modelChanged((ISynchronizeModelElement) input); 368 } 369 } 370 371 376 protected void updateActionBars(IStructuredSelection selection) { 377 ActionGroup group = getActionGroup(); 378 if (group != null) { 379 group.setContext(new ActionContext(selection)); 380 group.updateActionBars(); 381 } 382 } 383 384 protected SynchronizePageActionGroup getActionGroup() { 385 return (SynchronizePageActionGroup)getConfiguration(); 386 } 387 388 private String getGroupId(String group) { 389 return ((SynchronizePageConfiguration)getConfiguration()).getGroupId(group); 390 } 391 } 392 | Popular Tags |