KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > cheatsheets > actions > CheatSheetMenu


1 /*******************************************************************************
2  * Copyright (c) 2002, 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.ui.internal.cheatsheets.actions;
12
13 import com.ibm.icu.text.Collator;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Collections JavaDoc;
16 import java.util.Comparator JavaDoc;
17 import java.util.List JavaDoc;
18
19 import org.eclipse.jface.action.ContributionItem;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.events.*;
22 import org.eclipse.swt.widgets.*;
23 import org.eclipse.ui.*;
24
25 import org.eclipse.ui.cheatsheets.*;
26 import org.eclipse.ui.internal.cheatsheets.*;
27 import org.eclipse.ui.internal.cheatsheets.registry.*;
28 import org.eclipse.ui.internal.cheatsheets.views.CheatSheetView;
29
30 /**
31  * A menu for cheatsheet selection.
32  * <p>
33  * A <code>CheatSheetMenu</code> is used to populate a menu with
34  * cheatsheet items. If the user selects one of these items
35  * an action is performed to launch the selected cheatsheet.
36  * </p><p>
37  * The visible cheatsheet items within the menu are dynamic and reflect the
38  * available set. The available set consists of a limited combination of
39  * the most recently used cheatsheet list and the currently available
40  * cheatsheet.
41  * </p>
42  */

