Create altosuilib to share code between altosui and micropeak
authorKeith Packard <keithp@keithp.com>
Mon, 31 Dec 2012 19:32:56 +0000 (11:32 -0800)
committerKeith Packard <keithp@keithp.com>
Mon, 31 Dec 2012 19:36:38 +0000 (11:36 -0800)
Need to convert altosui to using it, but that shouldn't be hard

Signed-off-by: Keith Packard <keithp@keithp.com>
13 files changed:
Makefile.am
altosuilib/AltosConfigureUI.java [new file with mode: 0644]
altosuilib/AltosFontListener.java [new file with mode: 0644]
altosuilib/AltosUIDialog.java [new file with mode: 0644]
altosuilib/AltosUIFrame.java [new file with mode: 0644]
altosuilib/AltosUILib.java [new file with mode: 0644]
altosuilib/AltosUIListener.java [new file with mode: 0644]
altosuilib/AltosUIPreferences.java [new file with mode: 0644]
altosuilib/AltosUIPreferencesBackend.java [new file with mode: 0644]
altosuilib/AltosUIVersion.java.in [new file with mode: 0644]
altosuilib/AltosUnitsListener.java [new file with mode: 0644]
altosuilib/Makefile.am [new file with mode: 0644]
configure.ac

index 2e45aff060683d2087a0cb9d589500363c519443..e8945d9006beca9065c187835570fbd34e858152 100644 (file)
@@ -1,4 +1,4 @@
-SUBDIRS=src doc altoslib libaltos altosui ao-tools ao-utils altosdroid
+SUBDIRS=src doc altoslib libaltos altosuilib altosui ao-tools ao-utils altosdroid
 
 EXTRA_DIST = ChangeLog
 
