author | alexp |
Tue, 25 May 2010 20:54:59 +0400 | |
changeset 5590 | 3ec077d7e893 |
parent 4278 | b7a976422d27 |
child 5597 | ab490f66d2cf |
permissions | -rw-r--r-- |
2 | 1 |
/* |
1639 | 2 |
* Copyright 2002-2008 Sun Microsystems, Inc. All Rights Reserved. |
2 | 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. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
||
26 |
package sun.swing; |
|
27 |
||
28 |
import java.security.*; |
|
29 |
import java.lang.reflect.*; |
|
30 |
import java.awt.*; |
|
31 |
import static java.awt.RenderingHints.*; |
|
32 |
import java.awt.event.*; |
|
33 |
import java.awt.font.*; |
|
34 |
import java.awt.geom.*; |
|
35 |
import java.awt.print.PrinterGraphics; |
|
36 |
import java.text.Bidi; |
|
37 |
import java.text.AttributedCharacterIterator; |
|
38 |
import java.text.AttributedString; |
|
39 |
||
40 |
import javax.swing.*; |
|
41 |
import javax.swing.plaf.*; |
|
42 |
import javax.swing.text.Highlighter; |
|
43 |
import javax.swing.text.JTextComponent; |
|
44 |
import javax.swing.text.DefaultHighlighter; |
|
45 |
import javax.swing.text.DefaultCaret; |
|
46 |
import javax.swing.table.TableCellRenderer; |
|
5590
3ec077d7e893
6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents:
4278
diff
changeset
|
47 |
import javax.swing.table.TableColumnModel; |
3ec077d7e893
6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents:
4278
diff
changeset
|
48 |
|
2 | 49 |
import sun.swing.PrintColorUIResource; |
50 |
import sun.swing.ImageIconUIResource; |
|
51 |
import sun.print.ProxyPrintGraphics; |
|
52 |
import sun.awt.*; |
|
53 |
import sun.security.action.GetPropertyAction; |
|
54 |
import sun.security.util.SecurityConstants; |
|
55 |
import java.io.*; |
|
56 |
import java.util.*; |
|
57 |
import sun.font.FontDesignMetrics; |
|
3928 | 58 |
import sun.font.FontUtilities; |
1724
a22a286aa16f
6748082: remove platform-specific code from SwingUtilities2.isDisplayLocal
tdv
parents:
2
diff
changeset
|
59 |
import sun.java2d.SunGraphicsEnvironment; |
2 | 60 |
|
61 |
import java.util.concurrent.Callable; |
|
62 |
import java.util.concurrent.Future; |
|
63 |
import java.util.concurrent.FutureTask; |
|
64 |
||
65 |
/** |
|
66 |
* A collection of utility methods for Swing. |
|
67 |
* <p> |
|
68 |
* <b>WARNING:</b> While this class is public, it should not be treated as |
|
69 |
* public API and its API may change in incompatable ways between dot dot |
|
70 |
* releases and even patch releases. You should not rely on this class even |
|
71 |
* existing. |
|
72 |
* |
|
73 |
*/ |
|
74 |
public class SwingUtilities2 { |
|
75 |
/** |
|
76 |
* The <code>AppContext</code> key for our one <code>LAFState</code> |
|
77 |
* instance. |
|
78 |
*/ |
|
79 |
public static final Object LAF_STATE_KEY = |
|
80 |
new StringBuffer("LookAndFeel State"); |
|
81 |
||
3749
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
82 |
// Maintain a cache of CACHE_SIZE fonts and the left side bearing |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
83 |
// of the characters falling into the range MIN_CHAR_INDEX to |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
84 |
// MAX_CHAR_INDEX. The values in fontCache are created as needed. |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
85 |
private static LSBCacheEntry[] fontCache; |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
86 |
// Windows defines 6 font desktop properties, we will therefore only |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
87 |
// cache the metrics for 6 fonts. |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
88 |
private static final int CACHE_SIZE = 6; |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
89 |
// nextIndex in fontCache to insert a font into. |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
90 |
private static int nextIndex; |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
91 |
// LSBCacheEntry used to search in fontCache to see if we already |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
92 |
// have an entry for a particular font |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
93 |
private static LSBCacheEntry searchKey; |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
94 |
|
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
95 |
// getLeftSideBearing will consult all characters that fall in the |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
96 |
// range MIN_CHAR_INDEX to MAX_CHAR_INDEX. |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
97 |
private static final int MIN_CHAR_INDEX = (int)'W'; |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
98 |
private static final int MAX_CHAR_INDEX = (int)'W' + 1; |
2 | 99 |
|
100 |
public static final FontRenderContext DEFAULT_FRC = |
|
101 |
new FontRenderContext(null, false, false); |
|
102 |
||
103 |
/** |
|
104 |
* A JComponent client property is used to determine text aa settings. |
|
105 |
* To avoid having this property persist between look and feels changes |
|
106 |
* the value of the property is set to null in JComponent.setUI |
|
107 |
*/ |
|
108 |
public static final Object AA_TEXT_PROPERTY_KEY = |
|
109 |
new StringBuffer("AATextInfoPropertyKey"); |
|
110 |
||
111 |
/** |
|
112 |
* Used to tell a text component, being used as an editor for table |
|
113 |
* or tree, how many clicks it took to start editing. |
|
114 |
*/ |
|
115 |
private static final StringBuilder SKIP_CLICK_COUNT = |
|
116 |
new StringBuilder("skipClickCount"); |
|
117 |
||
118 |
/* Presently this class assumes default fractional metrics. |
|
119 |
* This may need to change to emulate future platform L&Fs. |
|
120 |
*/ |
|
121 |
public static class AATextInfo { |
|
122 |
||
123 |
private static AATextInfo getAATextInfoFromMap(Map hints) { |
|
124 |
||
125 |
Object aaHint = hints.get(KEY_TEXT_ANTIALIASING); |
|
126 |
Object contHint = hints.get(KEY_TEXT_LCD_CONTRAST); |
|
127 |
||
128 |
if (aaHint == null || |
|
129 |
aaHint == VALUE_TEXT_ANTIALIAS_OFF || |
|
130 |
aaHint == VALUE_TEXT_ANTIALIAS_DEFAULT) { |
|
131 |
return null; |
|
132 |
} else { |
|
133 |
return new AATextInfo(aaHint, (Integer)contHint); |
|
134 |
} |
|
135 |
} |
|
136 |
||
137 |
public static AATextInfo getAATextInfo(boolean lafCondition) { |
|
138 |
SunToolkit.setAAFontSettingsCondition(lafCondition); |
|
139 |
Toolkit tk = Toolkit.getDefaultToolkit(); |
|
140 |
Object map = tk.getDesktopProperty(SunToolkit.DESKTOPFONTHINTS); |
|
141 |
if (map instanceof Map) { |
|
142 |
return getAATextInfoFromMap((Map)map); |
|
143 |
} else { |
|
144 |
return null; |
|
145 |
} |
|
146 |
} |
|
147 |
||
148 |
Object aaHint; |
|
149 |
Integer lcdContrastHint; |
|
150 |
FontRenderContext frc; |
|
151 |
||
152 |
/* These are rarely constructed objects, and only when a complete |
|
153 |
* UI is being updated, so the cost of the tests here is minimal |
|
154 |
* and saves tests elsewhere. |
|
155 |
* We test that the values are ones we support/expect. |
|
156 |
*/ |
|
157 |
public AATextInfo(Object aaHint, Integer lcdContrastHint) { |
|
158 |
if (aaHint == null) { |
|
159 |
throw new InternalError("null not allowed here"); |
|
160 |
} |
|
161 |
if (aaHint == VALUE_TEXT_ANTIALIAS_OFF || |
|
162 |
aaHint == VALUE_TEXT_ANTIALIAS_DEFAULT) { |
|
163 |
throw new InternalError("AA must be on"); |
|
164 |
} |
|
165 |
this.aaHint = aaHint; |
|
166 |
this.lcdContrastHint = lcdContrastHint; |
|
167 |
this.frc = new FontRenderContext(null, aaHint, |
|
168 |
VALUE_FRACTIONALMETRICS_DEFAULT); |
|
169 |
} |
|
170 |
} |
|
171 |
||
172 |
/** |
|
173 |
* Key used in client properties used to indicate that the |
|
174 |
* <code>ComponentUI</code> of the JComponent instance should be returned. |
|
175 |
*/ |
|
176 |
public static final Object COMPONENT_UI_PROPERTY_KEY = |
|
177 |
new StringBuffer("ComponentUIPropertyKey"); |
|
178 |
||
179 |
/** Client Property key for the text maximal offsets for BasicMenuItemUI */ |
|
180 |
public static final StringUIClientPropertyKey BASICMENUITEMUI_MAX_TEXT_OFFSET = |
|
181 |
new StringUIClientPropertyKey ("maxTextOffset"); |
|
182 |
||
183 |
// security stuff |
|
184 |
private static Field inputEvent_CanAccessSystemClipboard_Field = null; |
|
185 |
private static final String UntrustedClipboardAccess = |
|
186 |
"UNTRUSTED_CLIPBOARD_ACCESS_KEY"; |
|
187 |
||
188 |
//all access to charsBuffer is to be synchronized on charsBufferLock |
|
189 |
private static final int CHAR_BUFFER_SIZE = 100; |
|
190 |
private static final Object charsBufferLock = new Object(); |
|
191 |
private static char[] charsBuffer = new char[CHAR_BUFFER_SIZE]; |
|
192 |
||
3749
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
193 |
static { |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
194 |
fontCache = new LSBCacheEntry[CACHE_SIZE]; |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
195 |
} |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
196 |
|
2 | 197 |
/** |
3976 | 198 |
* Fill the character buffer cache. Return the buffer length. |
199 |
*/ |
|
200 |
private static int syncCharsBuffer(String s) { |
|
201 |
int length = s.length(); |
|
202 |
if ((charsBuffer == null) || (charsBuffer.length < length)) { |
|
203 |
charsBuffer = s.toCharArray(); |
|
204 |
} else { |
|
205 |
s.getChars(0, length, charsBuffer, 0); |
|
206 |
} |
|
207 |
return length; |
|
208 |
} |
|
209 |
||
210 |
/** |
|
2 | 211 |
* checks whether TextLayout is required to handle characters. |
212 |
* |
|
213 |
* @param text characters to be tested |
|
214 |
* @param start start |
|
215 |
* @param limit limit |
|
216 |
* @return <tt>true</tt> if TextLayout is required |
|
217 |
* <tt>false</tt> if TextLayout is not required |
|
218 |
*/ |
|
219 |
public static final boolean isComplexLayout(char[] text, int start, int limit) { |
|
3928 | 220 |
return FontUtilities.isComplexText(text, start, limit); |
2 | 221 |
} |
222 |
||
223 |
// |
|
224 |
// WARNING WARNING WARNING WARNING WARNING WARNING |
|
225 |
// Many of the following methods are invoked from older API. |
|
226 |
// As this older API was not passed a Component, a null Component may |
|
227 |
// now be passsed in. For example, SwingUtilities.computeStringWidth |
|
228 |
// is implemented to call SwingUtilities2.stringWidth, the |
|
229 |
// SwingUtilities variant does not take a JComponent, as such |
|
230 |
// SwingUtilities2.stringWidth can be passed a null Component. |
|
231 |
// In other words, if you add new functionality to these methods you |
|
232 |
// need to gracefully handle null. |
|
233 |
// |
|
234 |
||
235 |
/** |
|
236 |
* Returns whether or not text should be drawn antialiased. |
|
237 |
* |
|
238 |
* @param c JComponent to test. |
|
239 |
* @return Whether or not text should be drawn antialiased for the |
|
240 |
* specified component. |
|
241 |
*/ |
|
242 |
public static AATextInfo drawTextAntialiased(JComponent c) { |
|
243 |
if (c != null) { |
|
244 |
/* a non-null property implies some form of AA requested */ |
|
245 |
return (AATextInfo)c.getClientProperty(AA_TEXT_PROPERTY_KEY); |
|
246 |
} |
|
247 |
// No component, assume aa is off |
|
248 |
return null; |
|
249 |
} |
|
250 |
||
251 |
/** |
|
252 |
* Returns the left side bearing of the first character of string. The |
|
3749
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
253 |
* left side bearing is calculated from the passed in |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
254 |
* FontMetrics. If the passed in String is less than one |
3980
b41744c10146
6883341: SWAT: jdk7-b72 swat build(2009-09-17) threw exceptions when running Java2D demo by clicking Paint ta
alexp
parents:
3976
diff
changeset
|
255 |
* character {@code 0} is returned. |
2 | 256 |
* |
257 |
* @param c JComponent that will display the string |
|
258 |
* @param fm FontMetrics used to measure the String width |
|
259 |
* @param string String to get the left side bearing for. |
|
3980
b41744c10146
6883341: SWAT: jdk7-b72 swat build(2009-09-17) threw exceptions when running Java2D demo by clicking Paint ta
alexp
parents:
3976
diff
changeset
|
260 |
* @throws NullPointerException if {@code string} is {@code null} |
b41744c10146
6883341: SWAT: jdk7-b72 swat build(2009-09-17) threw exceptions when running Java2D demo by clicking Paint ta
alexp
parents:
3976
diff
changeset
|
261 |
* |
b41744c10146
6883341: SWAT: jdk7-b72 swat build(2009-09-17) threw exceptions when running Java2D demo by clicking Paint ta
alexp
parents:
3976
diff
changeset
|
262 |
* @return the left side bearing of the first character of string |
b41744c10146
6883341: SWAT: jdk7-b72 swat build(2009-09-17) threw exceptions when running Java2D demo by clicking Paint ta
alexp
parents:
3976
diff
changeset
|
263 |
* or {@code 0} if the string is empty |
2 | 264 |
*/ |
265 |
public static int getLeftSideBearing(JComponent c, FontMetrics fm, |
|
266 |
String string) { |
|
3980
b41744c10146
6883341: SWAT: jdk7-b72 swat build(2009-09-17) threw exceptions when running Java2D demo by clicking Paint ta
alexp
parents:
3976
diff
changeset
|
267 |
int res = 0; |
b41744c10146
6883341: SWAT: jdk7-b72 swat build(2009-09-17) threw exceptions when running Java2D demo by clicking Paint ta
alexp
parents:
3976
diff
changeset
|
268 |
if (!string.isEmpty()) { |
b41744c10146
6883341: SWAT: jdk7-b72 swat build(2009-09-17) threw exceptions when running Java2D demo by clicking Paint ta
alexp
parents:
3976
diff
changeset
|
269 |
res = getLeftSideBearing(c, fm, string.charAt(0)); |
b41744c10146
6883341: SWAT: jdk7-b72 swat build(2009-09-17) threw exceptions when running Java2D demo by clicking Paint ta
alexp
parents:
3976
diff
changeset
|
270 |
} |
b41744c10146
6883341: SWAT: jdk7-b72 swat build(2009-09-17) threw exceptions when running Java2D demo by clicking Paint ta
alexp
parents:
3976
diff
changeset
|
271 |
return res; |
2 | 272 |
} |
273 |
||
274 |
/** |
|
3749
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
275 |
* Returns the left side bearing of the first character of string. The |
2 | 276 |
* left side bearing is calculated from the passed in FontMetrics. |
277 |
* |
|
278 |
* @param c JComponent that will display the string |
|
279 |
* @param fm FontMetrics used to measure the String width |
|
280 |
* @param firstChar Character to get the left side bearing for. |
|
281 |
*/ |
|
282 |
public static int getLeftSideBearing(JComponent c, FontMetrics fm, |
|
283 |
char firstChar) { |
|
3749
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
284 |
int charIndex = (int) firstChar; |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
285 |
if (charIndex < MAX_CHAR_INDEX && charIndex >= MIN_CHAR_INDEX) { |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
286 |
byte[] lsbs = null; |
2 | 287 |
|
3749
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
288 |
FontRenderContext frc = getFontRenderContext(c, fm); |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
289 |
Font font = fm.getFont(); |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
290 |
synchronized (SwingUtilities2.class) { |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
291 |
LSBCacheEntry entry = null; |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
292 |
if (searchKey == null) { |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
293 |
searchKey = new LSBCacheEntry(frc, font); |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
294 |
} else { |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
295 |
searchKey.reset(frc, font); |
2 | 296 |
} |
3749
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
297 |
// See if we already have an entry for this pair |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
298 |
for (LSBCacheEntry cacheEntry : fontCache) { |
2 | 299 |
if (searchKey.equals(cacheEntry)) { |
300 |
entry = cacheEntry; |
|
301 |
break; |
|
302 |
} |
|
303 |
} |
|
3749
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
304 |
if (entry == null) { |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
305 |
// No entry for this pair, add it. |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
306 |
entry = searchKey; |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
307 |
fontCache[nextIndex] = searchKey; |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
308 |
searchKey = null; |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
309 |
nextIndex = (nextIndex + 1) % CACHE_SIZE; |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
310 |
} |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
311 |
return entry.getLeftSideBearing(firstChar); |
2 | 312 |
} |
313 |
} |
|
3749
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
314 |
return 0; |
2 | 315 |
} |
316 |
||
317 |
/** |
|
318 |
* Returns the FontMetrics for the current Font of the passed |
|
319 |
* in Graphics. This method is used when a Graphics |
|
320 |
* is available, typically when painting. If a Graphics is not |
|
321 |
* available the JComponent method of the same name should be used. |
|
322 |
* <p> |
|
323 |
* Callers should pass in a non-null JComponent, the exception |
|
324 |
* to this is if a JComponent is not readily available at the time of |
|
325 |
* painting. |
|
326 |
* <p> |
|
327 |
* This does not necessarily return the FontMetrics from the |
|
328 |
* Graphics. |
|
329 |
* |
|
330 |
* @param c JComponent requesting FontMetrics, may be null |
|
331 |
* @param g Graphics Graphics |
|
332 |
*/ |
|
333 |
public static FontMetrics getFontMetrics(JComponent c, Graphics g) { |
|
334 |
return getFontMetrics(c, g, g.getFont()); |
|
335 |
} |
|
336 |
||
337 |
||
338 |
/** |
|
339 |
* Returns the FontMetrics for the specified Font. |
|
340 |
* This method is used when a Graphics is available, typically when |
|
341 |
* painting. If a Graphics is not available the JComponent method of |
|
342 |
* the same name should be used. |
|
343 |
* <p> |
|
344 |
* Callers should pass in a non-null JComonent, the exception |
|
345 |
* to this is if a JComponent is not readily available at the time of |
|
346 |
* painting. |
|
347 |
* <p> |
|
348 |
* This does not necessarily return the FontMetrics from the |
|
349 |
* Graphics. |
|
350 |
* |
|
351 |
* @param c JComponent requesting FontMetrics, may be null |
|
352 |
* @param c Graphics Graphics |
|
353 |
* @param font Font to get FontMetrics for |
|
354 |
*/ |
|
355 |
public static FontMetrics getFontMetrics(JComponent c, Graphics g, |
|
356 |
Font font) { |
|
357 |
if (c != null) { |
|
358 |
// Note: We assume that we're using the FontMetrics |
|
359 |
// from the widget to layout out text, otherwise we can get |
|
360 |
// mismatches when printing. |
|
361 |
return c.getFontMetrics(font); |
|
362 |
} |
|
363 |
return Toolkit.getDefaultToolkit().getFontMetrics(font); |
|
364 |
} |
|
365 |
||
366 |
||
367 |
/** |
|
368 |
* Returns the width of the passed in String. |
|
369 |
* If the passed String is <code>null</code>, returns zero. |
|
370 |
* |
|
371 |
* @param c JComponent that will display the string, may be null |
|
372 |
* @param fm FontMetrics used to measure the String width |
|
373 |
* @param string String to get the width of |
|
374 |
*/ |
|
375 |
public static int stringWidth(JComponent c, FontMetrics fm, String string){ |
|
376 |
if (string == null || string.equals("")) { |
|
377 |
return 0; |
|
378 |
} |
|
3976 | 379 |
boolean needsTextLayout = ((c != null) && |
380 |
(c.getClientProperty(TextAttribute.NUMERIC_SHAPING) != null)); |
|
381 |
if (needsTextLayout) { |
|
382 |
synchronized(charsBufferLock) { |
|
383 |
int length = syncCharsBuffer(string); |
|
384 |
needsTextLayout = isComplexLayout(charsBuffer, 0, length); |
|
385 |
} |
|
386 |
} |
|
387 |
if (needsTextLayout) { |
|
388 |
TextLayout layout = createTextLayout(c, string, |
|
389 |
fm.getFont(), fm.getFontRenderContext()); |
|
390 |
return (int) layout.getAdvance(); |
|
391 |
} else { |
|
392 |
return fm.stringWidth(string); |
|
393 |
} |
|
2 | 394 |
} |
395 |
||
396 |
||
397 |
/** |
|
398 |
* Clips the passed in String to the space provided. |
|
399 |
* |
|
400 |
* @param c JComponent that will display the string, may be null |
|
401 |
* @param fm FontMetrics used to measure the String width |
|
402 |
* @param string String to display |
|
403 |
* @param availTextWidth Amount of space that the string can be drawn in |
|
404 |
* @return Clipped string that can fit in the provided space. |
|
405 |
*/ |
|
406 |
public static String clipStringIfNecessary(JComponent c, FontMetrics fm, |
|
407 |
String string, |
|
408 |
int availTextWidth) { |
|
409 |
if ((string == null) || (string.equals(""))) { |
|
410 |
return ""; |
|
411 |
} |
|
412 |
int textWidth = SwingUtilities2.stringWidth(c, fm, string); |
|
413 |
if (textWidth > availTextWidth) { |
|
414 |
return SwingUtilities2.clipString(c, fm, string, availTextWidth); |
|
415 |
} |
|
416 |
return string; |
|
417 |
} |
|
418 |
||
419 |
||
420 |
/** |
|
421 |
* Clips the passed in String to the space provided. NOTE: this assumes |
|
422 |
* the string does not fit in the available space. |
|
423 |
* |
|
424 |
* @param c JComponent that will display the string, may be null |
|
425 |
* @param fm FontMetrics used to measure the String width |
|
426 |
* @param string String to display |
|
427 |
* @param availTextWidth Amount of space that the string can be drawn in |
|
428 |
* @return Clipped string that can fit in the provided space. |
|
429 |
*/ |
|
430 |
public static String clipString(JComponent c, FontMetrics fm, |
|
431 |
String string, int availTextWidth) { |
|
432 |
// c may be null here. |
|
433 |
String clipString = "..."; |
|
434 |
availTextWidth -= SwingUtilities2.stringWidth(c, fm, clipString); |
|
1301
15e81207e1f2
6727662: Code improvement and warnings removing from swing packages
rupashka
parents:
2
diff
changeset
|
435 |
boolean needsTextLayout; |
2 | 436 |
|
437 |
synchronized (charsBufferLock) { |
|
3976 | 438 |
int stringLength = syncCharsBuffer(string); |
2 | 439 |
needsTextLayout = |
440 |
isComplexLayout(charsBuffer, 0, stringLength); |
|
441 |
if (!needsTextLayout) { |
|
442 |
int width = 0; |
|
443 |
for (int nChars = 0; nChars < stringLength; nChars++) { |
|
444 |
width += fm.charWidth(charsBuffer[nChars]); |
|
445 |
if (width > availTextWidth) { |
|
446 |
string = string.substring(0, nChars); |
|
447 |
break; |
|
448 |
} |
|
449 |
} |
|
450 |
} |
|
451 |
} |
|
452 |
if (needsTextLayout) { |
|
453 |
FontRenderContext frc = getFontRenderContext(c, fm); |
|
454 |
AttributedString aString = new AttributedString(string); |
|
3976 | 455 |
if (c != null) { |
456 |
aString.addAttribute(TextAttribute.NUMERIC_SHAPING, |
|
457 |
c.getClientProperty(TextAttribute.NUMERIC_SHAPING)); |
|
458 |
} |
|
2 | 459 |
LineBreakMeasurer measurer = |
460 |
new LineBreakMeasurer(aString.getIterator(), frc); |
|
461 |
int nChars = measurer.nextOffset(availTextWidth); |
|
462 |
string = string.substring(0, nChars); |
|
463 |
||
464 |
} |
|
465 |
return string + clipString; |
|
466 |
} |
|
467 |
||
468 |
||
469 |
/** |
|
470 |
* Draws the string at the specified location. |
|
471 |
* |
|
472 |
* @param c JComponent that will display the string, may be null |
|
473 |
* @param g Graphics to draw the text to |
|
474 |
* @param text String to display |
|
475 |
* @param x X coordinate to draw the text at |
|
476 |
* @param y Y coordinate to draw the text at |
|
477 |
*/ |
|
478 |
public static void drawString(JComponent c, Graphics g, String text, |
|
479 |
int x, int y) { |
|
480 |
// c may be null |
|
481 |
||
482 |
// All non-editable widgets that draw strings call into this |
|
483 |
// methods. By non-editable that means widgets like JLabel, JButton |
|
484 |
// but NOT JTextComponents. |
|
485 |
if ( text == null || text.length() <= 0 ) { //no need to paint empty strings |
|
486 |
return; |
|
487 |
} |
|
488 |
if (isPrinting(g)) { |
|
489 |
Graphics2D g2d = getGraphics2D(g); |
|
490 |
if (g2d != null) { |
|
491 |
/* The printed text must scale linearly with the UI. |
|
492 |
* Calculate the width on screen, obtain a TextLayout with |
|
493 |
* advances for the printer graphics FRC, and then justify |
|
494 |
* it to fit in the screen width. This distributes the spacing |
|
495 |
* more evenly than directly laying out to the screen advances. |
|
496 |
*/ |
|
497 |
float screenWidth = (float) |
|
498 |
g2d.getFont().getStringBounds(text, DEFAULT_FRC).getWidth(); |
|
3976 | 499 |
TextLayout layout = createTextLayout(c, text, g2d.getFont(), |
2 | 500 |
g2d.getFontRenderContext()); |
501 |
||
502 |
layout = layout.getJustifiedLayout(screenWidth); |
|
503 |
/* Use alternate print color if specified */ |
|
504 |
Color col = g2d.getColor(); |
|
505 |
if (col instanceof PrintColorUIResource) { |
|
506 |
g2d.setColor(((PrintColorUIResource)col).getPrintColor()); |
|
507 |
} |
|
508 |
||
509 |
layout.draw(g2d, x, y); |
|
510 |
||
511 |
g2d.setColor(col); |
|
512 |
||
513 |
return; |
|
514 |
} |
|
515 |
} |
|
516 |
||
517 |
// If we get here we're not printing |
|
518 |
AATextInfo info = drawTextAntialiased(c); |
|
519 |
if (info != null && (g instanceof Graphics2D)) { |
|
520 |
Graphics2D g2 = (Graphics2D)g; |
|
521 |
||
522 |
Object oldContrast = null; |
|
523 |
Object oldAAValue = g2.getRenderingHint(KEY_TEXT_ANTIALIASING); |
|
524 |
if (info.aaHint != oldAAValue) { |
|
525 |
g2.setRenderingHint(KEY_TEXT_ANTIALIASING, info.aaHint); |
|
526 |
} else { |
|
527 |
oldAAValue = null; |
|
528 |
} |
|
529 |
if (info.lcdContrastHint != null) { |
|
530 |
oldContrast = g2.getRenderingHint(KEY_TEXT_LCD_CONTRAST); |
|
531 |
if (info.lcdContrastHint.equals(oldContrast)) { |
|
532 |
oldContrast = null; |
|
533 |
} else { |
|
534 |
g2.setRenderingHint(KEY_TEXT_LCD_CONTRAST, |
|
535 |
info.lcdContrastHint); |
|
536 |
} |
|
537 |
} |
|
538 |
||
3976 | 539 |
boolean needsTextLayout = ((c != null) && |
540 |
(c.getClientProperty(TextAttribute.NUMERIC_SHAPING) != null)); |
|
541 |
if (needsTextLayout) { |
|
542 |
synchronized(charsBufferLock) { |
|
543 |
int length = syncCharsBuffer(text); |
|
544 |
needsTextLayout = isComplexLayout(charsBuffer, 0, length); |
|
545 |
} |
|
546 |
} |
|
547 |
if (needsTextLayout) { |
|
548 |
TextLayout layout = createTextLayout(c, text, g2.getFont(), |
|
549 |
g2.getFontRenderContext()); |
|
550 |
layout.draw(g2, x, y); |
|
551 |
} else { |
|
552 |
g.drawString(text, x, y); |
|
553 |
} |
|
2 | 554 |
|
555 |
if (oldAAValue != null) { |
|
556 |
g2.setRenderingHint(KEY_TEXT_ANTIALIASING, oldAAValue); |
|
557 |
} |
|
558 |
if (oldContrast != null) { |
|
559 |
g2.setRenderingHint(KEY_TEXT_LCD_CONTRAST, oldContrast); |
|
560 |
} |
|
561 |
} |
|
562 |
else { |
|
563 |
g.drawString(text, x, y); |
|
564 |
} |
|
565 |
} |
|
566 |
||
567 |
||
568 |
/** |
|
569 |
* Draws the string at the specified location underlining the specified |
|
570 |
* character. |
|
571 |
* |
|
572 |
* @param c JComponent that will display the string, may be null |
|
573 |
* @param g Graphics to draw the text to |
|
574 |
* @param text String to display |
|
575 |
* @param underlinedIndex Index of a character in the string to underline |
|
576 |
* @param x X coordinate to draw the text at |
|
577 |
* @param y Y coordinate to draw the text at |
|
578 |
*/ |
|
579 |
public static void drawStringUnderlineCharAt(JComponent c,Graphics g, |
|
580 |
String text, int underlinedIndex, int x,int y) { |
|
581 |
if (text == null || text.length() <= 0) { |
|
582 |
return; |
|
583 |
} |
|
584 |
SwingUtilities2.drawString(c, g, text, x, y); |
|
585 |
int textLength = text.length(); |
|
586 |
if (underlinedIndex >= 0 && underlinedIndex < textLength ) { |
|
587 |
int underlineRectY = y; |
|
588 |
int underlineRectHeight = 1; |
|
589 |
int underlineRectX = 0; |
|
590 |
int underlineRectWidth = 0; |
|
591 |
boolean isPrinting = isPrinting(g); |
|
592 |
boolean needsTextLayout = isPrinting; |
|
593 |
if (!needsTextLayout) { |
|
594 |
synchronized (charsBufferLock) { |
|
3976 | 595 |
syncCharsBuffer(text); |
2 | 596 |
needsTextLayout = |
597 |
isComplexLayout(charsBuffer, 0, textLength); |
|
598 |
} |
|
599 |
} |
|
600 |
if (!needsTextLayout) { |
|
601 |
FontMetrics fm = g.getFontMetrics(); |
|
602 |
underlineRectX = x + |
|
603 |
SwingUtilities2.stringWidth(c,fm, |
|
604 |
text.substring(0,underlinedIndex)); |
|
605 |
underlineRectWidth = fm.charWidth(text. |
|
606 |
charAt(underlinedIndex)); |
|
607 |
} else { |
|
608 |
Graphics2D g2d = getGraphics2D(g); |
|
609 |
if (g2d != null) { |
|
610 |
TextLayout layout = |
|
3976 | 611 |
createTextLayout(c, text, g2d.getFont(), |
2 | 612 |
g2d.getFontRenderContext()); |
613 |
if (isPrinting) { |
|
614 |
float screenWidth = (float)g2d.getFont(). |
|
615 |
getStringBounds(text, DEFAULT_FRC).getWidth(); |
|
616 |
layout = layout.getJustifiedLayout(screenWidth); |
|
617 |
} |
|
618 |
TextHitInfo leading = |
|
619 |
TextHitInfo.leading(underlinedIndex); |
|
620 |
TextHitInfo trailing = |
|
621 |
TextHitInfo.trailing(underlinedIndex); |
|
622 |
Shape shape = |
|
623 |
layout.getVisualHighlightShape(leading, trailing); |
|
624 |
Rectangle rect = shape.getBounds(); |
|
625 |
underlineRectX = x + rect.x; |
|
626 |
underlineRectWidth = rect.width; |
|
627 |
} |
|
628 |
} |
|
629 |
g.fillRect(underlineRectX, underlineRectY + 1, |
|
630 |
underlineRectWidth, underlineRectHeight); |
|
631 |
} |
|
632 |
} |
|
633 |
||
634 |
||
635 |
/** |
|
636 |
* A variation of locationToIndex() which only returns an index if the |
|
637 |
* Point is within the actual bounds of a list item (not just in the cell) |
|
638 |
* and if the JList has the "List.isFileList" client property set. |
|
639 |
* Otherwise, this method returns -1. |
|
640 |
* This is used to make WindowsL&F JFileChooser act like native dialogs. |
|
641 |
*/ |
|
642 |
public static int loc2IndexFileList(JList list, Point point) { |
|
643 |
int index = list.locationToIndex(point); |
|
644 |
if (index != -1) { |
|
645 |
Object bySize = list.getClientProperty("List.isFileList"); |
|
646 |
if (bySize instanceof Boolean && ((Boolean)bySize).booleanValue() && |
|
647 |
!pointIsInActualBounds(list, index, point)) { |
|
648 |
index = -1; |
|
649 |
} |
|
650 |
} |
|
651 |
return index; |
|
652 |
} |
|
653 |
||
654 |
||
655 |
/** |
|
656 |
* Returns true if the given point is within the actual bounds of the |
|
657 |
* JList item at index (not just inside the cell). |
|
658 |
*/ |
|
659 |
private static boolean pointIsInActualBounds(JList list, int index, |
|
660 |
Point point) { |
|
661 |
ListCellRenderer renderer = list.getCellRenderer(); |
|
662 |
ListModel dataModel = list.getModel(); |
|
663 |
Object value = dataModel.getElementAt(index); |
|
664 |
Component item = renderer.getListCellRendererComponent(list, |
|
665 |
value, index, false, false); |
|
666 |
Dimension itemSize = item.getPreferredSize(); |
|
667 |
Rectangle cellBounds = list.getCellBounds(index, index); |
|
668 |
if (!item.getComponentOrientation().isLeftToRight()) { |
|
669 |
cellBounds.x += (cellBounds.width - itemSize.width); |
|
670 |
} |
|
671 |
cellBounds.width = itemSize.width; |
|
672 |
||
673 |
return cellBounds.contains(point); |
|
674 |
} |
|
675 |
||
676 |
||
677 |
/** |
|
678 |
* Returns true if the given point is outside the preferredSize of the |
|
679 |
* item at the given row of the table. (Column must be 0). |
|
680 |
* Does not check the "Table.isFileList" property. That should be checked |
|
681 |
* before calling this method. |
|
682 |
* This is used to make WindowsL&F JFileChooser act like native dialogs. |
|
683 |
*/ |
|
684 |
public static boolean pointOutsidePrefSize(JTable table, int row, int column, Point p) { |
|
685 |
if (table.convertColumnIndexToModel(column) != 0 || row == -1) { |
|
686 |
return true; |
|
687 |
} |
|
688 |
TableCellRenderer tcr = table.getCellRenderer(row, column); |
|
689 |
Object value = table.getValueAt(row, column); |
|
690 |
Component cell = tcr.getTableCellRendererComponent(table, value, false, |
|
691 |
false, row, column); |
|
692 |
Dimension itemSize = cell.getPreferredSize(); |
|
693 |
Rectangle cellBounds = table.getCellRect(row, column, false); |
|
694 |
cellBounds.width = itemSize.width; |
|
695 |
cellBounds.height = itemSize.height; |
|
696 |
||
697 |
// See if coords are inside |
|
698 |
// ASSUME: mouse x,y will never be < cell's x,y |
|
699 |
assert (p.x >= cellBounds.x && p.y >= cellBounds.y); |
|
1301
15e81207e1f2
6727662: Code improvement and warnings removing from swing packages
rupashka
parents:
2
diff
changeset
|
700 |
return p.x > cellBounds.x + cellBounds.width || |
15e81207e1f2
6727662: Code improvement and warnings removing from swing packages
rupashka
parents:
2
diff
changeset
|
701 |
p.y > cellBounds.y + cellBounds.height; |
2 | 702 |
} |
703 |
||
704 |
/** |
|
705 |
* Set the lead and anchor without affecting selection. |
|
706 |
*/ |
|
707 |
public static void setLeadAnchorWithoutSelection(ListSelectionModel model, |
|
708 |
int lead, int anchor) { |
|
709 |
if (anchor == -1) { |
|
710 |
anchor = lead; |
|
711 |
} |
|
712 |
if (lead == -1) { |
|
713 |
model.setAnchorSelectionIndex(-1); |
|
714 |
model.setLeadSelectionIndex(-1); |
|
715 |
} else { |
|
716 |
if (model.isSelectedIndex(lead)) { |
|
717 |
model.addSelectionInterval(lead, lead); |
|
718 |
} else { |
|
719 |
model.removeSelectionInterval(lead, lead); |
|
720 |
} |
|
721 |
model.setAnchorSelectionIndex(anchor); |
|
722 |
} |
|
723 |
} |
|
724 |
||
725 |
/** |
|
726 |
* Ignore mouse events if the component is null, not enabled, the event |
|
727 |
* is not associated with the left mouse button, or the event has been |
|
728 |
* consumed. |
|
729 |
*/ |
|
730 |
public static boolean shouldIgnore(MouseEvent me, JComponent c) { |
|
731 |
return c == null || !c.isEnabled() |
|
732 |
|| !SwingUtilities.isLeftMouseButton(me) |
|
733 |
|| me.isConsumed(); |
|
734 |
} |
|
735 |
||
736 |
/** |
|
737 |
* Request focus on the given component if it doesn't already have it |
|
738 |
* and <code>isRequestFocusEnabled()</code> returns true. |
|
739 |
*/ |
|
740 |
public static void adjustFocus(JComponent c) { |
|
741 |
if (!c.hasFocus() && c.isRequestFocusEnabled()) { |
|
742 |
c.requestFocus(); |
|
743 |
} |
|
744 |
} |
|
745 |
||
746 |
/** |
|
747 |
* The following draw functions have the same semantic as the |
|
748 |
* Graphics methods with the same names. |
|
749 |
* |
|
750 |
* this is used for printing |
|
751 |
*/ |
|
752 |
public static int drawChars(JComponent c, Graphics g, |
|
753 |
char[] data, |
|
754 |
int offset, |
|
755 |
int length, |
|
756 |
int x, |
|
757 |
int y) { |
|
758 |
if ( length <= 0 ) { //no need to paint empty strings |
|
759 |
return x; |
|
760 |
} |
|
761 |
int nextX = x + getFontMetrics(c, g).charsWidth(data, offset, length); |
|
762 |
if (isPrinting(g)) { |
|
763 |
Graphics2D g2d = getGraphics2D(g); |
|
764 |
if (g2d != null) { |
|
765 |
FontRenderContext deviceFontRenderContext = g2d. |
|
766 |
getFontRenderContext(); |
|
767 |
FontRenderContext frc = getFontRenderContext(c); |
|
768 |
if (frc != null && |
|
769 |
!isFontRenderContextPrintCompatible |
|
770 |
(deviceFontRenderContext, frc)) { |
|
771 |
TextLayout layout = |
|
3976 | 772 |
createTextLayout(c, new String(data, offset, length), |
2 | 773 |
g2d.getFont(), |
774 |
deviceFontRenderContext); |
|
775 |
float screenWidth = (float)g2d.getFont(). |
|
776 |
getStringBounds(data, offset, offset + length, frc). |
|
777 |
getWidth(); |
|
778 |
layout = layout.getJustifiedLayout(screenWidth); |
|
779 |
||
780 |
/* Use alternate print color if specified */ |
|
781 |
Color col = g2d.getColor(); |
|
782 |
if (col instanceof PrintColorUIResource) { |
|
783 |
g2d.setColor(((PrintColorUIResource)col).getPrintColor()); |
|
784 |
} |
|
785 |
||
786 |
layout.draw(g2d,x,y); |
|
787 |
||
788 |
g2d.setColor(col); |
|
789 |
||
790 |
return nextX; |
|
791 |
} |
|
792 |
} |
|
793 |
} |
|
794 |
// Assume we're not printing if we get here, or that we are invoked |
|
795 |
// via Swing text printing which is laid out for the printer. |
|
796 |
AATextInfo info = drawTextAntialiased(c); |
|
797 |
if (info != null && (g instanceof Graphics2D)) { |
|
798 |
Graphics2D g2 = (Graphics2D)g; |
|
799 |
||
800 |
Object oldContrast = null; |
|
801 |
Object oldAAValue = g2.getRenderingHint(KEY_TEXT_ANTIALIASING); |
|
802 |
if (info.aaHint != null && info.aaHint != oldAAValue) { |
|
803 |
g2.setRenderingHint(KEY_TEXT_ANTIALIASING, info.aaHint); |
|
804 |
} else { |
|
805 |
oldAAValue = null; |
|
806 |
} |
|
807 |
if (info.lcdContrastHint != null) { |
|
808 |
oldContrast = g2.getRenderingHint(KEY_TEXT_LCD_CONTRAST); |
|
809 |
if (info.lcdContrastHint.equals(oldContrast)) { |
|
810 |
oldContrast = null; |
|
811 |
} else { |
|
812 |
g2.setRenderingHint(KEY_TEXT_LCD_CONTRAST, |
|
813 |
info.lcdContrastHint); |
|
814 |
} |
|
815 |
} |
|
816 |
||
817 |
g.drawChars(data, offset, length, x, y); |
|
818 |
||
819 |
if (oldAAValue != null) { |
|
820 |
g2.setRenderingHint(KEY_TEXT_ANTIALIASING, oldAAValue); |
|
821 |
} |
|
822 |
if (oldContrast != null) { |
|
823 |
g2.setRenderingHint(KEY_TEXT_LCD_CONTRAST, oldContrast); |
|
824 |
} |
|
825 |
} |
|
826 |
else { |
|
827 |
g.drawChars(data, offset, length, x, y); |
|
828 |
} |
|
829 |
return nextX; |
|
830 |
} |
|
831 |
||
832 |
/* |
|
833 |
* see documentation for drawChars |
|
834 |
* returns the advance |
|
835 |
*/ |
|
836 |
public static float drawString(JComponent c, Graphics g, |
|
837 |
AttributedCharacterIterator iterator, |
|
838 |
int x, |
|
839 |
int y) { |
|
840 |
||
841 |
float retVal; |
|
842 |
boolean isPrinting = isPrinting(g); |
|
843 |
Color col = g.getColor(); |
|
844 |
||
845 |
if (isPrinting) { |
|
846 |
/* Use alternate print color if specified */ |
|
847 |
if (col instanceof PrintColorUIResource) { |
|
848 |
g.setColor(((PrintColorUIResource)col).getPrintColor()); |
|
849 |
} |
|
850 |
} |
|
851 |
||
852 |
Graphics2D g2d = getGraphics2D(g); |
|
853 |
if (g2d == null) { |
|
854 |
g.drawString(iterator,x,y); //for the cases where advance |
|
855 |
//matters it should not happen |
|
856 |
retVal = x; |
|
857 |
||
858 |
} else { |
|
859 |
FontRenderContext frc; |
|
860 |
if (isPrinting) { |
|
861 |
frc = getFontRenderContext(c); |
|
862 |
if (frc.isAntiAliased() || frc.usesFractionalMetrics()) { |
|
863 |
frc = new FontRenderContext(frc.getTransform(), false, false); |
|
864 |
} |
|
865 |
} else if ((frc = getFRCProperty(c)) != null) { |
|
866 |
/* frc = frc; ! */ |
|
867 |
} else { |
|
868 |
frc = g2d.getFontRenderContext(); |
|
869 |
} |
|
870 |
TextLayout layout = new TextLayout(iterator, frc); |
|
871 |
if (isPrinting) { |
|
872 |
FontRenderContext deviceFRC = g2d.getFontRenderContext(); |
|
873 |
if (!isFontRenderContextPrintCompatible(frc, deviceFRC)) { |
|
874 |
float screenWidth = layout.getAdvance(); |
|
875 |
layout = new TextLayout(iterator, deviceFRC); |
|
876 |
layout = layout.getJustifiedLayout(screenWidth); |
|
877 |
} |
|
878 |
} |
|
879 |
layout.draw(g2d, x, y); |
|
880 |
retVal = layout.getAdvance(); |
|
881 |
} |
|
882 |
||
883 |
if (isPrinting) { |
|
884 |
g.setColor(col); |
|
885 |
} |
|
886 |
||
887 |
return retVal; |
|
888 |
} |
|
889 |
||
3976 | 890 |
private static TextLayout createTextLayout(JComponent c, String s, |
891 |
Font f, FontRenderContext frc) { |
|
892 |
Object shaper = (c == null ? |
|
893 |
null : c.getClientProperty(TextAttribute.NUMERIC_SHAPING)); |
|
894 |
if (shaper == null) { |
|
895 |
return new TextLayout(s, f, frc); |
|
896 |
} else { |
|
897 |
Map<TextAttribute, Object> a = new HashMap<TextAttribute, Object>(); |
|
898 |
a.put(TextAttribute.FONT, f); |
|
899 |
a.put(TextAttribute.NUMERIC_SHAPING, shaper); |
|
900 |
return new TextLayout(s, a, frc); |
|
901 |
} |
|
902 |
} |
|
903 |
||
2 | 904 |
/* |
905 |
* Checks if two given FontRenderContexts are compatible for printing. |
|
906 |
* We can't just use equals as we want to exclude from the comparison : |
|
907 |
* + whether AA is set as irrelevant for printing and shouldn't affect |
|
908 |
* printed metrics anyway |
|
909 |
* + any translation component in the transform of either FRC, as it |
|
910 |
* does not affect metrics. |
|
911 |
* Compatible means no special handling needed for text painting |
|
912 |
*/ |
|
913 |
private static boolean |
|
914 |
isFontRenderContextPrintCompatible(FontRenderContext frc1, |
|
915 |
FontRenderContext frc2) { |
|
916 |
||
917 |
if (frc1 == frc2) { |
|
918 |
return true; |
|
919 |
} |
|
920 |
||
921 |
if (frc1 == null || frc2 == null) { // not supposed to happen |
|
922 |
return false; |
|
923 |
} |
|
924 |
||
925 |
if (frc1.getFractionalMetricsHint() != |
|
926 |
frc2.getFractionalMetricsHint()) { |
|
927 |
return false; |
|
928 |
} |
|
929 |
||
930 |
/* If both are identity, return true */ |
|
931 |
if (!frc1.isTransformed() && !frc2.isTransformed()) { |
|
932 |
return true; |
|
933 |
} |
|
934 |
||
935 |
/* That's the end of the cheap tests, need to get and compare |
|
936 |
* the transform matrices. We don't care about the translation |
|
937 |
* components, so return true if they are otherwise identical. |
|
938 |
*/ |
|
939 |
double[] mat1 = new double[4]; |
|
940 |
double[] mat2 = new double[4]; |
|
941 |
frc1.getTransform().getMatrix(mat1); |
|
942 |
frc2.getTransform().getMatrix(mat2); |
|
943 |
return |
|
944 |
mat1[0] == mat2[0] && |
|
945 |
mat1[1] == mat2[1] && |
|
946 |
mat1[2] == mat2[2] && |
|
947 |
mat1[3] == mat2[3]; |
|
948 |
} |
|
949 |
||
950 |
/* |
|
951 |
* Tries it best to get Graphics2D out of the given Graphics |
|
952 |
* returns null if can not derive it. |
|
953 |
*/ |
|
954 |
public static Graphics2D getGraphics2D(Graphics g) { |
|
955 |
if (g instanceof Graphics2D) { |
|
956 |
return (Graphics2D) g; |
|
957 |
} else if (g instanceof ProxyPrintGraphics) { |
|
958 |
return (Graphics2D)(((ProxyPrintGraphics)g).getGraphics()); |
|
959 |
} else { |
|
960 |
return null; |
|
961 |
} |
|
962 |
} |
|
963 |
||
964 |
/* |
|
965 |
* Returns FontRenderContext associated with Component. |
|
966 |
* FontRenderContext from Component.getFontMetrics is associated |
|
967 |
* with the component. |
|
968 |
* |
|
969 |
* Uses Component.getFontMetrics to get the FontRenderContext from. |
|
970 |
* see JComponent.getFontMetrics and TextLayoutStrategy.java |
|
971 |
*/ |
|
972 |
public static FontRenderContext getFontRenderContext(Component c) { |
|
973 |
assert c != null; |
|
974 |
if (c == null) { |
|
975 |
return DEFAULT_FRC; |
|
976 |
} else { |
|
977 |
return c.getFontMetrics(c.getFont()).getFontRenderContext(); |
|
978 |
} |
|
979 |
} |
|
980 |
||
981 |
/** |
|
982 |
* A convenience method to get FontRenderContext. |
|
983 |
* Returns the FontRenderContext for the passed in FontMetrics or |
|
984 |
* for the passed in Component if FontMetrics is null |
|
985 |
*/ |
|
986 |
private static FontRenderContext getFontRenderContext(Component c, FontMetrics fm) { |
|
987 |
assert fm != null || c!= null; |
|
988 |
return (fm != null) ? fm.getFontRenderContext() |
|
989 |
: getFontRenderContext(c); |
|
990 |
} |
|
991 |
||
992 |
/* |
|
993 |
* This method is to be used only for JComponent.getFontMetrics. |
|
994 |
* In all other places to get FontMetrics we need to use |
|
995 |
* JComponent.getFontMetrics. |
|
996 |
* |
|
997 |
*/ |
|
998 |
public static FontMetrics getFontMetrics(JComponent c, Font font) { |
|
999 |
FontRenderContext frc = getFRCProperty(c); |
|
1000 |
if (frc == null) { |
|
1001 |
frc = DEFAULT_FRC; |
|
1002 |
} |
|
1003 |
return FontDesignMetrics.getMetrics(font, frc); |
|
1004 |
} |
|
1005 |
||
1006 |
||
1007 |
/* Get any FontRenderContext associated with a JComponent |
|
1008 |
* - may return null |
|
1009 |
*/ |
|
1010 |
private static FontRenderContext getFRCProperty(JComponent c) { |
|
1011 |
if (c != null) { |
|
1012 |
AATextInfo info = |
|
1013 |
(AATextInfo)c.getClientProperty(AA_TEXT_PROPERTY_KEY); |
|
1014 |
if (info != null) { |
|
1015 |
return info.frc; |
|
1016 |
} |
|
1017 |
} |
|
1018 |
return null; |
|
1019 |
} |
|
1020 |
||
1021 |
/* |
|
1022 |
* returns true if the Graphics is print Graphics |
|
1023 |
* false otherwise |
|
1024 |
*/ |
|
1025 |
static boolean isPrinting(Graphics g) { |
|
1026 |
return (g instanceof PrinterGraphics || g instanceof PrintGraphics); |
|
1027 |
} |
|
1028 |
||
1029 |
/** |
|
1030 |
* Determines whether the SelectedTextColor should be used for painting text |
|
1031 |
* foreground for the specified highlight. |
|
1032 |
* |
|
1033 |
* Returns true only if the highlight painter for the specified highlight |
|
1034 |
* is the swing painter (whether inner class of javax.swing.text.DefaultHighlighter |
|
1035 |
* or com.sun.java.swing.plaf.windows.WindowsTextUI) and its background color |
|
1036 |
* is null or equals to the selection color of the text component. |
|
1037 |
* |
|
1038 |
* This is a hack for fixing both bugs 4761990 and 5003294 |
|
1039 |
*/ |
|
1040 |
public static boolean useSelectedTextColor(Highlighter.Highlight h, JTextComponent c) { |
|
1041 |
Highlighter.HighlightPainter painter = h.getPainter(); |
|
1042 |
String painterClass = painter.getClass().getName(); |
|
1043 |
if (painterClass.indexOf("javax.swing.text.DefaultHighlighter") != 0 && |
|
1044 |
painterClass.indexOf("com.sun.java.swing.plaf.windows.WindowsTextUI") != 0) { |
|
1045 |
return false; |
|
1046 |
} |
|
1047 |
try { |
|
1048 |
DefaultHighlighter.DefaultHighlightPainter defPainter = |
|
1049 |
(DefaultHighlighter.DefaultHighlightPainter) painter; |
|
1050 |
if (defPainter.getColor() != null && |
|
1051 |
!defPainter.getColor().equals(c.getSelectionColor())) { |
|
1052 |
return false; |
|
1053 |
} |
|
1054 |
} catch (ClassCastException e) { |
|
1055 |
return false; |
|
1056 |
} |
|
1057 |
return true; |
|
1058 |
} |
|
1059 |
||
1060 |
/** |
|
3749
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1061 |
* LSBCacheEntry is used to cache the left side bearing (lsb) for |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1062 |
* a particular <code>Font</code> and <code>FontRenderContext</code>. |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1063 |
* This only caches characters that fall in the range |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1064 |
* <code>MIN_CHAR_INDEX</code> to <code>MAX_CHAR_INDEX</code>. |
2 | 1065 |
*/ |
3749
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1066 |
private static class LSBCacheEntry { |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1067 |
// Used to indicate a particular entry in lsb has not been set. |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1068 |
private static final byte UNSET = Byte.MAX_VALUE; |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1069 |
// Used in creating a GlyphVector to get the lsb |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1070 |
private static final char[] oneChar = new char[1]; |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1071 |
|
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1072 |
private byte[] lsbCache; |
2 | 1073 |
private Font font; |
1074 |
private FontRenderContext frc; |
|
3749
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1075 |
|
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1076 |
|
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1077 |
public LSBCacheEntry(FontRenderContext frc, Font font) { |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1078 |
lsbCache = new byte[MAX_CHAR_INDEX - MIN_CHAR_INDEX]; |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1079 |
reset(frc, font); |
2 | 1080 |
|
3749
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1081 |
} |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1082 |
|
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1083 |
public void reset(FontRenderContext frc, Font font) { |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1084 |
this.font = font; |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1085 |
this.frc = frc; |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1086 |
for (int counter = lsbCache.length - 1; counter >= 0; counter--) { |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1087 |
lsbCache[counter] = UNSET; |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1088 |
} |
2 | 1089 |
} |
1090 |
||
1091 |
public int getLeftSideBearing(char aChar) { |
|
3749
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1092 |
int index = aChar - MIN_CHAR_INDEX; |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1093 |
assert (index >= 0 && index < (MAX_CHAR_INDEX - MIN_CHAR_INDEX)); |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1094 |
byte lsb = lsbCache[index]; |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1095 |
if (lsb == UNSET) { |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1096 |
oneChar[0] = aChar; |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1097 |
GlyphVector gv = font.createGlyphVector(frc, oneChar); |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1098 |
lsb = (byte) gv.getGlyphPixelBounds(0, frc, 0f, 0f).x; |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1099 |
if (lsb < 0) { |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1100 |
/* HRGB/HBGR LCD glyph images will always have a pixel |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1101 |
* on the left used in colour fringe reduction. |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1102 |
* Text rendering positions this correctly but here |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1103 |
* we are using the glyph image to adjust that position |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1104 |
* so must account for it. |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1105 |
*/ |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1106 |
Object aaHint = frc.getAntiAliasingHint(); |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1107 |
if (aaHint == VALUE_TEXT_ANTIALIAS_LCD_HRGB || |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1108 |
aaHint == VALUE_TEXT_ANTIALIAS_LCD_HBGR) { |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1109 |
lsb++; |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1110 |
} |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1111 |
} |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1112 |
lsbCache[index] = lsb; |
2 | 1113 |
} |
3749
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1114 |
return lsb; |
2 | 1115 |
|
1116 |
||
1117 |
} |
|
1118 |
||
1119 |
public boolean equals(Object entry) { |
|
1120 |
if (entry == this) { |
|
1121 |
return true; |
|
1122 |
} |
|
3749
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1123 |
if (!(entry instanceof LSBCacheEntry)) { |
2 | 1124 |
return false; |
1125 |
} |
|
3749
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1126 |
LSBCacheEntry oEntry = (LSBCacheEntry) entry; |
2 | 1127 |
return (font.equals(oEntry.font) && |
1128 |
frc.equals(oEntry.frc)); |
|
1129 |
} |
|
1130 |
||
1131 |
public int hashCode() { |
|
1132 |
int result = 17; |
|
1133 |
if (font != null) { |
|
1134 |
result = 37 * result + font.hashCode(); |
|
1135 |
} |
|
1136 |
if (frc != null) { |
|
1137 |
result = 37 * result + frc.hashCode(); |
|
1138 |
} |
|
1139 |
return result; |
|
1140 |
} |
|
1141 |
} |
|
1142 |
||
1143 |
/* |
|
1144 |
* here goes the fix for 4856343 [Problem with applet interaction |
|
1145 |
* with system selection clipboard] |
|
1146 |
* |
|
1147 |
* NOTE. In case isTrustedContext() no checking |
|
1148 |
* are to be performed |
|
1149 |
*/ |
|
1150 |
||
1151 |
/** |
|
3749
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1152 |
* checks the security permissions for accessing system clipboard |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1153 |
* |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1154 |
* for untrusted context (see isTrustedContext) checks the |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1155 |
* permissions for the current event being handled |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1156 |
* |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1157 |
*/ |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1158 |
public static boolean canAccessSystemClipboard() { |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1159 |
boolean canAccess = false; |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1160 |
if (!GraphicsEnvironment.isHeadless()) { |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1161 |
SecurityManager sm = System.getSecurityManager(); |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1162 |
if (sm == null) { |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1163 |
canAccess = true; |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1164 |
} else { |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1165 |
try { |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1166 |
sm.checkSystemClipboardAccess(); |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1167 |
canAccess = true; |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1168 |
} catch (SecurityException e) { |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1169 |
} |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1170 |
if (canAccess && ! isTrustedContext()) { |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1171 |
canAccess = canCurrentEventAccessSystemClipboard(true); |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1172 |
} |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1173 |
} |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1174 |
} |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1175 |
return canAccess; |
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1176 |
} |
2 | 1177 |
/** |
3749
d0ecbd7075b1
6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents:
3737
diff
changeset
|
1178 |
* Returns true if EventQueue.getCurrentEvent() has the permissions to |
2 | 1179 |
* access the system clipboard |
1180 |
*/ |
|
1181 |
public static boolean canCurrentEventAccessSystemClipboard() { |
|
1182 |
return isTrustedContext() |
|
1183 |
|| canCurrentEventAccessSystemClipboard(false); |
|
1184 |
} |
|
1185 |
||
1186 |
/** |
|
1187 |
* Returns true if the given event has permissions to access the |
|
1188 |
* system clipboard |
|
1189 |
* |
|
1190 |
* @param e AWTEvent to check |
|
1191 |
*/ |
|
1192 |
public static boolean canEventAccessSystemClipboard(AWTEvent e) { |
|
1193 |
return isTrustedContext() |
|
1194 |
|| canEventAccessSystemClipboard(e, false); |
|
1195 |
} |
|
1196 |
||
1197 |
/** |
|
1198 |
* returns canAccessSystemClipboard field from InputEvent |
|
1199 |
* |
|
1200 |
* @param ie InputEvent to get the field from |
|
1201 |
*/ |
|
1202 |
private static synchronized boolean inputEvent_canAccessSystemClipboard(InputEvent ie) { |
|
1203 |
if (inputEvent_CanAccessSystemClipboard_Field == null) { |
|
1204 |
inputEvent_CanAccessSystemClipboard_Field = |
|
1301
15e81207e1f2
6727662: Code improvement and warnings removing from swing packages
rupashka
parents:
2
diff
changeset
|
1205 |
AccessController.doPrivileged( |
15e81207e1f2
6727662: Code improvement and warnings removing from swing packages
rupashka
parents:
2
diff
changeset
|
1206 |
new java.security.PrivilegedAction<Field>() { |
15e81207e1f2
6727662: Code improvement and warnings removing from swing packages
rupashka
parents:
2
diff
changeset
|
1207 |
public Field run() { |
2 | 1208 |
try { |
1301
15e81207e1f2
6727662: Code improvement and warnings removing from swing packages
rupashka
parents:
2
diff
changeset
|
1209 |
Field field = InputEvent.class. |
2 | 1210 |
getDeclaredField("canAccessSystemClipboard"); |
1211 |
field.setAccessible(true); |
|
1212 |
return field; |
|
1213 |
} catch (SecurityException e) { |
|
1214 |
} catch (NoSuchFieldException e) { |
|
1215 |
} |
|
1216 |
return null; |
|
1217 |
} |
|
1218 |
}); |
|
1219 |
} |
|
1220 |
if (inputEvent_CanAccessSystemClipboard_Field == null) { |
|
1221 |
return false; |
|
1222 |
} |
|
1223 |
boolean ret = false; |
|
1224 |
try { |
|
1225 |
ret = inputEvent_CanAccessSystemClipboard_Field. |
|
1226 |
getBoolean(ie); |
|
1227 |
} catch(IllegalAccessException e) { |
|
1228 |
} |
|
1229 |
return ret; |
|
1230 |
} |
|
1231 |
||
1232 |
/** |
|
1233 |
* Returns true if the given event is corrent gesture for |
|
1234 |
* accessing clipboard |
|
1235 |
* |
|
1236 |
* @param ie InputEvent to check |
|
1237 |
*/ |
|
1238 |
||
1239 |
private static boolean isAccessClipboardGesture(InputEvent ie) { |
|
1240 |
boolean allowedGesture = false; |
|
1241 |
if (ie instanceof KeyEvent) { //we can validate only keyboard gestures |
|
1242 |
KeyEvent ke = (KeyEvent)ie; |
|
1243 |
int keyCode = ke.getKeyCode(); |
|
1244 |
int keyModifiers = ke.getModifiers(); |
|
1245 |
switch(keyCode) { |
|
1246 |
case KeyEvent.VK_C: |
|
1247 |
case KeyEvent.VK_V: |
|
1248 |
case KeyEvent.VK_X: |
|
1249 |
allowedGesture = (keyModifiers == InputEvent.CTRL_MASK); |
|
1250 |
break; |
|
1251 |
case KeyEvent.VK_INSERT: |
|
1252 |
allowedGesture = (keyModifiers == InputEvent.CTRL_MASK || |
|
1253 |
keyModifiers == InputEvent.SHIFT_MASK); |
|
1254 |
break; |
|
1255 |
case KeyEvent.VK_COPY: |
|
1256 |
case KeyEvent.VK_PASTE: |
|
1257 |
case KeyEvent.VK_CUT: |
|
1258 |
allowedGesture = true; |
|
1259 |
break; |
|
1260 |
case KeyEvent.VK_DELETE: |
|
1261 |
allowedGesture = ( keyModifiers == InputEvent.SHIFT_MASK); |
|
1262 |
break; |
|
1263 |
} |
|
1264 |
} |
|
1265 |
return allowedGesture; |
|
1266 |
} |
|
1267 |
||
1268 |
/** |
|
1269 |
* Returns true if e has the permissions to |
|
1270 |
* access the system clipboard and if it is allowed gesture (if |
|
1271 |
* checkGesture is true) |
|
1272 |
* |
|
1273 |
* @param e AWTEvent to check |
|
1274 |
* @param checkGesture boolean |
|
1275 |
*/ |
|
1276 |
private static boolean canEventAccessSystemClipboard(AWTEvent e, |
|
1277 |
boolean checkGesture) { |
|
1278 |
if (EventQueue.isDispatchThread()) { |
|
1279 |
/* |
|
1280 |
* Checking event permissions makes sense only for event |
|
1281 |
* dispathing thread |
|
1282 |
*/ |
|
1283 |
if (e instanceof InputEvent |
|
1284 |
&& (! checkGesture || isAccessClipboardGesture((InputEvent)e))) { |
|
1285 |
return inputEvent_canAccessSystemClipboard((InputEvent)e); |
|
1286 |
} else { |
|
1287 |
return false; |
|
1288 |
} |
|
1289 |
} else { |
|
1290 |
return true; |
|
1291 |
} |
|
1292 |
} |
|
1293 |
||
1294 |
/** |
|
1295 |
* Returns true if EventQueue.getCurrentEvent() has the permissions to |
|
1296 |
* access the system clipboard and if it is allowed gesture (if |
|
1297 |
* checkGesture true) |
|
1298 |
* |
|
1299 |
* @param checkGesture boolean |
|
1300 |
*/ |
|
1301 |
private static boolean canCurrentEventAccessSystemClipboard(boolean |
|
1302 |
checkGesture) { |
|
1303 |
AWTEvent event = EventQueue.getCurrentEvent(); |
|
1304 |
return canEventAccessSystemClipboard(event, checkGesture); |
|
1305 |
} |
|
1306 |
||
1307 |
/** |
|
1308 |
* see RFE 5012841 [Per AppContect security permissions] for the |
|
1309 |
* details |
|
1310 |
* |
|
1311 |
*/ |
|
1312 |
private static boolean isTrustedContext() { |
|
1313 |
return (System.getSecurityManager() == null) |
|
1314 |
|| (AppContext.getAppContext(). |
|
1315 |
get(UntrustedClipboardAccess) == null); |
|
1316 |
} |
|
1317 |
||
1318 |
public static String displayPropertiesToCSS(Font font, Color fg) { |
|
1319 |
StringBuffer rule = new StringBuffer("body {"); |
|
1320 |
if (font != null) { |
|
1321 |
rule.append(" font-family: "); |
|
1322 |
rule.append(font.getFamily()); |
|
1323 |
rule.append(" ; "); |
|
1324 |
rule.append(" font-size: "); |
|
1325 |
rule.append(font.getSize()); |
|
1326 |
rule.append("pt ;"); |
|
1327 |
if (font.isBold()) { |
|
1328 |
rule.append(" font-weight: 700 ; "); |
|
1329 |
} |
|
1330 |
if (font.isItalic()) { |
|
1331 |
rule.append(" font-style: italic ; "); |
|
1332 |
} |
|
1333 |
} |
|
1334 |
if (fg != null) { |
|
1335 |
rule.append(" color: #"); |
|
1336 |
if (fg.getRed() < 16) { |
|
1337 |
rule.append('0'); |
|
1338 |
} |
|
1339 |
rule.append(Integer.toHexString(fg.getRed())); |
|
1340 |
if (fg.getGreen() < 16) { |
|
1341 |
rule.append('0'); |
|
1342 |
} |
|
1343 |
rule.append(Integer.toHexString(fg.getGreen())); |
|
1344 |
if (fg.getBlue() < 16) { |
|
1345 |
rule.append('0'); |
|
1346 |
} |
|
1347 |
rule.append(Integer.toHexString(fg.getBlue())); |
|
1348 |
rule.append(" ; "); |
|
1349 |
} |
|
1350 |
rule.append(" }"); |
|
1351 |
return rule.toString(); |
|
1352 |
} |
|
1353 |
||
1354 |
/** |
|
1355 |
* Utility method that creates a <code>UIDefaults.LazyValue</code> that |
|
1356 |
* creates an <code>ImageIcon</code> <code>UIResource</code> for the |
|
1357 |
* specified image file name. The image is loaded using |
|
1358 |
* <code>getResourceAsStream</code>, starting with a call to that method |
|
1359 |
* on the base class parameter. If it cannot be found, searching will |
|
1360 |
* continue through the base class' inheritance hierarchy, up to and |
|
1361 |
* including <code>rootClass</code>. |
|
1362 |
* |
|
1363 |
* @param baseClass the first class to use in searching for the resource |
|
1364 |
* @param rootClass an ancestor of <code>baseClass</code> to finish the |
|
1365 |
* search at |
|
1366 |
* @param imageFile the name of the file to be found |
|
1367 |
* @return a lazy value that creates the <code>ImageIcon</code> |
|
1368 |
* <code>UIResource</code> for the image, |
|
1369 |
* or null if it cannot be found |
|
1370 |
*/ |
|
1371 |
public static Object makeIcon(final Class<?> baseClass, |
|
1372 |
final Class<?> rootClass, |
|
1373 |
final String imageFile) { |
|
1374 |
||
1375 |
return new UIDefaults.LazyValue() { |
|
1376 |
public Object createValue(UIDefaults table) { |
|
1377 |
/* Copy resource into a byte array. This is |
|
1378 |
* necessary because several browsers consider |
|
1379 |
* Class.getResource a security risk because it |
|
1380 |
* can be used to load additional classes. |
|
1381 |
* Class.getResourceAsStream just returns raw |
|
1382 |
* bytes, which we can convert to an image. |
|
1383 |
*/ |
|
1301
15e81207e1f2
6727662: Code improvement and warnings removing from swing packages
rupashka
parents:
2
diff
changeset
|
1384 |
byte[] buffer = |
2 | 1385 |
java.security.AccessController.doPrivileged( |
1301
15e81207e1f2
6727662: Code improvement and warnings removing from swing packages
rupashka
parents:
2
diff
changeset
|
1386 |
new java.security.PrivilegedAction<byte[]>() { |
15e81207e1f2
6727662: Code improvement and warnings removing from swing packages
rupashka
parents:
2
diff
changeset
|
1387 |
public byte[] run() { |
2 | 1388 |
try { |
1389 |
InputStream resource = null; |
|
1390 |
Class<?> srchClass = baseClass; |
|
1391 |
||
1392 |
while (srchClass != null) { |
|
1393 |
resource = srchClass.getResourceAsStream(imageFile); |
|
1394 |
||
1395 |
if (resource != null || srchClass == rootClass) { |
|
1396 |
break; |
|
1397 |
} |
|
1398 |
||
1399 |
srchClass = srchClass.getSuperclass(); |
|
1400 |
} |
|
1401 |
||
1402 |
if (resource == null) { |
|
1403 |
return null; |
|
1404 |
} |
|
1405 |
||
1406 |
BufferedInputStream in = |
|
1407 |
new BufferedInputStream(resource); |
|
1408 |
ByteArrayOutputStream out = |
|
1409 |
new ByteArrayOutputStream(1024); |
|
1410 |
byte[] buffer = new byte[1024]; |
|
1411 |
int n; |
|
1412 |
while ((n = in.read(buffer)) > 0) { |
|
1413 |
out.write(buffer, 0, n); |
|
1414 |
} |
|
1415 |
in.close(); |
|
1416 |
out.flush(); |
|
1417 |
return out.toByteArray(); |
|
1418 |
} catch (IOException ioe) { |
|
1419 |
System.err.println(ioe.toString()); |
|
1420 |
} |
|
1421 |
return null; |
|
1422 |
} |
|
1423 |
}); |
|
1424 |
||
1425 |
if (buffer == null) { |
|
1426 |
return null; |
|
1427 |
} |
|
1428 |
if (buffer.length == 0) { |
|
1429 |
System.err.println("warning: " + imageFile + |
|
1430 |
" is zero-length"); |
|
1431 |
return null; |
|
1432 |
} |
|
1433 |
||
1434 |
return new ImageIconUIResource(buffer); |
|
1435 |
} |
|
1436 |
}; |
|
1437 |
} |
|
1438 |
||
1439 |
/* Used to help decide if AA text rendering should be used, so |
|
1440 |
* this local display test should be additionally qualified |
|
1441 |
* against whether we have XRender support on both ends of the wire, |
|
1442 |
* as with that support remote performance may be good enough to turn |
|
1443 |
* on by default. An additional complication there is XRender does not |
|
1444 |
* appear capable of performing gamma correction needed for LCD text. |
|
1445 |
*/ |
|
1446 |
public static boolean isLocalDisplay() { |
|
1724
a22a286aa16f
6748082: remove platform-specific code from SwingUtilities2.isDisplayLocal
tdv
parents:
2
diff
changeset
|
1447 |
boolean isLocal; |
a22a286aa16f
6748082: remove platform-specific code from SwingUtilities2.isDisplayLocal
tdv
parents:
2
diff
changeset
|
1448 |
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); |
a22a286aa16f
6748082: remove platform-specific code from SwingUtilities2.isDisplayLocal
tdv
parents:
2
diff
changeset
|
1449 |
if (ge instanceof SunGraphicsEnvironment) { |
a22a286aa16f
6748082: remove platform-specific code from SwingUtilities2.isDisplayLocal
tdv
parents:
2
diff
changeset
|
1450 |
isLocal = ((SunGraphicsEnvironment) ge).isDisplayLocal(); |
a22a286aa16f
6748082: remove platform-specific code from SwingUtilities2.isDisplayLocal
tdv
parents:
2
diff
changeset
|
1451 |
} else { |
a22a286aa16f
6748082: remove platform-specific code from SwingUtilities2.isDisplayLocal
tdv
parents:
2
diff
changeset
|
1452 |
isLocal = true; |
2 | 1453 |
} |
1724
a22a286aa16f
6748082: remove platform-specific code from SwingUtilities2.isDisplayLocal
tdv
parents:
2
diff
changeset
|
1454 |
return isLocal; |
2 | 1455 |
} |
1456 |
||
1457 |
/** |
|
1458 |
* Returns an integer from the defaults table. If <code>key</code> does |
|
1459 |
* not map to a valid <code>Integer</code>, or can not be convered from |
|
1460 |
* a <code>String</code> to an integer, the value 0 is returned. |
|
1461 |
* |
|
1462 |
* @param key an <code>Object</code> specifying the int. |
|
1463 |
* @return the int |
|
1464 |
*/ |
|
1465 |
public static int getUIDefaultsInt(Object key) { |
|
1466 |
return getUIDefaultsInt(key, 0); |
|
1467 |
} |
|
1468 |
||
1469 |
/** |
|
1470 |
* Returns an integer from the defaults table that is appropriate |
|
1471 |
* for the given locale. If <code>key</code> does not map to a valid |
|
1472 |
* <code>Integer</code>, or can not be convered from a <code>String</code> |
|
1473 |
* to an integer, the value 0 is returned. |
|
1474 |
* |
|
1475 |
* @param key an <code>Object</code> specifying the int. Returned value |
|
1476 |
* is 0 if <code>key</code> is not available, |
|
1477 |
* @param l the <code>Locale</code> for which the int is desired |
|
1478 |
* @return the int |
|
1479 |
*/ |
|
1480 |
public static int getUIDefaultsInt(Object key, Locale l) { |
|
1481 |
return getUIDefaultsInt(key, l, 0); |
|
1482 |
} |
|
1483 |
||
1484 |
/** |
|
1485 |
* Returns an integer from the defaults table. If <code>key</code> does |
|
1486 |
* not map to a valid <code>Integer</code>, or can not be convered from |
|
1487 |
* a <code>String</code> to an integer, <code>default</code> is |
|
1488 |
* returned. |
|
1489 |
* |
|
1490 |
* @param key an <code>Object</code> specifying the int. Returned value |
|
1491 |
* is 0 if <code>key</code> is not available, |
|
1492 |
* @param defaultValue Returned value if <code>key</code> is not available, |
|
1493 |
* or is not an Integer |
|
1494 |
* @return the int |
|
1495 |
*/ |
|
1496 |
public static int getUIDefaultsInt(Object key, int defaultValue) { |
|
1497 |
return getUIDefaultsInt(key, null, defaultValue); |
|
1498 |
} |
|
1499 |
||
1500 |
/** |
|
1501 |
* Returns an integer from the defaults table that is appropriate |
|
1502 |
* for the given locale. If <code>key</code> does not map to a valid |
|
1503 |
* <code>Integer</code>, or can not be convered from a <code>String</code> |
|
1504 |
* to an integer, <code>default</code> is returned. |
|
1505 |
* |
|
1506 |
* @param key an <code>Object</code> specifying the int. Returned value |
|
1507 |
* is 0 if <code>key</code> is not available, |
|
1508 |
* @param l the <code>Locale</code> for which the int is desired |
|
1509 |
* @param defaultValue Returned value if <code>key</code> is not available, |
|
1510 |
* or is not an Integer |
|
1511 |
* @return the int |
|
1512 |
*/ |
|
1513 |
public static int getUIDefaultsInt(Object key, Locale l, int defaultValue) { |
|
1514 |
Object value = UIManager.get(key, l); |
|
1515 |
||
1516 |
if (value instanceof Integer) { |
|
1517 |
return ((Integer)value).intValue(); |
|
1518 |
} |
|
1519 |
if (value instanceof String) { |
|
1520 |
try { |
|
1521 |
return Integer.parseInt((String)value); |
|
1522 |
} catch (NumberFormatException nfe) {} |
|
1523 |
} |
|
1524 |
return defaultValue; |
|
1525 |
} |
|
1526 |
||
1527 |
// At this point we need this method here. But we assume that there |
|
1528 |
// will be a common method for this purpose in the future releases. |
|
1529 |
public static Component compositeRequestFocus(Component component) { |
|
1530 |
if (component instanceof Container) { |
|
1531 |
Container container = (Container)component; |
|
1532 |
if (container.isFocusCycleRoot()) { |
|
1533 |
FocusTraversalPolicy policy = container.getFocusTraversalPolicy(); |
|
1534 |
Component comp = policy.getDefaultComponent(container); |
|
1535 |
if (comp!=null) { |
|
1536 |
comp.requestFocus(); |
|
1537 |
return comp; |
|
1538 |
} |
|
1539 |
} |
|
1540 |
Container rootAncestor = container.getFocusCycleRootAncestor(); |
|
1541 |
if (rootAncestor!=null) { |
|
1542 |
FocusTraversalPolicy policy = rootAncestor.getFocusTraversalPolicy(); |
|
1543 |
Component comp = policy.getComponentAfter(rootAncestor, container); |
|
1544 |
||
1545 |
if (comp!=null && SwingUtilities.isDescendingFrom(comp, container)) { |
|
1546 |
comp.requestFocus(); |
|
1547 |
return comp; |
|
1548 |
} |
|
1549 |
} |
|
1550 |
} |
|
1551 |
if (component.isFocusable()) { |
|
1552 |
component.requestFocus(); |
|
1553 |
return component; |
|
1554 |
} |
|
1555 |
return null; |
|
1556 |
} |
|
1557 |
||
1558 |
/** |
|
1559 |
* Change focus to the visible component in {@code JTabbedPane}. |
|
1560 |
* This is not a general-purpose method and is here only to permit |
|
1561 |
* sharing code. |
|
1562 |
*/ |
|
1563 |
public static boolean tabbedPaneChangeFocusTo(Component comp) { |
|
1564 |
if (comp != null) { |
|
1565 |
if (comp.isFocusTraversable()) { |
|
1566 |
SwingUtilities2.compositeRequestFocus(comp); |
|
1567 |
return true; |
|
1568 |
} else if (comp instanceof JComponent |
|
1569 |
&& ((JComponent)comp).requestDefaultFocus()) { |
|
1570 |
||
1571 |
return true; |
|
1572 |
} |
|
1573 |
} |
|
1574 |
||
1575 |
return false; |
|
1576 |
} |
|
1577 |
||
1578 |
/** |
|
1579 |
* Submits a value-returning task for execution on the EDT and |
|
1580 |
* returns a Future representing the pending results of the task. |
|
1581 |
* |
|
1582 |
* @param task the task to submit |
|
1583 |
* @return a Future representing pending completion of the task |
|
1584 |
* @throws NullPointerException if the task is null |
|
1585 |
*/ |
|
1586 |
public static <V> Future<V> submit(Callable<V> task) { |
|
1587 |
if (task == null) { |
|
1588 |
throw new NullPointerException(); |
|
1589 |
} |
|
1590 |
FutureTask<V> future = new FutureTask<V>(task); |
|
1591 |
execute(future); |
|
1592 |
return future; |
|
1593 |
} |
|
1594 |
||
1595 |
/** |
|
1596 |
* Submits a Runnable task for execution on the EDT and returns a |
|
1597 |
* Future representing that task. |
|
1598 |
* |
|
1599 |
* @param task the task to submit |
|
1600 |
* @param result the result to return upon successful completion |
|
1601 |
* @return a Future representing pending completion of the task, |
|
1602 |
* and whose <tt>get()</tt> method will return the given |
|
1603 |
* result value upon completion |
|
1604 |
* @throws NullPointerException if the task is null |
|
1605 |
*/ |
|
1606 |
public static <V> Future<V> submit(Runnable task, V result) { |
|
1607 |
if (task == null) { |
|
1608 |
throw new NullPointerException(); |
|
1609 |
} |
|
1610 |
FutureTask<V> future = new FutureTask<V>(task, result); |
|
1611 |
execute(future); |
|
1612 |
return future; |
|
1613 |
} |
|
1614 |
||
1615 |
/** |
|
1616 |
* Sends a Runnable to the EDT for the execution. |
|
1617 |
*/ |
|
1618 |
private static void execute(Runnable command) { |
|
1619 |
SwingUtilities.invokeLater(command); |
|
1620 |
} |
|
1621 |
||
1622 |
/** |
|
1623 |
* Sets the {@code SKIP_CLICK_COUNT} client property on the component |
|
1624 |
* if it is an instance of {@code JTextComponent} with a |
|
1625 |
* {@code DefaultCaret}. This property, used for text components acting |
|
1626 |
* as editors in a table or tree, tells {@code DefaultCaret} how many |
|
1627 |
* clicks to skip before starting selection. |
|
1628 |
*/ |
|
1629 |
public static void setSkipClickCount(Component comp, int count) { |
|
1630 |
if (comp instanceof JTextComponent |
|
1631 |
&& ((JTextComponent) comp).getCaret() instanceof DefaultCaret) { |
|
1632 |
||
1633 |
((JTextComponent) comp).putClientProperty(SKIP_CLICK_COUNT, count); |
|
1634 |
} |
|
1635 |
} |
|
1636 |
||
1637 |
/** |
|
1638 |
* Return the MouseEvent's click count, possibly reduced by the value of |
|
1639 |
* the component's {@code SKIP_CLICK_COUNT} client property. Clears |
|
1640 |
* the {@code SKIP_CLICK_COUNT} property if the mouse event's click count |
|
1641 |
* is 1. In order for clearing of the property to work correctly, there |
|
1642 |
* must be a mousePressed implementation on the caller with this |
|
1643 |
* call as the first line. |
|
1644 |
*/ |
|
1645 |
public static int getAdjustedClickCount(JTextComponent comp, MouseEvent e) { |
|
1646 |
int cc = e.getClickCount(); |
|
1647 |
||
1648 |
if (cc == 1) { |
|
1649 |
comp.putClientProperty(SKIP_CLICK_COUNT, null); |
|
1650 |
} else { |
|
1651 |
Integer sub = (Integer) comp.getClientProperty(SKIP_CLICK_COUNT); |
|
1652 |
if (sub != null) { |
|
1653 |
return cc - sub; |
|
1654 |
} |
|
1655 |
} |
|
1656 |
||
1657 |
return cc; |
|
1658 |
} |
|
1659 |
||
1660 |
/** |
|
1661 |
* Used by the {@code liesIn} method to return which section |
|
1662 |
* the point lies in. |
|
1663 |
* |
|
1664 |
* @see #liesIn |
|
1665 |
*/ |
|
1666 |
public enum Section { |
|
1667 |
||
1668 |
/** The leading section */ |
|
1669 |
LEADING, |
|
1670 |
||
1671 |
/** The middle section */ |
|
1672 |
MIDDLE, |
|
1673 |
||
1674 |
/** The trailing section */ |
|
1675 |
TRAILING |
|
1676 |
} |
|
1677 |
||
1678 |
/** |
|
1679 |
* This method divides a rectangle into two or three sections along |
|
1680 |
* the specified axis and determines which section the given point |
|
1681 |
* lies in on that axis; used by drag and drop when calculating drop |
|
1682 |
* locations. |
|
1683 |
* <p> |
|
1684 |
* For two sections, the rectangle is divided equally and the method |
|
1685 |
* returns whether the point lies in {@code Section.LEADING} or |
|
1686 |
* {@code Section.TRAILING}. For horizontal divisions, the calculation |
|
1687 |
* respects component orientation. |
|
1688 |
* <p> |
|
1689 |
* For three sections, if the rectangle is greater than or equal to |
|
1690 |
* 30 pixels in length along the axis, the calculation gives 10 pixels |
|
1691 |
* to each of the leading and trailing sections and the remainder to the |
|
1692 |
* middle. For smaller sizes, the rectangle is divided equally into three |
|
1693 |
* sections. |
|
1694 |
* <p> |
|
1695 |
* Note: This method assumes that the point is within the bounds of |
|
1696 |
* the given rectangle on the specified axis. However, in cases where |
|
1697 |
* it isn't, the results still have meaning: {@code Section.MIDDLE} |
|
1698 |
* remains the same, {@code Section.LEADING} indicates that the point |
|
1699 |
* is in or somewhere before the leading section, and |
|
1700 |
* {@code Section.TRAILING} indicates that the point is in or somewhere |
|
1701 |
* after the trailing section. |
|
1702 |
* |
|
1703 |
* @param rect the rectangle |
|
1704 |
* @param p the point the check |
|
1705 |
* @param horizontal {@code true} to use the horizontal axis, |
|
1706 |
* or {@code false} for the vertical axis |
|
1707 |
* @param ltr {@code true} for left to right orientation, |
|
1708 |
* or {@code false} for right to left orientation; |
|
1709 |
* only used for horizontal calculations |
|
1710 |
* @param three {@code true} for three sections, |
|
1711 |
* or {@code false} for two |
|
1712 |
* |
|
1713 |
* @return the {@code Section} where the point lies |
|
1714 |
* |
|
1715 |
* @throws NullPointerException if {@code rect} or {@code p} are |
|
1716 |
* {@code null} |
|
1717 |
*/ |
|
1718 |
private static Section liesIn(Rectangle rect, Point p, boolean horizontal, |
|
1719 |
boolean ltr, boolean three) { |
|
1720 |
||
1721 |
/* beginning of the rectangle on the axis */ |
|
1722 |
int p0; |
|
1723 |
||
1724 |
/* point on the axis we're interested in */ |
|
1725 |
int pComp; |
|
1726 |
||
1727 |
/* length of the rectangle on the axis */ |
|
1728 |
int length; |
|
1729 |
||
1730 |
/* value of ltr if horizontal, else true */ |
|
1731 |
boolean forward; |
|
1732 |
||
1733 |
if (horizontal) { |
|
1734 |
p0 = rect.x; |
|
1735 |
pComp = p.x; |
|
1736 |
length = rect.width; |
|
1737 |
forward = ltr; |
|
1738 |
} else { |
|
1739 |
p0 = rect.y; |
|
1740 |
pComp = p.y; |
|
1741 |
length = rect.height; |
|
1742 |
forward = true; |
|
1743 |
} |
|
1744 |
||
1745 |
if (three) { |
|
1746 |
int boundary = (length >= 30) ? 10 : length / 3; |
|
1747 |
||
1748 |
if (pComp < p0 + boundary) { |
|
1749 |
return forward ? Section.LEADING : Section.TRAILING; |
|
1750 |
} else if (pComp >= p0 + length - boundary) { |
|
1751 |
return forward ? Section.TRAILING : Section.LEADING; |
|
1752 |
} |
|
1753 |
||
1754 |
return Section.MIDDLE; |
|
1755 |
} else { |
|
1756 |
int middle = p0 + length / 2; |
|
1757 |
if (forward) { |
|
1758 |
return pComp >= middle ? Section.TRAILING : Section.LEADING; |
|
1759 |
} else { |
|
1760 |
return pComp < middle ? Section.TRAILING : Section.LEADING; |
|
1761 |
} |
|
1762 |
} |
|
1763 |
} |
|
1764 |
||
1765 |
/** |
|
1766 |
* This method divides a rectangle into two or three sections along |
|
1767 |
* the horizontal axis and determines which section the given point |
|
1768 |
* lies in; used by drag and drop when calculating drop locations. |
|
1769 |
* <p> |
|
1770 |
* See the documentation for {@link #liesIn} for more information |
|
1771 |
* on how the section is calculated. |
|
1772 |
* |
|
1773 |
* @param rect the rectangle |
|
1774 |
* @param p the point the check |
|
1775 |
* @param ltr {@code true} for left to right orientation, |
|
1776 |
* or {@code false} for right to left orientation |
|
1777 |
* @param three {@code true} for three sections, |
|
1778 |
* or {@code false} for two |
|
1779 |
* |
|
1780 |
* @return the {@code Section} where the point lies |
|
1781 |
* |
|
1782 |
* @throws NullPointerException if {@code rect} or {@code p} are |
|
1783 |
* {@code null} |
|
1784 |
*/ |
|
1785 |
public static Section liesInHorizontal(Rectangle rect, Point p, |
|
1786 |
boolean ltr, boolean three) { |
|
1787 |
return liesIn(rect, p, true, ltr, three); |
|
1788 |
} |
|
1789 |
||
1790 |
/** |
|
1791 |
* This method divides a rectangle into two or three sections along |
|
1792 |
* the vertical axis and determines which section the given point |
|
1793 |
* lies in; used by drag and drop when calculating drop locations. |
|
1794 |
* <p> |
|
1795 |
* See the documentation for {@link #liesIn} for more information |
|
1796 |
* on how the section is calculated. |
|
1797 |
* |
|
1798 |
* @param rect the rectangle |
|
1799 |
* @param p the point the check |
|
1800 |
* @param three {@code true} for three sections, |
|
1801 |
* or {@code false} for two |
|
1802 |
* |
|
1803 |
* @return the {@code Section} where the point lies |
|
1804 |
* |
|
1805 |
* @throws NullPointerException if {@code rect} or {@code p} are |
|
1806 |
* {@code null} |
|
1807 |
*/ |
|
1808 |
public static Section liesInVertical(Rectangle rect, Point p, |
|
1809 |
boolean three) { |
|
1810 |
return liesIn(rect, p, false, false, three); |
|
1811 |
} |
|
5590
3ec077d7e893
6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents:
4278
diff
changeset
|
1812 |
|
3ec077d7e893
6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents:
4278
diff
changeset
|
1813 |
/** |
3ec077d7e893
6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents:
4278
diff
changeset
|
1814 |
* Maps the index of the column in the view at |
3ec077d7e893
6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents:
4278
diff
changeset
|
1815 |
* {@code viewColumnIndex} to the index of the column |
3ec077d7e893
6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents:
4278
diff
changeset
|
1816 |
* in the table model. Returns the index of the corresponding |
3ec077d7e893
6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents:
4278
diff
changeset
|
1817 |
* column in the model. If {@code viewColumnIndex} |
3ec077d7e893
6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents:
4278
diff
changeset
|
1818 |
* is less than zero, returns {@code viewColumnIndex}. |
3ec077d7e893
6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents:
4278
diff
changeset
|
1819 |
* |
3ec077d7e893
6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents:
4278
diff
changeset
|
1820 |
* @param cm the table model |
3ec077d7e893
6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents:
4278
diff
changeset
|
1821 |
* @param viewColumnIndex the index of the column in the view |
3ec077d7e893
6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents:
4278
diff
changeset
|
1822 |
* @return the index of the corresponding column in the model |
3ec077d7e893
6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents:
4278
diff
changeset
|
1823 |
* |
3ec077d7e893
6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents:
4278
diff
changeset
|
1824 |
* @see JTable#convertColumnIndexToModel(int) |
3ec077d7e893
6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents:
4278
diff
changeset
|
1825 |
* @see javax.swing.plaf.basic.BasicTableHeaderUI |
3ec077d7e893
6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents:
4278
diff
changeset
|
1826 |
*/ |
3ec077d7e893
6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents:
4278
diff
changeset
|
1827 |
public static int convertColumnIndexToModel(TableColumnModel cm, |
3ec077d7e893
6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents:
4278
diff
changeset
|
1828 |
int viewColumnIndex) { |
3ec077d7e893
6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents:
4278
diff
changeset
|
1829 |
if (viewColumnIndex < 0) { |
3ec077d7e893
6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents:
4278
diff
changeset
|
1830 |
return viewColumnIndex; |
3ec077d7e893
6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents:
4278
diff
changeset
|
1831 |
} |
3ec077d7e893
6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents:
4278
diff
changeset
|
1832 |
return cm.getColumn(viewColumnIndex).getModelIndex(); |
3ec077d7e893
6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents:
4278
diff
changeset
|
1833 |
} |
3ec077d7e893
6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents:
4278
diff
changeset
|
1834 |
|
3ec077d7e893
6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents:
4278
diff
changeset
|
1835 |
/** |
3ec077d7e893
6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents:
4278
diff
changeset
|
1836 |
* Maps the index of the column in the {@code cm} at |
3ec077d7e893
6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents:
4278
diff
changeset
|
1837 |
* {@code modelColumnIndex} to the index of the column |
3ec077d7e893
6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents:
4278
diff
changeset
|
1838 |
* in the view. Returns the index of the |
3ec077d7e893
6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents:
4278
diff
changeset
|
1839 |
* corresponding column in the view; returns {@code -1} if this column |
3ec077d7e893
6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents:
4278
diff
changeset
|
1840 |
* is not being displayed. If {@code modelColumnIndex} is less than zero, |
3ec077d7e893
6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents:
4278
diff
changeset
|
1841 |
* returns {@code modelColumnIndex}. |
3ec077d7e893
6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents:
4278
diff
changeset
|
1842 |
* |
3ec077d7e893
6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents:
4278
diff
changeset
|
1843 |
* @param cm the table model |
3ec077d7e893
6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents:
4278
diff
changeset
|
1844 |
* @param modelColumnIndex the index of the column in the model |
3ec077d7e893
6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents:
4278
diff
changeset
|
1845 |
* @return the index of the corresponding column in the view |
3ec077d7e893
6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents:
4278
diff
changeset
|
1846 |
* |
3ec077d7e893
6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents:
4278
diff
changeset
|
1847 |
* @see JTable#convertColumnIndexToView(int) |
3ec077d7e893
6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents:
4278
diff
changeset
|
1848 |
* @see javax.swing.plaf.basic.BasicTableHeaderUI |
3ec077d7e893
6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents:
4278
diff
changeset
|
1849 |
*/ |
3ec077d7e893
6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents:
4278
diff
changeset
|
1850 |
public static int convertColumnIndexToView(TableColumnModel cm, |
3ec077d7e893
6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents:
4278
diff
changeset
|
1851 |
int modelColumnIndex) { |
3ec077d7e893
6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents:
4278
diff
changeset
|
1852 |
if (modelColumnIndex < 0) { |
3ec077d7e893
6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents:
4278
diff
changeset
|
1853 |
return modelColumnIndex; |
3ec077d7e893
6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents:
4278
diff
changeset
|
1854 |
} |
3ec077d7e893
6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents:
4278
diff
changeset
|
1855 |
for (int column = 0; column < cm.getColumnCount(); column++) { |
3ec077d7e893
6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents:
4278
diff
changeset
|
1856 |
if (cm.getColumn(column).getModelIndex() == modelColumnIndex) { |
3ec077d7e893
6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents:
4278
diff
changeset
|
1857 |
return column; |
3ec077d7e893
6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents:
4278
diff
changeset
|
1858 |
} |
3ec077d7e893
6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents:
4278
diff
changeset
|
1859 |
} |
3ec077d7e893
6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents:
4278
diff
changeset
|
1860 |
return -1; |
3ec077d7e893
6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents:
4278
diff
changeset
|
1861 |
} |
2 | 1862 |
} |