Class DocumentExample
- java.lang.Object
-
public class DocumentExample extends SingleFrameApplication
This is a very simple example of a SingleFrameApplication that loads and saves a single text document. Although it does not possess all of the usual trappings of a single-document app, like versioning or support for undo/redo, it does serve to highlight how to use actions, resources, and tasks.The application's state is defined by two read-only bound properties:
- File
file - The current text File being edited.
- boolean
#isModified - True if the current file needs to be saved.
The application is
launchedin the main method on the "main" thread. All the work of actually constructing,intializing, andstartingthe 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, andsaveAs. It inheritscut/copy/paste/deleteProxyActions from theApplicationclass. 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, andsaveAsactions all return a Task object which encapsulates the work that will be done on a background thread. TheshowAboutBoxandcloseAboutBoxactions 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.
- See Also:
SingleFrameApplication,ResourceMap,Action,Task
- File
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class org.jdesktop.application.Application
Application.ExitListener
-
-
Constructor Summary
Constructors Constructor Description DocumentExample()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidcloseAboutBox()Close the about box dialog.java.io.FilegetFile()The File currently being edited.protected voidinitialize(java.lang.String[] args)Responsible for initializations that must occur before the GUI is constructed bystartup.booleanisModified()True if the file value has been modified but not saved.static voidmain(java.lang.String[] args)Launch the application on the EDT.Taskopen()Prompt the user for a filename and then attempt to load the file.protected voidready()Called after the startup() method has returned and there are no more events on thesystem event queue.Tasksave()Save the contents of the textArea to the currentfile.TasksaveAs()Save the contents of the textArea to the current file.voidshowAboutBox()Show the about box dialog.protected voidstartup()Responsible for starting the application; for creating and showing the initial GUI.-
Methods inherited from class org.jdesktop.application.SingleFrameApplication
configureWindow, getMainFrame, getMainView, setMainFrame, show, show, show, show, shutdown
-
Methods inherited from class org.jdesktop.application.Application
addExitListener, end, exit, exit, getContext, getExitListeners, getInstance, getInstance, hide, launch, quit, removeExitListener
-
Methods inherited from class org.jdesktop.application.AbstractBean
addPropertyChangeListener, addPropertyChangeListener, firePropertyChange, firePropertyChange, getPropertyChangeListeners, removePropertyChangeListener, removePropertyChangeListener
-
-
-
-
Method Detail
-
getFile
public java.io.File getFile()
The File currently being edited. The default value of this property is "untitled.txt".This is a bound read-only property. It is never null.
- Returns:
- the value of the file property.
- See Also:
isModified()
-
isModified
public boolean isModified()
True if the file value has been modified but not saved. The default value of this property is false.This is a bound read-only property.
- Returns:
- the value of the modified property.
- See Also:
isModified()
-
open
@Action public Task open()
Prompt the user for a filename and then attempt to load the file.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.
- Returns:
- a new LoadFileTask or null
-
save
@Action(enabledProperty="modified") public Task save()
Save the contents of the textArea to the currentfile.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.
- See Also:
getFile()
-
saveAs
@Action public Task saveAs()
Save the contents of the textArea to the current file.This action is nearly identical to
open. In this case, if the user chooses a file, aSaveFileTaskis returned. Note that the selected file only becomes the value of thefileproperty if the file is saved successfully.
-
showAboutBox
@Action public void showAboutBox()
Show the about box dialog.
-
closeAboutBox
@Action public void closeAboutBox()
Close the about box dialog.
-
initialize
protected void initialize(java.lang.String[] args)
Description copied from class:ApplicationResponsible for initializations that must occur before the GUI is constructed bystartup.This method is called by the static
launchmethod, beforestartupis called. Subclasses that want to do any initialization work beforestartupmust override it. Theinitializemethod runs on the event dispatching thread.By default initialize() does nothing.
- Overrides:
initializein classApplication- Parameters:
args- the main method's arguments.- See Also:
Application.launch(java.lang.Class<T>, java.lang.String[]),Application.startup(),Application.shutdown()
-
startup
protected void startup()
Description copied from class:ApplicationResponsible for starting the application; for creating and showing the initial GUI.This method is called by the static
launchmethod, subclasses must override it. It runs on the event dispatching thread.- Specified by:
startupin classApplication- See Also:
Application.launch(java.lang.Class<T>, java.lang.String[]),Application.initialize(java.lang.String[]),Application.shutdown()
-
ready
protected void ready()
Description copied from class:ApplicationCalled after the startup() method has returned and there are no more events on thesystem 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.
- Overrides:
readyin classApplication- See Also:
Application.launch(java.lang.Class<T>, java.lang.String[]),Application.startup(),Application.shutdown()
-
main
public static void main(java.lang.String[] args)
Launch the application on the EDT.
-
-