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