1 19 20 21 package org.netbeans.modules.properties; 22 23 24 import java.awt.Component ; 25 import java.beans.PropertyChangeEvent ; 26 import java.beans.PropertyChangeListener ; 27 import java.io.IOException ; 28 import java.lang.reflect.InvocationTargetException ; 29 import javax.swing.Action ; 30 31 import org.openide.actions.*; 32 import org.openide.cookies.EditCookie; 33 import org.openide.cookies.SaveCookie; 34 import org.openide.cookies.OpenCookie; 35 import org.openide.nodes.AbstractNode; 36 import org.openide.nodes.Children; 37 import org.openide.nodes.Node; 38 import org.openide.nodes.PropertySupport; 39 import org.openide.nodes.Sheet; 40 import org.openide.NotifyDescriptor; 41 import org.openide.DialogDisplayer; 42 import org.openide.util.actions.SystemAction; 43 import org.openide.util.HelpCtx; 44 import org.openide.util.NbBundle; 45 import org.openide.util.WeakListeners; 46 47 48 53 public class KeyNode extends AbstractNode implements PropertyChangeListener { 54 55 56 private PropertiesStructure propStructure; 57 58 59 private String itemKey; 60 61 62 static final long serialVersionUID = -7882925922830244768L; 63 64 65 69 public KeyNode (PropertiesStructure propStructure, String itemKey) { 70 super(Children.LEAF); 71 72 this.propStructure = propStructure; 73 this.itemKey = itemKey; 74 75 super.setName(itemKey); 76 77 setActions( 78 new SystemAction[] { 79 SystemAction.get(EditAction.class), 80 SystemAction.get(OpenAction.class), 81 SystemAction.get(FileSystemAction.class), 82 null, 83 SystemAction.get(CutAction.class), 84 SystemAction.get(CopyAction.class), 85 null, 86 SystemAction.get(DeleteAction.class), 87 SystemAction.get(RenameAction.class), 88 null, 89 SystemAction.get(ToolsAction.class), 90 SystemAction.get(PropertiesAction.class) 91 } 92 ); 93 94 setIconBaseWithExtension("org/netbeans/modules/properties/propertiesKey.gif"); 96 updateShortDescription(); 98 99 PropertiesDataObject pdo = ((PropertiesDataObject)propStructure.getParent().getEntry().getDataObject()); 101 102 getCookieSet().add(pdo.getOpenSupport().new PropertiesOpenAt(propStructure.getParent().getEntry(), itemKey)); 103 getCookieSet().add(propStructure.getParent().getEntry().getPropertiesEditor().new PropertiesEditAt(itemKey)); 104 105 Element.ItemElem item = getItem(); 106 PropertyChangeListener pcl = WeakListeners.propertyChange(this, item); 107 item.addPropertyChangeListener(pcl); 108 } 109 110 public Action getPreferredAction() { 111 return getActions(false)[0]; 112 } 113 114 117 public Element.ItemElem getItem() { 118 return propStructure.getItem(itemKey); 119 } 120 121 124 public boolean canDestroy () { 125 return true; 126 } 127 128 129 public void destroy () throws IOException { 130 propStructure.deleteItem(itemKey); 131 super.destroy (); 132 } 133 134 137 public final boolean canCopy () { 138 return true; 139 } 140 141 144 public final boolean canCut () { 145 return true; 146 } 147 148 151 public final boolean canRename () { 152 return true; 153 } 154 155 158 public void setName(final String name) { 159 if(name.equals(itemKey)) return; 161 162 String oldKey = itemKey; 163 itemKey = name; 164 if (false == propStructure.renameItem(oldKey, name)) { 165 itemKey = oldKey; 166 NotifyDescriptor.Message msg = new NotifyDescriptor.Message( 167 NbBundle.getBundle(KeyNode.class).getString("MSG_CannotRenameKey"), 168 NotifyDescriptor.ERROR_MESSAGE 169 ); 170 DialogDisplayer.getDefault().notify(msg); 171 return; 172 } 173 174 updateCookieNames(); 175 } 176 177 180 protected Sheet createSheet () { 181 Sheet sheet = Sheet.createDefault (); 182 Sheet.Set sheetSet = sheet.get (Sheet.PROPERTIES); 183 184 Node.Property property; 185 186 property = new PropertySupport.ReadWrite<String >( 188 PROP_NAME, 189 String .class, 190 NbBundle.getBundle(KeyNode.class).getString("PROP_item_key"), 191 NbBundle.getBundle(KeyNode.class).getString("HINT_item_key") 192 ) { 193 public String getValue() { 194 return itemKey; 195 } 196 197 public void setValue(String val) throws IllegalAccessException , IllegalArgumentException , InvocationTargetException { 198 KeyNode.this.setName(val); 199 } 200 }; 201 property.setName(Element.ItemElem.PROP_ITEM_KEY); 202 sheetSet.put (property); 203 204 property = new PropertySupport.ReadWrite<String >( 206 Element.ItemElem.PROP_ITEM_VALUE, 207 String .class, 208 NbBundle.getBundle(KeyNode.class).getString("PROP_item_value"), 209 NbBundle.getBundle(KeyNode.class).getString("HINT_item_value") 210 ) { 211 public String getValue() { 212 return getItem().getValue(); 213 } 214 215 public void setValue(String val) throws IllegalAccessException , 216 IllegalArgumentException , InvocationTargetException { 217 getItem().setValue(val); 218 } 219 }; 220 property.setName(Element.ItemElem.PROP_ITEM_VALUE); 221 sheetSet.put (property); 222 223 property = new PropertySupport.ReadWrite<String >( 225 Element.ItemElem.PROP_ITEM_COMMENT, 226 String .class, 227 NbBundle.getBundle(KeyNode.class).getString("PROP_item_comment"), 228 NbBundle.getBundle(KeyNode.class).getString("HINT_item_comment") 229 ) { 230 public String getValue() { 231 return getItem().getComment(); 232 } 233 234 public void setValue(String val) throws IllegalAccessException , 235 IllegalArgumentException , InvocationTargetException { 236 getItem().setComment(val); 237 } 238 }; 239 property.setName(Element.ItemElem.PROP_ITEM_COMMENT); 240 sheetSet.put (property); 241 242 return sheet; 243 } 244 245 246 @SuppressWarnings ("unchecked") 247 public <T extends Node.Cookie> T getCookie(Class <T> clazz) { 248 if (clazz.isInstance(getItem())) { 249 return (T) getItem(); 250 } 251 if (clazz.equals(SaveCookie.class)) { 252 return propStructure.getParent().getEntry().getCookie(clazz); 253 } 254 return super.getCookie(clazz); 255 } 256 257 259 private void updateShortDescription() { 260 String description; 261 262 Element.ItemElem item = getItem(); 263 264 if(item != null) { 265 String comment = item.getComment(); 266 if (comment != null) { 267 int displayLenght = Math.min(comment.length(),72); 268 description = comment.substring(0, displayLenght); 269 if (displayLenght < comment.length()) { 270 description += "..."; } 272 } else { 273 description = item.getKey() + "=" + item.getValue(); } 275 } else { 276 description = itemKey; 277 } 278 279 setShortDescription(description); 280 } 281 282 284 public boolean hasCustomizer() { 285 return true; 286 } 287 288 290 public Component getCustomizer() { 291 return new PropertyPanel(getItem()); 292 } 293 294 295 private void updateCookieNames() { 296 Node.Cookie opener = getCookie(OpenCookie.class); 298 if(opener instanceof PropertiesOpen.PropertiesOpenAt) { 299 ((PropertiesOpen.PropertiesOpenAt)opener).setKey(itemKey); 300 } 301 302 Node.Cookie editor = getCookie(EditCookie.class); 304 if(editor instanceof PropertiesEditorSupport.PropertiesEditAt) { 305 ((PropertiesEditorSupport.PropertiesEditAt)editor).setKey(itemKey); 306 } 307 } 308 309 312 private void setActions(SystemAction[] actions) { 313 systemActions = actions; 314 } 315 316 321 322 public void propertyChange(PropertyChangeEvent evt) { 323 if (Element.ItemElem.PROP_ITEM_COMMENT.equals(evt.getPropertyName())) { 324 updateShortDescription(); 325 } 326 else if (Element.ItemElem.PROP_ITEM_VALUE.equals(evt.getPropertyName())) { 327 updateShortDescription(); 328 } 329 } 330 331 } 332 | Popular Tags |