1 11 package org.eclipse.team.internal.ccvs.ui.repo; 12 13 import org.eclipse.core.runtime.IStatus; 14 import org.eclipse.jface.action.*; 15 import org.eclipse.jface.dialogs.IDialogSettings; 16 import org.eclipse.jface.viewers.*; 17 import org.eclipse.jface.window.Window; 18 import org.eclipse.jface.wizard.WizardDialog; 19 import org.eclipse.osgi.util.NLS; 20 import org.eclipse.osgi.util.TextProcessor; 21 import org.eclipse.swt.SWT; 22 import org.eclipse.swt.events.KeyAdapter; 23 import org.eclipse.swt.events.KeyEvent; 24 import org.eclipse.swt.widgets.*; 25 import org.eclipse.team.internal.ccvs.core.ICVSRemoteFile; 26 import org.eclipse.team.internal.ccvs.ui.*; 27 import org.eclipse.team.internal.ccvs.ui.actions.OpenRemoteFileAction; 28 import org.eclipse.team.internal.ccvs.ui.model.RemoteContentProvider; 29 import org.eclipse.ui.*; 30 import org.eclipse.ui.actions.ActionFactory; 31 import org.eclipse.ui.dialogs.IWorkingSetEditWizard; 32 import org.eclipse.ui.dialogs.IWorkingSetSelectionDialog; 33 import org.eclipse.ui.model.WorkbenchLabelProvider; 34 import org.eclipse.ui.part.*; 35 36 39 public abstract class RemoteViewPart extends ViewPart implements ISelectionListener { 40 41 protected TreeViewer viewer; 43 44 private DrillDownAdapter drillPart; 46 47 private Action refreshAction; 48 private Action collapseAllAction; 49 50 private Action selectWorkingSetAction; 51 private Action deselectWorkingSetAction; 52 private Action editWorkingSetAction; 53 private OpenRemoteFileAction openAction; 54 55 private RemoteContentProvider contentProvider; 56 private IDialogSettings settings; 57 private static final String SELECTED_WORKING_SET = "SelectedWorkingSet"; 59 private class ChangeWorkingSetAction extends Action { 60 String name; 61 public ChangeWorkingSetAction(String name, int index) { 62 super(NLS.bind(CVSUIMessages.RepositoriesView_workingSetMenuItem, new String [] { new Integer (index).toString(), name })); 63 this.name = name; 64 } 65 public void run() { 66 IWorkingSetManager manager = PlatformUI.getWorkbench().getWorkingSetManager(); 67 setWorkingSet(manager.getWorkingSet(name), true); 68 } 69 } 70 71 private class DecoratingRepoLabelProvider extends WorkbenchLabelProvider { 72 protected String decorateText(String input, Object element) { 73 return TextProcessor.process(input, ":@/"); } 76 } 77 78 protected RemoteViewPart(String partName) { 79 IDialogSettings workbenchSettings = CVSUIPlugin.getPlugin().getDialogSettings(); 80 settings = workbenchSettings.getSection(partName); 81 if (settings == null) { 82 settings = workbenchSettings.addNewSection(partName); 83 } 84 String name = settings.get(SELECTED_WORKING_SET); 85 IWorkingSet set = null; 86 if (name != null) 87 set = PlatformUI.getWorkbench().getWorkingSetManager().getWorkingSet(name); 88 setWorkingSet(set, false); 89 } 90 91 94 public void createPartControl(Composite parent) { 95 viewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL); 96 viewer.setContentProvider(getContentProvider()); 97 viewer.setLabelProvider(new DecoratingRepoLabelProvider()); 98 getSite().setSelectionProvider(viewer); 99 viewer.setInput(getTreeInput()); 100 viewer.setComparator(new RepositoryComparator()); 101 viewer.getControl().addKeyListener(getKeyListener()); 102 drillPart = new DrillDownAdapter(viewer); 103 104 contributeActions(); 105 initializeListeners(); 106 107 String helpID = getHelpContextId(); 109 if (helpID != null) 110 PlatformUI.getWorkbench().getHelpSystem().setHelp(viewer.getControl(), helpID); 111 } 112 113 116 public void setFocus() { 117 viewer.getControl().setFocus(); 118 } 119 120 126 protected String getHelpContextId() { 127 return null; 128 } 129 130 134 protected abstract Object getTreeInput(); 135 136 139 public RemoteContentProvider getContentProvider() { 140 if (contentProvider == null) { 141 contentProvider = new RemoteContentProvider(); 142 } 143 return contentProvider; 144 }; 145 146 protected KeyAdapter getKeyListener() { 147 return new KeyAdapter() { 148 public void keyPressed(KeyEvent event) { 149 if (event.keyCode == SWT.F5) { 150 refreshAction.run(); 151 } 152 } 153 }; 154 } 155 156 protected void initializeListeners() { 157 getSite().getWorkbenchWindow().getSelectionService().addPostSelectionListener(this); 159 } 160 161 protected IWorkingSet getWorkingSet() { 162 return getContentProvider().getWorkingSet(); 163 } 164 165 protected void setWorkingSet(IWorkingSet workingSet, boolean refreshViewer) { 166 if (settings != null) { 167 String name = null; 168 if (workingSet != null) 169 name = workingSet.getName(); 170 settings.put(SELECTED_WORKING_SET, name); 171 } 172 getContentProvider().setWorkingSet(workingSet); 173 String toolTip; 174 if (workingSet == null) { 175 toolTip = null; 176 } else { 177 toolTip = NLS.bind(CVSUIMessages.RemoteViewPart_workingSetToolTip, new String [] { workingSet.getName() }); 178 } 179 setTitleToolTip(toolTip); 180 if (refreshViewer) refreshViewer(); 181 } 182 183 186 protected void contributeActions() { 187 188 final Shell shell = getShell(); 189 190 CVSUIPlugin plugin = CVSUIPlugin.getPlugin(); 192 refreshAction = new Action(CVSUIMessages.RepositoriesView_refresh, CVSUIPlugin.getPlugin().getImageDescriptor(ICVSUIConstants.IMG_REFRESH_ENABLED)) { 193 public void run() { 194 refreshAll(); 195 } 196 }; 197 refreshAction.setToolTipText(CVSUIMessages.RepositoriesView_refreshTooltip); 198 refreshAction.setDisabledImageDescriptor(plugin.getImageDescriptor(ICVSUIConstants.IMG_REFRESH_DISABLED)); 199 refreshAction.setHoverImageDescriptor(plugin.getImageDescriptor(ICVSUIConstants.IMG_REFRESH)); 200 getViewSite().getActionBars().setGlobalActionHandler(ActionFactory.REFRESH.getId(), refreshAction); 201 202 collapseAllAction = new Action(CVSUIMessages.RepositoriesView_collapseAll, CVSUIPlugin.getPlugin().getImageDescriptor(ICVSUIConstants.IMG_COLLAPSE_ALL_ENABLED)) { 203 public void run() { 204 collapseAll(); 205 } 206 }; 207 collapseAllAction.setToolTipText(CVSUIMessages.RepositoriesView_collapseAllTooltip); 208 collapseAllAction.setHoverImageDescriptor(plugin.getImageDescriptor(ICVSUIConstants.IMG_COLLAPSE_ALL)); 209 210 selectWorkingSetAction = new Action(CVSUIMessages.RepositoriesView_newWorkingSet) { 212 public void run() { 213 IWorkingSetManager manager = PlatformUI.getWorkbench().getWorkingSetManager(); 214 IWorkingSetSelectionDialog dialog = manager.createWorkingSetSelectionDialog(shell, false); 215 IWorkingSet workingSet = null; 216 if (workingSet != null) 217 dialog.setSelection(new IWorkingSet[]{workingSet}); 218 219 if (dialog.open() == Window.OK) { 220 IWorkingSet[] result = dialog.getSelection(); 221 if (result != null && result.length > 0) { 222 setWorkingSet(result[0], true); 223 manager.addRecentWorkingSet(result[0]); 224 } else { 225 setWorkingSet(null, true); 226 } 227 } 228 } 229 }; 230 PlatformUI.getWorkbench().getHelpSystem().setHelp(selectWorkingSetAction, IHelpContextIds.SELECT_WORKING_SET_ACTION); 231 232 deselectWorkingSetAction = new Action(CVSUIMessages.RepositoriesView_deselectWorkingSet) { 234 public void run() { 235 setWorkingSet(null, true); 236 } 237 }; 238 PlatformUI.getWorkbench().getHelpSystem().setHelp(deselectWorkingSetAction, IHelpContextIds.DESELECT_WORKING_SET_ACTION); 239 240 editWorkingSetAction = new Action(CVSUIMessages.RepositoriesView_editWorkingSet) { 242 public void run() { 243 IWorkingSetManager manager = PlatformUI.getWorkbench().getWorkingSetManager(); 244 IWorkingSet workingSet = getWorkingSet(); 245 if (workingSet == null) { 246 setEnabled(false); 247 return; 248 } 249 IWorkingSetEditWizard wizard = manager.createWorkingSetEditWizard(workingSet); 250 if (wizard == null) { 251 CVSUIPlugin.log(IStatus.ERROR, NLS.bind("No wizard registered for working set {0}", new Object [] {workingSet.getName()}), null); return; 254 } 255 WizardDialog dialog = new WizardDialog(shell, wizard); 256 dialog.create(); 257 if (dialog.open() == Window.OK) 258 setWorkingSet(wizard.getSelection(), true); 259 } 260 }; 261 PlatformUI.getWorkbench().getHelpSystem().setHelp(editWorkingSetAction, IHelpContextIds.EDIT_WORKING_SET_ACTION); 262 263 MenuManager menuMgr = new MenuManager(); 265 Tree tree = viewer.getTree(); 266 Menu menu = menuMgr.createContextMenu(tree); 267 menuMgr.addMenuListener(new IMenuListener() { 268 public void menuAboutToShow(IMenuManager manager) { 269 addWorkbenchActions(manager); 270 } 271 272 }); 273 menuMgr.setRemoveAllWhenShown(true); 274 tree.setMenu(menu); 275 getSite().registerContextMenu(menuMgr, viewer); 276 277 IActionBars bars = getViewSite().getActionBars(); 279 IToolBarManager tbm = bars.getToolBarManager(); 280 drillPart.addNavigationActions(tbm); 281 tbm.add(refreshAction); 282 tbm.add(new Separator()); 283 tbm.add(collapseAllAction); 284 tbm.update(false); 285 286 openAction = new OpenRemoteFileAction(); 288 viewer.addOpenListener(new IOpenListener() { 289 public void open(OpenEvent event) { 290 handleOpen(event); 291 } 292 }); 293 294 updateWorkingSetMenu(); 295 bars.updateActionBars(); 296 } 297 298 303 protected void addWorkbenchActions(IMenuManager manager) { 304 manager.add(new Separator(IWorkbenchActionConstants.GROUP_FILE)); 306 manager.add(new Separator("checkoutGroup")); manager.add(new Separator("tagGroup")); manager.add(new Separator("miscGroup")); manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS)); 311 312 manager.add(refreshAction); 313 } 314 315 319 protected Shell getShell() { 320 return viewer.getTree().getShell(); 321 } 322 323 public void updateWorkingSetMenu() { 324 IActionBars bars = getViewSite().getActionBars(); 325 IMenuManager mgr = bars.getMenuManager(); 326 327 mgr.removeAll(); 328 329 mgr.add(selectWorkingSetAction); 330 mgr.add(deselectWorkingSetAction); 331 deselectWorkingSetAction.setEnabled(getWorkingSet() != null); 332 mgr.add(editWorkingSetAction); 333 editWorkingSetAction.setEnabled(getWorkingSet() != null); 334 335 mgr.add(new Separator()); 336 337 IWorkingSetManager manager = PlatformUI.getWorkbench().getWorkingSetManager(); 338 IWorkingSet[] workingSets = manager.getWorkingSets(); 339 for (int i = 0; i < workingSets.length; i++) { 340 String name = workingSets[i].getName(); 341 ChangeWorkingSetAction action = new ChangeWorkingSetAction(name, i + 1); 342 mgr.add(action); 343 action.setChecked(getWorkingSet() != null && getWorkingSet().getName().equals(name)); 344 } 345 346 bars.updateActionBars(); 347 } 348 349 protected void refreshAll() { 350 refreshViewer(); 352 } 353 354 protected void refreshViewer() { 355 if (viewer == null) return; 356 ((RemoteContentProvider)viewer.getContentProvider()).cancelJobs(CVSUIPlugin.getPlugin().getRepositoryManager().getKnownRepositoryRoots()); 357 ((RemoteContentProvider)viewer.getContentProvider()).purgeCache(); 358 CVSUIPlugin.getPlugin().getRepositoryManager().purgeCache(); 359 updateWorkingSetMenu(); 360 viewer.getControl().setRedraw(false); 361 viewer.refresh(); 362 viewer.getControl().setRedraw(true); 363 } 364 365 public void collapseAll() { 366 if (viewer == null) return; 367 viewer.getControl().setRedraw(false); 368 viewer.collapseToLevel(viewer.getInput(), AbstractTreeViewer.ALL_LEVELS); 369 viewer.getControl().setRedraw(true); 370 } 371 374 private void handleOpen(OpenEvent e) { 375 ISelection selection = e.getSelection(); 377 if (selection instanceof IStructuredSelection) { 378 IStructuredSelection structured = (IStructuredSelection)selection; 379 if (structured.size() == 1) { 380 Object first = structured.getFirstElement(); 381 if (first instanceof ICVSRemoteFile) { 382 openAction.selectionChanged(null, selection); 384 openAction.run(null); 385 } else { 386 viewer.setExpandedState(first, !viewer.getExpandedState(first)); 388 } 389 } 390 } 391 } 392 393 397 public TreeViewer getViewer() { 398 return viewer; 399 } 400 401 404 public void selectionChanged(IWorkbenchPart part, ISelection selection) { 405 } 406 407 410 public void dispose() { 411 getSite().getWorkbenchWindow().getSelectionService().removePostSelectionListener(this); 412 super.dispose(); 413 viewer = null; 414 } 415 416 } 417 | Popular Tags |