JBox.java

  1. package swingtree.components;

  2. import net.miginfocom.swing.MigLayout;
  3. import swingtree.style.ComponentExtension;
  4. import swingtree.style.StylableComponent;

  5. import javax.accessibility.Accessible;
  6. import javax.accessibility.AccessibleContext;
  7. import javax.accessibility.AccessibleRole;
  8. import javax.swing.BoxLayout;
  9. import javax.swing.JComponent;
  10. import javax.swing.UIDefaults;
  11. import javax.swing.plaf.ComponentUI;
  12. import javax.swing.plaf.PanelUI;
  13. import java.awt.Graphics;
  14. import java.awt.LayoutManager;

  15. /**
  16.  * <code>JBox</code> is a generic lightweight container similar to
  17.  * <code>javax.swing.JPanel</code>, but with 2 important differences:
  18.  * <ul>
  19.  *     <li>
  20.  *         The <code>JBox</code> is transparent by default, meaning that it does
  21.  *         not paint its background if it is not explicitly set through the style API.
  22.  *     </li>
  23.  *     <li> It does not have any insets by default. </li>
  24.  * </ul>
  25.  *  <b>Please note that the {@link JBox} type is in no way related to the {@link BoxLayout}!
  26.  *  The term <i>box</i> is referring to the purpose of this component, which
  27.  *  is to tightly store and wrap other sub-components seamlessly...</b>
  28.  *
  29.  * @author Daniel Nepp
  30.  */
  31. public class JBox extends JComponent implements Accessible, StylableComponent
  32. {
  33.     /**
  34.      * @see #getUIClassID
  35.      */
  36.     private static final String uiClassID = "PanelUI";

  37.     /**
  38.      * Creates a new JBox with the specified layout manager and buffering
  39.      * strategy.
  40.      *
  41.      * @param layout  the LayoutManager to use
  42.      * @param isDoubleBuffered  a boolean, true for double-buffering, which
  43.      *        uses additional memory space to achieve fast, flicker-free
  44.      *        updates
  45.      */
  46.     public JBox( LayoutManager layout, boolean isDoubleBuffered ) {
  47.         setLayout(layout);
  48.         setDoubleBuffered(isDoubleBuffered);
  49.         setOpaque(false);
  50.         updateUI();
  51.     }

  52.     /**
  53.      * Create a new buffered JBox with the specified layout manager
  54.      *
  55.      * @param layout  the LayoutManager to use
  56.      */
  57.     public JBox(LayoutManager layout) {
  58.         this(layout, true);
  59.     }

  60.     /**
  61.      * Creates a new <code>JBox</code> with the specified buffering strategy
  62.      * qnd a default <code>MigLayout</code> instance
  63.      * configured to be without insets and gaps between components.
  64.      * If <code>isDoubleBuffered</code> is true, the <code>JBox</code>
  65.      * will use a double buffer.
  66.      *
  67.      * @param isDoubleBuffered  a boolean, true for double-buffering, which
  68.      *        uses additional memory space to achieve fast, flicker-free
  69.      *        updates
  70.      */
  71.     public JBox(boolean isDoubleBuffered) {
  72.         this(new MigLayout("ins 0, hidemode 2, gap 0"), isDoubleBuffered);
  73.     }

  74.     /**
  75.      * Creates a new <code>JBox</code> with a double buffer
  76.      * and a flow layout.
  77.      */
  78.     public JBox() {
  79.         this(true);
  80.     }

  81.     /** {@inheritDoc} */
  82.     @Override public void paintComponent(Graphics g){
  83.         paintBackground(g, super::paintComponent);
  84.     }

  85.     /** {@inheritDoc} */
  86.     @Override public void paintChildren(Graphics g) {
  87.         paintForeground(g, super::paintChildren);
  88.     }

  89.     @Override public void setUISilently( ComponentUI ui ) {
  90.         this.ui = ui;
  91.     }

  92.     /**
  93.      * Resets the UI property with a value from the current look and feel.
  94.      *
  95.      * @see JComponent#updateUI
  96.      */
  97.     @Override
  98.     public void updateUI() {
  99.         ComponentExtension.from(this).installCustomUIIfPossible();
  100.         /*
  101.             The JBox is a SwingTree native type, so it also
  102.             enjoys the perks of having a SwingTree look and feel!
  103.         */
  104.     }

  105.     /**
  106.      * Returns the look and feel (L&amp;amp;F) object that renders this component.
  107.      *
  108.      * @return the PanelUI object that renders this component
  109.      */
  110.     /*@Override*/
  111.     @SuppressWarnings("MissingOverride")
  112.     public PanelUI getUI() { return (PanelUI) ui; }


  113.     /**
  114.      * Sets the look and feel (L&amp;F) object that renders this component.
  115.      *
  116.      * @param ui  the PanelUI L&amp;F object
  117.      * @see UIDefaults#getUI
  118.      */
  119.     public void setUI( PanelUI ui ) {
  120.         super.setUI(ui);
  121.     }

  122.     /**
  123.      * Returns a string that specifies the name of the L&amp;F class
  124.      * that renders this component.
  125.      *
  126.      * @return "PanelUI"
  127.      * @see JComponent#getUIClassID
  128.      * @see UIDefaults#getUI
  129.      */
  130.     @Override public String getUIClassID() { return uiClassID; }

  131.     /**
  132.      * Returns a string representation of this JBox. This method
  133.      * is intended to be used only for debugging purposes, and the
  134.      * content and format of the returned string may vary between
  135.      * implementations. The returned string may be empty but may not
  136.      * be <code>null</code>.
  137.      *
  138.      * @return  a string representation of this JBox.
  139.      */
  140.     @Override
  141.     protected String paramString() { return super.paramString(); }

  142. /////////////////
  143. // Accessibility support
  144. ////////////////

  145.     /**
  146.      * Gets the AccessibleContext associated with this JBox.
  147.      * For the JBox, the AccessibleContext takes the form of an
  148.      * AccessibleJBox.
  149.      * A new AccessibleJBox instance is created if necessary.
  150.      *
  151.      * @return an AccessibleJBox that serves as the
  152.      *         AccessibleContext of this JBox
  153.      */
  154.     @Override
  155.     public AccessibleContext getAccessibleContext() {
  156.         if ( accessibleContext == null )
  157.             accessibleContext = new AccessibleJBox();

  158.         return accessibleContext;
  159.     }

  160.     /**
  161.      * This class implements accessibility support for the
  162.      * <code>JBox</code> class.  It provides an implementation of the
  163.      * Java Accessibility API appropriate to panel user-interface
  164.      * elements.
  165.      * <p>
  166.      * <strong>Warning:</strong>
  167.      * Serialized objects of this class will not be compatible with
  168.      * future Swing releases. The current serialization support is
  169.      * appropriate for short term storage or RMI between applications running
  170.      * the same version of Swing.
  171.      * has been added to the <code>java.beans</code> package.
  172.      * Please see {@link java.beans.XMLEncoder}.
  173.      */
  174.     protected class AccessibleJBox extends JComponent.AccessibleJComponent {

  175.         /**
  176.          * Get the role of this object.
  177.          *
  178.          * @return an instance of AccessibleRole describing the role of the
  179.          * object
  180.          */
  181.         @Override
  182.         public AccessibleRole getAccessibleRole() { return AccessibleRole.PANEL; }
  183.     }
  184. }