/projects/eclipse_SDK-3.7.1/plugins/org.eclipse.jface.text.source_3.7.1.r371_v20110825-0800/org/eclipse/jface/text/templates/PositionBasedCompletionProposal.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus · Java · 169 lines · 74 code · 22 blank · 73 comment · 4 complexity · b8329394ebae3feee6d8608f1e1c8977 MD5 · raw file

  1. /*******************************************************************************
  2. * Copyright (c) 2000, 2008 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.jface.text.templates;
  12. import org.eclipse.swt.graphics.Image;
  13. import org.eclipse.swt.graphics.Point;
  14. import org.eclipse.core.runtime.Assert;
  15. import org.eclipse.jface.text.BadLocationException;
  16. import org.eclipse.jface.text.DocumentEvent;
  17. import org.eclipse.jface.text.IDocument;
  18. import org.eclipse.jface.text.ITextViewer;
  19. import org.eclipse.jface.text.Position;
  20. import org.eclipse.jface.text.contentassist.ICompletionProposal;
  21. import org.eclipse.jface.text.contentassist.ICompletionProposalExtension2;
  22. import org.eclipse.jface.text.contentassist.IContextInformation;
  23. /**
  24. * A position based completion proposal.
  25. *
  26. * @since 3.0
  27. */
  28. final class PositionBasedCompletionProposal implements ICompletionProposal, ICompletionProposalExtension2 {
  29. /** The string to be displayed in the completion proposal popup */
  30. private String fDisplayString;
  31. /** The replacement string */
  32. private String fReplacementString;
  33. /** The replacement position. */
  34. private Position fReplacementPosition;
  35. /** The cursor position after this proposal has been applied */
  36. private int fCursorPosition;
  37. /** The image to be displayed in the completion proposal popup */
  38. private Image fImage;
  39. /** The context information of this proposal */
  40. private IContextInformation fContextInformation;
  41. /** The additional info of this proposal */
  42. private String fAdditionalProposalInfo;
  43. /**
  44. * Creates a new completion proposal based on the provided information. The replacement string is
  45. * considered being the display string too. All remaining fields are set to <code>null</code>.
  46. *
  47. * @param replacementString the actual string to be inserted into the document
  48. * @param replacementPosition the position of the text to be replaced
  49. * @param cursorPosition the position of the cursor following the insert relative to replacementOffset
  50. */
  51. public PositionBasedCompletionProposal(String replacementString, Position replacementPosition, int cursorPosition) {
  52. this(replacementString, replacementPosition, cursorPosition, null, null, null, null);
  53. }
  54. /**
  55. * Creates a new completion proposal. All fields are initialized based on the provided information.
  56. *
  57. * @param replacementString the actual string to be inserted into the document
  58. * @param replacementPosition the position of the text to be replaced
  59. * @param cursorPosition the position of the cursor following the insert relative to replacementOffset
  60. * @param image the image to display for this proposal
  61. * @param displayString the string to be displayed for the proposal
  62. * @param contextInformation the context information associated with this proposal
  63. * @param additionalProposalInfo the additional information associated with this proposal
  64. */
  65. public PositionBasedCompletionProposal(String replacementString, Position replacementPosition, int cursorPosition, Image image, String displayString, IContextInformation contextInformation, String additionalProposalInfo) {
  66. Assert.isNotNull(replacementString);
  67. Assert.isTrue(replacementPosition != null);
  68. fReplacementString= replacementString;
  69. fReplacementPosition= replacementPosition;
  70. fCursorPosition= cursorPosition;
  71. fImage= image;
  72. fDisplayString= displayString;
  73. fContextInformation= contextInformation;
  74. fAdditionalProposalInfo= additionalProposalInfo;
  75. }
  76. /*
  77. * @see ICompletionProposal#apply(IDocument)
  78. */
  79. public void apply(IDocument document) {
  80. try {
  81. document.replace(fReplacementPosition.getOffset(), fReplacementPosition.getLength(), fReplacementString);
  82. } catch (BadLocationException x) {
  83. // ignore
  84. }
  85. }
  86. /*
  87. * @see ICompletionProposal#getSelection(IDocument)
  88. */
  89. public Point getSelection(IDocument document) {
  90. return new Point(fReplacementPosition.getOffset() + fCursorPosition, 0);
  91. }
  92. /*
  93. * @see ICompletionProposal#getContextInformation()
  94. */
  95. public IContextInformation getContextInformation() {
  96. return fContextInformation;
  97. }
  98. /*
  99. * @see ICompletionProposal#getImage()
  100. */
  101. public Image getImage() {
  102. return fImage;
  103. }
  104. /*
  105. * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getDisplayString()
  106. */
  107. public String getDisplayString() {
  108. if (fDisplayString != null)
  109. return fDisplayString;
  110. return fReplacementString;
  111. }
  112. /*
  113. * @see ICompletionProposal#getAdditionalProposalInfo()
  114. */
  115. public String getAdditionalProposalInfo() {
  116. return fAdditionalProposalInfo;
  117. }
  118. /*
  119. * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#apply(org.eclipse.jface.text.ITextViewer, char, int, int)
  120. */
  121. public void apply(ITextViewer viewer, char trigger, int stateMask, int offset) {
  122. apply(viewer.getDocument());
  123. }
  124. /*
  125. * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#selected(org.eclipse.jface.text.ITextViewer, boolean)
  126. */
  127. public void selected(ITextViewer viewer, boolean smartToggle) {
  128. }
  129. /*
  130. * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#unselected(org.eclipse.jface.text.ITextViewer)
  131. */
  132. public void unselected(ITextViewer viewer) {
  133. }
  134. /*
  135. * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#validate(org.eclipse.jface.text.IDocument, int, org.eclipse.jface.text.DocumentEvent)
  136. */
  137. public boolean validate(IDocument document, int offset, DocumentEvent event) {
  138. try {
  139. String content= document.get(fReplacementPosition.getOffset(), offset - fReplacementPosition.getOffset());
  140. if (fReplacementString.startsWith(content))
  141. return true;
  142. } catch (BadLocationException e) {
  143. // ignore concurrently modified document
  144. }
  145. return false;
  146. }
  147. }