home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 July / Chip_1998-07_cd.bin / zkuste / JBuilder / BDK / Win / bdk_sep97.exe / _SETUP.1 / SelectCustomizer.java < prev    next >
Encoding:
Java Source  |  1997-09-10  |  13.7 KB  |  550 lines

  1. package sunw.demo.select;
  2.  
  3. /**
  4.  * This class configures the DBViewer.
  5.  *
  6.  * @author kgh
  7.  */
  8.  
  9. import java.sql.*;
  10. import java.awt.*;
  11. import java.awt.event.*;
  12. import java.util.*;
  13. import java.beans.*;
  14.  
  15. public class SelectCustomizer extends Panel implements java.beans.Customizer {
  16.  
  17.     public SelectCustomizer() {
  18.     setLayout(null);
  19.     Thread th = new WorkerThread();
  20.     th.start();
  21.     }
  22.  
  23.     public Dimension getPreferredSize() {
  24.     return new Dimension(400, 500);
  25.     }
  26.  
  27.     public Dimension getMinimumSize() {
  28.     return getPreferredSize();
  29.     }
  30.  
  31.     public void setObject(Object objectToCustomize) {
  32.     target = (Select) objectToCustomize;
  33.  
  34.     url = target.getURL();
  35.     user = target.getUser();
  36.     password = target.getPassword();
  37.  
  38.     createStartupScreen();
  39.     setVisible(true);
  40.     }
  41.  
  42.     // Nested class for worker thread just calls our doWork method.
  43.     class WorkerThread extends Thread {
  44.         public void run() {
  45.         doWork();
  46.         }
  47.     }
  48.  
  49.     // This is called from the worker thread.  This is where we actually
  50.     // do any real work, like talking to the database.
  51.     // Note that we have to be careful about concurrency control here so
  52.     // thav we don't wedge the GUI while we're off chatting to the database.
  53.  
  54.     void doWork() {
  55.     for (;;) {
  56.         int work = getWork();
  57.  
  58.         // Open a database connection.
  59.         con = Util.connect(url, user, password, status);
  60.         if (con == null) {
  61.         continue;
  62.         }
  63.  
  64.         if (work == DO_USER_TABLES) {
  65.             getTables(false);
  66.         } else if (work == DO_ALL_TABLES) {
  67.             getTables(true);
  68.         } else if (work == DO_VIEW) {
  69.         getInfo(tableName);
  70.         }
  71.  
  72.         // Close the database connection.
  73.         Util.disconnect(con);
  74.         }
  75.     }
  76.  
  77.     private synchronized int getWork() {
  78.     work = NO_WORK;
  79.     while (work == NO_WORK) {
  80.         try {
  81.         wait();
  82.         } catch (Exception ex) {
  83.         }
  84.     }
  85.     int result = work;
  86.     return (result);
  87.     }
  88.  
  89.     synchronized void setWork(int newWork) {
  90.     work = newWork;
  91.     notify();
  92.     }
  93.  
  94.     private void getTables(boolean showSystemTables) {
  95.     try {
  96.         setStatus("Opened connection OK, searching for tables...");
  97.  
  98.         DatabaseMetaData md = con.getMetaData();
  99.         String types[];
  100.         if (showSystemTables) {
  101.               types = new String[2];
  102.             types[0] = "TABLE";
  103.             types[1] = "SYSTEM TABLE";
  104.         } else {
  105.               types = new String[2];
  106.             types[0] = "TABLE";
  107.         }
  108.         ResultSet rs = md.getTables(null, "%", "%", types);
  109.  
  110.         tableNames = new Vector();
  111.         int count = 0;
  112.         while (rs.next()) {
  113.             String name = rs.getString(2) + "." + rs.getString(3);
  114.         tableNames.addElement(name);
  115.         count++;
  116.         }
  117.         rs.close();
  118.  
  119.         setStatus("Got " + count + " tables");
  120.  
  121.         createTableScreen();
  122.  
  123.     } catch (SQLException sx)  {
  124.         setStatus(sx);
  125.         System.err.println("Caught SQLException : " + sx);
  126.         sx.printStackTrace();
  127.     } catch (Exception ex) {
  128.         setStatus("Couldn't load JDBC-ODBC bridge driver");
  129.         System.err.println("Caught Exception : " + ex);
  130.         ex.printStackTrace();
  131.     }
  132.     }
  133.  
  134.     public void getInfo(String table) {
  135.     // Dsiplay information on the contents of a table:
  136.     createInfoScreen(table);
  137.     columnNames = new Vector();
  138.     columnTypes = new Vector();
  139.     try {
  140.         int index = table.indexOf('.');
  141.         String schema = table.substring(0, index);
  142.         String name = table.substring(index+1, table.length());
  143.  
  144.         DatabaseMetaData md = con.getMetaData();
  145.         ResultSet rs = md.getColumns(null, schema, name, "%");
  146.         while (rs.next()) {
  147.         String columnName = rs.getString(4);
  148.         columnNames.addElement(columnName);
  149.         int type = rs.getShort(5);
  150.         int width = rs.getShort(7);
  151.         int scale = rs.getShort(9);
  152.         String typeName;
  153.         switch (type) {
  154.            case Types.BIT:
  155.             typeName = "BIT";
  156.             break;
  157.            case Types.TINYINT:
  158.             typeName = "TINYINT";
  159.             break;
  160.            case Types.SMALLINT:
  161.             typeName = "SMALLINT";
  162.             break;
  163.            case Types.INTEGER:
  164.             typeName = "INTEGER";
  165.             break;
  166.            case Types.BIGINT:
  167.             typeName = "BIGINT";
  168.             break;
  169.            case Types.FLOAT:
  170.             typeName = "FLOAT";
  171.             break;
  172.            case Types.DOUBLE:
  173.             typeName = "DOUBLE";
  174.             break;
  175.            case Types.REAL:
  176.             typeName = "REAL";
  177.             break;
  178.            case Types.NUMERIC:
  179.             typeName = "NUMERIC(" + width + "," + scale + ")";
  180.             break;
  181.            case Types.DECIMAL:
  182.             typeName = "DECIMAL(" + width + "," + scale + ")";
  183.             break;
  184.            case Types.CHAR:
  185.             typeName = "CHAR(" + width + ")";
  186.             break;
  187.            case Types.VARCHAR:
  188.             typeName = "CHAR(" + width + ")";
  189.             break;
  190.            case Types.LONGVARCHAR:
  191.             typeName = "LONGVARCHAR";
  192.             break;
  193.            case Types.BINARY:
  194.             typeName = "BINARY(" + width + ")";
  195.             break;
  196.            case Types.VARBINARY:
  197.             typeName = "VARBINARY(" + width + ")";
  198.             break;
  199.            case Types.LONGVARBINARY:
  200.             typeName = "LONGVARBINARY";
  201.             break;
  202.            case Types.DATE:
  203.             typeName = "DATE";
  204.             break;
  205.            case Types.TIME:
  206.             typeName = "TIME";
  207.             break;
  208.            case Types.TIMESTAMP:
  209.             typeName = "TIMESTAMP";
  210.             break;
  211.            case Types.OTHER:
  212.             typeName = "OTHER";
  213.             break;
  214.           default:
  215.             typeName = "???";
  216.             break;
  217.         }
  218.         columnTypes.addElement(typeName);
  219.         }
  220.         rs.close();
  221.         finishInfoScreen();
  222.  
  223.     } catch (SQLException sx)  {
  224.         setStatus(sx);
  225.     }
  226.     }
  227.  
  228.     synchronized void resetScreen() {
  229.     // Do the basic screen setup that's common to all our environments.
  230.     removeAll();
  231.  
  232.     int width = getSize().width;
  233.     int height = getSize().height;
  234.  
  235.     buttonOffset = 10;
  236.  
  237.     status = new Label("", Label.LEFT);
  238.     add(status);
  239.     status.setBounds(15, height-30, 2*width, 25);
  240.     }
  241.  
  242.     synchronized void addButton(Button b) {
  243.     int height = getSize().height;
  244.     add(b);
  245.     b.setBounds(buttonOffset, height-65, 80, 30);
  246.     buttonOffset += 80;
  247.     }
  248.  
  249.     synchronized void createStartupScreen() {
  250.  
  251.     // Set up the initial screen where we prompt the user for a
  252.     // database URL, account, and password.
  253.  
  254.     resetScreen();
  255.     int width = getSize().width;
  256.  
  257.     Label l1 = new Label("Welcome to the Select Customizer.", Label.CENTER);
  258.     add(l1);
  259.     l1.setFont(new Font("Dialog", Font.PLAIN, 16));
  260.     l1.setBounds(10,100,width-20,50);
  261.  
  262.     Label t1 = new Label("JDBC URL:", Label.RIGHT);
  263.     add(t1);
  264.     t1.setBounds(20, 250, 70, 30);
  265.     urlField = new TextField(url, 40);
  266.     urlField.addKeyListener(new UrlListener());
  267.     add(urlField);
  268.     urlField.setBounds(100, 250, 280, 30);
  269.  
  270.     Label t2 = new Label("User:", Label.RIGHT);
  271.     add(t2);
  272.     t2.setBounds(20, 300, 70, 30);
  273.     userField = new TextField(user, 40);
  274.     userField.addKeyListener(new UserListener());
  275.     add(userField);
  276.     userField.setBounds(100, 300, 280, 30);
  277.  
  278.     Label t3 = new Label("Password:", Label.RIGHT);
  279.     add(t3);
  280.     t3.setBounds(20, 350, 70, 30);
  281.     passwordField = new TextField(password, 40);
  282.     passwordField.setEchoChar('*');
  283.     passwordField.addKeyListener(new PasswordListener());
  284.     add(passwordField);
  285.     passwordField.setBounds(100, 350, 280, 30);
  286.  
  287.     setStatus("Enter login info and push \"user tables\" or \"all tables\"");
  288.  
  289.     Button user = new Button("User Tables");
  290.     user.addActionListener(new UserTablesListener());
  291.     addButton(user);
  292.  
  293.     Button all = new Button("All Tables");
  294.     all.addActionListener(new AllTablesListener());
  295.     addButton(all);
  296.  
  297.     }
  298.  
  299.     synchronized void createTableScreen() {
  300.  
  301.     // Set up the screen where we report on all known tables.
  302.     resetScreen();
  303.     int width = getSize().width;
  304.     int height = getSize().height;
  305.  
  306.     String summary = "Database \"" + url + "\" has " + tableNames.size() + " tables.";
  307.     Label l1 = new Label(summary, Label.LEFT);
  308.     add(l1);
  309.     l1.setBounds(30,10,width-20,25);
  310.  
  311.     Button back = new Button("Back");
  312.     back.addActionListener(new BackToStartListener());
  313.     addButton(back);
  314.  
  315.     tableList = new java.awt.List();
  316.     tableList.addItemListener(new TableListener());
  317.     add(tableList);
  318.     for (int i = 0; i < tableNames.size(); i++) {
  319.         String name = (String)tableNames.elementAt(i);
  320.         tableList.addItem(name);
  321.     }
  322.     tableList.setBounds(30, 40, 240, height-110);
  323.  
  324.     setStatus("Please chose a table");
  325.  
  326.     }
  327.  
  328.     synchronized void createInfoScreen(String table) {
  329.  
  330.     // Set up the screen where we describe a table.
  331.     resetScreen();
  332.     int width = getSize().width;
  333.     int height = getSize().height;
  334.     
  335.     Label l1 = new Label("Database:  " + url, Label.LEFT);
  336.     add(l1);
  337.     l1.setBounds(30,35,width-40,20);
  338.  
  339.     Label l2 = new Label("Table:    " + table, Label.LEFT);
  340.     add(l2);
  341.     l2.setBounds(30,55,width-20,20);
  342.  
  343.     Button back = new Button("Back");
  344.     back.addActionListener(new BackToTableListener());
  345.     addButton(back);
  346.  
  347.     setStatus("Loading table info...");
  348.     }
  349.  
  350.     synchronized void finishInfoScreen() {
  351.     int width = getSize().width;
  352.     int height = getSize().height;
  353.  
  354.     Label l3 = new Label("Number of columns:  " + columnNames.size(), Label.LEFT);
  355.     add(l3);
  356.     l3.setBounds(30,75,width-20,20);
  357.  
  358.     columnList = new java.awt.List();
  359.     columnList.setFont(new Font("Courier", Font.PLAIN, 12));
  360.     columnList.setMultipleMode(true);
  361.     columnList.addItemListener(new ColumnListener());
  362.     add(columnList);
  363.     for (int i = 0; i < columnNames.size(); i++) {
  364.         String name = (String)columnNames.elementAt(i);
  365.         String type = (String)columnTypes.elementAt(i);
  366.         int pad = 22 - name.length();
  367.         String entry = name;
  368.         if (pad > 0) {
  369.         entry += spaces.substring(0,pad);
  370.         }
  371.         entry += type;
  372.         columnList.addItem(entry);
  373.     }
  374.     columnList.setBounds(30, 120, 260, height-200);
  375.     setStatus("Choose the columns you want in the SQL SELECT.");
  376.     }
  377.     
  378.     synchronized void setStatus(String mess) {
  379.     status.setText(mess);
  380.     }
  381.  
  382.     synchronized void setStatus(SQLException sx) {
  383.     System.err.println("Caught " + sx);
  384.     setStatus("" + sx);
  385.     }
  386.  
  387.     void updateSelect() {
  388.     int len = columnList.getItemCount();
  389.     String columns = null;
  390.     for (int i = 0; i < len; i++) {
  391.         if (columnList.isIndexSelected(i)) {
  392.             String name = (String)columnNames.elementAt(i);
  393.         if (columns == null) {
  394.             columns = name;
  395.         } else {
  396.             columns = columns + ", " + name;
  397.              }
  398.         }
  399.     }
  400.     if (columns == null) {
  401.         selectString = "";
  402.     } else {
  403.         selectString = "SELECT " + columns + " FROM " + tableName;
  404.     }
  405.     System.err.println(selectString);
  406.     target.setSQL(selectString);
  407.     support.firePropertyChange("SQL", null, selectString);
  408.     target.update();
  409.     }
  410.  
  411.  
  412.     static void println(String mess) {
  413.     System.out.println(mess);
  414.     }
  415.  
  416.     static void print(String mess) {
  417.     System.out.print(mess);
  418.     }
  419.  
  420.     //----------------------------------------------------------------------
  421.  
  422.     // Nested classes for various GUI event listeners.
  423.     //
  424.     // Now that in JDK 1.1 these nested classes can only access public and
  425.     // package-private methods/fields of the containing class adn can't
  426.     // access private fields.
  427.     //
  428.     // So we've been careful to make sure that the fields they need are
  429.     // at least package-private.
  430.  
  431.     class UrlListener extends KeyAdapter {
  432.     public void keyReleased(KeyEvent evt) {
  433.         // Someone edited the "URL" string.
  434.         url = urlField.getText();
  435.         target.setURL(url);
  436.         support.firePropertyChange("URL", null, url);
  437.     }
  438.     }
  439.  
  440.     class UserListener extends KeyAdapter {
  441.     public void keyReleased(KeyEvent evt) {
  442.         // Someone edited the "user" string.
  443.             user = userField.getText();
  444.         target.setUser(user);
  445.         support.firePropertyChange("user", null, user);
  446.     }
  447.     }
  448.  
  449.     class PasswordListener extends KeyAdapter {
  450.     public void keyReleased(KeyEvent evt) {
  451.         // Someone edited the "password" string.
  452.             password = passwordField.getText();
  453.         target.setPassword(password);
  454.         support.firePropertyChange("password", null, password);
  455.     }
  456.     }
  457.  
  458.     class ColumnListener implements ItemListener {
  459.     public void itemStateChanged(ItemEvent evt) {
  460.          // Someone selected a column in our column view.
  461.         updateSelect();
  462.     }
  463.     }
  464.  
  465.     class TableListener implements ItemListener {
  466.     public void itemStateChanged(ItemEvent evt) {
  467.          // Someone selected a table in our table view.
  468.         tableName = tableList.getSelectedItem();
  469.         setWork(DO_VIEW);
  470.     }
  471.     }
  472.  
  473.     class UserTablesListener implements ActionListener {
  474.     public void actionPerformed(ActionEvent evt) {
  475.         setWork(DO_USER_TABLES);
  476.         }
  477.     }
  478.  
  479.     class AllTablesListener implements ActionListener {
  480.     public void actionPerformed(ActionEvent evt) {
  481.         setWork(DO_ALL_TABLES);
  482.         }
  483.     }
  484.  
  485.     class BackToStartListener implements ActionListener {
  486.     public void actionPerformed(ActionEvent evt) {
  487.         createStartupScreen();
  488.         }
  489.     }
  490.  
  491.     class BackToTableListener implements ActionListener {
  492.     public void actionPerformed(ActionEvent evt) {
  493.         createTableScreen();
  494.         }
  495.     }
  496.  
  497.     //----------------------------------------------------------------------
  498.  
  499.     public void addPropertyChangeListener(PropertyChangeListener l) {
  500.     support.addPropertyChangeListener(l);
  501.     }
  502.  
  503.     public void removePropertyChangeListener(PropertyChangeListener l) {
  504.     support.removePropertyChangeListener(l);
  505.     }
  506.  
  507.     PropertyChangeSupport support = new PropertyChangeSupport(this);
  508.  
  509.     //------------------------------------------------------------
  510.  
  511.     // GUI state:
  512.     TextField urlField;
  513.     TextField userField;
  514.     TextField passwordField;
  515.     Label status = new Label("");
  516.     int buttonOffset;
  517.  
  518.     // Keep track of pending work for the worker thread:
  519.     final static int NO_WORK = 0;
  520.     final static int DO_USER_TABLES = 1;
  521.     final static int DO_ALL_TABLES = 2;
  522.     final static int DO_VIEW = 3;
  523.     int work = NO_WORK;
  524.  
  525.     String spaces = "                                       " +
  526.         "                                           " +
  527.         "                                           ";
  528.  
  529.     //------------------------------------------------------------
  530.     // Database related state:
  531.     Connection con;
  532.     String url;
  533.     String user;
  534.     String password;
  535.     Vector tableNames;
  536.     java.awt.List tableList;
  537.     java.awt.List columnList;
  538.     String tableName;
  539.     String selectString;
  540.     Vector columnNames;
  541.     Vector columnTypes;
  542.     //------------------------------------------------------------
  543.  
  544.     // Object that we are customizing.
  545.     Select target;
  546. }
  547.  
  548.  
  549.  
  550.