[Schmitzm-commits] r2375 - in trunk: schmitzm-core/src/main/java/de/schmitzm/swing schmitzm-core/src/main/java/de/schmitzm/swing/event schmitzm-core/src/main/resources/de/schmitzm/swing/resource/locales schmitzm-hibernate/src/main/java/de/schmitzm/db/hibernate/gui schmitzm-hibernate/src/main/java/de/schmitzm/db/hibernate/gui/event

scm-commit at wald.intevation.org scm-commit at wald.intevation.org
Sat Jul 27 15:29:59 CEST 2013


Author: mojays
Date: 2013-07-27 15:29:59 +0200 (Sat, 27 Jul 2013)
New Revision: 2375

Added:
   trunk/schmitzm-core/src/main/java/de/schmitzm/swing/ListItemsSwingWorker.java
   trunk/schmitzm-core/src/main/java/de/schmitzm/swing/event/DefaultValueAdjustingSettable.java
Modified:
   trunk/schmitzm-core/src/main/resources/de/schmitzm/swing/resource/locales/SwingResourceBundle.properties
   trunk/schmitzm-core/src/main/resources/de/schmitzm/swing/resource/locales/SwingResourceBundle_de.properties
   trunk/schmitzm-hibernate/src/main/java/de/schmitzm/db/hibernate/gui/DatabaseEntityTableModel.java
   trunk/schmitzm-hibernate/src/main/java/de/schmitzm/db/hibernate/gui/event/DatabaseUpdateAdapter.java
   trunk/schmitzm-hibernate/src/main/java/de/schmitzm/db/hibernate/gui/event/DatabaseUpdateEmitter.java
   trunk/schmitzm-hibernate/src/main/java/de/schmitzm/db/hibernate/gui/event/DatabaseUpdateEvent.java
Log:
DatabaseUpdateEvent: "value adjusting" property (by new DefaultValueAdjustingSettable)
new generic ListItemsSwingWorker to perform work on list items without progress dialog (while GUI is locked by wait cursor and progress is optionally shown in main frame status bar)

