Advertisement
Guest User

Untitled

a guest
Jun 1st, 2016
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.84 KB | None | 0 0
  1. /*
  2. * IdmsJcf.java
  3. *
  4. * (C) 2008 CA, Inc.
  5. *
  6. */
  7.  
  8. package ca.idms.jcf;
  9.  
  10. import java.sql.DriverManager;
  11. import java.io.PrintWriter;
  12. import java.net.*;
  13. import java.applet.Applet;
  14. import java.awt.*;
  15. import java.awt.event.*;
  16. import javax.swing.*;
  17. import javax.swing.table.*;
  18. import javax.swing.event.*;
  19. import javax.swing.border.*;
  20.  
  21. /**
  22. * Java version of OCF, the IDMS Online Command Facility.
  23. *
  24. * <P>This class implements a UI on top of JdbcTable, and allows SQL
  25. * query and update statments to be executed with the results displayed
  26. * using the Java extension "Swing" package from JavaSoft (Sun).
  27. *
  28. * <P>Note: This source code and the classes compiled from it are
  29. * provided only for demonstration purposes, and are not a supported
  30. * part of the CA IDMS Server 4.2 product.
  31. *
  32. * @version 2.00 11/13/02
  33. * @author Dave Ross
  34. */
  35. public class IdmsJcf extends JApplet
  36. {
  37. static boolean isApp;
  38. static String url;
  39. static String name = "JdbcTest";
  40.  
  41. //
  42. // applet entry point
  43. //
  44. public void init()
  45. {
  46. String debug = getParameter("debug");
  47. // the security manager may not allow this in Java 1.3 or later
  48. // if so, use a caidms.properties file in the CODEBASE
  49. // to enable tracing for the CA IDMS JDBC Driver
  50. if (debug != null && debug.equals("true"))
  51. setLogWriter();
  52.  
  53. isApp = false;
  54.  
  55. try
  56. {
  57. String host = getDocumentBase().getHost();
  58. if (host == null || host.length() == 0)
  59. host = InetAddress.getLocalHost().getHostName();
  60. url = "jdbc:idms://" + host + ":3709" + "/";
  61. }
  62. catch (UnknownHostException e)
  63. {
  64. System.out.println("IdmsJcf.init(): " + e);
  65. showStatus("Unknown Host");
  66. }
  67. new JcfFrame();
  68. }
  69.  
  70. //
  71. // application entry point
  72. //
  73. public static void main(String s[])
  74. {
  75. url = "jdbc:idms:";
  76.  
  77. for (int i = 0; i < s.length; i++)
  78. {
  79. if (s[i].equals("-d"))
  80. setLogWriter();
  81.  
  82. if (s[i].equals("-h"))
  83. {
  84. if (++i < s.length)
  85. url = url + "//" + s[i] + "/";
  86. }
  87.  
  88. if (s[i].equals("-n"))
  89. {
  90. if (++i < s.length)
  91. name = s[i];
  92. }
  93. }
  94. isApp = true;
  95. new JcfFrame();
  96. }
  97.  
  98. //
  99. // try to enable trace
  100. //
  101. static void setLogWriter()
  102. {
  103. try
  104. {
  105. System.err.println("Enabling DriverManager trace...");
  106. DriverManager.setLogWriter(new PrintWriter(System.out));
  107. }
  108. catch (SecurityException e)
  109. {
  110. System.err.println("Cannot enable trace: " + e);
  111. }
  112. }
  113. }
  114.  
  115. class JcfFrame implements LayoutManager
  116. {
  117. Dimension origin = new Dimension(0, 0);
  118.  
  119. JFrame frame; // application window.
  120. JPanel mainPanel; // content window
  121. JButton catalogButton;
  122. JButton executeButton;
  123. JButton connectButton;
  124. JButton disconnButton;
  125. JTextArea query;
  126. JComponent queryScroller;
  127. JdbcTable jdbc; // table model
  128. JTable table;
  129. JScrollPane tableScroller;
  130.  
  131. JPanel connectPanel; // connect dialog box
  132. JTextField uid;
  133. JTextField pwd;
  134. JTextField url;
  135.  
  136. JPanel catalogPanel; // catalog dialog box
  137. JTextField sch;
  138. JTextField tab;
  139. JTextField col;
  140.  
  141. //Ken Grant 30/OCT/2006 - add fields for getCrossReference method
  142. JTextField fkSch;
  143. JTextField fkTab;
  144.  
  145. //Ken Grant 31/OCT/2006 - add fields that we will hide
  146. JTextField invisibleField;
  147. JTextField invisibleField2;
  148.  
  149. //Ken Grant 31/OCT/2006 - add combo box for catalog options
  150. JComboBox queryList;
  151.  
  152.  
  153. public JcfFrame()
  154. {
  155. // //
  156. // // Set the look and feel
  157. // //
  158. // String laf = UIManager.getSystemLookAndFeelClassName();
  159. // try
  160. // {
  161. // // UIManager.setLookAndFeel(laf);
  162. // UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
  163. // }
  164. // catch (UnsupportedLookAndFeelException e)
  165. // { System.err.println("Warning: UnsupportedLookAndFeel: " + laf); }
  166. // catch (Exception e)
  167. // { System.err.println("Error loading " + laf + ": " + e); }
  168.  
  169. //
  170. // Create the components.
  171. //
  172. createConnectDialog();
  173. createCatalogDialog();
  174. createButtons();
  175. createQuery();
  176. createTable();
  177.  
  178. //
  179. // Add all the components to the main panel.
  180. //
  181. mainPanel = new JPanel();
  182. mainPanel.add(catalogButton);
  183. mainPanel.add(executeButton);
  184. mainPanel.add(connectButton);
  185. mainPanel.add(disconnButton);
  186. mainPanel.add(queryScroller);
  187. mainPanel.add(tableScroller);
  188. mainPanel.setLayout(this); // see LayoutContainer(), below
  189.  
  190. //
  191. // Create a Frame and put the main panel in it.
  192. //
  193. frame = new JFrame("IdmsJcf - CA IDMS Java Command Facility");
  194. frame.addWindowListener(new WindowCloser());
  195. frame.setBackground(Color.lightGray);
  196. frame.getContentPane().add(mainPanel);
  197. frame.pack();
  198. frame.setVisible(false);
  199. Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
  200. int x = (d.width - 640) / 2;
  201. int y = (d.height - 480) / 2;
  202. frame.setBounds(x, y, 640, 480);
  203. frame.setVisible(true);
  204. }
  205.  
  206. class WindowCloser extends WindowAdapter
  207. {
  208. public void windowClosing(WindowEvent e)
  209. {
  210. if (jdbc != null) jdbc.close();
  211. if (IdmsJcf.isApp) System.exit(0);
  212. }
  213. }
  214.  
  215. /**
  216. * Creates the Connect dialog box.
  217. */
  218. public void createConnectDialog()
  219. {
  220. JPanel labels = new JPanel(false);
  221. labels.setLayout(new GridLayout(0, 1));
  222. labels.add(new JLabel("User ID: ", JLabel.RIGHT));
  223. labels.add(new JLabel("Password: ", JLabel.RIGHT));
  224. labels.add(new JLabel("Database: ", JLabel.RIGHT));
  225.  
  226. uid = new JTextField("jdbc");
  227. pwd = new JPasswordField("");
  228. url = new JTextField(IdmsJcf.url + IdmsJcf.name);
  229.  
  230. JPanel fields = new JPanel(false);
  231. fields.setLayout(new GridLayout(0, 1));
  232. fields.add(uid);
  233. fields.add(pwd);
  234. fields.add(url);
  235.  
  236. connectPanel = new JPanel(false);
  237. connectPanel.setLayout(new BoxLayout(connectPanel, BoxLayout.X_AXIS));
  238. connectPanel.add(labels);
  239. connectPanel.add(fields);
  240. }
  241.  
  242. /**
  243. * Creates the Catalog dialog box.
  244. */
  245. public void createCatalogDialog()
  246. {
  247. JPanel labels = new JPanel(false);
  248. labels.setLayout(new GridLayout(0, 1));
  249. labels.add(new JLabel("Schema Pattern: ", JLabel.RIGHT));
  250. labels.add(new JLabel("Table Pattern: ", JLabel.RIGHT));
  251. labels.add(new JLabel("Column Pattern: ", JLabel.RIGHT));
  252.  
  253. //Ken Grant 30/OCT/2006 - add labels for getCrossRefence method
  254. labels.add(new JLabel("Foreign Key Schema: ", JLabel.RIGHT));
  255. labels.add(new JLabel("Foreign Key Table: ", JLabel.RIGHT));
  256. labels.add(new JLabel("", JLabel.RIGHT));
  257. labels.add(new JLabel("Select Query: ", JLabel.RIGHT));
  258.  
  259.  
  260. sch = new JTextField("%", 18);
  261. tab = new JTextField("%", 18);
  262. col = new JTextField("%", 18);
  263.  
  264. //Ken Grant 30/OCT/2006 - add fields for getCrossReference method
  265. fkSch = new JTextField("", 18);
  266. fkTab = new JTextField("", 18);
  267.  
  268. //Ken Grant 31/OCT/2006 - add fields that we will hide. Makes dialog box prettier.
  269. invisibleField = new JTextField("", 18);
  270. invisibleField2 = new JTextField("", 18);
  271.  
  272. JPanel fields = new JPanel(false);
  273. fields.setLayout(new GridLayout(0, 1));
  274. fields.add(sch);
  275. fields.add(tab);
  276. fields.add(col);
  277.  
  278. //Ken Grant 30/OCT/2006 - add fields for getCrossReference method
  279. fields.add(fkSch);
  280. fields.add(fkTab);
  281. fields.add(invisibleField);
  282.  
  283. //Ken Grant 30/OCT/2006 - add a list box and populate it
  284. queryList = new JComboBox(QUERIES);
  285. fields.add(queryList);
  286. labels.add(new JLabel("", JLabel.RIGHT));
  287. fields.add(invisibleField2);
  288.  
  289.  
  290.  
  291. catalogPanel = new JPanel(false);
  292. catalogPanel.setLayout(new BoxLayout(catalogPanel, BoxLayout.X_AXIS));
  293. catalogPanel.add(labels);
  294. catalogPanel.add(fields);
  295.  
  296.  
  297. }
  298.  
  299.  
  300. /**
  301. * Creates the buttons.
  302. *
  303. * Anonymous inner classes are used to specify the event handlers.
  304. */
  305. private void createButtons()
  306. {
  307. connectButton = new JButton("Connect");
  308. connectButton.addActionListener(new ActionListener() {
  309. public void actionPerformed(ActionEvent e) {
  310. activateConnectDialog();
  311. sizeColumns(); }});
  312.  
  313. disconnButton = new JButton("Disconnect");
  314. disconnButton.addActionListener(new ActionListener() {
  315. public void actionPerformed(ActionEvent e) {
  316. jdbc.close();
  317. sizeColumns(); }});
  318.  
  319. executeButton = new JButton("Execute");
  320. executeButton.addActionListener(new ActionListener() {
  321. public void actionPerformed(ActionEvent e) {
  322. jdbc.execute(query.getText());
  323. sizeColumns(); }});
  324.  
  325. catalogButton = new JButton("Catalog");
  326. catalogButton.addActionListener(new ActionListener() {
  327. public void actionPerformed(ActionEvent e) {
  328. activateCatalogDialog();
  329. sizeColumns();
  330. }
  331. });
  332. }
  333.  
  334. /**
  335. * Creates the query text.
  336. */
  337. private void createQuery()
  338. {
  339. query = new JTextArea("select * from jdbctest");
  340. query.setLineWrap(true);
  341. queryScroller = new JScrollPane(query);
  342. queryScroller.setBorder(new BevelBorder(BevelBorder.LOWERED));
  343. }
  344.  
  345. /**
  346. * Creates the table.
  347. */
  348. private void createTable()
  349. {
  350. jdbc = new JdbcTable("ca.idms.jdbc.IdmsJdbcDriver");
  351.  
  352. table = new JTable(jdbc);
  353. table.setDefaultRenderer(Object.class, new StringRenderer());
  354. table.setDefaultRenderer(Boolean.class, new StringRenderer());
  355. table.setDefaultRenderer(Number.class, new NumberRenderer());
  356. table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
  357. table.setRowSelectionAllowed(false);
  358. table.setColumnSelectionAllowed(false);
  359.  
  360. tableScroller = new JScrollPane(table);
  361. tableScroller.setBorder(new BevelBorder(BevelBorder.LOWERED));
  362. }
  363.  
  364. /**
  365. * Replaces the default Object and Boolean default cell renderers.
  366. *
  367. * <P> Both classes are displayed as Strings. The main difference
  368. * from the default renderer is that SQL NULL values are displayed
  369. * as <null> instead of blanks.
  370. */
  371. class StringRenderer extends DefaultTableCellRenderer
  372. {
  373. public Component getTableCellRendererComponent(
  374. JTable t, Object o, boolean is, boolean has, int r, int c)
  375. {
  376. String s = (o == null) ? "<null>" : o.toString();
  377. return super.getTableCellRendererComponent(t, s, is, has, r, c);
  378. }
  379. }
  380.  
  381. /**
  382. * Replaces the default Number cell renderer.
  383. *
  384. * This class handles types that the default class does
  385. * not seem to (at least with swing 1.1), specifically
  386. * floating point and SQL date types. It also explicitly
  387. * indicates SQL NULL values.
  388. */
  389. class NumberRenderer extends DefaultTableCellRenderer
  390. {
  391. NumberRenderer()
  392. {
  393. setHorizontalAlignment(SwingConstants.RIGHT);
  394. }
  395.  
  396. public Component getTableCellRendererComponent(
  397. JTable t, Object o, boolean is, boolean has, int r, int c)
  398. {
  399. String s = (o == null) ? "<null>" : o.toString();
  400. return super.getTableCellRendererComponent(t, s, is, has, r, c);
  401. }
  402. }
  403.  
  404. /**
  405. * Displays the connect dialog box.
  406. */
  407. private void activateConnectDialog()
  408. {
  409. if(JOptionPane.showOptionDialog(
  410. mainPanel, // parentComponent
  411. connectPanel, // message
  412. "IdmsJcf Connection Information", // title
  413. JOptionPane.DEFAULT_OPTION, // optionType
  414. JOptionPane.INFORMATION_MESSAGE, // messageType
  415. null, // icon
  416. OPTIONS, // options
  417. OPTIONS[0]) == 0) // initialValue
  418. {
  419. jdbc.connect(url.getText(), uid.getText(), pwd.getText());
  420. }
  421. else if(!frame.isVisible())
  422. {
  423. System.exit(0);
  424. }
  425. }
  426. static final String[] OPTIONS = { "Connect" };
  427.  
  428. //Ken Grant 31/OCT
  429. static final String[] CATALOG_BUTTONS = { "Execute", "Cancel" };
  430.  
  431.  
  432. /**
  433. * Displays the catalog dialog box.
  434. */
  435. private void activateCatalogDialog()
  436. {
  437. JOptionPane optPane = new JOptionPane(
  438. catalogPanel, // message
  439. JOptionPane.INFORMATION_MESSAGE, // messageType
  440. JOptionPane.DEFAULT_OPTION, // optionType
  441. null, // icon
  442. CATALOG_BUTTONS, // options
  443. CATALOG_BUTTONS[0]); // initialValue
  444.  
  445.  
  446. JDialog dlg = optPane.createDialog(mainPanel, "IdmsJcf - Catalog DatabaseMetaData");
  447. invisibleField.setVisible(false);
  448. invisibleField2.setVisible(false);
  449. dlg.setVisible(true);
  450.  
  451. Object selectedValue = optPane.getValue();
  452. if( (selectedValue == null) ||(selectedValue.equals(CATALOG_BUTTONS[1])) )
  453. {
  454. return;
  455. }
  456.  
  457.  
  458. // switch (JOptionPane.showOptionDialog(
  459. // mainPanel, // parentComponent
  460. // catalogPanel, // message
  461. // "IdmsJcf - Catalog DatabaseMetaData",
  462. // JOptionPane.DEFAULT_OPTION, // optionType
  463. // JOptionPane.INFORMATION_MESSAGE, // messageType
  464. // null, // icon
  465. // CATALOG_BUTTONS, // options
  466. // CATALOG_BUTTONS[0])) // initialValue
  467. //// QUERIES, // options
  468. //// QUERIES[0])) // initialValue
  469. //
  470.  
  471. switch( queryList.getSelectedIndex() )
  472. {
  473. case 0:
  474. jdbc.getSchemas();
  475. break;
  476. case 1:
  477. jdbc.getTables(sch.getText(), tab.getText());
  478. break;
  479. case 2:
  480. jdbc.getColumns(sch.getText(), tab.getText(), col.getText());
  481. break;
  482. case 3:
  483. jdbc.getProcedures(sch.getText(), tab.getText());
  484. break;
  485. case 4:
  486. jdbc.getProcedureColumns(sch.getText(), tab.getText(), col.getText());
  487. break;
  488. //Ken Grant - add new cases for additional catalog functions
  489. case 5:
  490. jdbc.getPrimaryKeys(sch.getText(), tab.getText());
  491. break;
  492. case 6:
  493. jdbc.getExportedKeys(sch.getText(), tab.getText());
  494. break;
  495. case 7:
  496. jdbc.getImportedKeys(sch.getText(), tab.getText());
  497. break;
  498. case 8:
  499. jdbc.getCrossReference(sch.getText(), tab.getText(), fkSch.getText(), fkTab.getText());
  500. break;
  501. case 9:
  502. jdbc.getBestRowIdentifier(sch.getText(), tab.getText(), true);
  503. break;
  504. case 10:
  505. jdbc.getBestRowIdentifier(sch.getText(), tab.getText(), false);
  506. break;
  507. case 11:
  508. jdbc.getIndexInfo(sch.getText(), tab.getText(), true);
  509. break;
  510. case 12:
  511. jdbc.getIndexInfo(sch.getText(), tab.getText(), false);
  512. break;
  513. }
  514. }
  515. static final String[] QUERIES =
  516. {
  517. "Schemas",
  518. "Tables",
  519. "Columns",
  520. "Procedures",
  521. "Parameters",
  522. //Ken Grant 30/OCT/2006 - add query types for additional catalog calls
  523. "Primary Keys",
  524. "Exported Keys",
  525. "Imported Keys",
  526. "Cross Reference",
  527. "Best Row Identifier: includes nullable columns",
  528. "Best Row Identifier: excludes nullable columns",
  529. "Index Info: unique indexes for a table",
  530. "Index Info: all indexes for a table"
  531.  
  532. };
  533.  
  534. /**
  535. * Sets preferred lengths of columns.
  536. *
  537. * <P>Note: this must be called from an event handler only.
  538. */
  539. private void sizeColumns()
  540. {
  541. Font f = table.getFont();
  542. // FontMetrics m = f.getFontMetrics();
  543. String name = f.getName();
  544. int size = f.getSize();
  545. int style = f.getStyle();
  546. name = "Dialog";
  547. // name = "Monospaced";
  548. // name = "Dialog";
  549. style = f.PLAIN;
  550. table.setFont(new Font(name, style, size));
  551. // size = 8;
  552.  
  553. int count = jdbc.getColumnCount();
  554. TableColumnModel model = table.getColumnModel();
  555. for (int i = 0; i < count; i++)
  556. {
  557. int width = jdbc.getColumnWidth(i);
  558. int label = jdbc.getColumnName(i).length();
  559. if (width < label) width = label;
  560. model.getColumn(i).setPreferredWidth(width * size);
  561. }
  562. table.sizeColumnsToFit(-1); // force new widths to be respected
  563. frame.setVisible(true); // and image frame be repainted
  564. }
  565.  
  566. //
  567. // LayoutManager implementation methods
  568. //
  569.  
  570. /**
  571. * Adds the specified component with the specified name to the layout.
  572. *
  573. * @param s the component name.
  574. * @param c the component to be added.
  575. */
  576. public void addLayoutComponent(String s, Component c) {}
  577.  
  578. /**
  579. * Removes the specified component from the layout.
  580. *
  581. * @param c the component to be removed
  582. */
  583. public void removeLayoutComponent(Component c) {}
  584.  
  585. /**
  586. * Calculates the preferred size dimensions for the specified
  587. * panel given the components in the specified parent container.
  588. *
  589. * @param c the parent container to be laid out
  590. *
  591. * @see #minimumLayoutSize
  592. */
  593. public Dimension preferredLayoutSize(Container c) { return origin; }
  594.  
  595. /**
  596. * Calculates the minimum size dimensions for the specified
  597. * panel given the components in the specified parent container.
  598. *
  599. * @param c the parent container.
  600. * @see #preferredLayoutSize
  601. */
  602. public Dimension minimumLayoutSize(Container c) { return origin; }
  603.  
  604. /**
  605. * Lays out the container in the specified panel.
  606. *
  607. * @param c the parent container.
  608. */
  609. public void layoutContainer(Container c)
  610. {
  611. int top = 118;
  612. int inset = 4;
  613. Rectangle b = c.getBounds();
  614. connectButton.setBounds(b.width - 2 * inset - 120, inset, 120, 25);
  615. disconnButton.setBounds(b.width - 2 * inset - 120, 32, 120, 25);
  616. executeButton.setBounds(b.width - 2 * inset - 120, 60, 120, 25);
  617. catalogButton.setBounds(b.width - 2 * inset - 120, 88, 120, 25);
  618. queryScroller.setBounds(inset, inset, b.width - 2 * inset - 150, 108);
  619. tableScroller.setBounds(new Rectangle(
  620. inset, inset + top, b.width - 2 * inset, b.height - 2 * inset - top));
  621. }
  622. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement