Class Thinlet

All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable, Runnable
Direct Known Subclasses:
DefaultOWThinlet

public class Thinlet extends Container implements Runnable, Serializable
See Also:
  • Field Details

    • dtd

      protected static Object[] dtd
  • Constructor Details

    • Thinlet

      public Thinlet()
  • Method Details

    • create

      @NotNull public static @NotNull Object create(String classname)
      Creates a new component
      Parameters:
      classname - the widget type (e.g. button )
      Returns:
      a new component, every component is simply an Object
      Throws:
      IllegalArgumentException - for unknown widget type
    • createImpl

      @NotNull protected static @NotNull Object createImpl(String classname)
    • getDOMAttribute

      @Nullable protected static @Nullable String getDOMAttribute(Object node, @NotNull @NotNull String key)
      Gets the attribute value by the specified key for a DOM tag
      Parameters:
      node - a specified tag
      key - a string to identify the value pair
      Returns:
      the value, or null
    • get

      @Nullable protected static @Nullable Object get(Object component, Object key)
      Get a specified sub-component or component part. Key is e.g. "header" (table header), ":parent" (parent component), ":comp" (head of a component list), ":next" (next component in a list), "popupmenu" is a popupmenu, etc ...
    • getDOMText

      @Nullable protected static @Nullable String getDOMText(Object node)
      Gets the content string of a tag
      Parameters:
      node - a specified tag
      Returns:
      the value, or null
    • getDOMCount

      protected static int getDOMCount(Object node, @NotNull @NotNull String key)
      Gets the number of tags in a tag by a specified tagname
      Parameters:
      node - a specified tag
      key - the searched tagname
      Returns:
      the number of tags
    • getLangResource

      public static ResourceBundle getLangResource()
      Returns language resource bundle currently in use, or default bundle, or null.
    • setLangResource

      public void setLangResource(ResourceBundle res)
      Set current language resource bundle. This flushes all cached translated values, performs lazy loading of new values, and repaints the desktop. This implementation allows applications to switch language resources on the fly, without rebuilding/reloading components that use them.
      The pseudo-code is as follows:
      • if langResource and langResourceDefault are null, don't translate anything, no matter what other settings are. This behaviour provides compatibility with previous versions.
      • if only langResourceDefault is set, use this when translation is required
      • if allI18n is set to true:
        • if property "i18n" on a component is missing, or set to "true", translate
        • if property "i18n" is present, and set to "false", do not translate
      • if allI18n is set to false:
        • if property "i18n" on a component is missing, or set to "false", do not translate
        • if property "i18n" is present, and set to "true", translate

      The "translate" step is applied only to values from "text" and "tooltip" properties (for now), and is applied as follows:

      • use the value of "text" or "tooltip" as a lookup key
      • use langResource to lookup the result value
        • if no value is found, use langResourceDefault for lookup
          • if no value is found, just return the original value of the property. Set a flag on component that prevents lookups in the future. This flag is cleared when langResource is changed.
      • cache the result value, if any

      If translated value is found successfully, it is cached in the component. This cache is gradually flushed when setLangResource is called. Cached value is also flushed when setString() is called on a component.

      Parameters:
      res - resource bundle containing localized texts for "text" and "tooltip"
    • getLangResourceDefault

      public static ResourceBundle getLangResourceDefault()
      Returns default language resource bundle, or null.
    • setLangResourceDefault

      public void setLangResourceDefault(ResourceBundle res)
      Set default language resource bundle. Resources from this bundle will be used if they are missing in the current bundle.
      Parameters:
      res - resource bundle containing default localized texts for "text" and "tooltip"
    • setColors

      public void setColors(int background, int text, int textbackground, int border, int disable, int hover, int press, int focus, int select)
      Sets the 9 colors used for components, and repaints the whole UI
      Parameters:
      background - the backround of panels (dialogs, desktops), and disabled controls, not editable texts, lines between list items (the default value if #e6e6e6 )
      text - for text, arrow foreground ( black by default)
      textbackground - the background of text components, and lists ( white by default)
      border - for outer in inner borders of enabled components ( #909090 by default)
      disable - for text, border, arrow color in disabled components ( #b0b0b0 by default)
      hover - indicates that the mouse is inside a button area ( #ededed by default)
      press - for pressed buttons, gradient image is calculated using the background and this press color ( #b9b9b9 by default)
      focus - for text caret and rectagle color marking the focus owner ( #89899a by default)
      select - used as the background of selected text, and list items, and in slider ( #c5c5dd by default)
    • setFont

      public void setFont(Font font)
      Sets the only one font used everywhere, and revalidates the whole UI. Scrollbar width/height, spinbox, and combobox button width, and slider size is the same as the font height
      Overrides:
      setFont in class Container
      Parameters:
      font - the default font is SansSerif , plain , and 12pt
    • getPreferredSize

      public Dimension getPreferredSize()
      Gets the preferred size of the root component
      Overrides:
      getPreferredSize in class Container
      Returns:
      a dimension object indicating the root component's preferred size
    • getDOMNode

      @Nullable protected static @Nullable Object getDOMNode(Object node, @NotNull @NotNull String key, int index)
      Gets the subtag of the specified tag by tagname and index
      Parameters:
      node - a specified tag
      key - the searched tagname
      index - the index of the requested subtag
      Returns:
      the found tag, or null
    • getDefinition

      protected static Object @NotNull [] getDefinition(@Nullable @Nullable Object classname, String key, @Nullable @Nullable String type)
      Throws:
      IllegalArgumentException
    • doLayout

      protected void doLayout(Object component)
    • paint

      public void paint(@NotNull @NotNull Graphics g)
      Paints the components inside the graphics clip area
      Overrides:
      paint in class Container
    • update

      public void update(@NotNull @NotNull Graphics g)
      Invokes the paint method
      Overrides:
      update in class Container
    • processEvent

      protected void processEvent(@NotNull @NotNull AWTEvent e)
      Dispatches mouse, key, focus, and component events occurring on the Thinlet component internally
      Overrides:
      processEvent in class Container
    • set

      protected static boolean set(Object component, Object key, @Nullable @Nullable Object value)
    • run

      public void run()
      A second thread is used to repeat value change events for scrollbar or spinbox during the mouse is pressed, or to pop up tooltip
      Specified by:
      run in interface Runnable
    • isFocusTraversable

      public boolean isFocusTraversable()
      This component can be traversed using Tab or Shift-Tab keyboard focus traversal, although 1.4 replaced this method by isFocusable , so 1.4 compilers write deprecation warning
      Overrides:
      isFocusTraversable in class Component
      Returns:
      true as focus-transverable component, overwrites the default false value
    • getClass

      @Nullable public static @Nullable String getClass(Object component)
      Gets the type of the given component
      Parameters:
      component - a widget
      Returns:
      the class name of the component (e.g. button )
    • invoke

      protected boolean invoke(Object component, Object part, String event)
      Invokes a method, such as an action, on the specified component.
      Parameters:
      component - the component to fire the event on, such as a textfield or table
      part - the part of the component, null for a textfield, the row for a table
      event - the event to send, such as 'action'
      Returns:
      true if a method object was fired
    • invokeImpl

      protected void invokeImpl(Object method, Object part)
    • getParent

      @Nullable public @Nullable Object getParent(Object component)
      Gets the parent of this component
      Parameters:
      component - a widget
      Returns:
      the parent container of this component or item
    • repaint

      public void repaint(Object component)
      Repaint the given component's area later
      Parameters:
      component - a visible widget inside thinlet desktop
    • requestFocus

      public boolean requestFocus(Object component)
      Requests that both the Thinlet component, and the given widget get the input focus
      Parameters:
      component - a focusable widget inside visible and enabled parents, and tabbedpane's selected tab
      Returns:
      true, if the given component was focusable
    • setFocus

      protected boolean setFocus(Object component)
      Request focus for the given component
      Parameters:
      component - a focusable component
      Returns:
      true if the focusowner was changed, otherwise false
    • getDesktop

      public Object getDesktop()
      Get the topmost component
      Returns:
      the root object (it is a desktop ), never null
    • getCount

      public int getCount(Object component)
      Gets the count of sub-components in the list of the given component
      Parameters:
      component - a widget
      Returns:
      the number of components in this component
    • getSelectedIndex

      public int getSelectedIndex(Object component)
      Gets the index of the first selected item in the given component
      Parameters:
      component - a widget (combobox, tabbedpane, list, table, header, or tree)
      Returns:
      the first selected index or -1
    • removeAll

      public void removeAll(Object component)
      Removes all the components from this container's specified list
      Parameters:
      component - the specified container
    • handleException

      protected void handleException(@NotNull @NotNull Throwable throwable)
      Overwrite this method to handle exceptions thrown by the invoked custom methods
      Parameters:
      throwable - the thrown exception by the bussiness logic
    • getSelectedItem

      @Nullable public @Nullable Object getSelectedItem(Object component)
      Gets the first selected item of the given component
      Parameters:
      component - a widget (combobox, tabbedpane, list, table, header or tree)
      Returns:
      the first selected item or null
    • getSelectedItems

      public Object @NotNull [] getSelectedItems(Object component)
      Gets the selected item of the given component (list, table, or tree) when multiple selection is allowed
      Parameters:
      component - a widget
      Returns:
      the array of selected items, or a 0 length array
    • getItem

      @Nullable public @Nullable Object getItem(Object component, int index)
      Returns the subcomponent of the given component's specified list at the given index
      Parameters:
      component - a specified container
      index - the index of the component to get
      Returns:
      the index th component in this container
    • getItemCountImpl

      protected static int getItemCountImpl(Object component, String key)
    • getItems

      public Object @NotNull [] getItems(Object component)
      Gets all the components in this container
      Parameters:
      component - a specified container
      Returns:
      an array of all the components in this container
    • add

      public void add(Object component)
      Adds the specified component to the root desktop
      Parameters:
      component - a widget to be added
    • add

      public void add(Object parent, Object component)
      Adds the specified component to the end of the specified container
      Parameters:
      parent - a container
      component - a component to be added
    • add

      public void add(Object parent, Object component, int index)
      Adds the specified component to the container at the given position
      Parameters:
      parent - a container
      component - a component to be inserted
      index - the position at which to insert the component, or -1 to insert the component at the end
    • insertItem

      protected void insertItem(Object parent, Object key, Object component, int index)
      Referenced by DOM
    • remove

      public void remove(Object component)
      Remove the specified component from its parent list, or delete component's popupmenu or table's header
      Parameters:
      component - the component to be removed
    • find

      public Object find(@NotNull @NotNull String name)
      Finds the first component from the root desktop by a specified name value
      Parameters:
      name - parameter value identifies the widget
      Returns:
      the first suitable component, or null
    • find

      @Nullable public @Nullable Object find(Object component, @NotNull @NotNull String name)
      Finds the first component from the specified component by a name
      Parameters:
      component - the widget is searched inside this component
      name - parameter value identifies the widget
      Returns:
      the first suitable component, or null
    • parse

      public Object parse(@NotNull @NotNull String path) throws IOException
      Creates a component (and its subcomponents, and properties) from the given xml resource
      Parameters:
      path - is relative to your thinlet instance or the classpath (if the path starts with an / character), or a full URL
      Returns:
      the root component of the parsed resource
      Throws:
      IOException
    • parse

      public Object parse(@NotNull @NotNull String path, @NotNull @NotNull Object handler) throws IOException
      Creates a component from the given xml resource using the specified event handler
      Parameters:
      path - is relative to your application package or the classpath, or an URL
      handler - bussiness methods are implemented in this object
      Returns:
      the parsed components' root
      Throws:
      IOException
    • parse

      public Object parse(@NotNull @NotNull InputStream inputstream) throws IOException
      Creates a component from the given stream
      Parameters:
      inputstream - e.g. new URL("http://myserver/myservlet").openStream()
      Returns:
      the root component of the parsed stream
      Throws:
      IOException
    • parse

      public Object parse(@NotNull @NotNull InputStream inputstream, @NotNull @NotNull Object handler) throws IOException
      Creates a component from the given stream and event handler
      Parameters:
      inputstream - read xml from this stream
      handler - event handlers are implemented in this object
      Returns:
      the parsed components' root
      Throws:
      IOException
    • parseXML

      protected void parseXML(@NotNull @NotNull InputStream inputstream) throws IOException
      You can use the internal xml parser as a simple SAX-like parser, during the process it calls the startElement , characters , and endElement methods
      Parameters:
      inputstream - e.g. new URL("http://myserver/myservlet").openStream()
      Throws:
      IOException
    • startElement

      protected void startElement(String name, Map<String,String> attributelist)
      The SAX-like parser calls this method, you have to overwrite it
      Parameters:
      name - of the tag
      attributelist - a list of attributes including keys and value pairs
    • characters

      protected void characters(String text)
      The SAX-like parser calls this method, you have to overwrite it
      Parameters:
      text - the content of a tag
    • endElement

      protected void endElement()
      The SAX-like parser calls this method, you have to overwrite it
    • parseDOM

      protected Object parseDOM(@NotNull @NotNull InputStream inputstream) throws IOException
      You can use the internal xml parser as a simple DOM-like parser, use the getDOMAttribute , getDOMText , getDOMCount , getDOMNode , getClass , and getParent methods to analise the document
      Parameters:
      inputstream - e.g. new URL("http://myserver/myservlet").openStream()
      Returns:
      the root tag
      Throws:
      IOException
    • setResourceBundle

      public void setResourceBundle(ResourceBundle resourcebundle)
      Set a bundle used in parse method, it replaces the parameter values starting with the 'i18n.' string with a value found in the given bundle
      Parameters:
      resourcebundle - a bundle for the next parsing or null to remove the current one
      Throws:
      MissingResourceException - if no object for the given key can be found
    • parse

      @Nullable protected @Nullable Object parse(@NotNull @NotNull InputStream inputstream, boolean validate, boolean dom, @NotNull @NotNull Object handler) throws IOException
      Parameters:
      inputstream -
      validate - parse GUI from xml if true
      dom - parse an xml resoource
      handler -
      Returns:
      Throws:
      IOException
      IllegalArgumentException
    • finishParse

      protected void finishParse(@Nullable @Nullable Vector<Object> methods, Object root, @NotNull @NotNull Object handler)
    • addImpl

      protected void addImpl(Object parent, Object component, int index)
      Add the component to the parent's ':comp' list, and set its ':parent' or set single components
      Parameters:
      index - add at the specified index
      Throws:
      IllegalArgumentException
    • instance

      protected boolean instance(Object classname, Object extendclass)
    • addElement

      @NotNull protected @NotNull Object addElement(Object parent, String name)
    • addAttribute

      protected void addAttribute(Object component, String key, @NotNull @NotNull String value, @NotNull @NotNull Vector<Object> lasts)
      Throws:
      IllegalArgumentException
    • setString

      public void setString(Object component, String key, String value)
      Sets the given property pair (key and value) for the component
    • setString

      protected boolean setString(Object component, String key, String value, String defaultvalue)
    • getString

      public String getString(Object component, String key)
      Gets the property value of the given component by the property key
    • setChoice

      public void setChoice(Object component, String key, String value)
      Sets the given property pair (key and value) for the component
    • getWidget

      @Nullable public @Nullable Object getWidget(Object component, String key)
    • getChoice

      public String getChoice(Object component, String key)
      Gets the property value of the given component by the property key
    • setBoolean

      public void setBoolean(Object component, String key, boolean value)
      Sets the given property pair (key and value) for the component
    • getBoolean

      public boolean getBoolean(Object component, String key)
      Gets the property value of the given component by the property key
    • setInteger

      public void setInteger(Object component, String key, int value)
      Sets the given property pair (key and value) for the component
    • getInteger

      public int getInteger(Object component, String key)
      Gets the property value of the given component by the property key
    • setIcon

      public void setIcon(Object component, String key, Image icon)
      Sets the given property pair (key and value) for the component
    • getIcon

      public Image getIcon(Object component, String key)
      Gets the property value of the given component by the property key
    • setKeystroke

      public void setKeystroke(Object component, String key, String value)
    • setMethod

      public void setMethod(Object component, String key, @NotNull @NotNull String value, Object root, @NotNull @NotNull Object handler)
      Sets a new event handler method for a component
      Parameters:
      component - the target component
      key - the key name of the parameter (e.g. action )
      value - the method name and parameters (e.g. foo(this, this.text, mybutton, mybutton.enabled) for public void foo(Object component, String text, Object mybutton, boolean enabled) )
      root - the search starting component for name components in the arguments
      handler - the target event handler object including the method
      Throws:
      IllegalArgumentException
    • getComponent

      public Component getComponent(Object component, String key)
      Get the AWT component of the given (currently bean ) widget
      Parameters:
      component - a bean widget
      key - the identifier of the parameter
      Returns:
      an AWT component, or null
    • setFont

      public void setFont(Object component, Font font)
      Set custom font on a component, use the other setFont method instead
    • setFont

      public void setFont(Object component, String key, Font font)
      Set custom font on a component
      Parameters:
      component - component to use the custom font
      key - currently only "font" is supported
      font - custom font to use, or null to reset component to use default font
    • getFont

      public Font getFont(Object component, String key)
      Get custom font of a component. This method may return null if the default font is used.
      Parameters:
      component - a component
      key - the identifier of the parameter, e.g. "font"
      Returns:
    • setColor

      public void setColor(Object component, String key, Color color)
      Set custom color on a component. Notes: For "foreground" key, this sets the text color. For "background" key, on gradient-filled components (such as tabs, buttons etc) this will result in a component filled with solid background color, and not a new gradient. Also, Color.brighter() will be used for highlight, and Color.darker() will be used for pressed or not selected.
      Parameters:
      component - component to use for custom color
      key - currently "background" and "foreground" are supported
      color - custom color to use, or null to reset component to use default color
    • getColor

      public Color getColor(Object component, String key)
      Get custom color of a component. This method may return null if the default color is used.
      Parameters:
      component - a component
      key - the identifier of the parameter, e.g. "foreground"
      Returns:
      value of the custom color, or null if default color is used
    • setComponent

      public void setComponent(Object component, String key, Component bean)
      Set the AWT component for the given (currently bean ) widget
      Parameters:
      component - a bean widget
      key - the identifier of the parameter
      bean - an AWT component, or null
    • getMethod

      protected Object @NotNull [] getMethod(Object component, @NotNull @NotNull String value, Object root, @NotNull @NotNull Object handler)
      Returns:
      an object list including as follows: - handler object, - method, - list of parameters including 3 values: - ("thinlet", null, null) for the single thinlet component, - (target component, null, null) for named widget as parameter, e.g. mybutton, - (target, parameter name, default value) for a widget's given property, e.g. mylabel.enabled, - ("item", null, null) for an item of the target component as parameter, e.g. tree node, - ("item", parameter name, default value) for the item's given property e.g. list item's text.
    • getProperty

      @Nullable public @Nullable Object getProperty(Object component, Object key)
      Returns the value of the property with the specified key.
      Parameters:
      component - searches the hashtable of this component
      key - the client property key
      Returns:
      the value to which the key is mapped or null if the key is not mapped to any value
    • setAllI18n

      public void setAllI18n(boolean val)
      Sets the default behaviour of internationalization code. If set to "true", try to translate all components' "text" and "tooltip" values, unless explicitly prohibited by setting i18n="false" on a specific component. If set to "false", do not translate unless explicitly requested by setting i18n="true" on a specific component.
      Default value is "false", to provide backwards compatibility.
      Parameters:
      val - if "true", translate by default; if "false", do not translate by default.
    • putProperty

      public void putProperty(Object component, Object key, @Nullable @Nullable Object value)
      Binds the specified key to the specified value, and stores in this component. Null value removes the property. Use the parameter tag in the xml resource to bind a string value, the format is: parameter='key=value'
      Parameters:
      component - the hashtable is binded to this component
      key - the client property key
      value - the new client property value
    • getIcon

      public Image getIcon(String path)
      Creates an image, and loads it immediately by default
      Parameters:
      path - is relative to your thinlet instance or the classpath (if the path starts with '/' character), or a full URL
      Returns:
      the loaded image or null
    • getIcon

      @Nullable public @Nullable Image getIcon(@Nullable @Nullable String path, boolean preload)
      Creates an image from the specified resource. To speed up loading the same images use a cache (a simple hashtable). And flush the resources being used by an image when you won't use it henceforward
      Parameters:
      path - is relative to your thinlet instance or the classpath, or an URL
      preload - waits for the whole image if true, starts loading (and repaints, and updates the layout) only when required (painted, or size requested) if false
      Returns:
      the loaded image or null
    • destroy

      public boolean destroy()
      This method is called by the FrameLauncher if the window was closing, or AppletLauncher's destroy method. Overwrite it to e.g. save the application changes.
      Returns:
      true to exit, and false to keep the frame and continue the application