]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.ui/src/org/simantics/document/ui/CSSCompletionProposal.java
Externalize strings in org.simantics.document.ui
[simantics/platform.git] / bundles / org.simantics.document.ui / src / org / simantics / document / ui / CSSCompletionProposal.java
1 package org.simantics.document.ui;
2
3 import org.eclipse.core.runtime.IProgressMonitor;
4 import org.eclipse.jface.text.BadLocationException;
5 import org.eclipse.jface.text.DefaultInformationControl;
6 import org.eclipse.jface.text.DocumentEvent;
7 import org.eclipse.jface.text.IDocument;
8 import org.eclipse.jface.text.IInformationControl;
9 import org.eclipse.jface.text.IInformationControlCreator;
10 import org.eclipse.jface.text.ITextViewer;
11 import org.eclipse.jface.text.contentassist.ICompletionProposal;
12 import org.eclipse.jface.text.contentassist.ICompletionProposalExtension;
13 import org.eclipse.jface.text.contentassist.ICompletionProposalExtension2;
14 import org.eclipse.jface.text.contentassist.ICompletionProposalExtension3;
15 import org.eclipse.jface.text.contentassist.ICompletionProposalExtension4;
16 import org.eclipse.jface.text.contentassist.ICompletionProposalExtension5;
17 import org.eclipse.jface.text.contentassist.IContextInformation;
18 import org.eclipse.swt.graphics.Image;
19 import org.eclipse.swt.graphics.Point;
20 import org.eclipse.swt.widgets.Shell;
21 import org.simantics.scl.compiler.elaboration.modules.SCLValue;
22
23 public class CSSCompletionProposal implements ICompletionProposal, ICompletionProposalExtension, ICompletionProposalExtension2, ICompletionProposalExtension3, ICompletionProposalExtension4, ICompletionProposalExtension5 {
24     
25     private static final Image PRIVATE = Activator.imageDescriptorFromPlugin("org.simantics.scl.ui", "icons/private_co.gif").createImage(); //$NON-NLS-1$ //$NON-NLS-2$
26     private static final Image PUBLIC = Activator.imageDescriptorFromPlugin("org.simantics.scl.ui", "icons/public_co.gif").createImage(); //$NON-NLS-1$ //$NON-NLS-2$
27     private static final Image CONST = Activator.imageDescriptorFromPlugin("org.simantics.scl.ui", "icons/container_obj.gif").createImage(); //$NON-NLS-1$ //$NON-NLS-2$
28     private static final Image TYPE = Activator.imageDescriptorFromPlugin("org.simantics.scl.ui", "icons/typedef_obj.gif").createImage(); //$NON-NLS-1$ //$NON-NLS-2$
29     
30     private final String content;
31     private final String name;
32     private final String module;
33     private final String documentation;
34     private int replacementOffset;
35     private String prefix;
36     private final CSSCompletionType completionType;
37     
38     public CSSCompletionProposal(SCLValue value, int replacementOffset, String prefix) {
39         this.name = value.getName().name;
40         this.module = value.getName().module;
41         this.documentation = value.getDocumentation();
42         this.content = name + " :: " + value.getType() + "  (" + module + ")"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
43         this.replacementOffset = replacementOffset;
44         this.prefix = prefix;
45 //        System.out.println(prefix);
46         if (value.isPrivate())
47             this.completionType = CSSCompletionType.PRIVATE;
48         else
49             this.completionType = CSSCompletionType.PUBLIC;
50     }
51
52     public CSSCompletionProposal(String name, String module, CSSCompletionType completionType, int replacementOffset, String prefix) {
53         this.name = name;
54         this.module = module;
55         this.content = name + " (" + module + ")"; //$NON-NLS-1$ //$NON-NLS-2$
56         this.documentation = null;
57         this.replacementOffset = replacementOffset;
58         this.prefix = prefix;
59         this.completionType = completionType;
60     }
61
62     @Override
63     public void apply(IDocument document) {
64
65     }
66
67     @Override
68     public Point getSelection(IDocument document) {
69         return new Point(replacementOffset + getName().length(), 0);
70     }
71
72     @Override
73     public String getAdditionalProposalInfo() {
74         return documentation;
75     }
76
77     @Override
78     public String getDisplayString() {
79         return content;
80     }
81
82     @Override
83     public Image getImage() {
84         switch (completionType) {
85         case PRIVATE:
86             return PRIVATE;
87         case PUBLIC:
88             return PUBLIC;
89         case CONST:
90             return CONST;
91         case TYPE:
92             return TYPE;
93         default:
94             return null;
95         }
96     }
97
98     @Override
99     public IContextInformation getContextInformation() {
100         return null;
101     }
102
103     @Override
104     public void apply(IDocument document, char trigger, int offset) {
105     }
106
107     @Override
108     public boolean isValidFor(IDocument document, int offset) {
109         return validate(document, offset, null);
110     }
111
112     @Override
113     public char[] getTriggerCharacters() {
114         return null;
115     }
116
117     @Override
118     public int getContextInformationPosition() {
119         return 0;
120     }
121
122     @Override
123     public void apply(ITextViewer viewer, char trigger, int stateMask, int offset) {
124         try {
125 //            String replacement = getName().substring(offset - replacementOffset);
126             IDocument doc = viewer.getDocument();
127             Point p = viewer.getSelectedRange();
128 //            System.out.println("selectedRange : " + p);
129 //            System.out.println("prefix : " + prefix);
130 //            int start = offset - prefix.length();
131 //            int end = prefix.length();
132 //            System.out.println("start : " + start + ", end : " + end);
133 //            String sadd = doc.get(start, end);
134 //            System.out.println("toReplace : " + sadd);
135             if (p.y > 0) {
136                 doc.replace(p.x, p.y, ""); //$NON-NLS-1$
137                 doc.replace(offset, 0, getName());
138             } else {
139                 String currentText = doc.get(offset - prefix.length(), prefix.length());
140                 if (currentText.equals(getName()))
141                     return;
142                 doc.replace(offset - prefix.length(), prefix.length(), ""); //$NON-NLS-1$
143                 doc.replace(offset - prefix.length(), 0, getName());
144             }
145         } catch (BadLocationException x) {
146             x.printStackTrace();
147         }
148     }
149
150     @Override
151     public void selected(ITextViewer viewer, boolean smartToggle) {
152     }
153
154     @Override
155     public void unselected(ITextViewer viewer) {
156     }
157
158     @Override
159     public boolean validate(IDocument document, int offset, DocumentEvent event) {
160         try {
161 //            System.out.println("replacementOffset : " + replacementOffset);
162 //            System.out.println("offset : " + offset);
163             boolean a = offset >= replacementOffset;
164             boolean b = offset < replacementOffset + getName().length();
165             String s = document.get(replacementOffset, offset - replacementOffset);
166             prefix = s;
167             String d = getName();//.substring(0, offset - prefixStart);
168             boolean c = d.toLowerCase().startsWith(s.toLowerCase());
169             return a && b && c;
170         } catch (BadLocationException x) {
171             //x.printStackTrace();
172             return false;
173         }
174     }
175
176     @Override
177     public IInformationControlCreator getInformationControlCreator() {
178         return CREATOR;
179     }
180     
181     private static final IInformationControlCreator CREATOR = new IInformationControlCreator() {
182         
183         @Override
184         public IInformationControl createInformationControl(Shell parent) {
185             return new DefaultInformationControl(parent);
186         }
187     };
188
189     @Override
190     public CharSequence getPrefixCompletionText(IDocument document, int completionOffset) {
191         return getName();
192     }
193
194     @Override
195     public int getPrefixCompletionStart(IDocument document, int completionOffset) {
196         return replacementOffset - prefix.length();
197     }
198
199     @Override
200     public boolean isAutoInsertable() {
201         return true;
202     }
203
204     @Override
205     public Object getAdditionalProposalInfo(IProgressMonitor monitor) {
206         return documentation;
207     }
208
209     public String getName() {
210         return name;
211     }
212     
213     public boolean isPrivate() {
214         return CSSCompletionType.PRIVATE == completionType;
215     }
216
217     public String getModule() {
218         return module;
219     }
220 }