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: package org.netbeans.modules.gsfret.navigation;
042:
043: import java.awt.BorderLayout;
044: import java.awt.Rectangle;
045: import javax.swing.Action;
046: import javax.swing.KeyStroke;
047: import java.awt.Toolkit;
048: import java.awt.event.KeyEvent;
049: import java.beans.PropertyVetoException;
050: import javax.swing.JLabel;
051: import javax.swing.SwingUtilities;
052: import javax.swing.border.EmptyBorder;
053: import javax.swing.KeyStroke;
054: import javax.swing.event.ChangeEvent;
055: import org.netbeans.modules.gsf.api.StructureItem;
056: import org.netbeans.modules.gsfret.navigation.ClassMemberFilters;
057: import org.netbeans.modules.gsfret.navigation.ElementNode;
058: import org.netbeans.modules.gsfret.navigation.actions.FilterSubmenuAction;
059: import org.netbeans.modules.gsfret.navigation.actions.SortActionSupport.SortByNameAction;
060: import org.netbeans.modules.gsfret.navigation.actions.SortActionSupport.SortBySourceAction;
061: import org.netbeans.modules.gsfret.navigation.base.FiltersManager;
062: import org.openide.nodes.Node;
063: import org.openide.util.NbBundle;
064: import org.openide.util.Utilities;
065: import org.netbeans.modules.gsfret.navigation.base.TapPanel;
066: import org.openide.explorer.ExplorerManager;
067: import org.openide.explorer.view.BeanTreeView;
068: import org.openide.filesystems.FileObject;
069: import org.openide.util.Exceptions;
070: import org.openide.util.Lookup;
071:
072: /**
073: * This file is originally from Retouche, the Java Support
074: * infrastructure in NetBeans. I have modified the file as little
075: * as possible to make merging Retouche fixes back as simple as
076: * possible.
077: * <p>
078: *
079: * @author phrebejk
080: */
081: public class ClassMemberPanelUI extends javax.swing.JPanel implements
082: ExplorerManager.Provider, FiltersManager.FilterChangeListener {
083:
084: private ExplorerManager manager = new ExplorerManager();
085: private MyBeanTreeView elementView;
086: private TapPanel filtersPanel;
087: private JLabel filtersLbl;
088: private Lookup lookup = null; // XXX may need better lookup
089: private ClassMemberFilters filters;
090:
091: private Action[] actions; // General actions for the panel
092:
093: private static final Rectangle ZERO = new Rectangle(0, 0, 1, 1);
094:
095: /** Creates new form ClassMemberPanelUi */
096: public ClassMemberPanelUI() {
097:
098: initComponents();
099:
100: // Tree view of the elements
101: elementView = createBeanTreeView();
102: add(elementView, BorderLayout.CENTER);
103:
104: // filters
105: filtersPanel = new TapPanel();
106: filtersLbl = new JLabel(NbBundle.getMessage(
107: ClassMemberPanelUI.class, "LBL_Filter")); //NOI18N
108: filtersLbl.setBorder(new EmptyBorder(0, 5, 5, 0));
109: filtersPanel.add(filtersLbl);
110: filtersPanel.setOrientation(TapPanel.DOWN);
111: // tooltip
112: KeyStroke toggleKey = KeyStroke.getKeyStroke(KeyEvent.VK_T,
113: Toolkit.getDefaultToolkit().getMenuShortcutKeyMask());
114: String keyText = Utilities.keyToString(toggleKey);
115: filtersPanel.setToolTipText(NbBundle.getMessage(
116: ClassMemberPanelUI.class, "TIP_TapPanel", keyText));
117:
118: filters = new ClassMemberFilters(this );
119: filters.getInstance().hookChangeListener(this );
120: filtersPanel.add(filters.getComponent());
121:
122: actions = new Action[] { new SortByNameAction(filters),
123: new SortBySourceAction(filters), null,
124: new FilterSubmenuAction(filters.getInstance()) };
125:
126: add(filtersPanel, BorderLayout.SOUTH);
127:
128: manager.setRootContext(ElementNode.getWaitNode());
129:
130: }
131:
132: @Override
133: public boolean requestFocusInWindow() {
134: boolean result = super .requestFocusInWindow();
135: elementView.requestFocusInWindow();
136: return result;
137: }
138:
139: public org.openide.util.Lookup getLookup() {
140: // XXX Check for chenge of FileObject
141: return lookup;
142: }
143:
144: public org.netbeans.modules.gsfret.navigation.ElementScanningTask getTask() {
145:
146: return new ElementScanningTask(this );
147:
148: }
149:
150: public void showWaitNode() {
151: SwingUtilities.invokeLater(new Runnable() {
152: public void run() {
153: elementView.setRootVisible(true);
154: manager.setRootContext(ElementNode.getWaitNode());
155: }
156: });
157: }
158:
159: public void selectElementNode(int offset) {
160: ElementNode root = getRootNode();
161: if (root == null) {
162: return;
163: }
164: final ElementNode node = root.getNodeForOffset(offset);
165: Node[] selectedNodes = manager.getSelectedNodes();
166: if (!(selectedNodes != null && selectedNodes.length == 1 && selectedNodes[0] == node)) {
167: try {
168: manager
169: .setSelectedNodes(new Node[] { node == null ? getRootNode()
170: : node });
171: } catch (PropertyVetoException propertyVetoException) {
172: Exceptions.printStackTrace(propertyVetoException);
173: }
174: }
175: }
176:
177: public void refresh(final StructureItem description,
178: final FileObject fileObject) {
179: final ElementNode rootNode = getRootNode();
180:
181: if (rootNode != null
182: && rootNode.getFileObject().equals(fileObject)) {
183: // update
184: //System.out.println("UPDATE ======" + description.fileObject.getName() );
185: SwingUtilities.invokeLater(new Runnable() {
186: public void run() {
187: rootNode.updateRecursively(description);
188: }
189: });
190: } else {
191: //System.out.println("REFRES =====" + description.fileObject.getName() );
192: // New fileobject => refresh completely
193: SwingUtilities.invokeLater(new Runnable() {
194:
195: public void run() {
196: elementView.setRootVisible(false);
197: manager.setRootContext(new ElementNode(description,
198: ClassMemberPanelUI.this , fileObject));
199: boolean scrollOnExpand = elementView
200: .getScrollOnExpand();
201: elementView.setScrollOnExpand(false);
202: elementView.expandAll();
203: elementView.setScrollOnExpand(scrollOnExpand);
204: }
205: });
206:
207: }
208: }
209:
210: public void sort() {
211: getRootNode().refreshRecursively();
212: }
213:
214: public ClassMemberFilters getFilters() {
215: return filters;
216: }
217:
218: public void expandNode(Node n) {
219: elementView.expandNode(n);
220: }
221:
222: public Action[] getActions() {
223: return actions;
224: }
225:
226: public FileObject getFileObject() {
227: return getRootNode().getFileObject();
228: }
229:
230: // FilterChangeListener ----------------------------------------------------
231:
232: public void filterStateChanged(ChangeEvent e) {
233: ElementNode root = getRootNode();
234:
235: if (root != null) {
236: root.refreshRecursively();
237: }
238: }
239:
240: /** This method is called from within the constructor to
241: * initialize the form.
242: * WARNING: Do NOT modify this code. The content of this method is
243: * always regenerated by the Form Editor.
244: */
245: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
246: private void initComponents() {
247:
248: setLayout(new java.awt.BorderLayout());
249: }// </editor-fold>//GEN-END:initComponents
250:
251: // Variables declaration - do not modify//GEN-BEGIN:variables
252: // End of variables declaration//GEN-END:variables
253:
254: // Private methods ---------------------------------------------------------
255:
256: private ElementNode getRootNode() {
257:
258: Node n = manager.getRootContext();
259: if (n instanceof ElementNode) {
260: return (ElementNode) n;
261: } else {
262: return null;
263: }
264: }
265:
266: private MyBeanTreeView createBeanTreeView() {
267: // ActionMap map = getActionMap();
268: // map.put(DefaultEditorKit.copyAction, ExplorerUtils.actionCopy(manager));
269: // map.put(DefaultEditorKit.cutAction, ExplorerUtils.actionCut(manager));
270: // map.put(DefaultEditorKit.pasteAction, ExplorerUtils.actionPaste(manager));
271: // map.put("delete", new DelegatingAction(ActionProvider.COMMAND_DELETE, ExplorerUtils.actionDelete(manager, true)));
272: //
273:
274: MyBeanTreeView btv = new MyBeanTreeView(); // Add the BeanTreeView
275: // btv.setDragSource (true);
276: // btv.setRootVisible(false);
277: // associateLookup( ExplorerUtils.createLookup(manager, map) );
278: return btv;
279:
280: }
281:
282: // ExplorerManager.Provider imlementation ----------------------------------
283:
284: public ExplorerManager getExplorerManager() {
285: return manager;
286: }
287:
288: private static class MyBeanTreeView extends BeanTreeView {
289: public boolean getScrollOnExpand() {
290: return tree.getScrollsOnExpand();
291: }
292:
293: public void setScrollOnExpand(boolean scroll) {
294: this.tree.setScrollsOnExpand(scroll);
295: }
296: }
297: }
|