diff 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
line wrap: on
line diff
--- a/src/viewer_swing/java/com/glavsoft/viewer/swing/Utils.java	Tue Jul 03 13:20:49 2012 +0900
+++ b/src/viewer_swing/java/com/glavsoft/viewer/swing/Utils.java	Wed Aug 07 19:01:17 2013 +0900
@@ -1,4 +1,4 @@
-// Copyright (C) 2010, 2011 GlavSoft LLC.
+// Copyright (C) 2010, 2011, 2012, 2013 GlavSoft LLC.
 // All rights reserved.
 //
 //-------------------------------------------------------------------------
@@ -24,13 +24,16 @@
 
 package com.glavsoft.viewer.swing;
 
-import java.awt.Image;
-import java.awt.Toolkit;
+import javax.swing.*;
+import java.awt.*;
+import java.awt.image.ImageObserver;
 import java.net.URL;
+import java.util.HashMap;
 import java.util.LinkedList;
 import java.util.List;
-
-import javax.swing.ImageIcon;
+import java.util.Map;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
 
 /**
  * Utils for Swing GUI
@@ -38,12 +41,7 @@
 public class Utils {
 	private static List<Image> icons;
 
-	/**
-	 * Get application icons
-	 *
-	 * @return icons list
-	 */
-	public static List<Image> getIcons() {
+	private static List<Image> getApplicationIcons() {
 		if (icons != null) {
 			return icons;
 		}
@@ -69,4 +67,70 @@
 		URL resource = Utils.class.getResource("/com/glavsoft/viewer/images/button-"+name+".png");
 		return resource != null ? new ImageIcon(resource) : null;
 	}
+
+    private static Map<LocalMouseCursorShape, Cursor> cursorCash = new HashMap<LocalMouseCursorShape, Cursor>();
+    public static Cursor getCursor(LocalMouseCursorShape cursorShape) {
+        Cursor cursor = cursorCash.get(cursorShape);
+        if (cursor != null) return cursor;
+        String name = cursorShape.getCursorName();
+        URL resource = Utils.class.getResource("/com/glavsoft/viewer/images/cursor-"+name+".png");
+        if (resource != null) {
+            Image image = Toolkit.getDefaultToolkit().getImage(resource);
+            if (image != null) {
+                final CountDownLatch done = new CountDownLatch(1);
+                image.getWidth(new ImageObserver() {
+                    @Override
+                    public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) {
+                        boolean isReady = (infoflags & (ALLBITS | ABORT)) != 0;
+                        if (isReady) {
+                            done.countDown();
+                        }
+                        return ! isReady;
+                    }
+                });
+                try {
+                    done.await(3, TimeUnit.SECONDS);
+                } catch (InterruptedException e) {
+                    return Cursor.getDefaultCursor();
+                }
+                int w = image.getWidth(null);
+                int h = image.getHeight(null);
+                if (w < 0 || h < 0) return Cursor.getDefaultCursor();
+                w = (int)((w-0.5) / 2);
+                h = (int)((h-0.5) / 2);
+                cursor = Toolkit.getDefaultToolkit().createCustomCursor(
+                        image, new Point(w > 0 ? w: 0, h > 0 ? h : 0), name);
+                if (cursor != null) cursorCash.put(cursorShape, cursor);
+            }
+        }
+        return cursor != null ? cursor : Cursor.getDefaultCursor();
+    }
+
+    public static void decorateDialog(Window dialog) {
+        try {
+            dialog.setAlwaysOnTop(true);
+        } catch (SecurityException e) {
+            // nop
+        }
+		dialog.pack();
+        if (dialog instanceof JDialog) {
+		    ((JDialog)dialog).setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
+        }
+        dialog.toFront();
+		Utils.setApplicationIconsForWindow(dialog);
+	}
+
+	public static void setApplicationIconsForWindow(Window window) {
+		List<Image> icons = getApplicationIcons();
+		if (icons.size() != 0) {
+			window.setIconImages(icons);
+		}
+	}
+
+	public static void centerWindow(Window window) {
+        Point locationPoint = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
+		Rectangle bounds = window.getBounds();
+		locationPoint.setLocation(locationPoint.x - bounds.width/2, locationPoint.y - bounds.height/2);
+		window.setLocation(locationPoint);
+	}
 }