1 package com.bonevich.erj.ui.editor;
2
3 import com.bonevich.erj.model.*;
4 import com.bonevich.erj.ui.ErjFrame;
5
6 import java.util.*;
7 import java.awt.*;
8 import java.awt.event.*;
9 import javax.swing.*;
10 import javax.swing.border.TitledBorder;
11
12 /*** A model editor for attributes.
13 *
14 * @see Attribute
15 * @see ModelEditor
16 */
17 public final class AttributeEditor extends ModelEditor
18 {
19 //////////////////////////////////////////////////////////
20 // Constants
21 public static final String TYPE_STR = "Type";
22 public static final String CONSTRAINT_STR = "Constraint";
23 public static final String DEFAULT_VALUE_STR = "Default Value:";
24 public static final AttributeConstraint NOTNULL_CONSTRAINT = AttributeConstraint.NOTNULL;
25 public static final String DOMAIN_STR = "Domain";
26
27 //////////////////////////////////////////////////////////
28 // Attributes
29 private Attribute _attr;
30 private AttributeConstraint _currentConstraint = AttributeConstraint.DEFAULT;
31
32 private JPanel _consPane;
33 private TitledBorder _consBorder;
34 private JCheckBox _consButton;
35 private JLabel _consLabel;
36 private JTextField _consText;
37
38 private JPanel _typePane;
39 private TitledBorder _typeBorder;
40 private JComboBox _typeList;
41
42 private JPanel _domainPane;
43 private TitledBorder _domainBorder;
44 private JComboBox _domainList;
45 private JButton _editButton;
46
47 //////////////////////////////////////////////////////////
48 // Constructors
49 public AttributeEditor(JTabbedPane parent, Relation relation, Attribute attribute)
50 {
51 super(parent,relation);
52 setAttribute(attribute);
53 updateEditor();
54 }
55
56 //////////////////////////////////////////////////////////
57 // Operations
58 protected void initTabs()
59 {
60 // we want the default tab
61 setDefaultLabel("Edit Attribute Properties");
62 setDefaultTip("Edit the name, description and other properties of the attribute");
63 setDefaultIndex(0);
64
65 super.initTabs();
66
67 _consPane = new JPanel();
68 _consButton = new JCheckBox(NOTNULL_CONSTRAINT.toString(),false);
69 _consLabel = new JLabel(DEFAULT_VALUE_STR);
70 _consText = new JTextField(10);
71 _consBorder = BorderFactory.createTitledBorder(CONSTRAINT_STR);
72 JPanel valPane = new JPanel();
73
74 valPane.setAlignmentX(Component.LEFT_ALIGNMENT);
75 _consPane.setAlignmentX(Component.LEFT_ALIGNMENT);
76 _consPane.setAlignmentY(Component.TOP_ALIGNMENT);
77 _consButton.setAlignmentX(Component.LEFT_ALIGNMENT);
78 _consLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
79 _consText.setAlignmentX(Component.LEFT_ALIGNMENT);
80 _consBorder.setTitleFont(EDITOR_FONT);
81 _consBorder.setTitleColor(EDITOR_COLOR);
82 _consLabel.setFont(EDITOR_FONT);
83 _consLabel.setForeground(EDITOR_COLOR);
84 _consButton.setMnemonic('n');
85
86 valPane.setLayout(new BoxLayout(valPane, BoxLayout.X_AXIS));
87 valPane.add(_consLabel);
88 valPane.add(_consText);
89
90 _consPane.setBorder(_consBorder);
91 _consPane.setLayout(new BoxLayout(_consPane, BoxLayout.Y_AXIS));
92 _consPane.add(_consButton);
93 _consPane.add(valPane);
94
95 _consButton.addItemListener(
96 new ItemListener()
97 {
98 public void itemStateChanged(ItemEvent e)
99 {
100 if (e.getStateChange() == ItemEvent.SELECTED)
101 {
102 _currentConstraint = AttributeConstraint.NOTNULL;
103 } else {
104 _currentConstraint = AttributeConstraint.DEFAULT;
105 }
106 }
107 }
108 );
109
110 _typePane = new JPanel();
111 _typeList = new JComboBox(DataType.SQL92_DATATYPE_ARRAY);
112 _typeBorder = BorderFactory.createTitledBorder(TYPE_STR);
113
114 _typeList.setSelectedItem(null);
115 _typePane.setAlignmentX(Component.LEFT_ALIGNMENT);
116 _typePane.setAlignmentY(Component.TOP_ALIGNMENT);
117 _typeList.setAlignmentX(Component.LEFT_ALIGNMENT);
118 _typeBorder.setTitleFont(EDITOR_FONT);
119 _typeBorder.setTitleColor(EDITOR_COLOR);
120 _typeList.setEditor(new DataTypeComboBoxEditor());
121 _typeList.setEditable(true);
122 _typePane.setBorder(_typeBorder);
123 _typePane.add(_typeList);
124
125 _domainPane = new JPanel();
126 _domainList = new JComboBox();
127 _domainBorder = BorderFactory.createTitledBorder(DOMAIN_STR);
128
129 Schema schema = ( (Relation)getOwner() ).getSchema();
130 Iterator domains = schema.getDomainIterator();
131 while (domains.hasNext())
132 {
133 _domainList.addItem(domains.next());
134 }
135 _domainList.setSelectedItem(null);
136 _domainPane.setAlignmentX(Component.LEFT_ALIGNMENT);
137 _domainPane.setAlignmentY(Component.TOP_ALIGNMENT);
138 _domainList.setAlignmentX(Component.LEFT_ALIGNMENT);
139 _domainBorder.setTitleFont(EDITOR_FONT);
140 _domainBorder.setTitleColor(EDITOR_COLOR);
141 _domainPane.setBorder(_domainBorder);
142 _domainPane.add(_domainList);
143
144 //FIXME: this is broken, does not allow domain to override type, etc.
145 ActionListener typeListener = new ActionListener()
146 {
147 private boolean locked = false;
148
149 public void actionPerformed(ActionEvent e)
150 {
151 JComboBox list = (JComboBox) e.getSource();
152 AttributeType selectedItem = (AttributeType) list.getSelectedItem();
153
154 if (locked)
155 {
156 locked = false;
157 }
158 else
159 {
160 locked = true;
161 if (selectedItem instanceof DataType)
162 {
163 _domainList.setSelectedItem(null);
164 }
165 else if (selectedItem instanceof Domain)
166 {
167 Domain selected = (Domain) selectedItem;
168 // just set the type and constraint - overrides any user-specified values
169 setDataType(selected.getDataType());
170 if (selected.getDefaultConstraint() == AttributeConstraint.NOTNULL)
171 {
172 _consButton.setSelected(true);
173 } else {
174 _consButton.setSelected(false);
175 }
176 _consText.setText(selected.getDefaultValue().toString());
177 }
178 }
179 }
180 };
181
182 _typeList.addActionListener(typeListener);
183 _domainList.addActionListener(typeListener);
184
185 JPanel addlPane = new JPanel();
186 addlPane.setLayout(new BoxLayout(addlPane, BoxLayout.X_AXIS));
187 addlPane.setAlignmentX(Component.LEFT_ALIGNMENT);
188 addlPane.add(_consPane);
189 addlPane.add(_typePane);
190 addlPane.add(_domainPane);
191
192 _propPane.add(addlPane);
193 }
194
195 public boolean validate()
196 {
197 Relation relation = (Relation)getOwner();
198 String id = _identifierText.getText();
199 Attribute attr = relation.getAttribute(id);
200 if (attr != null && attr != _attr)
201 {
202 ErjFrame browser = ErjFrame.getInstance();
203 String warning = "Relation " + relation.toString() + " already contains\n" +
204 "an attribute with the identifier " + id +
205 ".\nPlease correct this before proceeding.";
206 JOptionPane.showMessageDialog(
207 browser, warning, "Invalid Identifier", JOptionPane.ERROR_MESSAGE
208 );
209 return false;
210 }
211 return true;
212 }
213
214 public void updateModel()
215 {
216 Relation relation = (Relation)getOwner();
217 if (_attr == null)
218 {
219 setAttribute(relation.createAttribute());
220 }
221 String name = _nameText.getText();
222 if (name != null && !EMPTY_STR.equals(name))
223 {
224 _attr.setName(name);
225 }
226 String id = _identifierText.getText();
227 if (id != null && !EMPTY_STR.equals(id))
228 {
229 _attr.setIdentifier(id);
230 }
231 _attr.setDescription(_descText.getText());
232 _attr.setConstraint(_currentConstraint);
233 if (!EMPTY_STR.equals(_consText.getText()))
234 {
235 _attr.setDefaultValue(new DefaultValue(_consText.getText()));
236 }
237
238 // If a domain was used, set it; otherwise default to datatype
239 Object item = _domainList.getSelectedItem();
240 if (item != null)
241 {
242 _attr.setType((Domain) item);
243 }
244 else if ( (item = _typeList.getSelectedItem()) instanceof DataType)
245 {
246 DataType type = (DataType)( (DataType)item ).clone();
247 if (type.takesDescriptor())
248 {
249 type.setDescriptor(
250 (
251 (DataTypeComboBoxEditor)_typeList.getEditor()
252 ).getDescriptor()
253 );
254 }
255 _attr.setType(type);
256 }
257 ownerUpdated();
258 }
259
260 public void updateEditor()
261 {
262 if (_attr != null)
263 {
264 String id = _attr.getIdentifier();
265 if (id != null && !EMPTY_STR.equals(id))
266 {
267 _nameText.getDocument().removeDocumentListener(_identifierText);
268 _identifierText.setText(id);
269 }
270
271 _nameText.setText(_attr.getName());
272 _descText.setText(_attr.getDescription());
273
274 AttributeType type = _attr.getType();
275 if (type instanceof Domain)
276 {
277 Domain domain = (Domain) type;
278 _domainList.setSelectedItem(domain);
279 }
280 else if (type instanceof DataType)
281 {
282 DataType dt = (DataType)type;
283 setDataType(dt);
284 }
285
286 if (_attr.getConstraint() == AttributeConstraint.NOTNULL)
287 {
288 _consButton.setSelected(true);
289 } else {
290 _consButton.setSelected(false);
291 }
292 _consText.setText(_attr.getDefaultValue().toString());
293 }
294 else
295 {
296 Relation relation = (Relation)getOwner();
297 // this will also set the identifier field
298 _nameText.setText(Attribute.NEW_ATTRIBUTE_STR + (relation.getAttributeCount() + 1));
299 }
300 }
301
302 private void setDataType(DataType dt)
303 {
304 _typeList.setSelectedItem(dt);
305 DataTypeComboBoxEditor editor = (DataTypeComboBoxEditor)_typeList.getEditor();
306 if (dt.takesDescriptor())
307 {
308 String descr = dt.getDescriptor() != null
309 ? dt.getDescriptor()
310 : dt.getDefaultDescriptor();
311 editor.setDescriptor(descr);
312 }
313 }
314
315 private void setAttribute(Attribute attr)
316 {
317 _attr = attr;
318 }
319
320 } /* end class AttributeEditor */
This page was automatically generated by Maven