1 19 package org.netbeans.modules.db.sql.visualeditor.querybuilder; 20 21 import org.openide.ErrorManager; 22 23 import org.openide.nodes.AbstractNode; 24 import org.openide.nodes.Sheet; 25 import org.openide.nodes.Children; 26 import org.openide.nodes.PropertySupport; 27 import org.openide.nodes.PropertySupport.Reflection; 28 29 import org.openide.util.NbBundle; 30 import org.openide.ErrorManager; 31 import org.openide.NotifyDescriptor; 32 import org.openide.DialogDisplayer; 33 34 import org.netbeans.modules.db.sql.visualeditor.Log; 35 36 42 43 public class TableNode extends AbstractNode 44 { 45 private int SQL_IDENTIFIER_LENGTH = 32; 47 48 private boolean DEBUG = false; 49 private String _fullTableName=null; 50 private String _corrName=null; 51 private QueryBuilder _queryBuilder; 52 53 55 TableNode(String fullTableName) 56 { 57 super(Children.LEAF); 58 _fullTableName = fullTableName; 59 } 60 61 TableNode(String fullTableName, String corrName, QueryBuilder queryBuilder) 62 { 63 super(Children.LEAF); 64 _fullTableName = fullTableName; 65 _corrName = corrName; 66 _queryBuilder = queryBuilder; 67 } 68 69 71 public String getTableName() { 72 return _fullTableName; 73 } 74 75 public String getCorrName() { 76 return _corrName; 77 } 78 79 public void setCorrName(String corrName) { 80 81 Log.err.log(ErrorManager.INFORMATIONAL, "Entering TableNode.setCorrName, corrname: " + corrName); 83 String oldCorrName = getCorrName(); 85 String oldTableSpec = (oldCorrName==null) ? getTableName() : oldCorrName; 86 87 if ( ((corrName == null) && (oldCorrName==null)) || 89 ((corrName !=null) && (corrName.equals(oldCorrName)))) 90 return; 91 92 if (corrName.trim().length()==0) { 94 _corrName=null; 95 } 96 else { 97 if ( ! isAliasValid ( corrName.trim() ) ) { 101 String msg = NbBundle.getMessage(TableNode.class, "INVALID_ALIAS"); NotifyDescriptor d = new NotifyDescriptor.Message(msg + "\n\n" +corrName, NotifyDescriptor.ERROR_MESSAGE); DialogDisplayer.getDefault().notify(d); 106 return; 107 } 108 String tmp = _queryBuilder._queryModel.genUniqueName(corrName); 109 _corrName= (tmp==null) ? corrName : tmp; 110 } 111 112 _queryBuilder._queryModel.renameTableSpec(oldTableSpec, _corrName); 114 116 _queryBuilder.generate(); 118 } 119 120 122 protected Sheet createSheet() { 123 Sheet s = Sheet.createDefault(); 124 Sheet.Set ss = s.get(Sheet.PROPERTIES); 125 try { 126 PropertySupport.Reflection p; 127 p = new Reflection(this, String .class, "getTableName", null); p.setName("tableName"); String tableDisplayName = NbBundle.getMessage(TableNode.class, "TABLE_DISPLAY_NAME"); p.setDisplayName(tableDisplayName); 132 133 String tableShortDescription = NbBundle.getMessage(TableNode.class, "TABLE_SHORT_DESCRIPTION"); p.setShortDescription(tableShortDescription); 136 ss.put(p); 137 p = new Reflection(this, String .class, "getCorrName", "setCorrName"); p.setName("aliasName"); String aliasDisplayName = NbBundle.getMessage(TableNode.class, "ALIAS_DISPLAY_NAME"); p.setDisplayName(aliasDisplayName); 142 String aliasShortDescription = NbBundle.getMessage(TableNode.class, "ALIAS_SHORT_DESCRIPTION"); p.setShortDescription(aliasShortDescription); 145 ss.put(p); 146 } catch (NoSuchMethodException nsme) { 147 ErrorManager.getDefault().notify(nsme); 148 } 149 return s; 150 } 151 152 public boolean isAliasValid ( String aliasName ) { 153 161 187 if ( aliasName.startsWith("\"") && aliasName.endsWith("\"") ) { return isValidDelimitedIdentifier ( aliasName.substring (1, (aliasName.length()-1) ) ) ; 189 } 190 else { 191 return isValidConventionalIdentifier ( aliasName ) ; 192 } 193 } 194 195 boolean isValidDelimitedIdentifier ( String identifier ) { 196 if ( identifier.length() > SQL_IDENTIFIER_LENGTH ) 197 return false; 198 199 return true; 200 } 201 202 boolean isValidConventionalIdentifier ( String identifier ) { 203 204 if ( identifier.length() > SQL_IDENTIFIER_LENGTH ) 205 return false; 206 207 char[] charArray = identifier.toCharArray(); 208 209 if (! Character.isLetter ( charArray[0] ) ) { 211 if (DEBUG) 212 System.out.println("isValidConventionalIdentifier called. charArray[0] = " + charArray[0] + "\n" ); return false; 214 } 215 216 for ( int i=1; i<charArray.length; i++ ) { 217 if ( ( ! Character.isLetter ( charArray [i] ) ) && 219 ( ! Character.isDigit ( charArray [i] ) ) && 220 ( charArray [i] != '_' ) ) { 221 return false; 222 } 223 } 224 return true; 225 } 226 } 227 | Popular Tags |