KickJava   Java API By Example, From Geeks To Geeks.

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


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
14 import java.util.Properties JavaDoc;
15
16 import org.eclipse.core.runtime.IStatus;
17 import org.eclipse.jface.action.*;
18 import org.eclipse.jface.viewers.*;
19 import org.eclipse.jface.wizard.WizardDialog;
20 import org.eclipse.osgi.util.NLS;
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.dnd.*;
23 import org.eclipse.swt.events.KeyAdapter;
24 import org.eclipse.swt.events.KeyEvent;
25 import org.eclipse.swt.widgets.*;
26 import org.eclipse.team.core.TeamException;
27 import org.eclipse.team.internal.ccvs.core.*;
28 import org.eclipse.team.internal.ccvs.core.util.KnownRepositories;
29 import org.eclipse.team.internal.ccvs.ui.*;
30 import org.eclipse.team.internal.ccvs.ui.model.AllRootsElement;
31 import org.eclipse.team.internal.ccvs.ui.wizards.NewLocationWizard;
32 import org.eclipse.team.internal.core.TeamPlugin;
33 import org.eclipse.team.internal.ui.actions.TeamAction;
34 import org.eclipse.ui.*;
35 import org.eclipse.ui.actions.ActionFactory;
36 import org.eclipse.ui.dialogs.PropertyDialogAction;
37 import org.eclipse.ui.part.PluginTransfer;
38 import org.eclipse.ui.part.PluginTransferData;
39
40 /**
41  * RepositoriesView is a view on a set of known CVS repositories
42  * which allows navigation of the structure of the repository and
43  * the performing of CVS-specific operations on the repository contents.
44  */

