[Schmitzm-commits] r2357 - trunk/schmitzm-hibernate/src/main/java/de/schmitzm/db/hibernate/gui

scm-commit at wald.intevation.org scm-commit at wald.intevation.org
Fri Jul 12 23:40:50 CEST 2013


Author: mojays
Date: 2013-07-12 23:40:49 +0200 (Fri, 12 Jul 2013)
New Revision: 2357

Added:
   trunk/schmitzm-hibernate/src/main/java/de/schmitzm/db/hibernate/gui/DatabaseEntityEditorFrameManager.java
Modified:
   trunk/schmitzm-hibernate/src/main/java/de/schmitzm/db/hibernate/gui/DatabaseEntityEditorFrame.java
Log:
Generic manager class to manage database entity frame instances.

Modified: trunk/schmitzm-hibernate/src/main/java/de/schmitzm/db/hibernate/gui/DatabaseEntityEditorFrame.java
===================================================================
--- trunk/schmitzm-hibernate/src/main/java/de/schmitzm/db/hibernate/gui/DatabaseEntityEditorFrame.java	2013-07-12 16:40:25 UTC (rev 2356)
+++ trunk/schmitzm-hibernate/src/main/java/de/schmitzm/db/hibernate/gui/DatabaseEntityEditorFrame.java	2013-07-12 21:40:49 UTC (rev 2357)
@@ -75,6 +75,7 @@
    */
   public DatabaseEntityEditorFrame(Frame parent, String title, boolean modal, E entity, boolean editable) {
     super(parent, title, modal, true);
+    setAlwaysOnTop(modal);
     setEntity(entity);
     setEditable(editable);
   }

