Class Util


  • public final class Util
    extends Object
    A utility class that handles localized Strings, display of error messages and so on. Developers extending the viewer are welcome to use this class

    This code is copyright the Big Faceless Organization. You're welcome to use, modify and distribute it in any form in your own projects, provided those projects continue to make use of the Big Faceless PDF library.

    Since:
    2.11 - created from the SuperJOptionPane class
    • Field Detail

      • PACKAGE

        public static final String PACKAGE
        The package the PDFViewer belongs to, which is "org.faceless.pdf2.viewer2"
      • LOGGER

        public static final BFOLogger LOGGER
        The BFOLogger instance for the viewer
      • JAVA_MAJOR_VERSION

        public static final int JAVA_MAJOR_VERSION
        The Java major version, i.e. 5, 6, 7, 8 or 9 at the time of writing
      • JAVA_MINOR_VERSION

        public static final int JAVA_MINOR_VERSION
        The Java minor version
    • Method Detail

      • displayThrowable

        public static void displayThrowable​(Throwable throwable,
                                            Component parent)
        Display an Error message, including the stack trace
        Parameters:
        throwable - the Throwable object this error relates to
        parent - the parent component (not used)
      • isLAFFlat

        public static boolean isLAFFlat()
        Return true if the LAF uses the Flat LaF toolkit
      • isLAFAqua

        public static boolean isLAFAqua()
        Return true if the LAF uses the Aqua toolkit (OS X)
      • isLAFGTK

        public static boolean isLAFGTK()
        Return true if the LAF uses the GTK+ toolkit (UNIX)
      • isLAFNimbus

        public static boolean isLAFNimbus()
        Return true if the LAF uses the Nimbus toolkit
      • isLAFWindows

        public static boolean isLAFWindows()
        Return true if the LAF uses the Window toolkit
      • isLAFMetal

        public static boolean isLAFMetal()
        Return true if the LAF uses the Metal toolkit
      • isJavaFX

        public static boolean isJavaFX​(Component o)
        Return true if the specified component is contained within a JavaFX component
        Parameters:
        o - the Component
        Since:
        2.18.8
      • isJNLP

        public static boolean isJNLP​(Component c)
        Return true if the specified component is running in a JNLP environment, false otherwise
        Parameters:
        c - the Component
        Since:
        2.11.1
      • openURL

        public static boolean openURL​(URL url,
                                      Component c)
        Display a URL in a web browser, if possible
        Parameters:
        url - the URL to open
        c - the Component
        Returns:
        true if the URL could be opened, false otherwise
        Since:
        2.11.1
      • getUIString

        public static String getUIString​(String key)
        Return a UIManager string field. Identical to getUIString except this method never returns null - if the String isn't found, the key is returned.
        Since:
        2.16.2
      • getUIString

        public static String getUIString​(String key,
                                         String field1)
        Return a UIManager string field with the specified fields replaced
        Parameters:
        key - the message key,
        field1 - the field to be substituted for {1}
        Returns:
        the modified string
      • getUIString

        public static String getUIString​(String key,
                                         String field1,
                                         String field2)
        Return a UIManager string field with the specified fields replaced
        Parameters:
        key - the message key,
        field1 - the field to be substituted for {1}
        field2 - the field to be substituted for {2}
        Returns:
        the modified string
      • getUIString

        public static String getUIString​(String key,
                                         String field1,
                                         String field2,
                                         String field3)
        Return a UIManager string field with the specified fields replaced
        Parameters:
        key - the message key,
        field1 - the field to be substituted for {1}
        field2 - the field to be substituted for {2}
        Returns:
        the modified string
      • fixScrollPaneKeyBindings

        public static void fixScrollPaneKeyBindings​(JScrollPane scrollpane)
        Remove the pageup/pagedown key bindings from ScrollPanes - they're clashing with the pageup/pagedown we're mapping on the DocumentPanel
      • toURL

        public static URL toURL​(DocumentPanel docpanel,
                                String path)
        Attempt to convert a relative URL to an absolute one. URL is assumed to be relative to the file.
        Parameters:
        docpanel - the document panel
        path - the URL path
      • hasFilePermission

        public static boolean hasFilePermission()
        Return true if the viewer can access the local filesystem
      • encodeColor

        public static String encodeColor​(Color c)
        Return the standard #RRGGBB representation of a Color
      • getScreenResolution

        public static int getScreenResolution​(Component comp)
        Get the screen resolution. Same as Toolkit.getScreenResolution, except it allows us to override it using the PDFViewer "dpi" property
      • newJDialog

        public static JDialog newJDialog​(Component comp,
                                         String title,
                                         boolean modal)

        Create a dialog for the specified component. This will search up the Swing hierarchy until the first Frame, Dialog or Window is found and return a dialog on that object. This caters for situations where the viewer itself is running in a JDialog.

        Note the JDialog(Window) constructor will do the same, but that's Java 6 only.

        Parameters:
        comp - the Component creating the dialog
        title - the title of the dialog, or null for no title
        modal - whether the dialog is to be modal or not.
        Since:
        2.13.1
      • fixFileChooser

        public static JFileChooser fixFileChooser​(JFileChooser chooser)
        Attempt to repair a reported bug in the AWT classes where Icons returned from this class a null, causing a NPE deep inside the JFileChooser UI classes.
      • createTransferHandler

        public static TransferHandler createTransferHandler​(PDFViewer viewer)
        Return the TransferHandler to use when copying/pasting text. By default this is a RichTextTransferHandler, but this can be overridden by setting the property "TransferHandler" on the PDFViewer PropertyManager. to the full class-name of the TransferHandler.
        Since:
        2.16.1
      • setAutoFocusComponent

        public static void setAutoFocusComponent​(JComponent component)
        Set the supplied component to gain focus when it's newly displayed, even inside a sub-pane in a popup window.
        Parameters:
        component - the component
      • patchJFileChooser

        public static boolean patchJFileChooser​(JFileChooser chooser,
                                                JComponent patch,
                                                boolean add)
        Insert an "Option pane" into the JFileChooser. This has to be done differently for each look and feel - if it can't be done, this method will return false. Don't blame us for this horror, blame Sun.
        Since:
        2.16.2
      • getJFileChooserFileName

        public static JTextField getJFileChooserFileName​(JFileChooser chooser)
        Return the JTextField containing the current filename from the JFileChooser, or null if this can't be determined for the current look-and-feel.
        Since:
        2.16.2
      • getRootAncestor

        public static JComponent getRootAncestor​(JComponent c)
        Get the top-level JComponent above the specified one - typically a JWindow or JFrame
      • setCanUseComposites

        public static void setCanUseComposites​(boolean comp)
      • canUseComposites

        public static boolean canUseComposites()
        Return true if the AWT can use custom Composites. OSX and Ubuntu can't up to and including Java 8, they throw an "Not Implemented" InternalError.
      • installUIDefaults

        public static void installUIDefaults​(String property)
        Install a property file that controls the appearance of the viewer. This uses the same syntax as the Property files from FlatLAF.
        Since:
        2.26
      • setFontScale

        public static void setFontScale​(float scale)

        Scale all the fonts used in Swing by the specified scale. Utility method which may be called any time from any code, but specifically will be called if the "fontScale" property is set on the viewer.

        Inspired by this StackOverflow posting.

        Parameters:
        scale - the font scale
      • getMenuShortcutKeyMask

        public static int getMenuShortcutKeyMask​(JComponent c)