KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > schema > completion > CompletionResultItem


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.schema.completion;
21
22 import java.awt.Color JavaDoc;
23 import java.awt.Component JavaDoc;
24 import java.awt.Font JavaDoc;
25 import java.awt.Graphics JavaDoc;
26 import java.awt.event.KeyEvent JavaDoc;
27 import javax.swing.text.*;
28 import javax.swing.Icon JavaDoc;
29 import javax.xml.XMLConstants JavaDoc;
30 import org.netbeans.editor.BaseDocument;
31 import org.netbeans.api.editor.completion.Completion;
32 import org.netbeans.spi.editor.completion.CompletionItem;
33 import org.netbeans.spi.editor.completion.CompletionTask;
34 import org.netbeans.modules.xml.axi.AXIComponent;
35 import org.netbeans.modules.xml.schema.completion.spi.CompletionContext;
36 import org.netbeans.modules.xml.schema.completion.util.CompletionContextImpl;
37 import org.netbeans.modules.xml.schema.completion.util.CompletionUtil;
38 import org.netbeans.modules.xml.text.syntax.dom.StartTag;
39 import org.netbeans.spi.editor.completion.support.AsyncCompletionTask;
40
41 /**
42  *
43  * @author Samaresh (Samaresh.Panda@Sun.Com)
44  */

45 public abstract class CompletionResultItem implements CompletionItem {
46
47     /**
48      * Creates a new instance of CompletionUtil
49      */

50     public CompletionResultItem(AXIComponent component, CompletionContext context) {
51         this.context = (CompletionContextImpl)context;
52         this.axiComponent = component;
53         this.typedChars = context.getTypedChars();
54     }
55         
56     Icon JavaDoc getIcon(){
57         return icon;
58     }
59     
60     public AXIComponent getAXIComponent() {
61         return axiComponent;
62     }
63     
64     /**
65      * The text user sees in the CC list.
66      * Implementors should initialize the displayText here and return.
67      */

68     public abstract String JavaDoc getItemText();
69     
70     /**
71      * Insert following text into document.
72      */

73     public String JavaDoc getReplacementText(){
74         return replacementText;
75     }
76     
77     public String JavaDoc toString() {
78         return getItemText();
79     }
80         
81     Color JavaDoc getPaintColor() { return Color.BLUE; }
82     
83     /**
84      * Actually replaces a piece of document by passes text.
85      * @param component a document source
86      * @param text a string to be inserted
87      * @param offset the target offset
88      * @param len a length that should be removed before inserting text
89      */

90     private boolean replaceText( JTextComponent component, String JavaDoc text, int offset, int len) {
91         BaseDocument doc = (BaseDocument)component.getDocument();
92         doc.atomicLock();
93         try {
94             doc.remove( offset, len );
95             doc.insertString( offset, text, null);
96             StartTag docRoot = context.getDocRoot();
97             String JavaDoc prefix = CompletionUtil.getPrefixFromTag(text);
98             if(prefix == null)
99                 return true;
100             
101             //insert namespace declaration for the new prefix
102
if(!context.isPrefixBeingUsed(prefix)) {
103                 String JavaDoc tns = context.getTargetNamespaceByPrefix(prefix);
104                 doc.insertString( docRoot.getElementOffset() +
105                         docRoot.getElementLength()-1, " "+
106                         XMLConstants.XMLNS_ATTRIBUTE+":"+prefix+"=\"" +
107                         tns + "\"", null);
108             }
109             //reformat the line
110
//((ExtFormatter)doc.getFormatter()).reformat(doc,
111
//Utilities.getRowStart(doc, offset), offset+text.length(), true);
112
} catch( BadLocationException exc ) {
113             return false; //not sucessfull
114
} finally {
115             doc.atomicUnlock();
116         }
117         return true;
118     }
119         
120     ////////////////////////////////////////////////////////////////////////////////
121
///////////////////methods from CompletionItem interface////////////////////////
122
////////////////////////////////////////////////////////////////////////////////
123
public CompletionTask createDocumentationTask() {
124         return new AsyncCompletionTask(new DocumentationQuery(this));
125     }
126     
127     public CompletionTask createToolTipTask() {
128         return new AsyncCompletionTask(new ToolTipQuery(this));
129     }
130     
131     public void defaultAction(JTextComponent component) {
132         int charsToRemove = (typedChars==null)?0:typedChars.length();
133         int substOffset = component.getCaretPosition() - charsToRemove;
134         
135         if(!shift) Completion.get().hideAll();
136         replaceText(component, getReplacementText(), substOffset, charsToRemove);
137     }
138         
139     public CharSequence JavaDoc getInsertPrefix() {
140         return getItemText();
141     }
142     
143     public abstract Component JavaDoc getPaintComponent(boolean isSelected);
144     
145     public int getPreferredWidth(Graphics JavaDoc g, Font JavaDoc defaultFont) {
146         Component JavaDoc renderComponent = getPaintComponent(false);
147         return renderComponent.getPreferredSize().width;
148     }
149     
150     public int getSortPriority() {
151         return 0;
152     }
153     
154     public CharSequence JavaDoc getSortText() {
155         return getItemText();
156     }
157     
158     public boolean instantSubstitution(JTextComponent component) {
159         defaultAction(component);
160         return true;
161     }
162     
163     public void processKeyEvent(KeyEvent JavaDoc e) {
164         shift = (e.getKeyCode() == KeyEvent.VK_ENTER && e.getID() == KeyEvent.KEY_PRESSED && e.isShiftDown());
165     }
166     
167     public void render(Graphics JavaDoc g, Font JavaDoc defaultFont,
168             Color JavaDoc defaultColor, Color JavaDoc backgroundColor,
169             int width, int height, boolean selected) {
170         Component JavaDoc renderComponent = getPaintComponent(selected);
171         renderComponent.setFont(defaultFont);
172         renderComponent.setForeground(defaultColor);
173         renderComponent.setBackground(backgroundColor);
174         renderComponent.setBounds(0, 0, width, height);
175         ((CompletionPaintComponent)renderComponent).paintComponent(g);
176     }
177         
178     protected boolean shift = false;
179     protected String JavaDoc typedChars;
180     protected String JavaDoc replacementText;
181     protected String JavaDoc displayText;
182     protected javax.swing.Icon JavaDoc icon;
183     protected CompletionPaintComponent component;
184     protected AXIComponent axiComponent;
185     private CompletionContextImpl context;
186     
187     private static Color JavaDoc foreground = Color.black;
188     private static Color JavaDoc background = Color.white;
189     private static Color JavaDoc selectionForeground = Color.black;
190     private static Color JavaDoc selectionBackground = new Color JavaDoc(204, 204, 255);
191     private static final int XML_ITEMS_SORT_PRIORITY = 20;
192     
193     public static final String JavaDoc ICON_ELEMENT = "element.png"; //NOI18N
194
public static final String JavaDoc ICON_ATTRIBUTE = "attribute.png"; //NOI18N
195
public static final String JavaDoc ICON_LOCATION = "/org/netbeans/modules/xml/schema/completion/resources/"; //NOI18N
196
}
197
Popular Tags