1 19 20 package org.netbeans.modules.properties; 21 22 import java.awt.Component ; 23 import java.awt.datatransfer.Transferable ; 24 import java.awt.Dialog ; 25 import java.awt.event.ActionEvent ; 26 import java.awt.event.ActionListener ; 27 import java.io.IOException ; 28 import java.text.MessageFormat ; 29 import java.util.List ; 30 import javax.swing.Action ; 31 import javax.swing.JPanel ; 32 33 import org.openide.actions.*; 34 import org.openide.DialogDescriptor; 35 import org.openide.filesystems.FileObject; 36 import org.openide.filesystems.FileUtil; 37 import org.openide.nodes.CookieSet; 38 import org.openide.nodes.Node; 39 import org.openide.nodes.NodeTransfer; 40 import org.openide.NotifyDescriptor; 41 import org.openide.DialogDisplayer; 42 import org.openide.util.actions.SystemAction; 43 import org.openide.util.datatransfer.NewType; 44 import org.openide.util.datatransfer.PasteType; 45 import org.openide.util.HelpCtx; 46 import org.openide.util.NbBundle; 47 48 49 55 public final class PropertiesLocaleNode extends FileEntryNode 56 implements CookieSet.Factory, 57 Node.Cookie { 58 59 60 private static final String LOCALE_ICON_BASE = "org/netbeans/modules/properties/propertiesLocale.gif"; 62 63 64 public PropertiesLocaleNode (PropertiesFileEntry fe) { 65 super(fe, fe.getChildren()); 66 setDisplayName(Util.getLocaleLabel(fe)); 67 68 setIconBaseWithExtension(LOCALE_ICON_BASE); 69 setShortDescription(messageToolTip()); 70 71 getCookieSet().add(PropertiesOpen.class, this); 72 getCookieSet().add(fe.getDataObject()); 73 } 74 75 76 @SuppressWarnings ("unchecked") 77 public <T extends Node.Cookie> T createCookie(Class <T> clazz) { 78 if(clazz.isAssignableFrom(PropertiesOpen.class)) { 79 return (T) ((PropertiesDataObject) getFileEntry().getDataObject()).getOpenSupport(); 80 } else { 81 return null; 82 } 83 } 84 85 90 protected SystemAction[] createActions () { 91 return new SystemAction[] { 92 SystemAction.get(EditAction.class), 93 SystemAction.get(OpenAction.class), 94 SystemAction.get(FileSystemAction.class), 95 null, 96 SystemAction.get(CutAction.class), 97 SystemAction.get(CopyAction.class), 98 SystemAction.get(PasteAction.class), 99 null, 100 SystemAction.get(DeleteAction.class), 101 SystemAction.get(LangRenameAction.class), 102 null, 103 SystemAction.get(NewAction.class), 104 SystemAction.get(SaveAsTemplateAction.class), 105 null, 106 SystemAction.get(ToolsAction.class), 107 SystemAction.get(PropertiesAction.class) 108 }; 109 } 110 111 public Action getPreferredAction() { 112 return getActions(false)[0]; 113 } 114 115 121 public String getName() { 122 String localeName = "invalid"; if (getFileEntry().getFile().isValid() && !getFileEntry().getFile().isVirtual()) { 124 localeName = Util.getLocaleSuffix (getFileEntry()); 125 if (localeName.length() > 0) 126 if (localeName.charAt(0) == PropertiesDataLoader.PRB_SEPARATOR_CHAR) 127 localeName = localeName.substring(1); 128 } 129 return localeName; 130 } 131 132 136 public void setName (String name) { 137 if(!name.startsWith(getFileEntry().getDataObject().getPrimaryFile().getName())) { 138 name = Util.assembleName (getFileEntry().getDataObject().getPrimaryFile().getName(), name); 139 } 140 141 if (name.equals(super.getName())) return; 143 144 super.setName (name); 145 setDisplayName(Util.getLocaleLabel(getFileEntry())); 146 setShortDescription(messageToolTip()); 147 } 148 149 150 private String messageToolTip () { 151 FileObject fo = getFileEntry().getFile(); 152 return FileUtil.getFileDisplayName(fo); 153 } 154 155 156 public boolean canRename() { 157 return getFileEntry().isDeleteAllowed (); 158 } 159 160 161 @SuppressWarnings ("unchecked") 162 public <T extends Node.Cookie> T getCookie(Class <T> cls) { 163 if (cls.isInstance(getFileEntry())) return (T) getFileEntry(); 164 if (cls == PropertiesLocaleNode.class) return (T) this; 165 return super.getCookie(cls); 166 } 167 168 171 public NewType[] getNewTypes () { 172 return new NewType[] { 173 new NewType() { 174 175 176 public String getName() { 177 return NbBundle.getBundle(PropertiesLocaleNode.class).getString("LAB_NewPropertyAction"); 178 } 179 180 181 public HelpCtx getHelpCtx() { 182 return new HelpCtx(Util.HELP_ID_ADDING); 183 } 184 185 186 public void create() throws IOException { 187 final Dialog [] dialog = new Dialog [1]; 188 final Element.ItemElem item = new Element.ItemElem( 189 null, 190 new Element.KeyElem(null, ""), new Element.ValueElem(null, ""), new Element.CommentElem(null, "") ); 194 final JPanel panel = new PropertyPanel(item); 195 196 DialogDescriptor dd = new DialogDescriptor( 197 panel, 198 NbBundle.getBundle(PropertiesLocaleNode.class).getString("CTL_NewPropertyTitle"), 199 true, 200 DialogDescriptor.OK_CANCEL_OPTION, 201 DialogDescriptor.OK_OPTION, 202 new ActionListener () { 203 private boolean bulkFlag = false; 204 public void actionPerformed(ActionEvent evt) { 205 206 if (bulkFlag) return; 208 bulkFlag =true; 209 210 if(evt.getSource() == DialogDescriptor.OK_OPTION) { 212 dialog[0].setVisible(false); 213 dialog[0].dispose(); 214 215 216 String key = item.getKey(); 217 String value = item.getValue(); 218 String comment = item.getComment(); 219 220 if(!((PropertiesFileEntry)getFileEntry()).getHandler().getStructure().addItem(key, value, comment)) { 222 NotifyDescriptor.Message msg = new NotifyDescriptor.Message( 223 MessageFormat.format( 224 NbBundle.getBundle(PropertiesLocaleNode.class).getString("MSG_KeyExists"), 225 new Object [] { 226 item.getKey(), 227 Util.getLocaleLabel(getFileEntry()) 228 } 229 ), 230 NotifyDescriptor.ERROR_MESSAGE); 231 DialogDisplayer.getDefault().notify(msg); 232 } 233 234 } else if (evt.getSource() == DialogDescriptor.CANCEL_OPTION) { 236 dialog[0].setVisible(false); 237 dialog[0].dispose(); 238 } 239 } 240 } 241 ); 242 243 dialog[0] = DialogDisplayer.getDefault().createDialog(dd); 244 dialog[0].setVisible(true); 245 246 } 247 248 } }; 250 } 251 252 254 public boolean hasCustomizer() { 255 return true; 256 } 257 258 259 public Component getCustomizer() { 260 return new LocaleNodeCustomizer((PropertiesFileEntry)getFileEntry()); 261 } 262 263 264 protected void createPasteTypes(Transferable t, List <PasteType> s) { 265 super.createPasteTypes(t, s); 266 Element.ItemElem item; 267 Node n = NodeTransfer.node(t, NodeTransfer.MOVE); 268 if (n != null && n.canDestroy ()) { 270 item = n.getCookie(Element.ItemElem.class); 271 if (item != null) { 272 Node n2 = getChildren().findChild(item.getKey()); 274 if (n == n2) { 275 return; 276 } 277 s.add(new KeyPasteType(item, n, KeyPasteType.MODE_PASTE_WITH_VALUE)); 278 s.add(new KeyPasteType(item, n, KeyPasteType.MODE_PASTE_WITHOUT_VALUE)); 279 return; 280 } 281 } 282 else { 284 item = NodeTransfer.cookie(t, NodeTransfer.COPY, Element.ItemElem.class); 285 if (item != null) { 286 s.add(new KeyPasteType(item, null, KeyPasteType.MODE_PASTE_WITH_VALUE)); 287 s.add(new KeyPasteType(item, null, KeyPasteType.MODE_PASTE_WITHOUT_VALUE)); 288 return; 289 } 290 } 291 } 292 293 294 295 private class KeyPasteType extends PasteType { 296 297 298 private Element.ItemElem item; 299 300 301 private Node node; 302 303 304 int mode; 305 306 307 public static final int MODE_PASTE_WITH_VALUE = 1; 308 309 310 public static final int MODE_PASTE_WITHOUT_VALUE = 2; 311 312 313 314 public KeyPasteType(Element.ItemElem item, Node node, int mode) { 315 this.item = item; 316 this.node = node; 317 this.mode = mode; 318 } 319 320 322 public String getName() { 323 String pasteKey = mode == 1 ? "CTL_PasteKeyValue" : "CTL_PasteKeyNoValue"; 324 return NbBundle.getBundle(PropertiesLocaleNode.class).getString(pasteKey); 325 } 326 327 332 public Transferable paste() throws IOException { 333 PropertiesStructure ps = ((PropertiesFileEntry)getFileEntry()).getHandler().getStructure(); 334 String value; 335 if (mode == MODE_PASTE_WITH_VALUE) 336 value = item.getValue(); 337 else 338 value = ""; 339 if (ps != null) { 340 Element.ItemElem newItem = ps.getItem(item.getKey()); 341 if (newItem == null) { 342 ps.addItem(item.getKey(), value, item.getComment()); 343 } 344 else { 345 newItem.setValue(value); 346 newItem.setComment(item.getComment()); 347 } 348 if (node != null) 349 node.destroy(); 350 } 351 352 return null; 353 } 354 } 356 } 357 | Popular Tags |