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 DomainEditor 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 NAME_EDIT_DOMAIN = "Edit Domain";
26
27 //////////////////////////////////////////////////////////
28 // Attributes
29 private Domain _domain;
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 //////////////////////////////////////////////////////////
43 // Constructors
44 public DomainEditor(JTabbedPane parent, Schema schema, Domain domain)
45 {
46 super(parent,schema);
47 setDomain(domain);
48 updateEditor();
49 }
50
51 //////////////////////////////////////////////////////////
52 // Operations
53 protected void initTabs()
54 {
55 // we want the default tab
56 setDefaultLabel("Edit Domain Properties");
57 setDefaultTip("Edit the name, description and other properties of the domain");
58 setDefaultIndex(0);
59
60 super.initTabs();
61
62 _consPane = new JPanel();
63 _consButton = new JCheckBox(NOTNULL_CONSTRAINT.toString(),false);
64 _consLabel = new JLabel(DEFAULT_VALUE_STR);
65 _consText = new JTextField(10);
66 _consBorder = BorderFactory.createTitledBorder(CONSTRAINT_STR);
67 JPanel valPane = new JPanel();
68
69 valPane.setAlignmentX(Component.LEFT_ALIGNMENT);
70 _consPane.setAlignmentX(Component.LEFT_ALIGNMENT);
71 _consPane.setAlignmentY(Component.TOP_ALIGNMENT);
72 _consButton.setAlignmentX(Component.LEFT_ALIGNMENT);
73 _consLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
74 _consText.setAlignmentX(Component.LEFT_ALIGNMENT);
75 _consBorder.setTitleFont(EDITOR_FONT);
76 _consBorder.setTitleColor(EDITOR_COLOR);
77 _consLabel.setFont(EDITOR_FONT);
78 _consLabel.setForeground(EDITOR_COLOR);
79 _consButton.setMnemonic('n');
80
81 valPane.setLayout(new BoxLayout(valPane, BoxLayout.X_AXIS));
82 valPane.add(_consLabel);
83 valPane.add(_consText);
84
85 _consPane.setBorder(_consBorder);
86 _consPane.setLayout(new BoxLayout(_consPane, BoxLayout.Y_AXIS));
87 _consPane.add(_consButton);
88 _consPane.add(valPane);
89
90 _consButton.addItemListener(
91 new ItemListener()
92 {
93 public void itemStateChanged(ItemEvent e)
94 {
95 if (e.getStateChange() == ItemEvent.SELECTED)
96 {
97 _currentConstraint = AttributeConstraint.NOTNULL;
98 } else {
99 _currentConstraint = AttributeConstraint.DEFAULT;
100 }
101 }
102 }
103 );
104
105 _typePane = new JPanel();
106 _typeList = new JComboBox(DataType.SQL92_DATATYPE_ARRAY);
107 _typeBorder = BorderFactory.createTitledBorder(TYPE_STR);
108
109 _typePane.setAlignmentX(Component.LEFT_ALIGNMENT);
110 _typePane.setAlignmentY(Component.TOP_ALIGNMENT);
111 _typeList.setAlignmentX(Component.LEFT_ALIGNMENT);
112 _typeBorder.setTitleFont(EDITOR_FONT);
113 _typeBorder.setTitleColor(EDITOR_COLOR);
114 _typeList.setEditor(new DataTypeComboBoxEditor());
115 _typeList.setEditable(true);
116 _typePane.setBorder(_typeBorder);
117 _typePane.add(_typeList);
118
119 JPanel addlPane = new JPanel();
120 addlPane.setLayout(new BoxLayout(addlPane, BoxLayout.X_AXIS));
121 addlPane.setAlignmentX(Component.LEFT_ALIGNMENT);
122 addlPane.add(_consPane);
123 addlPane.add(_typePane);
124
125 _propPane.add(addlPane);
126 }
127
128 public boolean validate()
129 {
130 Schema schema = (Schema) getOwner();
131 String id = _identifierText.getText();
132 Iterator domains = schema.getDomainIterator();
133 while (domains.hasNext())
134 {
135 Domain domain = (Domain) domains.next();
136 if (domain.getIdentifier().equals(id))
137 {
138 ErjFrame browser = ErjFrame.getInstance();
139 String warning = "Schema " + schema.toString() + " already contains\n" +
140 "a domain with the identifier " + id +
141 ".\nPlease correct this before proceeding.";
142 JOptionPane.showMessageDialog(
143 browser, warning, "Invalid Identifier", JOptionPane.ERROR_MESSAGE
144 );
145 return false;
146 }
147 }
148 return true;
149 }
150
151 public void updateModel()
152 {
153 Schema s = (Schema) getOwner();
154 if (_domain == null)
155 {
156 setDomain(s.createDomain());
157 }
158 String name = _nameText.getText();
159 if (name != null && !EMPTY_STR.equals(name))
160 {
161 _domain.setName(name);
162 }
163 String id = _identifierText.getText();
164 if (id != null && !EMPTY_STR.equals(id))
165 {
166 _domain.setIdentifier(id);
167 }
168 _domain.setDescription(_descText.getText());
169 _domain.setDefaultConstraint(_currentConstraint);
170 if (!EMPTY_STR.equals(_consText.getText()))
171 {
172 _domain.setDefaultValue(new DefaultValue(_consText.getText()));
173 }
174
175 Object item;
176 if ( (item = _typeList.getSelectedItem()) instanceof DataType)
177 {
178 DataType type = (DataType)((DataType) item).clone();
179 if (type.takesDescriptor())
180 {
181 type.setDescriptor(
182 (
183 (DataTypeComboBoxEditor)_typeList.getEditor()
184 ).getDescriptor()
185 );
186 }
187 //System.err.println("type = " + type.toString());
188 _domain.setDataType(type);
189 }
190 }
191
192 public void updateEditor()
193 {
194 if (_domain != null)
195 {
196 String id = _domain.getIdentifier();
197 if (id != null && !EMPTY_STR.equals(id))
198 {
199 _nameText.getDocument().removeDocumentListener(_identifierText);
200 _identifierText.setText(id);
201 }
202
203 _nameText.setText(_domain.getName());
204 _descText.setText(_domain.getDescription());
205 if (_domain.getDefaultConstraint() == AttributeConstraint.NOTNULL)
206 {
207 _consButton.setSelected(true);
208 } else {
209 _consButton.setSelected(false);
210 }
211 _consText.setText(_domain.getDefaultValue().toString());
212 DataType type = _domain.getDataType();
213 _typeList.setSelectedItem(type);
214 DataTypeComboBoxEditor editor = (DataTypeComboBoxEditor)_typeList.getEditor();
215 if (type.takesDescriptor())
216 {
217 String descr = type.getDescriptor() != null
218 ? type.getDescriptor()
219 : type.getDefaultDescriptor();
220 editor.setDescriptor(descr);
221 }
222 }
223 else
224 {
225 Schema schema = (Schema) getOwner();
226 // this will also set the identifier field
227 _nameText.setText(Domain.NEW_DOMAIN_STR + (schema.getDomainCount() + 1));
228 }
229 }
230
231 private void setDomain(Domain domain)
232 {
233 _domain = domain;
234 }
235
236 } /* end class ForeignKeyEditor */
This page was automatically generated by Maven