Guest User

Untitled

a guest
May 2nd, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.80 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import java.util.ArrayList;
  5. import java.sql.DriverManager;
  6. import java.sql.Connection;
  7. import java.sql.SQLException;
  8.  
  9. public class DataSourceWizard extends JPanel implements ActionListener
  10. {
  11. private CardLayout layout;
  12. private JPanel pnlCards;
  13. private JPanel pnlControls;
  14. private JButton btnOk;
  15. private JButton btnCancel;
  16. private JButton btnNext;
  17. private JButton btnPrevious;
  18. private int cardIndex = 0;
  19.  
  20. private javax.swing.Timer timer = new javax.swing.Timer(100, this);
  21.  
  22. private DataSourceWizardPanel wpnlDataSource;
  23. private JLabel lblDataSource;
  24. private DefaultComboBoxModel mdlDataSource;
  25. private JComboBox cmbDataSource;
  26. private JLabel lblConnectionString;
  27. private JTextField txtConnectionString;
  28. private JLabel lblUsername;
  29. private JTextField txtUsername;
  30. private JLabel lblPassword;
  31. private JTextField txtPassword;
  32. private JButton btnTestConnection;
  33.  
  34. private DataSourceWizardPanel wpnlConnectionSettings;
  35.  
  36. public DataSourceWizard()
  37. {
  38. this.initializeComponent();
  39. this.timer.start();
  40. }
  41. private final void initializeComponent()
  42. {
  43. int yComponent = 20;
  44.  
  45. this.layout = new CardLayout();
  46. this.pnlCards = new JPanel(this.layout);
  47. this.pnlControls = new JPanel(new FlowLayout(FlowLayout.RIGHT));
  48. this.btnOk = new JButton("Finish");
  49. this.btnCancel = new JButton("Cancel");
  50. this.btnNext = new JButton("Next »");
  51. this.btnPrevious = new JButton("« Back");
  52. //
  53. // The Data Source panel
  54. //
  55. this.wpnlDataSource = new DataSourceWizardPanel(this,
  56. "Data source setup",
  57. "Choose the data source you want your data to be extracted from.");
  58. this.lblDataSource = new JLabel("Data Source:");
  59. this.mdlDataSource = new DefaultComboBoxModel();
  60. this.cmbDataSource = new JComboBox(this.mdlDataSource);
  61. this.lblConnectionString = new JLabel("Connection string:");
  62. this.txtConnectionString = new JTextField("jdbc:odbc:");
  63. this.lblUsername = new JLabel("Username:");
  64. this.txtUsername = new JTextField();
  65. this.lblPassword = new JLabel("Password:");
  66. this.txtPassword = new JPasswordField();
  67. this.btnTestConnection = new JButton("Test Connection");
  68. this.lblDataSource.setDisplayedMnemonic(KeyEvent.VK_D);
  69. this.lblConnectionString.setDisplayedMnemonic(KeyEvent.VK_C);
  70. this.lblUsername.setDisplayedMnemonic(KeyEvent.VK_U);
  71. this.lblPassword.setDisplayedMnemonic(KeyEvent.VK_P);
  72. this.lblDataSource.setLabelFor(this.cmbDataSource);
  73. this.lblConnectionString.setLabelFor(this.txtConnectionString);
  74. this.lblUsername.setLabelFor(this.txtUsername);
  75. this.lblPassword.setLabelFor(this.txtPassword);
  76. this.btnTestConnection.setMnemonic(KeyEvent.VK_T);
  77. this.btnTestConnection.setDefaultCapable(true);
  78. this.btnTestConnection.addActionListener(this);
  79. this.lblDataSource.setBounds(20, yComponent, 150, 20);
  80. this.cmbDataSource.setBounds(160, yComponent, 180, 20);
  81. this.lblConnectionString.setBounds(20, yComponent += 25, 150, 20);
  82. this.txtConnectionString.setBounds(160, yComponent, 180, 20);
  83. this.lblUsername.setBounds(20, yComponent += 25, 150, 20);
  84. this.txtUsername.setBounds(160, yComponent, 180, 20);
  85. this.lblPassword.setBounds(20, yComponent += 25, 150, 20);
  86. this.txtPassword.setBounds(160, yComponent, 180, 20);
  87. this.btnTestConnection.setBounds(160, yComponent += 25, 120, 20);
  88. this.wpnlDataSource.addComponent(this.lblDataSource);
  89. this.wpnlDataSource.addComponent(this.cmbDataSource);
  90. this.wpnlDataSource.addComponent(this.lblConnectionString);
  91. this.wpnlDataSource.addComponent(this.txtConnectionString);
  92. this.wpnlDataSource.addComponent(this.lblUsername);
  93. this.wpnlDataSource.addComponent(this.txtUsername);
  94. this.wpnlDataSource.addComponent(this.lblPassword);
  95. this.wpnlDataSource.addComponent(this.txtPassword);
  96. this.wpnlDataSource.addComponent(this.btnTestConnection);
  97. this.mdlDataSource.addElement("JDBC");
  98. this.wpnlDataSource.pnlContent.setPreferredSize(new Dimension(
  99. this.wpnlDataSource.pnlContent.getPreferredSize().width, yComponent += 25));
  100. //
  101. // The Connection Settings panel
  102. //
  103. this.wpnlConnectionSettings = new DataSourceWizardPanel(this,
  104. "Connection settings",
  105. "Miscellaneous options required for a connection to be established.");
  106. //
  107. // The Controls panel
  108. //
  109. this.pnlControls.add(this.btnPrevious);
  110. this.pnlControls.add(this.btnNext);
  111. this.pnlControls.add(this.btnCancel);
  112. this.pnlControls.add(this.btnOk);
  113. this.btnPrevious.addActionListener(this);
  114. this.btnNext.addActionListener(this);
  115.  
  116. this.pnlCards.add(this.wpnlDataSource);
  117. this.pnlCards.add(this.wpnlConnectionSettings);
  118.  
  119. this.setLayout(new BorderLayout());
  120. this.add(this.pnlCards, BorderLayout.CENTER);
  121. this.add(this.pnlControls, BorderLayout.SOUTH);
  122. }
  123. public void actionPerformed(ActionEvent e)
  124. {
  125. if(e.getSource() == this.timer)
  126. {
  127. if(this.cardIndex <= 0)
  128. {
  129. this.cardIndex = 0;
  130. this.btnPrevious.setEnabled(false);
  131. }
  132. else if(this.cardIndex > 0)
  133. this.btnPrevious.setEnabled(true);
  134. if(this.cardIndex >= this.pnlCards.getComponents().length - 1)
  135. {
  136. this.cardIndex = this.pnlCards.getComponents().length - 1;
  137. this.btnNext.setEnabled(false);
  138. }
  139. else if(this.cardIndex < this.pnlCards.getComponents().length - 1)
  140. this.btnNext.setEnabled(true);
  141. }
  142. else if(e.getSource() == this.btnPrevious &&
  143. this.cardIndex > 0)
  144. {
  145. this.layout.previous(this.pnlCards);
  146. this.cardIndex--;
  147. }
  148. else if(e.getSource() == this.btnNext &&
  149. this.cardIndex < this.pnlCards.getComponents().length-1)
  150. {
  151. boolean isNextAllowed = false;
  152. try
  153. {
  154. switch(this.cardIndex)
  155. {
  156. case 0:
  157. isNextAllowed = this.cmbDataSource.
  158. getSelectedItem().equals("JDBC") &&
  159. this.txtConnectionString.getText().
  160. startsWith("jdbc:odbc:");
  161. break;
  162. default:
  163. isNextAllowed = true;
  164. break;
  165. }
  166. }
  167. catch(Exception ex){ isNextAllowed = false; }
  168. if(isNextAllowed)
  169. {
  170. this.layout.next(this.pnlCards);
  171. this.cardIndex++;
  172. }
  173. }
  174. else if(e.getSource() == this.btnTestConnection)
  175. {
  176. boolean testPassed = false;
  177. try
  178. {
  179. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  180. Connection connection = DriverManager.getConnection(
  181. this.txtConnectionString.getText().trim(),
  182. this.txtUsername.getText().trim(),
  183. this.txtPassword.getText().trim());
  184. connection.close();
  185. testPassed = true;
  186. }
  187. catch(ClassNotFoundException ex)
  188. {
  189. testPassed = false;
  190. }
  191. catch(SQLException ex)
  192. {
  193. testPassed = false;
  194. }
  195. if(testPassed)
  196. {
  197. JOptionPane.showMessageDialog((JFrame)null,
  198. "A connection was successfully established with the provided data source.",
  199. "Test Passed", JOptionPane.INFORMATION_MESSAGE);
  200. }
  201. else
  202. {
  203. JOptionPane.showMessageDialog((JFrame)null,
  204. "A connection could not be established with the provided data source.",
  205. "Test Failed", JOptionPane.ERROR_MESSAGE);
  206. }
  207. }
  208. }
  209. public static void main(String[] args)
  210. {
  211. JFrame frame = new JFrame("Test");
  212. frame.setSize(400, 350);
  213. frame.setLocationRelativeTo(null);
  214. frame.getContentPane().add(new DataSourceWizard());
  215. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  216. frame.setVisible(true);
  217. }
  218. }
  219. class DataSourceWizardPanel extends JPanel
  220. {
  221. private Font defaultFont = new Font("Trebuchet MS", Font.PLAIN, 11);
  222. private JLabel lblName = null;
  223. private JLabel lblDescription = null;
  224. private JPanel pnlHeader = null;
  225. public JPanel pnlContent = null;
  226. private DataSourceWizard wizard = null;
  227. private ArrayList<Component> components = null;
  228. private JScrollPane spnContent = null;
  229. public DataSourceWizardPanel(DataSourceWizard wizard)
  230. {
  231. this.wizard = wizard;
  232. this.initializeComponent();
  233. this.setFont(new Font("Trebuchet MS", Font.PLAIN, 11));
  234. }
  235. public DataSourceWizardPanel(DataSourceWizard wizard, String name, String description)
  236. {
  237. this(wizard);
  238. this.lblName.setText(name);
  239. this.lblDescription.setText("<HTML>" + description);
  240. }
  241. private final void initializeComponent()
  242. {
  243. this.lblName = new JLabel("Untitled");
  244. this.lblDescription = new JLabel("<HTML>No description provided");
  245. this.pnlHeader = new JPanel(new BorderLayout());
  246. this.pnlContent = new JPanel(null);
  247. this.components = new ArrayList<Component>();
  248. this.spnContent = new JScrollPane(this.pnlContent);
  249.  
  250. //this.pnlHeader.setPreferredSize(new Dimension(
  251. // this.pnlHeader.getPreferredSize().width, 90));
  252. this.pnlHeader.setBackground(new Color(255, 255, 255, 200));
  253. this.pnlHeader.setBorder(BorderFactory.createCompoundBorder(
  254. BorderFactory.createLineBorder(new Color(50, 50, 50, 100), 1),
  255. BorderFactory.createEmptyBorder(5, 5, 5, 5)
  256. ));
  257. this.pnlHeader.add(this.lblName, BorderLayout.NORTH);
  258. this.pnlHeader.add(this.lblDescription, BorderLayout.CENTER);
  259. this.spnContent.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
  260.  
  261. super.setLayout(new BorderLayout());
  262. super.add(this.pnlHeader, BorderLayout.NORTH);
  263. super.add(this.spnContent, BorderLayout.CENTER);
  264. }
  265. public void addComponent(JComponent component)
  266. {
  267. if(this.defaultFont != null) component.setFont(this.defaultFont);
  268. this.pnlContent.add(component);
  269. }
  270. public void setFont(Font font)
  271. {
  272. try
  273. {
  274. this.defaultFont = font;
  275. super.setFont(font);
  276. for(Component component : components)
  277. {
  278. if(component != null)
  279. component.setFont(font);
  280. }
  281. if(this.lblName != null)
  282. {
  283. this.lblName.setFont(new Font(font.getName(), Font.BOLD, font.getSize()));
  284. }
  285. if(this.lblDescription != null) this.lblDescription.setFont(font);
  286. if(this.pnlHeader != null) this.pnlHeader.setFont(font);
  287. if(this.pnlContent != null) this.pnlContent.setFont(font);
  288. }
  289. catch(Exception ex)
  290. {
  291. }
  292. }
  293. }
Add Comment
Please, Sign In to add comment