1 19 20 package org.netbeans.modules.java.tools; 21 22 import java.awt.Dialog ; 23 import java.awt.event.ActionEvent ; 24 import java.beans.PropertyChangeEvent ; 25 import java.beans.PropertyChangeListener ; 26 import java.text.MessageFormat ; 27 import java.util.*; 28 import java.util.Iterator ; 29 import javax.swing.JEditorPane ; 30 import javax.swing.text.JTextComponent ; 31 import javax.swing.text.TextAction ; 32 import org.netbeans.api.mdr.MDRepository; 33 import org.netbeans.jmi.javamodel.ClassDefinition; 34 import org.netbeans.jmi.javamodel.ClassMember; 35 import org.netbeans.jmi.javamodel.Element; 36 import org.netbeans.jmi.javamodel.JavaClass; 37 import org.netbeans.jmi.javamodel.Method; 38 import org.netbeans.jmi.javamodel.Resource; 39 import org.netbeans.modules.java.JavaDataObject; 40 import org.netbeans.modules.javacore.internalapi.JavaMetamodel; 41 import org.openide.*; 42 import org.openide.cookies.EditorCookie; 43 import org.openide.loaders.DataObject; 44 import org.openide.loaders.DataShadow; 45 import org.openide.nodes.*; 46 import org.openide.text.CloneableEditorSupport; 47 import org.openide.text.PositionBounds; 48 import org.openide.text.PositionRef; 49 50 import org.openide.util.HelpCtx; 51 import org.openide.util.NbBundle; 52 import org.openide.util.actions.NodeAction; 53 import org.openide.windows.TopComponent; 54 55 60 public class OverrideAction extends NodeAction { 61 62 private DelegateTextAction delegateAction = new DelegateTextAction(); 63 64 static String getString(String key) { 65 return NbBundle.getMessage(OverrideAction.class, key); 66 } 67 68 public OverrideAction () { 69 putValue("noIconInMenu", Boolean.TRUE); } 71 72 public HelpCtx getHelpCtx() { 73 return null; 74 } 75 76 public String getName() { 77 return getString("LAB_OverrideTool"); 78 } 79 80 public void performAction(final Node[] activatedNodes) { 81 Runnable run = new Runnable () { 82 public void run() { 83 ClassDefinition el = getJavaClass(activatedNodes[0]); 84 boolean isInterface = el instanceof JavaClass && ((JavaClass)el).isInterface(); 85 if (isInterface) { 86 NotifyDescriptor nd = new NotifyDescriptor( 87 getString("LAB_OverrideWarningDiagText"), 88 getString("LAB_OverrideWarningDiagTitle"), 89 NotifyDescriptor.DEFAULT_OPTION, 90 NotifyDescriptor.WARNING_MESSAGE, 91 null, 92 null 93 ); 94 DialogDisplayer.getDefault().notify(nd); 95 return; 96 } 97 if (el == null) return; 98 99 JMIInheritanceSupport support = new JMIInheritanceSupport(el); 100 101 String title = MessageFormat.format(getString("LBL_OverridePanel2_Title"), new Object [] { el.getName() } ); 103 OverridePanel2 panel = new OverridePanel2(support); 104 final DialogDescriptor desc = new DialogDescriptor(panel, title, true, DialogDescriptor.OK_CANCEL_OPTION, DialogDescriptor.OK_OPTION, null); 105 panel.addPropertyChangeListener(new PropertyChangeListener () { 106 public void propertyChange(PropertyChangeEvent evt) { 107 if (OverridePanel2.PROP_VALID.equals(evt.getPropertyName())) { 108 desc.setValid(((Boolean ) evt.getNewValue()).booleanValue()); 109 } 110 } 111 }); 112 HelpCtx ctx = new HelpCtx(OverrideAction.class.getName()); 113 desc.setHelpCtx(ctx); 114 115 Dialog dialog = DialogDisplayer.getDefault().createDialog(desc); 116 dialog.setVisible(true); 117 dialog.dispose(); 118 119 if (desc.getValue().equals(DialogDescriptor.OK_OPTION)) { 120 Map selMethods = panel.getSelection(); 121 Collection methods = support.sortMethods(selMethods); 122 123 Method first = null; 124 int index = 0; 125 126 MDRepository repository = JavaMetamodel.getDefaultRepository(); 127 boolean failed = true; 128 repository.beginTrans(true); 129 try { 130 if (!el.isValid()) { 131 el = getJavaClass(activatedNodes[0]); 132 support.setRootClass(el); 133 } 134 for (Iterator i = methods.iterator(); i.hasNext(); ) { 135 Method m = (Method) i.next(); 136 m = support.overrideMethod(m, panel.isGenerateSuperCalls(), panel.isGenerateJavadoc()); 137 if (first == null) { 138 first = m; 139 index = el.getFeatures().indexOf(m); 140 } 141 } 142 failed = false; 143 } finally { 144 repository.endTrans(failed); 145 } 146 147 if (first != null) { 148 PositionBounds bounds = null; 149 repository.beginTrans(false); 150 try { 151 if (!first.isValid()) { 152 if (!el.isValid()) 153 el = getJavaClass(activatedNodes[0]); 154 List list = el.getFeatures(); 155 if (list.size() > index) { 156 Iterator iter = list.iterator(); 157 Object obj = null; 158 for (int x = 0; x <= index; x++) 159 obj = iter.next(); 160 if (obj instanceof Method) { 162 first = (Method)obj; 163 } 164 } 165 } 166 if (first.isValid()) { 167 bounds = JavaMetamodel.getManager().getElementPosition(first); 168 } 169 } finally { 170 repository.endTrans(); 171 } 172 if (bounds != null) { 173 PositionRef beginPos = bounds.getBegin(); 174 CloneableEditorSupport editSupp = beginPos.getCloneableEditorSupport(); 175 JEditorPane [] panes = editSupp.getOpenedPanes(); 176 if (panes==null) { 177 editSupp.open(); 178 } 179 } 180 } 181 } 182 } 183 }; 184 JavaMetamodel.getManager().invokeAfterScanFinished(run, getString("LBL_OverridePanel2_Title")); 185 } 186 187 protected boolean enable(Node[] activatedNodes) { 188 if (activatedNodes.length != 1) { 189 return false; 190 } 191 192 Element elem = getElement(activatedNodes[0]); 193 return elem != null; 194 } 195 196 private Element getElement(Node n) { 197 Element el = (Element) n.getLookup().lookup(Element.class); 198 if (el == null) { 199 JavaDataObject jdo = (JavaDataObject) n.getCookie(JavaDataObject.class); 200 if (jdo !=null) 201 el = JavaMetamodel.getManager().getResource(jdo.getPrimaryFile()); 202 if (el==null) 203 return null; 204 } 205 206 if (!((el instanceof Resource) || (el instanceof ClassMember) || (el instanceof ClassDefinition))) { 207 el = null; 208 } 209 return el; 210 } 211 212 private ClassDefinition getJavaClass(Node n) { 213 MDRepository repository = JavaMetamodel.getDefaultRepository(); 215 repository.beginTrans(true); 216 repository.endTrans(); 217 218 int caretPosition = -1; 219 EditorCookie ec = (EditorCookie) n.getCookie(EditorCookie.class); 220 if (ec != null) { 221 TopComponent activetc = TopComponent.getRegistry().getActivated(); 222 if (activetc instanceof CloneableEditorSupport.Pane) { 223 JEditorPane pane = ((CloneableEditorSupport.Pane)activetc).getEditorPane(); 224 JTextComponent textComponent = delegateAction.getTextComponent(); 225 if (textComponent == null) { 226 return null; 227 } 228 229 if (textComponent.equals(pane)) { 230 caretPosition = textComponent.getCaretPosition(); 231 } 232 } 233 } 234 Element el = null; 235 if (caretPosition != -1) { 236 DataObject dobj = (DataObject)n.getCookie(DataObject.class); 237 while (dobj instanceof DataShadow) { 238 dobj = ((DataShadow) dobj).getOriginal(); 239 } 240 el = JavaMetamodel.getManager().getElementByOffset(dobj.getPrimaryFile(), caretPosition); 241 } else { 242 el = getElement(n); 243 } 244 245 if (el==null) 246 return null; 247 248 while (!(el instanceof ClassDefinition) && !(el instanceof Resource)) { 249 el = (Element)el.refImmediateComposite(); 250 } 251 252 if (el instanceof Resource) { 253 for (Iterator iter = ((Resource) el).getClassifiers().iterator(); iter.hasNext(); ) { 254 Object obj = iter.next(); 255 if (obj instanceof JavaClass) { 256 return (JavaClass) obj; 257 } 258 } 259 return null; 260 } 261 return (ClassDefinition) el; 262 } 263 264 protected boolean asynchronous() { 265 return false; 266 } 267 268 private static final class DelegateTextAction extends TextAction { 270 271 public DelegateTextAction() { 272 super(""); 273 } 274 275 public void actionPerformed(ActionEvent a) { 276 } 277 278 public JTextComponent getTextComponent() { 279 return getFocusedComponent(); 280 } 281 }; 283 } 284 | Popular Tags |