|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--java.awt.Component | +--java.awt.Container | +--java.awt.Window | +--java.awt.Frame | +--javax.swing.JFrame | +--ptolemy.gui.Top
This is a top-level window with a menubar and an optional status bar. Derived classes should add components to the content pane using a line like:
getContentPane().add(component, BorderLayout.CENTER);Derived classes may wish to modify the menus. The File and Help menus are exposed as protected members. The File menu items in the _fileMenuItems protected array are, in order, Open File, Open URL, New, Save, SaveAs, Print, Close, and Exit. The Help menu items in the _helpMenuItems protected array are, in order, About and Help.
A derived class can use the insert() methods of JMenu to insert a menu item defined by an Action or a JMenuItem into a specified position in the menu. Derived classes can also insert separators using the insertSeparator() method of JMenu. In principle, derived classes can also remove menu items using the remove() methods of JMenu; however, we discourage this. A basic principle of user interface design is habituation, where there is considerable value in having menus that have consistent contents and layout throughout the application (Microsoft, for example, violates this principle with adaptive menus).
Instead of removing items from the menu, they can be disabled. For example, to disable the "Save" item in the File menu, do
_fileMenuItems[3].setEnabled(false);
Some menu items are provided, but are disabled by default. The "New" item, for example, can be enabled with
_fileMenuItems[2].setEnabled(true);A derived class that enables this menu item can populate the menu with submenu items. This particular entry in the _fileMenuItems[2] is a JMenu, not just a JMenuItem, so it can have menu items added to it.
A derived class can add an entirely new menu (many do that). However, at this time, the JMenuBar interface does not support putting a new menu into an arbitrary position. For this reason, derived classes should insert new menus into the menu bar only in the _addMenus() protected method. This ensures that the File menu is always the rightmost menu, and the Help menu is always the leftmost menu. The _addMenus() method is called when the window is first packed.
Field Summary | |
protected static java.io.File |
_directory
The most recent directory used in a file dialog. |
protected javax.swing.filechooser.FileFilter |
_fileFilter
The FileFilter that determines what files are displayed by the Open dialog and the Save As dialog The initial default is null, which causes no FileFilter to be applied, which results in all files being displayed. |
protected javax.swing.JMenu |
_fileMenu
File menu for this frame. |
protected javax.swing.JMenuItem[] |
_fileMenuItems
Items in the file menu. |
protected javax.swing.JMenu |
_helpMenu
Help menu for this frame. |
protected javax.swing.JMenuItem[] |
_helpMenuItems
Help menu items. |
protected javax.swing.JMenuBar |
_menubar
Menubar for this frame. |
protected StatusBar |
_statusBar
The status bar. |
Fields inherited from class javax.swing.JFrame |
accessibleContext, EXIT_ON_CLOSE, rootPane, rootPaneCheckingEnabled |
Fields inherited from class java.awt.Frame |
CROSSHAIR_CURSOR, DEFAULT_CURSOR, E_RESIZE_CURSOR, HAND_CURSOR, ICONIFIED, MAXIMIZED_BOTH, MAXIMIZED_HORIZ, MAXIMIZED_VERT, MOVE_CURSOR, N_RESIZE_CURSOR, NE_RESIZE_CURSOR, NORMAL, NW_RESIZE_CURSOR, S_RESIZE_CURSOR, SE_RESIZE_CURSOR, SW_RESIZE_CURSOR, TEXT_CURSOR, W_RESIZE_CURSOR, WAIT_CURSOR |
Fields inherited from class java.awt.Component |
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT |
Fields inherited from interface javax.swing.WindowConstants |
DISPOSE_ON_CLOSE, DO_NOTHING_ON_CLOSE, HIDE_ON_CLOSE |
Fields inherited from interface java.awt.image.ImageObserver |
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH |
Constructor Summary | |
Top()
Construct an empty top-level frame with the default status bar. |
|
Top(StatusBar statusBar)
Construct an empty top-level frame with the specified status bar. |
Method Summary | |
protected void |
_about()
Open a dialog with basic information about this window. |
protected void |
_addMenus()
Add menus to the menu bar. |
protected boolean |
_clear()
Clear the current contents. |
protected boolean |
_close()
Close the window. |
protected void |
_exit()
Exit the application after querying the user to save data. |
protected java.lang.String |
_getName()
Get the name of this object, which in this base class is either the name of the file that has been associated with this object, or the string "Unnamed" if none. |
protected void |
_help()
Display the same information given by _about(). |
protected void |
_open()
Open a file dialog to identify a file to be opened, and then call _read() to open the file. |
protected void |
_openURL()
Open a dialog to enter a URL, and then invoke _read() to open the URL. |
protected void |
_print()
Print the contents. |
protected abstract void |
_read(java.net.URL url)
Read the specified URL. |
protected boolean |
_save()
Save the model to the current file, if there is one, and otherwise invoke _saveAs(). |
protected boolean |
_saveAs()
Query the user for a filename and save the model to that file. |
protected abstract void |
_writeFile(java.io.File file)
Write the model to the specified file. |
void |
centerOnScreen()
Center the window on the screen. |
void |
close()
Close the window, prompting the user to save changes if there have been any. |
static void |
deferIfNecessary(java.lang.Runnable action)
If this method is called in the AWT event dispatch thread, then simply execute the specified action. |
boolean |
getCentering()
Return true if the window is set to be centered when pack() is called. |
boolean |
isModified()
Return true if the data associated with this window has been modified since it was first read or last saved. |
void |
pack()
Size this window to its preferred size and make it displayable, and override the base class to populate the menu bar if the menus have not already been populated. |
void |
report(java.lang.String message)
Report a message to the user by displaying it in a status bar, if there is one. |
void |
report(java.lang.String message,
java.lang.Throwable throwable)
Report a Throwable, which is usually an Exception but can also be an Error. |
void |
report(java.lang.Throwable throwable)
Report a Throwable, which is usually an Exception but can also be an Error. |
void |
setBackground(java.awt.Color background)
Set background color. |
void |
setCentering(boolean centering)
Specify whether or not to center the window on the screen when packing it. |
void |
setModified(boolean modified)
Record whether the data associated with this window has been modified since it was first read or last saved. |
void |
show()
Override the base class to deiconify the window, if necessary. |
Methods inherited from class javax.swing.JFrame |
addImpl, createRootPane, frameInit, getAccessibleContext, getContentPane, getDefaultCloseOperation, getGlassPane, getJMenuBar, getLayeredPane, getRootPane, isDefaultLookAndFeelDecorated, isRootPaneCheckingEnabled, paramString, processWindowEvent, remove, setContentPane, setDefaultCloseOperation, setDefaultLookAndFeelDecorated, setGlassPane, setJMenuBar, setLayeredPane, setLayout, setRootPane, setRootPaneCheckingEnabled, update |
Methods inherited from class java.awt.Frame |
addNotify, finalize, getCursorType, getExtendedState, getFrames, getIconImage, getMaximizedBounds, getMenuBar, getState, getTitle, isResizable, isUndecorated, remove, removeNotify, setCursor, setExtendedState, setIconImage, setMaximizedBounds, setMenuBar, setResizable, setState, setTitle, setUndecorated |
Methods inherited from class java.awt.Window |
addPropertyChangeListener, addPropertyChangeListener, addWindowFocusListener, addWindowListener, addWindowStateListener, applyResourceBundle, applyResourceBundle, createBufferStrategy, createBufferStrategy, dispose, getBufferStrategy, getFocusableWindowState, getFocusCycleRootAncestor, getFocusOwner, getFocusTraversalKeys, getGraphicsConfiguration, getInputContext, getListeners, getLocale, getMostRecentFocusOwner, getOwnedWindows, getOwner, getToolkit, getWarningString, getWindowFocusListeners, getWindowListeners, getWindowStateListeners, hide, isActive, isFocusableWindow, isFocusCycleRoot, isFocused, isShowing, postEvent, processEvent, processWindowFocusEvent, processWindowStateEvent, removeWindowFocusListener, removeWindowListener, removeWindowStateListener, setCursor, setFocusableWindowState, setFocusCycleRoot, setLocationRelativeTo, toBack, toFront |
Methods inherited from class java.awt.Container |
add, add, add, add, add, addContainerListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getAlignmentX, getAlignmentY, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getContainerListeners, getFocusTraversalPolicy, getInsets, getLayout, getMaximumSize, getMinimumSize, getPreferredSize, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paint, paintComponents, preferredSize, print, printComponents, processContainerEvent, remove, removeAll, removeContainerListener, setFocusTraversalKeys, setFocusTraversalPolicy, setFont, transferFocusBackward, transferFocusDownCycle, validate, validateTree |
Methods inherited from class java.awt.Component |
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, createVolatileImage, createVolatileImage, disable, disableEvents, dispatchEvent, enable, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphics, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocation, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isOpaque, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, prepareImage, prepareImage, printAll, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, processMouseWheelEvent, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, reshape, resize, resize, setBounds, setBounds, setComponentOrientation, setDropTarget, setEnabled, setFocusable, setFocusTraversalKeysEnabled, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, setVisible, show, size, toString, transferFocus, transferFocusUpCycle |
Methods inherited from class java.lang.Object |
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Methods inherited from interface java.awt.MenuContainer |
getFont, postEvent |
Field Detail |
protected static java.io.File _directory
protected javax.swing.filechooser.FileFilter _fileFilter
protected javax.swing.JMenu _fileMenu
protected javax.swing.JMenuItem[] _fileMenuItems
protected javax.swing.JMenu _helpMenu
protected javax.swing.JMenuItem[] _helpMenuItems
protected javax.swing.JMenuBar _menubar
protected StatusBar _statusBar
Constructor Detail |
public Top()
public Top(StatusBar statusBar)
statusBar
- A status bar, or null to not insert one.Method Detail |
public void centerOnScreen()
public final void close()
public static void deferIfNecessary(java.lang.Runnable action)
Note that it does not work nearly as well to simply schedule the action yourself on the event thread because if there are a large number of actions, then the event thread will not be able to keep up. By grouping these actions, we avoid this problem.
action
- The Runnable object to execute.public boolean getCentering()
public boolean isModified()
public void report(java.lang.Throwable throwable)
throwable
- The Throwable to reportreport(String, Throwable)
public void report(java.lang.String message)
message
- The message to report.public void report(java.lang.String message, java.lang.Throwable throwable)
message
- The message.throwable
- The Throwable to report.public void setBackground(java.awt.Color background)
setBackground
in class java.awt.Component
background
- The background color.public void setCentering(boolean centering)
centering
- Set to false to disable centering.public void setModified(boolean modified)
modified
- Indicator of whether the data has been modified.public void pack()
pack
in class java.awt.Window
public void show()
show
in class java.awt.Window
protected void _about()
protected void _addMenus()
JMenu newMenu = new JMenu("My Menu"); _menubar.add(newMenu);The reason for doing this in a protected method rather than doing it directly in the constructor of the base class is subtle. Unfortunately, at this time, Java provides no mechanism for derived classes to insert menus at arbitrary points in the menu bar. Also, the menubar ignores the alignment property of the JMenu. By convention, however, we want the help menu to be the rightmost menu. Thus, we use a strategy pattern here, and call a protected method that derived classes can use to add menus. Thus, this method is called before the Help menu is added, and hence menus added in this method will appear to the left of the Help menu.
protected boolean _clear()
protected boolean _close()
protected void _exit()
protected java.lang.String _getName()
protected void _help()
protected void _open()
protected void _openURL()
protected void _print()
protected abstract void _read(java.net.URL url) throws java.lang.Exception
url
- The URL to read.
java.lang.Exception
- If the URL cannot be read.protected boolean _save()
protected boolean _saveAs()
protected abstract void _writeFile(java.io.File file) throws java.io.IOException
file
- The file to write to.
java.io.IOException
- If the write fails.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |