KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > navigator > CommonLayoutActionGroup


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.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 /**
32  * Adds view menus to switch between flat and hierarchical layout.
33  *
34  * @since 3.2
35  */

36 public class CommonLayoutActionGroup extends MultiActionGroup {
37
38     public static final String JavaDoc LAYOUT_GROUP_NAME = "layout"; //$NON-NLS-1$
39

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); //$NON-NLS-1$
56
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         /*
65          * @see org.eclipse.jface.action.IAction#run()
66          */

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     /*
89      * (non-Javadoc)
90      *
91      * @see ActionGroup#fillActionBars(IActionBars)
92      */

93     public void fillActionBars(IActionBars actionBars) {
94         if (!fHasContributedToViewMenu) {
95
96             IMenuManager viewMenu = actionBars.getMenuManager();
97             // Create layout sub menu
98
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             // Create layout sub menu
114
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"); //$NON-NLS-1$
129

130         fHierarchicalLayout = new CommonLayoutAction(false);
131         fHierarchicalLayout.setText(PackagesMessages.LayoutActionGroup_hierarchicalLayoutAction_label);
132         JavaPluginImages.setLocalImageDescriptors(fHierarchicalLayout, "hierarchicalLayout.gif"); //$NON-NLS-1$
133

134         return new IAction[] { fFlatLayoutAction, fHierarchicalLayout };
135     }
136
137     public void setFlatLayout(boolean flatLayout) {
138         if (fActions == null) {
139             fActions = createActions();
140             
141             // indicates check the flat action
142
setActions(fActions, flatLayout ? 0 : 1);
143         }
144         fHierarchicalLayout.setChecked(!flatLayout);
145         fFlatLayoutAction.setChecked(flatLayout);
146     }
147
148 }
149
Popular Tags