1 19 20 package org.netbeans.modules.html.palette.items; 21 import java.util.TreeSet ; 22 import javax.swing.text.BadLocationException ; 23 import javax.swing.text.Document ; 24 import javax.swing.text.JTextComponent ; 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 37 public class RADIO implements ActiveEditorDrop { 38 39 private static final int GROUP_DEFAULT = -1; 40 41 private String group = ""; 42 private int groupIndex = GROUP_DEFAULT; 43 private String value = ""; 44 private boolean selected = false; 45 private boolean disabled = false; 46 47 private String [] groups = new String [0]; 48 49 public RADIO() { 50 } 51 52 public boolean handleTransfer(JTextComponent targetComponent) { 53 54 Document doc = targetComponent.getDocument(); 55 if (doc instanceof BaseDocument) { 56 57 String oldGN = null; 58 if (groupIndex >= 0) oldGN = groups[groupIndex]; else if (group.length() > 0) oldGN = group; 62 63 groups = findGroups((BaseDocument)doc); 64 if (groups.length == 0) groupIndex = GROUP_DEFAULT; 66 67 if (groups.length > 0) { groupIndex = 0; if (groupIndex != GROUP_DEFAULT && oldGN != null) { for (; groupIndex < groups.length; groupIndex++) { 71 if (oldGN.equalsIgnoreCase(groups[groupIndex])) 72 break; 73 } 74 if (groupIndex == groups.length) groupIndex = 0; 76 } 77 } 78 } 79 80 RADIOCustomizer c = new RADIOCustomizer(this); 81 boolean accept = c.showDialog(); 82 if (accept) { 83 String body = createBody(); 84 try { 85 HTMLPaletteUtilities.insert(body, targetComponent); 86 } catch (BadLocationException ble) { 87 accept = false; 88 } 89 } 90 91 return accept; 92 } 93 94 private String createBody() { 95 96 String strName = " name=\"\""; if (groupIndex == GROUP_DEFAULT) 98 strName = " name=\"" + group + "\""; else 100 strName = " name=\"" + groups[groupIndex] + "\""; 102 String strValue = " value=\"" + value + "\""; 104 String strSelected = (selected ? " checked=\"checked\"" : ""); 106 String strDisabled = (disabled ? " disabled=\"disabled\"" : ""); 108 String radioBody = "<input type=\"radio\"" + strName + strValue + strSelected + strDisabled + " />"; 110 return radioBody; 111 } 112 113 private String [] findGroups(BaseDocument doc) { 114 115 String [] groups = new String [] {}; 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; boolean typeAttrFound = false; boolean radioValFound = false; boolean nameAttrFound = false; String groupName = null; 131 TreeSet groupSet = new TreeSet (); 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")) { inputTagFound = true; 138 } 139 else if (inputTagFound && token.getTokenID() == HTMLTokenContext.TAG_CLOSE_SYMBOL) { 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 [])groupSet.toArray(new String [0]); 170 171 } catch (IllegalStateException ise) { 172 } catch (BadLocationException ble) { 173 } 174 175 return groups; 176 } 177 178 public String getGroup() { 179 return group; 180 } 181 182 public void setGroup(String 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 getValue() { 195 return value; 196 } 197 198 public void setValue(String 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 [] getGroups() { 219 return groups; 220 } 221 222 public void setGroups(String [] groups) { 223 this.groups = groups; 224 } 225 226 } 227 | Popular Tags |