Changeset 3864


Ignore:
Timestamp:
04/02/08 10:58:38 (12 years ago)
Author:
curtis
Message:

Read formats and libraries info in a separate thread.

Location:
trunk/loci/plugins/config
Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/loci/plugins/config/ConfigWindow.java

    r3860 r3864  
    4848 */ 
    4949public class ConfigWindow extends JFrame 
    50   implements ItemListener, ListSelectionListener 
     50  implements ItemListener, ListSelectionListener, Runnable 
    5151{ 
    5252 
    5353  // -- Fields -- 
    5454 
     55  private DefaultListModel formatsListModel; 
    5556  private JList formatsList; 
    5657  private JPanel formatInfo; 
     
    5859  private JCheckBox enabled; 
    5960 
     61  private DefaultListModel libsListModel; 
    6062  private JList libsList; 
    6163  private JTextField type, status, version, path, url, license; 
    6264  private JTextArea notes; 
    6365 
     66  private PrintWriter log; 
     67 
    6468  // -- Constructor -- 
    6569 
     
    6872    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
    6973 
    70     // generate list of formats 
    71     FormatEntry[] formats = null; 
    72     try { 
    73       Class irClass = Class.forName("loci.formats.ImageReader"); 
    74       Object ir = irClass.newInstance(); 
    75       Method getClasses = irClass.getMethod("getReaders", null); 
    76       Object[] readers = (Object[]) getClasses.invoke(ir, null); 
    77       formats = new FormatEntry[readers.length]; 
    78       for (int i=0; i<readers.length; i++) { 
    79         formats[i] = new FormatEntry(readers[i]); 
    80       } 
    81       Arrays.sort(formats); 
    82     } 
    83     catch (Throwable t) { 
    84       t.printStackTrace(); 
    85       formats = new FormatEntry[0]; 
    86     } 
    87  
    88     // enumerate list of libraries 
    89     final String libCore = "Core library"; 
    90     final String libNative = "Native library"; 
    91     final String libPlugin = "ImageJ plugin"; 
    92     final String libJava = "Java library"; 
    93  
    94     String javaVersion = System.getProperty("java.version") + 
    95       " (" + System.getProperty("java.vendor") + ")"; 
    96  
    97     String bfVersion = "@date@"; 
    98     // Ant replaces date token with datestamp of the build 
    99     if (bfVersion.equals("@" + "date" + "@")) bfVersion = "Internal build"; 
    100  
    101     String qtVersion = null; 
    102     try { 
    103       Class qtToolsClass = Class.forName("loci.formats.LegacyQTTools"); 
    104       Object qtTools = qtToolsClass.newInstance(); 
    105       Method getQTVersion = qtToolsClass.getMethod("getQTVersion", null); 
    106       qtVersion = (String) getQTVersion.invoke(qtTools, null); 
    107     } 
    108     catch (Throwable t) { } 
    109  
    110     String matlabVersion = null; 
    111     try { 
    112       Class matlabClass = Class.forName("com.mathworks.jmi.Matlab"); 
    113       Object matlab = matlabClass.newInstance(); 
    114       Method eval = matlabClass.getMethod("eval", new Class[] {String.class}); 
    115       String ans = (String) eval.invoke(matlab, new Object[] {"version"}); 
    116       if (ans.startsWith("ans =")) ans = ans.substring(5); 
    117       matlabVersion = ans.trim(); 
    118     } 
    119     catch (Throwable t) { } 
    120  
    121     Hashtable versions = new Hashtable(); 
    122     if (javaVersion != null) versions.put("javaVersion", javaVersion); 
    123     if (bfVersion != null) versions.put("bfVersion", bfVersion); 
    124     if (qtVersion != null) versions.put("qtVersion", qtVersion); 
    125     if (matlabVersion != null) versions.put("matlabVersion", matlabVersion); 
    126  
    127     LibraryEntry[] libraries = parseLibraries("libraries.txt", versions); 
    128     Arrays.sort(libraries); 
    129  
    13074    // build UI 
    13175 
     
    13781    tabs.addTab("Options", optionsPanel); 
    13882 
    139     formatsList = makeList(formats); 
     83    formatsListModel = new DefaultListModel(); 
     84    formatsList = makeList(formatsListModel); 
    14085 
    14186    formatInfo = new JPanel(); 
     
    14893    doFormatLayout(null); 
    14994 
    150     // TODO - reader-specific options 
    151     // - QT: "Use QuickTime for Java" checkbox (default off) 
    152     // - ND2: "Use Nikon's ND2 plugin" checkbox (default off) 
    153     // - Flex: "LuraWave license code" label and text field (default nothing) 
    154     //   + if sys prop set, but ij prop not set, GRAY OUT AND DO NOT SHOW 
    155     // - SDT: "Merge lifetime bins to intensity" checkbox 
    156  
    157     libsList = makeList(libraries); 
     95    libsListModel = new DefaultListModel(); 
     96    libsList = makeList(libsListModel); 
    15897    JPanel libInfo = new JPanel(); 
    15998    tabs.addTab("Libraries", makeSplitPane(libsList, libInfo)); 
     
    202141    // TODO - "How to install" for each library? 
    203142 
     143    JPanel logPanel = new JPanel(); 
     144    tabs.addTab("Log", logPanel); 
     145 
     146    logPanel.setLayout(new java.awt.BorderLayout()); 
     147 
     148    JTextArea logArea = new JTextArea(); 
     149    logArea.setEditable(false); 
     150    logArea.setRows(10); 
     151    logPanel.add(new JScrollPane(logArea)); 
     152 
    204153    SpringUtilities.makeCompactGrid(libInfo, 7, 2, 3, 3, 3, 3); 
    205154 
    206     tabs.setSelectedIndex(1); 
     155    tabs.setSelectedIndex(1); // Formats tab 
    207156    pack(); 
     157 
     158    TextAreaWriter taw = new TextAreaWriter(logArea); 
     159    log = new PrintWriter(taw); 
     160 
     161    new Thread(this, "ConfigWindow-Loader").start(); 
    208162  } 
    209163 
     
    235189  } 
    236190 
    237   // -- Utility methods -- 
    238  
    239   public static JTextField makeTextField() { 
    240     JTextField textField = new JTextField(38); 
    241     int prefHeight = textField.getPreferredSize().height; 
    242     textField.setMaximumSize(new Dimension(Integer.MAX_VALUE, prefHeight)); 
    243     textField.setEditable(false); 
    244     return textField; 
    245   } 
    246  
    247   // -- Helper methods -- 
    248  
    249   private static LibraryEntry[] parseLibraries(String resource, 
    250     Hashtable versions) 
    251   { 
    252     Vector list = new Vector(); 
     191  // -- Runnable API methods -- 
     192 
     193  /** Populate configuration information in a separate thread. */ 
     194  public void run() { 
     195    log.println("LOCI Plugins configuration - " + new Date()); 
     196 
     197    // generate list of formats 
     198    log.println(); 
     199    log.println("-- Formats --"); 
     200    FormatEntry[] formats = null; 
     201    try { 
     202      Class irClass = Class.forName("loci.formats.ImageReader"); 
     203      Object ir = irClass.newInstance(); 
     204      Method getClasses = irClass.getMethod("getReaders", null); 
     205      Object[] readers = (Object[]) getClasses.invoke(ir, null); 
     206      for (int i=0; i<readers.length; i++) { 
     207        FormatEntry entry = new FormatEntry(log, readers[i]); 
     208        addEntry(entry, formatsListModel); 
     209      } 
     210    } 
     211    catch (Throwable t) { 
     212      log.println("Could not generate list of supported formats:"); 
     213      t.printStackTrace(log); 
     214    } 
     215 
     216    log.println(); 
     217    log.println("-- Libraries --"); 
     218 
     219    // enumerate list of libraries 
     220    final String libCore = "Core library"; 
     221    final String libNative = "Native library"; 
     222    final String libPlugin = "ImageJ plugin"; 
     223    final String libJava = "Java library"; 
     224 
     225    String javaVersion = System.getProperty("java.version") + 
     226      " (" + System.getProperty("java.vendor") + ")"; 
     227 
     228    String bfVersion = "@date@"; 
     229    // Ant replaces date token with datestamp of the build 
     230    if (bfVersion.equals("@" + "date" + "@")) bfVersion = "Internal build"; 
     231 
     232    String qtVersion = null; 
     233    try { 
     234      Class qtToolsClass = Class.forName("loci.formats.LegacyQTTools"); 
     235      Object qtTools = qtToolsClass.newInstance(); 
     236      Method getQTVersion = qtToolsClass.getMethod("getQTVersion", null); 
     237      qtVersion = (String) getQTVersion.invoke(qtTools, null); 
     238    } 
     239    catch (Throwable t) { 
     240      log.println("Could not determine QuickTime version:"); 
     241      t.printStackTrace(log); 
     242    } 
     243 
     244    String matlabVersion = null; 
     245    try { 
     246      Class matlabClass = Class.forName("com.mathworks.jmi.Matlab"); 
     247      Object matlab = matlabClass.newInstance(); 
     248      Method eval = matlabClass.getMethod("eval", new Class[] {String.class}); 
     249 
     250      String ans = (String) eval.invoke(matlab, new Object[] {"version"}); 
     251      if (ans.startsWith("ans =")) ans = ans.substring(5); 
     252      matlabVersion = ans.trim(); 
     253    } 
     254    catch (Throwable t) { 
     255      if (t instanceof ClassNotFoundException) { 
     256        // MATLAB library not available 
     257      } 
     258      else { 
     259        log.println("Error determining MATLAB version:"); 
     260        t.printStackTrace(log); 
     261      } 
     262    } 
     263 
     264    Hashtable versions = new Hashtable(); 
     265    if (javaVersion != null) versions.put("javaVersion", javaVersion); 
     266    if (bfVersion != null) versions.put("bfVersion", bfVersion); 
     267    if (qtVersion != null) versions.put("qtVersion", qtVersion); 
     268    if (matlabVersion != null) versions.put("matlabVersion", matlabVersion); 
     269 
     270    // parse libraries 
    253271    Hashtable props = null; 
    254272    String propKey = null; 
    255273    StringBuffer propValue = new StringBuffer(); 
     274    String resource = "libraries.txt"; 
    256275    BufferedReader in = new BufferedReader(new InputStreamReader( 
    257276      ConfigWindow.class.getResourceAsStream(resource))); 
     
    262281      } 
    263282      catch (IOException exc) { 
     283        log.println("Error parsing " + resource + ":"); 
     284        exc.printStackTrace(log); 
    264285        break; 
    265286      } 
     
    280301        else { 
    281302          addProp(props, propKey, propValue.toString(), versions); 
    282           list.add(new LibraryEntry(props)); 
     303          LibraryEntry entry = new LibraryEntry(log, props); 
     304          addEntry(entry, libsListModel); 
    283305        } 
    284306        props.clear(); 
     
    301323      in.close(); 
    302324    } 
    303     catch (IOException exc) { } 
    304     LibraryEntry[] entries = new LibraryEntry[list.size()]; 
    305     list.copyInto(entries); 
    306     return entries; 
    307   } 
    308  
    309   private static void addProp(Hashtable props, 
     325    catch (IOException exc) { 
     326      log.println("Error closing " + resource + ":"); 
     327      exc.printStackTrace(log); 
     328    } 
     329  } 
     330 
     331  // -- Utility methods -- 
     332 
     333  public static JTextField makeTextField() { 
     334    JTextField textField = new JTextField(38); 
     335    int prefHeight = textField.getPreferredSize().height; 
     336    textField.setMaximumSize(new Dimension(Integer.MAX_VALUE, prefHeight)); 
     337    textField.setEditable(false); 
     338    return textField; 
     339  } 
     340 
     341  public static void addEntry(final Comparable c, 
     342    final DefaultListModel listModel) 
     343  { 
     344    SwingUtilities.invokeLater(new Runnable() { 
     345      public void run() { 
     346        // binary search for proper location 
     347        int min = 0, max = listModel.size(); 
     348        while (min < max) { 
     349          int mid = (min + max) / 2; 
     350          Object o = listModel.get(mid); 
     351          int result = c.compareTo(o); 
     352          if (result > 0) min = mid + 1; 
     353          else max = mid; 
     354        } 
     355        listModel.add(max, c); 
     356      } 
     357    }); 
     358  } 
     359 
     360  // -- Helper methods -- 
     361 
     362  private void addProp(Hashtable props, 
    310363    String key, String value, Hashtable versions) 
    311364  { 
     
    329382  } 
    330383 
    331   private JList makeList(Object[] data) { 
    332     JList list = new JList(data); 
     384  private JList makeList(DefaultListModel listModel) { 
     385    JList list = new JList(listModel); 
    333386    list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 
    334387    list.setVisibleRowCount(25); 
     
    393446    } 
    394447    catch (Throwable t) { 
    395       t.printStackTrace(); 
     448      log.println("Error determining status for " + 
     449        entry.readerName + " reader:"); 
     450      t.printStackTrace(log); 
    396451      return false; 
    397452    } 
     
    411466    catch (Throwable t) { 
    412467      t.printStackTrace(); 
     468      log.println("Error " + (on ? "enabling" : "disabling") + 
     469        " " + entry.readerName + " reader:"); 
     470      t.printStackTrace(log); 
    413471    } 
    414472  } 
  • trunk/loci/plugins/config/FormatEntry.java

    r3858 r3864  
    2727 
    2828import java.awt.Component; 
     29import java.io.PrintWriter; 
    2930import java.lang.reflect.Method; 
    3031 
     
    5152  // -- Constructor -- 
    5253 
    53   public FormatEntry(Object reader) { 
     54  public FormatEntry(PrintWriter log, Object reader) { 
    5455    this.reader = reader; 
    55     Class readerClass = null; 
     56    Class readerClass = reader.getClass(); 
     57    String n = readerClass.getName(); 
     58    readerName = n.substring(n.lastIndexOf(".") + 1, n.length() - 6); 
    5659    try { 
    57       readerClass = reader.getClass(); 
    58       String n = readerClass.getName(); 
    59       readerName = n.substring(n.lastIndexOf(".") + 1, n.length() - 6); 
    6060      Method getFormat = readerClass.getMethod("getFormat", null); 
    6161      formatName = (String) getFormat.invoke(reader, null); 
    6262      Method getSuffixes = readerClass.getMethod("getSuffixes", null); 
    6363      suffixes = (String[]) getSuffixes.invoke(reader, null); 
     64      log.println("Successfully queried " + readerName + " reader."); 
    6465    } 
    6566    catch (Throwable t) { 
    66       t.printStackTrace(); 
     67      log.println("Error querying " + readerName + " reader:"); 
     68      t.printStackTrace(log); 
     69      log.println(); 
    6770      suffixes = new String[0]; 
    6871    } 
    6972    // create any extra widgets for this format, if any 
    7073    IFormatWidgets fw = null; 
     74    String fwClassName = "loci.plugins.config." + readerName + "Widgets"; 
    7175    try { 
    72       String fwClassName = "loci.plugins.config." + readerName + "Widgets"; 
    7376      Class fwClass = Class.forName(fwClassName); 
    7477      fw = (IFormatWidgets) fwClass.newInstance(); 
     78      log.println("Initialized extra widgets for " + readerName + " reader."); 
    7579    } 
    76     catch (Throwable t) { } 
     80    catch (Throwable t) { 
     81      if (t instanceof ClassNotFoundException) { 
     82        // no extra widgets for this reader 
     83      } 
     84      else { 
     85        log.println("Error constructing widgets for " + 
     86          readerName + " reader:"); 
     87        t.printStackTrace(log); 
     88      } 
     89    } 
    7790    labels = fw == null ? new String[0] : fw.getLabels(); 
    7891    widgets = fw == null ? new Component[0] : fw.getWidgets(); 
  • trunk/loci/plugins/config/LibraryEntry.java

    r3860 r3864  
    2727 
    2828import java.io.File; 
     29import java.io.PrintWriter; 
    2930import java.net.URLDecoder; 
    3031import java.util.Hashtable; 
     
    6566  // -- Constructor -- 
    6667 
    67   public LibraryEntry(Hashtable props) { 
    68     this((String) props.get("name"), 
     68  public LibraryEntry(PrintWriter log, Hashtable props) { 
     69    this(log, 
     70      (String) props.get("name"), 
    6971      (String) props.get("type"), 
    7072      (String) props.get("class"), 
     
    7577  } 
    7678 
    77   public LibraryEntry(String name, String type, String libClass, 
    78     String version, String url, String license, String notes) 
     79  public LibraryEntry(PrintWriter log, String name, String type, 
     80    String libClass, String version, String url, String license, String notes) 
    7981  { 
    8082    this.name = name; 
     
    118120                        if (slash.equals("\\")) slash = "\\\\"; 
    119121      path = path.replaceAll("/", slash); 
     122 
     123      log.println("Found library " + name + ":"); 
     124      if (!"".equals(version)) log.println("    Version = " + version); 
     125      log.println("    Path = " + path); 
    120126    } 
    121     catch (Throwable t) { } 
     127    catch (Throwable t) { 
     128      if (t instanceof ClassNotFoundException) { 
     129        log.println("No library " + name + "."); 
     130      } 
     131      else { 
     132        log.println("Error communicating with library " + name + ":"); 
     133        t.printStackTrace(log); 
     134      } 
     135    } 
    122136    status = version == null ? "Missing" : "Installed"; 
    123137  } 
  • trunk/loci/plugins/config/LociConfig.java

    r3857 r3864  
    2727 
    2828import ij.IJ; 
     29import ij.ImageJ; 
    2930import ij.plugin.PlugIn; 
     31import java.awt.*; 
    3032 
    3133/** 
     
    4446  /** Executes the plugin. */ 
    4547  public void run(String arg) { 
    46     IJ.showStatus("Loading LOCI plugins configuration dialog"); 
    4748    ConfigWindow cw = new ConfigWindow(); 
     49    Dimension size = cw.getSize(); 
     50 
     51    Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); 
     52 
     53    ImageJ ij = IJ.getInstance(); 
     54 
     55    Point p = new Point(); 
     56 
     57    if (ij == null) { 
     58      // center config window on screen 
     59      p.x = (screen.width - size.width) / 2; 
     60      p.y = (screen.height - size.height) / 2; 
     61    } 
     62    else { 
     63      // place config window below ImageJ window 
     64      Rectangle ijBounds = ij.getBounds(); 
     65      p.x = ijBounds.x + (ijBounds.width - size.width) / 2; 
     66      p.y = ijBounds.y + ijBounds.height + 5; 
     67    } 
     68 
     69    // nudge config window away from screen edges 
     70    final int pad = 10; 
     71    if (p.x < pad) p.x = pad; 
     72    else if (p.x + size.width + pad > screen.width) { 
     73      p.x = screen.width - size.width - pad; 
     74    } 
     75    if (p.y < pad) p.y = pad; 
     76    else if (p.y + size.height + pad > screen.height) { 
     77      p.y = screen.height - size.height - pad; 
     78    } 
     79 
     80    cw.setLocation(p); 
    4881    cw.setVisible(true); 
    49     IJ.showStatus(""); 
    5082  } 
    5183 
Note: See TracChangeset for help on using the changeset viewer.