/hg/icedtea-web: PolicyEditor uses SwingWorker for async IO

aazores at icedtea.classpath.org aazores at icedtea.classpath.org
Mon Jun 2 17:57:08 UTC 2014


changeset ea0ccfeeed06 in /hg/icedtea-web
details: http://icedtea.classpath.org/hg/icedtea-web?cmd=changeset;node=ea0ccfeeed06
author: Andrew Azores <aazores at redhat.com>
date: Mon Jun 02 13:56:57 2014 -0400

	PolicyEditor uses SwingWorker for async IO

	2014-06-02  Andrew Azores  <aazores at redhat.com>

	    PolicyEditor file I/O made asynchronous with a progress indicator
	    * netx/net/sourceforge/jnlp/security/policyeditor/PolicyEditor.java
	    (savePolicyFile, openAndParsePolicyFile): use SwingWorker to make
	    IO asynchronous, also show a progress bar.
	    (checkPolicyChangesWithDialog): handle Exceptions thrown by
	    policyFile.hasChanged() and policyFile.getFile().getCanonicalPath(),
	    rather than letting them bubble up
	    * netx/net/sourceforge/jnlp/security/policyeditor/IndeterminateProgressDialog.java:
	    new class


diffstat:

 ChangeLog                                                                        |   13 +
 netx/net/sourceforge/jnlp/security/policyeditor/IndeterminateProgressDialog.java |   57 +++
 netx/net/sourceforge/jnlp/security/policyeditor/PolicyEditor.java                |  146 ++++++---
 3 files changed, 172 insertions(+), 44 deletions(-)

diffs (275 lines):

diff -r 428af70f3b7c -r ea0ccfeeed06 ChangeLog
--- a/ChangeLog	Mon Jun 02 09:35:20 2014 -0400
+++ b/ChangeLog	Mon Jun 02 13:56:57 2014 -0400
@@ -1,5 +1,18 @@
 2014-06-02  Andrew Azores  <aazores at redhat.com>
 
+	PolicyEditor file I/O made asynchronous with a progress indicator
+	* netx/net/sourceforge/jnlp/security/policyeditor/PolicyEditor.java
+	(savePolicyFile, openAndParsePolicyFile): use SwingWorker to make
+	IO asynchronous, also show a progress bar.
+	(checkPolicyChangesWithDialog): handle Exceptions thrown by
+	policyFile.hasChanged() and policyFile.getFile().getCanonicalPath(),
+	rather than letting them bubble up
+	* netx/net/sourceforge/jnlp/security/policyeditor/IndeterminateProgressDialog.java:
+	new class
+
+2014-06-02  Andrew Azores  <aazores at redhat.com>
+
+	PolicyEditor file I/O and model logic extracted into a new class
 	* netx/net/sourceforge/jnlp/security/policyeditor/CustomPolicyViewer.java:
 	store PolicyFileModel as field. (updateCustomPermissions): new method
 	* netx/net/sourceforge/jnlp/security/policyeditor/PolicyEditor.java
diff -r 428af70f3b7c -r ea0ccfeeed06 netx/net/sourceforge/jnlp/security/policyeditor/IndeterminateProgressDialog.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/netx/net/sourceforge/jnlp/security/policyeditor/IndeterminateProgressDialog.java	Mon Jun 02 13:56:57 2014 -0400
@@ -0,0 +1,57 @@
+/*Copyright (C) 2014 Red Hat, Inc.
+
+This file is part of IcedTea.
+
+IcedTea is free software; you can redistribute it and/or
+modify it under the terms of the GNU General Public License as published by
+the Free Software Foundation, version 2.
+
+IcedTea is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with IcedTea; see the file COPYING.  If not, write to
+the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library.  Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module.  An independent module is a module which is not derived from
+or based on this library.  If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so.  If you do not wish to do so, delete this
+exception statement from your version.
+ */
+
+package net.sourceforge.jnlp.security.policyeditor;
+
+import java.awt.Window;
+
+import javax.swing.JDialog;
+import javax.swing.JProgressBar;
+
+public class IndeterminateProgressDialog extends JDialog {
+
+    private final JProgressBar progressBar = new JProgressBar();
+
+    public IndeterminateProgressDialog(final Window parent, final String title) {
+        super(parent);
+        this.setTitle(title);
+        this.setModalityType(ModalityType.DOCUMENT_MODAL);
+        progressBar.setIndeterminate(true);
+        this.add(progressBar);
+        this.pack();
+    }
+
+}
diff -r 428af70f3b7c -r ea0ccfeeed06 netx/net/sourceforge/jnlp/security/policyeditor/PolicyEditor.java
--- a/netx/net/sourceforge/jnlp/security/policyeditor/PolicyEditor.java	Mon Jun 02 09:35:20 2014 -0400
+++ b/netx/net/sourceforge/jnlp/security/policyeditor/PolicyEditor.java	Mon Jun 02 13:56:57 2014 -0400
@@ -91,6 +91,7 @@
 import javax.swing.ListSelectionModel;
 import javax.swing.ScrollPaneConstants;
 import javax.swing.SwingUtilities;
