1 11 12 package org.eclipse.ui.internal.navigator.resources.actions; 13 14 import org.eclipse.jface.action.Action; 15 import org.eclipse.jface.action.ContributionItem; 16 import org.eclipse.jface.action.IAction; 17 import org.eclipse.jface.action.IMenuManager; 18 import org.eclipse.jface.action.MenuManager; 19 import org.eclipse.jface.action.Separator; 20 import org.eclipse.jface.viewers.StructuredViewer; 21 import org.eclipse.swt.SWT; 22 import org.eclipse.swt.events.SelectionAdapter; 23 import org.eclipse.swt.events.SelectionEvent; 24 import org.eclipse.swt.widgets.Menu; 25 import org.eclipse.swt.widgets.MenuItem; 26 import org.eclipse.ui.IActionBars; 27 import org.eclipse.ui.ISharedImages; 28 import org.eclipse.ui.IWorkbenchActionConstants; 29 import org.eclipse.ui.PlatformUI; 30 import org.eclipse.ui.actions.ActionGroup; 31 import org.eclipse.ui.ide.IDE; 32 import org.eclipse.ui.internal.navigator.resources.plugin.WorkbenchNavigatorMessages; 33 import org.eclipse.ui.internal.navigator.resources.plugin.WorkbenchNavigatorPlugin; 34 import org.eclipse.ui.internal.navigator.workingsets.WorkingSetsContentProvider; 35 import org.eclipse.ui.navigator.IExtensionStateModel; 36 37 41 public class WorkingSetRootModeActionGroup extends ActionGroup { 42 43 private IExtensionStateModel stateModel; 44 private StructuredViewer structuredViewer; 45 46 private boolean hasContributedToViewMenu = false; 47 private IAction workingSetsAction = null; 48 private IAction projectsAction = null; 49 private IAction[] actions; 50 private int currentSelection; 51 private MenuItem[] items; 52 53 private class TopLevelContentAction extends Action implements IAction { 54 55 private final boolean groupWorkingSets; 56 57 63 public TopLevelContentAction(boolean toGroupWorkingSets) { 64 super("", AS_RADIO_BUTTON); groupWorkingSets = toGroupWorkingSets; 66 } 67 68 71 public void run() { 72 if (stateModel 73 .getBooleanProperty(WorkingSetsContentProvider.SHOW_TOP_LEVEL_WORKING_SETS) != groupWorkingSets) { 74 stateModel.setBooleanProperty( 75 WorkingSetsContentProvider.SHOW_TOP_LEVEL_WORKING_SETS, 76 groupWorkingSets); 77 78 structuredViewer.getControl().setRedraw(false); 79 try { 80 structuredViewer.refresh(); 81 } finally { 82 structuredViewer.getControl().setRedraw(true); 83 } 84 } 85 } 86 } 87 88 95 public WorkingSetRootModeActionGroup(StructuredViewer aStructuredViewer, 96 IExtensionStateModel aStateModel) { 97 super(); 98 structuredViewer = aStructuredViewer; 99 stateModel = aStateModel; 100 } 101 102 107 public void fillActionBars(IActionBars actionBars) { 108 synchronized (this) { 109 if (!hasContributedToViewMenu) { 110 contributeToViewMenu(actionBars.getMenuManager()); 111 hasContributedToViewMenu = true; 112 } 113 } 114 } 115 116 private void contributeToViewMenu(IMenuManager viewMenu) { 117 118 IMenuManager topLevelSubMenu = new MenuManager( 119 WorkbenchNavigatorMessages.WorkingSetRootModeActionGroup_Top_Level_Element_); 120 addActions(topLevelSubMenu); 121 viewMenu.insertBefore(IWorkbenchActionConstants.MB_ADDITIONS, 122 topLevelSubMenu); 123 } 124 125 128 protected void addActions(IMenuManager viewMenu) { 129 130 viewMenu.add(new Separator()); 131 items = new MenuItem[actions.length]; 132 133 for (int i = 0; i < actions.length; i++) { 134 final int j = i; 135 136 viewMenu.add(new ContributionItem() { 137 138 public void fill(Menu menu, int index) { 139 140 int style = SWT.CHECK; 141 if ((actions[j].getStyle() & IAction.AS_RADIO_BUTTON) != 0) 142 style = SWT.RADIO; 143 144 final MenuItem mi = new MenuItem(menu, style, index); 145 items[j] = mi; 146 mi.setText(actions[j].getText()); 147 mi.setSelection(currentSelection == j); 148 mi.addSelectionListener(new SelectionAdapter() { 149 150 public void widgetSelected(SelectionEvent e) { 151 if (currentSelection == j) { 152 items[currentSelection].setSelection(true); 153 return; 154 } 155 actions[j].run(); 156 157 items[currentSelection].setSelection(false); 159 currentSelection = j; 160 items[currentSelection].setSelection(true); 161 } 162 163 }); 164 165 } 166 167 public boolean isDynamic() { 168 return false; 169 } 170 }); 171 } 172 } 173 174 private IAction[] createActions() { 175 176 ISharedImages sharedImages = PlatformUI.getWorkbench() 177 .getSharedImages(); 178 179 projectsAction = new TopLevelContentAction(false); 180 projectsAction 181 .setText(WorkbenchNavigatorMessages.WorkingSetRootModeActionGroup_Project_); 182 projectsAction.setImageDescriptor(sharedImages 183 .getImageDescriptor(IDE.SharedImages.IMG_OBJ_PROJECT)); 184 185 workingSetsAction = new TopLevelContentAction(true); 186 workingSetsAction 187 .setText(WorkbenchNavigatorMessages.WorkingSetRootModeActionGroup_Working_Set_); 188 workingSetsAction.setImageDescriptor(WorkbenchNavigatorPlugin 189 .getDefault().getImageRegistry().getDescriptor( 190 "full/obj16/workingsets.gif")); 192 return new IAction[] { projectsAction, workingSetsAction }; 193 } 194 195 201 public void setShowTopLevelWorkingSets(boolean showTopLevelWorkingSets) { 202 if (actions == null) { 203 actions = createActions(); 204 setActions(actions, showTopLevelWorkingSets ? 1 208 : 0); 209 } 210 workingSetsAction.setChecked(showTopLevelWorkingSets); 211 projectsAction.setChecked(!showTopLevelWorkingSets); 212 213 if (items != null) { 214 for (int i = 0; i < items.length; i++) { 215 if(items[i] != null && actions[i] != null) 216 items[i].setSelection(actions[i].isChecked()); 217 } 218 } 219 if (stateModel != null) { 220 stateModel.setBooleanProperty( 221 WorkingSetsContentProvider.SHOW_TOP_LEVEL_WORKING_SETS, 222 showTopLevelWorkingSets); 223 } 224 225 } 226 227 235 private void setActions(IAction[] theActions, int selected) { 236 actions = theActions; 237 currentSelection = selected; 238 239 } 240 241 public void setStateModel(IExtensionStateModel sStateModel) { 242 stateModel = sStateModel; 243 } 244 } 245 | Popular Tags |