001: package org.drools.eclipse.editors.completion;
002:
003: import java.util.Comparator;
004:
005: import org.eclipse.jface.text.BadLocationException;
006: import org.eclipse.jface.text.IDocument;
007: import org.eclipse.jface.text.contentassist.ICompletionProposal;
008: import org.eclipse.jface.text.contentassist.IContextInformation;
009: import org.eclipse.swt.graphics.Image;
010: import org.eclipse.swt.graphics.Point;
011:
012: /**
013: * Holds a completion proposal to be popped up.
014: *
015: * @author Michael Neale
016: *
017: */
018: public class RuleCompletionProposal implements ICompletionProposal {
019:
020: private String content;
021: private String display;
022: private int replacementOffset;
023: private int replacementLength;
024: private int cursorPosition;
025: private Image image;
026: private int priority;
027:
028: /** This is used when the stuff that is displayed, is the stuff that is used. */
029: public RuleCompletionProposal(int replacementOffset,
030: int replacementLength, String content) {
031: this (replacementOffset, replacementLength, content, content);
032: }
033:
034: /** This is used when a different display value is shown to what is put in when selected. */
035: public RuleCompletionProposal(int replacementOffset,
036: int replacementLength, String display, String content) {
037: this (replacementOffset, replacementLength, display, content,
038: content.length());
039: }
040:
041: /** Also allows an icon to be used */
042: public RuleCompletionProposal(int replacementOffset,
043: int replacementLength, String display, String content,
044: Image image) {
045: this (replacementOffset, replacementLength, display, content,
046: content.length(), image);
047: }
048:
049: public RuleCompletionProposal(int replacementOffset,
050: int replacementLength, String display, String content,
051: int cursorPosition) {
052: this (replacementOffset, replacementLength, display, content,
053: cursorPosition, null);
054: }
055:
056: /** This is used when a different display value is shown to what is put in when selected. */
057: public RuleCompletionProposal(int replacementOffset,
058: int replacementLength, String display, String content,
059: int cursorPosition, Image image) {
060: this .replacementOffset = replacementOffset;
061: this .replacementLength = replacementLength;
062: this .content = content;
063: this .display = display;
064: this .cursorPosition = cursorPosition;
065: this .image = image;
066: }
067:
068: public String getDisplay() {
069: return display;
070: }
071:
072: public int getReplacementLength() {
073: return replacementLength;
074: }
075:
076: public int getCursorPosition() {
077: return cursorPosition;
078: }
079:
080: public Image getImage() {
081: return image;
082: }
083:
084: public String getContent() {
085: return content;
086: }
087:
088: public void setImage(Image image) {
089: this .image = image;
090: }
091:
092: public int getPriority() {
093: return priority;
094: }
095:
096: public void setPriority(int priority) {
097: this .priority = priority;
098: }
099:
100: public String toString() {
101: return content;
102: }
103:
104: //TODO:fixme now that we mix,JDT and own propsals, comparison is all wrong, resulting in wrong ordering of mixed proposals (such as with mvel
105: public static class RuleCompletionProposalComparator implements
106: Comparator {
107: public int compare(Object arg0, Object arg1) {
108: if (arg0 instanceof RuleCompletionProposal) {
109: if (arg1 instanceof RuleCompletionProposal) {
110: RuleCompletionProposal prop0 = (RuleCompletionProposal) arg0;
111: RuleCompletionProposal prop1 = (RuleCompletionProposal) arg1;
112: if (prop0.getPriority() == prop1.getPriority()) {
113: return prop0.display.compareTo(prop1.display);
114: } else if (prop0.getPriority() > prop1
115: .getPriority()) {
116: return -1;
117: } else {
118: return 1;
119: }
120: } else {
121: return -1;
122: }
123: } else {
124: if (arg1 instanceof RuleCompletionProposal) {
125: return 1;
126: }
127: return 0;
128: }
129: }
130: }
131:
132: public void apply(IDocument document) {
133: try {
134: document.replace(replacementOffset, replacementLength,
135: content);
136: } catch (BadLocationException x) {
137: // ignore
138: }
139: }
140:
141: public String getAdditionalProposalInfo() {
142: return null;
143: }
144:
145: public IContextInformation getContextInformation() {
146: return null;
147: }
148:
149: public String getDisplayString() {
150: if (display != null) {
151: return display;
152: }
153: return content;
154: }
155:
156: public int hashCode() {
157: final int PRIME = 31;
158: int result = 1;
159: result = PRIME * result
160: + ((content == null) ? 0 : content.hashCode());
161: result = PRIME * result
162: + ((display == null) ? 0 : display.hashCode());
163: return result;
164: }
165:
166: public boolean equals(Object obj) {
167: if (this == obj)
168: return true;
169: if (obj == null)
170: return false;
171: if (getClass() != obj.getClass())
172: return false;
173: final RuleCompletionProposal other = (RuleCompletionProposal) obj;
174: if (content == null) {
175: if (other.content != null)
176: return false;
177: } else if (!content.equals(other.content))
178: return false;
179: if (display == null) {
180: if (other.display != null)
181: return false;
182: } else if (!display.equals(other.display))
183: return false;
184: return true;
185: }
186:
187: public Point getSelection(IDocument document) {
188: return new Point(replacementOffset + cursorPosition, 0);
189: }
190: }
|