• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Eclipse Public License, Version 1.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.eclipse.org/org/documents/epl-v10.php
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.ide.eclipse.adt.internal.editors.layout.refactoring;
18 
19 import com.android.ide.eclipse.adt.internal.project.BaseProjectHelper;
20 import com.android.ide.eclipse.adt.internal.refactorings.extractstring.ExtractStringProposal;
21 
22 import org.eclipse.core.resources.IProject;
23 import org.eclipse.core.runtime.CoreException;
24 import org.eclipse.jdt.core.dom.ASTNode;
25 import org.eclipse.jdt.ui.text.java.IInvocationContext;
26 import org.eclipse.jdt.ui.text.java.IJavaCompletionProposal;
27 import org.eclipse.jdt.ui.text.java.IProblemLocation;
28 
29 /**
30  * Quick Assistant for Java files in Android projects
31  */
32 public class JavaQuickAssistant implements org.eclipse.jdt.ui.text.java.IQuickAssistProcessor {
JavaQuickAssistant()33     public JavaQuickAssistant() {
34     }
35 
36     @Override
hasAssists(IInvocationContext context)37     public boolean hasAssists(IInvocationContext context) throws CoreException {
38         return true;
39     }
40 
41     @Override
getAssists(IInvocationContext context, IProblemLocation[] locations)42     public IJavaCompletionProposal[] getAssists(IInvocationContext context,
43             IProblemLocation[] locations) throws CoreException {
44         // We should only offer Android quick assists within Android projects.
45         // This can be done by adding this logic to the extension registration:
46         //
47         //    <enablement>
48         //        <with variable="projectNatures">
49         //            <iterate operator="or">
50         //                <equals value="com.android.ide.eclipse.adt.AndroidNature"/>
51         //            </iterate>
52         //        </with>
53         //    </enablement>
54         //
55         // However, this causes some errors to be dumped to the log, so instead we filter
56         // out non Android projects programmatically:
57 
58         IProject project = context.getCompilationUnit().getJavaProject().getProject();
59         if (project == null || !BaseProjectHelper.isAndroidProject(project)) {
60             return null;
61         }
62 
63         ASTNode coveringNode = context.getCoveringNode();
64         if (coveringNode != null && coveringNode.getNodeType() == ASTNode.STRING_LITERAL
65                 && coveringNode.getLength() > 2) { // don't extract empty strings (includes quotes)
66             return new IJavaCompletionProposal[] {
67                 new ExtractStringProposal(context)
68             };
69         }
70 
71         return null;
72     }
73 }
74