KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > texteditor > quickdiff > QuickDiffToggleAction


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
12 package org.eclipse.ui.texteditor.quickdiff;
13
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.jface.action.IAction;
18 import org.eclipse.jface.action.IMenuListener;
19 import org.eclipse.jface.action.IMenuManager;
20 import org.eclipse.jface.action.MenuManager;
21 import org.eclipse.jface.action.Separator;
22 import org.eclipse.jface.viewers.ISelection;
23
24 import org.eclipse.ui.IEditorActionDelegate;
25 import org.eclipse.ui.IEditorPart;
26 import org.eclipse.ui.IWorkbenchActionConstants;
27 import org.eclipse.ui.texteditor.ITextEditor;
28 import org.eclipse.ui.texteditor.ITextEditorExtension;
29 import org.eclipse.ui.texteditor.ITextEditorExtension3;
30 import org.eclipse.ui.texteditor.IUpdate;
31
32 import org.eclipse.ui.internal.texteditor.quickdiff.QuickDiffMessages;
33 import org.eclipse.ui.internal.texteditor.quickdiff.QuickDiffRestoreAction;
34 import org.eclipse.ui.internal.texteditor.quickdiff.ReferenceSelectionAction;
35 import org.eclipse.ui.internal.texteditor.quickdiff.RestoreAction;
36 import org.eclipse.ui.internal.texteditor.quickdiff.RevertBlockAction;
37 import org.eclipse.ui.internal.texteditor.quickdiff.RevertLineAction;
38 import org.eclipse.ui.internal.texteditor.quickdiff.RevertSelectionAction;
39
40 /**
41  * Action to toggle the line number bar's quick diff display. When turned on, quick diff shows
42  * the changes relative to the saved version of the file.
43  *
44  * @since 3.0
45  */

