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.cnd.classview.actions;
043:
044: import java.awt.event.ActionEvent;
045: import java.util.ArrayList;
046: import java.util.Collection;
047: import java.util.Collections;
048: import java.util.Iterator;
049: import java.util.List;
050: import javax.swing.AbstractAction;
051: import javax.swing.JMenu;
052: import javax.swing.JMenuItem;
053: import org.netbeans.modules.cnd.api.model.CsmFile;
054: import org.netbeans.modules.cnd.api.model.CsmOffsetableDeclaration;
055: import org.netbeans.modules.cnd.classview.resources.I18n;
056: import org.netbeans.modules.cnd.utils.cache.CharSequenceKey;
057: import org.openide.util.actions.Presenter;
058:
059: /**
060: *
061: * @author Alexander Simon
062: */
063: public class MoreDeclarations extends AbstractAction implements
064: Presenter.Popup {
065: private static final String PROP_DECLARATION = "prop_declaration"; // NOI18N
066: private Collection<? extends CsmOffsetableDeclaration> arr;
067:
068: public MoreDeclarations(
069: Collection<? extends CsmOffsetableDeclaration> arr) {
070: this .arr = arr;
071: }
072:
073: public JMenuItem getPopupPresenter() {
074: JMenu result = new JMenu();
075: List<ItemWrapper> list = new ArrayList<ItemWrapper>();
076: for (CsmOffsetableDeclaration decl : arr) {
077: list.add(new ItemWrapper(decl));
078: }
079: Collections.sort(list);
080: result.setText(I18n.getMessage("LBL_MoreDeclarations")); //NOI18N
081: if (list.size() < 36) {
082: for (ItemWrapper i : list) {
083: result.add(createItem(i.decl));
084: }
085: } else {
086: int n = (int) Math.ceil(Math.sqrt((double) list.size()));
087: Iterator<ItemWrapper> i = list.iterator();
088: while (i.hasNext()) {
089: JMenu current = new JMenu();
090: CsmOffsetableDeclaration first = null;
091: CsmOffsetableDeclaration last = null;
092: for (int j = 0; j < n && i.hasNext(); j++) {
093: CsmOffsetableDeclaration decl = i.next().decl;
094: if (j == 0) {
095: first = decl;
096: } else {
097: last = decl;
098: }
099: current.add(createItem(decl));
100: }
101: if (first != null && last != null) {
102: current.setText(first.getContainingFile().getName()
103: + " ... "
104: + last.getContainingFile().getName()); // NOI18N
105: result.add(current);
106: } else {
107: result.add(createItem(first));
108: }
109: }
110: }
111: return result;
112: }
113:
114: private JMenuItem createItem(final CsmOffsetableDeclaration decl) {
115: JMenuItem item = new JMenuItem();
116: CsmFile file = decl.getContainingFile();
117: item.setText(file.getName().toString());
118: item.putClientProperty(PROP_DECLARATION, decl);
119: item.addActionListener(this );
120: return item;
121: }
122:
123: public void actionPerformed(ActionEvent ae) {
124: JMenuItem item = (JMenuItem) ae.getSource();
125: CsmOffsetableDeclaration decl = (CsmOffsetableDeclaration) item
126: .getClientProperty(PROP_DECLARATION);
127: GoToDeclarationAction action = new GoToDeclarationAction(decl);
128: action.actionPerformed(null);
129: }
130:
131: private class ItemWrapper implements Comparable<ItemWrapper> {
132: private CharSequence name;
133: private CsmOffsetableDeclaration decl;
134:
135: private ItemWrapper(CsmOffsetableDeclaration decl) {
136: this .decl = decl;
137: name = decl.getContainingFile().getName();
138: }
139:
140: public int compareTo(MoreDeclarations.ItemWrapper o) {
141: return CharSequenceKey.Comparator.compare(name, o.name);
142: }
143: }
144: }
|