KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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 org.eclipse.core.resources.IResource;
14 import org.eclipse.jface.action.IMenuManager;
15 import org.eclipse.jface.action.MenuManager;
16 import org.eclipse.jface.viewers.ISelection;
17 import org.eclipse.jface.viewers.IStructuredSelection;
18 import org.eclipse.team.internal.ui.TeamUIMessages;
19 import org.eclipse.team.internal.ui.Utils;
20 import org.eclipse.team.ui.synchronize.*;
21 import org.eclipse.ui.IWorkbenchSite;
22 import org.eclipse.ui.actions.ActionGroup;
23 import org.eclipse.ui.actions.OpenWithMenu;
24
25 /**
26  * This is the action group for the open actions. It contains open
27  * actions for
28  */

29 public class OpenWithActionGroup extends ActionGroup {
30
31     private OpenFileInSystemEditorAction openFileAction;
32     private OpenInCompareAction openInCompareAction;
33     private final boolean includeOpenInCompare;
34     private final ISynchronizePageConfiguration configuration;
35
36     public OpenWithActionGroup(ISynchronizePageConfiguration configuration, boolean includeOpenInCompare) {
37         this.configuration = configuration;
38         this.includeOpenInCompare = includeOpenInCompare;
39         makeActions();
40     }
41
42     protected void makeActions() {
43         IWorkbenchSite ws = getSite().getWorkbenchSite();
44         if (ws != null) {
45             openFileAction = new OpenFileInSystemEditorAction(ws.getPage());
46             if (includeOpenInCompare)
47                 openInCompareAction = new OpenInCompareAction(configuration);
48         }
49     }
50
51     private ISynchronizeParticipant getParticipant() {
52         return configuration.getParticipant();
53     }
54
55     private ISynchronizePageSite getSite() {
56         return configuration.getSite();
57     }
58
59     public void fillContextMenu(IMenuManager menu, String JavaDoc groupId) {
60         ISelection selection = getSite().getSelectionProvider().getSelection();
61         if (selection instanceof IStructuredSelection && !hasFileMenu(menu)) {
62             fillOpenWithMenu(menu, groupId, (IStructuredSelection)selection);
63         }
64     }
65
66     private boolean hasFileMenu(IMenuManager menu) {
67         return menu.find(openFileAction.getId()) != null;
68     }
69
70     /**
71      * Adds the OpenWith submenu to the context menu.
72      *
73      * @param menu the context menu
74      * @param selection the current selection
75      */

76     private void fillOpenWithMenu(IMenuManager menu, String JavaDoc groupId, IStructuredSelection selection) {
77
78         // Only supported if at least one file is selected.
79
if (selection == null || selection.size() < 1)
80             return;
81         Object JavaDoc[] elements = selection.toArray();
82         IResource resources[] = Utils.getResources(elements);
83         if(resources.length == 0 && openInCompareAction != null) {
84             // We can still show the compare editor open if the element has a compare input
85
if (elements.length > 0) {
86                 ISynchronizeParticipant participant = getParticipant();
87                 if (participant instanceof ModelSynchronizeParticipant) {
88                     ModelSynchronizeParticipant msp = (ModelSynchronizeParticipant) participant;
89                     boolean allElementsHaveCompareInput = true;
90                     for (int i = 0; i < elements.length; i++) {
91                         if (!msp.hasCompareInputFor(elements[i])) {
92                             allElementsHaveCompareInput = false;
93                             break;
94                         }
95                     }
96                     if (allElementsHaveCompareInput) {
97                         menu.appendToGroup(groupId, openInCompareAction);
98                     }
99                 }
100             }
101             return;
102         }
103         
104         for (int i = 0; i < resources.length; i++) {
105             if (resources[i].getType() != IResource.FILE) {
106                 // Only supported if all the items are files.
107
return;
108             }
109         }
110         
111         if (resources.length > 0 && openInCompareAction != null) {
112             // Support multiple files selected
113
menu.appendToGroup(groupId, openInCompareAction);
114         }
115         
116         for (int i = 0; i < resources.length; i++) {
117             if (!resources[i].exists()) {
118                 // Only support non-compare actions if all files exist.
119
return;
120             }
121         }
122         
123         if (openFileAction != null) {
124             openFileAction.selectionChanged(selection);
125             menu.appendToGroup(groupId, openFileAction);
126         }
127         
128         if (resources.length == 1) {
129             // Only support the "Open With..." submenu if exactly one file is selected.
130
IWorkbenchSite ws = getSite().getWorkbenchSite();
131             if (ws != null) {
132                 MenuManager submenu =
133                     new MenuManager(TeamUIMessages.OpenWithActionGroup_0);
134                 submenu.add(new OpenWithMenu(ws.getPage(), resources[0]));
135                 menu.appendToGroup(groupId, submenu);
136             }
137         }
138     }
139
140     public void openInCompareEditor() {
141         if (openInCompareAction != null)
142             openInCompareAction.run();
143     }
144 }
145
Popular Tags