View Javadoc
1 package com.bonevich.erj.ui.editor; 2 3 import com.bonevich.erj.model.*; 4 5 import java.awt.*; 6 import java.awt.event.*; 7 import javax.swing.*; 8 import javax.swing.border.TitledBorder; 9 10 import java.util.List; 11 import java.util.Iterator; 12 import java.util.ArrayList; 13 14 /*** A model editor for attributes. 15 * 16 * @see Attribute 17 * @see ModelEditor 18 */ 19 public final class KeyEditor extends ModelEditor 20 { 21 ////////////////////////////////////////////////////////// 22 // Constants 23 public static final String CONSTRAINT_STR = "Constraint"; 24 public static final String UNIQUEKEY_STR = "Unique Key"; 25 public static final String PRIMARYKEY_STR = "Primary Key"; 26 public static final String ATTR_STR = "Attribute(s)"; 27 28 ////////////////////////////////////////////////////////// 29 // Attributes 30 private KeyConstraint _key; 31 32 private JPanel _consPane; 33 private TitledBorder _consBorder; 34 private JRadioButton _akButton; 35 private JRadioButton _pkButton; 36 37 private JPanel _attrPane; 38 private TitledBorder _attrBorder; 39 private JList _attrList; 40 private AttributeListModel _listModel; 41 42 private String _currentConstraint = UNIQUEKEY_STR; 43 44 ////////////////////////////////////////////////////////// 45 // Constructors 46 public KeyEditor(JTabbedPane parent, Relation relation, KeyConstraint key) 47 { 48 super(parent,relation); 49 setKey(key); 50 updateEditor(); 51 } 52 53 ////////////////////////////////////////////////////////// 54 // Operations 55 protected void initTabs() 56 { 57 // we want the default tab 58 setDefaultLabel("Edit Key Properties"); 59 setDefaultTip("Edit the name, description and other properties of the key"); 60 setDefaultIndex(0); 61 62 super.initTabs(); 63 64 _consPane = new JPanel(); 65 _consBorder = BorderFactory.createTitledBorder(CONSTRAINT_STR); 66 _akButton = new JRadioButton(UNIQUEKEY_STR, false); 67 _pkButton = new JRadioButton(PRIMARYKEY_STR, false); 68 69 _akButton.setMnemonic('a'); 70 _akButton.setSelected(true); 71 _pkButton.setMnemonic('p'); 72 73 ButtonGroup group = new ButtonGroup(); 74 group.add(_akButton); 75 group.add(_pkButton); 76 77 _consBorder.setTitleFont(EDITOR_FONT); 78 _consBorder.setTitleColor(EDITOR_COLOR); 79 _consPane.setBorder(_consBorder); 80 _consPane.setLayout(new BoxLayout(_consPane, BoxLayout.Y_AXIS)); 81 _consPane.setAlignmentX(Component.LEFT_ALIGNMENT); 82 _consPane.setAlignmentY(Component.TOP_ALIGNMENT); 83 _consPane.add(_akButton); 84 _consPane.add(_pkButton); 85 86 _akButton.addItemListener( 87 new ItemListener() 88 { 89 public void itemStateChanged(ItemEvent e) 90 { 91 if (e.getStateChange() == ItemEvent.SELECTED) 92 { 93 _currentConstraint = UNIQUEKEY_STR; 94 toggleNameAndIdentifier( 95 PrimaryKey.NEW_PRIMARYKEY_STR, 96 UniqueKey.NEW_UNIQUEKEY_STR 97 ); 98 } 99 } 100 } 101 ); 102 _pkButton.addItemListener( 103 new ItemListener() 104 { 105 public void itemStateChanged(ItemEvent e) 106 { 107 if (e.getStateChange() == ItemEvent.SELECTED) 108 { 109 _currentConstraint = PRIMARYKEY_STR; 110 toggleNameAndIdentifier( 111 UniqueKey.NEW_UNIQUEKEY_STR, 112 PrimaryKey.NEW_PRIMARYKEY_STR 113 ); 114 } 115 } 116 } 117 ); 118 119 _attrPane = new JPanel(); 120 _attrBorder = BorderFactory.createTitledBorder(ATTR_STR); 121 122 Relation r = (Relation)getOwner(); 123 _listModel = new AttributeListModel(r); 124 _attrList = new JList(_listModel); 125 126 _attrPane.setAlignmentX(Component.RIGHT_ALIGNMENT); 127 _attrPane.setAlignmentY(Component.TOP_ALIGNMENT); 128 _attrList.setAlignmentX(Component.LEFT_ALIGNMENT); 129 _attrBorder.setTitleFont(EDITOR_FONT); 130 _attrBorder.setTitleColor(EDITOR_COLOR); 131 _attrPane.setBorder(_attrBorder); 132 133 JScrollPane scrPane = new JScrollPane(_attrList); 134 scrPane.setAlignmentX(Component.LEFT_ALIGNMENT); 135 scrPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 136 scrPane.setPreferredSize(new Dimension(100, 100)); 137 _attrPane.add(scrPane); 138 139 JPanel addlPane = new JPanel(); 140 addlPane.setLayout(new BoxLayout(addlPane, BoxLayout.X_AXIS)); 141 addlPane.setAlignmentX(Component.LEFT_ALIGNMENT); 142 addlPane.add(_consPane); 143 addlPane.add(_attrPane); 144 145 _propPane.add(addlPane); 146 } 147 148 private void toggleNameAndIdentifier(String oldStart, String newStart) 149 { 150 String name = _nameText.getText(); 151 String id = _identifierText.getText(); 152 System.err.println("name = " + name + "; id = " + id); 153 if (name.startsWith(oldStart)) 154 { 155 String newName = newStart + name.substring(oldStart.length(), name.length()); 156 //_nameText.setText(newName); 157 } 158 if (id.startsWith(oldStart)) 159 { 160 String newId = newStart + id.substring(oldStart.length(), id.length()); 161 _identifierText.setText(newId); 162 } 163 } 164 165 public boolean validate() 166 { 167 // check id 168 // check that at least one attr was selected 169 // check that there is only one primary key for the relation 170 // check that there is not a duplicate unique key (i.e. same attributes) 171 return true; 172 } 173 174 public void updateModel() 175 { 176 Relation relation = (Relation)getOwner(); 177 178 Object[] things = _attrList.getSelectedValues(); 179 Attribute[] attrs = new Attribute[things.length]; 180 for (int j = 0; j < things.length; j++) 181 { 182 attrs[j] = (Attribute)things[j]; 183 } 184 if (attrs != null && attrs.length > 0) 185 { 186 if (_key == null) 187 { 188 if (_currentConstraint == UNIQUEKEY_STR) 189 { 190 setKey(relation.createUniqueKey(attrs)); 191 } 192 else if (_currentConstraint == PRIMARYKEY_STR) 193 { 194 setKey(relation.createPrimaryKey(attrs)); 195 } 196 } 197 else 198 { 199 for (int i = 0; i < attrs.length; i++) 200 { 201 _key.addAttribute(attrs[i]); 202 } 203 } 204 205 _key.setName(_nameText.getText()); 206 _key.setIdentifier(_identifierText.getText()); 207 _key.setDescription(_descText.getText()); 208 209 ownerUpdated(); 210 } 211 else 212 { 213 System.err.println("Cannot have a key with no attributes!"); 214 } 215 } 216 217 public void updateEditor() 218 { 219 if (_key != null) 220 { 221 String id = _key.getIdentifier(); 222 if (id != null && !EMPTY_STR.equals(id)) 223 { 224 _nameText.getDocument().removeDocumentListener(_identifierText); 225 _identifierText.setText(id); 226 } 227 228 _nameText.setText(_key.getName()); 229 _descText.setText(_key.getDescription()); 230 231 // update the type selection 232 if (_key instanceof PrimaryKey) 233 { 234 _pkButton.setSelected(true); 235 } 236 else 237 { 238 _akButton.setSelected(true); 239 } 240 241 // update the attributes 242 _attrList.clearSelection(); 243 Iterator attrs = _key.getAttributeIterator(); 244 while (attrs.hasNext()) 245 { 246 int index = _listModel.indexOf(attrs.next()); 247 if (index >= 0) 248 { 249 _attrList.addSelectionInterval(index, index); 250 } 251 } 252 } 253 else 254 { 255 Relation relation = (Relation) getOwner(); 256 // this will also set the identifier field 257 _nameText.setText(UniqueKey.NEW_UNIQUEKEY_STR + (relation.getKeyCount() + 1)); 258 } 259 } 260 261 private void setKey(KeyConstraint key) 262 { 263 _key = key; 264 } 265 266 ////////////////////////////////////////////////////////// 267 // Inner classes 268 private class AttributeListModel extends DefaultListModel 269 { 270 public AttributeListModel(Relation relation) 271 { 272 Iterator attrs = relation.getAttributeIterator(); 273 while (attrs.hasNext()) 274 { 275 addElement(attrs.next()); 276 } 277 } 278 } /*** end inner class AttributeListModel */ 279 280 } /* end class KeyEditor */

This page was automatically generated by Maven