Author: jflesch
Date: 2007-01-01 20:01:13 +0000 (Mon, 01 Jan 2007)
New Revision: 11540

Added:
   trunk/apps/Thaw/src/thaw/plugins/index/AutoRefresh.java
   trunk/apps/Thaw/src/thaw/plugins/index/IndexConfigPanel.java
Log:
Forgot files

Added: trunk/apps/Thaw/src/thaw/plugins/index/AutoRefresh.java
===================================================================
--- trunk/apps/Thaw/src/thaw/plugins/index/AutoRefresh.java                     
        (rev 0)
+++ trunk/apps/Thaw/src/thaw/plugins/index/AutoRefresh.java     2007-01-01 
20:01:13 UTC (rev 11540)
@@ -0,0 +1,168 @@
+package thaw.plugins.index;
+
+import java.sql.*;
+
+import thaw.core.Config;
+import thaw.core.Logger;
+import thaw.plugins.Hsqldb;
+
+import thaw.fcp.FCPQueueManager;
+
+public class AutoRefresh implements Runnable {
+
+       public final static boolean DEFAULT_ACTIVATED = true;
+       public final static int DEFAULT_INTERVAL = 300;
+       public final static int DEFAULT_INDEX_NUMBER = 20;
+
+       private Hsqldb db;
+       private IndexBrowserPanel browserPanel;
+       private Config config;
+
+       private boolean threadRunning;
+
+       private int interval;
+       private int nmbIndexesPerInterval;
+
+       private int subInterval;
+       private int nmbIndexesPerSubInterval;
+
+       private FCPQueueManager queueManager;
+
+       public AutoRefresh(Hsqldb db, IndexBrowserPanel indexBrowser, 
FCPQueueManager queueManager, Config config) {
+               this.browserPanel = indexBrowser;
+               this.queueManager = queueManager;
+               this.config = config;
+               threadRunning = false;
+
+               interval = 0;
+               nmbIndexesPerInterval = 0;
+
+               try {
+                       if (config.getValue("indexRefreshInterval") != null) {
+                               interval = 
Integer.parseInt(config.getValue("indexRefreshInterval"));
+                       }
+
+                       if (config.getValue("nmbIndexesPerRefreshInterval") != 
null) {
+                               nmbIndexesPerInterval = 
Integer.parseInt(config.getValue("nmbIndexesPerRefreshInterval"));
+                       }
+               } catch(NumberFormatException e) {
+                       Logger.error(this, "Error while parsing value in the 
configuration, using default ones");
+                       interval = 0;
+                       nmbIndexesPerInterval = 0;
+               }
+
+               if (interval == 0)
+                       interval = DEFAULT_INTERVAL;
+               if (nmbIndexesPerInterval == 0)
+                       nmbIndexesPerInterval = DEFAULT_INDEX_NUMBER;
+
+
+               this.db = db;
+
+
+               if (interval >= nmbIndexesPerInterval) {
+                       nmbIndexesPerSubInterval = 1;
+                       subInterval = (interval / nmbIndexesPerInterval);
+               } else {
+                       subInterval = 1;
+                       nmbIndexesPerSubInterval = (nmbIndexesPerInterval / 
interval);
+               }
+
+
+       }
+
+       public void start() {
+               if (!threadRunning) {
+                       threadRunning = true;
+                       Thread th = new Thread(this);
+                       th.start();
+               }
+       }
+
+
+       public int updateNext(int lastIdx) {
+               try {
+                       Connection c = db.getConnection();
+                       PreparedStatement st;
+                       ResultSet results;
+                       int ret;
+
+                       st = c.prepareStatement("SELECT id, originalName, 
displayName, publicKey, privateKey, author, positionInTree, revision "+
+                                               "FROM indexes WHERE id > ? 
ORDER by id LIMIT 1");
+
+                       if (lastIdx != -1) {
+                               st.setInt(1, lastIdx);
+                               if (st.execute())
+                                       results = st.getResultSet();
+                               else
+                                       return -1;
+                       } else {
+                               results = db.executeQuery("SELECT id, 
originalName, displayName, publicKey, privateKey, author, positionInTree, 
revision "+
+                                                         "FROM indexes ORDER 
by Id LIMIT 1");
+
+                               if (results == null)
+                                       return -1;
+                       }
+
+                       if (!results.next())
+                               return -1;
+
+                       ret = results.getInt("id");
+
+                       Index index;
+
+                       index = 
browserPanel.getIndexTree().getRegisteredIndex(results.getString("publicKey"));
+
+                       if (index == null)
+                               index = new Index(queueManager, browserPanel,
+                                                 results.getInt("id"), null,
+                                                 
results.getString("originalName"),
+                                                 
results.getString("displayName"),
+                                                 
results.getString("publicKey"),
+                                                 
results.getString("privateKey"),
+                                                 results.getInt("revision"),
+                                                 results.getString("author"));
+
+                       if (index.getPrivateKey() != null) {
+                               Logger.debug(this, "Private key found ! index 
ignored");
+                               return ret;
+                       }
+
+                       index.updateFromFreenet(-1);
+
+                       return ret;
+
+               } catch(java.sql.SQLException e) {
+                       Logger.error(this, "SQLEXCEPTION while autorefreshing: 
"+e.toString());
+                       return -2;
+               }
+       }
+
+
+       public void run() {
+               int lastIdx = -1;
+
+               while(threadRunning) {
+                       try {
+                               Thread.sleep(1000 * subInterval);
+                       } catch(java.lang.InterruptedException e) {
+                               /* \_o< */
+                       }
+
+                       for (int i = 0 ; i < nmbIndexesPerSubInterval ; i++) {
+                               lastIdx = updateNext(lastIdx);
+
+                               if (lastIdx == -2) {
+                                       Logger.error(this, "Disabling 
auto-refreshing !");
+                                       return;
+                               }
+                       }
+               }
+       }
+
+       public void stop() {
+               if (threadRunning)
+                       threadRunning = false;
+       }
+
+}

Added: trunk/apps/Thaw/src/thaw/plugins/index/IndexConfigPanel.java
===================================================================
--- trunk/apps/Thaw/src/thaw/plugins/index/IndexConfigPanel.java                
                (rev 0)
+++ trunk/apps/Thaw/src/thaw/plugins/index/IndexConfigPanel.java        
2007-01-01 20:01:13 UTC (rev 11540)
@@ -0,0 +1,123 @@
+package thaw.plugins.index;
+
+import javax.swing.JPanel;
+import java.awt.BorderLayout;
+import java.awt.GridLayout;
+import javax.swing.JLabel;
+import javax.swing.JTextField;
+import javax.swing.JCheckBox;
+import java.awt.event.ActionListener;
+import java.awt.event.ActionEvent;
+
+import thaw.core.*;
+
+public class IndexConfigPanel implements ActionListener {
+       private ConfigWindow configWindow;
+       private Config config;
+
+       private JPanel panel;
+
+       private JCheckBox autorefreshActivated;
+       private JTextField refreshInterval;
+       private JTextField indexPerRefresh;
+
+
+       public IndexConfigPanel(ConfigWindow configWindow, Config config) {
+               this.configWindow = configWindow;
+               this.config = config;
+
+               panel = new JPanel();
+               panel.setLayout(new GridLayout(15, 1));
+
+               autorefreshActivated = new 
JCheckBox(I18n.getMessage("thaw.plugin.index.useAutoRefresh"));
+
+               JLabel refreshIntervalLabel = new 
JLabel(I18n.getMessage("thaw.plugin.index.autoRefreshInterval"));
+               refreshInterval = new JTextField("");
+
+               JLabel indexPerRefreshLabel = new 
JLabel(I18n.getMessage("thaw.plugin.index.nmbIndexPerRefresh"));
+               indexPerRefresh = new JTextField("");
+
+               resetValues();
+
+               autorefreshActivated.addActionListener(this);
+               configWindow.getOkButton().addActionListener(this);
+               configWindow.getCancelButton().addActionListener(this);
+
+               panel.add(autorefreshActivated);
+               panel.add(refreshIntervalLabel);
+               panel.add(refreshInterval);
+               panel.add(indexPerRefreshLabel);
+               panel.add(indexPerRefresh);
+
+               updateTextFieldState();
+       }
+
+
+       public void addTab() {
+               
configWindow.addTab(I18n.getMessage("thaw.plugin.index.browser"), panel);
+       }
+
+
+       public void removeTab() {
+               saveValues();
+               configWindow.removeTab(panel);
+       }
+
+
+       public void updateTextFieldState() {
+               refreshInterval.setEnabled(autorefreshActivated.isSelected());
+               indexPerRefresh.setEnabled(autorefreshActivated.isSelected());
+       }
+
+       public void resetValues() {
+               boolean activated = AutoRefresh.DEFAULT_ACTIVATED;
+               int refreshIntervalInt = AutoRefresh.DEFAULT_INTERVAL;
+               int nmbIndexInt = AutoRefresh.DEFAULT_INDEX_NUMBER;
+
+               try {
+                       if (config.getValue("indexAutoRefreshActivated") != 
null) {
+                               activated = 
Boolean.valueOf(config.getValue("indexAutoRefreshActivated")).booleanValue();
+                       }
+
+                       if (config.getValue("indexRefreshInterval") != null) {
+                               refreshIntervalInt = 
Integer.parseInt(config.getValue("indexRefreshInterval"));
+                       }
+
+                       if (config.getValue("nmbIndexesPerRefreshInterval") != 
null) {
+                               nmbIndexInt = 
Integer.parseInt(config.getValue("nmbIndexesPerRefreshInterval"));
+                       }
+               } catch(NumberFormatException e) {
+                       Logger.error(this, "Error while parsing config !");
+               }
+
+
+               autorefreshActivated.setSelected(activated);
+               refreshInterval.setText(Integer.toString(refreshIntervalInt));
+               indexPerRefresh.setText(Integer.toString(nmbIndexInt));
+       }
+
+
+       public void saveValues() {
+               config.setValue("indexAutoRefreshActivated",
+                               
Boolean.toString(autorefreshActivated.isSelected()));
+               config.setValue("indexRefreshInterval",
+                               refreshInterval.getText());
+               config.setValue("nmbIndexesPerRefreshInterval",
+                               indexPerRefresh.getText());
+       }
+
+       public void actionPerformed(ActionEvent e) {
+               if (e.getSource() == autorefreshActivated) {
+                       updateTextFieldState();
+               }
+
+               if (e.getSource() == configWindow.getOkButton()) {
+                       saveValues();
+               }
+
+               if (e.getSource() == configWindow.getCancelButton()) {
+                       resetValues();
+               }
+       }
+
+}


Reply via email to