KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > websvc > editor > completion > WSResultItem


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.websvc.editor.completion;
21
22 import java.awt.Image JavaDoc;
23 import org.netbeans.api.editor.completion.Completion;
24 import org.netbeans.editor.BaseDocument;
25 import org.netbeans.editor.ext.CompletionQuery;
26 import org.netbeans.spi.editor.completion.*;
27
28 import java.awt.Color JavaDoc;
29 import java.awt.Component JavaDoc;
30 import java.awt.Font JavaDoc;
31 import java.awt.Graphics JavaDoc;
32 import java.awt.event.KeyEvent JavaDoc;
33 import javax.swing.text.BadLocationException JavaDoc;
34 import javax.swing.text.JTextComponent JavaDoc;
35
36 /**
37  *
38  * @author Dusan Balek, Marek Fukala
39  */

40
41 public abstract class WSResultItem implements CompletionQuery.ResultItem, CompletionItem {
42
43     protected int substituteOffset = -1;
44
45     public abstract String JavaDoc getItemText();
46
47 // protected static Color getTypeColor(Type typ) {
48
// return (typ instanceof PrimitiveType) ? JEE5PaintComponent.KEYWORD_COLOR : JEE5PaintComponent.TYPE_COLOR;
49
// }
50

51     public void setSubstituteOffset(int substituteOffset) {
52         this.substituteOffset = substituteOffset;
53     }
54     
55     public boolean substituteCommonText(JTextComponent JavaDoc c, int offset, int len, int subLen) {
56         // [PENDING] not enough info in parameters...
57
// commonText
58
// substituteExp
59
return false;
60     }
61     
62     public Object JavaDoc getAssociatedObject() {
63         return this;
64     }
65     
66     public boolean substituteText(JTextComponent JavaDoc c, int offset, int len, boolean shift) {
67         BaseDocument doc = (BaseDocument)c.getDocument();
68         String JavaDoc text = getItemText();
69
70         if (text != null) {
71             if (toAdd != null && !toAdd.equals("\n")) // NOI18N
72
text += toAdd;
73             // Update the text
74
doc.atomicLock();
75             try {
76                 String JavaDoc textToReplace = doc.getText(offset, len);
77                 if (text.equals(textToReplace)) return false;
78                 doc.remove(offset, len);
79                 doc.insertString(offset, text, null);
80             } catch (BadLocationException JavaDoc e) {
81                 // Can't update
82
return false;
83             } finally {
84                 doc.atomicUnlock();
85             }
86             return true;
87
88         } else {
89             return false;
90         }
91     }
92     
93     public Component JavaDoc getPaintComponent(javax.swing.JList JavaDoc list, boolean isSelected, boolean cellHasFocus) {
94         Component JavaDoc ret = getPaintComponent(isSelected);
95         if (ret==null) return null;
96         if (isSelected) {
97             ret.setBackground(list.getSelectionBackground());
98             ret.setForeground(list.getSelectionForeground());
99         } else {
100             ret.setBackground(list.getBackground());
101             ret.setForeground(list.getForeground());
102         }
103         ret.getAccessibleContext().setAccessibleName(getItemText());
104         ret.getAccessibleContext().setAccessibleDescription(getItemText());
105         return ret;
106     }
107     
108     public abstract Component JavaDoc getPaintComponent(boolean isSelected);
109
110     public int getPreferredWidth(Graphics JavaDoc g, Font JavaDoc defaultFont) {
111         Component JavaDoc renderComponent = getPaintComponent(false);
112         return renderComponent.getPreferredSize().width;
113     }
114
115     public void render(Graphics JavaDoc g, Font JavaDoc defaultFont, Color JavaDoc defaultColor,
116     Color JavaDoc backgroundColor, int width, int height, boolean selected) {
117         Component JavaDoc renderComponent = getPaintComponent(selected);
118         renderComponent.setFont(defaultFont);
119         renderComponent.setForeground(defaultColor);
120         renderComponent.setBackground(backgroundColor);
121         renderComponent.setBounds(0, 0, width, height);
122         ((WSPaintComponent)renderComponent).paintComponent(g);
123     }
124     
125     public String JavaDoc toString() {
126         return getItemText();
127     }
128
129     // CompletionItem implementation
130

131     public static final String JavaDoc COMPLETION_SUBSTITUTE_TEXT= "completion-substitute-text"; //NOI18N
132

133     static String JavaDoc toAdd;
134
135     public void processKeyEvent(KeyEvent JavaDoc evt) {
136         if (evt.getID() == KeyEvent.KEY_TYPED) {
137             Completion completion = Completion.get();
138             switch (evt.getKeyChar()) {
139                 case ' ':
140                     if (evt.getModifiers() == 0) {
141                         completion.hideCompletion();
142                         completion.hideDocumentation();
143                     }
144                     break;
145                 case ';':
146                 case ',':
147                 case '(':
148                     completion.hideCompletion();
149                     completion.hideDocumentation();
150                 case '.':
151                     if (defaultAction((JTextComponent JavaDoc)evt.getSource(), Character.toString(evt.getKeyChar()))) {
152                         evt.consume();
153                         break;
154                     }
155             }
156         }
157     }
158
159     public CharSequence JavaDoc getSortText() {
160         return getItemText();
161     }
162     
163     public CharSequence JavaDoc getInsertPrefix() {
164         return getItemText();
165     }
166
167     public CompletionTask createDocumentationTask() {
168         return null;
169     }
170     
171     public CompletionTask createToolTipTask() {
172         return null;
173     }
174
175     public boolean instantSubstitution(JTextComponent JavaDoc c) {
176         Completion completion = Completion.get();
177         completion.hideCompletion();
178         completion.hideDocumentation();
179         defaultAction(c);
180         return true;
181     }
182
183     public void defaultAction(JTextComponent JavaDoc component) {
184         Completion completion = Completion.get();
185         completion.hideCompletion();
186         completion.hideDocumentation();
187         defaultAction(component, "");
188     }
189     
190     private boolean defaultAction(JTextComponent JavaDoc component, String JavaDoc addText) {
191         int substOffset = substituteOffset;
192         if (substOffset == -1)
193             substOffset = component.getCaret().getDot();
194         WSResultItem.toAdd = addText;
195         return substituteText(component, substOffset, component.getCaret().getDot() - substOffset, false);
196     }
197    
198     //TODO: cache paint components
199

200     private abstract static class FileAttributeValue extends WSResultItem {
201         
202         private String JavaDoc name;
203   
204         public FileAttributeValue(String JavaDoc name) {
205             this.name = name;
206         }
207         
208         public String JavaDoc getItemText() {
209             return name;
210         }
211         
212         public int getSortPriority() {
213             return 10;
214         }
215         
216     }
217     
218     static class PackageResultItem extends FileAttributeValue {
219         
220         public PackageResultItem(String JavaDoc name) {
221             super(name);
222         }
223         
224         public int getSortPriority() {
225             return 9;
226         }
227       
228         public Component JavaDoc getPaintComponent(final boolean isSelected) {
229             WSPaintComponent.NbStringPaintComponent pc = new WSPaintComponent.PackageItemPaintComponent();
230             pc.setString(getItemText());
231             return pc;
232         }
233     }
234     
235     static class FileResultItem extends FileAttributeValue {
236         
237         private Image JavaDoc icon;
238         
239         public FileResultItem(String JavaDoc name, Image JavaDoc icon) {
240             super(name);
241             this.icon = icon;
242         }
243         
244        public Component JavaDoc getPaintComponent(final boolean isSelected) {
245             WSPaintComponent.NbStringPaintComponent pc = new WSPaintComponent.FileItemPaintComponent(icon);
246             pc.setString(getItemText());
247             return pc;
248         }
249     }
250     
251     static class FileProtocolResultItem extends WSResultItem {
252         
253         private boolean quoted;
254         
255         public FileProtocolResultItem(boolean quoted) {
256             this.quoted = quoted;
257         }
258         
259         public String JavaDoc getDisplayname() {
260             return "file://";
261         }
262         
263         public String JavaDoc getItemText() {
264             return (quoted ? "" : "\"") + getDisplayname() + (quoted ? "" : "\"");
265         }
266         
267         public boolean substituteText(JTextComponent JavaDoc c, int offset, int len, boolean shift) {
268             boolean substituted = super.substituteText(c, offset, len, shift);
269             if(substituted && !quoted) {
270                 c.setCaretPosition(c.getCaretPosition() - 1);
271             }
272             return substituted;
273         }
274         
275         public int getSortPriority() {
276             return 100;
277         }
278         
279        public Component JavaDoc getPaintComponent(final boolean isSelected) {
280             WSPaintComponent.NbStringPaintComponent pc = new WSPaintComponent.FileProtocolItemPaintComponent();
281             pc.setString(getDisplayname());
282             return pc;
283         }
284        
285     }
286 }
287
Popular Tags