changeset 939:cea32875903d

Clear cache function made more visible.
author Jiri Vanek <jvanek@redhat.com>
date Thu, 20 Mar 2014 16:55:12 +0100
parents d133c4ebfe24
children 674128ef4394
files ChangeLog netx/net/sourceforge/jnlp/cache/CacheUtil.java netx/net/sourceforge/jnlp/controlpanel/CachePane.java netx/net/sourceforge/jnlp/resources/Messages.properties netx/net/sourceforge/jnlp/splashscreen/parts/JEditorPaneBasedExceptionDialog.java netx/net/sourceforge/jnlp/util/BasicExceptionDialog.java
diffstat 6 files changed, 163 insertions(+), 37 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Thu Mar 20 16:29:46 2014 +0100
+++ b/ChangeLog	Thu Mar 20 16:55:12 2014 +0100
@@ -1,3 +1,19 @@
+2014-03-20  Jiri Vanek  <jvanek@redhat.com>
+
+	Clear cache function made more visible.
+	* netx/net/sourceforge/jnlp/cache/CacheUtil.java: (okToClearCache) released
+	never released lock. (clearCache) now recriated directory after cleaning.
+	* netx/net/sourceforge/jnlp/controlpanel/CachePane.java: Added delete
+	all button. (restoreDisabled) and (disableButtons) are containing duplicated
+	code. (invokeLaterDeleteAll) and (visualCleanCache) utility methods accessing
+	CacheUtil.clearCache.
+	* netx/net/sourceforge/jnlp/resources/Messages.properties: added (CVCPCleanCache)
+	and (CVCPCleanCacheTip) keys
+	* netx/net/sourceforge/jnlp/splashscreen/parts/JEditorPaneBasedExceptionDialog.java:
+	added (cacheButton)
+	* netx/net/sourceforge/jnlp/util/BasicExceptionDialog.java: also added (cacheButton)
+	but also included some layout refactoring to have buttons in row.
+
 2014-03-20  Jiri Vanek  <jvanek@redhat.com>
 
 	Methods validating manifests' attributes moved to separate class.
--- a/netx/net/sourceforge/jnlp/cache/CacheUtil.java	Thu Mar 20 16:29:46 2014 +0100
+++ b/netx/net/sourceforge/jnlp/cache/CacheUtil.java	Thu Mar 20 16:55:12 2014 +0100
@@ -191,6 +191,7 @@
         try {
             cacheDir = cacheDir.getCanonicalFile();
             FileUtils.recursiveDelete(cacheDir, cacheDir);
+            cacheDir.mkdir();
         } catch (IOException e) {
             throw new RuntimeException(e);
         }
@@ -202,14 +203,15 @@
      * @return true if the cache can be cleared at this time without problems
      */
     private static boolean okToClearCache() {
-        File otherJavawsRunning = new File(JNLPRuntime.getConfiguration()
-                .getProperty(DeploymentConfiguration.KEY_USER_NETX_RUNNING_FILE));
+        File otherJavawsRunning = new File(JNLPRuntime.getConfiguration().getProperty(DeploymentConfiguration.KEY_USER_NETX_RUNNING_FILE));
+        FileLock locking = null;
         try {
             if (otherJavawsRunning.isFile()) {
                 FileOutputStream fis = new FileOutputStream(otherJavawsRunning);
                 
                 FileChannel channel = fis.getChannel();
-                if (channel.tryLock() == null) {
+                locking  = channel.tryLock();
+                if (locking == null) {
                     OutputController.getLogger().log("Other instances of netx are running");
                     return false;
                 }
@@ -222,6 +224,14 @@
             }
         } catch (IOException e) {
             return false;
+        } finally {
+            if (locking != null) {
+                try {
+                    locking.release();
+                } catch (IOException ex) {
+                    OutputController.getLogger().log(ex);
+                }
+            }
         }
     }
 
--- a/netx/net/sourceforge/jnlp/controlpanel/CachePane.java	Thu Mar 20 16:29:46 2014 +0100
+++ b/netx/net/sourceforge/jnlp/controlpanel/CachePane.java	Thu Mar 20 16:55:12 2014 +0100
@@ -46,6 +46,7 @@
 import javax.swing.JButton;
 import javax.swing.JComponent;
 import javax.swing.JDialog;
+import javax.swing.JOptionPane;
 import javax.swing.JPanel;
 import javax.swing.JScrollPane;
 import javax.swing.JTable;
@@ -58,6 +59,7 @@
 
 import net.sourceforge.jnlp.cache.CacheDirectory;
 import net.sourceforge.jnlp.cache.CacheLRUWrapper;
+import net.sourceforge.jnlp.cache.CacheUtil;
 import net.sourceforge.jnlp.cache.DirectoryNode;
 import net.sourceforge.jnlp.config.DeploymentConfiguration;
 import net.sourceforge.jnlp.runtime.Translator;
@@ -80,7 +82,7 @@
             Translator.R("CVCPColSize"),
             Translator.R("CVCPColLastModified") };
     JTable cacheTable;
