1/*
2 * Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package apple.laf;
27
28import java.security.AccessController;
29
30import apple.laf.JRSUIConstants.Hit;
31import apple.laf.JRSUIConstants.ScrollBarPart;
32import com.apple.laf.AquaImageFactory.NineSliceMetrics;
33import sun.security.action.GetPropertyAction;
34
35public final class JRSUIUtils {
36
37    static boolean isLeopard = isMacOSXLeopard();
38    static boolean isSnowLeopardOrBelow = isMacOSXSnowLeopardOrBelow();
39
40    static boolean isMacOSXLeopard() {
41        return isCurrentMacOSXVersion(5);
42    }
43
44    static boolean isMacOSXSnowLeopardOrBelow() {
45        return currentMacOSXVersionMatchesGivenVersionRange(6, true, true, false);
46    }
47
48    static boolean isCurrentMacOSXVersion(final int version) {
49        return currentMacOSXVersionMatchesGivenVersionRange(version, true, false, false);
50    }
51
52    static boolean currentMacOSXVersionMatchesGivenVersionRange(
53            final int version, final boolean inclusive,
54            final boolean matchBelow, final boolean matchAbove) {
55        // split the "10.x.y" version number
56        String osVersion = AccessController.doPrivileged(new GetPropertyAction("os.version"));
57        String[] fragments = osVersion.split("\\.");
58
59        // sanity check the "10." part of the version
60        if (!fragments[0].equals("10")) return false;
61        if (fragments.length < 2) return false;
62
63        // check if os.version matches the given version using the given match method
64        try {
65            int minorVers = Integer.parseInt(fragments[1]);
66
67            if (inclusive && minorVers == version) return true;
68            if (matchBelow && minorVers < version) return true;
69            if (matchAbove && minorVers > version) return true;
70
71        } catch (NumberFormatException e) {
72            // was not an integer
73        }
74        return false;
75    }
76
77    public static class TabbedPane {
78        public static boolean useLegacyTabs() {
79            return isLeopard;
80        }
81        public static boolean shouldUseTabbedPaneContrastUI() {
82            return !isSnowLeopardOrBelow;
83        }
84    }
85
86    public static class InternalFrame {
87        public static boolean shouldUseLegacyBorderMetrics() {
88            return isSnowLeopardOrBelow;
89        }
90    }
91
92    public static class Tree {
93        public static boolean useLegacyTreeKnobs() {
94            return isLeopard;
95        }
96    }
97
98    public static class ScrollBar {
99        private static native boolean shouldUseScrollToClick();
100
101        public static boolean useScrollToClick() {
102            return shouldUseScrollToClick();
103        }
104
105        public static void getPartBounds(final double[] rect,
106                                         final JRSUIControl control,
107                                         final int x, final int y, final int w,
108                                         final int h,
109                                         final ScrollBarPart part) {
110            control.getPartBounds(rect, x, y, w, h, part.ordinal);
111        }
112
113        public static double getNativeOffsetChange(final JRSUIControl control,
114                                                   final int x, final int y,
115                                                   final int w, final int h,
116                                                   final int offset,
117                                                   final int visibleAmount,
118                                                   final int extent) {
119            return control.getScrollBarOffsetChange(x, y, w, h, offset,
120                                                    visibleAmount, extent);
121        }
122    }
123
124    public static class Images {
125        public static boolean shouldUseLegacySecurityUIPath() {
126            return isSnowLeopardOrBelow;
127        }
128    }
129
130    public static class HitDetection {
131        public static Hit getHitForPoint(final JRSUIControl control,
132                                         final int x, final int y, final int w,
133                                         final int h, final int hitX,
134                                         final int hitY) {
135            return control.getHitForPoint(x, y, w, h, hitX, hitY);
136        }
137    }
138
139    public interface NineSliceMetricsProvider {
140        public NineSliceMetrics getNineSliceMetricsForState(JRSUIState state);
141    }
142}
143