]> git.uio.no Git - ifi-stolz-refaktor.git/blame - case-study/jdt-before/ui/org/eclipse/jdt/internal/ui/javaeditor/SpecificContentAssistAction.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-before / ui / org / eclipse / jdt / internal / ui / javaeditor / SpecificContentAssistAction.java
CommitLineData
1b2798f6
EK
1/*******************************************************************************
2 * Copyright (c) 2005, 2012 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 * Paul Fullbright <paul.fullbright@oracle.com> - content assist category enablement - http://bugs.eclipse.org/345213
11 *******************************************************************************/
12package org.eclipse.jdt.internal.ui.javaeditor;
13
14import org.eclipse.core.runtime.Assert;
15
16import org.eclipse.jface.action.Action;
17import org.eclipse.jface.viewers.ISelection;
18
19import org.eclipse.jface.text.BadLocationException;
20import org.eclipse.jface.text.IDocument;
21import org.eclipse.jface.text.ITextOperationTarget;
22import org.eclipse.jface.text.ITextSelection;
23import org.eclipse.jface.text.TextUtilities;
24import org.eclipse.jface.text.source.ISourceViewer;
25
26import org.eclipse.ui.IEditorPart;
27
28import org.eclipse.ui.texteditor.IDocumentProvider;
29import org.eclipse.ui.texteditor.ITextEditor;
30import org.eclipse.ui.texteditor.IUpdate;
31
32import org.eclipse.jdt.core.IJavaProject;
33
34import org.eclipse.jdt.ui.text.IJavaPartitions;
35
36import org.eclipse.jdt.internal.ui.text.java.CompletionProposalCategory;
37import org.eclipse.jdt.internal.ui.text.java.CompletionProposalComputerRegistry;
38
39
40/**
41 * Action to run content assist on a specific proposal category.
42 *
43 * @since 3.2
44 */
45final class SpecificContentAssistAction extends Action implements IUpdate {
46 /**
47 * The category represented by this action.
48 */
49 private final CompletionProposalCategory fCategory;
50 /**
51 * The content assist executor.
52 */
53 private final SpecificContentAssistExecutor fExecutor= new SpecificContentAssistExecutor(CompletionProposalComputerRegistry.getDefault());
54 /**
55 * The editor.
56 */
57 private JavaEditor fEditor;
58
59 /**
60 * Creates a new action for a certain proposal category.
61 *
62 * @param category the completion proposal category
63 */
64 public SpecificContentAssistAction(CompletionProposalCategory category) {
65 fCategory= category;
66 setText(category.getName());
67 setImageDescriptor(category.getImageDescriptor());
68 setActionDefinitionId("org.eclipse.jdt.ui.specific_content_assist.command"); //$NON-NLS-1$
69 }
70
71 /*
72 * @see org.eclipse.jface.action.Action#run()
73 */
74 @Override
75 public void run() {
76 ITextEditor editor= getActiveEditor();
77 if (editor == null)
78 return;
79
80 fExecutor.invokeContentAssist(editor, fCategory.getId());
81
82 return;
83 }
84
85 private ITextEditor getActiveEditor() {
86 return fEditor;
87 }
88
89 /**
90 * Sets the active editor part.
91 *
92 * @param part the editor, possibly <code>null</code>
93 */
94 public void setActiveEditor(IEditorPart part) {
95 JavaEditor editor;
96 if (part instanceof JavaEditor)
97 editor= (JavaEditor) part;
98 else
99 editor= null;
100 fEditor= editor;
101 setEnabled(computeEnablement(fEditor));
102 }
103
104 private boolean computeEnablement(ITextEditor editor) {
105 if (editor == null)
106 return false;
107
108 ITextOperationTarget target= (ITextOperationTarget) editor.getAdapter(ITextOperationTarget.class);
109 if (target == null || ! target.canDoOperation(ISourceViewer.CONTENTASSIST_PROPOSALS))
110 return false;
111
112 IJavaProject javaProject = EditorUtility.getJavaProject(editor.getEditorInput());
113 if (! fCategory.matches(javaProject))
114 return false;
115
116 ISelection selection= editor.getSelectionProvider().getSelection();
117 return isValidSelection(selection);
118 }
119
120 /**
121 * Computes the partition type at the selection start and checks whether the proposal category
122 * has any computers for this partition.
123 *
124 * @param selection the selection
125 * @return <code>true</code> if there are any computers for the selection
126 */
127 private boolean isValidSelection(ISelection selection) {
128 if (!(selection instanceof ITextSelection))
129 return false;
130 int offset= ((ITextSelection) selection).getOffset();
131
132 IDocument document= getDocument();
133 if (document == null)
134 return false;
135
136 String contentType;
137 try {
138 contentType= TextUtilities.getContentType(document, IJavaPartitions.JAVA_PARTITIONING, offset, true);
139 } catch (BadLocationException x) {
140 return false;
141 }
142
143 return fCategory.hasComputers(contentType);
144 }
145
146 private IDocument getDocument() {
147 Assert.isTrue(fEditor != null);
148 IDocumentProvider provider= fEditor.getDocumentProvider();
149 if (provider == null)
150 return null;
151
152 IDocument document= provider.getDocument(fEditor.getEditorInput());
153 return document;
154 }
155
156 /*
157 * @see org.eclipse.ui.texteditor.IUpdate#update()
158 */
159 public void update() {
160 setEnabled(computeEnablement(fEditor));
161 }
162}