-    private JButton deleteButton, refreshButton, doneButton;
+    private JButton deleteButton, refreshButton, doneButton, cleanAll;
 
     /**
      * Creates a new instance of the CachePane.
@@ -192,12 +194,7 @@
         deleteButton.addActionListener(new ActionListener() {
             @Override
             public void actionPerformed(ActionEvent e) {
-                // Deleting may take a while, so indicate busy by cursor
-                parent.getContentPane().setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
-                // Disable dialog and buttons while deleting
-                deleteButton.setEnabled(false);
-                refreshButton.setEnabled(false);
-                doneButton.setEnabled(false);
+                disableButtons();
                 // Delete on AWT thread after this action has been performed
                 // in order to allow the cache viewer to update itself
                 invokeLaterDelete();
@@ -206,14 +203,24 @@
         deleteButton.setEnabled(false);
         buttons.add(deleteButton);
 
+        this.cleanAll = new JButton(Translator.R("CVCPCleanCache"));
+        cleanAll.addActionListener(new ActionListener() {
+
+            @Override
+            public void actionPerformed(ActionEvent e) {
+                disableButtons();
+                // Delete on AWT thread after this action has been performed
+                // in order to allow the cache viewer to update itself
+                invokeLaterDeleteAll();
+            }
+        });
+        buttons.add(cleanAll);
+
         this.refreshButton = new JButton(Translator.R("CVCPButRefresh"));
         refreshButton.addActionListener(new ActionListener() {
             @Override
             public void actionPerformed(ActionEvent e) {
-                // Disable all its controls when performing cacheTable refresh (populating)
-                deleteButton.setEnabled(false);
-                refreshButton.setEnabled(false);
-                doneButton.setEnabled(false);
+                disableButtons();
                 // Populate cacheTable on AWT thread after this action event has been performed
                 invokeLaterPopulateTable();
             }
@@ -259,7 +266,7 @@
      * {@link CacheViewer} have been instantiated and painted.
      * @see CachePane#cacheTable
      */