45 public class RepositoriesView extends RemoteViewPart {
46     public static final String JavaDoc VIEW_ID = "org.eclipse.team.ccvs.ui.RepositoriesView"; //$NON-NLS-1$
47

48     // The root
49
private AllRootsElement root;
50     
51     // Actions
52
private IAction removeAction;
53     private Action newAction;
54     private Action newAnonAction;
55     private PropertyDialogAction propertiesAction;
56     private RemoveRootAction removeRootAction;
57     private RemoveDateTagAction removeDateTagAction;
58     
59     IRepositoryListener listener = new IRepositoryListener() {
60         public void repositoryAdded(final ICVSRepositoryLocation root) {
61             getViewer().getControl().getDisplay().asyncExec(new Runnable JavaDoc() {
62                 public void run() {
63                     refreshViewer();
64                     getViewer().setSelection(new StructuredSelection(root));
65                 }
66             });
67         }
68         public void repositoriesChanged(ICVSRepositoryLocation[] roots) {
69             refresh();
70         }
71         private void refresh() {
72             Display display = getViewer().getControl().getDisplay();
73             display.asyncExec(new Runnable JavaDoc() {
74                 public void run() {
75                     RepositoriesView.this.refreshViewer();
76                 }
77             });
78         }
79     };
80     
81     private static final class RepositoryDragSourceListener implements DragSourceListener {
82         private IStructuredSelection selection;
83
84         public void dragStart(DragSourceEvent event) {
85             if(selection!=null) {
86                 final Object JavaDoc[] array = selection.toArray();
87                 // event.doit = Utils.getResources(array).length > 0;
88
for (int i = 0; i < array.length; i++) {
89                     if (array[i] instanceof ICVSRemoteFile) {
90                         event.doit = true;
91                         return;
92                     }
93                 }
94                 event.doit = false;
95             }
96         }
97
98         public void dragSetData(DragSourceEvent event) {
99             if (selection!=null && CVSResourceTransfer.getInstance().isSupportedType(event.dataType)) {
100                 final Object JavaDoc[] array = selection.toArray();
101                 for (int i = 0; i < array.length; i++) {
102                     if (array[i] instanceof ICVSRemoteFile) {
103                         event.data = array[i];
104                         return;
105                     }
106                 }
107             } else if (PluginTransfer.getInstance().isSupportedType(event.dataType)) {
108                 final Object JavaDoc[] array = selection.toArray();
109                  for (int i = 0; i < array.length; i++) {
110                      if (array[i] instanceof ICVSRemoteFile) {
111                          event.data = new PluginTransferData("org.eclipse.team.cvs.ui.cvsRemoteDrop", CVSResourceTransfer.getInstance().toByteArray((ICVSRemoteFile) array[i])); //$NON-NLS-1$
112
return;
113                      }
114                  }
115                 
116              }
117         }
118         
119         public void dragFinished( DragSourceEvent event) {
120         }
121
122         public void updateSelection( IStructuredSelection selection) {
123             this.selection = selection;
124         }
125     }
126     
127     RepositoryDragSourceListener repositoryDragSourceListener;
128     
129     /**
130      * Constructor for RepositoriesView.
131      * @param partName
132      */

133     public RepositoriesView() {
134         super(VIEW_ID);
135     }
136
137     /**
138      * Contribute actions to the view
139      */

140     protected void contributeActions() {
141         
142         final Shell shell = getShell();
143         
144         // Create actions
145

146         // New Repository (popup)
147
newAction = new Action(CVSUIMessages.RepositoriesView_new, CVSUIPlugin.getPlugin().getImageDescriptor(ICVSUIConstants.IMG_NEWLOCATION)) {
148             public void run() {
149                 NewLocationWizard wizard = new NewLocationWizard();
150                 wizard.setSwitchPerspectives(false);
151                 WizardDialog dialog = new WizardDialog(shell, wizard);
152                 dialog.open();
153             }
154         };
155         PlatformUI.getWorkbench().getHelpSystem().setHelp(newAction, IHelpContextIds.NEW_REPOSITORY_LOCATION_ACTION);
156         
157         if (includeAnonConnection()) {
158             newAnonAction = new Action(CVSUIMessages.RepositoriesView_newAnonCVS, CVSUIPlugin.getPlugin().getImageDescriptor(ICVSUIConstants.IMG_NEWLOCATION)) {
159                 public void run() {
160                     Properties JavaDoc p = new Properties JavaDoc();
161                     p.setProperty("connection", "pserver"); //$NON-NLS-1$ //$NON-NLS-2$
162
p.setProperty("user", "anonymous"); //$NON-NLS-1$ //$NON-NLS-2$
163
p.setProperty("host", "dev.eclipse.org"); //$NON-NLS-1$ //$NON-NLS-2$
164
p.setProperty("root", "/cvsroot/eclipse"); //$NON-NLS-1$ //$NON-NLS-2$
165
NewLocationWizard wizard = new NewLocationWizard(p);
166                     wizard.setSwitchPerspectives(false);
167                     WizardDialog dialog = new WizardDialog(shell, wizard);
168                     dialog.open();
169                 }
170             };
171             PlatformUI.getWorkbench().getHelpSystem().setHelp(newAnonAction, IHelpContextIds.NEW_DEV_ECLIPSE_REPOSITORY_LOCATION_ACTION);
172         }
173         
174         // Properties
175
propertiesAction = new PropertyDialogAction(shell, getViewer());
176         getViewSite().getActionBars().setGlobalActionHandler(ActionFactory.PROPERTIES.getId(), propertiesAction);
177         IStructuredSelection selection = (IStructuredSelection)getViewer().getSelection();
178         if (selection.size() == 1 && selection.getFirstElement() instanceof RepositoryRoot) {
179             propertiesAction.setEnabled(true);
180         } else {
181             propertiesAction.setEnabled(false);
182         }
183         getViewer().addSelectionChangedListener(new ISelectionChangedListener() {
184             public void selectionChanged(SelectionChangedEvent event) {
185                 IStructuredSelection ss = (IStructuredSelection)event.getSelection();
186                 boolean enabled = ss.size() == 1 && ss.getFirstElement() instanceof RepositoryRoot;
187                 propertiesAction.setEnabled(enabled);
188             }
189         });
190         removeRootAction = new RemoveRootAction(viewer.getControl().getShell(), this);
191         removeRootAction.selectionChanged((IStructuredSelection)null);
192         removeDateTagAction = new RemoveDateTagAction();
193         removeDateTagAction.selectionChanged( (IStructuredSelection)null);
194         removeAction = new Action(){
195             public void run(){
196                 if(removeRootAction.isEnabled()){
197                     removeRootAction.run();
198                 }
199                 if(removeDateTagAction.isEnabled()){
200                     removeDateTagAction.run();
201                 }
202             }
203         };
204         PlatformUI.getWorkbench().getHelpSystem().setHelp(removeRootAction, IHelpContextIds.REMOVE_REPOSITORY_LOCATION_ACTION);
205         IActionBars bars = getViewSite().getActionBars();
206         bars.setGlobalActionHandler(ActionFactory.DELETE.getId(), removeAction);
207         
208         super.contributeActions();
209     }
210
211     /**
212      * Method includeEclipseConnection.
213      * @return boolean
214      */

215     private boolean includeAnonConnection() {
216         return System.getProperty("eclipse.cvs.anon") != null; //$NON-NLS-1$
217
}
218
219     /**
220      * @see org.eclipse.team.internal.ccvs.ui.repo.RemoteViewPart#addWorkbenchActions(org.eclipse.jface.action.IMenuManager)
221      */

222     protected void addWorkbenchActions(IMenuManager manager) {
223         // New actions go next
224
MenuManager sub = new MenuManager(CVSUIMessages.RepositoriesView_newSubmenu, IWorkbenchActionConstants.GROUP_ADD);
225         manager.add(sub);
226         super.addWorkbenchActions(manager);
227         IStructuredSelection selection = (IStructuredSelection)getViewer().getSelection();
228
229         removeRootAction.selectionChanged(selection);
230         removeDateTagAction.selectionChanged(selection);
231         if(removeRootAction.isEnabled()) {
232             manager.add(removeRootAction);
233         }
234         if(removeDateTagAction.isEnabled()){
235             manager.add(removeDateTagAction);
236         }
237         if (selection.size() == 1 && selection.getFirstElement() instanceof RepositoryRoot) {
238             manager.add(new Separator());
239             manager.add(propertiesAction);
240         }
241         sub.add(newAction);
242         if (newAnonAction != null)
243             sub.add(newAnonAction);
244         sub.add(new Separator("group1")); //$NON-NLS-1$
245
}
246     
247     /*
248      * @see WorkbenchPart#createPartControl
249      */

250     public void createPartControl(Composite parent) {
251         super.createPartControl(parent);
252         CVSUIPlugin.getPlugin().getRepositoryManager().addRepositoryListener(listener);
253     }
254     
255     /*
256      * @see WorkbenchPart#dispose
257      */

258     public void dispose() {
259         CVSUIPlugin.getPlugin().getRepositoryManager().removeRepositoryListener(listener);
260         super.dispose();
261     }
262     
263     /**
264      * Initialize the repositories and actions
265      */

266     private void initialize() {
267         root = new AllRootsElement();
268     }
269
270     protected void initializeListeners() {
271         super.initializeListeners();
272         viewer.addSelectionChangedListener(removeRootAction);
273         viewer.addSelectionChangedListener(removeDateTagAction);
274         viewer.addSelectionChangedListener(new ISelectionChangedListener(){
275             public void selectionChanged(SelectionChangedEvent event) {
276                 IStructuredSelection selection = (IStructuredSelection)event.getSelection();
277                 handleChange(selection);
278             }
279         });
280         
281         repositoryDragSourceListener = new RepositoryDragSourceListener();
282         viewer.addDragSupport( DND.DROP_LINK | DND.DROP_DEFAULT,
283                 new Transfer[] { CVSResourceTransfer.getInstance(),PluginTransfer.getInstance()},
284                 repositoryDragSourceListener);
285     }
286     
287     /**
288      * @see org.eclipse.team.internal.ccvs.ui.repo.RemoteViewPart#getTreeInput()
289      */

290     protected Object JavaDoc getTreeInput() {
291         initialize();
292         return root;
293     }
294
295     /**
296      * @see org.eclipse.ui.ISelectionListener#selectionChanged(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection)
297      */

298     public void selectionChanged(IWorkbenchPart part, ISelection selection) {
299         String JavaDoc msg = getStatusLineMessage(selection);
300         getViewSite().getActionBars().getStatusLineManager().setMessage(msg);
301     }
302
303     private String JavaDoc getStatusLineMessage(ISelection selection) {
304         if (selection==null || selection.isEmpty()) return ""; //$NON-NLS-1$
305
if (!(selection instanceof IStructuredSelection)) return ""; //$NON-NLS-1$
306
IStructuredSelection s = (IStructuredSelection)selection;
307         
308         if (s.size() > 1)
309             return NLS.bind(CVSUIMessages.RepositoriesView_NItemsSelected, new String JavaDoc[] { String.valueOf(s.size()) });
310         Object JavaDoc element = TeamAction.getAdapter(s.getFirstElement(), ICVSResource.class);
311         if (element instanceof ICVSRemoteResource) {
312             ICVSRemoteResource res = (ICVSRemoteResource)element;
313             String JavaDoc name;
314             if (res.isContainer()) {
315                 name = res.getRepositoryRelativePath();
316             } else {
317                 try {
318                     name = res.getRepositoryRelativePath() + " " + ((ICVSRemoteFile)res).getRevision(); //$NON-NLS-1$
319
} catch (TeamException e) {
320                     TeamPlugin.log(IStatus.ERROR, CVSUIMessages.RepositoriesView_CannotGetRevision, e);
321                     name = res.getRepositoryRelativePath();
322                 }
323             }
324             return NLS.bind(CVSUIMessages.RepositoriesView_ResourceInRepository, new String JavaDoc[] { name, res.getRepository().getLocation(true) });
325         }
326         return CVSUIMessages.RepositoriesView_OneItemSelected;
327     }
328     
329     /**
330      * @see org.eclipse.team.internal.ccvs.ui.repo.RemoteViewPart#getHelpContextId()
331      */

332     protected String JavaDoc getHelpContextId() {
333         return IHelpContextIds.REPOSITORIES_VIEW;
334     }
335
336     /**
337      * @see org.eclipse.team.internal.ccvs.ui.repo.RemoteViewPart#getKeyListener()
338      */

339     protected KeyAdapter getKeyListener() {
340         return new KeyAdapter() {
341             public void keyPressed(KeyEvent event) {
342                 if (event.keyCode == SWT.F5) {
343                     if (WorkbenchUserAuthenticator.USE_ALTERNATE_PROMPTER) {
344                         ICVSRepositoryLocation[] locations = KnownRepositories.getInstance().getRepositories();
345                         for (int i = 0; i < locations.length; i++) {
346                             locations[i].flushUserInfo();
347                         }
348                     } else {
349                         refreshAll();
350                     }
351                 } else if (event.keyCode == SWT.F9 && WorkbenchUserAuthenticator.USE_ALTERNATE_PROMPTER) {
352                     refreshAll();
353                 }
354             }
355         };
356     }
357         
358     private void handleChange(IStructuredSelection selection){
359         removeRootAction.updateSelection(selection);
360         removeDateTagAction.updateSelection(selection);
361         removeAction.setEnabled(removeRootAction.isEnabled() || removeDateTagAction.isEnabled());
362         
363         repositoryDragSourceListener.updateSelection(selection);
364     }
365     
366 }
367
Popular Tags