+import javax.swing.SwingWorker;
 import javax.swing.UIManager;
 import javax.swing.WindowConstants;
 import javax.swing.border.EmptyBorder;
@@ -101,6 +102,7 @@
 import net.sourceforge.jnlp.security.policyeditor.PolicyEditorPermissions.Group;
 import net.sourceforge.jnlp.util.FileUtils;
 import net.sourceforge.jnlp.util.FileUtils.OpenFileResult;
+import net.sourceforge.jnlp.util.ScreenFinder;
 import net.sourceforge.jnlp.util.logging.OutputController;
 
 /**
@@ -1100,54 +1102,99 @@
         if (ofr == OpenFileResult.CANT_WRITE) {
             FileUtils.showReadOnlyDialog(weakThis.get());
         }
-        try {
-            policyFile.openAndParsePolicyFile();
-            changesMade = false;
-            for (final String codebase : policyFile.getCodebases()) {
-                final String model;
-                if (codebase.isEmpty()) {
-                    model = R("PEGlobalSettings");
-                } else {
-                    model = codebase;
+
+        final Window parentWindow = SwingUtilities.getWindowAncestor(this);
+        final JDialog progressIndicator = new IndeterminateProgressDialog(parentWindow, "Loading...");
+        final SwingWorker<Void, Void> openPolicyFileWorker = new SwingWorker<Void, Void>() {
+            @Override
+            protected Void doInBackground() throws Exception {
+                try {
+                    SwingUtilities.invokeLater(new Runnable() {
+                        @Override
+                        public void run() {
+                            progressIndicator.setLocationRelativeTo(parentWindow);
+                            progressIndicator.setVisible(true);
+                        }
+                    });
+                    policyFile.openAndParsePolicyFile();
+                } catch (final FileNotFoundException fnfe) {
+                    OutputController.getLogger().log(fnfe);
+                    FileUtils.showCouldNotOpenDialog(weakThis.get(), R("PECouldNotOpen"));
+                } catch (final IOException ioe) {
+                    OutputController.getLogger().log(ioe);
+                    OutputController.getLogger().log(OutputController.Level.ERROR_ALL, R("RCantOpenFile", policyFile.getFile().getPath()));
+                    FileUtils.showCouldNotOpenDialog(weakThis.get(), R("PECouldNotOpen"));
                 }
-                listModel.addElement(model);
+                return null;
             }
-            addNewCodebase("");
-        } catch (final FileNotFoundException fnfe) {
-            OutputController.getLogger().log(fnfe);
-            FileUtils.showCouldNotOpenDialog(weakThis.get(), R("PECouldNotOpen"));
-        } catch (final IOException ioe) {
-            OutputController.getLogger().log(ioe);
-            OutputController.getLogger().log(OutputController.Level.ERROR_ALL, R("RCantOpenFile", policyFile.getFile().getPath()));
-            FileUtils.showCouldNotOpenDialog(weakThis.get(), R("PECouldNotOpen"));
-        }
+
+            @Override
+            public void done() {
+                changesMade = false;
+                for (final String codebase : policyFile.getCodebases()) {
+                    final String model;
+                    if (codebase.isEmpty()) {
+                        model = R("PEGlobalSettings");
+                    } else {
+                        model = codebase;
+                    }
+                    listModel.addElement(model);
+                }
+                addNewCodebase("");
+                progressIndicator.setVisible(false);
+                progressIndicator.dispose();
+            }
+        };
+        openPolicyFileWorker.execute();
     }
 
     /**
      * Save the policy model into the file pointed to by the filePath field.
      */
     private void savePolicyFile() {
-        try {
-            final int overwriteChanges = checkPolicyChangesWithDialog();
-            switch (overwriteChanges) {
-                case JOptionPane.YES_OPTION:
-                    openAndParsePolicyFile();
-                    return;
-                case JOptionPane.NO_OPTION:
-                    break;
-                case JOptionPane.CANCEL_OPTION:
-                    return;
-                default:
-                    break;
+        final int overwriteChanges = checkPolicyChangesWithDialog();
+        switch (overwriteChanges) {
+            case JOptionPane.YES_OPTION:
+                openAndParsePolicyFile();
+                return;
+            case JOptionPane.NO_OPTION:
+                break;
+            case JOptionPane.CANCEL_OPTION:
+                return;
+            default:
+                break;
+        }
+
+        final Window parentWindow = SwingUtilities.getWindowAncestor(this);
+        final JDialog progressIndicator = new IndeterminateProgressDialog(parentWindow, "Saving...");
+        final SwingWorker<Void, Void> savePolicyFileWorker = new SwingWorker<Void, Void>() {
+            @Override
+            public Void doInBackground() throws Exception {
+                try {
+                    SwingUtilities.invokeLater(new Runnable() {
+                        @Override
+                        public void run() {
+                            progressIndicator.setLocationRelativeTo(parentWindow);
+                            progressIndicator.setVisible(true);
+                        }
+                    });
+                    policyFile.savePolicyFile();
+                } catch (final IOException e) {
+                    OutputController.getLogger().log(e);
+                    showCouldNotSaveDialog();
+                }
+                return null;
             }
-            policyFile.savePolicyFile();
-            changesMade = false;
-        } catch (final IOException e) {
-            OutputController.getLogger().log(e);
-            showCouldNotSaveDialog();
-            return;
-        }
-        showChangesSavedDialog();
+
+            @Override
+            public void done() {
+                changesMade = false;
+                showChangesSavedDialog();
+                progressIndicator.setVisible(false);
+                progressIndicator.dispose();
+            }
+        };
+        savePolicyFileWorker.execute();
     }
 
     /**
@@ -1182,17 +1229,28 @@
      * to the settings. "No" otherwise
      * @throws IOException if the file cannot be read
      */
-    private int checkPolicyChangesWithDialog() throws IOException {
-        final boolean changed;
+    private int checkPolicyChangesWithDialog() {
+        boolean changed;
         try {
             changed = policyFile.hasChanged();
-        } catch (FileNotFoundException e){
+        } catch (FileNotFoundException e) {
+            OutputController.getLogger().log(e);
             JOptionPane.showMessageDialog(weakThis.get(), R("PEFileMissing"), R("PEFileModified"), JOptionPane.WARNING_MESSAGE);
             return JOptionPane.NO_OPTION;
+        } catch (IOException e) {
+            OutputController.getLogger().log(e);
+            changed = true;
         }
         if (changed) {
-            return JOptionPane.showConfirmDialog(weakThis.get(), R("PEFileModifiedDetail", policyFile.getFile().getCanonicalPath()),
-                    R("PEFileModified"), JOptionPane.YES_NO_CANCEL_OPTION);
+            String policyFilePath;
+            try {
+                policyFilePath = policyFile.getFile().getCanonicalPath();
+            } catch (final IOException e) {
+                OutputController.getLogger().log(e);
+                policyFilePath = policyFile.getFile().getPath();
+            }
+            return JOptionPane.showConfirmDialog(weakThis.get(), R("PEFileModifiedDetail", policyFilePath,
+                    R("PEFileModified"), JOptionPane.YES_NO_CANCEL_OPTION));
         } else if (!changesMade) {
             //Return without saving or reloading
             return JOptionPane.CANCEL_OPTION;


More information about the distro-pkg-dev mailing list