KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > wizards > buildpaths > newsourcepage > ClasspathModifierDropDownAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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
12 package org.eclipse.jdt.internal.ui.wizards.buildpaths.newsourcepage;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.swt.widgets.Control;
19 import org.eclipse.swt.widgets.Menu;
20
21 import org.eclipse.jface.action.ActionContributionItem;
22 import org.eclipse.jface.action.IAction;
23 import org.eclipse.jface.action.IMenuCreator;
24 import org.eclipse.jface.viewers.IStructuredSelection;
25
26 /**
27  * Drop down action for toolbars containing <code>BuildpathModifierAction</code>s.
28  * The drop down action manages a list of actions that are displayed when invoking
29  * the drop down. If there is at least one valid action, then the drop down action
30  * itself will also be valid and invoking run will delegate the call to the
31  * first valid action in the list.
32  */

33 public class ClasspathModifierDropDownAction extends BuildpathModifierAction implements IMenuCreator {
34     
35     /** The menu to be populated with items*/
36     private Menu fMenu;
37     private List JavaDoc fActions;
38     //The action to execute on run iff enabled
39
private BuildpathModifierAction fFirstValidAction;
40     
41     /**
42      * Create a drop down action using the same descriptors as the provided action, but it's on
43      * tool tip text. The action will automatically be put in the list of actions that are
44      * managed by this drop down menu.
45      */

46     public ClasspathModifierDropDownAction() {
47         super(null, null, BuildpathModifierAction.DROP_DOWN_ACTION, IAction.AS_DROP_DOWN_MENU);
48         
49         fActions= new ArrayList JavaDoc();
50         fFirstValidAction= null;
51         
52         setText(""); //$NON-NLS-1$
53
setToolTipText(""); //$NON-NLS-1$
54
}
55     
56     /**
57      * {@inheritDoc}
58      */

59     public String JavaDoc getDetailedDescription() {
60         if (fFirstValidAction != null) {
61             return fFirstValidAction.getDetailedDescription();
62         } else if (fActions.size() > 0) {
63             return ((BuildpathModifierAction)fActions.get(0)).getDetailedDescription();
64         } else {
65             return ""; //$NON-NLS-1$
66
}
67     }
68         
69     /**
70      * Runs the first action of the list of managed actions that is valid.
71      */

72     public void run() {
73         fFirstValidAction.run();
74     }
75
76     public IMenuCreator getMenuCreator() {
77         return this;
78     }
79
80     public Menu getMenu(Control parent) {
81         if (fMenu != null) {
82             fMenu.dispose();
83         }
84         fMenu = new Menu(parent);
85         createEntries(fMenu);
86         return fMenu;
87
88     }
89
90     public Menu getMenu(Menu parent) {
91         return fMenu;
92     }
93     
94     /**
95      * Add dynamically an action to the drop down menu.
96      *
97      * @param action the action to be added
98      */

99     public void addAction(BuildpathModifierAction action) {
100         fActions.add(action);
101         update();
102     }
103     
104     /**
105      * Remove an action from the drop down menu
106      *
107      * @param action the action to be removed
108      */

109     public void removeAction(BuildpathModifierAction action) {
110         fActions.remove(action);
111         update();
112     }
113     
114     /**
115      * Populate the menu with the given action item
116      *
117      * @param parent the menu to add an action for
118      * @param action the action to be added
119      */

120     private void addActionToMenu(Menu parent, IAction action) {
121         ActionContributionItem item = new ActionContributionItem(action);
122         item.fill(parent, -1);
123     }
124     
125     /**
126      * Fill the menu with all actions
127      *
128      * @param menu the menu to be populated
129      */

130     private void createEntries(Menu menu) {
131         for(int i= 0; i < fActions.size(); i++) {
132             IAction action= (IAction)fActions.get(i);
133             addActionToMenu(menu, action);
134         }
135     }
136     
137     public void dispose() {
138         if (fMenu != null) {
139             fMenu.dispose();
140             fMenu = null;
141         }
142     }
143
144     /**
145      * {@inheritDoc}
146      */

147     protected boolean canHandle(IStructuredSelection elements) {
148         update();
149         return fFirstValidAction != null;
150     }
151
152     private void update() {
153         for (Iterator JavaDoc iterator= fActions.iterator(); iterator.hasNext();) {
154             BuildpathModifierAction action= (BuildpathModifierAction)iterator.next();
155             if (action.isEnabled()) {
156                 if (action != fFirstValidAction) {
157                     updateButton(action);
158                 }
159                 fFirstValidAction= action;
160                 return;
161             }
162         }
163         if (fFirstValidAction != null) {
164             if (fActions.size() > 0) {
165                 updateButton((BuildpathModifierAction)fActions.get(0));
166             } else {
167                 updateButton(this);
168             }
169         }
170         fFirstValidAction= null;
171     }
172
173     private void updateButton(BuildpathModifierAction action) {
174         setImageDescriptor(action.getImageDescriptor());
175         setDisabledImageDescriptor(action.getDisabledImageDescriptor());
176         setText(action.getText());
177         setToolTipText(action.getToolTipText());
178     }
179 }
180
Popular Tags