-    private final void invokeLaterDelete() {
+    private  void invokeLaterDelete() {
         EventQueue.invokeLater(new Runnable() {
             @Override
             public void run() {
@@ -283,6 +290,7 @@
                     int row = cacheTable.getSelectedRow();
                     try {
                         if (fl == null) {
+                            JOptionPane.showMessageDialog(parent, Translator.R("CCannotClearCache"));
                             return;
                         }
                         int modelRow = cacheTable.convertRowIndexToModel(row);
@@ -310,21 +318,7 @@
                 } catch (Exception exception) {
                         OutputController.getLogger().log(OutputController.Level.ERROR_DEBUG, exception);
                 } finally {
-                    // If nothing selected then keep deleteButton disabled
-                    if (!cacheTable.getSelectionModel().isSelectionEmpty()) {
-                        deleteButton.setEnabled(true);
-                    }
-                    // Enable buttons
-                    refreshButton.setEnabled(true);
-                    doneButton.setEnabled(true);
-                    // If cacheTable is empty disable it and set background
-                    // color to indicate being disabled
-                    if (cacheTable.getModel().getRowCount() == 0) {
-                        cacheTable.setEnabled(false);
-                        cacheTable.setBackground(SystemColor.control);
-                    }
-                    // Reset cursor
-                    parent.getContentPane().setCursor(Cursor.getDefaultCursor());
+                    restoreDisabled();
                 }
             }
 
@@ -344,6 +338,23 @@
         });
     }
 
+    private void invokeLaterDeleteAll() {
+        EventQueue.invokeLater(new Runnable() {
+
+            @Override
+            public void run() {
+                try {
+                    visualCleanCache(parent);
+                    populateTable();
+                } catch (Exception exception) {
+                    OutputController.getLogger().log(OutputController.Level.ERROR_DEBUG, exception);
+                } finally {
+                    restoreDisabled();
+                }
+            }
+        });
+    }
+
     /**
      * Posts an event to the event queue to populate the
      * {@link CachePane#cacheTable} after the {@code CachePane} and
@@ -371,6 +382,7 @@
                 } finally {
                     refreshButton.setEnabled(true);
                     doneButton.setEnabled(true);
+                    cleanAll.setEnabled(true);
                 }
             }
         });
@@ -439,4 +451,43 @@
             defaultFocusComponent.requestFocusInWindow();
         }
     }
+
+    public void disableButtons() {
+        // may take a while, so indicate busy by cursor
+        parent.getContentPane().setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
+        // Disable dialog and buttons while operating
+        deleteButton.setEnabled(false);
+        refreshButton.setEnabled(false);
+        doneButton.setEnabled(false);
+        cleanAll.setEnabled(false);
+    }
+
+    public void restoreDisabled() {
+        cleanAll.setEnabled(true);
+        // If nothing selected then keep deleteButton disabled
+        if (!cacheTable.getSelectionModel().isSelectionEmpty()) {
+            deleteButton.setEnabled(true);
+        }
+        // Enable buttons
+        refreshButton.setEnabled(true);
+        doneButton.setEnabled(true);
+        // If cacheTable is empty disable it and set background
+        // color to indicate being disabled
+        if (cacheTable.getModel().getRowCount() == 0) {
+            cacheTable.setEnabled(false);
+            cacheTable.setBackground(SystemColor.control);
+        }
+        // Reset cursor
+        parent.getContentPane().setCursor(Cursor.getDefaultCursor());
+    }
+
+    public static boolean visualCleanCache(Component parent) {
+        boolean success = CacheUtil.clearCache();
+        if (!success) {
+            JOptionPane.showMessageDialog(parent, Translator.R("CCannotClearCache"));
+        }
+        return success;
+    }
 }
+
+
--- a/netx/net/sourceforge/jnlp/resources/Messages.properties	Thu Mar 20 16:29:46 2014 +0100
+++ b/netx/net/sourceforge/jnlp/resources/Messages.properties	Thu Mar 20 16:55:12 2014 +0100
@@ -679,6 +679,8 @@
 CVCPDialogTitle=Cache Viewer
 CVCPButRefresh=Refresh
 CVCPButDelete=Delete
+CVCPCleanCache=Clean all cache
+CVCPCleanCacheTip=Some errors may be caused by old files in your cache. Before submitting the bug, you may clear cache and try to run application again. 
 CVCPColLastModified=Last Modified
 CVCPColSize=Size (Bytes)
 CVCPColDomain=Domain
--- a/netx/net/sourceforge/jnlp/splashscreen/parts/JEditorPaneBasedExceptionDialog.java	Thu Mar 20 16:29:46 2014 +0100
+++ b/netx/net/sourceforge/jnlp/splashscreen/parts/JEditorPaneBasedExceptionDialog.java	Thu Mar 20 16:55:12 2014 +0100
@@ -74,6 +74,7 @@
     private JButton homeButton;
     private JButton aboutButton;
     private JButton consoleButton;
+    private JButton cacheButton;
     private JEditorPane htmlErrorAndHelpPanel;
     private JLabel exceptionLabel;
     private JLabel iconLabel;
@@ -143,6 +144,7 @@
         homeButton = new JButton();
         aboutButton = new JButton();
         consoleButton = BasicExceptionDialog.getShowButton(JEditorPaneBasedExceptionDialog.this);
+        cacheButton = BasicExceptionDialog.getClearCacheButton(JEditorPaneBasedExceptionDialog.this);
 
         setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
 
@@ -174,6 +176,8 @@
                         .addContainerGap()
                         .addComponent(aboutButton)
                         .addContainerGap()
+                        .addComponent(cacheButton)
+                        .addContainerGap()
                         .addComponent(consoleButton)
                         .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 314, Short.MAX_VALUE)
                         .addComponent(closeAndCopyButton)
@@ -185,6 +189,7 @@
                     .addGroup(jPanel2Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                         .addComponent(closeButton)
                         .addComponent(aboutButton)
+                        .addComponent(cacheButton)
                         .addComponent(consoleButton)
                         .addComponent(closeAndCopyButton))
                     .addContainerGap()));
--- a/netx/net/sourceforge/jnlp/util/BasicExceptionDialog.java	Thu Mar 20 16:29:46 2014 +0100
+++ b/netx/net/sourceforge/jnlp/util/BasicExceptionDialog.java	Thu Mar 20 16:55:12 2014 +0100
@@ -56,6 +56,8 @@
 import javax.swing.JPanel;
 import javax.swing.JScrollPane;
 import javax.swing.JTextArea;
+import javax.swing.SwingUtilities;
+import net.sourceforge.jnlp.controlpanel.CachePane;
 import net.sourceforge.jnlp.util.logging.JavaConsole;
 
 /**
@@ -79,23 +81,39 @@
         final JDialog errorDialog = optionPane.createDialog(R("Error"));
         errorDialog.setIconImages(ImageResources.INSTANCE.getApplicationImages());
 
-        final JPanel quickInfoPanel = new JPanel();
-        BoxLayout layout = new BoxLayout(quickInfoPanel, BoxLayout.Y_AXIS);
-        quickInfoPanel.setLayout(layout);
-        mainPanel.add(quickInfoPanel, BorderLayout.PAGE_START);
+        final JPanel quickInfoPanelAll = new JPanel();
+        final JPanel quickInfoPanelMessage = new JPanel();
+        final JPanel quickInfoPanelButtons = new JPanel();
+        BoxLayout layoutAll = new BoxLayout(quickInfoPanelAll, BoxLayout.Y_AXIS);
+        BoxLayout layoutMessage = new BoxLayout(quickInfoPanelMessage, BoxLayout.X_AXIS);
+        BoxLayout layoutButtons = new BoxLayout(quickInfoPanelButtons, BoxLayout.X_AXIS);
+        quickInfoPanelAll.setLayout(layoutAll);
+        quickInfoPanelMessage.setLayout(layoutMessage);
+        quickInfoPanelButtons.setLayout(layoutButtons);
+        mainPanel.add(quickInfoPanelAll, BorderLayout.PAGE_START);
+        quickInfoPanelAll.add(quickInfoPanelMessage);
+        quickInfoPanelAll.add(quickInfoPanelButtons);
 
         JLabel errorLabel = new JLabel(exception.getMessage());
         errorLabel.setAlignmentY(JComponent.LEFT_ALIGNMENT);
-        quickInfoPanel.add(errorLabel);
+        quickInfoPanelMessage.add(errorLabel);
 
         final JButton viewDetails = new JButton(R("ButShowDetails"));
         viewDetails.setAlignmentY(JComponent.LEFT_ALIGNMENT);
         viewDetails.setActionCommand("show");
-        quickInfoPanel.add(viewDetails);
+        quickInfoPanelButtons.add(viewDetails);
+
+        final JButton cacheButton = getClearCacheButton(errorDialog);
+        cacheButton.setAlignmentY(JComponent.LEFT_ALIGNMENT);
+        quickInfoPanelButtons.add(cacheButton);
 
         final JButton consoleButton = getShowButton(errorDialog);
         consoleButton.setAlignmentY(JComponent.LEFT_ALIGNMENT);
-        quickInfoPanel.add(consoleButton);
+        quickInfoPanelButtons.add(consoleButton);
+
+        final JPanel fillRest = new JPanel();
+        fillRest.setAlignmentY(JComponent.LEFT_ALIGNMENT);
+        quickInfoPanelButtons.add(fillRest);
 
         JTextArea textArea = new JTextArea();
         textArea.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
@@ -149,4 +167,28 @@
         }
         return consoleButton;
     }
+
+    public static JButton getClearCacheButton(final Component parent) {
+        JButton clearAllButton = new JButton();
+        clearAllButton.setText(R("CVCPCleanCache"));
+        clearAllButton.setToolTipText(R("CVCPCleanCacheTip"));
+        clearAllButton.addActionListener(new java.awt.event.ActionListener() {
+
+            @Override
+            public void actionPerformed(java.awt.event.ActionEvent evt) {
+                SwingUtilities.invokeLater(new Runnable() {
+
+                    @Override
+                    public void run() {
+                        try {
+                            CachePane.visualCleanCache(parent);
+                        } catch (Exception ex) {
+                            OutputController.getLogger().log(OutputController.Level.ERROR_DEBUG, ex);
+                        }
+                    }
+                });
+            }
+        });
+        return clearAllButton;
+    }
 }