1 30 package com.genimen.djeneric.tools.modeler.extenteditor; 31 32 import java.awt.BorderLayout ; 33 import java.awt.Color ; 34 import java.awt.Dimension ; 35 import java.awt.FlowLayout ; 36 import java.awt.Frame ; 37 import java.awt.Toolkit ; 38 import java.awt.event.ActionEvent ; 39 import java.awt.event.KeyEvent ; 40 import java.awt.event.KeyListener ; 41 import java.util.Vector ; 42 43 import javax.swing.DefaultComboBoxModel ; 44 import javax.swing.JButton ; 45 import javax.swing.JComboBox ; 46 import javax.swing.JDialog ; 47 import javax.swing.JLabel ; 48 import javax.swing.JPanel ; 49 import javax.swing.border.Border ; 50 import javax.swing.border.EtchedBorder ; 51 52 import com.genimen.djeneric.language.Messages; 53 import com.genimen.djeneric.repository.DjExtent; 54 import com.genimen.djeneric.repository.DjPersistenceManager; 55 import com.genimen.djeneric.repository.exceptions.ObjectNotDefinedException; 56 import com.genimen.djeneric.ui.DjFocusMeLater; 57 import com.genimen.djeneric.ui.Util; 58 import com.genimen.djeneric.util.DjLogger; 59 60 public class MappingChooser extends JDialog implements KeyListener 61 { 62 private static final long serialVersionUID = 1L; 63 JPanel panel1 = new JPanel (); 64 BorderLayout borderLayout1 = new BorderLayout (); 65 JPanel jPanel1 = new JPanel (); 66 JPanel jPanel2 = new JPanel (); 67 BorderLayout borderLayout2 = new BorderLayout (); 68 JPanel jPanel3 = new JPanel (); 69 JButton _butOk = new JButton (); 70 JButton _butCancel = new JButton (); 71 72 ColumnMapping[] mapping = new ColumnMapping[]{ 73 new ColumnMapping(DjPersistenceManager.MAPPING_OBJECT_ID, "Object ID"), 74 new ColumnMapping(DjPersistenceManager.MAPPING_STR, "String250"), 75 new ColumnMapping(DjPersistenceManager.MAPPING_TXT, "String4000"), 76 new ColumnMapping(DjPersistenceManager.MAPPING_NUM, "Numeric"), 77 new ColumnMapping(DjPersistenceManager.MAPPING_DAT, "Timestamp"), 78 new ColumnMapping(DjPersistenceManager.MAPPING_REL, "<Relation>"), 79 new ColumnMapping(DjPersistenceManager.MAPPING_LNG, "byte[]")}; 80 81 ColumnMapping[] mappingWithoutId = new ColumnMapping[mapping.length - 1]; 82 83 JComboBox _cbbType = new JComboBox (mapping); 84 JComboBox _cbbNum = new JComboBox (); 85 FlowLayout flowLayout1 = new FlowLayout (); 86 87 String _initialValue; 88 int _initialNum; 89 boolean _wasCancelled = true; 90 DjExtent _extent; 91 FlowLayout flowLayout2 = new FlowLayout (); 92 Border border1; 93 JPanel jPanel4 = new JPanel (); 94 BorderLayout borderLayout3 = new BorderLayout (); 95 JLabel jLabel1 = new JLabel (); 96 JLabel _lblPropertyName = new JLabel (); 97 98 public MappingChooser(Frame frame, String title, String initalValue, DjExtent extent, String colName) 99 { 100 super(frame, title, true); 101 try 102 { 103 System.arraycopy(mapping, 1, mappingWithoutId, 0, mapping.length - 1); 104 105 _extent = extent; 106 107 if (initalValue.equalsIgnoreCase(DjPersistenceManager.MAPPING_OBJECT_ID)) 108 { 109 _initialValue = initalValue; 110 _initialNum = -1; 111 } 112 else 113 { 114 _initialValue = initalValue.substring(0, 3); 115 _initialNum = Integer.parseInt(initalValue.substring(3)); 116 try 117 { 118 _extent.getPropertyByMapping(DjPersistenceManager.MAPPING_OBJECT_ID); 119 _cbbType.setModel(new DefaultComboBoxModel (mappingWithoutId)); 121 } 122 catch (Exception x) 123 { 124 DjLogger.log(x); 125 } 126 } 127 128 jbInit(); 129 _lblPropertyName.setText(colName); 130 131 ColumnMapping curMap = new ColumnMapping(_initialValue, _initialValue); 132 for (int i = 0; i < _cbbType.getModel().getSize(); i++) 133 { 134 if (_cbbType.getModel().getElementAt(i).equals(curMap)) 135 { 136 _cbbType.setSelectedIndex(i); 137 break; 138 } 139 } 140 141 _cbbNum.setSelectedItem(new Integer (_initialNum)); 142 pack(); 143 144 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 145 Dimension frameSize = getSize(); 146 setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2); 147 DjFocusMeLater.getNeededComponent(_cbbType).addKeyListener(this); 148 DjFocusMeLater.getNeededComponent(_cbbNum).addKeyListener(this); 149 addKeyListener(this); 150 setVisible(true); 151 } 152 catch (Exception ex) 153 { 154 DjLogger.log(ex); 155 } 156 } 157 158 void jbInit() throws Exception 159 { 160 border1 = new EtchedBorder (EtchedBorder.RAISED, Color.white, new Color (142, 142, 142)); 161 panel1.setLayout(borderLayout1); 162 jPanel2.setLayout(borderLayout2); 163 _butOk.setText(Messages.getString("global.Ok")); 164 _butOk.addKeyListener(new java.awt.event.KeyAdapter () 165 { 166 public void keyPressed(KeyEvent e) 167 { 168 _cbbType_keyPressed(e); 169 } 170 }); 171 _butOk.addActionListener(new java.awt.event.ActionListener () 172 { 173 public void actionPerformed(ActionEvent e) 174 { 175 _butOk_actionPerformed(e); 176 } 177 }); 178 _butCancel.setText(Messages.getString("global.Cancel")); 179 _butCancel.addKeyListener(new java.awt.event.KeyAdapter () 180 { 181 public void keyPressed(KeyEvent e) 182 { 183 _cbbType_keyPressed(e); 184 } 185 }); 186 _butCancel.addActionListener(new java.awt.event.ActionListener () 187 { 188 public void actionPerformed(ActionEvent e) 189 { 190 _butCancel_actionPerformed(e); 191 } 192 }); 193 jPanel1.setLayout(flowLayout1); 194 _cbbType.setPreferredSize(new Dimension (100, 23)); 195 _cbbType.addKeyListener(new java.awt.event.KeyAdapter () 196 { 197 public void keyPressed(KeyEvent e) 198 { 199 _cbbType_keyPressed(e); 200 } 201 }); 202 _cbbType.addActionListener(new java.awt.event.ActionListener () 203 { 204 public void actionPerformed(ActionEvent e) 205 { 206 _cbbType_actionPerformed(e); 207 } 208 }); 209 _cbbNum.setPreferredSize(new Dimension (80, 23)); 210 _cbbNum.addKeyListener(new java.awt.event.KeyAdapter () 211 { 212 public void keyPressed(KeyEvent e) 213 { 214 _cbbType_keyPressed(e); 215 } 216 }); 217 jPanel3.setLayout(flowLayout2); 218 flowLayout2.setAlignment(FlowLayout.RIGHT); 219 jPanel1.setAlignmentX((float) 0.0); 220 jPanel1.setAlignmentY((float) 0.0); 221 jPanel4.setLayout(borderLayout3); 222 jLabel1.setText(Messages.getString("MappingChooser.MappingFor") + " "); 223 _lblPropertyName.setText(Messages.getString("global.Property")); 224 getContentPane().add(panel1); 225 panel1.add(jPanel1, BorderLayout.CENTER); 226 jPanel1.add(_cbbType, null); 227 jPanel1.add(_cbbNum, null); 228 panel1.add(jPanel2, BorderLayout.SOUTH); 229 jPanel2.add(jPanel3, BorderLayout.CENTER); 230 jPanel3.add(_butCancel, null); 231 jPanel3.add(_butOk, null); 232 panel1.add(jPanel4, BorderLayout.NORTH); 233 jPanel4.add(jLabel1, BorderLayout.WEST); 234 jPanel4.add(_lblPropertyName, BorderLayout.CENTER); 235 Util.sizeButtons(jPanel3); 236 } 237 238 void _butOk_actionPerformed(ActionEvent e) 239 { 240 _wasCancelled = false; 241 setVisible(false); 242 } 243 244 void _butCancel_actionPerformed(ActionEvent e) 245 { 246 setVisible(false); 247 } 248 249 public String getMapping() 250 { 251 String num = ""; 252 if (_cbbNum.isEnabled()) 253 { 254 if (_cbbNum.getSelectedItem() == null) return null; 255 String n = _cbbNum.getSelectedItem().toString(); 256 if (n.length() == 1) n = "0" + n; 257 num += n; 258 } 259 ColumnMapping map = (ColumnMapping) _cbbType.getSelectedItem(); 260 return map.getValue() + num; 261 } 262 263 public boolean isCancelled() 264 { 265 return _wasCancelled; 266 } 267 268 private boolean propertyIsMapped(DjExtent extent, String map) 269 { 270 try 271 { 272 extent.getPropertyByMapping(map); 273 return true; 274 } 275 catch (ObjectNotDefinedException ond) 276 { 277 } 279 DjExtent[] specializations = extent.getSpecializations(); 280 for (int i = 0; i < specializations.length; i++) 281 { 282 if (propertyIsMapped(specializations[i], map)) return true; 283 } 284 return false; 285 } 286 287 void _cbbType_actionPerformed(ActionEvent e) 288 { 289 ColumnMapping mapping = (ColumnMapping) _cbbType.getSelectedItem(); 290 String val = mapping.getValue(); 291 if (val.equals(DjPersistenceManager.MAPPING_OBJECT_ID)) _cbbNum.setEnabled(false); 292 else 293 { 294 _cbbNum.setEnabled(true); 295 Vector v = new Vector (); 296 297 int sz = DjPersistenceManager.getMaxMappingCount(val); 298 299 for (int i = 1; i <= sz; i++) 300 { 301 String n = "" + i; 302 if (n.length() == 1) n = "0" + n; 303 String map = val + n; 304 if ((i == _initialNum) && val.equals(_initialValue)) 305 { 306 v.add(new Integer (i)); 307 } 308 else 309 { 310 if (!propertyIsMapped(_extent, map)) v.add(new Integer (i)); 311 } 312 } 313 _cbbNum.setModel(new DefaultComboBoxModel (v)); 314 if (val.equals(_initialValue)) 315 { 316 _cbbNum.setSelectedItem(new Integer (_initialNum)); 317 } 318 } 319 } 320 321 public void keyTyped(KeyEvent e) 322 { 323 } 324 325 public void keyPressed(KeyEvent e) 326 { 327 if (_cbbNum.isPopupVisible()) return; 328 if (_cbbType.isPopupVisible()) return; 329 330 if (e.getKeyCode() == KeyEvent.VK_ESCAPE) 331 { 332 _butCancel_actionPerformed(null); 333 } 334 335 if (e.getKeyCode() == KeyEvent.VK_ENTER) 336 { 337 _butOk_actionPerformed(null); 338 } 339 } 340 341 public void keyReleased(KeyEvent e) 342 { 343 } 344 345 void _cbbType_keyPressed(KeyEvent e) 346 { 347 if (_cbbNum.isPopupVisible() || _cbbType.isPopupVisible()) return; 348 349 if (e.getKeyCode() == KeyEvent.VK_ENTER) _butOk_actionPerformed(null); 350 if (e.getKeyCode() == KeyEvent.VK_ESCAPE) _butCancel_actionPerformed(null); 351 } 352 } 353 354 class ColumnMapping 355 { 356 357 String _value; 358 String _title; 359 360 public ColumnMapping(String value, String title) 361 { 362 _value = value; 363 _title = title; 364 } 365 366 public String toString() 367 { 368 return _title; 369 } 370 371 public String getValue() 372 { 373 return _value; 374 } 375 376 public boolean equals(Object o) 377 { 378 if (!(o instanceof ColumnMapping)) 379 { 380 return false; 381 } 382 if ((_value == null) || (o == null)) return false; 383 384 return _value.equals(((ColumnMapping) o)._value); 385 } 386 } | Popular Tags |