1 30 package com.genimen.djeneric.tools.modeler.dialogs; 31 32 import java.awt.BorderLayout ; 33 import java.awt.Dimension ; 34 import java.awt.Toolkit ; 35 import java.awt.event.ActionEvent ; 36 import java.awt.event.FocusEvent ; 37 import java.awt.event.KeyEvent ; 38 39 import javax.swing.DefaultComboBoxModel ; 40 import javax.swing.JButton ; 41 import javax.swing.JCheckBox ; 42 import javax.swing.JComboBox ; 43 import javax.swing.JDialog ; 44 import javax.swing.JLabel ; 45 import javax.swing.JPanel ; 46 import javax.swing.JTextField ; 47 48 import com.genimen.djeneric.language.Messages; 49 import com.genimen.djeneric.repository.DjExtent; 50 import com.genimen.djeneric.repository.DjPersistenceManager; 51 import com.genimen.djeneric.repository.DjProperty; 52 import com.genimen.djeneric.repository.DjRelation; 53 import com.genimen.djeneric.tools.modeler.ModelEditor; 54 import com.genimen.djeneric.ui.DjVerticalFlowLayout; 55 import com.genimen.djeneric.ui.Util; 56 import com.genimen.djeneric.util.DjLogger; 57 58 public class CreateRelationDialog extends JDialog 59 { 60 private static final long serialVersionUID = 1L; 61 JPanel panel1 = new JPanel (); 62 BorderLayout borderLayout1 = new BorderLayout (); 63 JPanel jPanel1 = new JPanel (); 64 BorderLayout borderLayout2 = new BorderLayout (); 65 JPanel jPanel2 = new JPanel (); 66 BorderLayout borderLayout3 = new BorderLayout (); 67 JButton _butOk = new JButton (); 68 JButton _butCancel = new JButton (); 69 JPanel jPanel3 = new JPanel (); 70 JPanel jPanel4 = new JPanel (); 71 JLabel jLabel1 = new JLabel (); 72 JLabel jLabel2 = new JLabel (); 73 DjVerticalFlowLayout verticalFlowLayout1 = new DjVerticalFlowLayout(); 74 DjVerticalFlowLayout verticalFlowLayout2 = new DjVerticalFlowLayout(); 75 JLabel jLabel3 = new JLabel (); 76 JLabel jLabel4 = new JLabel (); 77 JLabel jLabel5 = new JLabel (); 78 JLabel _lblMaster = new JLabel (); 79 JLabel _lblDetail = new JLabel (); 80 JTextField _edtRelation = new JTextField (); 81 JTextField _edtProperty = new JTextField (); 82 JCheckBox _chkContains = new JCheckBox (); 83 DjExtent _master; 84 DjExtent _detail; 85 String _mapping; 86 ModelEditor _editor; 87 JLabel jLabel6 = new JLabel (); 88 JCheckBox _chkRequired = new JCheckBox (); 89 JLabel _lblUnicity = new JLabel (); 90 JComboBox _cbbUnicity = new JComboBox (); 91 92 public CreateRelationDialog(ModelEditor editor, DjExtent master, DjExtent detail) 93 { 94 super(editor, Messages.getString("CreateRelationDialog.RelationProperties"), true); 95 try 96 { 97 _editor = editor; 98 _master = master; 99 _detail = detail; 100 101 jbInit(); 102 _edtRelation.setText(_detail.getDefaultMasterRelationName()); 103 _edtRelation.setText(fixRelationName()); 104 105 String propName = master.getObjectType(); 106 if (propName.length() > 1) propName = propName.substring(0, 1).toLowerCase() + propName.substring(1); 107 _edtProperty.setText(propName); 108 _edtProperty.setText(fixPropertyName()); 109 _lblMaster.setText(master.getName()); 110 _lblDetail.setText(detail.getName()); 111 pack(); 112 113 _mapping = _detail.findNextFreeMapping(DjPersistenceManager.MAPPING_REL); 114 115 _cbbUnicity.setModel(new DefaultComboBoxModel (new String []{"1:n", "1:1"})); 116 if (_mapping == null) 117 { 118 editor.setStatusMessage(Messages.getString("CreateRelationDialog.NoMoreRels"), false); 119 return; 120 } 121 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 122 Dimension frameSize = getSize(); 123 setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2); 124 125 setVisible(true); 126 } 127 catch (Exception ex) 128 { 129 DjLogger.log(ex); 130 } 131 } 132 133 protected String fixRelationName() 134 { 135 String tryString; 136 int tryCount = 0; 137 138 do 139 { 140 tryString = _edtRelation.getText(); 141 if (tryCount > 0) tryString += tryCount; 142 tryCount++; 143 } 144 while (_master.hasRelation(tryString)); 145 146 if (tryString.length() > 1) tryString = tryString.toLowerCase().substring(0, 1) + tryString.substring(1); 147 return tryString; 148 } 149 150 protected String fixPropertyName() 151 { 152 String tryString; 153 int tryCount = 0; 154 155 do 156 { 157 tryString = _edtProperty.getText(); 158 if (tryCount > 0) tryString += tryCount; 159 tryCount++; 160 } 161 while (_detail.hasProperty(tryString)); 162 163 return tryString; 164 } 165 166 void jbInit() throws Exception 167 { 168 panel1.setLayout(borderLayout1); 169 this.getContentPane().setLayout(borderLayout2); 170 jPanel1.setLayout(borderLayout3); 171 _butOk.setText(Messages.getString("global.Ok")); 172 _butOk.addActionListener(new java.awt.event.ActionListener () 173 { 174 public void actionPerformed(ActionEvent e) 175 { 176 _butOk_actionPerformed(e); 177 } 178 }); 179 _butCancel.setText(Messages.getString("global.Cancel")); 180 _butCancel.addActionListener(new java.awt.event.ActionListener () 181 { 182 public void actionPerformed(ActionEvent e) 183 { 184 _butCancel_actionPerformed(e); 185 } 186 }); 187 jLabel1.setText(Messages.getString("CreateRelationDialog.MasterExtent")); 188 jLabel2.setText(Messages.getString("CreateRelationDialog.DetailExtent")); 189 jPanel3.setLayout(verticalFlowLayout1); 190 jPanel4.setLayout(verticalFlowLayout2); 191 jLabel3.setText(Messages.getString("CreateRelationDialog.RelationName")); 192 jLabel4.setText(Messages.getString("CreateRelationDialog.DetailProperty")); 193 jLabel5.setText(Messages.getString("CreateRelationDialog.MasterContainsDetails")); 194 _lblMaster.setText(Messages.getString("global.Master")); 195 _lblDetail.setText(Messages.getString("CreateRelationDialog.detail")); 196 _edtRelation.setPreferredSize(new Dimension (200, 21)); 197 _edtRelation.addFocusListener(new java.awt.event.FocusAdapter () 198 { 199 public void focusGained(FocusEvent e) 200 { 201 _edtRelation_focusGained(e); 202 } 203 }); 204 _edtRelation.addKeyListener(new java.awt.event.KeyAdapter () 205 { 206 public void keyPressed(KeyEvent e) 207 { 208 handleEnterEscape(e); 209 } 210 }); 211 _edtProperty.setPreferredSize(new Dimension (200, 21)); 212 _edtProperty.addFocusListener(new java.awt.event.FocusAdapter () 213 { 214 public void focusGained(FocusEvent e) 215 { 216 _edtProperty_focusGained(e); 217 } 218 }); 219 _edtProperty.addKeyListener(new java.awt.event.KeyAdapter () 220 { 221 public void keyPressed(KeyEvent e) 222 { 223 handleEnterEscape(e); 224 } 225 }); 226 _chkContains.setSelected(true); 227 _chkContains.addKeyListener(new java.awt.event.KeyAdapter () 228 { 229 public void keyPressed(KeyEvent e) 230 { 231 handleEnterEscape(e); 232 } 233 }); 234 jLabel6.setText(Messages.getString("global.Required")); 235 _chkRequired.addKeyListener(new java.awt.event.KeyAdapter () 236 { 237 public void keyPressed(KeyEvent e) 238 { 239 handleEnterEscape(e); 240 } 241 }); 242 243 _chkRequired.setSelected(true); 244 245 _lblUnicity.setText(Messages.getString("RelationTableModel.Unicity")); 246 getContentPane().add(panel1, BorderLayout.CENTER); 247 this.getContentPane().add(jPanel1, BorderLayout.SOUTH); 248 jPanel1.add(jPanel2, BorderLayout.EAST); 249 jPanel2.add(_butCancel, null); 250 jPanel2.add(_butOk, null); 251 panel1.add(jPanel3, BorderLayout.WEST); 252 jPanel3.add(jLabel1, null); 253 jPanel3.add(jLabel2, null); 254 jPanel3.add(jLabel3, null); 255 jPanel3.add(jLabel4, null); 256 jPanel3.add(jLabel5, null); 257 jPanel3.add(jLabel6, null); 258 jPanel3.add(_lblUnicity, null); 259 panel1.add(jPanel4, BorderLayout.CENTER); 260 jPanel4.add(_lblMaster, null); 261 jPanel4.add(_lblDetail, null); 262 jPanel4.add(_edtRelation, null); 263 jPanel4.add(_edtProperty, null); 264 jPanel4.add(_chkContains, null); 265 jPanel4.add(_chkRequired, null); 266 jPanel4.add(_cbbUnicity, null); 267 Util.sizeButtons(jPanel2); 268 Util.sizeLabels(jPanel3); 269 Util.sizeLabels(jPanel4); 270 } 271 272 void _butOk_actionPerformed(ActionEvent e) 273 { 274 DjRelation newRel = null; 275 DjProperty newCol = null; 276 try 277 { 278 _edtRelation.setText(fixRelationName()); 279 _edtProperty.setText(fixPropertyName()); 280 281 newCol = new DjProperty(_edtProperty.getText(), _mapping, _edtProperty.getText(), _master.getNameSingular(), 282 _master, _chkRequired.isSelected(), _detail.getPropertyCount() * 10, null, Messages 283 .getString("CreateRelationDialog.LinkTo", _master.getNameSingular())); 284 _detail.addProperty(newCol); 285 newRel = _editor.getPersistenceManager().createRelation(_edtRelation.getText(), _master, _detail, newCol, 286 _chkContains.isSelected(), _master.getNamePlural()); 287 newRel.setUnicity(_cbbUnicity.getSelectedIndex() == 0 ? DjRelation.ONE_TO_MANY : DjRelation.ONE_TO_ONE); 288 _detail.addMasterRelation(newRel); 289 _master.addDetailRelation(newRel); 290 _editor.validateModel(false); 291 setVisible(false); 292 } 293 catch (Exception x) 294 { 295 _detail.removeMasterRelation(newRel); 297 _master.removeDetailRelation(newRel); 298 _detail.removeProperty(newCol); 299 _editor.setStatusMessage(x); 300 } 301 } 302 303 void _butCancel_actionPerformed(ActionEvent e) 304 { 305 setVisible(false); 306 } 307 308 void handleEnterEscape(KeyEvent e) 309 { 310 if (e.getKeyCode() == KeyEvent.VK_ENTER) _butOk_actionPerformed(null); 311 if (e.getKeyCode() == KeyEvent.VK_ESCAPE) _butCancel_actionPerformed(null); 312 } 313 314 void _edtRelation_focusGained(FocusEvent e) 315 { 316 _edtRelation.selectAll(); 317 318 } 319 320 void _edtProperty_focusGained(FocusEvent e) 321 { 322 _edtProperty.selectAll(); 323 324 } 325 326 void _chkRequired_keyTyped(KeyEvent e) 327 { 328 329 } 330 331 void _chkRequired_keyPressed(KeyEvent e) 332 { 333 334 } 335 336 void _chkRequired_keyReleased(KeyEvent e) 337 { 338 339 } 340 } | Popular Tags |