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_14;
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 private Container scrollPane;
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();
52 /* Make an opaque box to use the right color */
53 Box box = new Box(BoxLayout.X_AXIS);
57 /* Create a scrollpane to hold the box */
58 JScrollPane scroll = new JScrollPane();
59 JViewport view = scroll.getViewport();
62 /* Add the scroll pane to the top level */
65 return (Container) scrollPane;
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"
77 static public String[] icon_names;
79 static public void set_icon_names(String[] new_icon_names) { icon_names = new_icon_names; }
81 public String[] icon_names() {
82 if (icon_names == null)
83 set_icon_names(altos_icon_names);
87 public void set_icon() {
88 ArrayList<Image> icons = new ArrayList<Image>();
89 String[] icon_names = icon_names();
91 for (int i = 0; i < icon_names.length; i++) {
92 java.net.URL imgURL = AltosUIFrame.class.getResource(icon_names[i]);
94 icons.add(new ImageIcon(imgURL).getImage());
99 private boolean location_by_platform = true;
101 public void setLocationByPlatform(boolean lbp) {
102 location_by_platform = lbp;
103 super.setLocationByPlatform(lbp);
106 public void scan_device_selected(AltosDevice device) {
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);
119 public void setPosition (int position) {
120 Insets i = getInsets();
121 Dimension ps = getSize();
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();
129 /* compute X position */
133 case AltosUILib.position_top_left:
134 case AltosUILib.position_left:
135 case AltosUILib.position_bottom_left:
138 case AltosUILib.position_top:
139 case AltosUILib.position_center:
140 case AltosUILib.position_bottom:
141 x = (r.width - ps.width) / 2;
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;
150 /* compute Y position */
152 case AltosUILib.position_top_left:
153 case AltosUILib.position_top:
154 case AltosUILib.position_top_right:
157 case AltosUILib.position_left:
158 case AltosUILib.position_center:
159 case AltosUILib.position_right:
160 y = (r.height - ps.height) / 2;
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;
173 public void position_changed(int position) {
174 this.position = position;
175 if (!location_by_platform)
176 setPosition(position);
179 public void setVisible (boolean visible) {
181 setLocationByPlatform(location_by_platform);
182 super.setVisible(visible);
185 if (!location_by_platform)
186 setPosition(position);
190 static boolean global_settings_done;
192 public String getName() {
193 return "Altus Metrum";
196 public void macosx_quit_handler() {
197 System.out.printf("Got quit handler\n");
200 public void macosx_about_handler() {
201 System.out.printf("Got about handler\n");
204 public void macosx_preferences_handler() {
205 System.out.printf("Got preferences handler\n");
208 public void macosx_file_handler(String path) {
209 System.out.printf("Got file handler with \"%s\"\n", path);
212 /* Check that we are on Mac OS X. This is crucial to loading and using the OSXAdapter class.
214 public static boolean MAC_OS_X = (System.getProperty("os.name").toLowerCase().startsWith("mac os x"));
216 private static boolean registered_for_macosx_events;
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
222 public synchronized void register_for_macosx_events() {
223 if (registered_for_macosx_events)
225 registered_for_macosx_events = true;
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:");
243 public void next_row() {
249 public void set_inset(int i) {
253 public GridBagConstraints constraints (int x, int width, int fill, int anchor, double weightx, double weighty) {
254 return new GridBagConstraints(x, /* x */
258 weightx, /* weightx */
259 weighty, /* weighty */
262 new Insets(inset,inset,inset,inset), /* insets */
267 public GridBagConstraints constraints (int x, int width, int fill, int anchor) {
271 if (fill == GridBagConstraints.NONE) {
274 } else if (fill == GridBagConstraints.HORIZONTAL) {
277 } else if (fill == GridBagConstraints.VERTICAL) {
280 } else if (fill == GridBagConstraints.BOTH) {
285 return constraints (x, width, fill, anchor, weightx, weighty);
288 public GridBagConstraints constraints (int x, int width, int fill) {
289 return constraints (x, width, fill, GridBagConstraints.WEST);
292 public GridBagConstraints constraints(int x, int width) {
293 return constraints(x, width, GridBagConstraints.NONE);
296 static int open_frames;
298 public static void frame_opened() {
302 public static void frame_closed() {
304 if (open_frames == 0)
309 AltosUIPreferences.register_ui_listener(this);
310 AltosUIPreferences.register_position_listener(this);
311 position = AltosUIPreferences.position();
313 addWindowListener(new AltosUIFrameListener());
315 /* Try to make menus live in the menu bar like regular Mac apps */
316 if (!global_settings_done) {
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) {
328 public AltosUIFrame() {
332 public AltosUIFrame(String name) {