KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > repo > RemoteViewPart


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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 /**
37  * This class acts as a superclass to all remote CVS tree views.
38  */

39 public abstract class RemoteViewPart extends ViewPart implements ISelectionListener {
40     
41     // The tree viewer
42
protected TreeViewer viewer;
43
44     // Drill down adapter
45
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 JavaDoc SELECTED_WORKING_SET = "SelectedWorkingSet"; //$NON-NLS-1$
58

59     private class ChangeWorkingSetAction extends Action {
60         String JavaDoc name;
61         public ChangeWorkingSetAction(String JavaDoc name, int index) {
62             super(NLS.bind(CVSUIMessages.RepositoriesView_workingSetMenuItem, new String JavaDoc[] { new Integer JavaDoc(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 JavaDoc decorateText(String JavaDoc input, Object JavaDoc element) {
73             //Used to process RTL locales only
74
return TextProcessor.process(input, ":@/"); //$NON-NLS-1$
75
}
76     }
77     
78     protected RemoteViewPart(String JavaDoc partName) {
79         IDialogSettings workbenchSettings = CVSUIPlugin.getPlugin().getDialogSettings();
80         settings = workbenchSettings.getSection(partName);
81         if (settings == null) {
82             settings = workbenchSettings.addNewSection(partName);
83         }
84         String JavaDoc 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     /**
92      * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
93      */

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()/*WorkbenchLabelProvider()*/);
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         // F1 Help
108
String JavaDoc helpID = getHelpContextId();
109         if (helpID != null)
110             PlatformUI.getWorkbench().getHelpSystem().setHelp(viewer.getControl(), helpID);
111     }
112     
113     /**
114      * @see WorkbenchPart#setFocus
115      */

116     public void setFocus() {
117         viewer.getControl().setFocus();
118     }
119     
120     /**
121      * Method getHelpContextId should be overridden by subclasses to provide the
122      * appropriate help id.
123      *
124      * @return String
125      */

126     protected String JavaDoc getHelpContextId() {
127         return null;
128     }
129     
130     /**
131      * Method getTreeInput.
132      * @return Object
133      */

134     protected abstract Object JavaDoc getTreeInput();
135     
136     /**
137      * @see org.eclipse.team.internal.ccvs.ui.repo.RemoteViewPart#getContentProvider()
138      */

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         // listen for selection changes in the repo view
158
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 JavaDoc name = null;
168             if (workingSet != null)
169                 name = workingSet.getName();
170             settings.put(SELECTED_WORKING_SET, name);
171         }
172         getContentProvider().setWorkingSet(workingSet);
173         String JavaDoc toolTip;
174         if (workingSet == null) {
175             toolTip = null;
176         } else {
177             toolTip = NLS.bind(CVSUIMessages.RemoteViewPart_workingSetToolTip, new String JavaDoc[] { workingSet.getName() });
178         }
179         setTitleToolTip(toolTip);
180         if (refreshViewer) refreshViewer();
181     }
182     
183     /**
184      * Contribute actions to the view
185      */

186     protected void contributeActions() {
187         
188         final Shell shell = getShell();
189         
190         // Refresh (toolbar)
191
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         // Select Working Set (popup)
211
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         // Deselect Working Set (popup)
233
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         // Edit Working Set (popup)
241
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                     // todo
252
CVSUIPlugin.log(IStatus.ERROR, NLS.bind("No wizard registered for working set {0}", new Object JavaDoc[] {workingSet.getName()}), null); //$NON-NLS-1$
253
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         // Create the popup menu
264
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         // Create the local tool bar
278
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         // Register the open handler
287
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     /**
299      * Add the menu actions that were contributed in plugin.xml
300      *
301      * @param manager
302      */

303     protected void addWorkbenchActions(IMenuManager manager) {
304         // File actions go first (view file)
305
manager.add(new Separator(IWorkbenchActionConstants.GROUP_FILE));
306         // Misc additions
307
manager.add(new Separator("checkoutGroup")); //$NON-NLS-1$
308
manager.add(new Separator("tagGroup")); //$NON-NLS-1$
309
manager.add(new Separator("miscGroup")); //$NON-NLS-1$
310
manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
311
312         manager.add(refreshAction);
313     }
314     
315     /**
316      * Method getShell.
317      * @return Shell
318      */

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 JavaDoc 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         //CVSUIPlugin.getPlugin().getRepositoryManager().clearCaches();
351
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     /**
372      * Handle open request.
373      */

374     private void handleOpen(OpenEvent e) {
375         // Only act on single selection
376
ISelection selection = e.getSelection();
377         if (selection instanceof IStructuredSelection) {
378             IStructuredSelection structured = (IStructuredSelection)selection;
379             if (structured.size() == 1) {
380                 Object JavaDoc first = structured.getFirstElement();
381                 if (first instanceof ICVSRemoteFile) {
382                     // It's a file, open it.
383
openAction.selectionChanged(null, selection);
384                     openAction.run(null);
385                 } else {
386                     // Try to expand/contract
387
viewer.setExpandedState(first, !viewer.getExpandedState(first));
388                 }
389             }
390         }
391     }
392     
393     /**
394      * Returns the viewer.
395      * @return TreeViewer
396      */

397     public TreeViewer getViewer() {
398         return viewer;
399     }
400
401     /**
402      * @see org.eclipse.ui.ISelectionListener#selectionChanged(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection)
403      */

404     public void selectionChanged(IWorkbenchPart part, ISelection selection) {
405     }
406
407     /**
408      * @see org.eclipse.ui.IWorkbenchPart#dispose()
409      */

410     public void dispose() {
411         getSite().getWorkbenchWindow().getSelectionService().removePostSelectionListener(this);
412         super.dispose();
413         viewer = null;
414     }
415     
416 }
417
Popular Tags