1 11 package org.eclipse.ui.internal.cheatsheets.actions; 12 13 import com.ibm.icu.text.Collator; 14 import java.util.ArrayList ; 15 import java.util.Collections ; 16 import java.util.Comparator ; 17 import java.util.List ; 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 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 comparator = new Comparator () { 52 private Collator collator = Collator.getInstance(); 53 54 public int compare(Object ob1, Object 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 67 public CheatSheetMenu() { 68 super("LaunchCheatSheetMenu"); 70 if (reg == null) 71 reg = CheatSheetRegistryReader.getInstance(); 72 73 showActive(true); 74 } 75 76 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 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 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 114 public void fill(Menu menu, int index) { 115 String checkID = null; 117 if (showActive) { 118 checkID = getActiveCheatSheetID(); 119 } 120 121 ArrayList cheatsheets = getCheatSheetItems(); 123 Collections.sort(cheatsheets, comparator); 124 125 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 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 149 private String getActiveCheatSheetID() { 150 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 172 private IWorkbenchPage getActiveWorkbenchPage() { 173 IWorkbench workbench = CheatSheetPlugin.getPlugin().getWorkbench(); 174 IWorkbenchWindow window = workbench.getActiveWorkbenchWindow(); 175 176 return window.getActivePage(); 178 } 179 180 193 protected ArrayList getCheatSheetItems() { 194 ArrayList list = new ArrayList (MAX_CHEATSHEET_ITEMS); 195 int emptySlots = MAX_CHEATSHEET_ITEMS; 196 197 if (emptySlots > 0) { 199 ArrayList mru = new ArrayList (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 CheatSheetCollectionElement cheatSheetsCollection = (CheatSheetCollectionElement)reg.getCheatSheets(); 211 emptySlots = addCheatSheets(list, cheatSheetsCollection, emptySlots); 212 213 return list; 214 } 215 216 225 private int addCheatSheets(ArrayList list, CheatSheetCollectionElement cheatSheetsCollection, int emptySlots) { 226 Object [] 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 [] 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 254 private int getCheatSheetMru(List dest, int destStart, int count) { 255 CheatSheetHistory history = CheatSheetPlugin.getPlugin().getCheatSheetHistory(); 256 return history.copyItems(dest, destStart, count); 257 } 258 259 265 protected boolean getShowActive() { 266 return showActive; 267 } 268 269 272 public boolean isDynamic() { 273 return true; 274 } 275 276 279 public boolean isVisible() { 280 return getActiveWorkbenchPage() != null; 281 } 282 283 289 protected void run(CheatSheetElement element, SelectionEvent event) { 290 new OpenCheatSheetAction(element.getID()).run(); 291 } 292 293 297 private void runOther(SelectionEvent event) { 298 new CheatSheetCategoryBasedSelectionAction().run(); 299 } 300 301 307 protected void showActive(boolean b) { 308 showActive = b; 309 } 310 311 316 public void setMenuContributor(IMenuContributor menuContributor) { 317 this.menuContributor = menuContributor; 318 } 319 320 } 321 | Popular Tags |