|
What this is
Other links
The source code/* * Sun Public License Notice * * The contents of this file are subject to the Sun Public License * Version 1.0 (the "License"). You may not use this file except in * compliance with the License. A copy of the License is available at * http://www.sun.com/ * * The Original Code is NetBeans. The Initial Developer of the Original * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun * Microsystems, Inc. All Rights Reserved. */ package org.netbeans.modules.java.tools; import java.awt.Dialog; import java.awt.event.ActionEvent; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import java.text.MessageFormat; import java.util.*; import java.util.Iterator; import javax.swing.JEditorPane; import javax.swing.text.JTextComponent; import javax.swing.text.TextAction; import org.netbeans.jmi.javamodel.ClassDefinition; import org.netbeans.jmi.javamodel.ClassMember; import org.netbeans.jmi.javamodel.Element; import org.netbeans.jmi.javamodel.JavaClass; import org.netbeans.jmi.javamodel.Method; import org.netbeans.jmi.javamodel.Resource; import org.netbeans.modules.java.JavaEditor; import org.netbeans.modules.javacore.internalapi.JMIElementCookie; import org.netbeans.modules.javacore.internalapi.JavaMetamodel; import org.openide.*; import org.openide.cookies.EditorCookie; import org.openide.loaders.DataObject; import org.openide.loaders.DataShadow; import org.openide.nodes.*; import org.openide.text.CloneableEditorSupport; import org.openide.text.PositionBounds; import org.openide.text.PositionRef; import org.openide.util.HelpCtx; import org.openide.util.NbBundle; import org.openide.util.actions.NodeAction; import org.openide.windows.TopComponent; /** This action allows the user to override some of the methods present in the * selected class. * The action displays a modal dialog with several lists, then overrides * one or more methods the user selects. */ public class OverrideAction extends NodeAction { private DelegateTextAction delegateAction = new DelegateTextAction(); static String getString(String key) { return NbBundle.getMessage(OverrideAction.class, key); } public HelpCtx getHelpCtx() { return null; } public String getName() { return getString("LAB_OverrideTool"); } public void performAction(Node[] activatedNodes) { ClassDefinition el = getJavaClass(activatedNodes[0]); boolean isInterface = el instanceof JavaClass && ((JavaClass)el).isInterface(); if (el == null || isInterface) return; JMIInheritanceSupport support = new JMIInheritanceSupport(el); String title = MessageFormat.format(getString("LBL_OverridePanel2_Title"), //NOI18N new Object[] { el.getName() } ); OverridePanel2 panel = new OverridePanel2(support); final DialogDescriptor desc = new DialogDescriptor(panel, title, true, DialogDescriptor.OK_CANCEL_OPTION, DialogDescriptor.OK_OPTION, null); panel.addPropertyChangeListener(new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent evt) { if (OverridePanel2.PROP_VALID.equals(evt.getPropertyName())) { desc.setValid(((Boolean) evt.getNewValue()).booleanValue()); } } }); desc.setHelpCtx(null); Dialog dialog = DialogDisplayer.getDefault().createDialog(desc); dialog.show(); dialog.dispose(); if (desc.getValue().equals(DialogDescriptor.OK_OPTION)) { Collection methods = panel.getMethods(); Method first = null; for (Iterator i = methods.iterator(); i.hasNext(); ) { Method m = (Method) i.next(); m = support.overrideMethod(m, panel.isGenerateSuperCalls(), panel.isGenerateJavadoc()); if (first == null) { first = m; } } if (first != null) { PositionBounds bounds = JavaMetamodel.getManager().getElementPosition(first); PositionRef beginPos = bounds.getBegin(); CloneableEditorSupport editSupp = beginPos.getCloneableEditorSupport(); JEditorPane[] panes = editSupp.getOpenedPanes(); if (panes==null) { editSupp.open(); } } } } protected boolean enable(Node[] activatedNodes) { if (activatedNodes.length != 1) { return false; } return getElement(activatedNodes[0]) != null; } private Element getElement(Node n) { JMIElementCookie cookie = (JMIElementCookie) n.getCookie(JMIElementCookie.class); if (cookie == null) return null; Element el = cookie.getElement(); if (!((el instanceof Resource) || (el instanceof ClassMember) || (el instanceof ClassDefinition))) { el = null; } return el; } private ClassDefinition getJavaClass(Node n) { int caretPosition = -1; EditorCookie ec = (EditorCookie) n.getCookie(EditorCookie.class); if (ec != null) { TopComponent activetc = TopComponent.getRegistry().getActivated(); if (activetc instanceof JavaEditor.JavaEditorComponent) { caretPosition = delegateAction.getTextComponent().getCaretPosition(); } } Element el = null; if (caretPosition != -1) { DataObject dobj = (DataObject)n.getCookie(DataObject.class); while (dobj instanceof DataShadow) { dobj = ((DataShadow) dobj).getOriginal(); } el = JavaMetamodel.getManager().getElementByOffset(dobj.getPrimaryFile(), caretPosition); } else { el = getElement(n); } if (el instanceof Resource) { for (Iterator iter = ((Resource) el).getClassifiers().iterator(); iter.hasNext(); ) { Object obj = iter.next(); if (obj instanceof JavaClass) { return (JavaClass) obj; } } return null; } while (!(el instanceof ClassDefinition)) { el = (Element)el.refImmediateComposite(); } return (ClassDefinition) el; } protected boolean asynchronous() { return false; } // .......................................................................... private static final class DelegateTextAction extends TextAction { public DelegateTextAction() { super(""); } public void actionPerformed(ActionEvent a) { } public JTextComponent getTextComponent() { return getFocusedComponent(); } }; // DelegateTextAction } |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.