Added: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/ListItemsSwingWorker.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/ListItemsSwingWorker.java	                        (rev 0)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/ListItemsSwingWorker.java	2013-07-27 13:29:59 UTC (rev 2375)
@@ -0,0 +1,282 @@
+/**
+ * Copyright (c) 2009 Martin O. J. Schmitz.
+ * 
+ * This file is part of the SCHMITZM library - a collection of utility 
+ * classes based on Java 1.6, focusing (not only) on Java Swing 
+ * and the Geotools library.
+ * 
+ * The SCHMITZM project is hosted at:
+ * http://wald.intevation.org/projects/schmitzm/
+ * 
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 3
+ * of the License, or (at your option) any later version.
+ * 
+ * This program 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 Lesser General Public License (license.txt)
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ * or try this link: http://www.gnu.org/licenses/lgpl.html
+ * 
+ * Contributors:
+ *     Martin O. J. Schmitz - initial API and implementation
+ *     Stefan A. Tzeggai - additional utility classes
+ */
+package de.schmitzm.swing;
+
+import java.awt.Component;
+import java.awt.event.KeyAdapter;
+import java.awt.event.KeyEvent;
+import java.awt.event.KeyListener;
+import java.util.List;
+
+import javax.swing.JOptionPane;
+import javax.swing.JProgressBar;
+
+import de.schmitzm.io.ProgressUpdater;
+import de.schmitzm.lang.LangUtil;
+
+/**
+ * @author Martin O.J. Schmitz
+ *
+ */
+public abstract class ListItemsSwingWorker<E> extends Thread {
+  /** Indicates that operation should be interrupted. */
+  private boolean operationCanceled = false;
+  
+  /** Holds the parent swing component. This component will be blocked
+   *  by wait cursor until the work is done. */
+  protected Component parent;
+  /** Holds the items to perform the {@link Work} for. */
+  protected List<E> list;
+  /** Holds the item indices (of {@link #list}) to perform the work for (if {@code null}
+   *  the work is performed for all list items. */
+  protected int[] selIdx;
+  /** Defines the process work to perform for each list item. */
+  protected Work<E> work;
+  /** Holds the progress updater to update during work. */
+  protected ProgressUpdater progressUpdater;
+  
+  /** 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)}. */
+  protected String errorMess;
+  /** Holds the confirmation dialog message, which will be shown if the work is
+   *  canceled via ESC key. Can be changed by {@link #initMessages(String, String, String, String)}. */
+  protected String cancelConfirmMess;
+  /** Holds the confirmation dialog title, which will be shown if the work is
+   *  canceled via ESC key. Can be changed by {@link #initMessages(String, String, String, String)}. */
+  protected String cancelDialogTitle;
+  /** Holds the message, which will be shown after the work was
+   *  canceled via ESC key. Can be changed by {@link #initMessages(String, String, String, String)}. */
+  protected String cancelResultMess;
+  
+  
+  /** Listens for ESC key and asks the user to interrupt the running
+   *  operation. */
+  private KeyListener escListener = new KeyAdapter() {
+    @Override
+    public void keyPressed(KeyEvent e) {
+      if ( e.getKeyCode() != 27 )
+        return;
+      SwingUtil.invokeLater( new Runnable() {
+        @Override
+        public void run() {
+          // Confirm cancel
+          int ret = JOptionPane.showConfirmDialog(parent,
+                                                  cancelConfirmMess,
+                                                  cancelDialogTitle,
+                                                  JOptionPane.YES_NO_OPTION,
+                                                  JOptionPane.QUESTION_MESSAGE);
+          if ( ret != JOptionPane.YES_OPTION )
+            return;
+          // Cancel operation
+          operationCanceled = true;
+        }
+      });
+    }
+  };
+  
+  
+  /**
+   * Creates new worker which processes all list items.
+   * @param parent  parent component for wait cursor and message dialogs 
+   * @param list    items to process
+   * @param work    defines the work to process for each item
+   * @param progressBar progress bar to update during process (can be {@code null})
+   */
+  public ListItemsSwingWorker(Component parent, List<E> list, Work<E> work, JProgressBar progressBar) {
+    this(parent,list,null,work,progressBar);
+  }
+  
+  
+  /**
+   * Creates new worker.
+   * @param parent  parent component for wait cursor and message dialogs 
+   * @param list    items to process
+   * @param selIdx  item indices of list to process (if {@code null} all items will be
+   *                processed)
+   * @param work    defines the work to process for each item
+   * @param progressBar progress bar to update during process (can be {@code null})
+   */
+  public ListItemsSwingWorker(Component parent, List<E> list, int[] selIdx, Work<E> work, JProgressBar progressBar) {
+    this(parent,list,selIdx,work,
+         progressBar != null ? new ProgressBarUpdater(progressBar) : (ProgressBarUpdater)null);
+  }
+
+  /**
+   * Creates new worker which processes all list items.
+   * @param parent  parent component for wait cursor and message dialogs 
+   * @param list    items to process
+   * @param work    defines the work to process for each item
+   * @param progressUpdater used to show the progress during process (can be {@code null})
+   */
+  public ListItemsSwingWorker(Component parent, List<E> list, Work<E> work, ProgressUpdater progressUpdater) {
+    this(parent,list,null,work,progressUpdater);
+  }
+  
+  /**
+   * Creates new worker.
+   * @param parent  parent component for wait cursor and message dialogs 
+   * @param list    items to process
+   * @param selIdx  item indices of list to process (if {@code null} all items will be
+   *                processed)
+   * @param work    defines the work to process for each item
+   * @param progressUpdater used to show the progress during process (can be {@code null})
+   */
+  public ListItemsSwingWorker(Component parent, List<E> list, int[] selIdx, Work<E> work, ProgressUpdater progressUpdater) {
+    super();
+    this.parent = parent;
+    this.list   = list;
+    this.selIdx = selIdx;
+    this.work   = work;
+    this.progressUpdater = progressUpdater;
+    initMessages(null,null,null,null);
+  }
+  
+  /**
+   * Initializes the messages possibly shown during process. If {@code null} is
+   * specified, a default message is used. 
+   * @param cancelDialogTitle dialog title for (yes/no) cancel confirmation dialog
+   * @param cancelConfirmMess message for (yes/no) cancel confirmation dialog
+   * @param cancelResultMess  message for messages which pop up (showing the processed items)
+   *                          when process was canceled
+   * @param errorMess         message shown on error for an item
+   */
+  public void initMessages(String cancelDialogTitle, String cancelConfirmMess, String cancelResultMess, String errorMess) {
+    this.cancelConfirmMess = cancelConfirmMess != null ? cancelConfirmMess : SwingUtil.R("ListItemsSwingWorker.cancel.mess");
+    this.cancelResultMess  = cancelResultMess  != null ? cancelResultMess  : SwingUtil.R("ListItemsSwingWorker.cancel.result");
+    this.cancelDialogTitle = cancelDialogTitle != null ? cancelDialogTitle : SwingUtil.R("ListItemsSwingWorker.cancel.title");
+    this.errorMess         = errorMess         != null ? errorMess         : SwingUtil.R("ListItemsSwingWorker.error.mess");
+  }
+  
+  
+  /**
+   * Does nothing. Sub classes can override this method to perform
+   * some commands just after wait cursor was set and work process starts.<br>
+   * Note: This method is called in running Thread! GUI updates should
+   * be done on EDT explicitly. 
+   */
+  public void initWork() {
+  }
+  
+  /**
+   * If process was canceled, a message dialog pops up to show the number of
+   * processed items. Sub classes can override this method to perform
+   * some commands just before cursor will be reseted.<br>
+   * Note: This method is called in running Thread! GUI updates should
+   * be done on EDT explicitly. 
+   */
+  public void finishWork(int processedItems, int itemCount) {
+    if ( wasCanceled() ) {
+      JOptionPane.showMessageDialog(parent,
+                                    LangUtil.replaceParameters(cancelResultMess,processedItems,itemCount),
+                                    cancelDialogTitle,
+                                    JOptionPane.INFORMATION_MESSAGE);
+    }
+  }
+  
+  /**
+   * Returns whether the operation was canceled.
+   * @return {@code false} while work is in progress
+   */
+  public boolean wasCanceled() {
+    return operationCanceled;
+  }
+  
+  /**
+   * Returns a description for the item, which is used to show in
+   * error messages. This implementation simply returns the {@link Object#toString()}
+   * method. Sub classes can override this method to return a more specific description.
+   */
+  public String getItemDescription(E item) {
+    return item.toString();
+  }
+  
+  /**
+   * Called on error on 
+   * @param item
+   * @param err
+   */
+  public void performError(E item, Exception err) {
+    ExceptionDialog.show(parent,
+                         err,
+                         getItemDescription(item),
+                         errorMess+": "+err.getMessage());
+  }
+  
+  /**
+   * Returns a list item.
+   * @return {@code list.get(i)} if not selection indices are specified, 
+   *         {@code list.get( selIdx[i] )} otherwise
+   */
+  protected E getItem(int i) {
+    if ( selIdx != null )
+      i = selIdx[i];
+    return list.get(i);
+  }
+  
+  /**
+   * Performs the work for all (selected) list items.
+   */
+  @Override
+  public void run() {
+    operationCanceled = false;
+    ProgressUpdater progressUpdater = this.progressUpdater != null ? this.progressUpdater : ProgressUpdater.DUMMY;
+
+    SwingUtil.setWaitCursor(this.parent, this.escListener);
+    initWork();
+    int itemCount = selIdx != null ? selIdx.length : list.size();
+    progressUpdater.initProgress(0, itemCount);
+    int processedItems = 0;
+    
+    for (int i=0; i<itemCount && !operationCanceled; i++) {
+      E item = getItem(i);
+      try {
+        work.performWorkForItem(item);
+        progressUpdater.updateProgress(i+1);
+        processedItems++;
+      } catch (Exception err) {
+        performError(item, err);
+      }
+    }
+    finishWork(processedItems, itemCount);
+    
+    SwingUtil.resetCursor(parent);
+  }
+  
+  /**
+   * Specifies the work to perform for each list item.
+   */
+  public static interface Work<E> {
+    /**
+     * Called for each list item to process the work.
+     */
+    public void performWorkForItem(E item) throws Exception;
+  }
+
+}

