zinger.bsheet
Class PersistenceManager

java.lang.Object
  extended by java.awt.event.WindowAdapter
      extended by zinger.bsheet.PersistenceManager
All Implemented Interfaces:
java.awt.event.ActionListener, java.awt.event.WindowFocusListener, java.awt.event.WindowListener, java.awt.event.WindowStateListener, java.util.EventListener, javax.swing.event.TableModelListener

public class PersistenceManager
extends java.awt.event.WindowAdapter
implements java.awt.event.ActionListener, javax.swing.event.TableModelListener

Persistence manager for a single table model.

Author:
Alexey Zinger (inline_four@yahoo.com)

Field Summary
protected  java.io.File dataFile
          File the model currently persists to.
protected  boolean documentChanged
           
protected static java.util.Set instances
           
protected  java.util.Set listeners
           
protected  java.util.Map<javax.swing.filechooser.FileFilter,Persistence> persistenceMap
           
protected  javax.swing.JTable table
           
 
Constructor Summary
PersistenceManager(javax.swing.JTable table)
           
 
Method Summary
 void actionPerformed(java.awt.event.ActionEvent ev)
          Handles menu item events.
 void addPersistence(Persistence persistence)
           
 void addPersistenceListener(PersistenceListener listener)
           
static void closeAll()
          Steps through all instances and calls saveOnClose() on each.
protected static void disposed(PersistenceManager impexp)
          Removes specified instance from non-disposed instance set.
protected  void disposeWindow()
          Convenience method for finding the window component containing the table and disposing of that window.
protected  boolean doLoad(Persistence persistence)
          Attempts to load specified data file.
protected  Persistence findPersistenceForFile(java.io.File file)
           
protected  void fireEvent(PersistenceEvent ev)
           
protected  Persistence getChosenPersistence()
           
protected  javax.swing.JFileChooser getFileChooser()
           
protected  boolean load()
          Loads table model data from a picked file.
 boolean load(java.io.File dataFile)
           
protected  int needsSave()
          Determines if the data needs to be saved.
protected static void opened(PersistenceManager impexp)
          Adds specified instance to the non-disposed instance set.
 void removePersistence(Persistence persistence)
           
 void removePersistenceListener(PersistenceListener listener)
           
protected  boolean save()
          Persists table model data into a picked file.
 void saveOnClose()
          Determines if the data needs to be saved and saves it if it does.
 void tableChanged(javax.swing.event.TableModelEvent ev)
           
protected  void updateFrameTitle()
          Sets the title of the frame that contains the table to the name of the persistence file.
 void windowClosing(java.awt.event.WindowEvent ev)
          Handles window closing events.
 
Methods inherited from class java.awt.event.WindowAdapter
windowActivated, windowClosed, windowDeactivated, windowDeiconified, windowGainedFocus, windowIconified, windowLostFocus, windowOpened, windowStateChanged
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

instances

protected static java.util.Set instances

table

protected final javax.swing.JTable table

persistenceMap

protected final java.util.Map<javax.swing.filechooser.FileFilter,Persistence> persistenceMap

dataFile

protected java.io.File dataFile
File the model currently persists to. If null, a file must be chosen or created prior to saving.


listeners

protected final java.util.Set listeners

documentChanged

protected boolean documentChanged
Constructor Detail

PersistenceManager

public PersistenceManager(javax.swing.JTable table)
Method Detail

actionPerformed

public void actionPerformed(java.awt.event.ActionEvent ev)
Handles menu item events.

Specified by:
actionPerformed in interface java.awt.event.ActionListener

tableChanged

public void tableChanged(javax.swing.event.TableModelEvent ev)
Specified by:
tableChanged in interface javax.swing.event.TableModelListener
Since:
1.0.4

updateFrameTitle

protected void updateFrameTitle()
Sets the title of the frame that contains the table to the name of the persistence file.


getChosenPersistence

protected Persistence getChosenPersistence()

findPersistenceForFile

protected Persistence findPersistenceForFile(java.io.File file)
Since:
1.10

load

protected boolean load()
Loads table model data from a picked file. Prompts the user for a file and passes the call to load(File).

See Also:
Persistence.load(javax.swing.JTable, java.io.File)

doLoad

protected boolean doLoad(Persistence persistence)
Attempts to load specified data file. If no persistence is specified, we try to locate one by extensio using findPersistenceForFile(java.io.File).

Since:
1.1.0

load

public boolean load(java.io.File dataFile)
Since:
1.1.0

save

protected boolean save()
Persists table model data into a picked file. Ensures a persistence file is chosen and passes the call to save(File).

See Also:
Persistence.save(javax.swing.JTable, java.io.File)

needsSave

protected int needsSave()
Determines if the data needs to be saved. Part of the determination can be prompting the user to see if they want to save the changes.


windowClosing

public void windowClosing(java.awt.event.WindowEvent ev)
Handles window closing events.

Specified by:
windowClosing in interface java.awt.event.WindowListener
Overrides:
windowClosing in class java.awt.event.WindowAdapter

saveOnClose

public void saveOnClose()
Determines if the data needs to be saved and saves it if it does.

See Also:
needsSave(), save()

closeAll

public static void closeAll()
Steps through all instances and calls saveOnClose() on each.


disposeWindow

protected void disposeWindow()
Convenience method for finding the window component containing the table and disposing of that window. This operation is necessary to ensure disposal of native window peer objects and related threads.

See Also:
saveOnClose()

disposed

protected static void disposed(PersistenceManager impexp)
Removes specified instance from non-disposed instance set. If the set reaches an empty state (no more non-disposed instances), System.exit(0) is called.

See Also:
opened(zinger.bsheet.PersistenceManager)

opened

protected static void opened(PersistenceManager impexp)
Adds specified instance to the non-disposed instance set.

See Also:
disposed(zinger.bsheet.PersistenceManager)

getFileChooser

protected javax.swing.JFileChooser getFileChooser()

addPersistence

public void addPersistence(Persistence persistence)

removePersistence

public void removePersistence(Persistence persistence)

addPersistenceListener

public void addPersistenceListener(PersistenceListener listener)

removePersistenceListener

public void removePersistenceListener(PersistenceListener listener)

fireEvent

protected void fireEvent(PersistenceEvent ev)