sandbox/old-modules/bridges/refactoring/src/org/netbeans/modules/jackpot30/refactoring/RefactoringActionsProviderExt.java
1.1 --- a/sandbox/old-modules/bridges/refactoring/src/org/netbeans/modules/jackpot30/refactoring/RefactoringActionsProviderExt.java Mon Dec 19 11:37:36 2016 +0100
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,238 +0,0 @@
1.4 -/*
1.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
1.6 - *
1.7 - * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
1.8 - *
1.9 - * The contents of this file are subject to the terms of either the GNU
1.10 - * General Public License Version 2 only ("GPL") or the Common
1.11 - * Development and Distribution License("CDDL") (collectively, the
1.12 - * "License"). You may not use this file except in compliance with the
1.13 - * License. You can obtain a copy of the License at
1.14 - * http://www.netbeans.org/cddl-gplv2.html
1.15 - * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
1.16 - * specific language governing permissions and limitations under the
1.17 - * License. When distributing the software, include this License Header
1.18 - * Notice in each file and include the License file at
1.19 - * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
1.20 - * particular file as subject to the "Classpath" exception as provided
1.21 - * by Sun in the GPL Version 2 section of the License file that
1.22 - * accompanied this code. If applicable, add the following below the
1.23 - * License Header, with the fields enclosed by brackets [] replaced by
1.24 - * your own identifying information:
1.25 - * "Portions Copyrighted [year] [name of copyright owner]"
1.26 - *
1.27 - * If you wish your version of this file to be governed by only the CDDL
1.28 - * or only the GPL Version 2, indicate your decision by adding
1.29 - * "[Contributor] elects to include this software in this distribution
1.30 - * under the [CDDL or GPL Version 2] license." If you do not indicate a
1.31 - * single choice of license, a recipient has the option to distribute
1.32 - * your version of this file under either the CDDL, the GPL Version 2 or
1.33 - * to extend the choice of license to its licensees as provided above.
1.34 - * However, if you add GPL Version 2 code and therefore, elected the GPL
1.35 - * Version 2 license, then the option applies only if the new code is
1.36 - * made subject to such option by the copyright holder.
1.37 - *
1.38 - * Contributor(s):
1.39 - *
1.40 - * Portions Copyrighted 2010 Sun Microsystems, Inc.
1.41 - */
1.42 -package org.netbeans.modules.jackpot30.refactoring;
1.43 -
1.44 -import java.util.Collection;
1.45 -import java.util.HashSet;
1.46 -import java.util.logging.Logger;
1.47 -import javax.lang.model.element.Element;
1.48 -import javax.lang.model.element.ElementKind;
1.49 -import javax.lang.model.element.ExecutableElement;
1.50 -import javax.lang.model.element.VariableElement;
1.51 -import javax.lang.model.type.TypeKind;
1.52 -import org.netbeans.api.java.source.CompilationInfo;
1.53 -import org.netbeans.api.java.source.TreePathHandle;
1.54 -import org.netbeans.api.java.source.ui.ScanDialog;
1.55 -import org.netbeans.modules.jackpot30.refactoring.invertboolean.InvertBooleanRefactoringUI;
1.56 -import org.netbeans.modules.jackpot30.refactoring.noconstructor.ReplaceConstructorRefactoringUI;
1.57 -import org.netbeans.modules.refactoring.api.RenameRefactoring;
1.58 -import org.netbeans.modules.refactoring.java.RefactoringUtils;
1.59 -import org.netbeans.modules.refactoring.java.ui.RefactoringActionsProvider;
1.60 -import org.netbeans.modules.refactoring.spi.ui.ActionsImplementationProvider;
1.61 -import org.netbeans.modules.refactoring.spi.ui.RefactoringUI;
1.62 -import org.openide.cookies.EditorCookie;
1.63 -import org.openide.filesystems.FileObject;
1.64 -import org.openide.loaders.DataObject;
1.65 -import org.openide.nodes.Node;
1.66 -import org.openide.text.CloneableEditorSupport;
1.67 -import org.openide.util.Lookup;
1.68 -import org.openide.util.lookup.ServiceProvider;
1.69 -import org.openide.windows.TopComponent;
1.70 -
1.71 -
1.72 -/**
1.73 - *
1.74 - * @author lahvac
1.75 - */
1.76 -@ServiceProvider(service=ActionsImplementationProvider.class, supersedes="org.netbeans.modules.refactoring.java.ui.RefactoringActionsProvider")
1.77 -public class RefactoringActionsProviderExt extends RefactoringActionsProvider {
1.78 -
1.79 - private static RefactoringUI doReplaceConstructorImpl(TreePathHandle selectedElement, CompilationInfo info) {
1.80 - Element selected = selectedElement.resolveElement(info);
1.81 - if (selected==null) {
1.82 -// logger().log(Level.INFO, "doRename: " + selectedElement, new NullPointerException("selected")); // NOI18N
1.83 - return null;
1.84 - }
1.85 - if (selected.getKind() == ElementKind.CONSTRUCTOR) {
1.86 - return new ReplaceConstructorRefactoringUI(selectedElement);
1.87 - }
1.88 -
1.89 - return null;
1.90 - }
1.91 -
1.92 - public static void doReplaceConstructor(final Lookup lookup) {
1.93 - Runnable task;
1.94 - EditorCookie ec = lookup.lookup(EditorCookie.class);
1.95 - if (isFromEditor(ec)) {
1.96 - task = new TextComponentTask(ec) {
1.97 - @Override
1.98 - protected RefactoringUI createRefactoringUI(TreePathHandle selectedElement,int startOffset,int endOffset, final CompilationInfo info) {
1.99 - return doReplaceConstructorImpl(selectedElement, info);
1.100 - }
1.101 - };
1.102 - } else {
1.103 - task = new TreePathHandleTask(new HashSet<Node>(lookup.lookupAll(Node.class)), true) {
1.104 -
1.105 - RefactoringUI ui;
1.106 -
1.107 - @Override
1.108 - protected void treePathHandleResolved(TreePathHandle handle, CompilationInfo javac) {
1.109 - ui = doReplaceConstructorImpl(handle, javac);
1.110 - }
1.111 -
1.112 - @Override
1.113 - protected RefactoringUI createRefactoringUI(Collection<TreePathHandle> handles) {
1.114 - return ui;
1.115 - }
1.116 -
1.117 - };
1.118 - }
1.119 -
1.120 - ScanDialog.runWhenScanFinished(task, "Remove Constructor");//getActionName(RefactoringActionsFactory.renameAction()));
1.121 - }
1.122 -
1.123 - private static RefactoringUI doInvertBooleanImpl(TreePathHandle selectedElement, CompilationInfo info) {
1.124 - Element selected = selectedElement.resolveElement(info);
1.125 - if (selected==null) {
1.126 -// logger().log(Level.INFO, "doRename: " + selectedElement, new NullPointerException("selected")); // NOI18N
1.127 - return null;
1.128 - }
1.129 - if (selected.getKind().isField() && ((VariableElement) selected).asType().getKind() == TypeKind.BOOLEAN) {
1.130 - return new InvertBooleanRefactoringUI(selectedElement, ((VariableElement) selected).getSimpleName().toString());
1.131 - }
1.132 - if (selected.getKind() == ElementKind.METHOD && ((ExecutableElement) selected).getReturnType().getKind() == TypeKind.BOOLEAN) {
1.133 - return new InvertBooleanRefactoringUI(selectedElement, ((ExecutableElement) selected).getSimpleName().toString());
1.134 - }
1.135 -
1.136 - return null;
1.137 - }
1.138 -
1.139 - public static void doInvertBoolean(final Lookup lookup) {
1.140 - Runnable task;
1.141 - EditorCookie ec = lookup.lookup(EditorCookie.class);
1.142 - if (isFromEditor(ec)) {
1.143 - task = new TextComponentTask(ec) {
1.144 - @Override
1.145 - protected RefactoringUI createRefactoringUI(TreePathHandle selectedElement,int startOffset,int endOffset, final CompilationInfo info) {
1.146 - return doInvertBooleanImpl(selectedElement, info);
1.147 - }
1.148 - };
1.149 - } else {
1.150 - task = new TreePathHandleTask(new HashSet<Node>(lookup.lookupAll(Node.class)), true) {
1.151 -
1.152 - RefactoringUI ui;
1.153 -
1.154 - @Override
1.155 - protected void treePathHandleResolved(TreePathHandle handle, CompilationInfo javac) {
1.156 - ui = doInvertBooleanImpl(handle, javac);
1.157 - }
1.158 -
1.159 - @Override
1.160 - protected RefactoringUI createRefactoringUI(Collection<TreePathHandle> handles) {
1.161 - return ui;
1.162 - }
1.163 -
1.164 - };
1.165 - }
1.166 -
1.167 - ScanDialog.runWhenScanFinished(task, "Remove Constructor");//getActionName(RefactoringActionsFactory.renameAction()));
1.168 - }
1.169 -
1.170 -// static String getActionName(Action action) {
1.171 -// String arg = (String) action.getValue(Action.NAME);
1.172 -// arg = arg.replace("&", ""); // NOI18N
1.173 -// return arg.replace("...", ""); // NOI18N
1.174 -// }
1.175 -
1.176 -
1.177 - public static boolean canReplaceConstructor(Lookup lookup) {
1.178 - return canRefactor(lookup);
1.179 - }
1.180 -
1.181 - public static boolean canInvertBoolean(Lookup lookup) {
1.182 - return canRefactor(lookup);
1.183 - }
1.184 -
1.185 - private static boolean canRefactor(Lookup lookup) {
1.186 - Collection<? extends Node> nodes = new HashSet<Node>(lookup.lookupAll(Node.class));
1.187 - if(nodes.size() != 1)
1.188 - return false;
1.189 - Node node = nodes.iterator().next();
1.190 - TreePathHandle tph = node.getLookup().lookup(TreePathHandle.class);
1.191 - if (tph != null) {
1.192 - return RefactoringUtils.isRefactorable(tph.getFileObject());
1.193 - }
1.194 - DataObject dObj = node.getCookie(DataObject.class);
1.195 - if(null == dObj)
1.196 - return false;
1.197 - FileObject fileObj = dObj.getPrimaryFile();
1.198 - if(null == fileObj || !RefactoringUtils.isRefactorable(fileObj))
1.199 - return false;
1.200 -
1.201 - EditorCookie ec = lookup.lookup(EditorCookie.class);
1.202 - if (isFromEditor(ec)) {
1.203 - return true;
1.204 - }
1.205 - return false;
1.206 - }
1.207 -
1.208 - @Override
1.209 - protected RefactoringUI wrap(RefactoringUI orig) {
1.210 - if (orig.getRefactoring() instanceof RenameRefactoring) {
1.211 - return new RenameRefactoringWrapper(orig, ((RenameRefactoring) orig.getRefactoring()));
1.212 - }
1.213 -
1.214 - return super.wrap(orig);
1.215 - }
1.216 -
1.217 -
1.218 - //XXX: copied from RefactoringActionsProvider:\
1.219 - static boolean isFromEditor(EditorCookie ec) {
1.220 - if (ec != null && ec.getOpenedPanes() != null) {
1.221 - TopComponent activetc = TopComponent.getRegistry().getActivated();
1.222 - if (activetc instanceof CloneableEditorSupport.Pane) {
1.223 - return true;
1.224 - }
1.225 - }
1.226 - return false;
1.227 - }
1.228 -
1.229 - static boolean nodeHandle(Lookup lookup) {
1.230 - Node n = lookup.lookup(Node.class);
1.231 - if (n!=null) {
1.232 - if (n.getLookup().lookup(TreePathHandle.class)!=null)
1.233 - return true;
1.234 - }
1.235 - return false;
1.236 - }
1.237 -
1.238 - private static Logger logger() {
1.239 - return Logger.getLogger(RefactoringActionsProvider.class.getName());
1.240 - }
1.241 -}