001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.java.navigation.actions;
043:
044: import java.awt.event.ActionEvent;
045: import javax.swing.AbstractAction;
046: import javax.swing.Action;
047: import javax.swing.ImageIcon;
048: import javax.swing.JMenuItem;
049: import javax.swing.JRadioButtonMenuItem;
050: import org.netbeans.modules.java.navigation.ClassMemberFilters;
051: import org.openide.util.NbBundle;
052: import org.openide.util.Utilities;
053: import org.openide.util.actions.Presenter;
054:
055: /** "Radio button" type action, base class designed for subclassing
056: *
057: * @author Dafe Simonek
058: */
059: public abstract class SortActionSupport extends AbstractAction
060: implements Presenter.Popup {
061:
062: private JRadioButtonMenuItem menuItem;
063: protected ClassMemberFilters filters;
064:
065: /** Creates a new instance of SortByNameAction */
066: public SortActionSupport(ClassMemberFilters filters) {
067: this .filters = filters;
068: }
069:
070: public final JMenuItem getPopupPresenter() {
071: JMenuItem result = obtainMenuItem();
072: updateMenuItem();
073: return result;
074: }
075:
076: protected final JRadioButtonMenuItem obtainMenuItem() {
077: if (menuItem == null) {
078: menuItem = new JRadioButtonMenuItem(
079: (String) getValue(Action.NAME));
080: menuItem.setAction(this );
081: }
082: return menuItem;
083: }
084:
085: protected abstract void updateMenuItem();
086:
087: public static final String SELECTED = "selected";
088:
089: /** Enables sorting by names when selected
090: */
091: public static final class SortByNameAction extends
092: SortActionSupport {
093:
094: public SortByNameAction(ClassMemberFilters filters) {
095: super (filters);
096: putValue(Action.NAME, NbBundle.getMessage(
097: SortByNameAction.class, "LBL_SortByName")); //NOI18N
098: putValue(
099: Action.SMALL_ICON,
100: new ImageIcon(
101: Utilities
102: .loadImage("org/netbeans/modules/java/navigation/resources/sortAlpha.png"))); //NOI18N
103: }
104:
105: public void actionPerformed(ActionEvent e) {
106: filters.setNaturalSort(false);
107: updateMenuItem();
108: }
109:
110: protected void updateMenuItem() {
111: JRadioButtonMenuItem mi = obtainMenuItem();
112: mi.setSelected(!filters.isNaturalSort());
113: }
114: } // end of SortByNameAction
115:
116: /** Enables sorting by names when selected
117: */
118: public static final class SortBySourceAction extends
119: SortActionSupport {
120:
121: public SortBySourceAction(ClassMemberFilters filters) {
122: super (filters);
123: putValue(Action.NAME, NbBundle.getMessage(
124: SortBySourceAction.class, "LBL_SortBySource")); //NOI18N
125: putValue(
126: Action.SMALL_ICON,
127: new ImageIcon(
128: Utilities
129: .loadImage("org/netbeans/modules/java/navigation/resources/sortPosition.png"))); //NOI18N
130: }
131:
132: public void actionPerformed(ActionEvent e) {
133: filters.setNaturalSort(true);
134: updateMenuItem();
135: }
136:
137: protected void updateMenuItem() {
138: JRadioButtonMenuItem mi = obtainMenuItem();
139: mi.setSelected(filters.isNaturalSort());
140: }
141: } // end of SortBySourceAction
142:
143: }
|