modules/controls/src/main/java/com/sun/javafx/scene/control/skin/Utils.java

Print this page
rev 9240 : 8076423: JEP 253: Prepare JavaFX UI Controls & CSS APIs for Modularization

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.  Oracle designates this

@@ -28,41 +28,48 @@
  * and open the template in the editor.
  */
 
 package com.sun.javafx.scene.control.skin;
 
-import java.text.Bidi;
-import java.text.BreakIterator;
-import java.util.function.Consumer;
-
-import static javafx.scene.control.OverrunStyle.*;
-import javafx.application.Platform;
+import com.sun.javafx.scene.control.behavior.TextBinding;
+import com.sun.javafx.scene.text.TextLayout;
+import com.sun.javafx.tk.Toolkit;
 import javafx.application.ConditionalFeature;
+import javafx.application.Platform;
 import javafx.beans.InvalidationListener;
 import javafx.beans.Observable;
-import javafx.beans.property.Property;
 import javafx.beans.value.ObservableValue;
 import javafx.collections.ObservableList;
 import javafx.geometry.Bounds;
 import javafx.geometry.HPos;
 import javafx.geometry.Point2D;
 import javafx.geometry.VPos;
 import javafx.scene.Scene;
 import javafx.scene.control.ContextMenu;
 import javafx.scene.control.MenuItem;
 import javafx.scene.control.OverrunStyle;
+import com.sun.javafx.scene.control.ContextMenuContent;
 import javafx.scene.input.KeyCombination;
 import javafx.scene.input.Mnemonic;
+import javafx.scene.paint.Color;
 import javafx.scene.text.Font;
 import javafx.scene.text.Text;
 import javafx.scene.text.TextBoundsType;
 
-import com.sun.javafx.scene.control.behavior.TextBinding;
-import com.sun.javafx.scene.text.HitInfo;
-import com.sun.javafx.scene.text.TextLayout;
-import com.sun.javafx.tk.Toolkit;
-import javafx.util.Callback;
+import java.text.Bidi;
+import java.text.BreakIterator;
+import java.util.Locale;
+import java.util.function.Consumer;
+
+import static javafx.scene.control.OverrunStyle.CENTER_ELLIPSIS;
+import static javafx.scene.control.OverrunStyle.CENTER_WORD_ELLIPSIS;
+import static javafx.scene.control.OverrunStyle.CLIP;
+import static javafx.scene.control.OverrunStyle.ELLIPSIS;
+import static javafx.scene.control.OverrunStyle.LEADING_ELLIPSIS;
+import static javafx.scene.control.OverrunStyle.LEADING_WORD_ELLIPSIS;
+import static javafx.scene.control.OverrunStyle.WORD_ELLIPSIS;
+import static javafx.scene.control.skin.TextFieldSkin.TextPosInfo;
 
 /**
  * BE REALLY CAREFUL WITH RESTORING OR RESETTING STATE OF helper NODE AS LEFTOVER
  * STATE CAUSES REALLY ODD NASTY BUGS!
  *

@@ -86,11 +93,11 @@
      *
      * Note: This code assumes that TextBoundsType#VISUAL is never used by controls.
      * */
     static final TextLayout layout = Toolkit.getToolkit().getTextLayoutFactory().createLayout();
 
