KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > text > completion > ElementResultItem


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.xml.text.completion;
21
22 import java.awt.Color JavaDoc;
23
24 import org.netbeans.modules.xml.api.model.*;
25 import javax.swing.text.JTextComponent JavaDoc;
26 import javax.swing.text.Caret JavaDoc;
27
28 /**
29  * Represent element name (or its part for namespace prefix).
30  *
31  * @author sands
32  * @author Petr Kuzel
33  */

34 class ElementResultItem extends XMLResultItem {
35     
36     // does it represent start element name?
37
// then there is more possibilities how to complete it
38
private final boolean startElement;
39     
40     private final boolean empty;
41     
42     /**
43      * Create a start element result item.
44      */

45     public ElementResultItem(GrammarResult res){
46         super(res.getNodeName());
47         foreground = Color.blue;
48         startElement = true;
49         empty = res.isEmptyElement();
50     }
51     
52     /**
53      * Create an end element result item.
54      */

55     public ElementResultItem(String JavaDoc name) {
56         super(name);
57         foreground = Color.blue;
58         startElement = false;
59         empty = false;
60     }
61     
62     /**
63      * Replacenment text can be cutomized to retun pairs, empty tag or
64      * just name of element.
65      */

66     public String JavaDoc getReplacementText(int modifiers) {
67         boolean shift = (modifiers & java.awt.event.InputEvent.SHIFT_MASK) != 0;
68         
69         if (shift && startElement) {
70             if (empty) {
71                 return displayText + "/>";
72             } else {
73                 return displayText + ">";
74             }
75         } else if (startElement) {
76             return displayText;
77         } else {
78             return displayText + '>';
79         }
80     }
81     
82     
83     /**
84      * If called with <code>SHIFT_MASK</code> modified it createa a start tag and
85      * end tag pair and place caret between them.
86      */

87     public boolean substituteText( JTextComponent JavaDoc c, int offset, int len, int modifiers ){
88         String JavaDoc replacementText = getReplacementText(modifiers);
89         replaceText(c, replacementText, offset, len);
90         
91         boolean shift = (modifiers & java.awt.event.InputEvent.SHIFT_MASK) != 0;
92
93         if (shift && startElement) {
94             Caret JavaDoc caret = c.getCaret(); // it is at the end of replacement
95
int dot = caret.getDot();
96             int rlen = replacementText.length();
97             if (empty) {
98                 caret.setDot((dot - rlen) + replacementText.indexOf('/'));
99             }
100         }
101         
102         return false;
103     }
104     
105     /**
106      * @deprecated we use startElement flag
107      */

108 // static class EndTag extends ElementResultItem {
109
// }
110

111     Color JavaDoc getPaintColor() { return Color.blue; }
112 }
113
Popular Tags