43 public class CheatSheetMenu extends ContributionItem {
44     private static final int MAX_CHEATSHEET_ITEMS = 5;
45     private static CheatSheetRegistryReader reg;
46
47     private boolean showActive = false;
48     
49     private IMenuContributor menuContributor;
50
51     private Comparator JavaDoc comparator = new Comparator JavaDoc() {
52         private Collator collator = Collator.getInstance();
53
54         public int compare(Object JavaDoc ob1, Object JavaDoc ob2) {
55             if(ob1 == null || ob2 == null) {
56                 return -1;
57             }
58             CheatSheetElement d1 = (CheatSheetElement) ob1;
59             CheatSheetElement d2 = (CheatSheetElement) ob2;
60             return collator.compare(d1.getLabel(null), d2.getLabel(null));
61         }
62     };
63
64     /**
65      * Constructs a new instance of <code>CheatSheetMenu</code>.
66      */

67     public CheatSheetMenu() {
68         super("LaunchCheatSheetMenu"); //$NON-NLS-1$
69

70         if (reg == null)
71             reg = CheatSheetRegistryReader.getInstance();
72
73         showActive(true);
74     }
75
76     /* (non-Javadoc)
77      * Creates a menu item for a cheatsheet.
78      */

79     private void createMenuItem(Menu menu, int index, final CheatSheetElement element, boolean bCheck) {
80
81         MenuItem mi = new MenuItem(menu, bCheck ? SWT.RADIO : SWT.PUSH, index);
82         mi.setText(element.getLabel(null));
83         String JavaDoc key;
84         if (element.isComposite()) {
85             key = ICheatSheetResource.COMPOSITE_OBJ;
86         } else {
87             key = ICheatSheetResource.CHEATSHEET_OBJ;
88         }
89         mi.setImage(CheatSheetPlugin.getPlugin().getImageRegistry().get(key));
90         mi.setSelection(bCheck);
91         mi.addSelectionListener(new SelectionAdapter() {
92             public void widgetSelected(SelectionEvent e) {
93                 run(element, e);
94             }
95         });
96     }
97
98     /* (non-Javadoc)
99      * Creates a menu item for "Other...".
100      */

101     private void createOtherItem(Menu menu, int index) {
102         MenuItem mi = new MenuItem(menu, SWT.PUSH, index);
103         mi.setText(Messages.CHEAT_SHEET_OTHER_MENU);
104         mi.addSelectionListener(new SelectionAdapter() {
105             public void widgetSelected(SelectionEvent e) {
106                 runOther(e);
107             }
108         });
109     }
110
111     /* (non-Javadoc)
112      * Fills the menu with cheatsheet items.
113      */

114     public void fill(Menu menu, int index) {
115         // Get the checked cheatsheet.
116
String JavaDoc checkID = null;
117         if (showActive) {
118             checkID = getActiveCheatSheetID();
119         }
120
121         // Collect and sort cheatsheet items.
122
ArrayList JavaDoc cheatsheets = getCheatSheetItems();
123         Collections.sort(cheatsheets, comparator);
124
125         // Add cheatsheet shortcuts
126
for (int i = 0; i < cheatsheets.size(); i++) {
127             CheatSheetElement element = (CheatSheetElement) cheatsheets.get(i);
128             if (element != null) {
129                 createMenuItem(menu, index++, element, element.getID().equals(checkID));
130             }
131         }
132
133         // Add others item..
134
if (cheatsheets.size() > 0) {
135             new MenuItem(menu, SWT.SEPARATOR, index++);
136         }
137         createOtherItem(menu, index++);
138         if (menuContributor != null) {
139             menuContributor.contributeToViewMenu(menu, index);
140         }
141     }
142
143     /**
144      * Method getActiveCheatSheetID returns the id of the active
145      * cheatsheet or null.
146      *
147      * @return String
148      */

149     private String JavaDoc getActiveCheatSheetID() {
150         //get the active cheatsheet view, if opened
151
IWorkbenchPage page = getActiveWorkbenchPage();
152
153         if( page != null ) {
154             CheatSheetView view = (CheatSheetView) page.findView(ICheatSheetResource.CHEAT_SHEET_VIEW_ID);
155             if (view != null) {
156                 CheatSheetElement content = view.getContent();
157                 if (content != null) {
158                     return content.getID();
159                 }
160             }
161         }
162
163         return null;
164     }
165
166     /**
167      * Method getActiveWorkbenchPage returns the active
168      * workbench page or null.
169      *
170      * @return IWorkbenchPage
171      */

172     private IWorkbenchPage getActiveWorkbenchPage() {
173         IWorkbench workbench = CheatSheetPlugin.getPlugin().getWorkbench();
174         IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
175
176         //get the active cheatsheet view, if opened
177
return window.getActivePage();
178     }
179
180     /**
181      * Returns the available list of cheatsheets to display
182      * in the menu.
183      * <p>
184      * By default, the list contains the most recently used cheatsheets
185      * and then random cheatsheets until there are 5 present in the list.
186      * </p><p>
187      * Care should be taken to keep this list to a minimum (7 +/- 2 items
188      * is a good guideline to follow).
189      * </p>
190      *
191      * @return an <code>ArrayList<code> of cheatsheet items <code>CheatSheetElement</code>
192      */

193     protected ArrayList JavaDoc getCheatSheetItems() {
194         ArrayList JavaDoc list = new ArrayList JavaDoc(MAX_CHEATSHEET_ITEMS);
195         int emptySlots = MAX_CHEATSHEET_ITEMS;
196
197         // Add cheatsheets from MRU list
198
if (emptySlots > 0) {
199             ArrayList JavaDoc mru = new ArrayList JavaDoc(MAX_CHEATSHEET_ITEMS);
200             int count = getCheatSheetMru(mru, 0, MAX_CHEATSHEET_ITEMS);
201             for (int i = 0; i < count && emptySlots > 0; i++) {
202                 if (!list.contains(mru.get(i))) {
203                     list.add(mru.get(i));
204                     emptySlots--;
205                 }
206             }
207         }
208
209         // Add random cheatsheets until the list is filled.
210
CheatSheetCollectionElement cheatSheetsCollection = (CheatSheetCollectionElement)reg.getCheatSheets();
211         emptySlots = addCheatSheets(list, cheatSheetsCollection, emptySlots);
212
213         return list;
214     }
215
216     /**
217      * Method addCheatSheets fills a list with cheatsheet elements until there
218      * are no more empty slots.
219      *
220      * @param list - the list to file
221      * @param cheatSheetsCollection - the collection to get the elements from
222      * @param emptySlots - number of empty slots remaining
223      * @return int - number of empty slots remaining
224      */

225     private int addCheatSheets(ArrayList JavaDoc list, CheatSheetCollectionElement cheatSheetsCollection, int emptySlots) {
226         Object JavaDoc[] cheatSheets = cheatSheetsCollection.getCheatSheets();
227         for (int i = 0; i < cheatSheets.length && emptySlots > 0; i++) {
228             if (!list.contains(cheatSheets[i])) {
229                 list.add(cheatSheets[i]);
230                 emptySlots--;
231             }
232         }
233
234         Object JavaDoc[] cheatSheetsFromCollection = cheatSheetsCollection.getChildren();
235         for (int nX = 0; nX < cheatSheetsFromCollection.length && emptySlots > 0; nX++) {
236             CheatSheetCollectionElement collection = (CheatSheetCollectionElement) cheatSheetsFromCollection[nX];
237             emptySlots = addCheatSheets(list, collection, emptySlots);
238         }
239
240         return emptySlots;
241     }
242
243     /* (non-Javadoc)
244      * Gets the most recently used (MRU) shortcut cheatsheets
245      * (<code>CheatSheetElement</code> items)
246      * <p>
247      * The list is formed from the global cheatsheet history.
248      * </p>
249      * @param dest destination list to contain the items
250      * @param destStart index in destination list to start copying items at
251      * @param count number of items to copy from history
252      * @return the number of items actually copied
253      */

254     private int getCheatSheetMru(List JavaDoc dest, int destStart, int count) {
255         CheatSheetHistory history = CheatSheetPlugin.getPlugin().getCheatSheetHistory();
256         return history.copyItems(dest, destStart, count);
257     }
258
259     /**
260      * Returns whether the menu item representing the active cheatsheet
261      * will have a check mark.
262      *
263      * @return <code>true</code> if a check mark is shown, <code>false</code> otherwise
264      */

265     protected boolean getShowActive() {
266         return showActive;
267     }
268
269     /* (non-Javadoc)
270      * Returns whether this menu is dynamic.
271      */

272     public boolean isDynamic() {
273         return true;
274     }
275
276     /* (non-Javadoc)
277      * @see org.eclipse.jface.action.IContributionItem#isVisible()
278      */

279     public boolean isVisible() {
280         return getActiveWorkbenchPage() != null;
281     }
282
283     /**
284      * Runs an action to launch the cheatsheet.
285      *
286      * @param element the selected cheatsheet
287      * @param event SelectionEvent - the event send along with the selection callback
288      */

289     protected void run(CheatSheetElement element, SelectionEvent event) {
290         new OpenCheatSheetAction(element.getID()).run();
291     }
292
293     /* (non-Javadoc)
294      * Show the "other" dialog, select a cheatsheet, and launch it. Pass on the selection
295      * event should the meny need it.
296      */

297     private void runOther(SelectionEvent event) {
298         new CheatSheetCategoryBasedSelectionAction().run();
299     }
300
301     /**
302      * Sets the showActive flag. If <code>showActive == true</code> then the
303      * active cheatsheet is hilighted with a check mark.
304      *
305      * @param the new showActive flag
306      */

307     protected void showActive(boolean b) {
308         showActive = b;
309     }
310
311     /**
312      * Sets the menuContributor
313      * @param menuContributor an object which may add contributions to
314      * the menu.
315      */

316     public void setMenuContributor(IMenuContributor menuContributor) {
317         this.menuContributor = menuContributor;
318     }
319
320 }
321
Popular Tags