KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > text > java > ExperimentalProposal


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.text.java;
12
13 import org.eclipse.swt.graphics.Point;
14 import org.eclipse.swt.widgets.Shell;
15
16 import org.eclipse.jface.dialogs.MessageDialog;
17
18 import org.eclipse.jface.text.BadLocationException;
19 import org.eclipse.jface.text.IDocument;
20 import org.eclipse.jface.text.IRegion;
21 import org.eclipse.jface.text.Region;
22 import org.eclipse.jface.text.link.LinkedModeModel;
23 import org.eclipse.jface.text.link.LinkedModeUI;
24 import org.eclipse.jface.text.link.LinkedPosition;
25 import org.eclipse.jface.text.link.LinkedPositionGroup;
26
27 import org.eclipse.ui.IEditorPart;
28 import org.eclipse.ui.texteditor.link.EditorLinkedModeUI;
29
30 import org.eclipse.jdt.core.CompletionProposal;
31
32 import org.eclipse.jdt.ui.text.java.JavaContentAssistInvocationContext;
33
34 import org.eclipse.jdt.internal.ui.JavaPlugin;
35 import org.eclipse.jdt.internal.ui.javaeditor.EditorHighlightingSynchronizer;
36 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
37
38 /**
39  * An experimental proposal.
40  */

41 public final class ExperimentalProposal extends JavaMethodCompletionProposal {
42
43     private IRegion fSelectedRegion; // initialized by apply()
44
private int[] fArgumentOffsets;
45     private int[] fArgumentLengths;
46
47     public ExperimentalProposal(CompletionProposal proposal, JavaContentAssistInvocationContext context) {
48         super(proposal, context);
49     }
50
51     /*
52      * @see ICompletionProposalExtension#apply(IDocument, char)
53      */

54     public void apply(IDocument document, char trigger, int offset) {
55         super.apply(document, trigger, offset);
56         int baseOffset= getReplacementOffset();
57         String JavaDoc replacement= getReplacementString();
58
59         if (fArgumentOffsets != null && getTextViewer() != null) {
60             try {
61                 LinkedModeModel model= new LinkedModeModel();
62                 for (int i= 0; i != fArgumentOffsets.length; i++) {
63                     LinkedPositionGroup group= new LinkedPositionGroup();
64                     group.addPosition(new LinkedPosition(document, baseOffset + fArgumentOffsets[i], fArgumentLengths[i], LinkedPositionGroup.NO_STOP));
65                     model.addGroup(group);
66                 }
67
68                 model.forceInstall();
69                 JavaEditor editor= getJavaEditor();
70                 if (editor != null) {
71                     model.addLinkingListener(new EditorHighlightingSynchronizer(editor));
72                 }
73
74                 LinkedModeUI ui= new EditorLinkedModeUI(model, getTextViewer());
75                 ui.setExitPosition(getTextViewer(), baseOffset + replacement.length(), 0, Integer.MAX_VALUE);
76                 ui.setExitPolicy(new ExitPolicy(')', document));
77                 ui.setDoContextInfo(true);
78                 ui.setCyclingMode(LinkedModeUI.CYCLE_WHEN_NO_PARENT);
79                 ui.enter();
80
81                 fSelectedRegion= ui.getSelectedRegion();
82
83             } catch (BadLocationException e) {
84                 JavaPlugin.log(e);
85                 openErrorDialog(e);
86             }
87         } else {
88             fSelectedRegion= new Region(baseOffset + replacement.length(), 0);
89         }
90     }
91     
92     /*
93      * @see org.eclipse.jdt.internal.ui.text.java.JavaMethodCompletionProposal#needsLinkedMode()
94      */

95     protected boolean needsLinkedMode() {
96         return false; // we handle it ourselves
97
}
98     
99     /*
100      * @see org.eclipse.jdt.internal.ui.text.java.LazyJavaCompletionProposal#computeReplacementString()
101      */

102     protected String JavaDoc computeReplacementString() {
103         
104         if (!hasParameters() || !hasArgumentList())
105             return super.computeReplacementString();
106
107         char[][] parameterNames= fProposal.findParameterNames(null);
108         int count= parameterNames.length;
109         fArgumentOffsets= new int[count];
110         fArgumentLengths= new int[count];
111         StringBuffer JavaDoc buffer= new StringBuffer JavaDoc(String.valueOf(fProposal.getName()));
112         
113         FormatterPrefs prefs= getFormatterPrefs();
114         if (prefs.beforeOpeningParen)
115             buffer.append(SPACE);
116         buffer.append(LPAREN);
117         
118         setCursorPosition(buffer.length());
119         
120         if (prefs.afterOpeningParen)
121             buffer.append(SPACE);
122         
123         for (int i= 0; i != count; i++) {
124             if (i != 0) {
125                 if (prefs.beforeComma)
126                     buffer.append(SPACE);
127                 buffer.append(COMMA);
128                 if (prefs.afterComma)
129                     buffer.append(SPACE);
130             }
131             
132             fArgumentOffsets[i]= buffer.length();
133             buffer.append(parameterNames[i]);
134             fArgumentLengths[i]= parameterNames[i].length;
135         }
136         
137         if (prefs.beforeClosingParen)
138             buffer.append(SPACE);
139
140         buffer.append(RPAREN);
141
142         return buffer.toString();
143     }
144
145     /**
146      * Returns the currently active java editor, or <code>null</code> if it
147      * cannot be determined.
148      *
149      * @return the currently active java editor, or <code>null</code>
150      */

151     private JavaEditor getJavaEditor() {
152         IEditorPart part= JavaPlugin.getActivePage().getActiveEditor();
153         if (part instanceof JavaEditor)
154             return (JavaEditor) part;
155         else
156             return null;
157     }
158
159     /*
160      * @see ICompletionProposal#getSelection(IDocument)
161      */

162     public Point getSelection(IDocument document) {
163         if (fSelectedRegion == null)
164             return new Point(getReplacementOffset(), 0);
165
166         return new Point(fSelectedRegion.getOffset(), fSelectedRegion.getLength());
167     }
168
169     private void openErrorDialog(BadLocationException e) {
170         Shell shell= getTextViewer().getTextWidget().getShell();
171         MessageDialog.openError(shell, JavaTextMessages.ExperimentalProposal_error_msg, e.getMessage());
172     }
173
174 }
175
Popular Tags