public class DocumentExample extends SingleFrameApplication
The application's state is defined by two read-only bound properties:
file
#isModified
The application is launched
in the
main method on the "main" thread. All the work of actually
constructing, intializing
, and
starting
the application actually
happens on the EDT.
The resources for this Application are defined in resources/DocumentExample.properties
.
This application defines a small set of actions for opening
and saving files: open
, save
,
and saveAs
. It inherits
cut/copy/paste/delete
ProxyActions from the
Application
class. The ProxyActions perform their
action not on the component they're bound to (menu items and
toolbar buttons), but on the component that currently
has the keyboard focus. Their enabled state tracks the
selection value of the component with the keyboard focus,
as well as the contents of the system clipboard.
The action code that reads and writes files, runs asynchronously
on background threads. The open
, save
,
and saveAs
actions all return a Task object which
encapsulates the work that will be done on a background thread.
The showAboutBox
and
closeAboutBox
actions do their work
synchronously.
Warning: this application is intended as a simple example, not as a robust text editor. Read it, don't use it.
SingleFrameApplication
,
ResourceMap
,
Action
,
Task
Application.ExitListener
Constructor and Description |
---|
DocumentExample() |
Modifier and Type | Method and Description |
---|---|
void |
closeAboutBox()
Close the about box dialog.
|
java.io.File |
getFile()
The File currently being edited.
|
protected void |
initialize(java.lang.String[] args)
Responsible for initializations that must occur before the
GUI is constructed by
startup . |
boolean |
isModified()
True if the file value has been modified but not saved.
|
static void |
main(java.lang.String[] args)
Launch the application on the EDT.
|
Task |
open()
Prompt the user for a filename and then attempt to load the file.
|
protected void |
ready()
Called after the startup() method has returned and there
are no more events on the
system event queue . |
Task |
save()
Save the contents of the textArea to the current
file . |
Task |
saveAs()
Save the contents of the textArea to the current file.
|
void |
showAboutBox()
Show the about box dialog.
|
protected void |
startup()
Responsible for starting the application; for creating and showing
the initial GUI.
|
configureWindow, getMainFrame, getMainView, setMainFrame, show, show, show, show, shutdown
addExitListener, end, exit, exit, getContext, getExitListeners, getInstance, getInstance, hide, launch, quit, removeExitListener
addPropertyChangeListener, addPropertyChangeListener, firePropertyChange, firePropertyChange, getPropertyChangeListeners, removePropertyChangeListener, removePropertyChangeListener
public java.io.File getFile()
This is a bound read-only property. It is never null.
isModified()
public boolean isModified()
This is a bound read-only property.
isModified()
@Action public Task open()
The file is loaded on a worker thread because we don't want to block the EDT while the file system is accessed. To do that, this Action method returns a new LoadFileTask instance, if the user confirms selection of a file. The task is executed when the "open" Action's actionPerformed method runs. The LoadFileTask is responsible for updating the GUI after it has successfully completed loading the file.
@Action(enabledProperty="modified") public Task save()
file
.
The text is written to the file on a worker thread because we don't want to block the EDT while the file system is accessed. To do that, this Action method returns a new SaveFileTask instance. The task is executed when the "save" Action's actionPerformed method runs. The SaveFileTask is responsible for updating the GUI after it has successfully completed saving the file.
getFile()
@Action public Task saveAs()
This action is nearly identical to open
. In
this case, if the user chooses a file, a SaveFileTask
is returned. Note that the selected file only becomes the
value of the file
property if the file is saved
successfully.
@Action public void showAboutBox()
@Action public void closeAboutBox()
protected void initialize(java.lang.String[] args)
Application
startup
.
This method is called by the static launch
method,
before startup
is called. Subclasses that want
to do any initialization work before startup
must
override it. The initialize
method
runs on the event dispatching thread.
By default initialize() does nothing.
initialize
in class Application
args
- the main method's arguments.Application.launch(java.lang.Class<T>, java.lang.String[])
,
Application.startup()
,
Application.shutdown()
protected void startup()
Application
This method is called by the static launch
method,
subclasses must override it. It runs on the event dispatching
thread.
startup
in class Application
Application.launch(java.lang.Class<T>, java.lang.String[])
,
Application.initialize(java.lang.String[])
,
Application.shutdown()
protected void ready()
Application
system event queue
.
When this method is called, the application's GUI is ready
to use.
It's usually important for an application to start up as quickly as possible. Applications can override this method to do some additional start up work, after the GUI is up and ready to use.
ready
in class Application
Application.launch(java.lang.Class<T>, java.lang.String[])
,
Application.startup()
,
Application.shutdown()
public static void main(java.lang.String[] args)