|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.ObjectjApp.Application
public class Application
This class together with Document
class implements the basic
behaviour of a document based desktop application.
Constructor Summary | |
---|---|
protected |
Application(java.lang.String[] args)
Constructs the application object with a given title. |
Method Summary | |
---|---|
void |
addItemsToRightClick(javax.swing.JPopupMenu popupMenu)
Handles right clicks, the top of the chain of command for right clicks. |
Variable |
addVariable(java.lang.String name,
Variable var)
Adds an Appication Variable . |
static void |
centerWindow(java.awt.Window window)
Centers a window on the screen so that it is horizontally in the center, vertically one third from the top. |
protected void |
checkDesktopIntegration()
Checks and performs the desktop integration (file associations and icon installation) if this is the first launch of the application. |
protected boolean |
confirmIntegration()
Shows the Desktop Integration confirmation dialog. |
boolean |
confirmReplaceFile(java.awt.Component component,
java.io.File file)
Short cut to confirmReplaceFile(Component, File, boolean) . |
int |
confirmReplaceFile(java.awt.Component component,
java.io.File file,
boolean canCancel)
Obtains confirmation from user for overwriting a file. |
javax.swing.Action |
createAboutMenuAction()
Document calls this to create the 'About Appplication...' -action. |
javax.swing.Action |
createPreferencesMenuAction()
Document calls this to create the 'Preferences...' -action. |
java.awt.Point |
ensureWindowOnScreen(java.awt.Point location)
Ensures that a window is at least minimally on the screen so that the user can move it. |
void |
exitApplication()
Tries to close all documents and exit the application with System.exit(0). |
javax.swing.Action |
getAboutMenuAction()
Returns the 'About..." menu action that shows the 'About Application' dialog. |
Document |
getActiveDocument()
Gets the active document. |
java.awt.Frame |
getActiveWindow()
Gets the active window for use as parent frame for Application dialogs |
java.lang.String |
getApplicationConfigFileName()
Gets the application config file name. |
java.lang.String |
getApplicationDirName()
Gets the full path name for the application home directory. |
java.lang.String |
getApplicationFileNameBase()
Gets the base file name for application files. |
java.lang.String |
getApplicationID()
Gets the internal application for use as Windows Registry key etc. |
java.util.ResourceBundle |
getBundle()
Gets the application resource bundle. |
static java.lang.Object |
getClipboard()
Deprecated. |
java.lang.String |
getCopyright()
Get the application copyright text from the application properties bundle. |
java.lang.String |
getDescription()
Gets the application end user description. |
java.lang.Iterable<Document> |
getDocuments()
Gets a an Iterable to iterate over all open documents |
javax.swing.JFileChooser |
getFileChooser()
Gets a JFileChooser object. |
java.io.File |
getFileForReading(java.awt.Frame window,
java.lang.String defaultname,
java.lang.String desciption,
java.lang.String[] extensions)
Shows a file chooser dialog to let the user select, for reading from, an existing file with the given extension type and returns a File object to the selected file. |
java.io.File |
getFileForWriting(java.awt.Frame window,
java.lang.String defaultname,
java.lang.String extension)
Shows a file chooser dialog to let the user select a file for writing to, new or existing,with the given extension type and returns a File object to the selected file. |
FileTypeRegistry |
getFileTypeRegistry()
Gets the FileTypeRegistry for this this application. |
java.lang.Iterable<FileType> |
getFileTypes(Document handler)
Gets an Iterable over the file FileType s that the given
Document can handle. |
java.lang.String |
getFullName()
Gets the application end user full name. |
static java.lang.String |
getjAppVersion()
Gets the jApp version string, which has the format major.minor.bugfix.development . |
java.io.File |
getLatesDirectory()
Gets the latest directory user browsed with file chooser. |
static java.io.FileInputStream |
getlockedInputStream(java.io.File file)
Tries to open a file as input stream in an exclusive but shared access mode. |
static java.io.FileOutputStream |
getLockedOutputStream(java.io.File file,
boolean append)
Tries to open a file as output stream in an exclusive access mode. |
static int |
getMenuShortcutKeyMask()
A convenience method to get the menu short cut key mask, CTRL on windows, Comman/Apple on MacOS |
protected javax.swing.Action |
getNewAction()
Gets the 'New...' document action that creates a new document of default type. |
static javax.swing.KeyStroke |
getNewShortcut()
Gets the shortcut keystroke for the 'New...' action |
static javax.swing.ImageIcon |
getNullIcon()
Gets a null icon to be used in menus when no icon is needed. |
protected javax.swing.Action |
getOpenAction()
Gets the 'Open...' document that displays the file dialog and tries to open the selected document. |
static javax.swing.KeyStroke |
getOpenShortcut()
Gets the shortcut keystroke for the 'Open...' action |
javax.swing.Action |
getPreferencesMenuAction()
Returns the 'Preferences..." menu action that shows the 'Preferences' dialog. |
java.lang.String |
getResouceString(java.lang.String key,
java.lang.String defaultValue)
Gets a String resouce from the Application resource
bundle. |
java.lang.String |
getShortName()
Gets the application end user short name. |
java.lang.String |
getTemplateDirName()
Gets the full path name of the directory for document templates. |
java.lang.String |
getVersion()
Get the application version number from the application properties bundle. |
protected void |
initApplication()
Initializes the application object. |
protected void |
initialized()
Called when the application has been fully initialized. |
protected void |
initLinux()
Does Linux specific initialisation. |
protected void |
initMacOS()
Does MacOS specific initialisation. |
protected void |
initWindows()
Does Microsoft Windows specific initialisation. |
static boolean |
inLinux()
Returns true if running under Linux |
static boolean |
inMacOS()
Returns true if running under MacOS |
void |
install(boolean install)
Installs/uninstalls the application into/from the system. |
void |
installTemplate(java.lang.String name,
java.io.InputStream src)
Copies an input stream to the template directory under given file name. |
protected void |
installTemplates()
Installs document templates. |
static boolean |
inWindows()
Returns true if running under Windows |
static boolean |
isAqua()
Returns true if running with Aqua Look and Feel under MacOS X. |
static void |
maximizeWindow(java.awt.Window window,
java.awt.Insets insets)
Maxizes a window on screen. |
static javax.swing.JMenu |
newMenu(java.lang.String title)
This just a short cut for new JMenu(title). |
static javax.swing.JMenuItem |
newMenuItem(javax.swing.JMenu menu,
java.lang.String title,
javax.swing.Action action)
Creates a JMenuItem with a given title for an action. |
static javax.swing.JMenuItem |
newMenuItem(javax.swing.JMenu menu,
java.lang.String title,
java.awt.event.ActionListener that)
Creates a JMenuItem with the given title and 'null icon'. |
protected void |
openDocuments()
Opens the documents passed to the application on the command line or an empty document of default type if no other documents are open at once the application is up and running. |
Document |
openNewDocument(java.lang.String type)
Tries to create and open a new (empty) document of the given type. |
Document |
openOldDocument(java.lang.String file,
boolean isTemplate)
Given a filename tries to create and load an old (existing) document from that file. |
static void |
releaseInputStream(java.io.FileInputStream stream)
Releases the lock and closes the stream. |
static void |
releaseOutputStream(java.io.FileOutputStream stream)
Closes a stream and releases the lock. |
void |
reportException(java.lang.Exception e)
Used to report an exception by showing an error message dialog and stack trace to the console. |
void |
run()
Starts the Application createing and initializing everything as needed. |
void |
saveLatesDirectory(java.io.File dir)
Saves the current browsing directory for the file chooser. |
static void |
setClipboard(java.lang.Object cbclipboard)
Deprecated. |
protected void |
setLookAndFeel()
Sets the look and feel for the Application. |
int |
showConfirmDialog(java.awt.Component component,
java.lang.Object message,
java.lang.String title,
int type)
Calls JOptionPane.showConfirmDialog(Component, Object, String, int) . |
void |
showErrorMessage(java.lang.String message,
java.lang.String title)
Shortcut to showMessageDialog(Component, Object, String, int)
with null parent component and message type set to
JOptionPane.ERROR_MESSAGE . |
void |
showInfoMessage(java.lang.String message,
java.lang.String title)
Shortcut to showMessageDialog(Component, Object, String, int)
with null parent component and message type set to
JOptionPane.INFORMATION_MESSAGE . |
void |
showMessageDialog(java.awt.Component component,
java.lang.Object message,
java.lang.String title,
int type)
Calls JOptionPane.showMessageDialog(Component, Object, String, int) . |
void |
showWarningMessage(java.lang.String message,
java.lang.String title)
Shortcut to showMessageDialog(Component, Object, String, int)
with null parent component and message type set to
JOptionPane.WARNING_MESSAGE . |
Document |
tryOpenNewDocument()
Tries to open a new (empty) document of default type. |
Document |
tryOpenOldDocument()
Tries to open an existing document by letting the user choose a file. |
Document |
tryOpenOldDocument(java.lang.String file,
boolean isTemplate)
|
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
protected Application(java.lang.String[] args)
Application
constructor attempts to load a resource bundle from
For example if your main class is in package mypackage and your main
class is called MyMainClass this will attempt to load the bundle from
ResourceBundle.getBundle(getClass().getCanonicalName());
file mypackage/MyMainClass.properties
.
The Application attempts to read the following properties. These are not mandatory but should be set for any non trivial application. Especially application ID is important as it is used for example in registry entries in Windows. Some care should be exercised in order to come up with a unique name.
jApp-copyright = a copyright text for the application, defaults to "Copyright (c) Undefined"
jApp-version = version number for the application, defaults to "Undefined"
jApp-application-ID = a short indetifier to be used when an internal name is needed, defaults to "MyApp"
jApp-applicagtion-description = an end user desription of the application, defaults to appliation ID
jApp-application-short-name = short application name (max 16 chars), defaults to appliation ID
jApp-application-full-name = full application name, defaults to appliation ID
jApp-max-resent-files = number of recent files to remeber/store, defaults to 10
The following are optional, you can 'manually' register file types from with in your
application code with calls to FileTypeRegistry.registerFileType(Class, String, String, String)
.
The advatage of putting the following definition to the properties file is that the build process
can access it. Note that for more than one file type the build scripts needs to be manually adjusted.
jApp-application-doctypes =number of document types the application handles
jApp-document-1-extension = file extension without the dot
jApp-document-1-mimetype = the mime type for the document
jApp-document-1-description = and end user description of the file/document type
jApp-document-1-class = full class name for the document handler
Note that these are stored as properties to make it possible to utilize them in the build (Ant) process.
To use access them from the build file use 'loadproperties' Ant task as which includes only properties from lines that include the string 'jApp-'. This reduces the risk of name conflict between properties in the bundle and those in the build file.
<loadproperties srcFile="./src/yourmainpackage/YourMainClass.properties">
<filterchain>
<linecontains>
<contains value="jApp-" />
</linecontains>
<filterchain>
</loadproperties>
args
- You should pass the args
argument from the Java
main class main
method to
Application
so that it gets access to command
line parameters.Method Detail |
---|
public static int getMenuShortcutKeyMask()
public void saveLatesDirectory(java.io.File dir)
dir
- the direcotory to savepublic java.io.File getLatesDirectory()
public static java.lang.String getjAppVersion()
public java.lang.String getResouceString(java.lang.String key, java.lang.String defaultValue) throws java.lang.IllegalArgumentException
Application
resource
bundle.
key
- the resource keydefaultValue
-
java.lang.IllegalArgumentException
- if the resource cannot be found and defaultValue is nullApplication
public java.lang.String getVersion()
'jApp-version'
. The version number is stored in the
properties file so that it is available to the build process (Ant based)
and can be used in application packaging.
getBundle()
,
Application(String[])
public java.lang.String getCopyright()
'jApp-copyright'
. The version number is stored in the
properties file so that it is available to the build process (Ant based)
and can be used in application packaging.
getVersion()
public java.util.ResourceBundle getBundle()
Application
public Document getActiveDocument()
public javax.swing.JFileChooser getFileChooser()
@Deprecated public static java.lang.Object getClipboard()
Document.getClipboard()
instead.
@Deprecated public static void setClipboard(java.lang.Object cbclipboard)
cbclipboard
- the new cliboard object to set as the clicpboardDocument.createClipboard()
public void exitApplication()
public static javax.swing.JMenuItem newMenuItem(javax.swing.JMenu menu, java.lang.String title, java.awt.event.ActionListener that)
If the menu parameter is not null adds the new menu item into that menu
menu
- the menu to add the new item totitle
- title of the menu itemthat
- an action listener
public static javax.swing.JMenuItem newMenuItem(javax.swing.JMenu menu, java.lang.String title, javax.swing.Action action)
action
parameter is non null the action for the newly
created menu item is set to action
. Note that this may
override/set the menu item title. This is a static convenience function.
menu
- title
- action
-
public static javax.swing.JMenu newMenu(java.lang.String title)
title
- the title for the menu.
protected void openDocuments()
protected void setLookAndFeel()
initGUI
. Can be overriden to implement
different behaviour.
protected void initMacOS()
protected void initLinux()
protected void initWindows()
protected boolean confirmIntegration()
protected void checkDesktopIntegration()
public void run()
s Schedules the Application invokation, initilization and event processing to take place in the event dispatched thread.
protected void initialized()
public java.awt.Frame getActiveWindow()
public java.awt.Point ensureWindowOnScreen(java.awt.Point location)
location
- the windows location on screen
IllegalComponentStateException
- if the window is not visiblepublic static void centerWindow(java.awt.Window window)
window
- the window to center.public static void maximizeWindow(java.awt.Window window, java.awt.Insets insets)
This is static utility function.
Maximes a window on screen leaving insets
amount of space
all aroung the window. If insets
is null all screen space
is utilized.
window
- the window to maximizeinsets
- the insets use in maximizingpublic void showMessageDialog(java.awt.Component component, java.lang.Object message, java.lang.String title, int type)
JOptionPane.showMessageDialog(Component, Object, String, int)
.
This routine exists to be overridden in case some other widget toolkit
than Swing is used. All methdo calls from within jApp framework that call
JOptionPane.showMessageDialog(Component, Object, String, int)
are routed through here.
component
- the parent component for the swing methodmessage
- the message string to the swing methodtitle
- the title string to the swing methodtype
- the message type parameter to the swing methodshowConfirmDialog(Component, Object, String, int)
,
JOptionPane
public int showConfirmDialog(java.awt.Component component, java.lang.Object message, java.lang.String title, int type)
JOptionPane.showConfirmDialog(Component, Object, String, int)
.
This routine exists to be overridden in case some other widget toolkit
than Swing is used. All methdo calls from within jApp framework that call
JOptionPane.showConfirmDialog(Component, Object, String, int)
are routed through here.
component
- the parent component for the swing methodmessage
- the message string to the swing methodtitle
- the title string to the swing methodtype
- the message type parameter to the swing methodshowMessageDialog(Component, Object, String, int)
,
JOptionPane
public void showInfoMessage(java.lang.String message, java.lang.String title)
showMessageDialog(Component, Object, String, int)
with null parent component and message type set to
JOptionPane.INFORMATION_MESSAGE
.
message
- the message to display to the usertitle
- the title for the dialogpublic void showErrorMessage(java.lang.String message, java.lang.String title)
showMessageDialog(Component, Object, String, int)
with null parent component and message type set to
JOptionPane.ERROR_MESSAGE
.
message
- the message to display to the usertitle
- the title for the dialogpublic void showWarningMessage(java.lang.String message, java.lang.String title)
showMessageDialog(Component, Object, String, int)
with null parent component and message type set to
JOptionPane.WARNING_MESSAGE
.
message
- the message to display to the usertitle
- the title for the dialogpublic void reportException(java.lang.Exception e)
e
- the Exception to reportpublic Document openNewDocument(java.lang.String type)
FileTypeRegistry
is consulted to find out the
class
of the object that is used to handle the specified
type of document. The handler must be a descandent of the class
Document
.
type
- String designating the file type, e.g. ".txt" or ".doc"FileTypeRegistry
public Document openOldDocument(java.lang.String file, boolean isTemplate) throws java.lang.Exception
FileTypeRegistry
is consulted to find out the
class
of the object that is used to handle the specified
type of document. The handler must be a descandent of the class
Document
.
file
- a full path name for the document fileisTemplate
- if true the Document is detached from the file and marked not
dirty after reading in the contents of the document from the
file
java.lang.Exception
FileTypeRegistry
public Document tryOpenOldDocument(java.lang.String file, boolean isTemplate)
public Document tryOpenNewDocument()
public Document tryOpenOldDocument()
public java.lang.Iterable<Document> getDocuments()
protected void initApplication()
public java.io.File getFileForReading(java.awt.Frame window, java.lang.String defaultname, java.lang.String desciption, java.lang.String[] extensions)
window
- the parent window for the file chooser dialogdefaultname
- the default file namedesciption
- a user description of the type of files such as 'Graphics
Files'extensions
- the actual file extensions that are acceptable (including the
'dot', e.g. '.txt')
public int confirmReplaceFile(java.awt.Component component, java.io.File file, boolean canCancel)
showConfirmDialog(Component, Object, String, int)
a confirmation
dialog to the user. The dialog is two way (yes/no) or three way
(yes/no/cancel) depending on the parameter canCancel
.
component
- the parent component from which to obtain the Frame for the
dialog. Can be null.file
- the file about to be overwrittencanCancel
- true if the user is allowed to cancel
JOptionPane
.YES_OPTION/NO_OPTION/CANCEL_OPTION
public boolean confirmReplaceFile(java.awt.Component component, java.io.File file)
confirmReplaceFile(Component, File, boolean)
. Call
confirmReplaceFile(Component, File, boolean)
with the last
parameter set to false so that user neads to either confirm or deny, no
cancel option is given.
component
- the parent component from which to obtain the Frame for the
dialog. Can be null.file
- the file about to be overwritten
public java.io.File getFileForWriting(java.awt.Frame window, java.lang.String defaultname, java.lang.String extension)
window
- the parent window for the file chooser dialogdefaultname
- the default file nameextension
- the file extension (including the 'dot', e.g. '.txt')
public java.lang.String getTemplateDirName()
templates
.
getApplicationDirName()
public java.lang.String getApplicationDirName()
public java.lang.String getApplicationConfigFileName()
public java.lang.String getApplicationFileNameBase()
getApplicationDirName()
public Variable addVariable(java.lang.String name, Variable var)
Variable
. Application variables are
loaded when the Application
object has been initialized in
initApplication
and saved just before exiting the
application. jApp client code can utilize Application Variables to store
persistently whatever they need. The only requirement is that the value
of the Variable
needs to be serializable.
Internally jApp uses variables for example to store the 'recent files'
list. The names of all variables used internally by jApp begin with
"JAPP_"
so it is best to avoid names like that in order to
avoid name clashes.
name
- for the variblevar
- the variable object
Variable
public void installTemplate(java.lang.String name, java.io.InputStream src)
name
- the file name (without path) to copy the input stream tosrc
- the source input streamprotected void installTemplates()
installTemplate(String, InputStream)
public static boolean inMacOS()
public static boolean inWindows()
public static boolean inLinux()
public static boolean isAqua()
public static javax.swing.KeyStroke getNewShortcut()
public static javax.swing.KeyStroke getOpenShortcut()
protected javax.swing.Action getNewAction()
protected javax.swing.Action getOpenAction()
public javax.swing.Action getAboutMenuAction()
public javax.swing.Action getPreferencesMenuAction()
public javax.swing.Action createAboutMenuAction()
Document
calls this to create the 'About Appplication...' -action.
Override to implement this functionality.
public javax.swing.Action createPreferencesMenuAction()
Document
calls this to create the 'Preferences...' -action.
Override to implement this functionality.
public java.lang.String getApplicationID()
public java.lang.String getShortName()
getFullName()
public java.lang.String getFullName()
getShortName()
public java.lang.String getDescription()
public FileTypeRegistry getFileTypeRegistry()
FileTypeRegistry
for this this application.
FileTypeRegistry
public java.lang.Iterable<FileType> getFileTypes(Document handler)
FileType
s that the given
Document
can handle.
handler
- the Document whose filetypes are requested
public void install(boolean install)
s Installation means associating the application with its file types with the Desktop Manager. Does not nothing in Mac OS as file associations are handled automatically by properly packaging the application on that platform.
install
- pass true for install false for un-installpublic static javax.swing.ImageIcon getNullIcon()
public static java.io.FileOutputStream getLockedOutputStream(java.io.File file, boolean append) throws java.io.IOException
FileChannel.tryLock(long, long, boolean)
this method guards against
access to the same file from the same process / thread.
file
- the file to open in output modeappend
- weather to open the in append mode or not
java.io.IOException
- if the stream or lock open fails with exceptionpublic static java.io.FileInputStream getlockedInputStream(java.io.File file) throws java.io.IOException
FileChannel.tryLock(long, long, boolean)
this method guards against
access to the same file from the same process / thread.
file
- the file to open in input mode
java.io.IOException
- if the stream or lock open fails with exceptionpublic static void releaseInputStream(java.io.FileInputStream stream) throws java.io.IOException
stream
- the locked stream to close and release
java.io.IOException
- if close operation failse
java.lang.IllegalArgumentException
- the stream has not been previously locked with
getlockedInputStream(File)
public static void releaseOutputStream(java.io.FileOutputStream stream) throws java.io.IOException
stream
- the locked stream to close and release
java.io.IOException
- if close operation failse
java.lang.IllegalArgumentException
- the stream has not been previously locked with
getLockedOutputStream(File,boolean)
public void addItemsToRightClick(javax.swing.JPopupMenu popupMenu)
popupMenu
- the popup menu to fill in
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |