KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > synchronize > actions > RefactorActionGroup


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.ui.synchronize.actions;
12
13 import java.util.Iterator JavaDoc;
14 import java.util.List JavaDoc;
15 import org.eclipse.core.resources.IResource;
16 import org.eclipse.core.runtime.IAdaptable;
17 import org.eclipse.jface.action.IMenuManager;
18 import org.eclipse.jface.action.MenuManager;
19 import org.eclipse.jface.viewers.*;
20 import org.eclipse.swt.widgets.Shell;
21 import org.eclipse.team.internal.ui.TeamUIMessages;
22 import org.eclipse.team.internal.ui.Utils;
23 import org.eclipse.team.ui.synchronize.ISynchronizePageSite;
24 import org.eclipse.ui.*;
25 import org.eclipse.ui.actions.*;
26 import org.eclipse.ui.navigator.INavigatorContentService;
27
28 /**
29  * This action group is modeled after the class of the same name in
30  * the org.eclipse.ui.workbench plugin. We couldn't reuse that class
31  * because of a hard dependency on the navigator.
32  */

33 public class RefactorActionGroup extends ActionGroup {
34     
35     private CopyToClipboardAction copyAction;
36     private MoveResourceAction moveAction;
37     private RenameResourceAction renameAction;
38     private ISynchronizePageSite site;
39     private DeleteResourceAction deleteAction;
40     private final INavigatorContentService navigatorContentService;
41     
42     public RefactorActionGroup(ISynchronizePageSite site) {
43         this(site, null);
44     }
45
46     public RefactorActionGroup(ISynchronizePageSite site, INavigatorContentService navigatorContentService) {
47         this.site = site;
48         this.navigatorContentService = navigatorContentService;
49         makeActions();
50     }
51
52     public void fillContextMenu(IMenuManager parentMenu, String JavaDoc groupId) {
53
54         final MenuManager menu = new MenuManager(TeamUIMessages.RefactorActionGroup_0);
55
56         final IStructuredSelection selection= getSelection();
57         final boolean anyResourceSelected = !selection.isEmpty() && allResourcesAreOfType(selection, IResource.PROJECT | IResource.FOLDER | IResource.FILE);
58
59         // Actions can work on non-resource selections
60
copyAction.selectionChanged(getObjectSelection());
61         menu.add(copyAction);
62         
63         if (anyResourceSelected) {
64             deleteAction.selectionChanged(selection);
65             moveAction.selectionChanged(selection);
66             renameAction.selectionChanged(selection);
67             menu.add(deleteAction);
68             menu.add(moveAction);
69             menu.add(renameAction);
70         }
71         parentMenu.appendToGroup(groupId, menu);
72     }
73
74     public void fillActionBars(IActionBars actionBars) {
75         actionBars.setGlobalActionHandler(ActionFactory.COPY.getId(), copyAction);
76         actionBars.setGlobalActionHandler(ActionFactory.DELETE.getId(), deleteAction);
77         actionBars.setGlobalActionHandler(ActionFactory.RENAME.getId(), renameAction);
78         actionBars.setGlobalActionHandler(ActionFactory.MOVE.getId(), moveAction);
79     }
80
81     public void updateActionBars() {
82         final IStructuredSelection structuredSelection= getSelection();
83         copyAction.selectionChanged(getObjectSelection());
84         deleteAction.selectionChanged(structuredSelection);
85         moveAction.selectionChanged(structuredSelection);
86         renameAction.selectionChanged(structuredSelection);
87     }
88
89     protected void makeActions() {
90             
91         final Shell shell = site.getShell();
92         final ISharedImages images = PlatformUI.getWorkbench().getSharedImages();
93         
94         copyAction= new CopyToClipboardAction(shell, navigatorContentService);
95         moveAction= new MoveResourceAction(shell);
96         renameAction= new RenameResourceAction(shell);
97         deleteAction = new DeleteResourceAction(shell) {
98             protected List JavaDoc getSelectedResources() {
99                 return getSelection().toList();//Arrays.asList(Utils.getResources(getSelection().toArray()));
100
}
101         };
102         
103         copyAction.setImageDescriptor(images.getImageDescriptor(ISharedImages.IMG_TOOL_COPY));
104         copyAction.setDisabledImageDescriptor(images.getImageDescriptor(ISharedImages.IMG_TOOL_COPY_DISABLED));
105         
106         deleteAction.setImageDescriptor(images.getImageDescriptor(ISharedImages.IMG_TOOL_DELETE));
107         deleteAction.setDisabledImageDescriptor(images.getImageDescriptor(ISharedImages.IMG_TOOL_DELETE_DISABLED));
108     }
109     
110     private IStructuredSelection getSelection() {
111         final ISelection selection= getContext().getSelection();
112
113         if (!(selection instanceof IStructuredSelection))
114             return new StructuredSelection();
115
116         return new StructuredSelection(Utils.getResources(((IStructuredSelection)selection).toArray()));
117     }
118     
119     private IStructuredSelection getObjectSelection() {
120         final ISelection selection= getContext().getSelection();
121
122         if (!(selection instanceof IStructuredSelection))
123             return new StructuredSelection();
124
125         return (IStructuredSelection)selection;
126     }
127
128     private boolean allResourcesAreOfType(IStructuredSelection selection, int resourceMask) {
129         Iterator JavaDoc resources = selection.iterator();
130         while (resources.hasNext()) {
131             Object JavaDoc next = resources.next();
132             IResource resource = null;
133             if (next instanceof IResource) {
134                 resource = (IResource)next;
135             } else if (next instanceof IAdaptable) {
136                 IAdaptable adaptable = (IAdaptable)next;
137                 resource = (IResource)adaptable.getAdapter(IResource.class);
138             }
139             if(resource == null) {
140                 IResource[] r = Utils.getResources(new Object JavaDoc[] {next});
141                 if(r.length == 1) {
142                     resource = r[0];
143                 }
144             }
145             if (resource == null || (resource.getType() & resourceMask) == 0) {
146                 return false;
147             }
148         }
149         return true;
150     }
151     
152     /* (non-Javadoc)
153      * @see org.eclipse.ui.actions.ActionGroup#dispose()
154      */

155     public void dispose() {
156         super.dispose();
157         copyAction.dispose();
158     }
159 }
160
Popular Tags