[jsword-svn] common/java/swing/org/crosswire/common/progress/swing s

jswordcvs at crosswire.org jswordcvs at crosswire.org
Mon Mar 7 19:31:42 MST 2005


Update of /cvs/jsword/common/java/swing/org/crosswire/common/progress/swing
In directory www.crosswire.org:/tmp/cvs-serv23334/java/swing/org/crosswire/common/progress/swing

Modified Files:
	JobsViewPane.java JobsProgressBar.java 
Log Message:
Improved the Job class allowing for cooperative interruption.
Fixed the download cancel. Improved the cancel of indexing.
It works but you have to restart to try to index again.

Index: JobsViewPane.java
===================================================================
RCS file: /cvs/jsword/common/java/swing/org/crosswire/common/progress/swing/JobsViewPane.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -C2 -d -r1.7 -r1.8
*** JobsViewPane.java	6 Mar 2005 20:21:59 -0000	1.7
--- JobsViewPane.java	8 Mar 2005 02:31:40 -0000	1.8
***************
*** 161,165 ****
          // 3) not have an accelerator
          JButton cancel = new JButton(Msg.CANCEL.toString());
!         if (!job.canInterrupt())
          {
              cancel.setEnabled(false);
--- 161,165 ----
          // 3) not have an accelerator
          JButton cancel = new JButton(Msg.CANCEL.toString());
!         if (!job.isInterruptable())
          {
              cancel.setEnabled(false);

Index: JobsProgressBar.java
===================================================================
RCS file: /cvs/jsword/common/java/swing/org/crosswire/common/progress/swing/JobsProgressBar.java,v
retrieving revision 1.10
retrieving revision 1.11
diff -C2 -d -r1.10 -r1.11
*** JobsProgressBar.java	6 Mar 2005 20:21:59 -0000	1.10
--- JobsProgressBar.java	8 Mar 2005 02:31:40 -0000	1.11
***************
*** 9,12 ****
--- 9,14 ----
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
+ import java.beans.PropertyChangeEvent;
+ import java.beans.PropertyChangeListener;
  import java.util.ArrayList;
  import java.util.HashMap;
***************
*** 53,57 ****
   * @version $Id$
   */
! public class JobsProgressBar extends JPanel implements WorkListener
  {
      /**
--- 55,59 ----
   * @version $Id$
   */
! public class JobsProgressBar extends JPanel implements WorkListener, PropertyChangeListener
  {
      /**
***************
*** 64,68 ****
          if (small)
          {
!             // They start of at 15pt (on Windows at least)
              font = new Font("SansSerif", Font.PLAIN, 10); //$NON-NLS-1$
          }
--- 66,70 ----
          if (small)
          {
!             // They start off at 15pt (on Windows at least)
              font = new Font("SansSerif", Font.PLAIN, 10); //$NON-NLS-1$
          }
***************
*** 111,114 ****
--- 113,118 ----
      protected synchronized void addJob(Job job)
      {
+         job.addPropertyChangeListener(this);
+ 
          int i = findEmptyPosition();
          log.debug("adding job to panel at " + i + ": " + job.getJobDescription()); //$NON-NLS-1$ //$NON-NLS-2$
***************
*** 128,137 ****
          // progress.setPreferredSize(preferred);
  
!         // Decorate the progress bar if necessary
!         Component decorated = decorateProgressBar(job, progress);
!         this.add(decorated, i);
!         this.revalidate();
! 
!         JobData jobdata = new JobData(job, i, decorated, progress);
          jobs.put(job, jobdata);
          if (i >= positions.size())
--- 132,136 ----
          // progress.setPreferredSize(preferred);
  
!         JobData jobdata = new JobData(job, i, progress);
          jobs.put(job, jobdata);
          if (i >= positions.size())
***************
*** 143,202 ****
              positions.set(i, jobdata);
          }
-     }
  
!     /**
!      * Decorate the progress bar if the job can be interrupted.
!      * We put the cancel button in a 1 row, 2 column grid
!      * where the button is in a minimally sized fixed cell
!      * and the progress meter follows in a horizontally stretchy cell
!      */
!     private Component decorateProgressBar(Job job, JProgressBar progress)
!     {
!         if (!job.canInterrupt())
!         {
!             return progress;
!         }
! 
!         JPanel panel = new JPanel(new GridBagLayout());
!         GridBagConstraints gbc = new GridBagConstraints();
!         gbc.gridwidth = 1;
!         gbc.fill = GridBagConstraints.NONE;
!         panel.add(createCancelButton(job), gbc);
!         gbc.weightx = 1.0;
!         gbc.gridwidth = GridBagConstraints.REMAINDER;
!         gbc.fill = GridBagConstraints.HORIZONTAL;
!         panel.add(progress, gbc);
!         return panel;
      }
  
!     /**
!      * Create a cancel button that only shows the cancel icon.
!      * When the button is pressed the job is interrupted.
!      * @param job A job to interrupt when the button is pressed
!      * @return a custom cancel button
!      */
!     private JButton createCancelButton(final Job job)
      {
!         Icon stop = GuiUtil.getIcon("toolbarButtonGraphics/general/Stop16.gif"); //$NON-NLS-1$
! 
!         // Create a cancel button
!         JButton cancel = new JButton(stop);
!         // Only paint the icon not the button
!         cancel.setContentAreaFilled(false);
!         // Make the button as small as possible
!         cancel.setMargin(new Insets(0, 0, 0, 0));
!         // We don't need no stinkin' border
!         cancel.setBorderPainted(false);
!         // Under WinXP this does nothing
!         cancel.setRolloverEnabled(true);
!         cancel.setToolTipText(Msg.CANCEL.toString());
!         cancel.addActionListener(new ActionListener()
!         {
!             public void actionPerformed(ActionEvent ev)
!             {
!                 job.interrupt();
!             }
!         });
!         return cancel;
      }
  
--- 142,155 ----
              positions.set(i, jobdata);
          }
  
!         this.add(jobdata.getComponent(), i);
!         this.revalidate();
      }
  
!     public void propertyChange(PropertyChangeEvent evt)
      {
!         Job job = (Job) evt.getSource();
!         JobData jobdata = (JobData) jobs.get(job);
!         jobdata.propertyChange(evt);
      }
  
***************
*** 218,221 ****
--- 171,176 ----
      protected synchronized void removeJob(Job job)
      {
+         job.addPropertyChangeListener(this);
+ 
          JobData jobdata = (JobData) jobs.get(job);
  
***************
*** 281,295 ****
       * A simple struct to group information about a Job
       */
!     private static class JobData
      {
          /**
           * Simple ctor
           */
!         JobData(Job job, int index, Component comp, JProgressBar progress)
          {
              this.job = job;
-             this.comp = comp;
-             this.progress = progress;
              this.index = index;
          }
  
--- 236,250 ----
       * A simple struct to group information about a Job
       */
!     private static class JobData implements PropertyChangeListener
      {
          /**
           * Simple ctor
           */
!         JobData(Job job, int index, JProgressBar progress)
          {
              this.job = job;
              this.index = index;
+             this.progress = progress;
+             this.comp = decorateProgressBar();
          }
  
***************
*** 329,332 ****
--- 284,299 ----
  
          /**
+          * @return Returns the cancelButton.
+          */
+         public JButton getCancelButton()
+         {
+             if (cancelButton == null)
+             {
+                 cancelButton = createCancelButton();
+             }
+             return cancelButton;
+         }
+         
+         /**
           * Accessor for the index
           */
***************
*** 336,343 ****
          }
  
          private Job job;
-         private Component comp;
-         private JProgressBar progress;
          private int index;
      }
  }
--- 303,375 ----
          }
  
+         public void propertyChange(PropertyChangeEvent evt)
+         {
+             if (cancelButton != null)
+             {
+                 cancelButton.setEnabled(job.isInterruptable());
+             }
+         }
+ 
+         /**
+          * Create a cancel button that only shows the cancel icon.
+          * When the button is pressed the job is interrupted.
+          * @return a custom cancel button
+          */
+         private JButton createCancelButton()
+         {
+             Icon stop = GuiUtil.getIcon("toolbarButtonGraphics/general/Stop16.gif"); //$NON-NLS-1$
+ 
+             // Create a cancel button
+             cancelButton = new JButton(stop);
+             // Only paint the icon not the button
+             cancelButton.setContentAreaFilled(false);
+             // Make the button as small as possible
+             cancelButton.setMargin(new Insets(0, 0, 0, 0));
+             // We don't need no stinkin' border
+             cancelButton.setBorderPainted(false);
+             // Under WinXP this does nothing
+             cancelButton.setRolloverEnabled(true);
+             cancelButton.setToolTipText(Msg.CANCEL.toString());
+             cancelButton.addActionListener(new ActionListener()
+             {
+                 public void actionPerformed(ActionEvent ev)
+                 {
+                     getJob().interrupt();
+                 }
+             });
+             return cancelButton;
+         }
+ 
+         /**
+          * Decorate the progress bar if the job can be interrupted.
+          * We put the cancel button in a 1 row, 2 column grid
+          * where the button is in a minimally sized fixed cell
+          * and the progress meter follows in a horizontally stretchy cell
+          */
+         private Component decorateProgressBar()
+         {
+             if (!job.isInterruptable())
+             {
+                 return progress;
+             }
+ 
+             JPanel panel = new JPanel(new GridBagLayout());
+             GridBagConstraints gbc = new GridBagConstraints();
+             gbc.gridwidth = 1;
+             gbc.fill = GridBagConstraints.NONE;
+             panel.add(createCancelButton(), gbc);
+             gbc.weightx = 1.0;
+             gbc.gridwidth = GridBagConstraints.REMAINDER;
+             gbc.fill = GridBagConstraints.HORIZONTAL;
+             panel.add(progress, gbc);
+             return panel;
+         }
+ 
          private Job job;
          private int index;
+         private JProgressBar progress;
+         private Component comp;
+         private JButton cancelButton;
      }
+ 
  }



More information about the jsword-svn mailing list