1 /*
2  * Copyright (c) 2007, 2016, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 package com.sun.swingset3.demos;
24 
25 import java.awt.*;
26 import java.net.URI;
27 import java.io.IOException;
28 import javax.swing.*;
29 
30 /**
31  * @author Pavel Porvatov
32  */
33 public class DemoUtilities {
34 
DemoUtilities()35     private DemoUtilities() {
36         // Hide constructor
37     }
38 
setToplevelLocation(Window toplevel, Component component, int relativePosition)39     public static void setToplevelLocation(Window toplevel, Component component,
40             int relativePosition) {
41 
42         Rectangle compBounds = component.getBounds();
43 
44         // Convert component location to screen coordinates
45         Point p = new Point();
46         SwingUtilities.convertPointToScreen(p, component);
47 
48         int x;
49         int y;
50 
51         // Set frame location to be centered on panel
52         switch (relativePosition) {
53             case SwingConstants.NORTH: {
54                 x = (p.x + (compBounds.width / 2)) - (toplevel.getWidth() / 2);
55                 y = p.y - toplevel.getHeight();
56                 break;
57             }
58             case SwingConstants.EAST: {
59                 x = p.x + compBounds.width;
60                 y = (p.y + (compBounds.height / 2)) - (toplevel.getHeight() / 2);
61                 break;
62             }
63             case SwingConstants.SOUTH: {
64                 x = (p.x + (compBounds.width / 2)) - (toplevel.getWidth() / 2);
65                 y = p.y + compBounds.height;
66                 break;
67             }
68             case SwingConstants.WEST: {
69                 x = p.x - toplevel.getWidth();
70                 y = (p.y + (compBounds.height / 2)) - (toplevel.getHeight() / 2);
71                 break;
72             }
73             case SwingConstants.NORTH_EAST: {
74                 x = p.x + compBounds.width;
75                 y = p.y - toplevel.getHeight();
76                 break;
77             }
78             case SwingConstants.NORTH_WEST: {
79                 x = p.x - toplevel.getWidth();
80                 y = p.y - toplevel.getHeight();
81                 break;
82             }
83             case SwingConstants.SOUTH_EAST: {
84                 x = p.x + compBounds.width;
85                 y = p.y + compBounds.height;
86                 break;
87             }
88             case SwingConstants.SOUTH_WEST: {
89                 x = p.x - toplevel.getWidth();
90                 y = p.y + compBounds.height;
91                 break;
92             }
93             default:
94             case SwingConstants.CENTER: {
95                 x = (p.x + (compBounds.width / 2)) - (toplevel.getWidth() / 2);
96                 y = (p.y + (compBounds.height / 2)) - (toplevel.getHeight() / 2);
97             }
98         }
99         toplevel.setLocation(x, y);
100     }
101 
browse(URI uri)102     public static boolean browse(URI uri) throws IOException {
103         // Try using the Desktop api first
104         try {
105             Desktop desktop = Desktop.getDesktop();
106             desktop.browse(uri);
107 
108             return true;
109         } catch (SecurityException e) {
110             e.printStackTrace();
111         }
112 
113         return false;
114     }
115 }
116