46 public class QuickDiffToggleAction implements IEditorActionDelegate, IUpdate {
47
48     /** The editor we are working on. */
49     ITextEditor fEditor= null;
50
51     /** Our UI proxy action. */
52     IAction fProxy;
53
54     /** The restore actions associated with this toggle action. */
55     QuickDiffRestoreAction[] fRestoreActions=
56         new QuickDiffRestoreAction[] {
57             new RevertSelectionAction(fEditor, true),
58             new RevertBlockAction(fEditor, true),
59             new RevertLineAction(fEditor, true),
60             new RestoreAction(fEditor, true),
61         };
62
63     /** The menu listener that adds the ruler context menu. */
64     private IMenuListener fListener= new IMenuListener() {
65         /** Group name for additions, in CompilationUnitEditor... */
66         private static final String JavaDoc GROUP_ADD= "add"; //$NON-NLS-1$
67
/** Group name for debug contributions */
68         private static final String JavaDoc GROUP_DEBUB= "debug"; //$NON-NLS-1$
69
private static final String JavaDoc GROUP_QUICKDIFF= "quickdiff"; //$NON-NLS-1$
70
private static final String JavaDoc MENU_ID= "quickdiff.menu"; //$NON-NLS-1$
71
private static final String JavaDoc GROUP_RESTORE= "restore"; //$NON-NLS-1$
72

73         public void menuAboutToShow(IMenuManager manager) {
74             // update the toggle action itself
75
update();
76
77             IMenuManager menu= (IMenuManager)manager.find(MENU_ID);
78             // only add menu if it isn't there yet
79
if (menu == null) {
80                 /* HACK: pre-install menu groups
81                  * This is needed since we get the blank context menu, but want to show up
82                  * in the same position as the extension-added QuickDiffToggleAction.
83                  * The extension is added at the end (naturally), but other menus (debug, add)
84                  * don't add themselves to MB_ADDITIONS or alike, but rather to the end, too. So
85                  * we pre-install their respective menu groups here.
86                  */

87                 if (manager.find(GROUP_DEBUB) == null)
88                     manager.insertBefore(IWorkbenchActionConstants.MB_ADDITIONS, new Separator(GROUP_DEBUB));
89                 if (manager.find(GROUP_ADD) == null)
90                     manager.insertAfter(IWorkbenchActionConstants.MB_ADDITIONS, new Separator(GROUP_ADD));
91                 if (manager.find(GROUP_RESTORE) == null)
92                     manager.insertAfter(GROUP_ADD, new Separator(GROUP_RESTORE));
93                 if (manager.find(GROUP_QUICKDIFF) == null)
94                     manager.insertAfter(GROUP_RESTORE, new Separator(GROUP_QUICKDIFF));
95
96                 // create quickdiff menu
97
menu= new MenuManager(QuickDiffMessages.quickdiff_menu_label, MENU_ID);
98                 List JavaDoc descriptors= new QuickDiff().getReferenceProviderDescriptors();
99                 for (Iterator JavaDoc it= descriptors.iterator(); it.hasNext();) {
100                     ReferenceProviderDescriptor desc= (ReferenceProviderDescriptor) it.next();
101                     ReferenceSelectionAction action= new ReferenceSelectionAction(desc, fEditor);
102                     if (action.isEnabled())
103                         menu.add(action);
104                 }
105                 manager.appendToGroup(GROUP_QUICKDIFF, menu);
106
107                 // create restore menu if this action is enabled
108
if (isConnected()) {
109                     for (int i= 0; i < fRestoreActions.length; i++) {
110                         fRestoreActions[i].update();
111                     }
112                     // only add block action if selection action is not enabled
113
if (fRestoreActions[0].isEnabled())
114                         manager.appendToGroup(GROUP_RESTORE, fRestoreActions[0]);
115                     else if (fRestoreActions[1].isEnabled())
116                         manager.appendToGroup(GROUP_RESTORE, fRestoreActions[1]);
117                     if (fRestoreActions[2].isEnabled())
118                         manager.appendToGroup(GROUP_RESTORE, fRestoreActions[2]);
119                     if (fRestoreActions[3].isEnabled())
120                         manager.appendToGroup(GROUP_RESTORE, fRestoreActions[3]);
121                 }
122             }
123         }
124     };
125
126     /*
127      * @see org.eclipse.ui.IEditorActionDelegate#setActiveEditor(org.eclipse.jface.action.IAction, org.eclipse.ui.IEditorPart)
128      */

129     public void setActiveEditor(IAction action, IEditorPart targetEditor) {
130         fProxy= action;
131         removePopupMenu();
132         if (targetEditor instanceof ITextEditor) {
133             fEditor= (ITextEditor)targetEditor;
134         } else
135             fEditor= null;
136         for (int i= 0; i < fRestoreActions.length; i++) {
137             fRestoreActions[i].setEditor(fEditor);
138         }
139         setPopupMenu();
140     }
141
142     /**
143      * Removes the ruler context menu listener from the current editor.
144      */

145     private void removePopupMenu() {
146         if (!(fEditor instanceof ITextEditorExtension))
147             return;
148         ((ITextEditorExtension)fEditor).removeRulerContextMenuListener(fListener);
149     }
150
151     /**
152      * Installs a submenu with <code>fEditor</code>'s ruler context menu that contains the choices
153      * for the quick diff reference. This allows the toggle action to lazily install the menu once
154      * quick diff has been enabled.
155      */

156     private void setPopupMenu() {
157         if (!(fEditor instanceof ITextEditorExtension))
158             return;
159         ((ITextEditorExtension)fEditor).addRulerContextMenuListener(fListener);
160     }
161
162     /**
163      * States whether this toggle action has been installed and a incremental differ has been
164      * installed with the line number bar.
165      *
166      * @return <code>true</code> if a differ has been installed on <code>fEditor</code>.
167      */

168     boolean isConnected() {
169         if (!(fEditor instanceof ITextEditorExtension3))
170             return false;
171         return ((ITextEditorExtension3)fEditor).isChangeInformationShowing();
172     }
173
174     /*
175      * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
176      */

177     public void run(IAction action) {
178         fProxy= action;
179         if (fEditor == null)
180             return;
181
182         if (fEditor instanceof ITextEditorExtension3) {
183             ITextEditorExtension3 extension= (ITextEditorExtension3)fEditor;
184             extension.showChangeInformation(!extension.isChangeInformationShowing());
185         }
186     }
187
188     /*
189      * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
190      */

191     public void selectionChanged(IAction action, ISelection selection) {
192         fProxy= action;
193     }
194
195     /*
196      * @see org.eclipse.ui.texteditor.IUpdate#update()
197      */

198     public void update() {
199         if (fProxy == null)
200             return;
201         if (isConnected())
202             fProxy.setText(QuickDiffMessages.quickdiff_toggle_disable);
203         else
204             fProxy.setText(QuickDiffMessages.quickdiff_toggle_enable);
205     }
206
207 }
208
Popular Tags