altosuilib: Track open frames, exit when none remain
[fw/altos] / altosuilib / AltosUIFrame.java
1 /*
2  * Copyright © 2011 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 package org.altusmetrum.altosuilib_11;
19
20 import java.awt.*;
21 import java.awt.event.*;
22 import javax.swing.*;
23 import java.util.*;
24
25 class AltosUIFrameListener extends WindowAdapter {
26         @Override
27         public void windowClosing (WindowEvent e) {
28                 AltosUIFrame frame = (AltosUIFrame) e.getWindow();
29                 AltosUIPreferences.unregister_ui_listener(frame);
30                 AltosUIFrame.frame_closed();
31                 frame.setVisible(false);
32                 frame.dispose();
33         }
34 }
35
36 public class AltosUIFrame extends JFrame implements AltosUIListener, AltosPositionListener {
37
38         public void ui_changed(String look_and_feel) {
39                 SwingUtilities.updateComponentTreeUI(this);
40                 this.pack();
41         }
42
43         static String[] altos_icon_names = {
44                 "/altusmetrum-altosui-16.png",
45                 "/altusmetrum-altosui-32.png",
46                 "/altusmetrum-altosui-48.png",
47                 "/altusmetrum-altosui-64.png",
48                 "/altusmetrum-altosui-128.png",
49                 "/altusmetrum-altosui-256.png"
50         };
51
52         static public String[] icon_names;
53
54         static public void set_icon_names(String[] new_icon_names) { icon_names = new_icon_names; }
55
56         public String[] icon_names() {
57                 if (icon_names == null)
58                         set_icon_names(altos_icon_names);
59                 return icon_names;
60         }
61
62         public void set_icon() {
63                 ArrayList<Image> icons = new ArrayList<Image>();
64                 String[] icon_names = icon_names();
65
66                 for (int i = 0; i < icon_names.length; i++) {
67                         java.net.URL imgURL = AltosUIFrame.class.getResource(icon_names[i]);
68                         if (imgURL != null)
69                                 icons.add(new ImageIcon(imgURL).getImage());
70                 }
71                 setIconImages(icons);
72         }
73
74         private boolean location_by_platform = true;
75
76         public void setLocationByPlatform(boolean lbp) {
77                 location_by_platform = lbp;
78                 super.setLocationByPlatform(lbp);
79         }
80
81         public void scan_device_selected(AltosDevice device) {
82         }
83
84         public void setSize() {
85                 /* Smash sizes around so that the window comes up in the right shape */
86                 Insets i = getInsets();
87                 Dimension ps = rootPane.getPreferredSize();
88                 ps.width += i.left + i.right;
89                 ps.height += i.top + i.bottom;
90                 setPreferredSize(ps);
91                 setSize(ps);
92         }
93
94         public void setPosition (int position) {
95                 Insets i = getInsets();
96                 Dimension ps = getSize();
97
98                 /* Stick the window in the desired location on the screen */
99                 setLocationByPlatform(false);
100                 GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
101                 GraphicsConfiguration gc = gd.getDefaultConfiguration();
102                 Rectangle r = gc.getBounds();
103
104                 /* compute X position */
105                 int x = 0;
106                 int y = 0;
107                 switch (position) {
108                 case AltosUILib.position_top_left:
109                 case AltosUILib.position_left:
110                 case AltosUILib.position_bottom_left:
111                         x = 0;
112                         break;
113                 case AltosUILib.position_top:
114                 case AltosUILib.position_center:
115                 case AltosUILib.position_bottom:
116                         x = (r.width - ps.width) / 2;
117                         break;
118                 case AltosUILib.position_top_right:
119                 case AltosUILib.position_right:
120                 case AltosUILib.position_bottom_right:
121                         x = r.width - ps.width + i.right;
122                         break;
123                 }
124
125                 /* compute Y position */
126                 switch (position) {
127                 case AltosUILib.position_top_left:
128                 case AltosUILib.position_top:
129                 case AltosUILib.position_top_right:
130                         y = 0;
131                         break;
132                 case AltosUILib.position_left:
133                 case AltosUILib.position_center:
134                 case AltosUILib.position_right:
135                         y = (r.height - ps.height) / 2;
136                         break;
137                 case AltosUILib.position_bottom_left:
138                 case AltosUILib.position_bottom:
139                 case AltosUILib.position_bottom_right:
140                         y = r.height - ps.height + i.bottom;
141                         break;
142                 }
143                 setLocation(x, y);
144         }
145
146         int position;
147
148         public void position_changed(int position) {
149                 this.position = position;
150                 if (!location_by_platform)
151                         setPosition(position);
152         }
153
154         public void setVisible (boolean visible) {
155                 if (visible)
156                         setLocationByPlatform(location_by_platform);
157                 super.setVisible(visible);
158                 if (visible) {
159                         setSize();
160                         if (!location_by_platform)
161                                 setPosition(position);
162                 }
163         }
164
165         static boolean global_settings_done;
166
167         public String getName() {
168                 return "Altus Metrum";
169         }
170
171         public void macosx_quit_handler() {
172                 System.out.printf("Got quit handler\n");
173         }
174
175         public void macosx_about_handler() {
176                 System.out.printf("Got about handler\n");
177         }
178
179         public void macosx_preferences_handler() {
180                 System.out.printf("Got preferences handler\n");
181         }
182
183         public void macosx_file_handler(String path) {
184                 System.out.printf("Got file handler with \"%s\"\n", path);
185         }
186
187         /* Check that we are on Mac OS X.  This is crucial to loading and using the OSXAdapter class.
188          */
189         public static boolean MAC_OS_X = (System.getProperty("os.name").toLowerCase().startsWith("mac os x"));
190
191         private static boolean registered_for_macosx_events;
192
193         /* Generic registration with the Mac OS X application menu
194          * Checks the platform, then attempts to register with the Apple EAWT
195          * See OSXAdapter.java to see how this is done without directly referencing any Apple APIs
196          */
197         public synchronized void register_for_macosx_events() {
198                 if (registered_for_macosx_events)
199                         return;
200                 registered_for_macosx_events = true;
201                 if (MAC_OS_X) {
202                         try {
203                                 // Generate and register the OSXAdapter, passing it a hash of all the methods we wish to
204                                 // use as delegates for various com.apple.eawt.ApplicationListener methods
205                                 OSXAdapter.setQuitHandler(this, getClass().getDeclaredMethod("macosx_quit_handler", (Class[])null));
206 //                              OSXAdapter.setAboutHandler(this, getClass().getDeclaredMethod("macosx_about_handler", (Class[])null));
207                                 OSXAdapter.setPreferencesHandler(this, getClass().getDeclaredMethod("macosx_preferences_handler", (Class[])null));
208                                 OSXAdapter.setFileHandler(this, getClass().getDeclaredMethod("macosx_file_handler", new Class[] { String.class }));
209                         } catch (Exception e) {
210                                 System.err.println("Error while loading the OSXAdapter:");
211                                 e.printStackTrace();
212                         }
213                 }
214         }
215
216         int row = 0;
217
218         public void next_row() {
219                 row++;
220         }
221
222         int inset = 0;
223
224         public void set_inset(int i) {
225                 inset = i;
226         }
227
228         public GridBagConstraints constraints (int x, int width, int fill, int anchor, double weightx, double weighty) {
229                 return new GridBagConstraints(x,                        /* x */
230                                               row,                      /* y */
231                                               width,                    /* width */
232                                               1,                        /* height */
233                                               weightx,                  /* weightx */
234                                               weighty,                  /* weighty */
235                                               anchor,                   /* anchor */
236                                               fill,                     /* fill */
237                                               new Insets(inset,inset,inset,inset),      /* insets */
238                                               0,                        /* ipadx */
239                                               0);                       /* ipady */
240         }
241
242         public GridBagConstraints constraints (int x, int width, int fill, int anchor) {
243                 double  weightx = 0;
244                 double  weighty = 0;
245
246                 if (fill == GridBagConstraints.NONE) {
247                         weightx = 0;
248                         weighty = 0;
249                 } else if (fill == GridBagConstraints.HORIZONTAL) {
250                         weightx = 1;
251                         weighty = 0;
252                 } else if (fill == GridBagConstraints.VERTICAL) {
253                         weightx = 0;
254                         weighty = 1;
255                 } else if (fill == GridBagConstraints.BOTH) {
256                         weightx = 1;
257                         weighty = 1;
258                 }
259
260                 return constraints (x, width, fill, anchor, weightx, weighty);
261         }
262
263         public GridBagConstraints constraints (int x, int width, int fill) {
264                 return constraints (x, width, fill, GridBagConstraints.WEST);
265         }
266
267         public GridBagConstraints constraints(int x, int width) {
268                 return constraints(x, width, GridBagConstraints.NONE);
269         }
270
271         static int open_frames;
272
273         public static void frame_opened() {
274                 ++open_frames;
275         }
276
277         public static void frame_closed() {
278                 --open_frames;
279                 if (open_frames == 0)
280                         System.exit(0);
281         }
282
283         void init() {
284                 AltosUIPreferences.register_ui_listener(this);
285                 AltosUIPreferences.register_position_listener(this);
286                 position = AltosUIPreferences.position();
287                 frame_opened();
288                 addWindowListener(new AltosUIFrameListener());
289
290                 /* Try to make menus live in the menu bar like regular Mac apps */
291                 if (!global_settings_done) {
292                         try {
293                                 global_settings_done = true;
294                                 System.setProperty("com.apple.mrj.application.apple.menu.about.name", getName());
295                                 System.setProperty("com.apple.macos.useScreenMenuBar", "true");
296                                 System.setProperty("apple.laf.useScreenMenuBar", "true" ); // for older versions of Java
297                         } catch (Exception e) {
298                         }
299                 }
300                 set_icon();
301         }
302
303         public AltosUIFrame() {
304                 init();
305         }
306
307         public AltosUIFrame(String name) {
308                 super(name);
309                 init();
310         }
311 }