Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * IdmsJcf.java
- *
- * (C) 2008 CA, Inc.
- *
- */
- package ca.idms.jcf;
- import java.sql.DriverManager;
- import java.io.PrintWriter;
- import java.net.*;
- import java.applet.Applet;
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import javax.swing.table.*;
- import javax.swing.event.*;
- import javax.swing.border.*;
- /**
- * Java version of OCF, the IDMS Online Command Facility.
- *
- * <P>This class implements a UI on top of JdbcTable, and allows SQL
- * query and update statments to be executed with the results displayed
- * using the Java extension "Swing" package from JavaSoft (Sun).
- *
- * <P>Note: This source code and the classes compiled from it are
- * provided only for demonstration purposes, and are not a supported
- * part of the CA IDMS Server 4.2 product.
- *
- * @version 2.00 11/13/02
- * @author Dave Ross
- */
- public class IdmsJcf extends JApplet
- {
- static boolean isApp;
- static String url;
- static String name = "JdbcTest";
- //
- // applet entry point
- //
- public void init()
- {
- String debug = getParameter("debug");
- // the security manager may not allow this in Java 1.3 or later
- // if so, use a caidms.properties file in the CODEBASE
- // to enable tracing for the CA IDMS JDBC Driver
- if (debug != null && debug.equals("true"))
- setLogWriter();
- isApp = false;
- try
- {
- String host = getDocumentBase().getHost();
- if (host == null || host.length() == 0)
- host = InetAddress.getLocalHost().getHostName();
- url = "jdbc:idms://" + host + ":3709" + "/";
- }
- catch (UnknownHostException e)
- {
- System.out.println("IdmsJcf.init(): " + e);
- showStatus("Unknown Host");
- }
- new JcfFrame();
- }
- //
- // application entry point
- //
- public static void main(String s[])
- {
- url = "jdbc:idms:";
- for (int i = 0; i < s.length; i++)
- {
- if (s[i].equals("-d"))
- setLogWriter();
- if (s[i].equals("-h"))
- {
- if (++i < s.length)
- url = url + "//" + s[i] + "/";
- }
- if (s[i].equals("-n"))
- {
- if (++i < s.length)
- name = s[i];
- }
- }
- isApp = true;
- new JcfFrame();
- }
- //
- // try to enable trace
- //
- static void setLogWriter()
- {
- try
- {
- System.err.println("Enabling DriverManager trace...");
- DriverManager.setLogWriter(new PrintWriter(System.out));
- }
- catch (SecurityException e)
- {
- System.err.println("Cannot enable trace: " + e);
- }
- }
- }
- class JcfFrame implements LayoutManager
- {
- Dimension origin = new Dimension(0, 0);
- JFrame frame; // application window.
- JPanel mainPanel; // content window
- JButton catalogButton;
- JButton executeButton;
- JButton connectButton;
- JButton disconnButton;
- JTextArea query;
- JComponent queryScroller;
- JdbcTable jdbc; // table model
- JTable table;
- JScrollPane tableScroller;
- JPanel connectPanel; // connect dialog box
- JTextField uid;
- JTextField pwd;
- JTextField url;
- JPanel catalogPanel; // catalog dialog box
- JTextField sch;
- JTextField tab;
- JTextField col;
- //Ken Grant 30/OCT/2006 - add fields for getCrossReference method
- JTextField fkSch;
- JTextField fkTab;
- //Ken Grant 31/OCT/2006 - add fields that we will hide
- JTextField invisibleField;
- JTextField invisibleField2;
- //Ken Grant 31/OCT/2006 - add combo box for catalog options
- JComboBox queryList;
- public JcfFrame()
- {
- // //
- // // Set the look and feel
- // //
- // String laf = UIManager.getSystemLookAndFeelClassName();
- // try
- // {
- // // UIManager.setLookAndFeel(laf);
- // UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
- // }
- // catch (UnsupportedLookAndFeelException e)
- // { System.err.println("Warning: UnsupportedLookAndFeel: " + laf); }
- // catch (Exception e)
- // { System.err.println("Error loading " + laf + ": " + e); }
- //
- // Create the components.
- //
- createConnectDialog();
- createCatalogDialog();
- createButtons();
- createQuery();
- createTable();
- //
- // Add all the components to the main panel.
- //
- mainPanel = new JPanel();
- mainPanel.add(catalogButton);
- mainPanel.add(executeButton);
- mainPanel.add(connectButton);
- mainPanel.add(disconnButton);
- mainPanel.add(queryScroller);
- mainPanel.add(tableScroller);
- mainPanel.setLayout(this); // see LayoutContainer(), below
- //
- // Create a Frame and put the main panel in it.
- //
- frame = new JFrame("IdmsJcf - CA IDMS Java Command Facility");
- frame.addWindowListener(new WindowCloser());
- frame.setBackground(Color.lightGray);
- frame.getContentPane().add(mainPanel);
- frame.pack();
- frame.setVisible(false);
- Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
- int x = (d.width - 640) / 2;
- int y = (d.height - 480) / 2;
- frame.setBounds(x, y, 640, 480);
- frame.setVisible(true);
- }
- class WindowCloser extends WindowAdapter
- {
- public void windowClosing(WindowEvent e)
- {
- if (jdbc != null) jdbc.close();
- if (IdmsJcf.isApp) System.exit(0);
- }
- }
- /**
- * Creates the Connect dialog box.
- */
- public void createConnectDialog()
- {
- JPanel labels = new JPanel(false);
- labels.setLayout(new GridLayout(0, 1));
- labels.add(new JLabel("User ID: ", JLabel.RIGHT));
- labels.add(new JLabel("Password: ", JLabel.RIGHT));
- labels.add(new JLabel("Database: ", JLabel.RIGHT));
- uid = new JTextField("jdbc");
- pwd = new JPasswordField("");
- url = new JTextField(IdmsJcf.url + IdmsJcf.name);
- JPanel fields = new JPanel(false);
- fields.setLayout(new GridLayout(0, 1));
- fields.add(uid);
- fields.add(pwd);
- fields.add(url);
- connectPanel = new JPanel(false);
- connectPanel.setLayout(new BoxLayout(connectPanel, BoxLayout.X_AXIS));
- connectPanel.add(labels);
- connectPanel.add(fields);
- }
- /**
- * Creates the Catalog dialog box.
- */
- public void createCatalogDialog()
- {
- JPanel labels = new JPanel(false);
- labels.setLayout(new GridLayout(0, 1));
- labels.add(new JLabel("Schema Pattern: ", JLabel.RIGHT));
- labels.add(new JLabel("Table Pattern: ", JLabel.RIGHT));
- labels.add(new JLabel("Column Pattern: ", JLabel.RIGHT));
- //Ken Grant 30/OCT/2006 - add labels for getCrossRefence method
- labels.add(new JLabel("Foreign Key Schema: ", JLabel.RIGHT));
- labels.add(new JLabel("Foreign Key Table: ", JLabel.RIGHT));
- labels.add(new JLabel("", JLabel.RIGHT));
- labels.add(new JLabel("Select Query: ", JLabel.RIGHT));
- sch = new JTextField("%", 18);
- tab = new JTextField("%", 18);
- col = new JTextField("%", 18);
- //Ken Grant 30/OCT/2006 - add fields for getCrossReference method
- fkSch = new JTextField("", 18);
- fkTab = new JTextField("", 18);
- //Ken Grant 31/OCT/2006 - add fields that we will hide. Makes dialog box prettier.
- invisibleField = new JTextField("", 18);
- invisibleField2 = new JTextField("", 18);
- JPanel fields = new JPanel(false);
- fields.setLayout(new GridLayout(0, 1));
- fields.add(sch);
- fields.add(tab);
- fields.add(col);
- //Ken Grant 30/OCT/2006 - add fields for getCrossReference method
- fields.add(fkSch);
- fields.add(fkTab);
- fields.add(invisibleField);
- //Ken Grant 30/OCT/2006 - add a list box and populate it
- queryList = new JComboBox(QUERIES);
- fields.add(queryList);
- labels.add(new JLabel("", JLabel.RIGHT));
- fields.add(invisibleField2);
- catalogPanel = new JPanel(false);
- catalogPanel.setLayout(new BoxLayout(catalogPanel, BoxLayout.X_AXIS));
- catalogPanel.add(labels);
- catalogPanel.add(fields);
- }
- /**
- * Creates the buttons.
- *
- * Anonymous inner classes are used to specify the event handlers.
- */
- private void createButtons()
- {
- connectButton = new JButton("Connect");
- connectButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- activateConnectDialog();
- sizeColumns(); }});
- disconnButton = new JButton("Disconnect");
- disconnButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- jdbc.close();
- sizeColumns(); }});
- executeButton = new JButton("Execute");
- executeButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- jdbc.execute(query.getText());
- sizeColumns(); }});
- catalogButton = new JButton("Catalog");
- catalogButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- activateCatalogDialog();
- sizeColumns();
- }
- });
- }
- /**
- * Creates the query text.
- */
- private void createQuery()
- {
- query = new JTextArea("select * from jdbctest");
- query.setLineWrap(true);
- queryScroller = new JScrollPane(query);
- queryScroller.setBorder(new BevelBorder(BevelBorder.LOWERED));
- }
- /**
- * Creates the table.
- */
- private void createTable()
- {
- jdbc = new JdbcTable("ca.idms.jdbc.IdmsJdbcDriver");
- table = new JTable(jdbc);
- table.setDefaultRenderer(Object.class, new StringRenderer());
- table.setDefaultRenderer(Boolean.class, new StringRenderer());
- table.setDefaultRenderer(Number.class, new NumberRenderer());
- table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
- table.setRowSelectionAllowed(false);
- table.setColumnSelectionAllowed(false);
- tableScroller = new JScrollPane(table);
- tableScroller.setBorder(new BevelBorder(BevelBorder.LOWERED));
- }
- /**
- * Replaces the default Object and Boolean default cell renderers.
- *
- * <P> Both classes are displayed as Strings. The main difference
- * from the default renderer is that SQL NULL values are displayed
- * as <null> instead of blanks.
- */
- class StringRenderer extends DefaultTableCellRenderer
- {
- public Component getTableCellRendererComponent(
- JTable t, Object o, boolean is, boolean has, int r, int c)
- {
- String s = (o == null) ? "<null>" : o.toString();
- return super.getTableCellRendererComponent(t, s, is, has, r, c);
- }
- }
- /**
- * Replaces the default Number cell renderer.
- *
- * This class handles types that the default class does
- * not seem to (at least with swing 1.1), specifically
- * floating point and SQL date types. It also explicitly
- * indicates SQL NULL values.
- */
- class NumberRenderer extends DefaultTableCellRenderer
- {
- NumberRenderer()
- {
- setHorizontalAlignment(SwingConstants.RIGHT);
- }
- public Component getTableCellRendererComponent(
- JTable t, Object o, boolean is, boolean has, int r, int c)
- {
- String s = (o == null) ? "<null>" : o.toString();
- return super.getTableCellRendererComponent(t, s, is, has, r, c);
- }
- }
- /**
- * Displays the connect dialog box.
- */
- private void activateConnectDialog()
- {
- if(JOptionPane.showOptionDialog(
- mainPanel, // parentComponent
- connectPanel, // message
- "IdmsJcf Connection Information", // title
- JOptionPane.DEFAULT_OPTION, // optionType
- JOptionPane.INFORMATION_MESSAGE, // messageType
- null, // icon
- OPTIONS, // options
- OPTIONS[0]) == 0) // initialValue
- {
- jdbc.connect(url.getText(), uid.getText(), pwd.getText());
- }
- else if(!frame.isVisible())
- {
- System.exit(0);
- }
- }
- static final String[] OPTIONS = { "Connect" };
- //Ken Grant 31/OCT
- static final String[] CATALOG_BUTTONS = { "Execute", "Cancel" };
- /**
- * Displays the catalog dialog box.
- */
- private void activateCatalogDialog()
- {
- JOptionPane optPane = new JOptionPane(
- catalogPanel, // message
- JOptionPane.INFORMATION_MESSAGE, // messageType
- JOptionPane.DEFAULT_OPTION, // optionType
- null, // icon
- CATALOG_BUTTONS, // options
- CATALOG_BUTTONS[0]); // initialValue
- JDialog dlg = optPane.createDialog(mainPanel, "IdmsJcf - Catalog DatabaseMetaData");
- invisibleField.setVisible(false);
- invisibleField2.setVisible(false);
- dlg.setVisible(true);
- Object selectedValue = optPane.getValue();
- if( (selectedValue == null) ||(selectedValue.equals(CATALOG_BUTTONS[1])) )
- {
- return;
- }
- // switch (JOptionPane.showOptionDialog(
- // mainPanel, // parentComponent
- // catalogPanel, // message
- // "IdmsJcf - Catalog DatabaseMetaData",
- // JOptionPane.DEFAULT_OPTION, // optionType
- // JOptionPane.INFORMATION_MESSAGE, // messageType
- // null, // icon
- // CATALOG_BUTTONS, // options
- // CATALOG_BUTTONS[0])) // initialValue
- //// QUERIES, // options
- //// QUERIES[0])) // initialValue
- //
- switch( queryList.getSelectedIndex() )
- {
- case 0:
- jdbc.getSchemas();
- break;
- case 1:
- jdbc.getTables(sch.getText(), tab.getText());
- break;
- case 2:
- jdbc.getColumns(sch.getText(), tab.getText(), col.getText());
- break;
- case 3:
- jdbc.getProcedures(sch.getText(), tab.getText());
- break;
- case 4:
- jdbc.getProcedureColumns(sch.getText(), tab.getText(), col.getText());
- break;
- //Ken Grant - add new cases for additional catalog functions
- case 5:
- jdbc.getPrimaryKeys(sch.getText(), tab.getText());
- break;
- case 6:
- jdbc.getExportedKeys(sch.getText(), tab.getText());
- break;
- case 7:
- jdbc.getImportedKeys(sch.getText(), tab.getText());
- break;
- case 8:
- jdbc.getCrossReference(sch.getText(), tab.getText(), fkSch.getText(), fkTab.getText());
- break;
- case 9:
- jdbc.getBestRowIdentifier(sch.getText(), tab.getText(), true);
- break;
- case 10:
- jdbc.getBestRowIdentifier(sch.getText(), tab.getText(), false);
- break;
- case 11:
- jdbc.getIndexInfo(sch.getText(), tab.getText(), true);
- break;
- case 12:
- jdbc.getIndexInfo(sch.getText(), tab.getText(), false);
- break;
- }
- }
- static final String[] QUERIES =
- {
- "Schemas",
- "Tables",
- "Columns",
- "Procedures",
- "Parameters",
- //Ken Grant 30/OCT/2006 - add query types for additional catalog calls
- "Primary Keys",
- "Exported Keys",
- "Imported Keys",
- "Cross Reference",
- "Best Row Identifier: includes nullable columns",
- "Best Row Identifier: excludes nullable columns",
- "Index Info: unique indexes for a table",
- "Index Info: all indexes for a table"
- };
- /**
- * Sets preferred lengths of columns.
- *
- * <P>Note: this must be called from an event handler only.
- */
- private void sizeColumns()
- {
- Font f = table.getFont();
- // FontMetrics m = f.getFontMetrics();
- String name = f.getName();
- int size = f.getSize();
- int style = f.getStyle();
- name = "Dialog";
- // name = "Monospaced";
- // name = "Dialog";
- style = f.PLAIN;
- table.setFont(new Font(name, style, size));
- // size = 8;
- int count = jdbc.getColumnCount();
- TableColumnModel model = table.getColumnModel();
- for (int i = 0; i < count; i++)
- {
- int width = jdbc.getColumnWidth(i);
- int label = jdbc.getColumnName(i).length();
- if (width < label) width = label;
- model.getColumn(i).setPreferredWidth(width * size);
- }
- table.sizeColumnsToFit(-1); // force new widths to be respected
- frame.setVisible(true); // and image frame be repainted
- }
- //
- // LayoutManager implementation methods
- //
- /**
- * Adds the specified component with the specified name to the layout.
- *
- * @param s the component name.
- * @param c the component to be added.
- */
- public void addLayoutComponent(String s, Component c) {}
- /**
- * Removes the specified component from the layout.
- *
- * @param c the component to be removed
- */
- public void removeLayoutComponent(Component c) {}
- /**
- * Calculates the preferred size dimensions for the specified
- * panel given the components in the specified parent container.
- *
- * @param c the parent container to be laid out
- *
- * @see #minimumLayoutSize
- */
- public Dimension preferredLayoutSize(Container c) { return origin; }
- /**
- * Calculates the minimum size dimensions for the specified
- * panel given the components in the specified parent container.
- *
- * @param c the parent container.
- * @see #preferredLayoutSize
- */
- public Dimension minimumLayoutSize(Container c) { return origin; }
- /**
- * Lays out the container in the specified panel.
- *
- * @param c the parent container.
- */
- public void layoutContainer(Container c)
- {
- int top = 118;
- int inset = 4;
- Rectangle b = c.getBounds();
- connectButton.setBounds(b.width - 2 * inset - 120, inset, 120, 25);
- disconnButton.setBounds(b.width - 2 * inset - 120, 32, 120, 25);
- executeButton.setBounds(b.width - 2 * inset - 120, 60, 120, 25);
- catalogButton.setBounds(b.width - 2 * inset - 120, 88, 120, 25);
- queryScroller.setBounds(inset, inset, b.width - 2 * inset - 150, 108);
- tableScroller.setBounds(new Rectangle(
- inset, inset + top, b.width - 2 * inset, b.height - 2 * inset - top));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement