1 package com.bonevich.erj.ui.editor;
2
3 import com.bonevich.erj.model.*;
4 import com.bonevich.erj.ui.*;
5 import com.bonevich.util.dependency.Dependent;
6 import com.bonevich.util.dependency.UpdateAction;
7
8 import java.util.*;
9 import java.awt.*;
10 import java.awt.event.*;
11 import javax.swing.*;
12
13 /*** A model editor for relations.
14 *
15 * @see Relation
16 * @see ModelEditor
17 */
18 public final class RelationEditor extends ModelEditor
19 {
20 //////////////////////////////////////////////////////////
21 // Constants
22 public static final int MODE_EDIT_RELATION = 0;
23 public static final int MODE_EDIT_ATTRIBUTES = 1;
24 public static final int MODE_EDIT_KEYS = 2;
25
26 public static final String NAME_EDIT_RELATION = "Edit Relation";
27 public static final String NAME_EDIT_ATTRIBUTES = "Edit Attributes";
28 public static final String NAME_EDIT_KEYS = "Edit Keys";
29
30 public static final String TIP_EDIT_RELATION = "Edit the properties of the relation";
31 public static final String TIP_EDIT_ATTRIBUTES = "Add, remove, and edit attributes for the relation";
32 public static final String TIP_EDIT_KEYS = "Add, remove, and edit keys for the relation";
33
34 public static final String ATTR_ADD = "Add";
35 public static final String ATTR_EDIT = "Edit";
36 public static final String ATTR_DELETE = "Delete";
37 public static final String ATTR_UP = "Up";
38 public static final String ATTR_DOWN = "Down";
39
40 public static final String KEY_ADD = "Add";
41 public static final String KEY_EDIT = "Edit";
42 public static final String KEY_DELETE = "Delete";
43
44 //////////////////////////////////////////////////////////
45 // Attributes
46 private JPanel _attrPane;
47 private JPanel _keyPane;
48 private JTable _attrTable;
49 private JTable _keyTable;
50 private AttributeTableModel _attrModel;
51 private KeyConstraintTableModel _keyModel;
52
53 private JButton _addButton;
54 private JButton _editButton;
55 private JButton _delButton;
56 private JButton _upButton;
57 private JButton _dnButton;
58
59 private JButton _addKeyButton;
60 private JButton _editKeyButton;
61 private JButton _delKeyButton;
62
63 private Dependent _dep_relation = new Dependent(new UpdateAction()
64 {
65 public void onUpdate()
66 {
67 updateEditor();
68 _attrTable.revalidate();
69 _attrPane.repaint();
70 _keyTable.revalidate();
71 _keyPane.repaint();
72 }
73 });
74
75 //////////////////////////////////////////////////////////
76 // Constructors
77 public RelationEditor(JTabbedPane parent, Relation relation)
78 {
79 super(parent,relation);
80 addActionListeners();
81 }
82
83 //////////////////////////////////////////////////////////
84 // Operations
85 protected void initTabs()
86 {
87 // we want the default tab
88 setDefaultLabel(NAME_EDIT_RELATION);
89 setDefaultTip(TIP_EDIT_RELATION);
90 setDefaultIndex(MODE_EDIT_RELATION);
91 super.initTabs();
92
93 initAttributePane();
94 initKeyPane();
95 }
96
97 private void initAttributePane()
98 {
99 _attrPane = new JPanel();
100 _addButton = new JButton(ATTR_ADD);
101 _editButton = new JButton(ATTR_EDIT);
102 _delButton = new JButton(ATTR_DELETE);
103 _upButton = new JButton(ATTR_UP);
104 _dnButton = new JButton(ATTR_DOWN);
105
106 _attrPane.setLayout(new BorderLayout());
107
108 _attrModel = new AttributeTableModel();
109 _attrTable = new JTable();
110 _attrTable.setModel(_attrModel);
111
112 JScrollPane scrollPane = new JScrollPane(_attrTable);
113 scrollPane.setAlignmentX(Component.LEFT_ALIGNMENT);
114 scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
115 scrollPane.setPreferredSize(new Dimension(450, 200));
116 JPanel attrTablePane = new JPanel();
117 attrTablePane.setAlignmentX(Component.LEFT_ALIGNMENT);
118 attrTablePane.add(scrollPane);
119
120 JPanel btnPane = new JPanel();
121 btnPane.setLayout(new BoxLayout(btnPane,BoxLayout.Y_AXIS));
122 btnPane.add(_addButton);
123 btnPane.add(_editButton);
124 btnPane.add(_delButton);
125 btnPane.add(_upButton);
126 btnPane.add(_dnButton);
127
128 _attrPane.add(attrTablePane, BorderLayout.CENTER);
129 _attrPane.add(btnPane, BorderLayout.EAST);
130
131 addTab(NAME_EDIT_ATTRIBUTES, null, _attrPane , TIP_EDIT_ATTRIBUTES, MODE_EDIT_ATTRIBUTES);
132 }
133
134 private void initKeyPane()
135 {
136 _keyPane = new JPanel();
137 _addKeyButton = new JButton(KEY_ADD);
138 _editKeyButton = new JButton(KEY_EDIT);
139 _delKeyButton = new JButton(KEY_DELETE);
140
141 _keyPane.setLayout(new BorderLayout());
142
143 _keyModel = new KeyConstraintTableModel();
144 _keyTable = new JTable();
145 _keyTable.setModel(_keyModel);
146
147 JScrollPane scrollPane = new JScrollPane(_keyTable);
148 scrollPane.setAlignmentX(Component.LEFT_ALIGNMENT);
149 scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
150 scrollPane.setPreferredSize(new Dimension(450, 200));
151 JPanel keyTablePane = new JPanel();
152 keyTablePane.setAlignmentX(Component.LEFT_ALIGNMENT);
153 keyTablePane.add(scrollPane);
154
155 JPanel btnPane = new JPanel();
156 btnPane.setLayout(new BoxLayout(btnPane,BoxLayout.Y_AXIS));
157 btnPane.add(_addKeyButton);
158 btnPane.add(_editKeyButton);
159 btnPane.add(_delKeyButton);
160
161 _keyPane.add(keyTablePane, BorderLayout.CENTER);
162 _keyPane.add(btnPane, BorderLayout.EAST);
163
164 addTab(NAME_EDIT_KEYS, null, _keyPane, TIP_EDIT_KEYS, MODE_EDIT_KEYS);
165 }
166
167 public boolean validate()
168 {
169 return true;
170 }
171
172 public void updateModel()
173 {
174 Relation relation = (Relation)getOwner();
175 relation.setName(_nameText.getText());
176 relation.setIdentifier(_identifierText.getText());
177 relation.setDescription(_descText.getText());
178
179 ownerUpdated();
180 }
181
182 public void updateEditor()
183 {
184 Relation relation = (Relation)getOwner();
185
186 String id = relation.getIdentifier();
187 if (id != null && !EMPTY_STR.equals(id))
188 {
189 _nameText.getDocument().removeDocumentListener(_identifierText);
190 _identifierText.setText(id);
191 }
192
193 _nameText.setText(relation.getName());
194 _descText.setText(relation.getDescription());
195
196 // update the attribute table
197 _attrModel.clearRows();
198 Iterator attrs = relation.getAttributeIterator();
199 while (attrs.hasNext())
200 {
201 Attribute attr = (Attribute) attrs.next();
202 _attrModel.setRow(attr);
203 }
204
205 // update the key constraint table
206 _keyModel.clearRows();
207 Iterator keys = relation.getKeyIterator();
208 while (keys.hasNext())
209 {
210 KeyConstraint key = (KeyConstraint) keys.next();
211 _keyModel.setRow(key);
212 }
213 }
214
215 private void addActionListeners()
216 {
217 _addButton.addActionListener(
218 new ActionListener()
219 {
220 public void actionPerformed(ActionEvent e)
221 {
222 CmdCreateAttribute cmd = new CmdCreateAttribute((Relation)getOwner(), getWindow());
223 cmd.invoke();
224 _dep_relation.onGet();
225 }
226 }
227 );
228
229 _editButton.addActionListener(
230 new ActionListener()
231 {
232 public void actionPerformed(ActionEvent e)
233 {
234 int rowIndex = _attrTable.getSelectedRow();
235 if (rowIndex > -1)
236 {
237 AttributeTableModel model = (AttributeTableModel)_attrTable.getModel();
238 Attribute attr = (Attribute)model.getRow(rowIndex);
239 CmdEditAttribute cmd = new CmdEditAttribute(attr, getWindow());
240 cmd.invoke();
241 _dep_relation.onGet();
242 }
243 }
244 }
245 );
246
247 _delButton.addActionListener(
248 new ActionListener()
249 {
250 public void actionPerformed(ActionEvent e)
251 {
252 int rowIndex = _attrTable.getSelectedRow();
253 if (rowIndex > -1)
254 {
255 AttributeTableModel model = (AttributeTableModel)_attrTable.getModel();
256 Attribute attr = (Attribute)model.getRow(rowIndex);
257 Relation relation = (Relation)getOwner();
258 relation.deleteAttribute(attr);
259 _dep_relation.onGet();
260 }
261 }
262 }
263 );
264
265 _upButton.addActionListener(
266 new ActionListener()
267 {
268 public void actionPerformed(ActionEvent e)
269 {
270 int rowIndex = _attrTable.getSelectedRow();
271 if (rowIndex > 0)
272 {
273 AttributeTableModel model = (AttributeTableModel)_attrTable.getModel();
274 Attribute attr = (Attribute)model.getRow(rowIndex);
275 Relation relation = (Relation)getOwner();
276 relation.moveAttributeUp(attr);
277 _attrTable.changeSelection(--rowIndex, _attrTable.getSelectedColumn(), false, false);
278 _dep_relation.onGet();
279 }
280 }
281 }
282 );
283
284 _dnButton.addActionListener(
285 new ActionListener()
286 {
287 public void actionPerformed(ActionEvent e)
288 {
289 int rowIndex = _attrTable.getSelectedRow();
290 if (rowIndex < _attrTable.getRowCount() - 1)
291 {
292 AttributeTableModel model = (AttributeTableModel)_attrTable.getModel();
293 Attribute attr = (Attribute)model.getRow(rowIndex);
294 Relation relation = (Relation)getOwner();
295 relation.moveAttributeDown(attr);
296 _attrTable.changeSelection(++rowIndex, _attrTable.getSelectedColumn(), false, false);
297 _dep_relation.onGet();
298 }
299 }
300 }
301 );
302
303 _addKeyButton.addActionListener(
304 new ActionListener()
305 {
306 public void actionPerformed(ActionEvent e)
307 {
308 CmdCreateKey cmd = new CmdCreateKey((Relation) getOwner(), getWindow());
309 cmd.invoke();
310 _dep_relation.onGet();
311 }
312 }
313 );
314
315 _editKeyButton.addActionListener(
316 new ActionListener()
317 {
318 public void actionPerformed(ActionEvent e)
319 {
320 int rowIndex = _keyTable.getSelectedRow();
321 if (rowIndex > -1)
322 {
323 KeyConstraintTableModel model = (KeyConstraintTableModel) _keyTable.getModel();
324 KeyConstraint key = (KeyConstraint) model.getRow(rowIndex);
325 CmdProperties cmd = null;
326 if (key instanceof ForeignKey)
327 {
328 cmd = new CmdEditForeignKey((ForeignKey) key, getWindow());
329 }
330 else
331 {
332 cmd = new CmdEditKey(key, getWindow());
333 }
334 cmd.invoke();
335 _dep_relation.onGet();
336 }
337 }
338 }
339 );
340
341 _delKeyButton.addActionListener(
342 new ActionListener()
343 {
344 public void actionPerformed(ActionEvent e)
345 {
346 int rowIndex = _keyTable.getSelectedRow();
347 if (rowIndex > -1)
348 {
349 KeyConstraintTableModel model = (KeyConstraintTableModel) _keyTable.getModel();
350 KeyConstraint key = (KeyConstraint) model.getRow(rowIndex);
351 Relation relation = (Relation) getOwner();
352 relation.deleteKey(key);
353 _dep_relation.onGet();
354 }
355 }
356 }
357 );
358 }
359
360 } /* end class RelationEditor */
This page was automatically generated by Maven