001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.swing.plaf.util;
043:
044: import javax.imageio.ImageIO;
045: import javax.swing.*;
046: import java.awt.*;
047: import java.lang.reflect.Method;
048: import java.net.URL;
049: import java.util.HashMap;
050: import java.util.Map;
051:
052: /** XP color scheme installer.
053: *
054: * @author Dafe Simonek
055: */
056: public final class UIUtils {
057: private static HashMap<RenderingHints.Key, Object> hintsMap = null;
058: private static final boolean noAntialias = Boolean
059: .getBoolean("nb.no.antialias"); //NOI18N
060:
061: /** true when XP style colors are installed into UI manager, false otherwise */
062: private static boolean colorsReady = false;
063:
064: /** No need to instantiate this utility class. */
065: private UIUtils() {
066: }
067:
068: /** Finds if windows LF is active.
069: * @return true if windows LF is active, false otherwise */
070: public static boolean isWindowsLF() {
071: if (Boolean.getBoolean("netbeans.winsys.forceclassic")) {
072: return false;
073: }
074: String lfID = UIManager.getLookAndFeel().getID();
075: // #79401 - return true also for "JGoodies Windows" LF
076: return lfID.endsWith("Windows"); //NOI18N
077: }
078:
079: /** Finds if windows LF with XP theme is active.
080: * @return true if windows LF and XP theme is active, false otherwise */
081: public static boolean isXPLF() {
082: if (!isWindowsLF()) {
083: return false;
084: }
085: Boolean isXP = (Boolean) Toolkit.getDefaultToolkit()
086: .getDesktopProperty("win.xpstyle.themeActive"); //NOI18N
087: return isXP == null ? false : isXP.booleanValue();
088: }
089:
090: private static final Map<RenderingHints.Key, Object> getHints() {
091: //XXX should do this in update() in the UI instead
092: //Note for this method we do NOT want only text antialiasing - we
093: //want antialiased curves.
094: if (hintsMap == null) {
095: hintsMap = new HashMap<RenderingHints.Key, Object>();
096: hintsMap.put(RenderingHints.KEY_TEXT_ANTIALIASING,
097: RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
098: hintsMap.put(RenderingHints.KEY_ANTIALIASING,
099: RenderingHints.VALUE_ANTIALIAS_ON);
100: }
101: return hintsMap;
102: }
103:
104: public static final void configureRenderingHints(Graphics g) {
105: if (noAntialias)
106: return;
107: Graphics2D g2d = (Graphics2D) g;
108:
109: g2d.addRenderingHints(getHints());
110: }
111:
112: public static Image loadImage(String s) {
113: if (openideAvailable == null) {
114: checkOpenide();
115: }
116: if (Boolean.TRUE.equals(openideAvailable)) {
117: return loadWithUtilities(s);
118: } else {
119: return loadWithImageIO(s);
120: }
121: }
122:
123: /** Computes "middle" color in terms of rgb color space. Ignores alpha
124: * (transparency) channel
125: */
126: public static Color getMiddle(Color c1, Color c2) {
127: return new Color((c1.getRed() + c2.getRed()) / 2, (c1
128: .getGreen() + c2.getGreen()) / 2, (c1.getBlue() + c2
129: .getBlue()) / 2);
130: }
131:
132: private static void checkOpenide() {
133: try {
134: utilsClass = Class.forName("org.openide.util.Utilities"); //NOI18N
135: utilsMethod = utilsClass.getDeclaredMethod("loadImage",
136: new Class[] { String.class }); //NOI18N
137: openideAvailable = Boolean.TRUE;
138: } catch (Exception e) {
139: openideAvailable = Boolean.FALSE;
140: }
141: }
142:
143: private static Image loadWithUtilities(String s) {
144: Image result = null;
145: try {
146: result = (Image) utilsMethod.invoke(null,
147: new Object[] { s });
148: } catch (Exception e) {
149: System.err.println("Error loading image " + s); //NOI18N
150: e.printStackTrace(); //XXX
151: }
152: return result;
153: }
154:
155: private static Image loadWithImageIO(String s) {
156: Image result = null;
157: try {
158: URL url = UIUtils.class.getResource(s);
159: result = ImageIO.read(url);
160: } catch (Exception e) {
161: System.err
162: .println("Error loading image using ImageIO " + s); //NOI18N
163: e.printStackTrace();
164: }
165: return result;
166: }
167:
168: private static Boolean openideAvailable = null;
169: private static Class<?> utilsClass = null;
170: private static Method utilsMethod = null;
171:
172: //XXX move/duplicate org.netbeans.swing.tabcontrol.plaf.ColorUtil gradient paint caching?
173: public static GradientPaint getGradientPaint(float x1, float y1,
174: Color upper, float x2, float y2, Color lower,
175: boolean repeats) {
176: return new GradientPaint(x1, y1, upper, x2, y2, lower, repeats);
177: }
178:
179: public static Color adjustColor(Color c, int rDiff, int gDiff,
180: int bDiff) {
181: //XXX deleteme once converted to relative colors
182: int red = Math.max(0, Math.min(255, c.getRed() + rDiff));
183: int green = Math.max(0, Math.min(255, c.getGreen() + gDiff));
184: int blue = Math.max(0, Math.min(255, c.getBlue() + bDiff));
185: return new Color(red, green, blue);
186: }
187:
188: /**
189: * Rotates a float value around 0-1
190: */
191: private static float minMax(float f) {
192: return Math.max(0, Math.min(1, f));
193: }
194:
195: public static boolean isBrighter(Color a, Color b) {
196: int[] ac = new int[] { a.getRed(), a.getGreen(), a.getBlue() };
197: int[] bc = new int[] { b.getRed(), b.getGreen(), b.getBlue() };
198: int dif = 0;
199:
200: for (int i = 0; i < 3; i++) {
201: int currDif = ac[i] - bc[i];
202: if (Math.abs(currDif) > Math.abs(dif)) {
203: dif = currDif;
204: }
205: }
206: return dif > 0;
207: }
208:
209: public static Object[] addInputMapsWithoutCtrlPageUpAndCtrlPageDown(
210: Object[] uiDefaults) {
211: final Object[] inputMaps = new Object[] {
212: //turn of the default mapping of Ctrl+PAGE_UP and Ctrl+PAGE_DOWN shortcuts
213: "List.focusInputMap",
214: new UIDefaults.LazyInputMap(new Object[] { "ctrl C",
215: "copy", "ctrl V", "paste", "ctrl X", "cut",
216: "COPY", "copy", "PASTE", "paste", "CUT", "cut",
217: "UP", "selectPreviousRow", "KP_UP",
218: "selectPreviousRow", "shift UP",
219: "selectPreviousRowExtendSelection",
220: "shift KP_UP",
221: "selectPreviousRowExtendSelection",
222: "ctrl shift UP",
223: "selectPreviousRowExtendSelection",
224: "ctrl shift KP_UP",
225: "selectPreviousRowExtendSelection", "ctrl UP",
226: "selectPreviousRowChangeLead", "ctrl KP_UP",
227: "selectPreviousRowChangeLead", "DOWN",
228: "selectNextRow", "KP_DOWN", "selectNextRow",
229: "shift DOWN", "selectNextRowExtendSelection",
230: "shift KP_DOWN",
231: "selectNextRowExtendSelection",
232: "ctrl shift DOWN",
233: "selectNextRowExtendSelection",
234: "ctrl shift KP_DOWN",
235: "selectNextRowExtendSelection", "ctrl DOWN",
236: "selectNextRowChangeLead", "ctrl KP_DOWN",
237: "selectNextRowChangeLead", "LEFT",
238: "selectPreviousColumn", "KP_LEFT",
239: "selectPreviousColumn", "shift LEFT",
240: "selectPreviousColumnExtendSelection",
241: "shift KP_LEFT",
242: "selectPreviousColumnExtendSelection",
243: "ctrl shift LEFT",
244: "selectPreviousColumnExtendSelection",
245: "ctrl shift KP_LEFT",
246: "selectPreviousColumnExtendSelection",
247: "ctrl LEFT", "selectPreviousColumnChangeLead",
248: "ctrl KP_LEFT",
249: "selectPreviousColumnChangeLead", "RIGHT",
250: "selectNextColumn", "KP_RIGHT",
251: "selectNextColumn", "shift RIGHT",
252: "selectNextColumnExtendSelection",
253: "shift KP_RIGHT",
254: "selectNextColumnExtendSelection",
255: "ctrl shift RIGHT",
256: "selectNextColumnExtendSelection",
257: "ctrl shift KP_RIGHT",
258: "selectNextColumnExtendSelection",
259: "ctrl RIGHT", "selectNextColumnChangeLead",
260: "ctrl KP_RIGHT",
261: "selectNextColumnChangeLead",
262: "HOME",
263: "selectFirstRow",
264: "shift HOME",
265: "selectFirstRowExtendSelection",
266: "ctrl shift HOME",
267: "selectFirstRowExtendSelection",
268: "ctrl HOME",
269: "selectFirstRowChangeLead",
270: "END",
271: "selectLastRow",
272: "shift END",
273: "selectLastRowExtendSelection",
274: "ctrl shift END",
275: "selectLastRowExtendSelection",
276: "ctrl END",
277: "selectLastRowChangeLead",
278: "PAGE_UP",
279: "scrollUp",
280: "shift PAGE_UP",
281: "scrollUpExtendSelection",
282: "ctrl shift PAGE_UP",
283: "scrollUpExtendSelection",
284: // "ctrl PAGE_UP", "scrollUpChangeLead",
285: "PAGE_DOWN",
286: "scrollDown",
287: "shift PAGE_DOWN",
288: "scrollDownExtendSelection",
289: "ctrl shift PAGE_DOWN",
290: "scrollDownExtendSelection",
291: // "ctrl PAGE_DOWN", "scrollDownChangeLead",
292: "ctrl A", "selectAll", "ctrl SLASH",
293: "selectAll", "ctrl BACK_SLASH",
294: "clearSelection", "SPACE", "addToSelection",
295: "ctrl SPACE", "toggleAndAnchor", "shift SPACE",
296: "extendTo", "ctrl shift SPACE",
297: "moveSelectionTo" }),
298: "ScrollPane.ancestorInputMap",
299: new UIDefaults.LazyInputMap(new Object[] { "RIGHT",
300: "unitScrollRight", "KP_RIGHT",
301: "unitScrollRight", "DOWN", "unitScrollDown",
302: "KP_DOWN", "unitScrollDown", "LEFT",
303: "unitScrollLeft", "KP_LEFT", "unitScrollLeft",
304: "UP", "unitScrollUp", "KP_UP", "unitScrollUp",
305: "PAGE_UP", "scrollUp",
306: "PAGE_DOWN",
307: "scrollDown",
308: // "ctrl PAGE_UP", "scrollLeft",
309: // "ctrl PAGE_DOWN", "scrollRight",
310: "ctrl HOME", "scrollHome", "ctrl END",
311: "scrollEnd" }),
312: "ScrollPane.ancestorInputMap.RightToLeft",
313: new UIDefaults.LazyInputMap(new Object[] {
314: // "ctrl PAGE_UP", "scrollRight",
315: // "ctrl PAGE_DOWN", "scrollLeft",
316: }),
317: "Table.ancestorInputMap",
318: new UIDefaults.LazyInputMap(new Object[] { "ctrl C",
319: "copy", "ctrl V", "paste", "ctrl X", "cut",
320: "COPY", "copy", "PASTE", "paste", "CUT", "cut",
321: "RIGHT", "selectNextColumn", "KP_RIGHT",
322: "selectNextColumn", "shift RIGHT",
323: "selectNextColumnExtendSelection",
324: "shift KP_RIGHT",
325: "selectNextColumnExtendSelection",
326: "ctrl shift RIGHT",
327: "selectNextColumnExtendSelection",
328: "ctrl shift KP_RIGHT",
329: "selectNextColumnExtendSelection",
330: "ctrl RIGHT", "selectNextColumnChangeLead",
331: "ctrl KP_RIGHT", "selectNextColumnChangeLead",
332: "LEFT", "selectPreviousColumn", "KP_LEFT",
333: "selectPreviousColumn", "shift LEFT",
334: "selectPreviousColumnExtendSelection",
335: "shift KP_LEFT",
336: "selectPreviousColumnExtendSelection",
337: "ctrl shift LEFT",
338: "selectPreviousColumnExtendSelection",
339: "ctrl shift KP_LEFT",
340: "selectPreviousColumnExtendSelection",
341: "ctrl LEFT", "selectPreviousColumnChangeLead",
342: "ctrl KP_LEFT",
343: "selectPreviousColumnChangeLead", "DOWN",
344: "selectNextRow", "KP_DOWN", "selectNextRow",
345: "shift DOWN",
346: "selectNextRowExtendSelection",
347: "shift KP_DOWN",
348: "selectNextRowExtendSelection",
349: "ctrl shift DOWN",
350: "selectNextRowExtendSelection",
351: "ctrl shift KP_DOWN",
352: "selectNextRowExtendSelection",
353: "ctrl DOWN",
354: "selectNextRowChangeLead",
355: "ctrl KP_DOWN",
356: "selectNextRowChangeLead",
357: "UP",
358: "selectPreviousRow",
359: "KP_UP",
360: "selectPreviousRow",
361: "shift UP",
362: "selectPreviousRowExtendSelection",
363: "shift KP_UP",
364: "selectPreviousRowExtendSelection",
365: "ctrl shift UP",
366: "selectPreviousRowExtendSelection",
367: "ctrl shift KP_UP",
368: "selectPreviousRowExtendSelection",
369: "ctrl UP",
370: "selectPreviousRowChangeLead",
371: "ctrl KP_UP",
372: "selectPreviousRowChangeLead",
373: "HOME",
374: "selectFirstColumn",
375: "shift HOME",
376: "selectFirstColumnExtendSelection",
377: "ctrl shift HOME",
378: "selectFirstRowExtendSelection",
379: "ctrl HOME",
380: "selectFirstRow",
381: "END",
382: "selectLastColumn",
383: "shift END",
384: "selectLastColumnExtendSelection",
385: "ctrl shift END",
386: "selectLastRowExtendSelection",
387: "ctrl END",
388: "selectLastRow",
389: "PAGE_UP",
390: "scrollUpChangeSelection",
391: "shift PAGE_UP",
392: "scrollUpExtendSelection",
393: "ctrl shift PAGE_UP",
394: "scrollLeftExtendSelection",
395: // "ctrl PAGE_UP", "scrollLeftChangeSelection",
396: "PAGE_DOWN",
397: "scrollDownChangeSelection",
398: "shift PAGE_DOWN",
399: "scrollDownExtendSelection",
400: "ctrl shift PAGE_DOWN",
401: "scrollRightExtendSelection",
402: // "ctrl PAGE_DOWN", "scrollRightChangeSelection",
403: "TAB", "selectNextColumnCell", "shift TAB",
404: "selectPreviousColumnCell", "ENTER",
405: "selectNextRowCell", "shift ENTER",
406: "selectPreviousRowCell", "ctrl A", "selectAll",
407: "ctrl SLASH", "selectAll", "ctrl BACK_SLASH",
408: "clearSelection", "ESCAPE", "cancel", "F2",
409: "startEditing", "SPACE", "addToSelection",
410: "ctrl SPACE", "toggleAndAnchor", "shift SPACE",
411: "extendTo", "ctrl shift SPACE",
412: "moveSelectionTo" }),
413: "Table.ancestorInputMap.RightToLeft",
414: new UIDefaults.LazyInputMap(new Object[] { "RIGHT",
415: "selectPreviousColumn", "KP_RIGHT",
416: "selectPreviousColumn", "shift RIGHT",
417: "selectPreviousColumnExtendSelection",
418: "shift KP_RIGHT",
419: "selectPreviousColumnExtendSelection",
420: "ctrl shift RIGHT",
421: "selectPreviousColumnExtendSelection",
422: "ctrl shift KP_RIGHT",
423: "selectPreviousColumnExtendSelection",
424: "shift RIGHT",
425: "selectPreviousColumnChangeLead",
426: "shift KP_RIGHT",
427: "selectPreviousColumnChangeLead",
428: "LEFT",
429: "selectNextColumn",
430: "KP_LEFT",
431: "selectNextColumn",
432: "shift LEFT",
433: "selectNextColumnExtendSelection",
434: "shift KP_LEFT",
435: "selectNextColumnExtendSelection",
436: "ctrl shift LEFT",
437: "selectNextColumnExtendSelection",
438: "ctrl shift KP_LEFT",
439: "selectNextColumnExtendSelection",
440: "ctrl LEFT",
441: "selectNextColumnChangeLead",
442: "ctrl KP_LEFT",
443: "selectNextColumnChangeLead",
444: // "ctrl PAGE_UP", "scrollRightChangeSelection",
445: // "ctrl PAGE_DOWN", "scrollLeftChangeSelection",
446: "ctrl shift PAGE_UP",
447: "scrollRightExtendSelection",
448: "ctrl shift PAGE_DOWN",
449: "scrollLeftExtendSelection", }),
450: "Tree.focusInputMap",
451: new UIDefaults.LazyInputMap(new Object[] { "ctrl C",
452: "copy", "ctrl V", "paste",
453: "ctrl X",
454: "cut",
455: "COPY",
456: "copy",
457: "PASTE",
458: "paste",
459: "CUT",
460: "cut",
461: "UP",
462: "selectPrevious",
463: "KP_UP",
464: "selectPrevious",
465: "shift UP",
466: "selectPreviousExtendSelection",
467: "shift KP_UP",
468: "selectPreviousExtendSelection",
469: "ctrl shift UP",
470: "selectPreviousExtendSelection",
471: "ctrl shift KP_UP",
472: "selectPreviousExtendSelection",
473: "ctrl UP",
474: "selectPreviousChangeLead",
475: "ctrl KP_UP",
476: "selectPreviousChangeLead",
477: "DOWN",
478: "selectNext",
479: "KP_DOWN",
480: "selectNext",
481: "shift DOWN",
482: "selectNextExtendSelection",
483: "shift KP_DOWN",
484: "selectNextExtendSelection",
485: "ctrl shift DOWN",
486: "selectNextExtendSelection",
487: "ctrl shift KP_DOWN",
488: "selectNextExtendSelection",
489: "ctrl DOWN",
490: "selectNextChangeLead",
491: "ctrl KP_DOWN",
492: "selectNextChangeLead",
493: "RIGHT",
494: "selectChild",
495: "KP_RIGHT",
496: "selectChild",
497: "LEFT",
498: "selectParent",
499: "KP_LEFT",
500: "selectParent",
501: "PAGE_UP",
502: "scrollUpChangeSelection",
503: "shift PAGE_UP",
504: "scrollUpExtendSelection",
505: "ctrl shift PAGE_UP",
506: "scrollUpExtendSelection",
507: // "ctrl PAGE_UP", "scrollUpChangeLead",
508: "PAGE_DOWN",
509: "scrollDownChangeSelection",
510: "shift PAGE_DOWN",
511: "scrollDownExtendSelection",
512: "ctrl shift PAGE_DOWN",
513: "scrollDownExtendSelection",
514: // "ctrl PAGE_DOWN", "scrollDownChangeLead",
515: "HOME", "selectFirst", "shift HOME",
516: "selectFirstExtendSelection",
517: "ctrl shift HOME",
518: "selectFirstExtendSelection", "ctrl HOME",
519: "selectFirstChangeLead", "END", "selectLast",
520: "shift END", "selectLastExtendSelection",
521: "ctrl shift END", "selectLastExtendSelection",
522: "ctrl END", "selectLastChangeLead", "F2",
523: "startEditing", "ctrl A", "selectAll",
524: "ctrl SLASH", "selectAll", "ctrl BACK_SLASH",
525: "clearSelection", "ctrl LEFT", "scrollLeft",
526: "ctrl KP_LEFT", "scrollLeft", "ctrl RIGHT",
527: "scrollRight", "ctrl KP_RIGHT", "scrollRight",
528: "SPACE", "addToSelection", "ctrl SPACE",
529: "toggleAndAnchor", "shift SPACE", "extendTo",
530: "ctrl shift SPACE", "moveSelectionTo" }), };
531: Object[] res = new Object[uiDefaults.length + inputMaps.length];
532: System.arraycopy(uiDefaults, 0, res, 0, uiDefaults.length);
533: System.arraycopy(inputMaps, 0, res, uiDefaults.length,
534: inputMaps.length);
535: return res;
536: }
537: }
|