Added: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/event/DefaultValueAdjustingSettable.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/event/DefaultValueAdjustingSettable.java	                        (rev 0)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/event/DefaultValueAdjustingSettable.java	2013-07-27 13:29:59 UTC (rev 2375)
@@ -0,0 +1,26 @@
+package de.schmitzm.swing.event;
+
+/**
+ * Default implementation of {@link ValueAdjustingSettable} which simply maintains
+ * a {@link #valueIsAdjusting} flag and corresponding getter and setter methods.
+ * @author Martin O.J. Schmitz
+ *
+ */
+public class DefaultValueAdjustingSettable implements ValueAdjustingSettable {
+  protected boolean valueIsAdjusting = false;
+  
+  /**
+   * Sets the "value adjusting" flag.
+   */
+  @Override
+  public void setValueIsAdjusting(boolean valueIsAdjusting) {
+    this.valueIsAdjusting = valueIsAdjusting;
+  }
+
+  /**
+   * Returns whether the "value adjusting" flag is set.
+   */
+  public boolean getValueIsAdjusting() {
+    return this.valueIsAdjusting;
+  }
+}
\ No newline at end of file

Modified: trunk/schmitzm-core/src/main/resources/de/schmitzm/swing/resource/locales/SwingResourceBundle.properties
===================================================================
--- trunk/schmitzm-core/src/main/resources/de/schmitzm/swing/resource/locales/SwingResourceBundle.properties	2013-07-26 17:12:22 UTC (rev 2374)
+++ trunk/schmitzm-core/src/main/resources/de/schmitzm/swing/resource/locales/SwingResourceBundle.properties	2013-07-27 13:29:59 UTC (rev 2375)
@@ -355,3 +355,7 @@
 AbstractMutableListPanel.action.remove.conf.mess=All ${0} selected items will be deleted. Continue?
 AbstractMutableListPanel.action.remove.err.mess=No items selected!
 
+ListItemsSwingWorker.cancel.title=Cancel
+ListItemsSwingWorker.cancel.mess=Cancel running operation?
+ListItemsSwingWorker.cancel.result=Operation canceled. ${0} of ${1} items were processed.
+ListItemsSwingWorker.error.mess=Error during operation

Modified: trunk/schmitzm-core/src/main/resources/de/schmitzm/swing/resource/locales/SwingResourceBundle_de.properties
===================================================================
--- trunk/schmitzm-core/src/main/resources/de/schmitzm/swing/resource/locales/SwingResourceBundle_de.properties	2013-07-26 17:12:22 UTC (rev 2374)
+++ trunk/schmitzm-core/src/main/resources/de/schmitzm/swing/resource/locales/SwingResourceBundle_de.properties	2013-07-27 13:29:59 UTC (rev 2375)
@@ -326,3 +326,8 @@
 AbstractMutableListPanel.action.remove.conf.title=L\u00f6schen...
 AbstractMutableListPanel.action.remove.conf.mess=Alle ${0} ausgew\u00e4hlten Elemente werden gel\u00f6scht. Aktion fortsetzen?
 AbstractMutableListPanel.action.remove.err.mess=Keine Datens\u00e4tze ausgew\u00e4hlt!
+
+ListItemsSwingWorker.cancel.title=Abbrechen
+ListItemsSwingWorker.cancel.mess=Laufende Operation abbrechen?
+ListItemsSwingWorker.cancel.result=Operation abgebrochen. ${0} von ${1} Elemente bearbeitet.
+ListItemsSwingWorker.error.mess=Fehler bei der Bearbeitung

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-26 17:12:22 UTC (rev 2374)
+++ trunk/schmitzm-hibernate/src/main/java/de/schmitzm/db/hibernate/gui/DatabaseEntityTableModel.java	2013-07-27 13:29:59 UTC (rev 2375)
@@ -33,6 +33,10 @@
   /** Holds the {@link DatabaseUpdateEmitter} which informs the table about
    *  database changes. */
   protected DatabaseUpdateEmitter dbUpdateEmitter = null;
+
+  /** Flag indicates during database update events that, when value is not adjusting anymore
+   *  the table has to be updated completely (not just the one changed row) */
+  private boolean doCompleteUpdate = false;
   
   /**
    * Creates a new table model.
@@ -140,14 +144,30 @@
     int row = entities.indexOf( e.getUpdatedRecord() );
     if (e instanceof DatabaseRecordDeletedEvent){
       entities.remove(row);
-      fireTableRowsDeleted(row, row);   		
+      if ( e.getValueIsAdjusting() )
+        doCompleteUpdate = true;
+      else {
+        if ( doCompleteUpdate ) {
+          fireTableDataChanged();
+          doCompleteUpdate = false;
+        } else
+          fireTableRowsDeleted(row, row);
+      }
     } else {
       E updatedRecord = (E)e.getUpdatedRecord();
       int id      = updatedRecord.getId();
       updatedRecord = (E)DBUtil.getGlobalSession().get(updatedRecord.getClass(), id);
       DBUtil.getGlobalSession().evict(updatedRecord);
       entities.set(row,updatedRecord);
-      fireTableRowsUpdated(row, row);
+      if ( e.getValueIsAdjusting() )
+        doCompleteUpdate = true;
+      else {
+        if ( doCompleteUpdate ) {
+          fireTableDataChanged();
+          doCompleteUpdate = false;
+        } else
+          fireTableRowsUpdated(row, row);
+      }
     }
   }
 

Modified: trunk/schmitzm-hibernate/src/main/java/de/schmitzm/db/hibernate/gui/event/DatabaseUpdateAdapter.java
===================================================================
--- trunk/schmitzm-hibernate/src/main/java/de/schmitzm/db/hibernate/gui/event/DatabaseUpdateAdapter.java	2013-07-26 17:12:22 UTC (rev 2374)
+++ trunk/schmitzm-hibernate/src/main/java/de/schmitzm/db/hibernate/gui/event/DatabaseUpdateAdapter.java	2013-07-27 13:29:59 UTC (rev 2375)
@@ -5,22 +5,19 @@
  * @author <a href="mailto:martin.schmitz at koeln.de">Martin Schmitz</a>
  *
  */
