View Javadoc
1 package com.bonevich.erj.ui; 2 3 import com.bonevich.erj.ErjConstants; 4 import com.bonevich.erj.app.Project; 5 import com.bonevich.erj.db.DatabaseLogin; 6 import com.bonevich.erj.db.SupportedDatabase; 7 import com.bonevich.util.Functor; 8 import com.bonevich.util.JarFileClassLoader; 9 10 import org.apache.commons.logging.Log; 11 import org.apache.commons.logging.LogFactory; 12 13 import java.awt.*; 14 import java.awt.event.ActionEvent; 15 import java.awt.event.ActionListener; 16 import java.io.File; 17 import java.net.URL; 18 import java.net.URLClassLoader; 19 import java.util.*; 20 import java.util.jar.JarEntry; 21 import java.util.jar.JarFile; 22 23 import javax.swing.*; 24 import javax.swing.border.Border; 25 import javax.swing.border.TitledBorder; 26 27 public class TargetDatabaseDialog extends JDialog 28 implements ErjConstants 29 { 30 ////////////////////////////////////////////////////////// 31 // Constants 32 public static final String NAME_STR = "Database Name: "; 33 public static final String URL_STR = "Database URL: "; 34 public static final String DRIVER_STR = "Driver Class: "; 35 public static final String PROPS_STR = "Login Properties"; 36 public static final String EDIT_STR = "Edit Database Login Properties"; 37 38 private static final Log _logger = LogFactory.getLog(TargetDatabaseDialog.class); 39 40 ////////////////////////////////////////////////////////// 41 // Attributes 42 private Project _project; 43 44 private JButton _okBtn = new JButton("OK"); 45 private JButton _applyBtn = new JButton("Apply"); 46 private JButton _cancelBtn = new JButton("Cancel"); 47 48 private JPanel _propPane = new JPanel(); 49 private JLabel _nameLabel = new JLabel(NAME_STR); 50 private JTextField _nameText = new JTextField(20); 51 private JLabel _urlLabel = new JLabel(URL_STR); 52 private JTextField _urlText = new JTextField(20); 53 private JLabel _driverLabel = new JLabel(DRIVER_STR); 54 private JComboBox _driverList; 55 private JPanel _loginPropsPane = new JPanel(); 56 private Border _loginPropsTitleBrdr = BorderFactory.createTitledBorder(PROPS_STR); 57 private Border _loginPropsBrdr = 58 BorderFactory.createCompoundBorder( 59 _loginPropsTitleBrdr, 60 BorderFactory.createEmptyBorder(5,5,5,5) 61 ); 62 { 63 _propPane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5)); 64 _nameLabel.setFont(EDITOR_FONT); 65 _nameLabel.setForeground(EDITOR_COLOR); 66 _urlLabel.setFont(EDITOR_FONT); 67 _urlLabel.setForeground(EDITOR_COLOR); 68 _driverLabel.setFont(EDITOR_FONT); 69 _driverLabel.setForeground(EDITOR_COLOR); 70 ((TitledBorder) _loginPropsTitleBrdr).setTitleFont(EDITOR_FONT); 71 ((TitledBorder) _loginPropsTitleBrdr).setTitleColor(EDITOR_COLOR); 72 } 73 74 private Map _fields = new HashMap(5); 75 76 ////////////////////////////////////////////////////////// 77 // Constructors 78 public TargetDatabaseDialog(Project project) 79 { 80 super(ErjFrame.getInstance(), EDIT_STR, true); 81 _project = project; 82 setResizable(false); 83 initComponents(); 84 updateUI(); 85 } 86 87 private void initComponents() 88 { 89 _propPane.setLayout(new GridBagLayout()); 90 GridBagConstraints constraints = new GridBagConstraints(); 91 92 // name label and textbox 93 constraints.gridwidth = 1; 94 constraints.anchor = GridBagConstraints.WEST; 95 _propPane.add(_nameLabel, constraints); 96 _propPane.add(Box.createHorizontalStrut(10)); 97 constraints.gridwidth = GridBagConstraints.REMAINDER; 98 _propPane.add(_nameText, constraints); 99 100 // database url label and textbox 101 constraints.gridwidth = 1; 102 _propPane.add(_urlLabel, constraints); 103 _propPane.add(Box.createHorizontalStrut(10)); 104 constraints.gridwidth = GridBagConstraints.REMAINDER; 105 _propPane.add(_urlText, constraints); 106 107 // database driver label and combo-box 108 _driverList = new JComboBox(_project.getTargetDatabase().getDriverNames()); 109 _driverList.setEditable(false); 110 111 constraints.gridwidth = 1; 112 _propPane.add(_driverLabel, constraints); 113 _propPane.add(Box.createHorizontalStrut(10)); 114 constraints.insets = new Insets(5,0,5,0); 115 constraints.gridwidth = GridBagConstraints.REMAINDER; 116 constraints.fill = GridBagConstraints.NONE; 117 _propPane.add(_driverList, constraints); 118 119 // login properties pane and fields 120 constraints.gridwidth = 1; 121 constraints.insets = new Insets(0,0,0,0); 122 constraints.gridwidth = GridBagConstraints.REMAINDER; 123 constraints.fill = GridBagConstraints.HORIZONTAL; 124 initLoginPropertiesComponents(); 125 126 // buttons 127 JPanel btnPane = new JPanel(); 128 ((FlowLayout) btnPane.getLayout()).setAlignment(FlowLayout.CENTER); 129 btnPane.add(_okBtn); 130 btnPane.add(_applyBtn); 131 btnPane.add(_cancelBtn); 132 133 constraints.anchor = GridBagConstraints.SOUTH; 134 constraints.insets = new Insets(10,0,0,0); 135 constraints.fill = GridBagConstraints.HORIZONTAL; 136 constraints.gridwidth = 5; 137 138 _propPane.add(btnPane, constraints); 139 140 getContentPane().add(_propPane, BorderLayout.CENTER); 141 pack(); 142 setLocationRelativeTo(ErjFrame.getInstance()); 143 addActionListeners(); 144 } 145 146 private void initLoginPropertiesComponents() 147 { 148 _loginPropsPane.setLayout(new GridBagLayout()); 149 _loginPropsPane.setAlignmentX(Component.LEFT_ALIGNMENT); 150 _loginPropsPane.setBorder(_loginPropsBrdr); 151 GridBagConstraints constraints = new GridBagConstraints(); 152 constraints.anchor = GridBagConstraints.WEST; 153 154 java.util.List fields = _project.getTargetDatabase().getSupportedDatabase().getLoginFieldMaps(); 155 if (_logger.isDebugEnabled()) 156 { 157 _logger.debug("fields = " + fields); 158 } 159 Iterator itr = fields.iterator(); 160 while (itr.hasNext()) 161 { 162 // field label and textbox 163 Map field = (Map) itr.next(); 164 JLabel fieldLabel = new JLabel((String)field.get(SupportedDatabase.LABEL)); 165 fieldLabel.setToolTipText((String)field.get(SupportedDatabase.TOOLTIP)); 166 JTextField fieldText = null; 167 String name = (String) field.get(SupportedDatabase.NAME); 168 if ("password".equalsIgnoreCase(name)) 169 { 170 fieldText = new JPasswordField(20); 171 } 172 else 173 { 174 fieldText = new JTextField(20); 175 } 176 _fields.put(name, fieldText); 177 178 constraints.gridwidth = 1; 179 _loginPropsPane.add(fieldLabel, constraints); 180 _loginPropsPane.add(Box.createHorizontalStrut(10)); 181 constraints.gridwidth = GridBagConstraints.REMAINDER; 182 _loginPropsPane.add(fieldText, constraints); 183 } 184 _propPane.add(_loginPropsPane, constraints); 185 } 186 187 private void addActionListeners() 188 { 189 _okBtn.addActionListener( 190 new ActionListener() 191 { 192 public void actionPerformed(ActionEvent e) 193 { 194 //System.err.println("OK!! " + e.getSource()); 195 updateProject(); 196 dispose(); 197 } 198 } 199 ); 200 201 _applyBtn.addActionListener( 202 new ActionListener() 203 { 204 public void actionPerformed(ActionEvent e) 205 { 206 //System.err.println("Apply!! " + e.getSource()); 207 updateProject(); 208 } 209 } 210 ); 211 212 _cancelBtn.addActionListener( 213 new ActionListener() 214 { 215 public void actionPerformed(ActionEvent e) 216 { 217 //System.err.println("Cancel!! " + e.getSource()); 218 dispose(); 219 } 220 } 221 ); 222 } 223 224 private void updateUI() 225 { 226 _nameText.setText(_project.getTargetDatabase().getDatabaseName()); 227 _urlText.setText(_project.getTargetDatabase().getJdbcUrl()); 228 _driverList.setSelectedItem(_project.getTargetDatabase().getDriver()); 229 230 Iterator itr = _fields.keySet().iterator(); 231 while (itr.hasNext()) 232 { 233 String name = (String) itr.next(); 234 JTextField field = (JTextField) _fields.get(name); 235 field.setText(_project.getTargetDatabase().getLoginProperties().getProperty(name)); 236 } 237 } 238 239 private void updateProject() 240 { 241 DatabaseLogin login = _project.getTargetDatabase(); 242 login.setDatabaseName(_nameText.getText()); 243 login.setJdbcUrl(_urlText.getText()); 244 login.setDriver((String) _driverList.getSelectedItem()); 245 246 Iterator itr = _fields.keySet().iterator(); 247 while (itr.hasNext()) 248 { 249 String name = (String) itr.next(); 250 JTextField field = (JTextField) _fields.get(name); 251 login.setLoginProperty(name, field.getText()); 252 } 253 } 254 255 } /* end class TargetDatabaseDialog */

This page was automatically generated by Maven