diff --git a/altosuilib/AltosConfigureUI.java b/altosuilib/AltosConfigureUI.java
new file mode 100644 (file)
index 0000000..a4b644b
--- /dev/null
@@ -0,0 +1,393 @@
+/*
+ * Copyright © 2010 Keith Packard <keithp@keithp.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+package org.altusmetrum.altosuilib;
+
+import java.awt.*;
+import java.awt.event.*;
+import java.beans.*;
+import javax.swing.*;
+import javax.swing.event.*;
+
+class DelegatingRenderer implements ListCellRenderer {
+
+       // ...
+       public static void install(JComboBox comboBox) {
+               DelegatingRenderer renderer = new DelegatingRenderer(comboBox);
+               renderer.initialise();
+               comboBox.setRenderer(renderer);
+       }
+
+       // ...
+       private final JComboBox comboBox;
+
+       // ...
+       private ListCellRenderer delegate;
+
+       // ...
+       private DelegatingRenderer(JComboBox comboBox) {
+               this.comboBox = comboBox;
+       }
+
+       // ...
+       private void initialise() {
+               delegate = new JComboBox().getRenderer();
+               comboBox.addPropertyChangeListener("UI", new PropertyChangeListener() {
+
+                               public void propertyChange(PropertyChangeEvent evt) {
+                                       delegate = new JComboBox().getRenderer();
+                               }
+                       });
+       }
+
+       // ...
+       public Component getListCellRendererComponent(JList list,
+                                                     Object value, int index, boolean isSelected, boolean cellHasFocus) {
+
+               return delegate.getListCellRendererComponent(list,
+                                                            ((UIManager.LookAndFeelInfo) value).getName(),
+                                                            index, isSelected, cellHasFocus);
+       }
+}
+
+public class AltosConfigureUI
+       extends AltosUIDialog
+       implements DocumentListener
+{
+       JFrame          owner;
+       Container       pane;
+
+       JRadioButton    enable_voice;
+       JButton         test_voice;
+       JButton         close;
+
+       JButton         configure_log;
+       JTextField      log_directory;
+
+       JLabel          callsign_label;
+       JTextField      callsign_value;
+
+       JRadioButton    imperial_units;
+
+       JLabel          font_size_label;
+       JComboBox       font_size_value;
+
+       JLabel          look_and_feel_label;
+       JComboBox       look_and_feel_value;
+
+       JRadioButton    serial_debug;
+
+       JButton         manage_bluetooth;
+       JButton         manage_frequencies;
+
+       int             row;
+
+       final static String[] font_size_names = { "Small", "Medium", "Large" };
+
+       /* DocumentListener interface methods */
+       public void changedUpdate(DocumentEvent e) {
+               if (callsign_value != null) 
+                       AltosUIPreferences.set_callsign(callsign_value.getText());
+       }
+
+       public void insertUpdate(DocumentEvent e) {
+               changedUpdate(e);
+       }
+
+       public void removeUpdate(DocumentEvent e) {
+               changedUpdate(e);
+       }
+
+       public GridBagConstraints constraints (int x, int width, int fill) {
+               GridBagConstraints c = new GridBagConstraints();
+               Insets insets = new Insets(4, 4, 4, 4);
+
+               c.insets = insets;
+               c.fill = fill;
+               if (width == 3)
+                       c.anchor = GridBagConstraints.CENTER;
+               else
+                       c.anchor = GridBagConstraints.WEST;
+               c.gridx = x;
+               c.gridwidth = width;
+               c.gridy = row;
+               return c;
+       }
+
+       public GridBagConstraints constraints(int x, int width) {
+               return constraints(x, width, GridBagConstraints.NONE);
+       }
+
+       public void add_voice() {
+//             GridBagConstraints c = new GridBagConstraints();
+//
+//             /* Voice settings */
+//             c.gridx = 0;
+//             c.gridy = row;
+//             c.gridwidth = 1;
+//             c.fill = GridBagConstraints.NONE;
+//             c.anchor = GridBagConstraints.WEST;
+//             pane.add(new JLabel("Voice"), c);
+//
+//             enable_voice = new JRadioButton("Enable", AltosUIPreferences.voice());
+//             enable_voice.addActionListener(new ActionListener() {
+//                             public void actionPerformed(ActionEvent e) {
+//                                     JRadioButton item = (JRadioButton) e.getSource();
+//                                     boolean enabled = item.isSelected();
+//                                     AltosUIPreferences.set_voice(enabled);
+//                                     if (enabled)
+//                                             voice.speak_always("Enable voice.");
+//                                     else
+//                                             voice.speak_always("Disable voice.");
+//                             }
+//                     });
+//             c.gridx = 1;
+//             c.gridy = row;
+//             c.gridwidth = 1;
+//             c.weightx = 1;
+//             c.fill = GridBagConstraints.NONE;
+//             c.anchor = GridBagConstraints.WEST;
+//             pane.add(enable_voice, c);
+//             enable_voice.setToolTipText("Enable/Disable all audio in-flight announcements");
+//
+//             c.gridx = 2;
+//             c.gridy = row++;
+//             c.gridwidth = 1;
+//             c.weightx = 1;
+//             c.fill = GridBagConstraints.NONE;
+//             c.anchor = GridBagConstraints.EAST;
+//             test_voice = new JButton("Test Voice");
+//             test_voice.addActionListener(new ActionListener() {
+//                             public void actionPerformed(ActionEvent e) {
+//                                     voice.speak("That's one small step for man; one giant leap for mankind.");
+//                             }
+//                     });
+//             pane.add(test_voice, c);
+//             test_voice.setToolTipText("Play a stock audio clip to check volume");
+//             row++;
+       }
+
+       public void add_log_dir() {
+               /* Log directory settings */
+               pane.add(new JLabel("Log Directory"), constraints(0, 1));
+
+               configure_log = new JButton(AltosUIPreferences.logdir().getPath());
+               configure_log.addActionListener(new ActionListener() {
+                               public void actionPerformed(ActionEvent e) {
+                                       AltosUIPreferences.ConfigureLog();
+                                       configure_log.setText(AltosUIPreferences.logdir().getPath());
+                               }
+                       });
+               pane.add(configure_log, constraints(1, 2));
+               configure_log.setToolTipText("Which directory flight logs are stored in");
+               row++;
+       }
+
+       public void add_callsign() {
+//             /* Callsign setting */
+//             pane.add(new JLabel("Callsign"), constraints(0, 1));
+//
+//             callsign_value = new JTextField(AltosUIPreferences.callsign());
+//             callsign_value.getDocument().addDocumentListener(this);
+//             callsign_value.setToolTipText("Callsign sent in packet mode");
+//             pane.add(callsign_value, constraints(1, 2, GridBagConstraints.BOTH));
+//             row++;
+       }
+
+       public void add_units() {
+               /* Imperial units setting */
+               pane.add(new JLabel("Imperial Units"), constraints(0, 1));
+
+               imperial_units = new JRadioButton("Enable", AltosUIPreferences.imperial_units());
+               imperial_units.addActionListener(new ActionListener() {
+                               public void actionPerformed(ActionEvent e) {
+                                       JRadioButton item = (JRadioButton) e.getSource();
+                                       boolean enabled = item.isSelected();
+                                       AltosUIPreferences.set_imperial_units(enabled);
+                               }
+                       });
+               imperial_units.setToolTipText("Use Imperial units instead of metric");
+               pane.add(imperial_units, constraints(1, 2));
+               row++;
+       }
+
+       public void add_font_size() {
+               /* Font size setting */
+               pane.add(new JLabel("Font size"), constraints(0, 1));
+
+               font_size_value = new JComboBox(font_size_names);
+               int font_size = AltosUIPreferences.font_size();
+               font_size_value.setSelectedIndex(font_size - AltosUILib.font_size_small);
+               font_size_value.addActionListener(new ActionListener() {
+                               public void actionPerformed(ActionEvent e) {
+                                       int     size = font_size_value.getSelectedIndex() + AltosUILib.font_size_small;
+
+                                       AltosUIPreferences.set_font_size(size);
+                               }
+                       });
+               pane.add(font_size_value, constraints(1, 2, GridBagConstraints.BOTH));
+               font_size_value.setToolTipText("Font size used in telemetry window");
+               row++;
+       }
+
+       public void add_look_and_feel() {
+               /* Look & Feel setting */
+               pane.add(new JLabel("Look & feel"), constraints(0, 1));
+
+               /*
+               class LookAndFeelRenderer extends BasicComboBoxRenderer implements ListCellRenderer {
+
+                       public LookAndFeelRenderer() {
+                               super();
+                       }
+
+                       public Component getListCellRendererComponent(
+                               JList list,
+                               Object value,
+                               int index,
+                               boolean isSelected,
+                               boolean cellHasFocus)
+                       {
+                               super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
+                               setText(((UIManager.LookAndFeelInfo) value).getName());
+                               return this;
+                       }
+               }
+               */
+
+               final UIManager.LookAndFeelInfo[] look_and_feels = UIManager.getInstalledLookAndFeels();
+
+               look_and_feel_value = new JComboBox(look_and_feels);
+
+               DelegatingRenderer.install(look_and_feel_value);
+
+               String look_and_feel  = AltosUIPreferences.look_and_feel();
+               for (int i = 0; i < look_and_feels.length; i++)
+                       if (look_and_feel.equals(look_and_feels[i].getClassName()))
+                               look_and_feel_value.setSelectedIndex(i);
+
+               look_and_feel_value.addActionListener(new ActionListener() {
+                               public void actionPerformed(ActionEvent e) {
+                                       int     id = look_and_feel_value.getSelectedIndex();
+
+                                       AltosUIPreferences.set_look_and_feel(look_and_feels[id].getClassName());
+                               }
+                       });
+               pane.add(look_and_feel_value, constraints(1, 2, GridBagConstraints.BOTH));
+               look_and_feel_value.setToolTipText("Look&feel used for new windows");
+               row++;
+       }
+
+       public void add_serial_debug() {
+               GridBagConstraints c = new GridBagConstraints();
+
+               /* Serial debug setting */
+               pane.add(new JLabel("Serial Debug"), constraints(0, 1));
+
+               serial_debug = new JRadioButton("Enable", AltosUIPreferences.serial_debug());
+               serial_debug.addActionListener(new ActionListener() {
+                               public void actionPerformed(ActionEvent e) {
+                                       JRadioButton item = (JRadioButton) e.getSource();
+                                       boolean enabled = item.isSelected();
+                                       AltosUIPreferences.set_serial_debug(enabled);
+                               }
+                       });
+               serial_debug.setToolTipText("Enable/Disable USB I/O getting sent to the console");
+       }
+
+       public void add_bluetooth() {
+//             GridBagConstraints c = new GridBagConstraints();
+//             c.gridx = 1;
+//             c.gridy = row++;
+//             c.gridwidth = 3;
+//             c.fill = GridBagConstraints.NONE;
+//             c.anchor = GridBagConstraints.WEST;
+//             pane.add(serial_debug, c);
+//
+//             manage_bluetooth = new JButton("Manage Bluetooth");
+//             manage_bluetooth.addActionListener(new ActionListener() {
+//                             public void actionPerformed(ActionEvent e) {
+//                                     AltosBTManage.show(owner, AltosBTKnown.bt_known());
+//                             }
+//                     });
+//             c.gridx = 0;
+//             c.gridy = row;
+//             c.gridwidth = 2;
+//             c.fill = GridBagConstraints.NONE;
+//             c.anchor = GridBagConstraints.WEST;
+//             pane.add(manage_bluetooth, c);
+       }
+
+       public void add_frequencies() {
+//             GridBagConstraints c = new GridBagConstraints();
+//             manage_frequencies = new JButton("Manage Frequencies");
+//             manage_frequencies.addActionListener(new ActionListener() {
+//                             public void actionPerformed(ActionEvent e) {
+//                                     AltosConfigFreqUI.show(owner);
+//                             }
+//                     });
+//             manage_frequencies.setToolTipText("Configure which values are shown in frequency menus");
+//             c.gridx = 2;
+//             c.gridx = 2;
+//             c.gridy = row++;
+//             c.gridwidth = 2;
+//             c.fill = GridBagConstraints.NONE;
+//             c.anchor = GridBagConstraints.WEST;
+//             pane.add(manage_frequencies, c);
+       }
+
+       public AltosConfigureUI(JFrame in_owner) {
+               super(in_owner, "Configure AltosUI", false);
+
+               owner = in_owner;
+               pane = getContentPane();
+               pane.setLayout(new GridBagLayout());
+
+               row = 0;
+
+               /* Nice label at the top */
+               pane.add(new JLabel ("Configure AltOS UI"),
+                        constraints(0, 3));
+               row++;
+
+               pane.add(new JLabel (String.format("AltOS version %s", AltosUIVersion.version)),
+                        constraints(0, 3));
+               row++;
+
+               add_voice();
+               add_log_dir();
+               add_callsign();
+               add_units();
+               add_font_size();
+               add_look_and_feel();
+               add_bluetooth();
+               add_frequencies();
+
+               /* And a close button at the bottom */
+               close = new JButton("Close");
+               close.addActionListener(new ActionListener() {
+                               public void actionPerformed(ActionEvent e) {
+                                       setVisible(false);
+                               }
+                       });
+               pane.add(close, constraints(0, 3));
+
+               pack();
+               setLocationRelativeTo(owner);
+               setVisible(true);
+       }
+}
diff --git a/altosuilib/AltosFontListener.java b/altosuilib/AltosFontListener.java
new file mode 100644 (file)
index 0000000..ef54326
--- /dev/null
@@ -0,0 +1,22 @@
+/*
+ * Copyright © 2011 Keith Packard <keithp@keithp.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+package org.altusmetrum.altosuilib;
+
+public interface AltosFontListener {
+       void font_size_changed(int font_size);
+}
diff --git a/altosuilib/AltosUIDialog.java b/altosuilib/AltosUIDialog.java
new file mode 100644 (file)
index 0000000..c0c33ba
--- /dev/null
@@ -0,0 +1,59 @@
+/*
+ * Copyright © 2011 Keith Packard <keithp@keithp.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+package org.altusmetrum.altosuilib;
+
+import java.awt.*;
+import java.awt.event.*;
+import javax.swing.*;
+
+class AltosUIDialogListener extends WindowAdapter {
+       public void windowClosing (WindowEvent e) {
+               AltosUIPreferences.unregister_ui_listener((AltosUIDialog) e.getWindow());
+       }
+}
+
+public class AltosUIDialog extends JDialog implements AltosUIListener {
+
+       public void ui_changed(String look_and_feel) {
+               SwingUtilities.updateComponentTreeUI(this);
+               this.pack();
+       }
+
+       public AltosUIDialog() {
+               AltosUIPreferences.register_ui_listener(this);
+               addWindowListener(new AltosUIDialogListener());
+       }
+
+       public AltosUIDialog(Frame frame, String label, boolean modal) {
+               super(frame, label, modal);
+               AltosUIPreferences.register_ui_listener(this);
+               addWindowListener(new AltosUIDialogListener());
+       }
+
+       public AltosUIDialog(Dialog dialog, String label, boolean modal) {
+               super(dialog, label, modal);
+               AltosUIPreferences.register_ui_listener(this);
+               addWindowListener(new AltosUIDialogListener());
+       }
+
+       public AltosUIDialog(Frame frame, boolean modal) {
+               super(frame, modal);
+               AltosUIPreferences.register_ui_listener(this);
+               addWindowListener(new AltosUIDialogListener());
+       }
+}
diff --git a/altosuilib/AltosUIFrame.java b/altosuilib/AltosUIFrame.java
new file mode 100644 (file)
index 0000000..409aea2
--- /dev/null
@@ -0,0 +1,82 @@
+/*
+ * Copyright © 2011 Keith Packard <keithp@keithp.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+package org.altusmetrum.altosuilib;
+
+import java.awt.*;
+import java.awt.event.*;
+import javax.swing.*;
+import java.util.*;
+
+class AltosUIFrameListener extends WindowAdapter {
+       public void windowClosing (WindowEvent e) {
+               AltosUIPreferences.unregister_ui_listener((AltosUIFrame) e.getWindow());
+       }
+}
+
+public class AltosUIFrame extends JFrame implements AltosUIListener {
+
+       public void ui_changed(String look_and_feel) {
+               SwingUtilities.updateComponentTreeUI(this);
+               this.pack();
+       }
+
+       static String[] altos_icon_names = {
+               "/altus-metrum-16.png",
+               "/altus-metrum-32.png",
+               "/altus-metrum-48.png",
+               "/altus-metrum-64.png",
+               "/altus-metrum-128.png",
+               "/altus-metrum-256.png"
+       };
+
+       static public String[] icon_names;
+       
+       static public void set_icon_names(String[] new_icon_names) { icon_names = new_icon_names; }
+
+       public String[] icon_names() {
+               if (icon_names == null)
+                       set_icon_names(altos_icon_names);
+               return icon_names;
+       }
+
+       public void set_icon() {
+               ArrayList<Image> icons = new ArrayList<Image>();
+               String[] icon_names = icon_names();
+               
+               for (int i = 0; i < icon_names.length; i++) {
+                       java.net.URL imgURL = AltosUIFrame.class.getResource(icon_names[i]);
+                       if (imgURL != null)
+                               icons.add(new ImageIcon(imgURL).getImage());
+               }
+               setIconImages(icons);
+       }
+                       
+
+       public AltosUIFrame() {
+               AltosUIPreferences.register_ui_listener(this);
+               addWindowListener(new AltosUIFrameListener());
+               set_icon();
+       }
+
+       public AltosUIFrame(String name) {
+               super(name);
+               AltosUIPreferences.register_ui_listener(this);
+               addWindowListener(new AltosUIFrameListener());
+               set_icon();
+       }
+}
diff --git a/altosuilib/AltosUILib.java b/altosuilib/AltosUILib.java
new file mode 100644 (file)
index 0000000..717678b
--- /dev/null
@@ -0,0 +1,93 @@
+/*
+ * Copyright © 2010 Keith Packard <keithp@keithp.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+package org.altusmetrum.altosuilib;
+
+import java.awt.*;
+import libaltosJNI.*;
+
+import org.altusmetrum.AltosLib.*;
+
+public class AltosUILib extends AltosLib {
+
+       static final int tab_elt_pad = 5;
+
+       static Font label_font;
+       static Font value_font;
+       static Font status_font;
+       static Font table_label_font;
+       static Font table_value_font;
+
+       final static int font_size_small = 1;
+       final static int font_size_medium = 2;
+       final static int font_size_large = 3;
+
+       static void set_fonts(int size) {
+               int     brief_size;
+               int     table_size;
+               int     status_size;
+
+               switch (size) {
+               case font_size_small:
+                       brief_size = 16;
+                       status_size = 18;
+                       table_size = 11;
+                       break;
+               default:
+               case font_size_medium:
+                       brief_size = 22;
+                       status_size = 24;
+                       table_size = 14;
+                       break;
+               case font_size_large:
+                       brief_size = 26;
+                       status_size = 30;
+                       table_size = 17;
+                       break;
+               }
+               label_font = new Font("Dialog", Font.PLAIN, brief_size);
+               value_font = new Font("Monospaced", Font.PLAIN, brief_size);
+               status_font = new Font("SansSerif", Font.BOLD, status_size);
+               table_label_font = new Font("SansSerif", Font.PLAIN, table_size);
+               table_value_font = new Font("Monospaced", Font.PLAIN, table_size);
+       }
+
+       static final int text_width = 20;
+
+       static public boolean initialized = false;
+       static public boolean loaded_library = false;
+
+       public static boolean load_library() {
+               if (!initialized) {
+                       try {
+                               System.loadLibrary("altos");
+                               libaltos.altos_init();
+                               loaded_library = true;
+                       } catch (UnsatisfiedLinkError e) {
+                               try {
+                                       System.loadLibrary("altos64");
+                                       libaltos.altos_init();
+                                       loaded_library = true;
+                               } catch (UnsatisfiedLinkError e2) {
+                                       loaded_library = false;
+                               }
+                       }
+                       initialized = true;
+               }
+               return loaded_library;
+       }
+}
diff --git a/altosuilib/AltosUIListener.java b/altosuilib/AltosUIListener.java
new file mode 100644 (file)
index 0000000..f4127f5
--- /dev/null
@@ -0,0 +1,22 @@
+/*
+ * Copyright © 2011 Keith Packard <keithp@keithp.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+package org.altusmetrum.altosuilib;
+
+public interface AltosUIListener {
+       public void ui_changed(String look_and_feel);
+}
diff --git a/altosuilib/AltosUIPreferences.java b/altosuilib/AltosUIPreferences.java
new file mode 100644 (file)
index 0000000..485cb58
--- /dev/null
@@ -0,0 +1,180 @@
+/*
+ * Copyright © 2011 Keith Packard <keithp@keithp.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+package org.altusmetrum.altosuilib;
+
+import java.io.*;
+import java.util.*;
+import java.awt.Component;
+import javax.swing.*;
+import org.altusmetrum.AltosLib.*;
+
+public class AltosUIPreferences extends AltosPreferences {
+
+       /* font size preferences name */
+       final static String fontSizePreference = "FONT-SIZE";
+
+       /* Look&Feel preference name */
+       final static String lookAndFeelPreference = "LOOK-AND-FEEL";
+
+       /* UI Component to pop dialogs up */
+       static Component component;
+
+       static LinkedList<AltosFontListener> font_listeners;
+
+       static int font_size = AltosUILib.font_size_medium;
+
+       static LinkedList<AltosUIListener> ui_listeners;
+
+       static String look_and_feel = null;
+
+       /* Serial debug */
+       public static boolean serial_debug;
+
+       public static void init() {
+               AltosPreferences.init(new AltosUIPreferencesBackend());
+
+               font_listeners = new LinkedList<AltosFontListener>();
+
+               font_size = backend.getInt(fontSizePreference, AltosUILib.font_size_medium);
+               AltosUILib.set_fonts(font_size);
+               look_and_feel = backend.getString(lookAndFeelPreference, UIManager.getSystemLookAndFeelClassName());
+
+               ui_listeners = new LinkedList<AltosUIListener>();
+               serial_debug = backend.getBoolean(serialDebugPreference, false);
+               AltosLink.set_debug(serial_debug);
+       }
+
+       static { init(); }
+
+       public static void set_component(Component in_component) {
+               component = in_component;
+       }
+
+       private static boolean check_dir(File dir) {
+               if (!dir.exists()) {
+                       if (!dir.mkdirs()) {
+                               JOptionPane.showMessageDialog(component,
+                                                             dir.getName(),
+                                                             "Cannot create directory",
+                                                             JOptionPane.ERROR_MESSAGE);
+                               return false;
+                       }
+               } else if (!dir.isDirectory()) {
+                       JOptionPane.showMessageDialog(component,
+                                                     dir.getName(),
+                                                     "Is not a directory",
+                                                     JOptionPane.ERROR_MESSAGE);
+                       return false;
+               }
+               return true;
+       }
+
+       /* Configure the log directory. This is where all telemetry and eeprom files
+        * will be written to, and where replay will look for telemetry files
+        */
+       public static void ConfigureLog() {
+               JFileChooser    logdir_chooser = new JFileChooser(logdir.getParentFile());
+
+               logdir_chooser.setDialogTitle("Configure Data Logging Directory");
+               logdir_chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
+
+               if (logdir_chooser.showDialog(component, "Select Directory") == JFileChooser.APPROVE_OPTION) {
+                       File dir = logdir_chooser.getSelectedFile();
+                       if (check_dir(dir))
+                               set_logdir(dir);
+               }
+       }
+       public static int font_size() {
+               synchronized (backend) {
+                       return font_size;
+               }
+       }
+
+       static void set_fonts() {
+       }
+
+       public static void set_font_size(int new_font_size) {
+               synchronized (backend) {
+                       font_size = new_font_size;
+                       backend.putInt(fontSizePreference, font_size);
+                       flush_preferences();
+                       AltosUILib.set_fonts(font_size);
+                       for (AltosFontListener l : font_listeners)
+                               l.font_size_changed(font_size);
+               }
+       }
+
+       public static void register_font_listener(AltosFontListener l) {
+               synchronized (backend) {
+                       font_listeners.add(l);
+               }
+       }
+
+       public static void unregister_font_listener(AltosFontListener l) {
+               synchronized (backend) {
+                       font_listeners.remove(l);
+               }
+       }
+
+       public static void set_look_and_feel(String new_look_and_feel) {
+               try {
+                       UIManager.setLookAndFeel(new_look_and_feel);
+               } catch (Exception e) {
+               }
+               synchronized(backend) {
+                       look_and_feel = new_look_and_feel;
+                       backend.putString(lookAndFeelPreference, look_and_feel);
+                       flush_preferences();
+                       for (AltosUIListener l : ui_listeners)
+                               l.ui_changed(look_and_feel);
+               }
+       }
+
+       public static String look_and_feel() {
+               synchronized (backend) {
+                       return look_and_feel;
+               }
+       }
+
+       public static void register_ui_listener(AltosUIListener l) {
+               synchronized(backend) {
+                       ui_listeners.add(l);
+               }
+       }
+
+       public static void unregister_ui_listener(AltosUIListener l) {
+               synchronized (backend) {
+                       ui_listeners.remove(l);
+               }
+       }
+       public static void set_serial_debug(boolean new_serial_debug) {
+               AltosLink.set_debug(new_serial_debug);
+               synchronized (backend) {
+                       serial_debug = new_serial_debug;
+                       backend.putBoolean(serialDebugPreference, serial_debug);
+                       flush_preferences();
+               }
+       }
+
+       public static boolean serial_debug() {
+               synchronized (backend) {
+                       return serial_debug;
+               }
+       }
+
+}
diff --git a/altosuilib/AltosUIPreferencesBackend.java b/altosuilib/AltosUIPreferencesBackend.java
new file mode 100644 (file)
index 0000000..c6c05e5
--- /dev/null
@@ -0,0 +1,101 @@
+/*
+ * Copyright © 2012 Mike Beattie <mike@ethernal.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+package org.altusmetrum.altosuilib;
+
+import java.io.File;
+import java.util.prefs.*;
+import org.altusmetrum.AltosLib.*;
+import javax.swing.filechooser.FileSystemView;
+
+public class AltosUIPreferencesBackend implements AltosPreferencesBackend {
+
+       private Preferences _preferences = null;
+       
+       public AltosUIPreferencesBackend() {
+               _preferences = Preferences.userRoot().node("/org/altusmetrum/altosui");
+       }
+
+       public AltosUIPreferencesBackend(Preferences in_preferences) {
+               _preferences = in_preferences;
+       }
+
+       public String  getString(String key, String def) {
+               return _preferences.get(key, def);
+       }
+       public void    putString(String key, String value) {
+               _preferences.put(key, value);
+       }
+
+       public int     getInt(String key, int def) {
+               return _preferences.getInt(key, def);
+       }
+       public void    putInt(String key, int value) {
+               _preferences.putInt(key, value);
+       }
+
+       public double  getDouble(String key, double def) {
+               return _preferences.getDouble(key, def);
+       }
+       public void    putDouble(String key, double value) {
+               _preferences.putDouble(key, value);
+       }
+
+       public boolean getBoolean(String key, boolean def) {
+               return _preferences.getBoolean(key, def);
+       }
+       public void    putBoolean(String key, boolean value) {
+               _preferences.putBoolean(key, value);
+       }
+
+       public boolean nodeExists(String key) {
+               try {
+                       return _preferences.nodeExists(key);
+               } catch (BackingStoreException be) {
+                       return false;
+               }
+       }
+
+       public AltosPreferencesBackend node(String key) {
+               return new AltosUIPreferencesBackend(_preferences.node(key));
+       }
+
+       public String[] keys() {
+               try {
+                       return _preferences.keys();
+               } catch (BackingStoreException be) {
+                       return null;
+               }
+       }
+
+       public void remove(String key) {
+               _preferences.remove(key);
+       }
+
+       public void    flush() {
+               try {
+                       _preferences.flush();
+               } catch (BackingStoreException ee) {
+                       System.err.printf("Cannot save preferences\n");
+               }
+       }
+
+       public File homeDirectory() {
+               /* Use the file system view default directory */
+               return FileSystemView.getFileSystemView().getDefaultDirectory();
+       }
+}
diff --git a/altosuilib/AltosUIVersion.java.in b/altosuilib/AltosUIVersion.java.in
new file mode 100644 (file)
index 0000000..6fb3b38
--- /dev/null
@@ -0,0 +1,22 @@
+/*
+ * Copyright © 2011 Keith Packard <keithp@keithp.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+package org.altusmetrum.altosuilib;
+
+public class AltosUIVersion {
+       public final static String version = "@VERSION@";
+}
diff --git a/altosuilib/AltosUnitsListener.java b/altosuilib/AltosUnitsListener.java
new file mode 100644 (file)
index 0000000..22c66cd
--- /dev/null
@@ -0,0 +1,22 @@
+/*
+ * Copyright © 2012 Keith Packard <keithp@keithp.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+package org.altusmetrum.altosuilib;
+
+public interface AltosUnitsListener {
+       public void units_changed();
+}
diff --git a/altosuilib/Makefile.am b/altosuilib/Makefile.am
new file mode 100644 (file)
index 0000000..26aee7c
--- /dev/null
@@ -0,0 +1,41 @@
+AM_JAVACFLAGS=-encoding UTF-8 -Xlint:deprecation
+
+JAVAROOT=bin
+
+CLASSPATH_ENV=mkdir -p $(JAVAROOT); CLASSPATH="bin:../altoslib/*:../libaltos:$(FREETTS)/*:/usr/share/java/*"
+
+SRC=.
+BIN=bin/org/altusmetrum/AltosUILib
+
+AltosUILibdir = $(datadir)/java
+
+AltosUILib_JAVA = \
+       AltosConfigureUI.java \
+       AltosFontListener.java \
+       AltosUIDialog.java \
+       AltosUIFrame.java \
+       AltosUILib.java \
+       AltosUIListener.java \
+       AltosUIPreferencesBackend.java \
+       AltosUIPreferences.java \
+       AltosUIVersion.java \
+       AltosUnitsListener.java
+
+JAR=AltosUILib.jar
+
+all-local: $(JAR)
+
+clean-local:
+       -rm -rf bin $(JAR)
+
+install-AltosUILibJAVA: $(JAR)
+       @$(NORMAL_INSTALL)
+       test -z "$(AltosUILibdir)" || $(MKDIR_P) "$(DESTDIR)$(AltosUILibdir)"
+       echo " $(INSTALL_DATA)" "$<" "'$(DESTDIR)$(AltosUILibdir)/$(JAR)"; \
+       $(INSTALL_DATA) "$<" "$(DESTDIR)$(AltosUILibdir)"
+
+bin:
+       mkdir -p bin
+
+$(JAR): classAltosUILib.stamp
+       jar cf $@ -C bin org
index 55dd20ec5cbd9ceb2c6b9925d809b4820336faf5..6ad2591c3a1ae25277f0094c38a4a06dc7eca045 100644 (file)
@@ -146,6 +146,8 @@ AM_CONDITIONAL([LIBSTLINK], [test x$HAVE_STLINK != xno])
 AC_OUTPUT([
 Makefile
 altoslib/Makefile
+altosuilib/Makefile
+altosuilib/AltosUIVersion.java
 altosui/Makefile
 altosui/AltosVersion.java
 altosui/Info.plist