Author: jflesch
Date: 2006-08-05 19:55:18 +0000 (Sat, 05 Aug 2006)
New Revision: 9912

Added:
   trunk/apps/Thaw/images/edit-find-replace.png
   trunk/apps/Thaw/images/edit-find.png
   trunk/apps/Thaw/images/min-edit-find-replace.png
   trunk/apps/Thaw/images/min-edit-find.png
   trunk/apps/Thaw/src/thaw/core/LibraryPlugin.java
   trunk/apps/Thaw/src/thaw/plugins/Hsqldb.java
   trunk/apps/Thaw/src/thaw/plugins/SqlConsole.java
Modified:
   trunk/apps/Thaw/src/thaw/i18n/thaw.properties
   trunk/apps/Thaw/src/thaw/plugins/Console.java
Log:
Hsqldb integration

Added: trunk/apps/Thaw/images/edit-find-replace.png
===================================================================
(Binary files differ)


Property changes on: trunk/apps/Thaw/images/edit-find-replace.png
___________________________________________________________________
Name: svn:mime-type
   + application/octet-stream

Added: trunk/apps/Thaw/images/edit-find.png
===================================================================
(Binary files differ)


Property changes on: trunk/apps/Thaw/images/edit-find.png
___________________________________________________________________
Name: svn:mime-type
   + application/octet-stream

Added: trunk/apps/Thaw/images/min-edit-find-replace.png
===================================================================
(Binary files differ)


Property changes on: trunk/apps/Thaw/images/min-edit-find-replace.png
___________________________________________________________________
Name: svn:mime-type
   + application/octet-stream

Added: trunk/apps/Thaw/images/min-edit-find.png
===================================================================
(Binary files differ)


Property changes on: trunk/apps/Thaw/images/min-edit-find.png
___________________________________________________________________
Name: svn:mime-type
   + application/octet-stream

Added: trunk/apps/Thaw/src/thaw/core/LibraryPlugin.java
===================================================================
--- trunk/apps/Thaw/src/thaw/core/LibraryPlugin.java    2006-08-05 18:56:45 UTC 
(rev 9911)
+++ trunk/apps/Thaw/src/thaw/core/LibraryPlugin.java    2006-08-05 19:55:18 UTC 
(rev 9912)
@@ -0,0 +1,34 @@
+package thaw.core;
+
+/**
+ * Plugins adding functionality for other plugins should extends this class.
+ * Then plugins using these library plugins will be able to register them one 
by one.
+ * realStart() is called when the first plugin has registered itself.
+ * realStop() is called when the last plugin has unregistered itself.
+ */
+public abstract class LibraryPlugin implements Plugin {
+       private int nmbRegistered = 0;
+
+       public abstract boolean run(Core core);
+       public abstract boolean stop();
+
+       public void registerChild(Plugin child) {
+               nmbRegistered++;
+
+               if(nmbRegistered == 1)
+                       realStart();
+       }
+
+
+       public void unregisterChild(Plugin child) {
+               nmbRegistered--;
+               
+               if(nmbRegistered == 0)
+                       realStop();
+       }
+
+       public abstract void realStart();
+
+       public abstract void realStop();
+
+}

Modified: trunk/apps/Thaw/src/thaw/i18n/thaw.properties
===================================================================
--- trunk/apps/Thaw/src/thaw/i18n/thaw.properties       2006-08-05 18:56:45 UTC 
(rev 9911)
+++ trunk/apps/Thaw/src/thaw/i18n/thaw.properties       2006-08-05 19:55:18 UTC 
(rev 9912)
@@ -170,8 +170,8 @@

 ## HsqlDb
 thaw.plugin.hsqldb.database=Database
+thaw.plugin.hsqldb.console=Sql console

-
 ## Index management
 thaw.plugin.index.editor=Your shared files


Modified: trunk/apps/Thaw/src/thaw/plugins/Console.java
===================================================================
--- trunk/apps/Thaw/src/thaw/plugins/Console.java       2006-08-05 18:56:45 UTC 
(rev 9911)
+++ trunk/apps/Thaw/src/thaw/plugins/Console.java       2006-08-05 19:55:18 UTC 
(rev 9912)
@@ -35,6 +35,10 @@

        private long maxLogSize = 5120;

+       public Console() {
+
+       }
+
        public boolean run(Core core) {
                this.core = core;


Added: trunk/apps/Thaw/src/thaw/plugins/Hsqldb.java
===================================================================
--- trunk/apps/Thaw/src/thaw/plugins/Hsqldb.java        2006-08-05 18:56:45 UTC 
(rev 9911)
+++ trunk/apps/Thaw/src/thaw/plugins/Hsqldb.java        2006-08-05 19:55:18 UTC 
(rev 9912)
@@ -0,0 +1,125 @@
+package thaw.plugins;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+
+import java.sql.ResultSet;
+import java.sql.Statement;
+
+import thaw.core.*;
+import thaw.fcp.*;
+
+public class Hsqldb extends LibraryPlugin {
+       private Core core;
+
+       private Connection connection;
+
+       public Hsqldb() {
+
+       }
+
+       public boolean run(Core core) {
+               this.core = core;
+
+               try {
+                       Class.forName("org.hsqldb.jdbcDriver");
+               } catch (Exception e) {
+                       Logger.error(this, "ERROR: failed to load HSQLDB JDBC 
driver.");
+                       e.printStackTrace();
+                       return false;
+               }
+
+               return true;
+       }
+
+       public void realStart() {
+               Logger.info(this, "Connecting to the database ...");
+
+               if(core.getConfig().getValue("hsqldb.url") == null)
+                       core.getConfig().setValue("hsqldb.url", 
"jdbc:hsqldb:file:thaw.db");
+
+               try {
+                       connect();
+               } catch (java.sql.SQLException e) {
+                       Logger.error(this, "SQLException while connecting to 
the database '"+core.getConfig().getValue("hsqldb.url")+"'");
+                       e.printStackTrace();
+               }
+       }
+
+
+       public void connect() throws java.sql.SQLException {
+               if(core.getConfig().getValue("hsqldb.url") == null)
+                       core.getConfig().setValue("hsqldb.url", 
"jdbc:hsqldb:file:thaw.db");
+
+               if(connection != null)
+                       disconnect();
+
+               connection = 
DriverManager.getConnection(core.getConfig().getValue("hsqldb.url"),
+                                                        "sa", "");
+       }
+
+       public void disconnect() throws java.sql.SQLException {
+               connection.close();
+       }
+
+
+       public boolean stop() {
+
+               return true;
+       }
+
+       public void realStop() {
+               Logger.info(this, "Disconnecting from the database ...");
+               
+               try {
+                       connection.commit();
+                       connection.close();
+               } catch(java.sql.SQLException e) {
+                       Logger.error(this, "SQLException while closing 
connection !");
+                       e.printStackTrace();
+               }
+       }
+
+       public String getNameForUser() {
+               return I18n.getMessage("thaw.plugin.hsqldb.database");
+       }
+
+
+
+       public Connection getConnection() {
+               return connection;
+       }
+
+
+       public ResultSet executeQuery(String query) throws 
java.sql.SQLException {
+
+               ResultSet results;
+               
+               Statement stmt = connection.createStatement();
+                       
+               results = stmt.executeQuery(query);
+
+               return results;
+       }
+
+
+       public boolean execute(String query) {
+               boolean result;
+               
+               try {
+                       Statement stmt = connection.createStatement();
+                       
+                       result = stmt.execute(query);
+                       
+               }
+               
+               catch(Exception e){
+                       Logger.warning(this, "Exception '"+e.toString()+"' 
while running the following query:");
+                       Logger.warning(this, query);
+
+                       return false;
+               }
+
+               return result;
+       }
+}

Added: trunk/apps/Thaw/src/thaw/plugins/SqlConsole.java
===================================================================
--- trunk/apps/Thaw/src/thaw/plugins/SqlConsole.java    2006-08-05 18:56:45 UTC 
(rev 9911)
+++ trunk/apps/Thaw/src/thaw/plugins/SqlConsole.java    2006-08-05 19:55:18 UTC 
(rev 9912)
@@ -0,0 +1,224 @@
+package thaw.plugins;
+
+import javax.swing.JPanel;
+import javax.swing.JTextArea;
+import javax.swing.JTextField;
+import javax.swing.JScrollPane;
+import javax.swing.JButton;
+
+import java.awt.BorderLayout;
+
+import java.sql.ResultSet;
+import java.sql.ResultSetMetaData;
+
+import java.awt.Font;
+
+import thaw.core.*;
+import thaw.fcp.*;
+
+import thaw.plugins.index.*;
+
+public class SqlConsole implements Plugin, java.awt.event.ActionListener {
+       public final static int BUFFER_SIZE = 51200;
+
+       private Core core;
+       private Hsqldb hsqldb;
+
+       private IndexEditorPanel editorPanel;
+       private JPanel panel;
+
+       private JTextArea sqlArea;
+       private JTextField commandField;
+       private JButton sendButton;
+
+       public SqlConsole() {
+
+       }
+
+       public boolean run(Core core) {
+               this.core = core;
+
+               if(core.getPluginManager().getPlugin("thaw.plugins.Hsqldb") == 
null) {
+                       Logger.info(this, "Loading Hsqldb plugin");
+
+                       
if(!core.getPluginManager().loadPlugin("thaw.plugins.Hsqldb")
+                          || 
!core.getPluginManager().runPlugin("thaw.plugins.Hsqldb")) {
+                               Logger.error(this, "Unable to load 
thaw.plugins.Hsqldb !");
+                               return false;
+                       }
+               }
+
+               hsqldb = 
(Hsqldb)core.getPluginManager().getPlugin("thaw.plugins.Hsqldb");
+
+               hsqldb.registerChild(this);
+
+               panel = getPanel();
+
+               
core.getMainWindow().addTab(I18n.getMessage("thaw.plugin.hsqldb.console"),
+                                           panel);
+                                           
+
+               return true;
+       }
+
+
+       public boolean stop() {
+               core.getMainWindow().removeTab(panel);
+
+               hsqldb.unregisterChild(this);
+
+               return true;
+       }
+
+       public String getNameForUser() {
+               return I18n.getMessage("thaw.plugin.hsqldb.console");
+       }
+
+       protected JPanel getPanel() {
+               JPanel panel;
+               JPanel subPanel;
+
+               panel = new JPanel();
+               panel.setLayout(new BorderLayout());
+
+               subPanel = new JPanel();
+               subPanel.setLayout(new BorderLayout());
+
+               sqlArea = new JTextArea("");
+               sqlArea.setEditable(false);
+               sqlArea.setFont(new Font("Monospaced", Font.PLAIN, 12));
+
+               commandField = new JTextField("");
+               commandField.addActionListener(this);
+
+               sendButton = new JButton(" Ok ");
+               sendButton.addActionListener(this);
+               
+               subPanel.add(commandField, BorderLayout.CENTER);
+               subPanel.add(sendButton, BorderLayout.EAST);
+
+               panel.add(new JScrollPane(sqlArea), BorderLayout.CENTER);
+               panel.add(subPanel, BorderLayout.SOUTH);
+
+               return panel;
+       }
+
+       public void addToConsole(String txt) {
+               String text = sqlArea.getText() + txt;
+
+               if(text.length() > BUFFER_SIZE) {
+                       text = text.substring((int)(text.length() - 
BUFFER_SIZE));
+               }
+
+               sqlArea.setText(text);
+       }
+
+       public void actionPerformed(java.awt.event.ActionEvent e) {
+
+               sendCommand(commandField.getText());
+
+               commandField.setText("");
+
+       }
+
+       protected void display(String txt, int lng) {
+               if(txt == null)
+                       txt = "(null)";
+
+               int txtLength = txt.length();
+
+               String fTxt = txt;
+
+               if(lng > 30)
+                       lng = 30;
+
+               for(int i = 0 ; i + txtLength < lng; i++) {
+                       fTxt = fTxt + " ";
+               }
+
+               addToConsole(fTxt);
+       }
+
+       public void sendCommand(String cmd) {
+
+               /* A simple reminder :) */
+               if(cmd.toLowerCase().equals("list_tables"))
+                       cmd = "SELECT * FROM INFORMATION_SCHEMA.SYSTEM_TABLES";
+
+               addToConsole("\n> "+cmd+"\n\n");
+
+               try {
+
+                       if(cmd.toLowerCase().equals("reconnect")) {
+                               hsqldb.connect();
+                               addToConsole("Ok\n");
+                               return;
+                       }
+
+                       ResultSet result = hsqldb.executeQuery(cmd);
+                       
+                       if(result == null) {
+                               addToConsole("(null)\n");
+                               return;
+                       }
+                       
+                       java.sql.SQLWarning warning = result.getWarnings();
+
+                       while(warning != null) {
+                               addToConsole("Warning: "+warning.toString());
+                               warning = warning.getNextWarning();
+                       }
+
+
+
+                       ResultSetMetaData metadatas = result.getMetaData();
+                       
+                       int nmbCol = metadatas.getColumnCount();
+                       
+                       addToConsole("      ");
+                       
+                       for(int i = 1; i <= nmbCol ; i++) {
+                               display(metadatas.getColumnLabel(i), 
metadatas.getColumnDisplaySize(i));
+                               addToConsole("  ");
+                       }
+                       addToConsole("\n");
+
+                       addToConsole("      ");
+                       for(int i = 1; i <= nmbCol ; i++) {
+                               display(metadatas.getColumnTypeName(i), 
metadatas.getColumnDisplaySize(i));
+                               addToConsole("  ");
+                       }
+                       addToConsole("\n");
+
+                       addToConsole("      ");
+                       for(int i = 1; i <= nmbCol ; i++) {
+                               display("----", 
metadatas.getColumnDisplaySize(i));
+                               addToConsole("  ");
+                       }
+                       addToConsole("\n");
+
+                       boolean ret = true;
+
+                       while(ret) {
+                               ret = result.next();
+
+                               if(!ret)
+                                       break;
+
+                               display(Integer.toString(result.getRow()), 4);
+                               addToConsole("  ");
+
+                               for(int i =1; i <= nmbCol ; i++) {
+                                       display(result.getString(i), 
metadatas.getColumnDisplaySize(i));
+                                       addToConsole("  ");
+                               }
+                               addToConsole("\n");
+                       }
+
+               } catch(java.sql.SQLException e) {
+                       addToConsole("SQLException : "+e.toString()+" : 
"+e.getCause()+"\n");
+               }
+               
+       }
+
+}


Reply via email to