1 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 41 public final class FilledArgumentNamesMethodProposal extends JavaMethodCompletionProposal { 42 43 private IRegion fSelectedRegion; private int[] fArgumentOffsets; 45 private int[] fArgumentLengths; 46 47 public FilledArgumentNamesMethodProposal(CompletionProposal proposal, JavaContentAssistInvocationContext context) { 48 super(proposal, context); 49 } 50 51 54 public void apply(IDocument document, char trigger, int offset) { 55 super.apply(document, trigger, offset); 56 int baseOffset= getReplacementOffset(); 57 String 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 95 protected boolean needsLinkedMode() { 96 return false; } 98 99 102 protected String 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 buffer= new StringBuffer (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 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 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.FilledArgumentNamesMethodProposal_error_msg, e.getMessage()); 172 } 173 174 } 175 | Popular Tags |