1 19 20 21 package org.netbeans.modules.properties; 22 23 24 import java.awt.Component ; 25 import java.awt.datatransfer.Transferable ; 26 import java.awt.Dialog ; 27 import java.awt.event.ActionEvent ; 28 import java.awt.event.ActionListener ; 29 import java.beans.PropertyChangeEvent ; 30 import java.beans.PropertyChangeListener ; 31 import java.io.IOException ; 32 import java.text.MessageFormat ; 33 import java.util.List ; 34 35 import org.openide.DialogDescriptor; 36 import org.openide.filesystems.FileLock; 37 import org.openide.filesystems.FileObject; 38 import org.openide.filesystems.FileSystem; 39 import org.openide.filesystems.FileUtil; 40 import org.openide.loaders.DataFolder; 41 import org.openide.loaders.DataNode; 42 import org.openide.loaders.DataObject; 43 import org.openide.nodes.Children; 44 import org.openide.nodes.Node; 45 import org.openide.nodes.NodeTransfer; 46 import org.openide.NotifyDescriptor; 47 import org.openide.DialogDisplayer; 48 import org.openide.cookies.SaveCookie; 49 import org.openide.loaders.FileEntry; 50 import org.openide.util.WeakListeners; 51 import org.openide.util.datatransfer.NewType; 52 import org.openide.util.datatransfer.PasteType; 53 import org.openide.util.NbBundle; 54 55 64 public class PropertiesDataNode extends DataNode { 65 66 70 private final transient PropertyChangeListener dataObjectListener; 71 72 77 public PropertiesDataNode(DataObject dataObject, Children children) { 78 super(dataObject, children); 79 setIconBaseWithExtension("org/netbeans/modules/properties/propertiesObject.png"); 81 dataObjectListener = new NameUpdater(); 82 dataObject.addPropertyChangeListener( 83 WeakListeners.propertyChange(dataObjectListener, dataObject)); 84 } 85 86 94 final class NameUpdater implements PropertyChangeListener { 95 96 98 public void propertyChange(PropertyChangeEvent e) { 99 if (DataObject.PROP_FILES.equals(e.getPropertyName())) { 100 ((PropertiesDataObject) getDataObject()).fireNameChange(); 101 } 102 } 103 104 } 105 106 108 public NewType[] getNewTypes() { 109 return new NewType[] {new NewLocaleType()}; 110 } 111 112 114 public boolean hasCustomizer() { 115 return true; 116 } 117 118 121 public Component getCustomizer() { 122 return new BundleNodeCustomizer((PropertiesDataObject)getDataObject()); 123 } 124 125 128 public void createPasteTypes(Transferable transferable, List <PasteType> types) { 129 super.createPasteTypes(transferable, types); 130 131 int mode = NodeTransfer.COPY; 133 134 Node node = NodeTransfer.node(transferable, mode); 135 136 if(node == null || !(node instanceof PropertiesLocaleNode)) { 137 mode = NodeTransfer.MOVE; 139 140 node = NodeTransfer.node(transferable, mode); 141 142 if(node == null || !(node instanceof PropertiesLocaleNode)) 143 return; 144 145 PropertiesFileEntry entry = (PropertiesFileEntry)((PropertiesLocaleNode)node).getFileEntry(); 146 if(((PropertiesDataObject)getDataObject()).files().contains(entry.getFile())) { 147 return; 148 } 149 } 150 151 PropertiesFileEntry entry = (PropertiesFileEntry)((PropertiesLocaleNode)node).getFileEntry(); 152 types.add(new EntryPasteType(entry, mode)); 153 } 154 155 156 private class EntryPasteType extends PasteType { 157 158 159 private PropertiesFileEntry entry; 160 161 162 private int flag; 163 164 165 168 public EntryPasteType(PropertiesFileEntry entry, int flag) { 169 this.entry = entry; 170 this.flag = flag; 171 } 172 173 175 public Transferable paste() throws IOException { 176 DataFolder dataFolder = PropertiesDataNode.this.getDataObject().getFolder(); 177 178 if(dataFolder == null) 179 return null; 180 181 FileObject folder = dataFolder.getPrimaryFile(); 182 183 String newName = getDataObject().getPrimaryFile().getName() + Util.getLocaleSuffix(entry); 184 185 int entryIndex = ((PropertiesDataObject)getDataObject()).getBundleStructure().getEntryIndexByFileName(newName); 186 187 if(entryIndex != -1) { 189 newName = FileUtil.findFreeFileName(folder, newName, entry.getFile().getExt()); 190 } 191 192 if(flag == NodeTransfer.COPY) { 193 FileObject fileObject = entry.getFile(); 194 fileObject.copy(folder, newName, fileObject.getExt()); 195 196 } else if(flag == NodeTransfer.MOVE) { 197 FileObject fileObject = entry.getFile(); 198 FileLock lock = entry.takeLock(); 199 200 ((PropertiesDataObject) entry.getDataObject()).removeSecondaryEntry2(entry); 202 try { 203 FileObject fo2 = fileObject.move(lock, folder, newName, fileObject.getExt()); 204 try { 205 DataObject.find(fo2); 208 } 209 catch (Exception e) { 210 } 211 } finally { 212 lock.releaseLock (); 213 } 214 } 215 216 return null; 217 } 218 219 } 221 222 223 private class NewLocaleType extends NewType { 224 225 226 public String getName() { 227 return NbBundle.getBundle(PropertiesDataNode.class).getString("LAB_NewLocaleAction"); 228 } 229 230 231 public void create() throws IOException { 232 final PropertiesDataObject propertiesDataObject = (PropertiesDataObject)getCookie(DataObject.class); 233 234 final Dialog [] dialog = new Dialog [1]; 235 final LocalePanel panel = new LocalePanel(); 236 237 DialogDescriptor dialogDescriptor = new DialogDescriptor( 238 panel, 239 NbBundle.getBundle(PropertiesDataNode.class).getString("CTL_NewLocaleTitle"), 240 true, 241 DialogDescriptor.OK_CANCEL_OPTION, 242 DialogDescriptor.OK_OPTION, 243 new ActionListener () { 244 public void actionPerformed(ActionEvent evt) { 245 if (evt.getSource() == DialogDescriptor.OK_OPTION) { 246 Util.createLocaleFile(propertiesDataObject, panel.getLocale().toString(), true); 248 } 249 dialog[0].setVisible(false); 250 dialog[0].dispose(); 251 } 252 } 253 ); 254 255 dialog[0] = DialogDisplayer.getDefault().createDialog(dialogDescriptor); 256 dialog[0].setVisible(true); 257 } 258 259 } 261 } 262 | Popular Tags |