1 19 20 package org.netbeans.modules.db.explorer.nodes; 21 22 import java.awt.datatransfer.Transferable ; 23 import java.io.IOException ; 24 import java.util.*; 25 import java.text.MessageFormat ; 26 27 28 import org.openide.*; 29 import org.openide.nodes.NodeTransfer; 30 import org.openide.nodes.Node; 31 import org.openide.util.datatransfer.PasteType; 32 import org.openide.util.NbBundle; 33 34 import org.netbeans.lib.ddl.*; 35 import org.netbeans.lib.ddl.impl.*; 36 import org.netbeans.modules.db.explorer.*; 37 import org.netbeans.modules.db.explorer.infos.*; 38 import org.openide.util.Lookup; 39 import org.openide.util.datatransfer.ExTransferable; 40 41 43 public class TableNode extends DatabaseNode { 44 public void setInfo(DatabaseNodeInfo nodeinfo) 45 { 46 super.setInfo(nodeinfo); 47 getCookieSet().add(this); 48 } 49 50 88 89 public void setName(String newname) 90 { 91 try { 92 DatabaseNodeInfo info = getInfo(); 93 Specification spec = (Specification)info.getSpecification(); 94 AbstractCommand cmd = spec.createCommandRenameTable(info.getName(), newname); 95 cmd.setObjectOwner((String )info.get(DatabaseNodeInfo.SCHEMA)); 96 cmd.execute(); 97 super.setName(newname); 98 info.put(DatabaseNode.TABLE, newname); 99 } catch (CommandNotSupportedException ex) { 100 DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(ex.getMessage(), NotifyDescriptor.ERROR_MESSAGE)); 101 } catch (Exception ex) { 102 DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(ex.getMessage(), NotifyDescriptor.ERROR_MESSAGE)); 104 } 105 } 106 107 public Transferable clipboardCopy() throws IOException { 108 Transferable result; 109 final DbMetaDataTransferProvider dbTansferProvider = (DbMetaDataTransferProvider)Lookup.getDefault().lookup(DbMetaDataTransferProvider.class); 110 if (dbTansferProvider != null) { 111 ExTransferable exTransferable = ExTransferable.create(super.clipboardCopy()); 112 ConnectionNodeInfo cni = (ConnectionNodeInfo)getInfo().getParent(DatabaseNode.CONNECTION); 113 final DatabaseConnection dbconn = ConnectionList.getDefault().getConnection(cni.getDatabaseConnection()); 114 exTransferable.put(new ExTransferable.Single(dbTansferProvider.getTableDataFlavor()) { 115 protected Object getData() { 116 return dbTansferProvider.createTableData(dbconn.getDatabaseConnection(), dbconn.findJDBCDriver(), getInfo().getName()); 117 } 118 }); 119 result = exTransferable; 120 } else { 121 result = super.clipboardCopy(); 122 } 123 return result; 124 } 125 126 protected void createPasteTypes(Transferable t, List s) 127 { 128 super.createPasteTypes(t, s); 129 DatabaseNodeInfo nfo; 130 Node n = NodeTransfer.node(t, NodeTransfer.MOVE); 131 if (n != null && n.canDestroy ()) { 132 139 nfo = (ColumnNodeInfo)n.getCookie(ColumnNodeInfo.class); 140 if (nfo != null) { 141 s.add(new ColumnPasteType((ColumnNodeInfo)nfo, n)); 142 return; 143 } 144 145 } else { 146 153 nfo = (DatabaseNodeInfo)NodeTransfer.cookie(t, NodeTransfer.MOVE, ColumnNodeInfo.class); 154 if (nfo != null) { 155 s.add(new ColumnPasteType((ColumnNodeInfo)nfo, null)); 156 return; 157 } 158 } 159 } 160 161 163 private class TablePasteType extends PasteType 164 { 165 166 private DatabaseNodeInfo info; 167 168 169 private Node node; 170 171 173 public TablePasteType(TableNodeInfo info, Node node) 174 { 175 this.info = info; 176 this.node = node; 177 } 178 179 180 public String getName() 181 { 182 ResourceBundle bundle = NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle"); return bundle.getString("PasteTableName"); } 185 186 191 public Transferable paste() throws IOException 192 { 193 TableNodeInfo info = (TableNodeInfo)getInfo(); 194 ResourceBundle bundle = NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle_noi18n"); TableListNodeInfo ownerinfo = (TableListNodeInfo)getInfo().getParent(DatabaseNode.TABLELIST); 196 if (info != null) { 197 TableNodeInfo exinfo = ownerinfo.getChildrenTableInfo(info); 198 DatabaseNodeChildren chi = (DatabaseNodeChildren)getChildren(); 199 String name = info.getName(); 200 if (exinfo != null) { 201 String namefmt = bundle.getString("PasteTableNameFormat"); name = MessageFormat.format(namefmt, new String [] {name}); 203 } 204 205 try { 206 207 210 ownerinfo.addTable(name); 211 if (node != null) node.destroy (); 212 213 } catch (Exception e) { 214 throw new IOException (e.getMessage()); 215 } 216 217 } else 218 throw new IOException (bundle.getString("EXC_CannotFindTableOwnerInformation")); 220 return null; 221 } 222 } 223 224 226 private class ColumnPasteType extends PasteType { 227 final ResourceBundle bundle = NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle"); 229 230 private DatabaseNodeInfo info; 231 232 233 private Node node; 234 235 237 public ColumnPasteType(ColumnNodeInfo info, Node node) 238 { 239 this.info = info; 240 this.node = node; 241 } 242 243 244 public String getName() { 245 return bundle.getString("PasteColumnName"); } 247 248 253 public Transferable paste() throws IOException 254 { 255 ResourceBundle bundle = NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle"); TableNodeInfo ownerinfo = (TableNodeInfo)getInfo(); 257 if (info != null) { 258 try { 259 String name = info.getName(); 260 ColumnNodeInfo coli = (ColumnNodeInfo)info; 261 TableColumn col = coli.getColumnSpecification(); 262 Specification spec = (Specification)ownerinfo.getSpecification(); 263 AddColumn cmd = (AddColumn)spec.createCommandAddColumn(ownerinfo.getTable()); 264 cmd.getColumns().add(col); 265 cmd.setObjectOwner((String )info.get(DatabaseNodeInfo.SCHEMA)); 266 cmd.execute(); 267 ownerinfo.addColumn(name); 268 if (node != null) node.destroy(); 269 } catch (final Exception ex) { 270 ex.printStackTrace(); 271 278 } 279 } else throw new IOException (bundle.getString("EXC_CannotFindColumnOwnerInformation")); return null; 281 } 282 } 283 284 public String getShortDescription() { 285 return NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("ND_Table"); } 287 288 } 289 | Popular Tags |