* @param defaultValue Returned value if <code>key</code> is not available, * or is not an Integer * @return the int */ public static int getUIDefaultsInt(Object key, int defaultValue) { return getUIDefaultsInt(key, null, defaultValue); } /** * Returns an integer from the defaults table that is appropriate * for the given locale. If <code>key</code> does not map to a valid * <code>Integer</code>, or can not be convered from a <code>String</code> * to an integer, <code>default</code> is returned. * * @param key an <code>Object</code> specifying the int. Returned value * is 0 if <code>key</code> is not available, * @param l the <code>Locale</code> for which the int is desired * @param defaultValue Returned value if <code>key</code> is not available, * or is not an Integer * @return the int */ public static int getUIDefaultsInt(Object key, Locale l, int defaultValue) { Object value = UIManager.get(key, l); if (value instanceof Integer) { return ((Integer)value).intValue(); } if (value instanceof String) { try { return Integer.parseInt((String)value); } catch (NumberFormatException nfe) {} } return defaultValue; } // At this point we need this method here. But we assume that there // will be a common method for this purpose in the future releases. public static Component compositeRequestFocus(Component component) { if (component instanceof Container) { Container container = (Container)component; if (container.isFocusCycleRoot()) { FocusTraversalPolicy policy = container.getFocusTraversalPolicy(); Component comp = policy.getDefaultComponent(container); if (comp!=null) { comp.requestFocus(); return comp; } } Container rootAncestor = container.getFocusCycleRootAncestor(); if (rootAncestor!=null) { FocusTraversalPolicy policy = rootAncestor.getFocusTraversalPolicy(); Component comp = policy.getComponentAfter(rootAncestor, container); if (comp!=null && SwingUtilities.isDescendingFrom(comp, container)) { comp.requestFocus(); return comp; } } } if (component.isFocusable()) { component.requestFocus(); return component; } return null; } /** * Change focus to the visible component in {@code JTabbedPane}. * This is not a general-purpose method and is here only to permit * sharing code. */ public static boolean tabbedPaneChangeFocusTo(Component comp) { if (comp != null) { if (comp.isFocusTraversable()) { SwingUtilities2.compositeRequestFocus(comp); return true; } else if (comp instanceof JComponent && ((JComponent)comp).requestDefaultFocus()) { return true; } } return false; } /** * Submits a value-returning task for execution on the EDT and * returns a Future representing the pending results of the task. * * @param task the task to submit * @return a Future representing pending completion of the task * @throws NullPointerException if the task is null */ public static <V> Future<V> submit(Callable<V> task) { if (task == null) { throw new NullPointerException(); } FutureTask<V> future = new FutureTask<V>(task); execute(future); return future; } /** * Submits a Runnable task for execution on the EDT and returns a * Future representing that task. * * @param task the task to submit * @param result the result to return upon successful completion * @return a Future representing pending completion of the task, * and whose <tt>get()</tt> method will return the given * result value upon completion * @throws NullPointerException if the task is null */ public static <V> Future<V> submit(Runnable task, V result) { if (task == null) { throw new NullPointerException(); } FutureTask<V> future = new FutureTask<V>(task, result); execute(future); return future; } /** * Sends a Runnable to the EDT for the execution. */ private static void execute(Runnable command) { SwingUtilities.invokeLater(command); } /** * Sets the {@code SKIP_CLICK_COUNT} client property on the component * if it is an instance of {@code JTextComponent} with a * {@code DefaultCaret}. This property, used for text components acting * as editors in a table or tree, tells {@code DefaultCaret} how many * clicks to skip before starting selection. */ public static void setSkipClickCount(Component comp, int count) { if (comp instanceof JTextComponent && ((JTextComponent) comp).getCaret() instanceof DefaultCaret) { ((JTextComponent) comp).putClientProperty(SKIP_CLICK_COUNT, count); } } /** * Return the MouseEvent's click count, possibly reduced by the value of * the component's {@code SKIP_CLICK_COUNT} client property. Clears * the {@code SKIP_CLICK_COUNT} property if the mouse event's click count * is 1. In order for clearing of the property to work correctly, there * must be a mousePressed implementation on the caller with this * call as the first line. */ public static int getAdjustedClickCount(JTextComponent comp, MouseEvent e) { int cc = e.getClickCount(); if (cc == 1) { comp.putClientProperty(SKIP_CLICK_COUNT, null); } else { Integer sub = (Integer) comp.getClientProperty(SKIP_CLICK_COUNT); if (sub != null) { return cc - sub; } } return cc; } /** * Used by the {@code liesIn} method to return which section * the point lies in. * * @see #liesIn */ public enum Section { /** The leading section */ LEADING, /** The middle section */ MIDDLE, /** The trailing section */ TRAILING } /** * This method divides a rectangle into two or three sections along * the specified axis and determines which section the given point * lies in on that axis; used by drag and drop when calculating drop * locations. * <p> * For two sections, the rectangle is divided equally and the method * returns whether the point lies in {@code Section.LEADING} or * {@code Section.TRAILING}. For horizontal divisions, the calculation * respects component orientation. * <p> * For three sections, if the rectangle is greater than or equal to * 30 pixels in length along the axis, the calculation gives 10 pixels * to each of the leading and trailing sections and the remainder to the * middle. For smaller sizes, the rectangle is divided equally into three * sections. * <p> * Note: This method assumes that the point is within the bounds of * the given rectangle on the specified axis. However, in cases where * it isn't, the results still have meaning: {@code Section.MIDDLE} * remains the same, {@code Section.LEADING} indicates that the point * is in or somewhere before the leading section, and * {@code Section.TRAILING} indicates that the point is in or somewhere * after the trailing section. * * @param rect the rectangle * @param p the point the check * @param horizontal {@code true} to use the horizontal axis, * or {@code false} for the vertical axis * @param ltr {@code true} for left to right orientation, * or {@code false} for right to left orientation; * only used for horizontal calculations * @param three {@code true} for three sections, * or {@code false} for two * * @return the {@code Section} where the point lies * * @throws NullPointerException if {@code rect} or {@code p} are * {@code null} */ private static Section liesIn(Rectangle rect, Point p, boolean horizontal, boolean ltr, boolean three) { /* beginning of the rectangle on the axis */ int p0; /* point on the axis we're interested in */ int pComp; /* length of the rectangle on the axis */ int length; /* value of ltr if horizontal, else true */ boolean forward; if (horizontal) { p0 = rect.x; pComp = p.x; length = rect.width; forward = ltr; } else { p0 = rect.y; pComp = p.y; length = rect.height; forward = true; } if (three) { int boundary = (length >= 30) ? 10 : length / 3; if (pComp < p0 + boundary) { return forward ? Section.LEADING : Section.TRAILING; } else if (pComp >= p0 + length - boundary) { return forward ? Section.TRAILING : Section.LEADING; } return Section.MIDDLE; } else { int middle = p0 + length / 2; if (forward) { return pComp >= middle ? Section.TRAILING : Section.LEADING; } else { return pComp < middle ? Section.TRAILING : Section.LEADING; } } } /** * This method divides a rectangle into two or three sections along * the horizontal axis and determines which section the given point * lies in; used by drag and drop when calculating drop locations. * <p> * See the documentation for {@link #liesIn} for more information * on how the section is calculated. * * @param rect the rectangle * @param p the point the check * @param ltr {@code true} for left to right orientation, * or {@code false} for right to left orientation * @param three {@code true} for three sections, * or {@code false} for two * * @return the {@code Section} where the point lies * * @throws NullPointerException if {@code rect} or {@code p} are * {@code null} */ public static Section liesInHorizontal(Rectangle rect, Point p, boolean ltr, boolean three) { return liesIn(rect, p, true, ltr, three); } /** * This method divides a rectangle into two or three sections along * the vertical axis and determines which section the given point * lies in; used by drag and drop when calculating drop locations. * <p> * See the documentation for {@link #liesIn} for more information * on how the section is calculated. * * @param rect the rectangle * @param p the point the check * @param three {@code true} for three sections, * or {@code false} for two * * @return the {@code Section} where the point lies * * @throws NullPointerException if {@code rect} or {@code p} are * {@code null} */ public static Section liesInVertical(Rectangle rect, Point p, boolean three) { return liesIn(rect, p, false, false, three); } /** * Maps the index of the column in the view at * {@code viewColumnIndex} to the index of the column * in the table model. Returns the index of the corresponding * column in the model. If {@code viewColumnIndex} * is less than zero, returns {@code viewColumnIndex}. * * @param cm the table model * @param viewColumnIndex the index of the column in the view * @return the index of the corresponding column in the model * * @see JTable#convertColumnIndexToModel(int) * @see javax.swing.plaf.basic.BasicTableHeaderUI */ public static int convertColumnIndexToModel(TableColumnModel cm, int viewColumnIndex) { if (viewColumnIndex < 0) { return viewColumnIndex; } return cm.getColumn(viewColumnIndex).getModelIndex(); } /** * Maps the index of the column in the {@code cm} at * {@code modelColumnIndex} to the index of the column * in the view. Returns the index of the * corresponding column in the view; returns {@code -1} if this column * is not being displayed. If {@code modelColumnIndex} is less than zero, * returns {@code modelColumnIndex}. * * @param cm the table model * @param modelColumnIndex the index of the column in the model * @return the index of the corresponding column in the view * * @see JTable#convertColumnIndexToView(int) * @see javax.swing.plaf.basic.BasicTableHeaderUI */ public static int convertColumnIndexToView(TableColumnModel cm, int modelColumnIndex) { if (modelColumnIndex < 0) { return modelColumnIndex; } for (int column = 0; column < cm.getColumnCount(); column++) { if (cm.getColumn(column).getModelIndex() == modelColumnIndex) { return column; } } return -1; }}Lines 1501–1871 of 1871. Previous lines
* @param defaultValue Returned value if <code>key</code> is not available, * or is not an Integer * @return the int */ public static int getUIDefaultsInt(Object key, int defaultValue) { return getUIDefaultsInt(key, null, defaultValue); } /** * Returns an integer from the defaults table that is appropriate * for the given locale. If <code>key</code> does not map to a valid * <code>Integer</code>, or can not be convered from a <code>String</code> * to an integer, <code>default</code> is returned. * * @param key an <code>Object</code> specifying the int. Returned value * is 0 if <code>key</code> is not available, * @param l the <code>Locale</code> for which the int is desired * @param defaultValue Returned value if <code>key</code> is not available, * or is not an Integer * @return the int */ public static int getUIDefaultsInt(Object key, Locale l, int defaultValue) { Object value = UIManager.get(key, l); if (value instanceof Integer) { return ((Integer)value).intValue(); } if (value instanceof String) { try { return Integer.parseInt((String)value); } catch (NumberFormatException nfe) {} } return defaultValue; } // At this point we need this method here. But we assume that there // will be a common method for this purpose in the future releases. public static Component compositeRequestFocus(Component component) { if (component instanceof Container) { Container container = (Container)component; if (container.isFocusCycleRoot()) { FocusTraversalPolicy policy = container.getFocusTraversalPolicy(); Component comp = policy.getDefaultComponent(container); if (comp!=null) { comp.requestFocus(); return comp; } } Container rootAncestor = container.getFocusCycleRootAncestor(); if (rootAncestor!=null) { FocusTraversalPolicy policy = rootAncestor.getFocusTraversalPolicy(); Component comp = policy.getComponentAfter(rootAncestor, container); if (comp!=null && SwingUtilities.isDescendingFrom(comp, container)) { comp.requestFocus(); return comp; } } } if (component.isFocusable()) { component.requestFocus(); return component; } return null; } /** * Change focus to the visible component in {@code JTabbedPane}. * This is not a general-purpose method and is here only to permit * sharing code. */ public static boolean tabbedPaneChangeFocusTo(Component comp) { if (comp != null) { if (comp.isFocusTraversable()) { SwingUtilities2.compositeRequestFocus(comp); return true; } else if (comp instanceof JComponent && ((JComponent)comp).requestDefaultFocus()) { return true; } } return false; } /** * Submits a value-returning task for execution on the EDT and * returns a Future representing the pending results of the task. * * @param task the task to submit * @return a Future representing pending completion of the task * @throws NullPointerException if the task is null */ public static <V> Future<V> submit(Callable<V> task) { if (task == null) { throw new NullPointerException(); } FutureTask<V> future = new FutureTask<V>(task); execute(future); return future; } /** * Submits a Runnable task for execution on the EDT and returns a * Future representing that task. * * @param task the task to submit * @param result the result to return upon successful completion * @return a Future representing pending completion of the task, * and whose <tt>get()</tt> method will return the given * result value upon completion * @throws NullPointerException if the task is null */ public static <V> Future<V> submit(Runnable task, V result) { if (task == null) { throw new NullPointerException(); } FutureTask<V> future = new FutureTask<V>(task, result); execute(future); return future; } /** * Sends a Runnable to the EDT for the execution. */ private static void execute(Runnable command) { SwingUtilities.invokeLater(command); } /** * Sets the {@code SKIP_CLICK_COUNT} client property on the component * if it is an instance of {@code JTextComponent} with a * {@code DefaultCaret}. This property, used for text components acting * as editors in a table or tree, tells {@code DefaultCaret} how many * clicks to skip before starting selection. */ public static void setSkipClickCount(Component comp, int count) { if (comp instanceof JTextComponent && ((JTextComponent) comp).getCaret() instanceof DefaultCaret) { ((JTextComponent) comp).putClientProperty(SKIP_CLICK_COUNT, count); } } /** * Return the MouseEvent's click count, possibly reduced by the value of * the component's {@code SKIP_CLICK_COUNT} client property. Clears * the {@code SKIP_CLICK_COUNT} property if the mouse event's click count * is 1. In order for clearing of the property to work correctly, there * must be a mousePressed implementation on the caller with this * call as the first line. */ public static int getAdjustedClickCount(JTextComponent comp, MouseEvent e) { int cc = e.getClickCount(); if (cc == 1) { comp.putClientProperty(SKIP_CLICK_COUNT, null); } else { Integer sub = (Integer) comp.getClientProperty(SKIP_CLICK_COUNT); if (sub != null) { return cc - sub; } } return cc; } /** * Used by the {@code liesIn} method to return which section * the point lies in. * * @see #liesIn */ public enum Section { /** The leading section */ LEADING, /** The middle section */ MIDDLE, /** The trailing section */ TRAILING } /** * This method divides a rectangle into two or three sections along * the specified axis and determines which section the given point * lies in on that axis; used by drag and drop when calculating drop * locations. * <p> * For two sections, the rectangle is divided equally and the method * returns whether the point lies in {@code Section.LEADING} or * {@code Section.TRAILING}. For horizontal divisions, the calculation * respects component orientation. * <p> * For three sections, if the rectangle is greater than or equal to * 30 pixels in length along the axis, the calculation gives 10 pixels * to each of the leading and trailing sections and the remainder to the * middle. For smaller sizes, the rectangle is divided equally into three * sections. * <p> * Note: This method assumes that the point is within the bounds of * the given rectangle on the specified axis. However, in cases where * it isn't, the results still have meaning: {@code Section.MIDDLE} * remains the same, {@code Section.LEADING} indicates that the point * is in or somewhere before the leading section, and * {@code Section.TRAILING} indicates that the point is in or somewhere * after the trailing section. * * @param rect the rectangle * @param p the point the check * @param horizontal {@code true} to use the horizontal axis, * or {@code false} for the vertical axis * @param ltr {@code true} for left to right orientation, * or {@code false} for right to left orientation; * only used for horizontal calculations * @param three {@code true} for three sections, * or {@code false} for two * * @return the {@code Section} where the point lies * * @throws NullPointerException if {@code rect} or {@code p} are * {@code null} */ private static Section liesIn(Rectangle rect, Point p, boolean horizontal, boolean ltr, boolean three) { /* beginning of the rectangle on the axis */ int p0; /* point on the axis we're interested in */ int pComp; /* length of the rectangle on the axis */ int length; /* value of ltr if horizontal, else true */ boolean forward; if (horizontal) { p0 = rect.x; pComp = p.x; length = rect.width; forward = ltr; } else { p0 = rect.y; pComp = p.y; length = rect.height; forward = true; } if (three) { int boundary = (length >= 30) ? 10 : length / 3; if (pComp < p0 + boundary) { return forward ? Section.LEADING : Section.TRAILING; } else if (pComp >= p0 + length - boundary) { return forward ? Section.TRAILING : Section.LEADING; } return Section.MIDDLE; } else { int middle = p0 + length / 2; if (forward) { return pComp >= middle ? Section.TRAILING : Section.LEADING; } else { return pComp < middle ? Section.TRAILING : Section.LEADING; } } } /** * This method divides a rectangle into two or three sections along * the horizontal axis and determines which section the given point * lies in; used by drag and drop when calculating drop locations. * <p> * See the documentation for {@link #liesIn} for more information * on how the section is calculated. * * @param rect the rectangle * @param p the point the check * @param ltr {@code true} for left to right orientation, * or {@code false} for right to left orientation * @param three {@code true} for three sections, * or {@code false} for two * * @return the {@code Section} where the point lies * * @throws NullPointerException if {@code rect} or {@code p} are * {@code null} */ public static Section liesInHorizontal(Rectangle rect, Point p, boolean ltr, boolean three) { return liesIn(rect, p, true, ltr, three); } /** * This method divides a rectangle into two or three sections along * the vertical axis and determines which section the given point * lies in; used by drag and drop when calculating drop locations. * <p> * See the documentation for {@link #liesIn} for more information * on how the section is calculated. * * @param rect the rectangle * @param p the point the check * @param three {@code true} for three sections, * or {@code false} for two * * @return the {@code Section} where the point lies * * @throws NullPointerException if {@code rect} or {@code p} are * {@code null} */ public static Section liesInVertical(Rectangle rect, Point p, boolean three) { return liesIn(rect, p, false, false, three); } /** * Maps the index of the column in the view at * {@code viewColumnIndex} to the index of the column * in the table model. Returns the index of the corresponding * column in the model. If {@code viewColumnIndex} * is less than zero, returns {@code viewColumnIndex}. * * @param cm the table model * @param viewColumnIndex the index of the column in the view * @return the index of the corresponding column in the model * * @see JTable#convertColumnIndexToModel(int) * @see javax.swing.plaf.basic.BasicTableHeaderUI */ public static int convertColumnIndexToModel(TableColumnModel cm, int viewColumnIndex) { if (viewColumnIndex < 0) { return viewColumnIndex; } return cm.getColumn(viewColumnIndex).getModelIndex(); } /** * Maps the index of the column in the {@code cm} at * {@code modelColumnIndex} to the index of the column * in the view. Returns the index of the * corresponding column in the view; returns {@code -1} if this column * is not being displayed. If {@code modelColumnIndex} is less than zero, * returns {@code modelColumnIndex}. * * @param cm the table model * @param modelColumnIndex the index of the column in the model * @return the index of the corresponding column in the view * * @see JTable#convertColumnIndexToView(int) * @see javax.swing.plaf.basic.BasicTableHeaderUI */ public static int convertColumnIndexToView(TableColumnModel cm, int modelColumnIndex) { if (modelColumnIndex < 0) { return modelColumnIndex; } for (int column = 0; column < cm.getColumnCount(); column++) { if (cm.getColumn(column).getModelIndex() == modelColumnIndex) { return column; } } return -1; } }