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; version 2 of the License.
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.
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.
18 package org.altusmetrum.altosuilib_11;
21 import java.awt.event.*;
25 class AltosUIFrameListener extends WindowAdapter {
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);
36 public class AltosUIFrame extends JFrame implements AltosUIListener, AltosPositionListener {
38 public void ui_changed(String look_and_feel) {
39 SwingUtilities.updateComponentTreeUI(this);
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"
52 static public String[] icon_names;
54 static public void set_icon_names(String[] new_icon_names) { icon_names = new_icon_names; }
56 public String[] icon_names() {
57 if (icon_names == null)
58 set_icon_names(altos_icon_names);
62 public void set_icon() {
63 ArrayList<Image> icons = new ArrayList<Image>();
64 String[] icon_names = icon_names();
66 for (int i = 0; i < icon_names.length; i++) {
67 java.net.URL imgURL = AltosUIFrame.class.getResource(icon_names[i]);
69 icons.add(new ImageIcon(imgURL).getImage());
74 private boolean location_by_platform = true;
76 public void setLocationByPlatform(boolean lbp) {
77 location_by_platform = lbp;
78 super.setLocationByPlatform(lbp);
81 public void scan_device_selected(AltosDevice device) {
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;
94 public void setPosition (int position) {
95 Insets i = getInsets();
96 Dimension ps = getSize();
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();
104 /* compute X position */
108 case AltosUILib.position_top_left:
109 case AltosUILib.position_left:
110 case AltosUILib.position_bottom_left:
113 case AltosUILib.position_top:
114 case AltosUILib.position_center:
115 case AltosUILib.position_bottom:
116 x = (r.width - ps.width) / 2;
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;
125 /* compute Y position */
127 case AltosUILib.position_top_left:
128 case AltosUILib.position_top:
129 case AltosUILib.position_top_right:
132 case AltosUILib.position_left:
133 case AltosUILib.position_center:
134 case AltosUILib.position_right:
135 y = (r.height - ps.height) / 2;
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;
148 public void position_changed(int position) {
149 this.position = position;
150 if (!location_by_platform)
151 setPosition(position);
154 public void setVisible (boolean visible) {
156 setLocationByPlatform(location_by_platform);
157 super.setVisible(visible);
160 if (!location_by_platform)
161 setPosition(position);
165 static boolean global_settings_done;
167 public String getName() {
168 return "Altus Metrum";
171 public void macosx_quit_handler() {
172 System.out.printf("Got quit handler\n");
175 public void macosx_about_handler() {
176 System.out.printf("Got about handler\n");
179 public void macosx_preferences_handler() {
180 System.out.printf("Got preferences handler\n");
183 public void macosx_file_handler(String path) {
184 System.out.printf("Got file handler with \"%s\"\n", path);
187 /* Check that we are on Mac OS X. This is crucial to loading and using the OSXAdapter class.
189 public static boolean MAC_OS_X = (System.getProperty("os.name").toLowerCase().startsWith("mac os x"));
191 private static boolean registered_for_macosx_events;
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
197 public synchronized void register_for_macosx_events() {
198 if (registered_for_macosx_events)
200 registered_for_macosx_events = true;
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:");
218 public void next_row() {
224 public void set_inset(int i) {
228 public GridBagConstraints constraints (int x, int width, int fill, int anchor, double weightx, double weighty) {
229 return new GridBagConstraints(x, /* x */
233 weightx, /* weightx */
234 weighty, /* weighty */
237 new Insets(inset,inset,inset,inset), /* insets */
242 public GridBagConstraints constraints (int x, int width, int fill, int anchor) {
246 if (fill == GridBagConstraints.NONE) {
249 } else if (fill == GridBagConstraints.HORIZONTAL) {
252 } else if (fill == GridBagConstraints.VERTICAL) {
255 } else if (fill == GridBagConstraints.BOTH) {
260 return constraints (x, width, fill, anchor, weightx, weighty);
263 public GridBagConstraints constraints (int x, int width, int fill) {
264 return constraints (x, width, fill, GridBagConstraints.WEST);
267 public GridBagConstraints constraints(int x, int width) {
268 return constraints(x, width, GridBagConstraints.NONE);
271 static int open_frames;
273 public static void frame_opened() {
277 public static void frame_closed() {
279 if (open_frames == 0)
284 AltosUIPreferences.register_ui_listener(this);
285 AltosUIPreferences.register_position_listener(this);
286 position = AltosUIPreferences.position();
288 addWindowListener(new AltosUIFrameListener());
290 /* Try to make menus live in the menu bar like regular Mac apps */
291 if (!global_settings_done) {
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) {
303 public AltosUIFrame() {
307 public AltosUIFrame(String name) {