1 19 20 package org.netbeans.modules.dbschema; 21 22 24 public abstract class KeyElement extends DBMemberElement implements ColumnElementHolder { 25 27 public KeyElement() { 28 this(new Memory(), null); 29 } 30 31 36 public KeyElement(KeyElement.Impl impl, TableElement declaringTable) { 37 super(impl, declaringTable); 38 } 39 40 43 final Impl getKeyImpl() { 44 return (Impl)getElementImpl(); 45 } 46 47 49 53 public void addColumn(ColumnElement el) throws DBException { 54 addColumns(new ColumnElement[]{el}); 55 } 56 57 61 public void addColumns(final ColumnElement[] els) throws DBException { 62 for (int i = 0; i < els.length; i++) 63 if (getColumn(els[i].getName()) != null) 64 throwAddException("FMT_EXC_AddColumn", els[i]); 66 getKeyImpl().changeColumns(els, TableElement.Impl.ADD); 67 } 68 69 73 public void removeColumn(ColumnElement el) throws DBException { 74 removeColumns(new ColumnElement[]{el}); 75 } 76 77 81 public void removeColumns(final ColumnElement[] els) throws DBException { 82 getKeyImpl().changeColumns(els, TableElement.Impl.REMOVE); 83 } 84 85 90 public void setColumns(ColumnElement[] els) throws DBException { 91 getKeyImpl().changeColumns(els, TableElement.Impl.SET); 92 } 93 94 97 public ColumnElement[] getColumns() { 98 return getKeyImpl().getColumns(); 99 } 100 101 105 public ColumnElement getColumn(DBIdentifier name) { 106 return getKeyImpl().getColumn(name); 107 } 108 109 115 private void throwAddException(String formatKey, ColumnElement element) throws DBException { 116 String msg = element.getName().getName(); throw new DBException(msg); 119 } 120 121 124 public interface Impl extends DBMemberElement.Impl { 125 130 public void changeColumns(ColumnElement[] elems, int action) throws DBException; 131 132 135 public ColumnElement[] getColumns(); 136 137 141 public ColumnElement getColumn(DBIdentifier name); 142 } 143 144 static class Memory extends DBMemberElement.Memory implements Impl { 145 146 private DBMemoryCollection.Column columns; 147 148 150 Memory() { 151 } 152 153 156 Memory(KeyElement key) { 157 super(key); 158 } 159 160 164 public synchronized void changeColumns(ColumnElement[] elems, int action) { 165 initColumns(); 166 columns.change(elems, action); 167 } 168 169 172 public synchronized ColumnElement[] getColumns() { 173 initColumns(); 174 return (ColumnElement[])columns.getElements(); 175 } 176 177 181 public synchronized ColumnElement getColumn(DBIdentifier name) { 182 initColumns(); 183 return (ColumnElement)columns.getElement(name); 184 } 185 186 188 void initColumns() { 189 if (columns == null) 190 columns = new DBMemoryCollection.Column(this); 191 } 192 } 193 } 194 | Popular Tags |