-    static double getAscent(Font font, TextBoundsType boundsType) {
+    public static double getAscent(Font font, TextBoundsType boundsType) {
         layout.setContent("", font.impl_getNativeFont());
         layout.setWrapWidth(0);
         layout.setLineSpacing(0);
         if (boundsType == TextBoundsType.LOGICAL_VERTICAL_CENTER) {
             layout.setBoundsType(TextLayout.BOUNDS_CENTER);

@@ -98,11 +105,11 @@
             layout.setBoundsType(0);
         }
         return -layout.getBounds().getMinY();
     }
 
-    static double getLineHeight(Font font, TextBoundsType boundsType) {
+    public static double getLineHeight(Font font, TextBoundsType boundsType) {
         layout.setContent("", font.impl_getNativeFont());
         layout.setWrapWidth(0);
         layout.setLineSpacing(0);
         if (boundsType == TextBoundsType.LOGICAL_VERTICAL_CENTER) {
             layout.setBoundsType(TextLayout.BOUNDS_CENTER);

@@ -112,22 +119,22 @@
 
         // RT-37092: Use the line bounds specifically, to include font leading.
         return layout.getLines()[0].getBounds().getHeight();
     }
 
-    static double computeTextWidth(Font font, String text, double wrappingWidth) {
+    public static double computeTextWidth(Font font, String text, double wrappingWidth) {
         layout.setContent(text != null ? text : "", font.impl_getNativeFont());
         layout.setWrapWidth((float)wrappingWidth);
         return layout.getBounds().getWidth();
     }
 
-    static double computeTextHeight(Font font, String text, double wrappingWidth, TextBoundsType boundsType) {
+    public static double computeTextHeight(Font font, String text, double wrappingWidth, TextBoundsType boundsType) {
         return computeTextHeight(font, text, wrappingWidth, 0, boundsType);
     }
 
     @SuppressWarnings("deprecation")
-    static double computeTextHeight(Font font, String text, double wrappingWidth, double lineSpacing, TextBoundsType boundsType) {
+    public static double computeTextHeight(Font font, String text, double wrappingWidth, double lineSpacing, TextBoundsType boundsType) {
         layout.setContent(text != null ? text : "", font.impl_getNativeFont());
         layout.setWrapWidth((float)wrappingWidth);
         layout.setLineSpacing((float)lineSpacing);
         if (boundsType == TextBoundsType.LOGICAL_VERTICAL_CENTER) {
             layout.setBoundsType(TextLayout.BOUNDS_CENTER);

@@ -135,11 +142,11 @@
             layout.setBoundsType(0);
         }
         return layout.getBounds().getHeight();
     }
 
-    static int computeTruncationIndex(Font font, String text, double width) {
+    public static int computeTruncationIndex(Font font, String text, double width) {
         helper.setText(text);
         helper.setFont(font);
         helper.setWrappingWidth(0);
         helper.setLineSpacing(0);
         // The -2 is a fudge to make sure the result more often matches

@@ -153,11 +160,11 @@
         helper.setLineSpacing(DEFAULT_LINE_SPACING);
         helper.setText(DEFAULT_TEXT);
         return index;
     }
 
-    static String computeClippedText(Font font, String text, double width,
+    public static String computeClippedText(Font font, String text, double width,
                                      OverrunStyle type, String ellipsisString) {
         if (font == null) {
             throw new IllegalArgumentException("Must specify a font");
         }
         OverrunStyle style = (type == null || type == CLIP) ? ELLIPSIS : type;

@@ -354,11 +361,11 @@
                 return leading + ellipsis + trailing;
             }
         }
     }
 
-    static String computeClippedWrappedText(Font font, String text, double width,
+    public static String computeClippedWrappedText(Font font, String text, double width,
                                             double height, OverrunStyle truncationStyle,
                                             String ellipsisString, TextBoundsType boundsType) {
         if (font == null) {
             throw new IllegalArgumentException("Must specify a font");
         }

@@ -632,15 +639,15 @@
         // if min > value, return min
         // if min > max, return min
         return Math.min(Math.max(value, min), Math.max(min,max));
     }
 
-    static void addMnemonics(ContextMenu popup, Scene scene) {
+    public static void addMnemonics(ContextMenu popup, Scene scene) {
         addMnemonics(popup, scene, false);
     }
 
-    static void addMnemonics(ContextMenu popup, Scene scene, boolean initialState) {
+    public static void addMnemonics(ContextMenu popup, Scene scene, boolean initialState) {
 
         if (!com.sun.javafx.PlatformUtil.isMac()) {
 
             ContextMenuContent cmContent = (ContextMenuContent)popup.getSkin().getNode();
             MenuItem menuitem;

@@ -665,11 +672,11 @@
         }
     }
 
 
 
-    static void removeMnemonics(ContextMenu popup, Scene scene) {
+    public static void removeMnemonics(ContextMenu popup, Scene scene) {
 
         if (!com.sun.javafx.PlatformUtil.isMac()) {
 
             ContextMenuContent cmContent = (ContextMenuContent)popup.getSkin().getNode();
             MenuItem menuitem;

@@ -698,11 +705,11 @@
                 }
             }
         }
     }
 
-    static double computeXOffset(double width, double contentWidth, HPos hpos) {
+    public static double computeXOffset(double width, double contentWidth, HPos hpos) {
         if (hpos == null) {
             return 0;
         }
 
         switch(hpos) {

@@ -715,11 +722,11 @@
             default:
                 return 0;
         }
     }
 
-    static double computeYOffset(double height, double contentHeight, VPos vpos) {
+    public static double computeYOffset(double height, double contentHeight, VPos vpos) {
         if (vpos == null) {
             return 0;
         }
 
         switch(vpos) {

@@ -749,11 +756,11 @@
 
 
     // Workaround for RT-26961. HitInfo.getInsertionIndex() doesn't skip
     // complex character clusters / ligatures.
     private static BreakIterator charIterator = null;
-    public static int getHitInsertionIndex(HitInfo hit, String text) {
+    public static int getHitInsertionIndex(TextPosInfo hit, String text) {
         int charIndex = hit.getCharIndex();
         if (text != null && !hit.isLeading()) {
             if (charIterator == null) {
                 charIterator = BreakIterator.getCharacterInstance();
             }

@@ -788,6 +795,17 @@
                 }
             };
             p.addListener(listener);
         }
     }
+
+    public static String formatHexString(Color c) {
+        if (c != null) {
+            return String.format((Locale) null, "#%02x%02x%02x",
+                    Math.round(c.getRed() * 255),
+                    Math.round(c.getGreen() * 255),
+                    Math.round(c.getBlue() * 255));
+        } else {
+            return null;
+        }
+    }
 }