1 11 package org.eclipse.jdt.internal.corext.fix; 12 13 import java.util.ArrayList ; 14 import java.util.List ; 15 16 import org.eclipse.text.edits.MultiTextEdit; 17 import org.eclipse.text.edits.ReplaceEdit; 18 import org.eclipse.text.edits.TextEdit; 19 20 import org.eclipse.core.runtime.CoreException; 21 22 import org.eclipse.swt.graphics.Image; 23 24 import org.eclipse.jface.resource.ImageDescriptor; 25 26 import org.eclipse.jface.text.link.LinkedModeModel; 27 import org.eclipse.jface.text.link.LinkedPosition; 28 29 import org.eclipse.jdt.core.ICompilationUnit; 30 import org.eclipse.jdt.core.dom.ITypeBinding; 31 import org.eclipse.jdt.core.dom.rewrite.ITrackedNodePosition; 32 import org.eclipse.jdt.core.dom.rewrite.ImportRewrite; 33 34 import org.eclipse.jdt.internal.corext.codemanipulation.StubUtility; 35 36 import org.eclipse.jdt.ui.JavaElementLabels; 37 38 import org.eclipse.jdt.internal.ui.JavaPlugin; 39 import org.eclipse.jdt.internal.ui.viewsupport.BindingLabelProvider; 40 41 public class LinkedProposalPositionGroup { 42 43 49 public static abstract class PositionInformation { 50 public abstract int getOffset(); 51 public abstract int getLength(); 52 public abstract int getSequenceRank(); 53 } 54 55 public static class Proposal { 56 57 private String fDisplayString; 58 private Image fImage; 59 private int fRelevance; 60 61 public Proposal(String displayString, Image image, int relevance) { 62 fDisplayString= displayString; 63 fImage= image; 64 fRelevance= relevance; 65 } 66 67 public String getDisplayString() { 68 return fDisplayString; 69 } 70 71 public Image getImage() { 72 return fImage; 73 } 74 75 public int getRelevance() { 76 return fRelevance; 77 } 78 79 public void setImage(Image image) { 80 fImage= image; 81 } 82 83 public String getAdditionalProposalInfo() { 84 return null; 85 } 86 87 public TextEdit computeEdits(int offset, LinkedPosition position, char trigger, int stateMask, LinkedModeModel model) throws CoreException { 88 return new ReplaceEdit(position.getOffset(), position.getLength(), fDisplayString); 89 } 90 } 91 92 public static PositionInformation createPositionInformation(ITrackedNodePosition pos, boolean isFirst) { 93 return new TrackedNodePosition(pos, isFirst); 94 } 95 96 private static class TrackedNodePosition extends PositionInformation { 97 98 private final ITrackedNodePosition fPos; 99 private final boolean fIsFirst; 100 101 public TrackedNodePosition(ITrackedNodePosition pos, boolean isFirst) { 102 fPos= pos; 103 fIsFirst= isFirst; 104 } 105 106 public int getOffset() { 107 return fPos.getStartPosition(); 108 } 109 110 public int getLength() { 111 return fPos.getLength(); 112 } 113 114 public int getSequenceRank() { 115 return fIsFirst ? 0 : 1; 116 } 117 } 118 119 private static final class JavaLinkedModeProposal extends Proposal { 120 private final ITypeBinding fTypeProposal; 121 private final ICompilationUnit fCompilationUnit; 122 123 public JavaLinkedModeProposal(ICompilationUnit unit, ITypeBinding typeProposal, int relevance) { 124 super(BindingLabelProvider.getBindingLabel(typeProposal, JavaElementLabels.ALL_DEFAULT | JavaElementLabels.ALL_POST_QUALIFIED), null, relevance); 125 fTypeProposal= typeProposal; 126 fCompilationUnit= unit; 127 ImageDescriptor desc= BindingLabelProvider.getBindingImageDescriptor(fTypeProposal, BindingLabelProvider.DEFAULT_IMAGEFLAGS); 128 if (desc != null) { 129 setImage(JavaPlugin.getImageDescriptorRegistry().get(desc)); 130 } 131 } 132 133 136 public TextEdit computeEdits(int offset, LinkedPosition position, char trigger, int stateMask, LinkedModeModel model) throws CoreException { 137 ImportRewrite impRewrite= StubUtility.createImportRewrite(fCompilationUnit, true); 138 String replaceString= impRewrite.addImport(fTypeProposal); 139 140 MultiTextEdit composedEdit= new MultiTextEdit(); 141 composedEdit.addChild(new ReplaceEdit(position.getOffset(), position.getLength(), replaceString)); 142 composedEdit.addChild(impRewrite.rewriteImports(null)); 143 return composedEdit; 144 } 145 } 146 147 148 private final String fGroupId; 149 private final List fPositions; 150 private final List fProposals; 151 152 153 public LinkedProposalPositionGroup(String groupID) { 154 fGroupId= groupID; 155 fPositions= new ArrayList (); 156 fProposals= new ArrayList (); 157 } 158 159 public void addPosition(PositionInformation position) { 160 fPositions.add(position); 161 } 162 163 public void addProposal(Proposal proposal) { 164 fProposals.add(proposal); 165 } 166 167 168 public void addPosition(ITrackedNodePosition position, boolean isFirst) { 169 addPosition(createPositionInformation(position, isFirst)); 170 } 171 172 public void addProposal(String displayString, Image image, int relevance) { 173 addProposal(new Proposal(displayString, image, relevance)); 174 } 175 176 public void addProposal(ITypeBinding type, ICompilationUnit cu, int relevance) { 177 addProposal(new JavaLinkedModeProposal(cu, type, relevance)); 178 } 179 180 public String getGroupId() { 181 return fGroupId; 182 } 183 184 public PositionInformation[] getPositions() { 185 return (PositionInformation[])fPositions.toArray(new PositionInformation[fPositions.size()]); 186 } 187 188 public Proposal[] getProposals() { 189 return (Proposal[])fProposals.toArray(new Proposal[fProposals.size()]); 190 } 191 192 } 193 | Popular Tags |