[Schmitzm-commits] r2376 - in trunk: schmitzm-core/src/main/java/de/schmitzm/swing schmitzm-hibernate/src/main/java/de/schmitzm/db/hibernate/gui

scm-commit at wald.intevation.org scm-commit at wald.intevation.org
Sat Jul 27 17:14:55 CEST 2013


Author: mojays
Date: 2013-07-27 17:14:55 +0200 (Sat, 27 Jul 2013)
New Revision: 2376

Modified:
   trunk/schmitzm-core/src/main/java/de/schmitzm/swing/ListItemsSwingWorker.java
   trunk/schmitzm-hibernate/src/main/java/de/schmitzm/db/hibernate/gui/DatabaseEntityTableModel.java
Log:
ListItemsSwingWorker improved by some additional methods.

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/ListItemsSwingWorker.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/ListItemsSwingWorker.java	2013-07-27 13:29:59 UTC (rev 2375)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/ListItemsSwingWorker.java	2013-07-27 15:14:55 UTC (rev 2376)
@@ -33,6 +33,7 @@
 import java.awt.event.KeyAdapter;
 import java.awt.event.KeyEvent;
 import java.awt.event.KeyListener;
+import java.util.ArrayList;
 import java.util.List;
 
 import javax.swing.JOptionPane;
@@ -61,6 +62,8 @@
   protected Work<E> work;
   /** Holds the progress updater to update during work. */
   protected ProgressUpdater progressUpdater;
+  /** Holds the processed items. */
+  protected List<E> processedItems = new ArrayList<E>();
   
   /** Holds the message, which will be shown during work when
    *  an error occurs on an item. Can be changed by {@link #initMessages(String, String, String, String)}. */
@@ -191,10 +194,10 @@
    * Note: This method is called in running Thread! GUI updates should
    * be done on EDT explicitly. 
    */
-  public void finishWork(int processedItems, int itemCount) {
+  public void finishWork(List<E> processedItems, int itemCount) {
     if ( wasCanceled() ) {
       JOptionPane.showMessageDialog(parent,
-                                    LangUtil.replaceParameters(cancelResultMess,processedItems,itemCount),
+                                    LangUtil.replaceParameters(cancelResultMess,processedItems.size(),itemCount),
                                     cancelDialogTitle,
                                     JOptionPane.INFORMATION_MESSAGE);
     }
@@ -241,6 +244,13 @@
   }
   
   /**
+   * Returns the items which were successfully processed.
+   */
+  protected List<E> getProcessedItems() {
+    return processedItems;
+  }
+  
+  /**
    * Performs the work for all (selected) list items.
    */
   @Override
@@ -252,18 +262,19 @@
     initWork();
     int itemCount = selIdx != null ? selIdx.length : list.size();
     progressUpdater.initProgress(0, itemCount);
-    int processedItems = 0;
-    
+    processedItems.clear();
+    work.performBeforeWork(this);
     for (int i=0; i<itemCount && !operationCanceled; i++) {
       E item = getItem(i);
       try {
         work.performWorkForItem(item);
+        processedItems.add(item);
         progressUpdater.updateProgress(i+1);
-        processedItems++;
       } catch (Exception err) {
         performError(item, err);
       }
     }
+    work.performAfterWork(operationCanceled,processedItems);
     finishWork(processedItems, itemCount);
     
     SwingUtil.resetCursor(parent);
@@ -274,9 +285,47 @@
    */
   public static interface Work<E> {
     /**
+     * Called before list items are processed.
+     * @param worker worker which performs the work
+     */
+    public void performBeforeWork(ListItemsSwingWorker<E> worker);
+
+    /**
      * Called for each list item to process the work.
      */
     public void performWorkForItem(E item) throws Exception;
+
+    /**
+     * Called after all list item were processed.
+     * @param canceled indicates whether work was canceled
+     * @param processedItems processed items (until interruption)
+     * 
+     */
+    public void performAfterWork(boolean canceled, List<E> processedItems);
   }
+  
+  /**
+   * Default implementation of {@link Work} which does nothing in {@link #performBeforeWork()}
+   * and {@link #performAfterWork(List)}.
+   * @author Martin O.J. Schmitz
+   */
+  public static abstract class AbstractWork<E> implements Work<E> {
+    /**
+     * Called before list items are processed. Does nothing.
+     * @param worker worker which performs the work
+     */
+    @Override
+    public void performBeforeWork(ListItemsSwingWorker<E> worker) {
+    }
+    
+    /**
+     * Called after all list item were processed. Does nothing.
+     * @param canceled indicates whether work was canceled
+     * @param processedItems processed items (until interruption)
+     */
+    @Override
+    public void performAfterWork(boolean canceled, List<E> processedItems) {
+    }
+  }
 
 }

Modified: trunk/schmitzm-hibernate/src/main/java/de/schmitzm/db/hibernate/gui/DatabaseEntityTableModel.java
===================================================================
--- trunk/schmitzm-hibernate/src/main/java/de/schmitzm/db/hibernate/gui/DatabaseEntityTableModel.java	2013-07-27 13:29:59 UTC (rev 2375)
+++ trunk/schmitzm-hibernate/src/main/java/de/schmitzm/db/hibernate/gui/DatabaseEntityTableModel.java	2013-07-27 15:14:55 UTC (rev 2376)
@@ -8,6 +8,7 @@
 import de.schmitzm.db.hibernate.DBUtil;
 import de.schmitzm.db.hibernate.HibernateSessionFactory;
 import de.schmitzm.db.hibernate.gui.event.DatabaseRecordDeletedEvent;
+import de.schmitzm.db.hibernate.gui.event.DatabaseRecordUpdatedEvent;
 import de.schmitzm.db.hibernate.gui.event.DatabaseUpdateEmitter;
 import de.schmitzm.db.hibernate.gui.event.DatabaseUpdateEvent;
 import de.schmitzm.db.hibernate.gui.event.DatabaseUpdateListener;
@@ -153,7 +154,7 @@
         } else
           fireTableRowsDeleted(row, row);
       }
-    } else {
+    } else if ( e instanceof DatabaseRecordUpdatedEvent ) {
       E updatedRecord = (E)e.getUpdatedRecord();
       int id      = updatedRecord.getId();
       updatedRecord = (E)DBUtil.getGlobalSession().get(updatedRecord.getClass(), id);



More information about the Schmitzm-commits mailing list