jrworkspace by
jTEM

de.jtem.jrworkspace.plugin.simplecontroller
Class SimpleController

java.lang.Object
  extended by de.jtem.jrworkspace.plugin.simplecontroller.SimpleController
All Implemented Interfaces:
Controller

public class SimpleController
extends Object
implements Controller

A simple implementation of the Controller interface. It creates a JFrame if a plug-in with PerspectiveFlavor is registered. First call registerPlugin(Plugin) to insert a plug-in. Then call startup() to initialize the application.

Plugin properties

The SimpleController allows Plugins to read and save properties. These properties are read from and saved to a file at startup and shutdown via XStream.

When shutdown() is called (from the main windows closing method or from a plugin that implements ShutdownFlavor) the user gets the chance to decide where the properties are saved. The decisions are saved via the java preferences API (see setPropertiesResource(Class, String). If nothing else is specified the SimpleController tries to read the plugin properties from

 
                System.getProperty("user.home") + "/.jrworkspace/default_simple.xml"
 
and saves the user decisions in the preferences node of the package of the SimpleController.

Besides the name (and path) of the properties file the user may choose

The user may cancel the dialog, which also cancels the shutdown process.

It is recommended that applications call

                setPropertiesResource(MyClass.class,"propertiesFileName")
 
before calling startup(). Then the controller loads the plugin properties from this resource. After deployment this resource may most likely only be opened for reading, which has the effect that it will only be used to call setPropertiesInputStream(InputStream) and the properties file will retain its default value or whatever it is set to via setPropertiesFile(File).

When loading properties the availability of a properties file is checked in the following order

  1. the user properties file (from the java preferences), when loadFromUserPropertyFile is true
  2. the propertiesInputStream
  3. the propertiesFile
When saving properties the availability of a properties file for output is checked in the following order
  1. the user properties file (from the java preferences)
  2. the propertiesFile
The user is prompted when askBeforeSaveOnExit is true or both files above can't be opened for writing.

Note to Eclipse developers: if you change the path of the file to save the properties into in the dialog at shutdown to point to the source folder and DISABLE the load from this file check box, then the resource will be accessed to load the properties (and the situation after deployment is always tested) and the source folder file is used to save (which then may be included in version control). In order to trigger copying of the source folder file to the bin folder one may add a do nothing builder which has "Refresh resources upon completion" enabled and make sure that the "Filtered resources" do not filter this file.

Author:
Stefan Sechelmann, Paul Peters
See Also:
Plugin

Nested Class Summary
protected  class SimpleController.FlavorListener
          The methods in this class are intended to be invoked when the respective controller is started.
protected  class SimpleController.MenuFlavorComparator
           
static class SimpleController.Status
           
protected  class SimpleController.ToolBarFlavorComparator
           
 
Field Summary
protected  AboutDialog aboutDialog
           
protected  JPanel centerPanel
           
protected static boolean DEFAULT_ASK_BEFORE_SAVE_ON_EXIT
           
protected static boolean DEFAULT_LOAD_FROM_USER_PROPERTY_FILE
           
protected static boolean DEFAULT_SAVE_ON_EXIT
           
protected static String DEFAULT_USER_PROPERTY_FILE
           
protected  Stack<Plugin> dependencyStack
           
protected  SimpleController.FlavorListener flavorListener
           
protected  Frame fullScreenFrame
           
protected  boolean hasHelpMenu
           
protected  boolean hasMainWindow
           
protected  boolean hasMenuBar
           
protected  boolean hasPreferencesMenu
           
protected  boolean hasStatusBar
           
protected  boolean hasToolBar
           
protected  HelpWindow helpWindow
           
protected  HelpWindowAction helpWindowAction
           
protected  Set<Class<? extends Plugin>> installed
           
protected  boolean localStartup
           
protected  JFrame mainWindow
           
protected  boolean manageLookAndFeel
           
protected  PerspectiveFlavor perspective
           
protected  Set<Plugin> plugins
           
protected  PreferencesWindow preferencesWindow
           
protected  HashMap<String,Object> properties
           
protected  com.thoughtworks.xstream.XStream propertyxStream
           
protected  File propFile
           
protected  InputStream propInputStream
           
protected  SimpleController.Status status
           
protected  JLabel statusLabel
           
protected  JPanel toolBarPanel
           
protected  Preferences userPreferences
           
 
Constructor Summary
SimpleController()
          Construct a SimpleController.
 
Method Summary
protected  void activatePlugin(Plugin p)
           
<T> T
deleteProperty(Class<?> context, String key)
          Deletes a property from this controller
<T extends Plugin>
T
getPlugin(Class<T> clazz)
          Returns an instance of the plug-in class clazz, if there is a plug-in of this class available.
<T> List<T>
getPlugins(Class<T> clazz)
          Returns all plug-ins for which the following expression evaluates to true pClass.isAssignableFrom(plug-in)
<T> T
getProperty(Class<?> context, String key, T defaultValue)
          Retrieves a property from this Controller.
 String getUserPropertyFile()
           
protected  void initializeComponents()
           
 boolean isActive(Plugin p)
          Returns true if the controller believes this plug-in is active
 boolean isAskBeforeSaveOnExit()
           
 boolean isLoadFromUserPropertyFile()
           
 boolean isSaveOnExit()
           
protected  void loadProperties()
           
protected  void readUserPreferences()
           
 void registerPlugin(Plugin p)
          Registers a plug-in with this SimpleController
protected  boolean savePropertiesOnExit()
           
 void setAskBeforeSaveOnExit(boolean askBeforeSaveOnExit)
           
 void setFullscreen(boolean fs)
          If there is a main window it's full-screen mode can be changed with this method
 void setLoadFromUserPropertyFile(boolean loadFromUserPropertyFile)
           
 void setManageLookAndFeel(boolean manageLookAndFeel)
          The SimpleController manages the swing look and feel if this flag is set
 void setPropertiesFile(File propertiesFile)
          Sets the properties File of this SimpleController.
 void setPropertiesInputStream(InputStream in)
          Sets the properties InputStream of this SimpleController.
 void setPropertiesResource(Class<?> clazz, String propertiesFileName)
          The provided resource serves 2 purposes: to set the properties File and InputStream via setPropertiesFile(File) (if this resource allows write access) and setPropertiesInputStream(InputStream) (if this resource allows read access), to save and read user decisions about the reading and loading of the property file in a package specific node, via the Java Preferences API.
 void setSaveOnExit(boolean saveOnExit)
           
 void setShowMenuBar(boolean show)
          Hides or reveals this controllers menu bar
 void setShowPreferencesWindow(boolean show)
          Sets the visibility state of the preferences windows if there is one
 void setShowStatusBar(boolean show)
          Hides or reveals this controllers status bar
 void setShowToolBar(boolean show)
          Hides or reveals this controllers tool bar
 void setUserPropertyFile(String userPropertyFile)
          Overwrite or initialize the file chosen by the user for reading and writing of properties.
 void shutdown()
          Call this method to save the properties and exit the application.
 void startup()
          Installs all registered plug-ins, reads user preferences, restores plugin properties, and opens the main window if there is a plug-in implementing PerspectiveFlavor
 JRootPane startupLocal()
          Starts this SimpleController but does not open the main window instead it returns the content panel of this window that can be used outside of the controller.
 Object storeProperty(Class<?> context, String key, Object property)
          Stores the property with the given key and context class
protected  void updateMainWindow()
           
protected  void updateMenuBarInternal()
           
protected  void updateToolBar()
           
protected  void writeUserPreferences()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

aboutDialog

protected AboutDialog aboutDialog

centerPanel

protected JPanel centerPanel

DEFAULT_ASK_BEFORE_SAVE_ON_EXIT

protected static boolean DEFAULT_ASK_BEFORE_SAVE_ON_EXIT

DEFAULT_LOAD_FROM_USER_PROPERTY_FILE

protected static boolean DEFAULT_LOAD_FROM_USER_PROPERTY_FILE

DEFAULT_SAVE_ON_EXIT

protected static boolean DEFAULT_SAVE_ON_EXIT

DEFAULT_USER_PROPERTY_FILE

protected static String DEFAULT_USER_PROPERTY_FILE

dependencyStack

protected Stack<Plugin> dependencyStack

flavorListener

protected SimpleController.FlavorListener flavorListener

fullScreenFrame

protected Frame fullScreenFrame

hasHelpMenu

protected boolean hasHelpMenu

hasMainWindow

protected boolean hasMainWindow

hasMenuBar

protected boolean hasMenuBar

hasPreferencesMenu

protected boolean hasPreferencesMenu

hasStatusBar

protected boolean hasStatusBar

hasToolBar

protected boolean hasToolBar

helpWindow

protected HelpWindow helpWindow

helpWindowAction

protected HelpWindowAction helpWindowAction

installed

protected Set<Class<? extends Plugin>> installed

localStartup

protected boolean localStartup

mainWindow

protected JFrame mainWindow

manageLookAndFeel

protected boolean manageLookAndFeel

perspective

protected PerspectiveFlavor perspective

plugins

protected Set<Plugin> plugins

preferencesWindow

protected PreferencesWindow preferencesWindow

properties

protected HashMap<String,Object> properties

propertyxStream

protected com.thoughtworks.xstream.XStream propertyxStream

propFile

protected File propFile

propInputStream

protected InputStream propInputStream

status

protected SimpleController.Status status

statusLabel

protected JLabel statusLabel

toolBarPanel

protected JPanel toolBarPanel

userPreferences

protected Preferences userPreferences
Constructor Detail

SimpleController

public SimpleController()
Construct a SimpleController. Initialize the properties file to System.getProperty("user.home") + "/.jrworkspace/default_simple.xml". This will be written on shutdown().

Method Detail

activatePlugin

protected void activatePlugin(Plugin p)

deleteProperty

public <T> T deleteProperty(Class<?> context,
                            String key)
Description copied from interface: Controller
Deletes a property from this controller

Specified by:
deleteProperty in interface Controller
Type Parameters:
T - The property type
Parameters:
context - A context
key - The key name of the property to delete
Returns:
The deleted value or null if the key was not found

getPlugin

public <T extends Plugin> T getPlugin(Class<T> clazz)
Description copied from interface: Controller
Returns an instance of the plug-in class clazz, if there is a plug-in of this class available. If the result is available but not yet installed it will be installed.

Specified by:
getPlugin in interface Controller
Type Parameters:
T - the class type of the plug-in to get
Parameters:
clazz - the class
Returns:
a plug-in or null if there is no plug-in of type class

getPlugins

public <T> List<T> getPlugins(Class<T> clazz)
Description copied from interface: Controller
Returns all plug-ins for which the following expression evaluates to true pClass.isAssignableFrom(plug-in)

Specified by:
getPlugins in interface Controller
Type Parameters:
T - the plug-in class type
Parameters:
clazz - the class of type T
Returns:
A list of plug-ins

getProperty

public <T> T getProperty(Class<?> context,
                         String key,
                         T defaultValue)
Description copied from interface: Controller
Retrieves a property from this Controller.

Specified by:
getProperty in interface Controller
Type Parameters:
T - The property type
Parameters:
context - A context
key - the key name of the property to retrieve
defaultValue - a default value, which is returned if the property was not saved before
Returns:
the stored property or defaultValue

getUserPropertyFile

public String getUserPropertyFile()

initializeComponents

protected void initializeComponents()

isActive

public boolean isActive(Plugin p)
Description copied from interface: Controller
Returns true if the controller believes this plug-in is active

Specified by:
isActive in interface Controller
Parameters:
p - the plug-in
Returns:
active

isAskBeforeSaveOnExit

public boolean isAskBeforeSaveOnExit()

isLoadFromUserPropertyFile

public boolean isLoadFromUserPropertyFile()

isSaveOnExit

public boolean isSaveOnExit()

loadProperties

protected void loadProperties()

readUserPreferences

protected void readUserPreferences()

registerPlugin

public void registerPlugin(Plugin p)
Registers a plug-in with this SimpleController

Parameters:
p - the plug-in to register

savePropertiesOnExit

protected boolean savePropertiesOnExit()
Returns:
false when the shutdown was canceled by the user

setAskBeforeSaveOnExit

public void setAskBeforeSaveOnExit(boolean askBeforeSaveOnExit)

setFullscreen

public void setFullscreen(boolean fs)
If there is a main window it's full-screen mode can be changed with this method

Parameters:
fs -

setLoadFromUserPropertyFile

public void setLoadFromUserPropertyFile(boolean loadFromUserPropertyFile)

setManageLookAndFeel

public void setManageLookAndFeel(boolean manageLookAndFeel)
The SimpleController manages the swing look and feel if this flag is set

Parameters:
manageLookAndFeel -

setPropertiesFile

public void setPropertiesFile(File propertiesFile)
Sets the properties File of this SimpleController. This does not overwrite a file chosen by the the user and persisted as user properties.

Parameters:
propertiesFile - a file or null
See Also:
setPropertiesResource(Class, String)

setPropertiesInputStream

public void setPropertiesInputStream(InputStream in)
Sets the properties InputStream of this SimpleController. If also a properties File is provided the InputStream is used for reading the properties.

Parameters:
in - an InputStream or null
See Also:
setPropertiesResource(Class, String)

setPropertiesResource

public void setPropertiesResource(Class<?> clazz,
                                  String propertiesFileName)
The provided resource serves 2 purposes:
  1. to set the properties File and InputStream via setPropertiesFile(File) (if this resource allows write access) and setPropertiesInputStream(InputStream) (if this resource allows read access),
  2. to save and read user decisions about the reading and loading of the property file in a package specific node, via the Java Preferences API.

Parameters:
clazz - the class from which the resource may be obtained, the the properties node of package of this class is used to save the user decisions. Can be null, then the user preferences are disabled and only static property files are used.
propertiesFileName - name of the resource that contains the plug-in properties. This argument may be null, then only the second purpose is served and the properties File and InputStream are NOT set to null and may be set independently.

setSaveOnExit

public void setSaveOnExit(boolean saveOnExit)

setShowMenuBar

public void setShowMenuBar(boolean show)
Hides or reveals this controllers menu bar

Parameters:
show - a flag

setShowPreferencesWindow

public void setShowPreferencesWindow(boolean show)
Sets the visibility state of the preferences windows if there is one

Parameters:
show -

setShowStatusBar

public void setShowStatusBar(boolean show)
Hides or reveals this controllers status bar

Parameters:
show - a flag

setShowToolBar

public void setShowToolBar(boolean show)
Hides or reveals this controllers tool bar

Parameters:
show - a flag

setUserPropertyFile

public void setUserPropertyFile(String userPropertyFile)
Overwrite or initialize the file chosen by the user for reading and writing of properties.

Parameters:
userPropertyFile -

shutdown

public void shutdown()
Call this method to save the properties and exit the application. This is done in a newly created thread to allow user interaction during saving. The user is allowed to cancel that process.

See Also:
setPropertiesResource(Class, String)

startup

public void startup()
Installs all registered plug-ins, reads user preferences, restores plugin properties, and opens the main window if there is a plug-in implementing PerspectiveFlavor


startupLocal

public JRootPane startupLocal()
Starts this SimpleController but does not open the main window instead it returns the content panel of this window that can be used outside of the controller.

Returns:
The content pane of the main window if there is one an empty Container else

storeProperty

public Object storeProperty(Class<?> context,
                            String key,
                            Object property)
Description copied from interface: Controller
Stores the property with the given key and context class

Specified by:
storeProperty in interface Controller
Parameters:
context - the context class e.g. the plug-in class which stores the value
key - a key string
property - the property to save
Returns:
The old value of this property

updateMainWindow

protected void updateMainWindow()

updateMenuBarInternal

protected void updateMenuBarInternal()

updateToolBar

protected void updateToolBar()

writeUserPreferences

protected void writeUserPreferences()

jrworkspace by
jTEM

jTEM