1 19 20 package org.netbeans.modules.tasklist.suggestions.ui; 21 22 import java.awt.*; 23 import java.awt.event.ActionListener ; 24 import java.awt.event.ActionEvent ; 25 import java.util.List ; 26 import java.util.ArrayList ; 27 import java.util.Collection ; 28 import java.util.Iterator ; 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 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 SuggestionsView view = SuggestionsView.getCurrentView(); 65 if (view != null) { 66 return view.isShowing(); 67 } 68 return false; 69 } 70 71 protected String iconResource() { 72 return "org/netbeans/modules/tasklist/suggestions/showCategoryAction.gif"; } 74 75 public HelpCtx getHelpCtx() { 76 return HelpCtx.DEFAULT_HELP; 77 } 78 79 80 81 public String getName() { 82 return NbBundle.getMessage(ShowCategoryAction.class, 83 "ShowCategory"); } 85 86 90 public Component getToolbarPresenter() { 91 final Component original = super.getToolbarPresenter(); 92 AbstractButton ab = (AbstractButton) original; 93 ab.addActionListener(new ActionListener () { 94 public void actionPerformed(ActionEvent e) { 95 JPopupMenu menu = new JPopupMenu(); 96 97 SuggestionsView view = SuggestionsView.getCurrentView(); 101 if (view == null) { 102 return; 104 } 105 106 String current = null; 107 Filter filter = view.getFilter(); 108 if (view.isFiltered()) { 109 List 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 String allDesc = NbBundle.getMessage(ShowCategoryAction.class, 120 "All"); menu.add(createMenuItem(allDesc, null, (current == null) || 122 allDesc.equals(current))); 123 124 menu.addSeparator(); 126 127 Collection types = SuggestionTypes.getDefault().getAllTypes(); 128 Iterator it = types.iterator(); 129 while (it.hasNext()) { 130 SuggestionType type = (SuggestionType) it.next(); 131 String category = type.getLocalizedName(); 132 boolean isSelected = category.equals(current); 133 menu.add(createMenuItem(category, type, isSelected)); 134 } 135 137 menu.show(original, original.getWidth(), 0); 138 } 139 }); 140 return original; 141 } 142 143 146 public JMenuItem getMenuPresenter() { 147 JMenu mainItem = new JMenuPlus(); 148 Actions.setMenuText(mainItem, getName(), true); 149 mainItem.addMenuListener(new MainItemListener()); 154 return mainItem; 155 } 156 157 160 public JMenuItem getPopupPresenter() { 161 JMenu mainItem = new JMenuPlus(); 162 Actions.setMenuText(mainItem, getName(), true); 163 mainItem.addMenuListener(new MainItemListener()); 166 return mainItem; 167 } 168 169 170 public void performAction() { 171 } 173 174 private final static String TYPE = "type"; 177 private static JMenuItem createMenuItem(final String category, 178 SuggestionType type, 179 boolean isSelected) { 180 JMenuItem curMenuItem = new JRadioButtonMenuItem(category, 181 isSelected); 182 curMenuItem.addActionListener(new ActionListener () { 183 public void actionPerformed(ActionEvent e) { 185 JMenuItem item = (JMenuItem) e.getSource(); 186 SuggestionType type = (SuggestionType) (item.getClientProperty(TYPE)); 187 String category = null; 188 if (type != null) { 189 category = type.getLocalizedName(); 190 } 191 SuggestionsView view = SuggestionsView.getCurrentView(); 192 if (view == null) { 193 return; 195 } 196 197 SuggestionManagerImpl manager = 198 (SuggestionManagerImpl) SuggestionManager.getDefault(); 199 if (type == null) { view.setFilter(new SuggestionFilter("Simple")); 204 manager.notifyFiltered((SuggestionList) view.getList(), null); 205 view.notifyFiltered(null); 207 } else { 208 AppliedFilterCondition cond = 209 new AppliedFilterCondition( 210 SuggestionImplProperties.PROP_CATEGORY, 211 new StringFilterCondition(StringFilterCondition.EQUALS, category)); 212 213 List conditions = new ArrayList (1); 214 conditions.add(cond); 215 Filter filter = new SuggestionFilter(category); 223 filter.setConditions(conditions); 224 view.setFilter(filter); 227 manager.notifyFiltered((SuggestionList) view.getList(), type); 228 view.notifyFiltered(type); 230 } 231 } 232 }); 233 curMenuItem.putClientProperty(TYPE, type); return curMenuItem; 235 } 236 237 239 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 SuggestionsView view = SuggestionsView.getCurrentView(); 257 if (view == null) { 258 return; 260 } 261 262 String current = null; 263 Filter filter = view.getFilter(); 264 if (view.isFiltered()) { 265 List 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 String allDesc = NbBundle.getMessage(ShowCategoryAction.class, 276 "All"); menu.add(createMenuItem(allDesc, null, (current == null) || 278 allDesc.equals(current))); 279 280 menu.addSeparator(); 282 283 Collection types = SuggestionTypes.getDefault().getAllTypes(); 284 Iterator it = types.iterator(); 285 while (it.hasNext()) { 286 SuggestionType type = (SuggestionType) it.next(); 287 String 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 |