altos/test: Adjust CRC error rate after FEC fix
[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_14;
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         private Container scrollPane;
45
46         public Container getScrollablePane() {
47                 if (scrollPane == null) {
48                         Container content = super.getContentPane();
49                         /* Create a container to hold the dialog contents */
50                         scrollPane = new Container();
51
52                         /* Make an opaque box to use the right color */
53                         Box box = new Box(BoxLayout.X_AXIS);
54                         box.add(scrollPane);
55                         box.setOpaque(true);
56
57                         /* Create a scrollpane to hold the box */
58                         JScrollPane scroll = new JScrollPane();
59                         JViewport view = scroll.getViewport();
60                         view.add(box);
61
62                         /* Add the scroll pane to the top level */
63                         content.add(scroll);
64                 }
65                 return (Container) scrollPane;
66         }
67
68         static String[] altos_icon_names = {
69                 "/altusmetrum-altosui-16.png",
70                 "/altusmetrum-altosui-32.png",
71                 "/altusmetrum-altosui-48.png",
72                 "/altusmetrum-altosui-64.png",
73                 "/altusmetrum-altosui-128.png",
74                 "/altusmetrum-altosui-256.png"
75         };
76
77         static public String[] icon_names;
78
79         static public void set_icon_names(String[] new_icon_names) { icon_names = new_icon_names; }
80
81         public String[] icon_names() {
82                 if (icon_names == null)
83                         set_icon_names(altos_icon_names);
84                 return icon_names;
85         }
86
87         public void set_icon() {
88                 ArrayList<Image> icons = new ArrayList<Image>();
89                 String[] icon_names = icon_names();
90
91                 for (int i = 0; i < icon_names.length; i++) {
92                         java.net.URL imgURL = AltosUIFrame.class.getResource(icon_names[i]);
93                         if (imgURL != null)
94                                 icons.add(new ImageIcon(imgURL).getImage());
95                 }
96                 setIconImages(icons);
97         }
98
99         private boolean location_by_platform = true;
100
101         public void setLocationByPlatform(boolean lbp) {
102                 location_by_platform = lbp;
103                 super.setLocationByPlatform(lbp);
104         }
105
106         public void scan_device_selected(AltosDevice device) {
107         }
108
109         public void setSize() {
110                 /* Smash sizes around so that the window comes up in the right shape */
111                 Insets i = getInsets();
112                 Dimension ps = rootPane.getPreferredSize();
113                 ps.width += i.left + i.right;
114                 ps.height += i.top + i.bottom;
115                 setPreferredSize(ps);
116                 setSize(ps);
117         }
118
119         public void setPosition (int position) {
120                 Insets i = getInsets();
121                 Dimension ps = getSize();
122
123                 /* Stick the window in the desired location on the screen */
124                 setLocationByPlatform(false);
125                 GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
126                 GraphicsConfiguration gc = gd.getDefaultConfiguration();
127                 Rectangle r = gc.getBounds();
128
129                 /* compute X position */
130                 int x = 0;
131                 int y = 0;
132                 switch (position) {
133                 case AltosUILib.position_top_left:
134                 case AltosUILib.position_left:
135                 case AltosUILib.position_bottom_left:
136                         x = 0;
137                         break;
138                 case AltosUILib.position_top:
139                 case AltosUILib.position_center:
140                 case AltosUILib.position_bottom:
141                         x = (r.width - ps.width) / 2;
142                         break;
143                 case AltosUILib.position_top_right:
144                 case AltosUILib.position_right:
145                 case AltosUILib.position_bottom_right:
146                         x = r.width - ps.width + i.right;
147                         break;
148                 }
149
150                 /* compute Y position */
151                 switch (position) {
152                 case AltosUILib.position_top_left:
153                 case AltosUILib.position_top:
154                 case AltosUILib.position_top_right:
155                         y = 0;
156                         break;
157                 case AltosUILib.position_left:
158                 case AltosUILib.position_center:
159                 case AltosUILib.position_right:
160                         y = (r.height - ps.height) / 2;
161                         break;
162                 case AltosUILib.position_bottom_left:
163                 case AltosUILib.position_bottom:
164                 case AltosUILib.position_bottom_right:
165                         y = r.height - ps.height + i.bottom;
166                         break;
167                 }
168                 setLocation(x, y);
169         }
170
171         int position;
172
173         public void position_changed(int position) {
174                 this.position = position;
175                 if (!location_by_platform)
176                         setPosition(position);
177         }
178
179         public void setVisible (boolean visible) {
180                 if (visible)
181                         setLocationByPlatform(location_by_platform);
182                 super.setVisible(visible);
183                 if (visible) {
184                         setSize();
185                         if (!location_by_platform)
186                                 setPosition(position);
187                 }
188         }
189
190         static boolean global_settings_done;
191
192         public String getName() {
193                 return "Altus Metrum";
194         }
195
196         public void macosx_quit_handler() {
197                 System.out.printf("Got quit handler\n");
198         }
199
200         public void macosx_about_handler() {
201                 System.out.printf("Got about handler\n");
202         }
203
204         public void macosx_preferences_handler() {
205                 System.out.printf("Got preferences handler\n");
206         }
207
208         public void macosx_file_handler(String path) {
209                 System.out.printf("Got file handler with \"%s\"\n", path);
210         }
211
212         /* Check that we are on Mac OS X.  This is crucial to loading and using the OSXAdapter class.
213          */
214         public static boolean MAC_OS_X = (System.getProperty("os.name").toLowerCase().startsWith("mac os x"));
215
216         private static boolean registered_for_macosx_events;
217
218         /* Generic registration with the Mac OS X application menu
219          * Checks the platform, then attempts to register with the Apple EAWT
220          * See OSXAdapter.java to see how this is done without directly referencing any Apple APIs
221          */
222         public synchronized void register_for_macosx_events() {
223                 if (registered_for_macosx_events)
224                         return;
225                 registered_for_macosx_events = true;
226                 if (MAC_OS_X) {
227                         try {
228                                 // Generate and register the OSXAdapter, passing it a hash of all the methods we wish to
229                                 // use as delegates for various com.apple.eawt.ApplicationListener methods
230                                 OSXAdapter.setQuitHandler(this, getClass().getDeclaredMethod("macosx_quit_handler", (Class[])null));
231 //                              OSXAdapter.setAboutHandler(this, getClass().getDeclaredMethod("macosx_about_handler", (Class[])null));
232                                 OSXAdapter.setPreferencesHandler(this, getClass().getDeclaredMethod("macosx_preferences_handler", (Class[])null));
233                                 OSXAdapter.setFileHandler(this, getClass().getDeclaredMethod("macosx_file_handler", new Class[] { String.class }));
234                         } catch (Exception e) {
235                                 System.err.println("Error while loading the OSXAdapter:");
236                                 e.printStackTrace();
237                         }
238                 }
239         }
240
241         int row = 0;
242
243         public void next_row() {
244                 row++;
245         }
246
247         int inset = 0;
248
249         public void set_inset(int i) {
250                 inset = i;
251         }
252
253         public GridBagConstraints constraints (int x, int width, int fill, int anchor, double weightx, double weighty) {
254                 return new GridBagConstraints(x,                        /* x */
255                                               row,                      /* y */
256                                               width,                    /* width */
257                                               1,                        /* height */
258                                               weightx,                  /* weightx */
259                                               weighty,                  /* weighty */
260                                               anchor,                   /* anchor */
261                                               fill,                     /* fill */
262                                               new Insets(inset,inset,inset,inset),      /* insets */
263                                               0,                        /* ipadx */
264                                               0);                       /* ipady */
265         }
266
267         public GridBagConstraints constraints (int x, int width, int fill, int anchor) {
268                 double  weightx = 0;
269                 double  weighty = 0;
270
271                 if (fill == GridBagConstraints.NONE) {
272                         weightx = 0;
273                         weighty = 0;
274                 } else if (fill == GridBagConstraints.HORIZONTAL) {
275                         weightx = 1;
276                         weighty = 0;
277                 } else if (fill == GridBagConstraints.VERTICAL) {
278                         weightx = 0;
279                         weighty = 1;
280                 } else if (fill == GridBagConstraints.BOTH) {
281                         weightx = 1;
282                         weighty = 1;
283                 }
284
285                 return constraints (x, width, fill, anchor, weightx, weighty);
286         }
287
288         public GridBagConstraints constraints (int x, int width, int fill) {
289                 return constraints (x, width, fill, GridBagConstraints.WEST);
290         }
291
292         public GridBagConstraints constraints(int x, int width) {
293                 return constraints(x, width, GridBagConstraints.NONE);
294         }
295
296         static int open_frames;
297
298         public static void frame_opened() {
299                 ++open_frames;
300         }
301
302         public static void frame_closed() {
303                 --open_frames;
304                 if (open_frames == 0)
305                         System.exit(0);
306         }
307
308         void init() {
309                 AltosUIPreferences.register_ui_listener(this);
310                 AltosUIPreferences.register_position_listener(this);
311                 position = AltosUIPreferences.position();
312                 frame_opened();
313                 addWindowListener(new AltosUIFrameListener());
314
315                 /* Try to make menus live in the menu bar like regular Mac apps */
316                 if (!global_settings_done) {
317                         try {
318                                 global_settings_done = true;
319                                 System.setProperty("com.apple.mrj.application.apple.menu.about.name", getName());
320                                 System.setProperty("com.apple.macos.useScreenMenuBar", "true");
321                                 System.setProperty("apple.laf.useScreenMenuBar", "true" ); // for older versions of Java
322                         } catch (Exception e) {
323                         }
324                 }
325                 set_icon();
326         }
327
328         public AltosUIFrame() {
329                 init();
330         }
331
332         public AltosUIFrame(String name) {
333                 super(name);
334                 init();
335         }
336 }