Package jmri.util

Class JmriJFrame

All Implemented Interfaces:
ComponentListener, WindowListener, ImageObserver, MenuContainer, Serializable, EventListener, Accessible, RootPaneContainer, WindowConstants, BeanInterface, ModifiedFlag, WindowInterface
Direct Known Subclasses:
AbstractAudioFrame, AbstractMonFrame, AbstractPacketGenFrame, AbstractRouteAddEditFrame, AcelaPacketGenFrame, ActivateTrainFrame, AddEditSingleLightControlFrame, AddEntryExitPairFrame, AddNodeFrame, AddSignalMastJFrame, AlignTableFrame, AnalogClockFrame, AppsLaunchFrame, AssociateFrame, AutomatTableFrame, AutoTrainsFrame, BeanTableFrame, BlockBossFrame, BlockPathEditFrame, CbusEventHighlightFrame, CbusFilterFrame, CbusNodeEditEventFrame, CbusNodeRestoreFcuFrame, CircuitBuilder.CBFrame, CMRInetManagerFrame, CMRInetMetricsFrame, ConditionalFrame, ConditionalNGDebugger, ConfigBaseStationFrame, ConsistToolFrame, ControllerFilterFrame, CoordinateEdit, DataSource, DebuggerFrame, DiagnosticFrame, DispatcherFrame, DisplayFrame, DmiFrame, DrawFrame, DualDecoderSelectFrame, EasyDccPacketGenFrame, EditFrame, EditNodeFrame, Editor.JFrameItem, EditorFrame, FollowerFrame, HexFileFrame, ImageIndexEditor, ImportLogixFrame, InlineLogixNGsFrame, ItemDialog, JmriJFrameWithPermissions, JmriServerFrame, JmriSRCPServerFrame, LcdClockFrame, LearnThrottleFrame, LI101Frame, ListFrame, ListFrame, LIUSBConfigFrame, LnTcpServerFrame, LoaderFrame, LogFrame, LogixNGBrowseWindow, LogixNGInitializationTable, LV102Frame, LZ100Frame, LZV100Frame, ManageLocationsFrame, MastBuilderPane, MdiMainFrame, MeterFrame, MultiPaneWindow, MultiSensorIconFrame, NixieClockFrame, NodeConfigFrame, NodeConfigFrame, NodeConfigFrame, NodeConfigFrame, NodeConfigFrame, NodeConfigFrame, NodeConfigManagerFrame, NodeIOListFrame, NodeSpecificFrame, NodeTableFrame, NumberMapFrame, OperationsFrame, OsIndicatorFrame, PacketGenFrame, PacketGenFrame, PacketGenFrame, PacketTableFrame, PaneProgFrame, PickFrame, PollTableFrame, PortalEditFrame, PowerPanelFrame, PragotronClockFrame, PreferencesFrame, ProtocolOptionsFrame, RenumberFrame, ReportFrame, RosterGroupTableFrame, RpsTrackingFrame, SendPacketFrame, SensorGroupFrame, SensorTextEdit, SerialFilterFrame, SerialPacketGenFrame, SerialPacketGenFrame, SerialPacketGenFrame, SerialPacketGenFrame, SerialPacketGenFrame, SerialPacketGenFrame, SerialPacketGenFrame, ServerFrame, SignalEditFrame, SignalHeadAddEditFrame, SignallingFrame, SignallingSourceFrame, SignalMastRepeaterJFrame, SimpleClockFrame, SimpleLightCtrlFrame, SimpleProgFrame, SimpleServerFrame, SimpleTurnoutCtrlFrame, SlipTurnoutTextEdit, SoundSetFrame, SpeedoConsoleFrame, SpeedometerFrame, SpeedProfileFrame, SpeedProfileTable, SprogCbusModeSwitcherFrame, SprogHexFile, SprogPacketGenFrame, SprogSlotMonFrame, SprogUpdateFrame, SprogVersionFrame, StackMonFrame, StatusFrame, SystemInfoFrame, TabbedPreferencesFrame, ThreePaneTLRWindow, ThrottlesPreferencesWindow, ThrottleWindow, TieToolFrame, TimeTableFrame, TrackerTableAction.TableFrame, TreeFrame, TreeViewer, TwoPaneTBWindow, UsbBrowserFrame, UserInterface, UserInterface, VirtualLCDFrame, VSDManagerFrame, WarrantRoute, WarrantTableFrame, WhereUsedFrame, WhereUsedFrame, XpaConfigureFrame, XpaPacketGenFrame, Z21ConfigFrame

JFrame extended for common JMRI use.

We needed a place to refactor common JFrame additions in JMRI code, so this class was created.