Added: trunk/schmitzm-hibernate/src/main/java/de/schmitzm/db/hibernate/gui/DatabaseEntityEditorFrameManager.java
===================================================================
--- trunk/schmitzm-hibernate/src/main/java/de/schmitzm/db/hibernate/gui/DatabaseEntityEditorFrameManager.java	                        (rev 0)
+++ trunk/schmitzm-hibernate/src/main/java/de/schmitzm/db/hibernate/gui/DatabaseEntityEditorFrameManager.java	2013-07-12 21:40:49 UTC (rev 2357)
@@ -0,0 +1,192 @@
+/**
+ * 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.db.hibernate.gui;
+
+import java.awt.Component;
+import java.awt.Window;
+import java.util.HashSet;
+import java.util.Set;
+
+import org.hibernate.Session;
+
+import de.schmitzm.db.hibernate.HibernateSessionFactory;
+import de.schmitzm.db.hibernate.gui.event.DatabaseUpdateEmitter;
+import de.schmitzm.db.hibernate.gui.event.DatabaseUpdateEvent;
+import de.schmitzm.db.hibernate.gui.event.DatabaseUpdateListener;
+import de.schmitzm.db.hibernate.types.UniqueIDType;
+import de.schmitzm.swing.DialogManager;
+import de.schmitzm.swing.ExceptionDialog;
+import de.schmitzm.swing.SwingUtil;
+
+/**
+ * Dialog manager which manages the editor frame instances (M) for a database entity (E),
+ * so that only one frame instance is present for each entity.
+ * This implementation expects that the entity uses some kind of ID check
+ * in its {@code equals(Object)} method, so that there is only one frame,
+ * also when there are several entity (E) instances who represent the
+ * same database record!
+ * @author Martin O.J. Schmitz
+ *
+ */
+public abstract class DatabaseEntityEditorFrameManager<E extends UniqueIDType, M extends DatabaseEntityEditorFrame<E,?>> extends DialogManager<E,M> implements DatabaseUpdateListener {
+
+  private Set<M> idLessFrames = new HashSet<M>();
+  
+  /**
+   * Creates a new instance of the manager.
+   * @param updateEmitter frame manager listens to database updates from the emitter
+   *                      to recognize when a previously ID-less frame was saved
+   *                      on database
+   */
+  public DatabaseEntityEditorFrameManager(DatabaseUpdateEmitter updateEmitter) {
+    super();
+    if ( updateEmitter != null )
+      updateEmitter.addDatabaseUpdateListener(this);
+  }
+
+  /**
+   * Creates a new frame instance.
+   * @param entity entity to create the frame for
+   * @param owner  parent frame 
+   * @param constArgs constructor arguments for the new frame
+   */
+  protected abstract M createNewFrame(E entity, Window owner, Object... constArgs);
+  
+  /**
+   * Returns a frame instance for the given entity. 
+   * @param entity the entity a frame should be returned for
+   * @param owner owner for a new instance
+   * @param constArgs constructor arguments for a new instance.
+   */
+  @Override
+  public M getInstanceFor(E entity, Component owner, Object... constArgs) {
+    if ( entity == null )
+      return null;
+    try {
+      M frame = dialogCache.get(entity);
+      // Wenn Entity noch keine ID hat, im temporaeren Cache suchen
+      if ( frame == null && entity.getId() == null )
+        frame = searchForIdLessFrame(entity); 
+        
+      // noch kein Frame vorhanden -> Frame erzeugen
+      if ( frame == null ) {
+          if (entity != null && entity.getId() != null) {
+//          HibernateSessionFactory.getSession().refresh(kontakt);
+//          HibernateSessionFactory.getSession(kontakt).refresh(kontakt);
+//            HibernateSessionFactory.getSession(kontakt).merge(kontakt);
+            HibernateSessionFactory.getGlobalFactory().getSession().evict(entity);
+            entity = (E)HibernateSessionFactory.getGlobalFactory().getSession(entity).get(entity.getClass(),entity.getId());
+          }
+        frame = createNewFrame(entity,
+                               SwingUtil.getParentWindow(owner),
+                               constArgs);
+
+        // Wenn Entity noch keine ID hat, wird er nicht
+        // in den normalen Cache geschrieben, da er dort nicht
+        // wiedergefunden wird, wenn er spaeter eine ID hat (der
+        // Hash aendert sich mit der ID!)
+        if ( entity.getId() == null )
+          idLessFrames.add(frame);
+        else
+          dialogCache.put(entity, frame);
+      }
+      return frame;
+    } catch (Exception e) {
+      ExceptionDialog.show(owner, e);
+      return null;
+    } 
+  }
+  
+  /**
+   * Disposes the frame for the specified entity.
+   */
+  @Override
+  public boolean disposeInstanceFor(E entity) {
+    boolean disposed = super.disposeInstanceFor(entity);
+    // close session when frame is closed
+    Session session = HibernateSessionFactory.getGlobalFactory().getSession(entity, false);
+    if ( HibernateSessionFactory.getGlobalFactory().isMultipleSessionsUsed() && 
+         session != null && session.isOpen() ) {
+      session.evict(entity); // ??
+//      session.clear();    // ??
+//      session.close();
+      HibernateSessionFactory.getGlobalFactory().disposeSession(entity);
+    }
+    // If no frame was disposed check the temporary cache
+    // of ID-less frames
+    if ( !disposed ) {
+      M frame = searchForIdLessFrame(entity);
+      if ( frame != null ) {
+        frame.dispose();
+        idLessFrames.remove(frame);
+        return true;
+      }
+    }
+    return disposed;
+  }
+
+  /**
+   * Checks whether a previously ID-less entity is written, so it
+   * now has an ID. In this case the corresponding frame is moved from the
+   * temporary set of ID-less frames to the normal cache.   
+   */
+  @Override
+  public void databaseUpdated(DatabaseUpdateEvent e) {
+// Klappt leider nicht 
+//    if ( !(e.getUpdatedRecord() instanceof E) )
+//      return;
+    
+    M frame = searchForIdLessFrame((E)e.getUpdatedRecord());
+    if ( frame != null ) {
+      dialogCache.put(frame.getEntity(), frame);
+      idLessFrames.remove(frame);
+    }
+  }
+  
+  /**
+   * Searches (brute force) in the temporary set of ID-less frames
+   * for a frame which holds the given entity.
+   * @return {@code null} if there is no frame for the given {@link Kontakt} 
+   */
+  private M searchForIdLessFrame(E entity) {
+    for ( M frame : idLessFrames )
+      if ( frame.getEntity() != null && frame.getEntity().equals(entity) )
+        return frame;
+    return null;
+  }
+
+  /**
+   * Does nothing.
+   */
+  @Override
+  public void databaseConnectionStateChanged(boolean connected) {
+  }
+
+}



More information about the Schmitzm-commits mailing list