1 11 package org.eclipse.jdt.internal.ui.navigator; 12 13 import org.eclipse.jface.action.Action; 14 import org.eclipse.jface.action.IAction; 15 import org.eclipse.jface.action.IMenuManager; 16 import org.eclipse.jface.action.MenuManager; 17 import org.eclipse.jface.action.Separator; 18 import org.eclipse.jface.viewers.StructuredViewer; 19 20 import org.eclipse.ui.IActionBars; 21 import org.eclipse.ui.IWorkbenchActionConstants; 22 import org.eclipse.ui.PlatformUI; 23 import org.eclipse.ui.navigator.IExtensionStateModel; 24 25 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; 26 import org.eclipse.jdt.internal.ui.JavaPluginImages; 27 import org.eclipse.jdt.internal.ui.actions.MultiActionGroup; 28 import org.eclipse.jdt.internal.ui.navigator.IExtensionStateConstants.Values; 29 import org.eclipse.jdt.internal.ui.packageview.PackagesMessages; 30 31 36 public class CommonLayoutActionGroup extends MultiActionGroup { 37 38 public static final String LAYOUT_GROUP_NAME = "layout"; 40 private IExtensionStateModel fStateModel; 41 private StructuredViewer fStructuredViewer; 42 43 private boolean fHasContributedToViewMenu = false; 44 private IAction fHierarchicalLayout = null; 45 private IAction fFlatLayoutAction = null; 46 private IAction[] fActions; 47 48 private IMenuManager fLayoutSubMenu; 49 50 private class CommonLayoutAction extends Action implements IAction { 51 52 private final boolean fIsFlatLayout; 53 54 public CommonLayoutAction(boolean flat) { 55 super("", AS_RADIO_BUTTON); fIsFlatLayout = flat; 57 if (fIsFlatLayout) { 58 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.LAYOUT_FLAT_ACTION); 59 } else { 60 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.LAYOUT_HIERARCHICAL_ACTION); 61 } 62 } 63 64 67 public void run() { 68 if (fStateModel.getBooleanProperty(Values.IS_LAYOUT_FLAT) != fIsFlatLayout) { 69 fStateModel.setBooleanProperty(Values.IS_LAYOUT_FLAT, fIsFlatLayout); 70 71 fStructuredViewer.getControl().setRedraw(false); 72 try { 73 fStructuredViewer.refresh(); 74 } finally { 75 fStructuredViewer.getControl().setRedraw(true); 76 } 77 } 78 } 79 } 80 81 public CommonLayoutActionGroup(StructuredViewer structuredViewer, 82 IExtensionStateModel stateModel) { 83 super(); 84 fStateModel = stateModel; 85 fStructuredViewer = structuredViewer; 86 } 87 88 93 public void fillActionBars(IActionBars actionBars) { 94 if (!fHasContributedToViewMenu) { 95 96 IMenuManager viewMenu = actionBars.getMenuManager(); 97 if (fLayoutSubMenu == null) { 99 fLayoutSubMenu = new MenuManager(PackagesMessages.LayoutActionGroup_label); 100 addActions(fLayoutSubMenu); 101 102 viewMenu.insertAfter(IWorkbenchActionConstants.MB_ADDITIONS, new Separator(LAYOUT_GROUP_NAME)); 103 } 104 105 viewMenu.appendToGroup(LAYOUT_GROUP_NAME, fLayoutSubMenu); 106 107 fHasContributedToViewMenu = true; 108 } 109 } 110 111 public void unfillActionBars(IActionBars actionBars) { 112 if (fHasContributedToViewMenu) { 113 if (fLayoutSubMenu != null) { 115 actionBars.getMenuManager().remove(fLayoutSubMenu); 116 fLayoutSubMenu.dispose(); 117 fLayoutSubMenu= null; 118 } 119 120 fHasContributedToViewMenu = false; 121 } 122 } 123 124 private IAction[] createActions() { 125 126 fFlatLayoutAction = new CommonLayoutAction(true); 127 fFlatLayoutAction.setText(PackagesMessages.LayoutActionGroup_flatLayoutAction_label); 128 JavaPluginImages.setLocalImageDescriptors(fFlatLayoutAction, "flatLayout.gif"); 130 fHierarchicalLayout = new CommonLayoutAction(false); 131 fHierarchicalLayout.setText(PackagesMessages.LayoutActionGroup_hierarchicalLayoutAction_label); 132 JavaPluginImages.setLocalImageDescriptors(fHierarchicalLayout, "hierarchicalLayout.gif"); 134 return new IAction[] { fFlatLayoutAction, fHierarchicalLayout }; 135 } 136 137 public void setFlatLayout(boolean flatLayout) { 138 if (fActions == null) { 139 fActions = createActions(); 140 141 setActions(fActions, flatLayout ? 0 : 1); 143 } 144 fHierarchicalLayout.setChecked(!flatLayout); 145 fFlatLayoutAction.setChecked(flatLayout); 146 } 147 148 } 149 | Popular Tags |