KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > suggestions > ui > ShowCategoryAction


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.tasklist.suggestions.ui;
21
22 import java.awt.*;
23 import java.awt.event.ActionListener JavaDoc;
24 import java.awt.event.ActionEvent JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.Iterator JavaDoc;
29
30 import javax.swing.event.*;
31 import javax.swing.*;
32
33 import org.openide.util.HelpCtx;
34 import org.openide.util.NbBundle;
35 import org.openide.util.actions.*;
36 import org.openide.awt.*;
37 import org.netbeans.modules.tasklist.client.SuggestionManager;
38 import org.netbeans.modules.tasklist.core.filter.Filter;
39 import org.netbeans.modules.tasklist.core.filter.FilterCondition;
40 import org.netbeans.modules.tasklist.core.filter.StringFilterCondition;
41 import org.netbeans.modules.tasklist.core.filter.AppliedFilterCondition;
42
43 import org.netbeans.modules.tasklist.suggestions.*;
44
45
46 /**
47  * Let the user choose one particular category from
48  * pull down menu.
49  *
50  * @author Tor Norbye
51  * @author Petr Kuzel, toolbar presenter
52  */

53 public final class ShowCategoryAction extends CallableSystemAction
54         implements Presenter.Menu {
55
56     private static final long serialVersionUID = 1;
57
58     protected boolean asynchronous() {
59         return false;
60     }
61
62     public boolean isEnabled() {
63         // FIXME works only for the live suggestions view
64
SuggestionsView view = SuggestionsView.getCurrentView();
65         if (view != null) {
66             return view.isShowing();
67         }
68         return false;
69     }
70
71     protected String JavaDoc iconResource() {
72         return "org/netbeans/modules/tasklist/suggestions/showCategoryAction.gif"; // NOI18N
73
}
74
75     public HelpCtx getHelpCtx() {
76         return HelpCtx.DEFAULT_HELP;
77     }
78
79
80     /* @return Returns localized name of this action */
81     public String JavaDoc getName() {
82         return NbBundle.getMessage(ShowCategoryAction.class,
83                 "ShowCategory"); // NOI18N
84
}
85
86     /* Returns a Component that presents the Action, that implements this
87     * interface, in a ToolBar.
88     * @return the Component representation for the Action
89     */

90     public Component getToolbarPresenter() {
91         final Component original = super.getToolbarPresenter();
92         AbstractButton ab = (AbstractButton) original;
93         ab.addActionListener(new ActionListener JavaDoc() {
94             public void actionPerformed(ActionEvent JavaDoc e) {
95                 JPopupMenu menu = new JPopupMenu();
96
97                 // FIXME works only for the live suggestions view
98
// XXX missing common super interface for JPopupMenu and JMenu
99
// <<<< from menu presenter
100
SuggestionsView view = SuggestionsView.getCurrentView();
101                 if (view == null) {
102                     // INTERNAL ERROR
103
return;
104                 }
105
106                 String JavaDoc current = null;
107                 Filter filter = view.getFilter();
108                 if (view.isFiltered()) {
109                     List JavaDoc aconditions = filter.getConditions();
110                     if ((aconditions != null) && (aconditions.size() > 0)) {
111                         AppliedFilterCondition acondition = (AppliedFilterCondition) aconditions.get(0);
112                         if (acondition.getCondition() instanceof StringFilterCondition) {
113                             current = ((StringFilterCondition) acondition.getCondition()).getConstant();
114                         }
115                     }
116                 }
117
118                 // Add All item
119
String JavaDoc allDesc = NbBundle.getMessage(ShowCategoryAction.class,
120                         "All"); // NOI18N
121
menu.add(createMenuItem(allDesc, null, (current == null) ||
122                         allDesc.equals(current)));
123
124                 // Add separator
125
menu.addSeparator();
126
127                 Collection JavaDoc types = SuggestionTypes.getDefault().getAllTypes();
128                 Iterator JavaDoc it = types.iterator();
129                 while (it.hasNext()) {
130                     SuggestionType type = (SuggestionType) it.next();
131                     String JavaDoc category = type.getLocalizedName();
132                     boolean isSelected = category.equals(current);
133                     menu.add(createMenuItem(category, type, isSelected));
134                 }
135                 // >>>> from menu presenter
136

137                 menu.show(original, original.getWidth(), 0);
138             }
139         });
140         return original;
141     }
142
143     /* Returns a submneu that will present this action in a Menu.
144     * @return the JMenuItem representation for this action
145     */

146     public JMenuItem getMenuPresenter() {
147         JMenu mainItem = new JMenuPlus();
148         Actions.setMenuText(mainItem, getName(), true);
149         //mainItem.setIcon (SystemAction.get(
150
// ShowCategoryAction.class).getIcon());
151
//HelpCtx.setHelpIDString (mainItem,
152
// ShowCategoryAction.class.getName());
153
mainItem.addMenuListener(new MainItemListener());
154         return mainItem;
155     }
156
157     /* Returns a submneu that will present this action in a PopupMenu.
158     * @return the JMenuItem representation for this action
159     */

160     public JMenuItem getPopupPresenter() {
161         JMenu mainItem = new JMenuPlus();
162         Actions.setMenuText(mainItem, getName(), true);
163         //HelpCtx.setHelpIDString (mainItem,
164
// ShowCategoryAction.class.getName());
165
mainItem.addMenuListener(new MainItemListener());
166         return mainItem;
167     }
168
169
170     public void performAction() {
171         // all functionality is accomplished by menu listeners
172
}
173
174     // Property I attach suggestion types to on menu items
175
private final static String JavaDoc TYPE = "type"; // NOI18N
176

177     private static JMenuItem createMenuItem(final String JavaDoc category,
178                                      SuggestionType type,
179                                      boolean isSelected) {
180         JMenuItem curMenuItem = new JRadioButtonMenuItem(category,
181                 isSelected);
182         curMenuItem.addActionListener(new ActionListener JavaDoc() {
183             // Select the given category
184
public void actionPerformed(ActionEvent JavaDoc e) {
185                 JMenuItem item = (JMenuItem) e.getSource();
186                 SuggestionType type = (SuggestionType) (item.getClientProperty(TYPE));
187                 String JavaDoc category = null;
188                 if (type != null) {
189                     category = type.getLocalizedName();
190                 }
191                 SuggestionsView view = SuggestionsView.getCurrentView();
192                 if (view == null) {
193                     // INTERNAL ERROR
194
return;
195                 }
196
197                 SuggestionManagerImpl manager =
198                         (SuggestionManagerImpl) SuggestionManager.getDefault();
199                 if (type == null) { // All
200
// TODO : boolean parameter?
201
// view.setFilter(new SuggestionFilter("Simple"), false); // NOI18N
202
view.setFilter(new SuggestionFilter("Simple")); // NOI18N
203

204                     manager.notifyFiltered((SuggestionList) view.getList(), null);
205                     // Make sure you do this AFTER view.setFilter
206
view.notifyFiltered(null);
207                 } else {
208                     AppliedFilterCondition cond =
209               new AppliedFilterCondition(
210                 SuggestionImplProperties.PROP_CATEGORY,
211                             new StringFilterCondition(StringFilterCondition.EQUALS, category));
212
213                     List JavaDoc conditions = new ArrayList JavaDoc(1);
214                     conditions.add(cond);
215                     // If you ever construct more complicated filters, e.g.
216
// filters which allow multiple SuggestionProviders to
217
// be visible simultaneously, update the code in
218
// SuggestionManagerImpl which deals with the field
219
// named "unfiltered", since it makes an assumption that
220
// one and only one is visible at a time (unless there
221
// is no filtering in effect.)
222
Filter filter = new SuggestionFilter(category);
223                     filter.setConditions(conditions);
224             // TODO : boolean parameter?
225
// view.setFilter(filter, true);
226
view.setFilter(filter);
227                     manager.notifyFiltered((SuggestionList) view.getList(), type);
228                     // Make sure you do this AFTER view.setFilter
229
view.notifyFiltered(type);
230                 }
231             }
232         });
233         curMenuItem.putClientProperty(TYPE, type); // NOI18N
234
return curMenuItem;
235     }
236
237     // innerclasses .......................................................
238

239     /** Listens to selecting of main item and expands it to the
240      * submenu of exiting and new modes
241      */

242     private static final class MainItemListener implements MenuListener {
243
244         public void menuCanceled(MenuEvent e) {
245         }
246
247         public void menuDeselected(MenuEvent e) {
248             JMenu menu = (JMenu) e.getSource();
249             menu.removeAll();
250         }
251
252         public void menuSelected(MenuEvent e) {
253             JMenu menu = (JMenu) e.getSource();
254
255             // XXX missing common super interface for JPopupMenu and JMenu
256
SuggestionsView view = SuggestionsView.getCurrentView();
257             if (view == null) {
258                 // INTERNAL ERROR
259
return;
260             }
261
262             String JavaDoc current = null;
263             Filter filter = view.getFilter();
264             if (view.isFiltered()) {
265                 List JavaDoc conditions = filter.getConditions();
266                 if ((conditions != null) && (conditions.size() > 0)) {
267                     FilterCondition condition = (FilterCondition) conditions.get(0);
268                     if (condition instanceof StringFilterCondition) {
269                         current = ((StringFilterCondition) condition).getConstant();
270                     }
271                 }
272             }
273
274             // Add All item
275
String JavaDoc allDesc = NbBundle.getMessage(ShowCategoryAction.class,
276                     "All"); // NOI18N
277
menu.add(createMenuItem(allDesc, null, (current == null) ||
278                     allDesc.equals(current)));
279
280             // Add separator
281
menu.addSeparator();
282
283             Collection JavaDoc types = SuggestionTypes.getDefault().getAllTypes();
284             Iterator JavaDoc it = types.iterator();
285             while (it.hasNext()) {
286                 SuggestionType type = (SuggestionType) it.next();
287                 String JavaDoc category = type.getLocalizedName();
288                 boolean isSelected = category.equals(current);
289                 menu.add(createMenuItem(category, type, isSelected));
290             }
291         }
292
293
294     }
295 }
296
Popular Tags