1 19 20 package org.netbeans.modules.db.explorer.nodes; 21 22 import java.io.IOException ; 23 import java.util.*; 24 25 import javax.swing.Action ; 26 27 import org.openide.*; 28 import org.openide.nodes.*; 29 import org.openide.util.HelpCtx; 30 import org.openide.util.NbBundle; 31 32 import org.netbeans.modules.db.explorer.*; 33 import org.netbeans.modules.db.explorer.actions.*; 34 import org.netbeans.api.db.explorer.DatabaseException; 35 import org.netbeans.modules.db.explorer.infos.DatabaseNodeInfo; 36 37 public class DatabaseNode extends AbstractNode implements Node.Cookie { 38 39 40 protected DatabaseNodeInfo info; 41 42 43 private boolean writable = false; 44 private boolean cutflag = false, copyflag = false, delflag = false; 45 46 47 public static final String ROOT = "root"; public static final String DRIVER_LIST = "driverlist"; public static final String DRIVER = "driver"; public static final String CONNECTION = "connection"; public static final String CATALOG = "catalog"; public static final String TABLELIST = "tablelist"; public static final String TABLE = "table"; public static final String VIEW = "view"; public static final String VIEWLIST = "viewlist"; public static final String VIEWCOLUMN = "viewcolumn"; public static final String INDEX = "index"; public static final String COLUMN = "column"; public static final String INDEXCOLUMN = "indexcolumn"; public static final String PRIMARY_KEY = "pcolumn"; public static final String INDEXED_COLUMN = "icolumn"; public static final String FOREIGN_COLUMN = "fcolumn"; public static final String FOREIGN_KEY = "fcolumn"; public static final String EXPORTED_KEY = "ekey"; public static final String IMPORTED_KEY = "fkey"; public static final String PROCEDURE = "procedure"; public static final String PROCEDURELIST = "procedurelist"; public static final String PROCEDURE_COLUMN = "procedurecolumn"; 70 71 public DatabaseNode() 72 { 73 super(new DatabaseNodeChildren()); 74 } 75 76 77 public DatabaseNode(Children child) 78 { 79 super(child); 80 } 81 82 83 public DatabaseNodeInfo getInfo() 84 { 85 return info; 86 } 87 88 89 public void setInfo(DatabaseNodeInfo nodeinfo) 90 { 91 info = (DatabaseNodeInfo)nodeinfo.clone(); 92 processInfo(); 93 } 94 95 protected void processInfo() { 96 super.setName(info.getName()); 97 setIconBase(info.getIconBase()); 98 99 102 Map opts = (Map)info.get("options"); if (opts != null) { 104 String str = (String )opts.get("cut"); if (str != null) cutflag = str.toUpperCase().equals("YES"); str = (String )opts.get("copy"); if (str != null) copyflag = str.toUpperCase().equals("YES"); str = (String )opts.get("delete"); if (str != null) delflag = str.toUpperCase().equals("YES"); } 111 112 try { 113 Vector prop = (Vector)info.get(DatabaseNodeInfo.PROPERTIES); 114 Enumeration prop_i = prop.elements(); 115 while (prop_i.hasMoreElements()) { 116 Map propmap = (Map)prop_i.nextElement(); 117 if (((String )propmap.get(DatabaseNodeInfo.CODE)).equals(DatabaseNodeInfo.NAME)) { 118 writable = ((String )propmap.get(DatabaseNodeInfo.WRITABLE)).toUpperCase().equals("YES"); } 120 } 121 } catch (Exception e) {} 122 } 123 124 125 public void setName(String newname) 126 { 127 super.setName(newname); 128 info.setName(newname); 129 } 130 131 public boolean canRename() 132 { 133 return writable; 134 } 135 136 139 public boolean canCut () 140 { 141 return cutflag; 142 } 143 144 147 public boolean canCopy () 148 { 149 return copyflag; 150 } 151 152 155 public boolean canDestroy() 156 { 157 return delflag; 158 } 159 160 public void destroy() throws IOException 161 { 162 info.delete(); 163 DatabaseNodeInfo parent = info.getParent(); 164 super.destroy(); 165 try{ 166 parent.refreshChildren(); 167 } catch (Exception ex){ 168 org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.INFORMATIONAL, ex); 169 } 170 } 171 172 public Node.Cookie getCookie(Class cls) 173 { 174 if (cls.isInstance(info)) return info; 175 return super.getCookie(cls); 176 } 177 178 public Action [] getActions(boolean context) { 179 if (context) { 180 return getContextActions(); 181 } 182 Vector actions = info.getActions(); 183 if (actions.size() > 0) { 184 return (Action []) actions.toArray(new Action [actions.size()]); 185 } 186 return new Action [0]; 187 } 188 189 protected Map createProperty(String name) 190 { 191 return null; 192 } 193 194 protected PropertySupport createPropertySupport(String name, Class type, String displayName, String shortDescription, DatabaseNodeInfo rep, boolean writable) 195 { 196 return new DatabasePropertySupport(name, type, displayName, shortDescription, rep, writable); 197 } 198 199 protected PropertySupport createPropertySupport(String name, Class type, String displayName, String shortDescription, DatabaseNodeInfo rep, boolean writable, boolean expert) 200 { 201 PropertySupport ps = new DatabasePropertySupport(name, type, displayName, shortDescription, rep, writable); 202 ps.setExpert(expert); 203 return ps; 204 } 205 206 208 protected Sheet createSheet() 209 { 210 Sheet sheet = Sheet.createDefault(); 211 Sheet.Set ps = sheet.get(Sheet.PROPERTIES); 212 Vector prop = (Vector)info.get(DatabaseNodeInfo.PROPERTIES); 213 Enumeration prop_i = prop.elements(); 214 while (prop_i.hasMoreElements()) { 215 boolean canWrite, expert = false; 216 Map propmap = (Map)prop_i.nextElement(); 217 String key = (String )propmap.get(DatabaseNodeInfo.CODE); 218 String expkey = (String )propmap.get("expert"); if (expkey != null) expert = expkey.toUpperCase().equals("YES"); 221 try { 222 223 PropertySupport psitem = null; 224 String pname = null, pclass = null, pdesc = null; 225 if (propmap == null) { 226 propmap = createProperty(key); 227 if (propmap != null) info.put(key, propmap); 228 } 229 230 if (key.equals("name")) { if (!info.isReadOnly()) psitem = new PropertySupport.Name(this); 232 } else { 233 Class pc = null; 234 pname = (String )propmap.get(DatabaseNodeInfo.NAME); 235 if (info.canAdd(propmap, pname)) { 236 pclass = (String )propmap.get(DatabaseNodeInfo.CLASS); 237 canWrite = info.canWrite(propmap, pname, writable); 238 if (pclass.equals("java.lang.Boolean")) pc = Boolean .class; else if (pclass.equals("java.lang.Integer")) pc = Integer.TYPE; else pc = Class.forName(pclass); 241 242 try { 243 pname = NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString(pname); 244 } catch (MissingResourceException e) { 245 pdesc = NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("DatabaseNodeUntitled"); } 247 248 psitem = createPropertySupport(key, pc, pname, pdesc, info, canWrite, expert); 249 } 250 } 251 252 if (psitem != null) ps.put(psitem); 253 255 } catch (Exception ex) { 256 ex.printStackTrace(); 257 } 258 } 259 260 return sheet; 261 } 262 263 267 protected void deleteNode(DatabaseNode node) 268 throws DatabaseException 269 { 270 try { 271 DatabaseNodeInfo ninfo = node.getInfo(); 272 DatabaseNodeChildren children = (DatabaseNodeChildren)getChildren(); 273 info.getChildren().removeElement(ninfo); 274 children.remove(new Node[] {node}); 275 } catch (Exception e) { 276 throw new DatabaseException(e.getMessage()); 277 } 278 } 279 280 283 public void deleteNode() 284 throws DatabaseException 285 { 286 try { 287 DatabaseNode parent = (DatabaseNode)getParentNode().getCookie(null); 288 if (parent != null) parent.deleteNode(this); 289 } catch (Exception e) { 290 throw new DatabaseException(e.getMessage()); 291 } 292 } 293 294 public HelpCtx getHelpCtx () { 295 return new HelpCtx ("dbexpovew"); 296 } 297 298 public String getShortDescription() { 299 String code = getInfo().getCode(); 300 301 if (code.equals(DatabaseNode.INDEXCOLUMN)) 302 return NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("ND_Column"); else if (code.equals("fklist")) 304 return NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("ND_ForeignKeyList"); else if (code.equals("ilist")) 306 return NbBundle.getBundle("org.netbeans.modules.db.resources.Bundle").getString("ND_IndexList"); else 308 return ""; } 310 311 } 312 | Popular Tags |