Features:

  • Size limited to the maximum available on the screen, after removing any menu bars (macOS) and taskbars (Windows)
  • Cleanup upon closing the frame: When the frame is closed (WindowClosing event), the dispose() method is invoked to do cleanup. This is inherited from JFrame itself, so super.dispose() needs to be invoked in the over-loading methods.
  • Maintains a list of existing JmriJFrames

Window Closing

Normally, a JMRI window wants to be disposed when it closes. This is what's needed when each invocation of the corresponding action can create a new copy of the window. To do this, you don't have to do anything in your subclass.

If you want this behavior, but need to do something when the window is closing, override the windowClosing(java.awt.event.WindowEvent) method to do what you want. Also, if you override dispose(), make sure to call super.dispose().

If you want the window to just do nothing or just hide, rather than be disposed, when closed, set the DefaultCloseOperation to DO_NOTHING_ON_CLOSE or HIDE_ON_CLOSE depending on what you're looking for.

See Also:
  • Field Details

  • Constructor Details

    • JmriJFrame

      public JmriJFrame(boolean saveSize, boolean savePosition)
      Creates a JFrame with standard settings, optional save/restore of size and position.
      Parameters:
      saveSize - Set true to save the last known size
      savePosition - Set true to save the last known location
    • JmriJFrame

      public JmriJFrame()
      Creates a JFrame with standard settings, including saving/restoring of size and position.
    • JmriJFrame

      public JmriJFrame(String name)
      Creates a JFrame with with given name plus standard settings, including saving/restoring of size and position.
      Parameters:
      name - Title of the JFrame
    • JmriJFrame

      public JmriJFrame(String name, boolean saveSize, boolean savePosition)
      Creates a JFrame with with given name plus standard settings, including optional save/restore of size and position.
      Parameters:
      name - Title of the JFrame
      saveSize - Set true to save the last knowm size
      savePosition - Set true to save the last known location
  • Method Details

    • initFrame

      final void initFrame()
    • setFrameTitle

      final void setFrameTitle(String name)
    • makePrivateWindow

      public void makePrivateWindow()
      Remove this window from the Windows Menu by removing it from the list of active JmriJFrames.
    • makePublicWindow

      public void makePublicWindow()
      Add this window to the Windows Menu by adding it to the list of active JmriJFrames.
    • setFrameLocation

      public void setFrameLocation()
      Reset frame location and size to stored preference value
    • getScreenDimensions

      returns the previously initialized array of screens. See getScreenDimensionsOnce()
      Returns:
      ArrayList of screen bounds and insets
    • generateWindowRef

      public void generateWindowRef()
      Regenerates the window frame ref that is used for saving and setting frame size and position against.
    • pack

      public void pack()
      Overrides:
      pack in class Window
    • undecorate

      public void undecorate()
      Remove any decoration, such as the title bar or close window control, from the JFrame.

      JmriJFrames are often built internally and presented to the user before any scripting action can interact with them. At that point it's too late to directly invoke setUndecorated(true) because the JFrame is already displayable. This method uses dispose() to drop the windowing resources, sets undecorated, and then redisplays the window.

    • reSizeToFitOnScreen

      Tries to get window to fix entirely on screen. First choice is to move the origin up and left as needed, then to make the window smaller
    • offSetFrameOnScreen

      Move a frame down and to the left by it's top offset or a fixed amount, whichever is larger
      Parameters:
      f - JmirJFrame to move
    • getWindowFrameRef

    • initComponents

      public void initComponents()
      By default, Swing components should be created an installed in this method, rather than in the ctor itself.
    • addHelpMenu

      public final void addHelpMenu(String ref, boolean direct)
      Add a standard help menu, including window specific help item. Final because it defines the content of a standard help menu, not to be messed with individually
      Parameters:
      ref - JHelp reference for the desired window-specific help page; null means no page
      direct - true if the help main-menu item goes directly to the help system, such as when there are no items in the help menu
    • addWindowCloseShortCut

      Adds a "Close Window" key shortcut to close window on op-W.
    • setEscapeKeyAction

      public void setEscapeKeyAction(AbstractAction action)
      Bind an action to the Escape key.

      Binds an AbstractAction to the Escape key. If an action is already bound to the Escape key, that action will be replaced. Passing null unbinds any existing actions from the Escape key.

      Note that binding the Escape key to any action may break expected or standardized behaviors. See Keyboard Shortcuts, Mnemonics, and Other Keyboard Operations in the Java Look and Feel Design Guidelines for standardized behaviors.

      Parameters:
      action - The AbstractAction to bind to.
      See Also:
    • getEscapeKeyAction

      The action associated with the Escape key.
      Returns:
      An AbstractAction or null if no action is bound to the Escape key.
      See Also:
    • setEscapeKeyClosesWindow

      public void setEscapeKeyClosesWindow(boolean closesWindow)
      Bind the Escape key to an action that closes the window.

      If closesWindow is true, this method creates an action that triggers the "window is closing" event; otherwise this method removes any actions from the Escape key.

      Parameters:
      closesWindow - Create or destroy an action to close the window.
      See Also:
    • getEscapeKeyClosesWindow

      public boolean getEscapeKeyClosesWindow()
      Does the Escape key close the window?
      Returns:
      true if Escape key is bound to action created by setEscapeKeyClosesWindow, false in all other cases.
      See Also:
    • getMaximumSize

      Provide a maximum frame size that is limited to what can fit on the screen after toolbars, etc are deducted.

      Some of the methods used here return null pointers on some Java implementations, however, so this will return the superclasses's maximum size if the algorithm used here fails.

      Overrides:
      getMaximumSize in class Container
      Returns:
      the maximum window size
    • getPreferredSize

      The preferred size must fit on the physical screen, so calculate the lesser of either the preferred size from the layout or the screen size.
      Overrides:
      getPreferredSize in class Container
      Returns:
      the preferred size or the maximum size, whichever is smaller
    • getFrameList

      Get a List of the currently-existing JmriJFrame objects. The returned list is a copy made at the time of the call, so it can be manipulated as needed by the caller.
      Returns:
      a list of JmriJFrame instances. If there are no instances, an empty list is returned.
    • getFrameList

      public static <T extends JmriJFrame> List<T> getFrameList(@Nonnull Class<T> type)
      Get a list of currently-existing JmriJFrame objects that are specific sub-classes of JmriJFrame.

      The returned list is a copy made at the time of the call, so it can be manipulated as needed by the caller.

      Type Parameters:
      T - generic JmriJframe.
      Parameters:
      type - The Class the list should be limited to.
      Returns:
      An ArrayList of Frames.
    • getFrame

      public static JmriJFrame getFrame(String name)
      Get a JmriJFrame of a particular name. If more than one exists, there's no guarantee as to which is returned.
      Parameters:
      name - the name of one or more JmriJFrame objects
      Returns:
      a JmriJFrame with the matching name or null if no matching frames exist
    • setSavePosition

      public void setSavePosition(boolean save)
      Set whether the frame Position is saved or not after it has been created.
      Parameters:
      save - true if the frame position should be saved.
    • setSaveSize

      public void setSaveSize(boolean save)
      Set whether the frame Size is saved or not after it has been created.
      Parameters:
      save - true if the frame size should be saved.
    • getSavePosition

      public boolean getSavePosition()
      Returns if the frame Position is saved or not.
      Returns:
      true if the frame position should be saved
    • getSaveSize

      public boolean getSaveSize()
      Returns if the frame Size is saved or not.
      Returns:
      true if the frame size should be saved
    • setModifiedFlag

      public void setModifiedFlag(boolean flag)
      Set the modified flag to a specific value A frame is considered "modified" if it has changes that have not been stored.
      Specified by:
      setModifiedFlag in interface ModifiedFlag
      Parameters:
      flag - true if the object has been modified
    • getModifiedFlag

      public boolean getModifiedFlag()
      Get the current value of the modified flag.
      Specified by:
      getModifiedFlag in interface ModifiedFlag
      Returns:
      true if the object has been modified and needs to be stored
    • handleModified

      protected void handleModified()
      Handle closing a window or quiting the program while the modified bit was set.
    • storeValues

      protected void storeValues()
    • markWindowModified

      public void markWindowModified(boolean yes)
    • windowOpened

      public void windowOpened(WindowEvent e)
      Does nothing in this class
      Specified by:
      windowOpened in interface WindowListener
    • windowClosed

      public void windowClosed(WindowEvent e)
      Does nothing in this class
      Specified by:
      windowClosed in interface WindowListener
    • windowActivated

      public void windowActivated(WindowEvent e)
      Does nothing in this class
      Specified by:
      windowActivated in interface WindowListener
    • windowDeactivated

      public void windowDeactivated(WindowEvent e)
      Does nothing in this class
      Specified by:
      windowDeactivated in interface WindowListener
    • windowIconified

      public void windowIconified(WindowEvent e)
      Does nothing in this class
      Specified by:
      windowIconified in interface WindowListener
    • windowDeiconified

      public void windowDeiconified(WindowEvent e)
      Does nothing in this class
      Specified by:
      windowDeiconified in interface WindowListener
    • windowClosing

      The JmriJFrame implementation calls handleModified().
      Specified by:
      windowClosing in interface WindowListener
    • componentHidden

      Does nothing in this class
      Specified by:
      componentHidden in interface ComponentListener
    • componentMoved

      public void componentMoved(ComponentEvent e)
      Specified by:
      componentMoved in interface ComponentListener
    • componentResized

      Specified by:
      componentResized in interface ComponentListener
    • componentShown

      public void componentShown(ComponentEvent e)
      Does nothing in this class
      Specified by:
      componentShown in interface ComponentListener
    • setShutDownTask

      protected void setShutDownTask()
    • dispose

      When window is finally destroyed, remove it from the list of windows.

      Subclasses that over-ride this method must invoke this implementation with super.dispose() right before returning.

      Specified by:
      dispose in interface WindowInterface
      Overrides:
      dispose in class Window
    • setIndexedProperty

      public void setIndexedProperty(String key, int index, Object value)
      Set the value of an element in an indexed property.

      NOTE Implementing methods must not call Bean.setIndexedProperty(), as doing so will cause a stack overflow. Implementing methods may call Beans.setIntrospectedIndexedProperty() instead.

      Specified by:
      setIndexedProperty in interface BeanInterface
      Parameters:
      key - name of the property
      index - index of the property element to change
      value - the value to set the property to
    • getIndexedProperty

      public Object getIndexedProperty(String key, int index)
      Get the value of an element in an indexed property.

      NOTE Implementing methods must not call Bean.getIndexedProperty(), as doing so will cause a stack overflow. Implementing methods may call Beans.getIntrospectedIndexedProperty() instead.

      Specified by:
      getIndexedProperty in interface BeanInterface
      Parameters:
      key - name of the property
      index - index of the property element to change
      Returns:
      value of the property or null
    • setProperty

      public void setProperty(String key, Object value)
      Set the value of a property.

      NOTE Implementing methods must not call Bean.setProperty(), as doing so will cause a stack overflow. Implementing methods may call Beans.setIntrospectedProperty() instead. Subclasses should override this method with something more direct and faster

      Specified by:
      setProperty in interface BeanInterface
      Parameters:
      key - name of the property
      value - the value to set the property to
    • getProperty

      public Object getProperty(String key)
      Get the value of a property.

      NOTE Implementing methods must not call Bean.getProperty(), as doing so will cause a stack overflow. Implementing methods may call Beans.getIntrospectedProperty() instead. Subclasses should override this method with something more direct and faster

      Specified by:
      getProperty in interface BeanInterface
      Parameters:
      key - name of the property
      Returns:
      The value of the property or null
    • hasProperty

      public boolean hasProperty(String key)
      Test that a property exists.

      NOTE Implementing method must not call Bean.hasProperty(), as doing so will cause a stack overflow. Implementing methods may call Beans.hasIntrospectedProperty() instead.

      Specified by:
      hasProperty in interface BeanInterface
      Parameters:
      key - name of the property
      Returns:
      true is property key exists
    • hasIndexedProperty

      public boolean hasIndexedProperty(String key)
      Test that a property exists and is indexed.

      NOTE Implementing method must not call Bean.hasIndexedProperty(), as doing so will cause a stack overflow. Implementing methods may call Beans.hasIntrospectedIndexedProperty() instead.

      Specified by:
      hasIndexedProperty in interface BeanInterface
      Parameters:
      key - name of the property
      Returns:
      true is property key exists and is indexed
    • show

      public void show(JmriPanel child, JmriAbstractAction action)
      Show, in whatever way is appropriate, a specific JmriPanel
      Specified by:
      show in interface WindowInterface
      Parameters:
      child - new JmriPanel to show
      action - JmriAbstractAction making the request
    • show

      public void show(JmriPanel child, JmriAbstractAction action, WindowInterface.Hint hint)
      Show, in whatever way is appropriate, a specific JmriPanel, in a hinted location
      Specified by:
      show in interface WindowInterface
      Parameters:
      child - new JmriPanel to show
      action - JmriAbstractAction making the request
      hint - suggestion on where to put the content
    • multipleInstances

      public boolean multipleInstances()
      Should 2nd and subsequent requests for a panel create a new instance, or provide the 1st one for reuse?
      Specified by:
      multipleInstances in interface WindowInterface
      Returns:
      true if multiple instances should be provided, false if only one should be provided
    • setWindowInterface

    • getWindowInterface

    • getPropertyNames

      List all property names or keys.

      NOTE Implementing method must not call Bean.getPropertyNames(), as doing so will cause a stack overflow. Implementing methods may call Beans.getIntrospectedPropertyNames() instead.

      NOTE Implementations of this method should not return null.

      Specified by:
      getPropertyNames in interface BeanInterface
      Returns:
      property names or an empty Set.
    • setAllowInFrameServlet

      public void setAllowInFrameServlet(boolean allow)
    • getAllowInFrameServlet

      public boolean getAllowInFrameServlet()
    • getFrame

      public Frame getFrame()
      Returns the WindowInterface as a Frame or null.
      Specified by:
      getFrame in interface WindowInterface
      Returns:
      a Frame or null