KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > html > palette > items > RADIO


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.html.palette.items;
21 import java.util.TreeSet JavaDoc;
22 import javax.swing.text.BadLocationException JavaDoc;
23 import javax.swing.text.Document JavaDoc;
24 import javax.swing.text.JTextComponent JavaDoc;
25 import org.netbeans.editor.BaseDocument;
26 import org.netbeans.editor.TokenItem;
27 import org.netbeans.editor.ext.html.HTMLSyntaxSupport;
28 import org.netbeans.editor.ext.html.HTMLTokenContext;
29 import org.netbeans.modules.html.palette.HTMLPaletteUtilities;
30 import org.openide.text.ActiveEditorDrop;
31
32
33 /**
34  *
35  * @author Libor Kotouc
36  */

37 public class RADIO implements ActiveEditorDrop {
38
39     private static final int GROUP_DEFAULT = -1;
40     
41     private String JavaDoc group = "";
42     private int groupIndex = GROUP_DEFAULT;
43     private String JavaDoc value = "";
44     private boolean selected = false;
45     private boolean disabled = false;
46    
47     private String JavaDoc[] groups = new String JavaDoc[0];
48     
49     public RADIO() {
50     }
51
52     public boolean handleTransfer(JTextComponent JavaDoc targetComponent) {
53
54         Document JavaDoc doc = targetComponent.getDocument();
55         if (doc instanceof BaseDocument) {
56             
57             String JavaDoc oldGN = null;
58             if (groupIndex >= 0) // non-empty group list from previous run =>
59
oldGN = groups[groupIndex]; // => save previously selected group name
60
else if (group.length() > 0) // new group was inserted in the previous run
61
oldGN = group;
62             
63             groups = findGroups((BaseDocument)doc);
64             if (groups.length == 0) // no groups found => reset index
65
groupIndex = GROUP_DEFAULT;
66             
67             if (groups.length > 0) { // some groups found
68
groupIndex = 0; // point at the beginning by default
69
if (groupIndex != GROUP_DEFAULT && oldGN != null) {// non-empty group list from previous run
70
for (; groupIndex < groups.length; groupIndex++) {
71                         if (oldGN.equalsIgnoreCase(groups[groupIndex]))
72                             break;
73                     }
74                     if (groupIndex == groups.length) // previously selected group not found
75
groupIndex = 0;
76                 }
77             }
78         }
79         
80         RADIOCustomizer c = new RADIOCustomizer(this);
81         boolean accept = c.showDialog();
82         if (accept) {
83             String JavaDoc body = createBody();
84             try {
85                 HTMLPaletteUtilities.insert(body, targetComponent);
86             } catch (BadLocationException JavaDoc ble) {
87                 accept = false;
88             }
89         }
90         
91         return accept;
92     }
93     
94     private String JavaDoc createBody() {
95         
96         String JavaDoc strName = " name=\"\""; // NOI18N
97
if (groupIndex == GROUP_DEFAULT)
98             strName = " name=\"" + group + "\""; // NOI18N
99
else
100             strName = " name=\"" + groups[groupIndex] + "\""; // NOI18N
101

102         String JavaDoc strValue = " value=\"" + value + "\""; // NOI18N
103

104         String JavaDoc strSelected = (selected ? " checked=\"checked\"" : ""); // NOI18N
105

106         String JavaDoc strDisabled = (disabled ? " disabled=\"disabled\"" : ""); // NOI18N
107

108         String JavaDoc radioBody = "<input type=\"radio\"" + strName + strValue + strSelected + strDisabled + " />"; // NOI18N
109

110         return radioBody;
111     }
112
113     private String JavaDoc[] findGroups(BaseDocument doc) {
114          
115         String JavaDoc[] groups = new String JavaDoc[] {};
116         
117         if (doc.getLength() == 0)
118             return groups;
119
120         HTMLSyntaxSupport sup = (HTMLSyntaxSupport)(doc.getSyntaxSupport().get(HTMLSyntaxSupport.class));
121         
122         try {
123             TokenItem token = sup.getTokenChain(0, 1);
124             final int end = doc.getLength();
125             
126             boolean inputTagFound = false; // '<input' found
127
boolean typeAttrFound = false; // '<input type' found
128
boolean radioValFound = false; // '<input type="radio"' found
129
boolean nameAttrFound = false; // '<input name' found
130
String JavaDoc groupName = null;
131             TreeSet JavaDoc groupSet = new TreeSet JavaDoc();
132             
133             while (token != null && token.getOffset() < end) {
134                 token = token.getNext();
135                 if (token != null) {
136                     if (token.getTokenID() == HTMLTokenContext.TAG_OPEN && token.getImage().equals("input")) { //input open
137
inputTagFound = true;
138                     }
139                     else if (inputTagFound && token.getTokenID() == HTMLTokenContext.TAG_CLOSE_SYMBOL) { // input close
140

141                         if (radioValFound && groupName != null && groupName.length() > 0)
142                             groupSet.add(groupName);
143                         
144                         inputTagFound = false;
145                         typeAttrFound = false;
146                         radioValFound = false;
147                         nameAttrFound = false;
148                         groupName = null;
149                     }
150                     else if (inputTagFound && token.getTokenID() == HTMLTokenContext.ARGUMENT) {
151                         if (token.getImage().equals("type"))
152                             typeAttrFound = true;
153                         else if (token.getImage().equals("name"))
154                             nameAttrFound = true;
155                     }
156                     else if (typeAttrFound && token.getTokenID() == HTMLTokenContext.VALUE && token.getImage().equals("\"radio\"")) {
157                         radioValFound = true;
158                         typeAttrFound = false;
159                     }
160                     else if (nameAttrFound && token.getTokenID() == HTMLTokenContext.VALUE) {
161                         groupName = token.getImage();
162                         groupName = groupName.substring(1);
163                         groupName = groupName.substring(0, groupName.length() - 1);
164                         nameAttrFound = false;
165                     }
166                 }
167             }
168
169             groups = (String JavaDoc[])groupSet.toArray(new String JavaDoc[0]);
170             
171         } catch (IllegalStateException JavaDoc ise) {
172         } catch (BadLocationException JavaDoc ble) {
173         }
174         
175         return groups;
176     }
177
178     public String JavaDoc getGroup() {
179         return group;
180     }
181
182     public void setGroup(String JavaDoc group) {
183         this.group = group;
184     }
185
186     public int getGroupIndex() {
187         return groupIndex;
188     }
189
190     public void setGroupIndex(int groupIndex) {
191         this.groupIndex = groupIndex;
192     }
193
194     public String JavaDoc getValue() {
195         return value;
196     }
197
198     public void setValue(String JavaDoc value) {
199         this.value = value;
200     }
201
202     public boolean isSelected() {
203         return selected;
204     }
205
206     public void setSelected(boolean selected) {
207         this.selected = selected;
208     }
209
210     public boolean isDisabled() {
211         return disabled;
212     }
213
214     public void setDisabled(boolean disabled) {
215         this.disabled = disabled;
216     }
217
218     public String JavaDoc[] getGroups() {
219         return groups;
220     }
221
222     public void setGroups(String JavaDoc[] groups) {
223         this.groups = groups;
224     }
225     
226 }
227
Popular Tags