Mercurial > hg > Applications > TightVNC_orginal
comparison src/viewer_swing/java/com/glavsoft/viewer/swing/Utils.java @ 52:472a9bcacb21 draft default tip
TightVNC 2.7.1.0
author | you@cr.ie.u-ryukyu.ac.jp |
---|---|
date | Wed, 07 Aug 2013 19:01:17 +0900 |
parents | 4689cc86d6cb |
children |
comparison
equal
deleted
inserted
replaced
0:4689cc86d6cb | 52:472a9bcacb21 |
---|---|
1 // Copyright (C) 2010, 2011 GlavSoft LLC. | 1 // Copyright (C) 2010, 2011, 2012, 2013 GlavSoft LLC. |
2 // All rights reserved. | 2 // All rights reserved. |
3 // | 3 // |
4 //------------------------------------------------------------------------- | 4 //------------------------------------------------------------------------- |
5 // This file is part of the TightVNC software. Please visit our Web site: | 5 // This file is part of the TightVNC software. Please visit our Web site: |
6 // | 6 // |
22 //------------------------------------------------------------------------- | 22 //------------------------------------------------------------------------- |
23 // | 23 // |
24 | 24 |
25 package com.glavsoft.viewer.swing; | 25 package com.glavsoft.viewer.swing; |
26 | 26 |
27 import java.awt.Image; | 27 import javax.swing.*; |
28 import java.awt.Toolkit; | 28 import java.awt.*; |
29 import java.awt.image.ImageObserver; | |
29 import java.net.URL; | 30 import java.net.URL; |
31 import java.util.HashMap; | |
30 import java.util.LinkedList; | 32 import java.util.LinkedList; |
31 import java.util.List; | 33 import java.util.List; |
32 | 34 import java.util.Map; |
33 import javax.swing.ImageIcon; | 35 import java.util.concurrent.CountDownLatch; |
36 import java.util.concurrent.TimeUnit; | |
34 | 37 |
35 /** | 38 /** |
36 * Utils for Swing GUI | 39 * Utils for Swing GUI |
37 */ | 40 */ |
38 public class Utils { | 41 public class Utils { |
39 private static List<Image> icons; | 42 private static List<Image> icons; |
40 | 43 |
41 /** | 44 private static List<Image> getApplicationIcons() { |
42 * Get application icons | |
43 * | |
44 * @return icons list | |
45 */ | |
46 public static List<Image> getIcons() { | |
47 if (icons != null) { | 45 if (icons != null) { |
48 return icons; | 46 return icons; |
49 } | 47 } |
50 icons = new LinkedList<Image>(); | 48 icons = new LinkedList<Image>(); |
51 URL resource = Utils.class.getResource("/com/glavsoft/viewer/images/tightvnc-logo-16x16.png"); | 49 URL resource = Utils.class.getResource("/com/glavsoft/viewer/images/tightvnc-logo-16x16.png"); |
67 | 65 |
68 public static ImageIcon getButtonIcon(String name) { | 66 public static ImageIcon getButtonIcon(String name) { |
69 URL resource = Utils.class.getResource("/com/glavsoft/viewer/images/button-"+name+".png"); | 67 URL resource = Utils.class.getResource("/com/glavsoft/viewer/images/button-"+name+".png"); |
70 return resource != null ? new ImageIcon(resource) : null; | 68 return resource != null ? new ImageIcon(resource) : null; |
71 } | 69 } |
70 | |
71 private static Map<LocalMouseCursorShape, Cursor> cursorCash = new HashMap<LocalMouseCursorShape, Cursor>(); | |
72 public static Cursor getCursor(LocalMouseCursorShape cursorShape) { | |
73 Cursor cursor = cursorCash.get(cursorShape); | |
74 if (cursor != null) return cursor; | |
75 String name = cursorShape.getCursorName(); | |
76 URL resource = Utils.class.getResource("/com/glavsoft/viewer/images/cursor-"+name+".png"); | |
77 if (resource != null) { | |
78 Image image = Toolkit.getDefaultToolkit().getImage(resource); | |
79 if (image != null) { | |
80 final CountDownLatch done = new CountDownLatch(1); | |
81 image.getWidth(new ImageObserver() { | |
82 @Override | |
83 public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) { | |
84 boolean isReady = (infoflags & (ALLBITS | ABORT)) != 0; | |
85 if (isReady) { | |
86 done.countDown(); | |
87 } | |
88 return ! isReady; | |
89 } | |
90 }); | |
91 try { | |
92 done.await(3, TimeUnit.SECONDS); | |
93 } catch (InterruptedException e) { | |
94 return Cursor.getDefaultCursor(); | |
95 } | |
96 int w = image.getWidth(null); | |
97 int h = image.getHeight(null); | |
98 if (w < 0 || h < 0) return Cursor.getDefaultCursor(); | |
99 w = (int)((w-0.5) / 2); | |
100 h = (int)((h-0.5) / 2); | |
101 cursor = Toolkit.getDefaultToolkit().createCustomCursor( | |
102 image, new Point(w > 0 ? w: 0, h > 0 ? h : 0), name); | |
103 if (cursor != null) cursorCash.put(cursorShape, cursor); | |
104 } | |
105 } | |
106 return cursor != null ? cursor : Cursor.getDefaultCursor(); | |
107 } | |
108 | |
109 public static void decorateDialog(Window dialog) { | |
110 try { | |
111 dialog.setAlwaysOnTop(true); | |
112 } catch (SecurityException e) { | |
113 // nop | |
114 } | |
115 dialog.pack(); | |
116 if (dialog instanceof JDialog) { | |
117 ((JDialog)dialog).setModalityType(Dialog.ModalityType.APPLICATION_MODAL); | |
118 } | |
119 dialog.toFront(); | |
120 Utils.setApplicationIconsForWindow(dialog); | |
121 } | |
122 | |
123 public static void setApplicationIconsForWindow(Window window) { | |
124 List<Image> icons = getApplicationIcons(); | |
125 if (icons.size() != 0) { | |
126 window.setIconImages(icons); | |
127 } | |
128 } | |
129 | |
130 public static void centerWindow(Window window) { | |
131 Point locationPoint = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint(); | |
132 Rectangle bounds = window.getBounds(); | |
133 locationPoint.setLocation(locationPoint.x - bounds.width/2, locationPoint.y - bounds.height/2); | |
134 window.setLocation(locationPoint); | |
135 } | |
72 } | 136 } |