KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > editors > text > NextPreviousPulldownActionDelegate


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
12 package org.eclipse.ui.internal.editors.text;
13
14 import com.ibm.icu.text.Collator;
15 import java.util.Iterator JavaDoc;
16 import java.util.SortedSet JavaDoc;
17 import java.util.TreeSet JavaDoc;
18
19 import org.eclipse.swt.widgets.Control;
20 import org.eclipse.swt.widgets.Menu;
21
22 import org.eclipse.jface.action.Action;
23 import org.eclipse.jface.action.ActionContributionItem;
24 import org.eclipse.jface.action.IAction;
25 import org.eclipse.jface.action.IMenuCreator;
26 import org.eclipse.jface.preference.IPreferenceStore;
27 import org.eclipse.jface.viewers.ISelection;
28
29 import org.eclipse.ui.IWorkbenchWindow;
30 import org.eclipse.ui.IWorkbenchWindowPulldownDelegate2;
31 import org.eclipse.ui.texteditor.AnnotationPreference;
32 import org.eclipse.ui.texteditor.MarkerAnnotationPreferences;
33
34 /**
35  * The abstract class for next and previous pulldown action delegates.
36  *
37  * @since 3.0
38  */

39 public abstract class NextPreviousPulldownActionDelegate extends Action implements IMenuCreator, IWorkbenchWindowPulldownDelegate2 {
40
41     /** The cached menu. */
42     private Menu fMenu;
43
44     /** The preference store */
45     private IPreferenceStore fStore;
46
47     /** Action for handling menu item selection. */
48     private static class NavigationEnablementAction extends Action implements Comparable JavaDoc {
49
50         /** The preference store. */
51         private IPreferenceStore fStore;
52
53         /** The preference key for the value in the store. */
54         private String JavaDoc fKey;
55         
56         /**
57          * The display string.
58          * @since 3.2
59          */

60         private String JavaDoc fName;
61
62         /**
63          * Creates a named navigation enablement action.
64          *
65          * @param name the name of this action
66          * @param store the preference store
67          * @param key the preference key
68          */

69         public NavigationEnablementAction(String JavaDoc name, IPreferenceStore store, String JavaDoc key) {
70             super(name, IAction.AS_CHECK_BOX);
71             fStore= store;
72             fKey= key;
73             fName= name;
74             setChecked(fStore.getBoolean(fKey));
75         }
76
77         /*
78          * @see IAction#run()
79          */

80         public void run() {
81             fStore.setValue(fKey, isChecked());
82         }
83
84         /*
85          * @see java.lang.Comparable#compareTo(java.lang.Object)
86          * @since 3.2
87          */

88         public int compareTo(Object JavaDoc o) {
89             if (!(o instanceof NavigationEnablementAction))
90                 return -1;
91
92             String JavaDoc otherName= ((NavigationEnablementAction)o).fName;
93
94             return Collator.getInstance().compare(fName, otherName);
95         }
96     }
97
98     /**
99      * Returns the preference key to be used in the
100      * <code>NavigationEnablementAction</code>.
101      *
102      * @param annotationPreference the annotation preference
103      * @return the preference key or <code>null</code> if the key is not defined in XML
104      */

105     public abstract String JavaDoc getPreferenceKey(AnnotationPreference annotationPreference);
106
107     /*
108      * @see org.eclipse.jface.action.IMenuCreator#getMenu(org.eclipse.swt.widgets.Control)
109      */

110     public Menu getMenu(Control parent) {
111         if (fMenu != null)
112             fMenu.dispose();
113
114         fMenu= new Menu(parent);
115         fillMenu(fMenu);
116
117         return fMenu;
118     }
119
120     /**
121      * Creates a next previous action delegate.
122      */

123     public NextPreviousPulldownActionDelegate() {
124         fStore= EditorsPlugin.getDefault().getPreferenceStore();
125     }
126
127     /*
128      * @see org.eclipse.jface.action.IMenuCreator#getMenu(org.eclipse.swt.widgets.Menu)
129      */

130     public Menu getMenu(Menu parent) {
131         if (fMenu == null) {
132             fMenu= new Menu(parent);
133             fillMenu(fMenu);
134         }
135
136         return fMenu;
137     }
138
139     /*
140      * @see org.eclipse.jface.action.IMenuCreator#dispose()
141      */

142     public void dispose() {
143         if (fMenu != null) {
144             fMenu.dispose();
145             fMenu= null;
146         }
147     }
148
149     /**
150      * Fills the given menu using marker
151      * annotation preferences.
152      *
153      * @param menu the menu to fill
154      */

155     private void fillMenu(Menu menu) {
156         IAction[] actions= getActionsFromDescriptors();
157
158         for (int i= 0; i < actions.length; i++) {
159             ActionContributionItem item= new ActionContributionItem(actions[i]);
160             item.fill(menu, -1);
161         }
162     }
163
164     /**
165      * Creates actions using marker
166      * annotation preferences.
167      *
168      * @return the navigation enablement actions
169      */

170     private IAction[] getActionsFromDescriptors() {
171         MarkerAnnotationPreferences fMarkerAnnotationPreferences= EditorsPlugin.getDefault().getMarkerAnnotationPreferences();
172         SortedSet JavaDoc containers= new TreeSet JavaDoc();
173
174         Iterator JavaDoc iter= fMarkerAnnotationPreferences.getAnnotationPreferences().iterator();
175         while (iter.hasNext()) {
176             AnnotationPreference preference= (AnnotationPreference)iter.next();
177             String JavaDoc key= preference.getShowInNextPrevDropdownToolbarActionKey();
178             if (key != null && fStore.getBoolean(key)) {
179                 String JavaDoc preferenceKey= getPreferenceKey(preference);
180
181                 /*
182                  * Fixes bug 41689
183                  * This code can be simplified if we decide that
184                  * we don't allow to use different settings for go to
185                  * previous and go to next annotation.
186                  */

187                 preferenceKey= preference.getIsGoToNextNavigationTargetKey();
188
189                 if (preferenceKey != null)
190                     containers.add(new NavigationEnablementAction(preference.getPreferenceLabel(), fStore, preferenceKey));
191             }
192         }
193
194         return (IAction[]) containers.toArray(new Action[containers.size()]);
195     }
196
197     /*
198      * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#init(org.eclipse.ui.IWorkbenchWindow)
199      */

200     public void init(IWorkbenchWindow window) {
201     }
202
203     /*
204      * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
205      */

206     public void run(IAction action) {
207     }
208
209     /*
210      * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction, org.eclipse.jface.viewers.ISelection)
211      */

212     public void selectionChanged(IAction action, ISelection selection) {
213     }
214 }
215
Popular Tags