KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > persistence > editor > completion > NNResultItem


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.j2ee.persistence.editor.completion;
21
22 import org.netbeans.api.db.explorer.ConnectionManager;
23 import org.netbeans.api.db.explorer.DatabaseConnection;
24 import org.netbeans.api.editor.completion.Completion;
25 import org.netbeans.editor.BaseDocument;
26 import org.netbeans.editor.ext.CompletionQuery;
27 import org.netbeans.spi.editor.completion.*;
28
29 import java.awt.Color JavaDoc;
30 import java.awt.Component JavaDoc;
31 import java.awt.Font JavaDoc;
32 import java.awt.Graphics JavaDoc;
33 import java.awt.event.KeyEvent JavaDoc;
34 import javax.swing.text.BadLocationException JavaDoc;
35 import javax.swing.text.JTextComponent JavaDoc;
36
37 /**
38  *
39  * @author Dusan Balek, Andrei Badea, Marek Fukala
40  */

41
42 public abstract class NNResultItem implements CompletionQuery.ResultItem, CompletionItem {
43
44     protected int substituteOffset = -1;
45
46     public abstract String JavaDoc getItemText();
47
48     public int getSubstituteOffset() {
49         return substituteOffset;
50     }
51     
52     public boolean substituteCommonText(JTextComponent JavaDoc c, int offset, int len, int subLen) {
53         // [PENDING] not enough info in parameters...
54
// commonText
55
// substituteExp
56
return false;
57     }
58     
59     public boolean substituteText(JTextComponent JavaDoc c, int offset, int len, boolean shift) {
60         BaseDocument doc = (BaseDocument)c.getDocument();
61         String JavaDoc text = getItemText();
62
63         if (text != null) {
64             if (toAdd != null && !toAdd.equals("\n")) // NOI18N
65
text += toAdd;
66             // Update the text
67
doc.atomicLock();
68             try {
69                 String JavaDoc textToReplace = doc.getText(offset, len);
70                 if (text.equals(textToReplace)) return false;
71                 
72                 //dirty hack for @Table(name=CUS|
73
if(!text.startsWith("\"")) {
74                     text = quoteText(text);
75                 }
76                 
77                 //check if there is already an end quote
78
char ch = doc.getText(offset + len, 1).charAt(0);
79                 if(ch == '"') {
80                     //remove also this end quote since the inserted value is always quoted
81
len++;
82                 }
83                 
84                 doc.remove(offset, len);
85                 doc.insertString(offset, text, null);
86             } catch (BadLocationException JavaDoc e) {
87                 // Can't update
88
e.printStackTrace();
89             } finally {
90                 doc.atomicUnlock();
91             }
92             return true;
93
94         } else {
95             return false;
96         }
97     }
98     
99     public Component JavaDoc getPaintComponent(javax.swing.JList JavaDoc list, boolean isSelected, boolean cellHasFocus) {
100         Component JavaDoc ret = getPaintComponent(isSelected);
101         if (ret==null) return null;
102         if (isSelected) {
103             ret.setBackground(list.getSelectionBackground());
104             ret.setForeground(list.getSelectionForeground());
105         } else {
106             ret.setBackground(list.getBackground());
107             ret.setForeground(list.getForeground());
108         }
109         ret.getAccessibleContext().setAccessibleName(getItemText());
110         ret.getAccessibleContext().setAccessibleDescription(getItemText());
111         return ret;
112     }
113     
114     public abstract Component JavaDoc getPaintComponent(boolean isSelected);
115
116     public int getPreferredWidth(Graphics JavaDoc g, Font JavaDoc defaultFont) {
117         Component JavaDoc renderComponent = getPaintComponent(false);
118         return renderComponent.getPreferredSize().width;
119     }
120
121     public void render(Graphics JavaDoc g, Font JavaDoc defaultFont, Color JavaDoc defaultColor,
122     Color JavaDoc backgroundColor, int width, int height, boolean selected) {
123         Component JavaDoc renderComponent = getPaintComponent(selected);
124         renderComponent.setFont(defaultFont);
125         renderComponent.setForeground(defaultColor);
126         renderComponent.setBackground(backgroundColor);
127         renderComponent.setBounds(0, 0, width, height);
128         ((NNPaintComponent)renderComponent).paintComponent(g);
129     }
130     
131     public String JavaDoc toString() {
132         return getItemText();
133     }
134
135     // CompletionItem implementation
136

137     public static final String JavaDoc COMPLETION_SUBSTITUTE_TEXT= "completion-substitute-text"; //NOI18N
138

139     static String JavaDoc toAdd;
140
141     public void processKeyEvent(KeyEvent JavaDoc evt) {
142         if (evt.getID() == KeyEvent.KEY_TYPED) {
143             Completion completion = Completion.get();
144             switch (evt.getKeyChar()) {
145                 case ' ':
146                     if (evt.getModifiers() == 0) {
147                         completion.hideCompletion();
148                         completion.hideDocumentation();
149                     }
150                     break;
151                 case ';':
152                 case ',':
153                 case '(':
154                     completion.hideCompletion();
155                     completion.hideDocumentation();
156                 case '.':
157                     if (defaultAction((JTextComponent JavaDoc)evt.getSource(), Character.toString(evt.getKeyChar()))) {
158                         evt.consume();
159                         break;
160                     }
161             }
162         }
163     }
164
165     protected String JavaDoc quoteText(String JavaDoc s) {
166         return "\"" + s + "\"";
167     }
168     
169     public CharSequence JavaDoc getSortText() {
170         return getItemText();
171     }
172     
173     public CharSequence JavaDoc getInsertPrefix() {
174         return getItemText();
175     }
176
177     public CompletionTask createDocumentationTask() {
178         return null;
179     }
180     
181     public CompletionTask createToolTipTask() {
182         return null;
183     }
184
185     public boolean instantSubstitution(JTextComponent JavaDoc c) {
186         Completion completion = Completion.get();
187         completion.hideCompletion();
188         completion.hideDocumentation();
189         defaultAction(c);
190         return true;
191     }
192
193     public void defaultAction(JTextComponent JavaDoc component) {
194         Completion completion = Completion.get();
195         completion.hideCompletion();
196         completion.hideDocumentation();
197         defaultAction(component, "");
198     }
199     
200     private boolean defaultAction(JTextComponent JavaDoc component, String JavaDoc addText) {
201         int substOffset = substituteOffset;
202         if (substOffset == -1)
203             substOffset = component.getCaret().getDot();
204         NNResultItem.toAdd = addText;
205         return substituteText(component, substOffset, component.getCaret().getDot() - substOffset, false);
206     }
207     
208     private abstract static class DBElementItem extends NNResultItem {
209         
210         private String JavaDoc name;
211         private boolean quote;
212         
213         protected static NNPaintComponent.DBElementPaintComponent paintComponent = null;
214         
215         // XXX should have an elementTypeName param
216
public DBElementItem(String JavaDoc name, boolean quote, int substituteOffset) {
217             this.name = name;
218             this.quote = quote;
219             this.substituteOffset = substituteOffset;
220         }
221         
222         public DBElementItem(String JavaDoc name, boolean quote) {
223             this(name, quote, -1);
224         }
225         
226         protected String JavaDoc getName() {
227             return name;
228         }
229         
230         protected boolean getQuoted() {
231             return quote;
232         }
233         
234         public String JavaDoc getItemText() {
235             if (quote) {
236                 return quoteText(name);
237             } else {
238                 return name;
239             }
240         }
241         
242         public Component JavaDoc getPaintComponent(boolean isSelected) {
243             if (paintComponent == null) {
244                 paintComponent = new NNPaintComponent.DBElementPaintComponent();
245             }
246             paintComponent.setString(getTypeName() + ": " + name); // NOI18N
247
paintComponent.setSelected(isSelected);
248             return paintComponent;
249         }
250         
251         public Object JavaDoc getAssociatedObject() {
252             return this;
253         }
254         
255         /**
256          * Returns the element name (table, schema, etc.).
257          */

258         public abstract String JavaDoc getTypeName();
259     }
260     
261     public static final class PersistenceUnitElementItem extends DBElementItem {
262         
263         protected static NNPaintComponent.PersistenceUnitElementPaintComponent paintComponent = null;
264
265         public PersistenceUnitElementItem(String JavaDoc name, boolean quote, int substituteOffset) {
266             super(name, quote, substituteOffset);
267         }
268         
269         public String JavaDoc getTypeName() {
270             return "Persistence Unit";
271         }
272         
273         public int getSortPriority() {
274             return 100;
275         }
276         
277         public Component JavaDoc getPaintComponent(boolean isSelected) {
278             if (paintComponent == null) {
279                 paintComponent = new NNPaintComponent.PersistenceUnitElementPaintComponent();
280             }
281             paintComponent.setContent(getName());
282             paintComponent.setSelected(isSelected);
283             return paintComponent;
284         }
285         
286     }
287     
288     public static final class EntityPropertyElementItem extends DBElementItem {
289         
290         protected static NNPaintComponent.EntityPropertyElementPaintComponent paintComponent = null;
291
292         public EntityPropertyElementItem(String JavaDoc name, boolean quote, int substituteOffset) {
293             super(name, quote, substituteOffset);
294         }
295         
296         public String JavaDoc getTypeName() {
297             return "Persistence Unit";
298         }
299         
300         public int getSortPriority() {
301             return 100;
302         }
303         
304         public Component JavaDoc getPaintComponent(boolean isSelected) {
305             if (paintComponent == null) {
306                 paintComponent = new NNPaintComponent.EntityPropertyElementPaintComponent();
307             }
308             paintComponent.setContent(getName());
309             paintComponent.setSelected(isSelected);
310             return paintComponent;
311         }
312         
313     }
314     
315     public static final class CatalogElementItem extends DBElementItem {
316         
317         public CatalogElementItem(String JavaDoc name, boolean quote, int substituteOffset) {
318             super(name, quote, substituteOffset);
319         }
320         
321         public String JavaDoc getTypeName() {
322             return "Catalog";
323         }
324         
325         public int getSortPriority() {
326             return 100;
327         }
328     }
329     
330     public static final class SchemaElementItem extends DBElementItem {
331         
332         public SchemaElementItem(String JavaDoc name, boolean quote, int substituteOffset) {
333             super(name, quote, substituteOffset);
334         }
335         
336         public String JavaDoc getTypeName() {
337             return "Schema";
338         }
339         
340         public int getSortPriority() {
341             return 100;
342         }
343     }
344
345     public static final class TableElementItem extends DBElementItem {
346
347         protected static NNPaintComponent.TableElementPaintComponent paintComponent = null;
348         
349         public TableElementItem(String JavaDoc name, boolean quote, int substituteOffset) {
350             super(name, quote, substituteOffset);
351         }
352         
353         public String JavaDoc getTypeName() {
354             return "Table";
355         }
356         
357         public int getSortPriority() {
358             return 100;
359         }
360         
361         public Component JavaDoc getPaintComponent(boolean isSelected) {
362             if (paintComponent == null) {
363                 paintComponent = new NNPaintComponent.TableElementPaintComponent();
364             }
365             paintComponent.setContent(getName());
366             paintComponent.setSelected(isSelected);
367             return paintComponent;
368         }
369         
370         public Object JavaDoc getAssociatedObject() {
371             return this;
372         }
373     }
374     
375     public static final class ColumnElementItem extends DBElementItem {
376         
377         private String JavaDoc tableName;
378         
379         protected static NNPaintComponent.ColumnElementPaintComponent paintComponent = null;
380         
381         public ColumnElementItem(String JavaDoc columnName, String JavaDoc tableName, boolean quote, int substituteOffset) {
382             super(columnName, quote, substituteOffset);
383             this.tableName = tableName;
384         }
385         
386         public String JavaDoc getTypeName() {
387             return "Column";
388         }
389         
390         public int getSortPriority() {
391             return 100;
392         }
393         
394         public String JavaDoc getItemText() {
395             return "\"" + getName() + "\""; // NOI18N
396
}
397         
398         public Component JavaDoc getPaintComponent(boolean isSelected) {
399             if (paintComponent == null) {
400                 paintComponent = new NNPaintComponent.ColumnElementPaintComponent();
401             }
402             paintComponent.setContent(getName(), tableName);
403             paintComponent.setSelected(isSelected);
404             return paintComponent;
405         }
406         
407         public Object JavaDoc getAssociatedObject() {
408             return this;
409         }
410         
411     }
412
413     public static final class NoConnectionElementItem extends NNResultItem {
414         
415         private static NNPaintComponent.NoConnectionItemPaintComponent paintComponent = null;
416         private DatabaseConnection dbconn;
417         
418         public NoConnectionElementItem(DatabaseConnection dbconn) {
419             this.dbconn = dbconn;
420         }
421         
422         public int getSortPriority() {
423             return 1;
424         }
425         
426         public String JavaDoc getItemText() {
427             return "";
428         }
429
430         public boolean substituteText(JTextComponent JavaDoc c, int offset, int len, boolean shift) {
431             ConnectionManager.getDefault().showConnectionDialog(dbconn);
432             return false;
433         }
434         
435         public Component JavaDoc getPaintComponent(boolean isSelected) {
436             if (paintComponent == null) {
437                 paintComponent = new NNPaintComponent.NoConnectionItemPaintComponent();
438             }
439             paintComponent.setSelected(isSelected);
440             return paintComponent;
441         }
442         
443         public Object JavaDoc getAssociatedObject() {
444             return this;
445         }
446         
447     }
448     
449 }
450
Popular Tags