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-2007 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.gsfret.navigation;
043:
044: import java.util.ArrayList;
045: import java.util.Collection;
046: import java.util.Collections;
047: import javax.swing.ImageIcon;
048: import javax.swing.JComponent;
049: import org.netbeans.modules.gsf.api.ElementKind;
050: import org.netbeans.modules.gsf.api.Modifier;
051: import org.netbeans.modules.gsf.api.StructureItem;
052: import org.netbeans.modules.gsfret.navigation.ElementNode.Description;
053: import org.netbeans.modules.gsfret.navigation.base.FiltersDescription;
054: import org.netbeans.modules.gsfret.navigation.base.FiltersManager;
055: import org.openide.util.NbBundle;
056: import org.openide.util.Utilities;
057:
058: /**
059: * This file is originally from Retouche, the Java Support
060: * infrastructure in NetBeans. I have modified the file as little
061: * as possible to make merging Retouche fixes back as simple as
062: * possible.
063: * <p>
064: * Creates filtering for the ClassMemberPanel
065: *
066: * @author phrebejk
067: */
068: public final class ClassMemberFilters {
069:
070: private ClassMemberPanelUI ui;
071:
072: /** constants for defined filters */
073: private static final String SHOW_NON_PUBLIC = "show_non_public";
074: private static final String SHOW_STATIC = "show_static";
075: private static final String SHOW_FIELDS = "show_fields";
076: private static final String SHOW_INHERITED = "show_inherited";
077:
078: private FiltersManager filters;
079:
080: private boolean naturalSort = false;
081:
082: /** Creates a new instance of ClassMemberFilters */
083: ClassMemberFilters(ClassMemberPanelUI ui) {
084: this .ui = ui;
085: }
086:
087: public FiltersManager getInstance() {
088: if (filters == null) {
089: filters = createFilters();
090: }
091: return filters;
092: }
093:
094: public JComponent getComponent() {
095: FiltersManager f = getInstance();
096: return f.getComponent();
097:
098: }
099:
100: public Collection<StructureItem> filter(
101: Collection<StructureItem> original) {
102:
103: boolean non_public = filters.isSelected(SHOW_NON_PUBLIC);
104: boolean statik = filters.isSelected(SHOW_STATIC);
105: boolean fields = filters.isSelected(SHOW_FIELDS);
106: // XXX Enable later boolean inherited = filters.isSelected(SHOW_INHERITED);
107:
108: ArrayList<StructureItem> result = new ArrayList<StructureItem>(
109: original.size());
110: for (StructureItem description : original) {
111:
112: // I default no access modifier to mean public, unlike the Java version where it means package protected
113: if (!non_public
114: && (description.getModifiers().contains(
115: Modifier.PROTECTED) || description
116: .getModifiers().contains(Modifier.PRIVATE))) {
117: // if ( !non_public &&
118: // !description.getModifiers().contains(Modifier.PUBLIC)
119: // /* Fix for #89777 && !description.modifiers.contains(Modifier.PROTECTED) */ ) {
120: continue;
121: }
122:
123: if (!statik
124: && description.getModifiers().contains(
125: Modifier.STATIC)) {
126: continue;
127: }
128:
129: if (!fields
130: && (description.getKind() == ElementKind.FIELD || (description
131: .getKind() == ElementKind.ATTRIBUTE))) {
132: continue;
133: }
134:
135: // XXX Inherited members
136:
137: result.add(description);
138: }
139:
140: Collections.sort(result,
141: isNaturalSort() ? Description.POSITION_COMPARATOR
142: : Description.ALPHA_COMPARATOR);
143:
144: return result;
145: }
146:
147: public boolean isNaturalSort() {
148: return naturalSort;
149: }
150:
151: public void setNaturalSort(boolean naturalSort) {
152: this .naturalSort = naturalSort;
153: ui.sort();
154: }
155:
156: // Privare methods ---------------------------------------------------------
157:
158: /** Creates filter descriptions and filters itself */
159: private static FiltersManager createFilters() {
160: FiltersDescription desc = new FiltersDescription();
161:
162: // XXX to be enabled later
163: // desc.addFilter(SHOW_INHERITED,
164: // NbBundle.getMessage(ClassMemberFilters.class, "LBL_ShowInherited"), //NOI18N
165: // NbBundle.getMessage(ClassMemberFilters.class, "LBL_ShowInheritedTip"), //NOI18N
166: // false,
167: // new ImageIcon (Utilities.loadImage("org/netbeans/modules/gsfret/navigation/resources/filterHideInherited.png")), //NOI18N
168: // null
169: // );
170: desc
171: .addFilter(
172: SHOW_FIELDS,
173: NbBundle.getMessage(ClassMemberFilters.class,
174: "LBL_ShowFields"), //NOI18N
175: NbBundle.getMessage(ClassMemberFilters.class,
176: "LBL_ShowFieldsTip"), //NOI18N
177: true,
178: new ImageIcon(
179: Utilities
180: .loadImage("org/netbeans/modules/gsfret/navigation/resources/filterHideFields.gif")), //NOI18N
181: null);
182: desc
183: .addFilter(
184: SHOW_STATIC,
185: NbBundle.getMessage(ClassMemberFilters.class,
186: "LBL_ShowStatic"), //NOI18N
187: NbBundle.getMessage(ClassMemberFilters.class,
188: "LBL_ShowStaticTip"), //NOI18N
189: true,
190: new ImageIcon(
191: Utilities
192: .loadImage("org/netbeans/modules/gsfret/navigation/resources/filterHideStatic.png")), //NOI18N
193: null);
194: desc
195: .addFilter(
196: SHOW_NON_PUBLIC,
197: NbBundle.getMessage(ClassMemberFilters.class,
198: "LBL_ShowNonPublic"), //NOI18N
199: NbBundle.getMessage(ClassMemberFilters.class,
200: "LBL_ShowNonPublicTip"), //NOI18N
201: true,
202: new ImageIcon(
203: Utilities
204: .loadImage("org/netbeans/modules/gsfret/navigation/resources/filterHideNonPublic.png")), //NOI18N
205: null);
206:
207: return FiltersDescription.createManager(desc);
208: }
209:
210: }
|