-public class DatabaseUpdateAdapter {
+public class DatabaseUpdateAdapter implements DatabaseUpdateListener {
   /**
    * Called if a table changed.
    */
+  @Override
   public void databaseUpdated(DatabaseUpdateEvent e) {
   }
 
   /**
-   * Called when database connection was established.
+   * Called when the database connection state changes.
+   * @param connected indicates whether the connection was established or closed
    */
-  public void databaseConnectionEstablished() {
+  @Override
+  public void databaseConnectionStateChanged(boolean connected) {
   }
-
-  /**
-   * Called when database connection was closed
-   */
-  public void databaseConnectionReleased() {
-  }
 }

Modified: trunk/schmitzm-hibernate/src/main/java/de/schmitzm/db/hibernate/gui/event/DatabaseUpdateEmitter.java
===================================================================
--- trunk/schmitzm-hibernate/src/main/java/de/schmitzm/db/hibernate/gui/event/DatabaseUpdateEmitter.java	2013-07-26 17:12:22 UTC (rev 2374)
+++ trunk/schmitzm-hibernate/src/main/java/de/schmitzm/db/hibernate/gui/event/DatabaseUpdateEmitter.java	2013-07-27 13:29:59 UTC (rev 2375)
@@ -55,30 +55,73 @@
    * @param updatedRecord updated database entity
    */
   public void fireDatabaseUpdate(Object source, Object updatedRecord) {
-    fireDatabaseUpdate(new DatabaseUpdateEvent(source, updatedRecord));
+    fireDatabaseUpdate(source, updatedRecord, false);
   }
 
   /**
    * Calls {@link DatabaseUpdateListener#databaseUpdated(DatabaseUpdateEvent)}
    * for all connected listeners.
    * @param source object which initiates the event
+   * @param updatedRecord updated database entity
+   * @param valueIsAdjusting indicates whether the update is part of multiple following
+   *                         updates (to avoid that listeners do unnecessary things) 
+   */
+  public void fireDatabaseUpdate(Object source, Object updatedRecord, boolean valueIsAdjusting) {
+    DatabaseUpdateEvent event = new DatabaseUpdateEvent(source, updatedRecord);
+    event.setValueIsAdjusting(valueIsAdjusting);
+    fireDatabaseUpdate(event);
+  }
+
+  /**
+   * Calls {@link DatabaseUpdateListener#databaseUpdated(DatabaseUpdateEvent)}
+   * for all connected listeners.
+   * @param source object which initiates the event
    * @param deletedRecord deleted database entity
    */
   public void fireDatabaseRecordDeleted(Object source, Object deletedRecord) {
-	  fireDatabaseUpdate(new DatabaseRecordDeletedEvent(source, deletedRecord));
+	  fireDatabaseUpdate(source, deletedRecord,false);
   }
 
   /**
    * Calls {@link DatabaseUpdateListener#databaseUpdated(DatabaseUpdateEvent)}
+   * for all connected listeners.
+   * @param source object which initiates the event
+   * @param deletedRecord deleted database entity
+   * @param valueIsAdjusting indicates whether the update is part of multiple following
+   *                         updates (to avoid that listeners do unnecessary things) 
+   */
+  public void fireDatabaseRecordDeleted(Object source, Object deletedRecord, boolean valueIsAdjusting) {
+      DatabaseRecordDeletedEvent event = new DatabaseRecordDeletedEvent(source, deletedRecord);
+      event.setValueIsAdjusting(valueIsAdjusting);
+      fireDatabaseUpdate(event);
+  }
+
+  /**
+   * Calls {@link DatabaseUpdateListener#databaseUpdated(DatabaseUpdateEvent)}
    * for all connected listeners
    * @param source object which initiates the event
    * @param updatedType updated type/table
    */
   public void fireDatabaseUpdate(Object source, Class<?> updatedType) {
-    fireDatabaseUpdate(new DatabaseUpdateEvent(source, updatedType));
+    fireDatabaseUpdate(source, updatedType, false);
   }
 
   /**
+   * Calls {@link DatabaseUpdateListener#databaseUpdated(DatabaseUpdateEvent)}
+   * for all connected listeners
+   * @param source object which initiates the event
+   * @param updatedType updated type/table
+   * @param valueIsAdjusting indicates whether the update is part of multiple following
+   *                         updates (to avoid that listeners do unnecessary things) 
+   */
+  public void fireDatabaseUpdate(Object source, Class<?> updatedType, boolean valueIsAdjusting) {
+    DatabaseUpdateEvent event = new DatabaseUpdateEvent(source, updatedType);
+    event.setValueIsAdjusting(valueIsAdjusting);
+    fireDatabaseUpdate(event);
+  }
+
+
+  /**
    * Calls {@link DatabaseUpdateListener#databaseConnectionEstablished()}
    * for all connected listeners.
    */

Modified: trunk/schmitzm-hibernate/src/main/java/de/schmitzm/db/hibernate/gui/event/DatabaseUpdateEvent.java
===================================================================
--- trunk/schmitzm-hibernate/src/main/java/de/schmitzm/db/hibernate/gui/event/DatabaseUpdateEvent.java	2013-07-26 17:12:22 UTC (rev 2374)
+++ trunk/schmitzm-hibernate/src/main/java/de/schmitzm/db/hibernate/gui/event/DatabaseUpdateEvent.java	2013-07-27 13:29:59 UTC (rev 2375)
@@ -1,29 +1,30 @@
 package de.schmitzm.db.hibernate.gui.event;
 
 import de.schmitzm.db.hibernate.DBUtil;
+import de.schmitzm.swing.event.DefaultValueAdjustingSettable;
 
 /**
- * Ereignis einer DB-Aenderungen (Daten, Verbindung, ...).
+ * Event which indicates a database change (e.g. record updated, deleted).
  * @author <a href="mailto:martin.schmitz at koeln.de">Martin Schmitz</a>
  *
  */
-public class DatabaseUpdateEvent{
-  /** Enthaelt die Tabelle, die sich geaendert hat. */
+public class DatabaseUpdateEvent extends DefaultValueAdjustingSettable {
+  /** Holds the table name which has been changed. */
   protected String updatedTable = null;
-  /** Enthaelt den zur Tabelle korrespondierenden Datentyp,
-   *  der sich geaendert hat. */
+  /** Holds the corresponding entity type of the database table which has
+   *  been changed. */
   protected Class<?> updatedType = null;
-  /** Enthaelt den Datensatz, der sich geaendert hat. */
+  /** Holds the record which has been changed. */
   protected Object updatedRecord = null;
   
-  /** Enthaelt des Ausloeser des Events. */
+  /** Holds the object which originated the change. */
   protected Object updateSource = null;
   
   /**
-   * Erzeugt ein neues Event. Dem Event wird kein Datensatz zugeordnet.
-   * @param source Ausloeser des Events.
-   * @param updatedTable Tabelle, die sich geaendert hat
-   * @param updatedType  Zu der Tabelle korrespondierender Datentyp
+   * Creates a new event which is not assigned to a record.
+   * @param source origin of the event
+   * @param updatedTable name of changed table
+   * @param updatedType  entity type which corresponds to the changed table
    */
   public <E> DatabaseUpdateEvent(Object source, String updatedTable, Class<E> updatedType) {
     this.updateSource = source;
@@ -33,10 +34,10 @@
   }
 
   /**
-   * Erzeugt ein neues Event. Dem Event wird kein Datentyp zugeordnet.
-   * @param source Ausloeser des Events.
-   * @param updatedTable Tabelle, die sich geaendert hat
-   * @param updatedRecord  Datensatz, der sich geaendert hat
+   * Creates a new event for a concrete record.
+   * @param source origin of the event
+   * @param updatedTable name of changed table
+   * @param updatedRecord  changed record
    */
   public <E> DatabaseUpdateEvent(Object source, String updatedTable, Object updatedRecord) {
     this.updateSource = source;
@@ -46,81 +47,93 @@
   }
 
   /**
-   * Erzeugt ein allgemeines Event. Dem Event wird weder ein Datentyp,
-   * noch eine Tabelle oder ein Datensatz zugeordnet.
+   * Creates a common event which is not assigned to a source, table or record.
    */
   public <E> DatabaseUpdateEvent() {
     this(null, null,(Class<?>)null);
   }
 
   /**
-   * Erzeugt ein allgemeines Event. Dem Event wird weder ein Datentyp,
-   * noch eine Tabelle oder ein Datensatz zugeordnet.
-   * @param source Ausloeser des Events.
+   * Creates a common event which is not assigned to a table or record.
+   * @param source origin of the event
    */
   public <E> DatabaseUpdateEvent(Object source) {
     this(source, null,(Class<?>)null);
   }
 
   /**
-   * Erzeugt ein neues Event. Dem Event wird weder ein Datentyp,
-   * noch ein Datensatz zugeordnet.
-   * @param source Ausloeser des Events.
-   * @param updatedTable Tabelle, die sich geaendert hat
+   * Creates a new event.
+   * @param source origin of the event
+   * @param updatedTable name of changed table
    */
   public <E> DatabaseUpdateEvent(Object source, String updatedTable) {
     this(source, updatedTable,(Class<?>)null);
   }
 
   /**
-   * Erzeugt ein neues Event. Die Tablle wird ueber
-   * {@link DBUtil#getDatabaseTable(Class)} ermittelt. Dem Event
-   * wird kein Datensatz zugeordnet.
-   * @param source Ausloeser des Events.
-   * @param updatedType   Datentyp, der sich geaendert hat
+   * Creates a new event which is not assigned to a specific record.
+   * The table name is determined by {@link DBUtil#getDatabaseTable(Class)}.
+   * @param source origin of the event
+   * @param updatedType  entity type which corresponds to the changed table
    */
   public <E> DatabaseUpdateEvent(Object source, Class<E> updatedType) {
     this(source, DBUtil.getDatabaseTable(updatedType), updatedType);
   }
 
   /**
-   * Erzeugt ein neues Event. Die Tablle wird ueber
-   * {@link DBUtil#getDatabaseTable(Class)} ermittelt.
-   * @param source Ausloeser des Events.
-   * @param updatedRecord  Datensatz, der sich geaendert hat
+   * Creates a new event which is assigned to a specific record.
+   * The table name is determined by {@link DBUtil#getDatabaseTable(Class)}.
+   * @param source origin of the event
+   * @param updatedRecord  changed record
    */
   public <E> DatabaseUpdateEvent(Object source, Object updatedRecord) {
     this(source, updatedRecord != null ? DBUtil.getDatabaseTable( updatedRecord.getClass()) : null, updatedRecord);
   }
 
   /**
-   * Liefert den Ausloeser des Events.
+   * Returns the origin of the database change.
    */
   public Object getSource() {
     return updateSource;
   }
 
   /**
-   * Liefert die Tabelle, die sich geaendert hat.
+   * Returns the table which has been changed.
    */
   public String getUpdatedTable() {
     return updatedTable;
   }
 
   /**
-   * Liefert den Datentyp, der geaendert hat.
+   * Returns the data type which has been changed.
    */
   public Class<?> getUpdatedType() {
     return updatedType;
   }
 
   /**
-   * Liefert den Datensatz, der geaendert hat.
+   * Returns the record which has been changed.
    */
   public Object getUpdatedRecord() {
     return updatedRecord;
   }
 
+  /**
+   * Sets whether the database change is part of many changes. Listeners can
+   * use this flag to e.g. avoid unnecessary GUI updates. 
+   */
+  @Override
+  public void setValueIsAdjusting(boolean valueIsAdjusting) {
+    this.valueIsAdjusting = valueIsAdjusting;
+  }
 
+  /**
+   * Returns whether the database change is part of many changes. Listeners can
+   * use this flag to e.g. avoid unnecessary GUI updates. 
+   */
+  public boolean getValueIsAdjusting() {
+    return this.valueIsAdjusting;
+  }
 
+
 }



More information about the Schmitzm-commits mailing list