Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8132791

No access to SynthContext.getContext()

    XMLWordPrintable

    Details

    • Subcomponent:
    • Resolved In Build:
      b117
    • CPU:
      x86
    • OS:
      linux

      Description

      A DESCRIPTION OF THE REQUEST :
      When subclassing for custom UI implementation, there is no other way to create SynthContext objects than using the public constructor available in SynthContext. The SynthContext.getContext() method is not visible.

      JUSTIFICATION :
      Instantiating SynthContext via the public constructor will cause the SynthContext.dispose() method to indefinitely put these instances into its queue with no way to remove them.
      Getting SynthContext instances should be done by using the getContext() static method which is package visible.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      SynthContext.getContext() should be public.
      public constructor should be deprecated.
      ACTUAL -
      SynthContext.getContext() is package visible.

      ---------- BEGIN SOURCE ----------
      import java.awt.event.FocusListener;
      import java.lang.reflect.Method;

      import javax.swing.JComponent;
      import javax.swing.JTextField;
      import javax.swing.plaf.ComponentUI;
      import javax.swing.plaf.synth.Region;
      import javax.swing.plaf.synth.SynthConstants;
      import javax.swing.plaf.synth.SynthContext;
      import javax.swing.plaf.synth.SynthLookAndFeel;
      import javax.swing.plaf.synth.SynthStyle;
      import javax.swing.plaf.synth.SynthTextFieldUI;

      public class MyTextFieldUI extends SynthTextFieldUI {

      private int getComponentState(final JTextField c) {
      if (c.isEnabled() && c.isEditable()) {
      if (c.isFocusOwner()) {
      return SynthConstants.ENABLED | SynthConstants.FOCUSED;
      }
      return SynthConstants.ENABLED;
      }
      return SynthConstants.DISABLED;
      }

      @Override
      public SynthContext getContext(final JComponent c) {

      Region region = SynthLookAndFeel.getRegion(c);
      return new SynthContext(c, region, SynthLookAndFeel.getStyle(c, region), this.getComponentState((JTextField) c));
      }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      import java.awt.event.FocusListener;
      import java.lang.reflect.Method;

      import javax.swing.JComponent;
      import javax.swing.JTextField;
      import javax.swing.plaf.ComponentUI;
      import javax.swing.plaf.synth.Region;
      import javax.swing.plaf.synth.SynthConstants;
      import javax.swing.plaf.synth.SynthContext;
      import javax.swing.plaf.synth.SynthLookAndFeel;
      import javax.swing.plaf.synth.SynthStyle;
      import javax.swing.plaf.synth.SynthTextFieldUI;

      public class MyTextFieldUI extends SynthTextFieldUI {

      private int getComponentState(final JTextField c) {
      if (c.isEnabled() && c.isEditable()) {
      if (c.isFocusOwner()) {
      return SynthConstants.ENABLED | SynthConstants.FOCUSED;
      }
      return SynthConstants.ENABLED;
      }
      return SynthConstants.DISABLED;
      }

      @Override
      public SynthContext getContext(final JComponent c) {
      try {
      Method method = SynthContext.class.getDeclaredMethod("getContext", JComponent.class, SynthStyle.class, int.class);
      method.setAccessible(true);

      Region region = SynthLookAndFeel.getRegion(c);

      return (SynthContext) method.invoke(
      SynthContext.class,
      c,
      SynthLookAndFeel.getStyle(c, region),
      this.getComponentState((JTextField) c)
      );

      } catch (Exception e) {
      e.printStackTrace();
      }

      return null;
      }
      }

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              alexsch Alexandr Scherbatiy
              Reporter:
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: