2 * Copyright © 2011 Keith Packard <keithp@keithp.com>
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.
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.
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.
19 package org.altusmetrum.altosuilib_13;
22 import java.awt.event.*;
26 class AltosUIFrameListener extends WindowAdapter {
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);
37 public class AltosUIFrame extends JFrame implements AltosUIListener, AltosPositionListener {
39 public void ui_changed(String look_and_feel) {
40 SwingUtilities.updateComponentTreeUI(this);
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"
53 static public String[] icon_names;
55 static public void set_icon_names(String[] new_icon_names) { icon_names = new_icon_names; }
57 public String[] icon_names() {
58 if (icon_names == null)
59 set_icon_names(altos_icon_names);
63 public void set_icon() {
64 ArrayList<Image> icons = new ArrayList<Image>();
65 String[] icon_names = icon_names();
67 for (int i = 0; i < icon_names.length; i++) {
68 java.net.URL imgURL = AltosUIFrame.class.getResource(icon_names[i]);
70 icons.add(new ImageIcon(imgURL).getImage());
75 private boolean location_by_platform = true;
77 public void setLocationByPlatform(boolean lbp) {
78 location_by_platform = lbp;
79 super.setLocationByPlatform(lbp);
82 public void scan_device_selected(AltosDevice device) {
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;
95 public void setPosition (int position) {
96 Insets i = getInsets();
97 Dimension ps = getSize();
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();
105 /* compute X position */
109 case AltosUILib.position_top_left:
110 case AltosUILib.position_left:
111 case AltosUILib.position_bottom_left:
114 case AltosUILib.position_top:
115 case AltosUILib.position_center:
116 case AltosUILib.position_bottom:
117 x = (r.width - ps.width) / 2;
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;
126 /* compute Y position */
128 case AltosUILib.position_top_left:
129 case AltosUILib.position_top:
130 case AltosUILib.position_top_right:
133 case AltosUILib.position_left:
134 case AltosUILib.position_center:
135 case AltosUILib.position_right:
136 y = (r.height - ps.height) / 2;
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;
149 public void position_changed(int position) {
150 this.position = position;
151 if (!location_by_platform)
152 setPosition(position);
155 public void setVisible (boolean visible) {
157 setLocationByPlatform(location_by_platform);
158 super.setVisible(visible);
161 if (!location_by_platform)
162 setPosition(position);
166 static boolean global_settings_done;
168 public String getName() {
169 return "Altus Metrum";
172 public void macosx_quit_handler() {
173 System.out.printf("Got quit handler\n");
176 public void macosx_about_handler() {
177 System.out.printf("Got about handler\n");
180 public void macosx_preferences_handler() {
181 System.out.printf("Got preferences handler\n");
184 public void macosx_file_handler(String path) {
185 System.out.printf("Got file handler with \"%s\"\n", path);
188 /* Check that we are on Mac OS X. This is crucial to loading and using the OSXAdapter class.
190 public static boolean MAC_OS_X = (System.getProperty("os.name").toLowerCase().startsWith("mac os x"));
192 private static boolean registered_for_macosx_events;
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
198 public synchronized void register_for_macosx_events() {
199 if (registered_for_macosx_events)
201 registered_for_macosx_events = true;
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:");
219 public void next_row() {
225 public void set_inset(int i) {
229 public GridBagConstraints constraints (int x, int width, int fill, int anchor, double weightx, double weighty) {
230 return new GridBagConstraints(x, /* x */
234 weightx, /* weightx */
235 weighty, /* weighty */
238 new Insets(inset,inset,inset,inset), /* insets */
243 public GridBagConstraints constraints (int x, int width, int fill, int anchor) {
247 if (fill == GridBagConstraints.NONE) {
250 } else if (fill == GridBagConstraints.HORIZONTAL) {
253 } else if (fill == GridBagConstraints.VERTICAL) {
256 } else if (fill == GridBagConstraints.BOTH) {
261 return constraints (x, width, fill, anchor, weightx, weighty);
264 public GridBagConstraints constraints (int x, int width, int fill) {
265 return constraints (x, width, fill, GridBagConstraints.WEST);
268 public GridBagConstraints constraints(int x, int width) {
269 return constraints(x, width, GridBagConstraints.NONE);
272 static int open_frames;
274 public static void frame_opened() {
278 public static void frame_closed() {
280 if (open_frames == 0)
285 AltosUIPreferences.register_ui_listener(this);
286 AltosUIPreferences.register_position_listener(this);
287 position = AltosUIPreferences.position();
289 addWindowListener(new AltosUIFrameListener());
291 /* Try to make menus live in the menu bar like regular Mac apps */
292 if (!global_settings_done) {
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) {
304 public AltosUIFrame() {
308 public AltosUIFrame(String name) {