Changeset 2835


Ignore:
Timestamp:
05/30/07 12:25:45 (13 years ago)
Author:
sorber
Message:

Added option to toggle automatic spreadsheet launch.
Misc small changes to spreadsheet launch code.

Location:
trunk/loci/visbio
Files:
2 added
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/loci/visbio/overlays/OverlayManager.java

    r2823 r2835  
    2828import loci.visbio.help.HelpManager; 
    2929import loci.visbio.state.OptionManager; 
     30import loci.visbio.state.SpreadsheetLaunchOption; 
    3031 
    3132/** OverlayManager is the manager encapsulating VisBio's overlay logic. */ 
     
    7374    } 
    7475 
     76    // add option for launching spreadsheet automatically 
     77    String path = ""; 
     78    try { 
     79      path = SpreadsheetLauncher.getDefaultApplicationPath(); 
     80    } 
     81    catch (SpreadsheetLaunchException ex) {} 
     82    om.addOption("General", new SpreadsheetLaunchOption('s', path, true)); 
     83 
    7584    // help window 
    7685    bio.setSplashStatus(null); 
  • trunk/loci/visbio/overlays/OverlayWidget.java

    r2826 r2835  
    3838import javax.swing.text.Document; 
    3939import loci.formats.gui.ExtensionFileFilter; 
     40import loci.visbio.VisBioFrame; 
    4041import loci.visbio.data.*; 
     42import loci.visbio.state.OptionManager; 
     43import loci.visbio.state.SpreadsheetLaunchOption; 
     44import loci.visbio.state.SpreadsheetOptionStrategy; 
    4145import loci.visbio.util.*; 
    4246import org.apache.poi.hssf.usermodel.HSSFWorkbook; 
     
    8286  protected JFileChooser overlayLoadBox; 
    8387 
    84   /** File chooser for exporting overlays to .xls */ 
     88  /** File chooser for exporting overlays to .xls format. */ 
    8589  protected JFileChooser overlayXLSBox; 
    8690 
     
    454458 
    455459  /** Sets notes for current overlay. */ 
    456   public void setNotes(String text) { notes.setText(text); } 
     460  public void setNotes(String newText) { notes.setText(newText); } 
    457461 
    458462  /** Gets notes for current overlay. */ 
     
    460464 
    461465  /** Sets statistics for current overlay. */ 
    462   public void setStatistics(String text) { stats.setText(text); } 
     466  public void setStatistics(String newText) { stats.setText(newText); } 
    463467 
    464468  /** Gets statistics for current overlay. */ 
     
    679683    } 
    680684    else if (src == save) { 
    681       StatsOptionsPane statsPane = (StatsOptionsPane) overlaySaveBox.getAccessory(); 
     685      StatsOptionsPane statsPane = (StatsOptionsPane) 
     686        overlaySaveBox.getAccessory(); 
    682687      statsPane.loadSettings(); 
    683688      int rval = overlaySaveBox.showSaveDialog(this); 
     
    707712        FileOutputStream fout = new FileOutputStream(file); 
    708713        HSSFWorkbook wb = overlay.exportOverlays(); 
    709         wb.write(fout); // TODO use a task here... this can be slow for  
     714        wb.write(fout); // TODO use a task here... this can be slow for 
    710715        // really big overlay sets 
    711716        fout.close(); 
     
    713718      } 
    714719      catch (IOException exc) { 
    715         //update this TODO ACS 
     720        // TODO add error message or something more sophisticated? 
    716721        exc.printStackTrace(); 
    717722      } 
     
    870875  } 
    871876 
    872   /** Launches the spreadsheet */ 
     877  /** Launches the spreadsheet. */ 
    873878  protected void launchSpreadsheet(File file) { 
    874     try { 
    875       SpreadsheetLauncher launcher = new SpreadsheetLauncher(); 
    876       launcher.launchSpreadsheet(file); 
    877     } 
    878     catch (SpreadsheetLaunchException ex) { 
    879       displayErrorMessage(ex.getMessage()); 
    880     } 
    881   } 
    882  
    883   /** Displays an error message */ 
     879    OptionManager om = (OptionManager) 
     880      VisBioFrame.getVisBio().getManager(OptionManager.class); 
     881    SpreadsheetLaunchOption opt = (SpreadsheetLaunchOption) 
     882      om.getOption(SpreadsheetOptionStrategy.getText()); 
     883    if (opt.getSelected()) { 
     884      try { 
     885        SpreadsheetLauncher launcher = new SpreadsheetLauncher(); 
     886        launcher.launchSpreadsheet(file, opt.getValue()); 
     887      } 
     888      catch (SpreadsheetLaunchException ex) { 
     889        displayErrorMessage(ex.getMessage()); 
     890      } 
     891    } 
     892  } 
     893 
     894  /** Displays an error message. */ 
    884895  protected void displayErrorMessage(String message) { 
    885     JOptionPane.showMessageDialog((JComponent) this, message,  
     896    JOptionPane.showMessageDialog((JComponent) this, message, 
    886897        "Could not launch spreadsheet", JOptionPane.ERROR_MESSAGE); 
    887898  } 
  • trunk/loci/visbio/overlays/SpreadsheetLaunchException.java

    r2828 r2835  
    2424package loci.visbio.overlays; 
    2525 
    26 import java.io.File; 
    27  
    28 /** Exception thrown when spreadsheet launch command fails */ 
     26/** Exception thrown when spreadsheet launch command fails. */ 
    2927public class SpreadsheetLaunchException extends Exception{ 
    30    
    31   // --  Fields -- 
    32    
    33   /** The command that caused this exception */ 
    34   protected String command; 
    3528 
    3629  // -- Constructor -- 
    3730 
    38   /** Constructs a new SpreadsheetLaunchException */ 
     31  /** Constructs a new SpreadsheetLaunchException. */ 
    3932  public SpreadsheetLaunchException(String msg) { 
    4033    super(msg); 
    4134  } 
    42  
    4335} 
  • trunk/loci/visbio/overlays/SpreadsheetLauncher.java

    r2827 r2835  
    2929import java.util.Locale; 
    3030 
     31/** Launches an external spreadsheet application. */ 
    3132public class SpreadsheetLauncher { 
    3233 
    33   // -- Constants --  
     34  // -- Constants -- 
    3435 
    35   protected static final String WIN_PATH = "C:\\Program Files\\Microsoft Office\\excel.exe"; 
     36  /** Default path to spreadsheet (Excel) on Windows computers. */ 
     37  protected static final String WIN_PATH = 
     38    "C:\\Program Files\\Microsoft Office\\excel.exe"; 
    3639 
     40  /** Default path to spreadsheet (OpenOffice) on Linux computers. */ 
    3741  protected static final String LIN_PATH = "/usr/bin/oocalc"; 
    3842 
    39   protected static final String MAC_PATH = "/Applications/Microsoft Office 2004/Microsoft Excel"; 
     43  /** Default path to spreadsheet (Excel) on Macintosh computers. */ 
     44  protected static final String MAC_PATH = 
     45    "/Applications/Microsoft Office 2004/Microsoft Excel"; 
    4046 
     47  /** The name of the OS. */ 
    4148  // copied from LookUtils 
    4249  // TODO can use LookUtils methods as soon as we update jgoodies.looks jar file 
     
    4451 
    4552  // --  Fields -- 
    46    
    47   /** Path to spreadsheet executable */ 
    48   protected String path; 
     53 
     54  /** Path to spreadsheet executable. */ 
     55  protected final String path; 
    4956 
    5057  // -- Constructor -- 
    5158 
    52   /** Constructs a spreadsheet launcher */ 
    53   public SpreadsheetLauncher() { 
    54     // determine OS 
    55     if (isWindows()) path = WIN_PATH; 
    56     else if (isLinux()) path = LIN_PATH;  
    57     else if (isMac()) path = MAC_PATH; 
    58     else path = ""; 
     59  /** Constructs a spreadsheet launcher. */ 
     60  public SpreadsheetLauncher() throws SpreadsheetLaunchException{ 
     61    path = getDefaultApplicationPath(); 
     62  } 
     63 
     64  // -- Static SpreadsheetLauncher API methods 
     65 
     66  /** Returns the default spreadsheet application path for the current OS. */ 
     67  public static String getDefaultApplicationPath() throws 
     68    SpreadsheetLaunchException 
     69  { 
     70    String def = ""; 
     71    if (isWindows()) def = WIN_PATH; 
     72    else if (isLinux()) def = LIN_PATH; 
     73    else if (isMac()) def = MAC_PATH; 
     74    else { 
     75      throw new SpreadsheetLaunchException(makeCantIdentifyOSMessage()); 
     76    } 
     77    return def; 
    5978  } 
    6079 
    6180  // -- SpreadsheetLauncher API methods -- 
    62     
    63   /** Tries to launch the appropriate spreadsheet application */ 
     81 
     82  /** Tries to launch the appropriate spreadsheet application. */ 
    6483  public void launchSpreadsheet(File file) throws SpreadsheetLaunchException { 
     84    launchSpreadsheet(file, path); 
     85  } 
     86 
     87  /** Tries to launch the appropriate spreadsheet application. */ 
     88  public void launchSpreadsheet(File file, String appPath) throws 
     89    SpreadsheetLaunchException 
     90  { 
    6591    if (file.exists()) { 
    66       String command = path + " " + file.getAbsolutePath(); 
     92      String command = appPath + " " + file.getAbsolutePath(); 
    6793      try { 
    6894        Runtime.getRuntime().exec(command); 
    6995      } 
    70       catch (IOException ex) {  
     96      catch (IOException ex) { 
    7197        throw new SpreadsheetLaunchException(makeCommandErrorMessage(command)); 
    7298      } 
    7399    } 
    74100    else { 
    75       throw new SpreadsheetLaunchException(makeFileErrorMessage(file)); 
     101      throw new SpreadsheetLaunchException(makeFileDoesNotExistMessage(file)); 
    76102    } 
    77103  } 
     
    79105  // -- Helper methods -- 
    80106 
    81   /** Whether the OS is windows */  
    82   protected boolean isWindows() {  
     107  /** Whether the OS is windows. */ 
     108  protected static boolean isWindows() { 
    83109    // return LookUtils.IS_OS_WINDOWS_MODERN; 
    84110    // copied from LookUtils: 
    85111    return OS_NAME.startsWith("Windows"); 
    86112  } 
    87    
    88   /** Whether the OS is mac */  
    89   protected boolean isMac() { 
     113 
     114  /** Whether the OS is mac. */ 
     115  protected static boolean isMac() { 
    90116    // return LookUtils.IS_OS_MAC; 
    91117    // Copied from LookUtils 
     
    93119  } 
    94120 
    95   /** Whether OS is Linux */ 
    96   protected boolean isLinux() { 
     121  /** Whether OS is Linux. */ 
     122  protected static boolean isLinux() { 
    97123    // return LookUtils.IS_OS_LINUX; 
    98     return OS_NAME.toUpperCase(Locale.ENGLISH).startsWith("LINUX"); // copied from LookUtils 
     124    return OS_NAME.toUpperCase(Locale.ENGLISH).startsWith("LINUX"); 
     125    // copied from LookUtils 
    99126  } 
    100127 
    101   /** Makes an error message from the given command */ 
     128  /** Makes an error message from the given command. */ 
    102129  protected String makeCommandErrorMessage(String command) { 
    103     String msg = "Could not launch spreadsheet using the following command:\n\t"  
    104       + command + "\nYou may wish to specify the spreadsheet application path" 
    105       + " yourself in the Options menu."; 
     130    String msg = 
     131      "Could not launch spreadsheet using the following command:\n\t" + 
     132      command + "\nYou may wish to change the spreadsheet application path" + 
     133      " in the 'File > Options...' menu."; 
    106134    return msg; 
    107135  } 
    108136 
    109   /** makes an error message from the given file */ 
    110   protected String makeFileErrorMessage(File file) { 
    111     return "Could not launch spreadsheet.  File does not exist:\n\t" +  
     137  /** Makes an error message from the given file. */ 
     138  protected String makeFileDoesNotExistMessage(File file) { 
     139    return "Could not launch spreadsheet.  File does not exist:\n\t" + 
    112140      file.getAbsolutePath(); 
    113141  } 
    114142 
    115   /** For testing */ 
    116   public static void main (String[] args) { 
    117     SpreadsheetLauncher s = new SpreadsheetLauncher(); 
     143  /** Returns an error message indicating the OS could not be identified. */ 
     144  protected static String makeCantIdentifyOSMessage() { 
     145    return "Could not launch spreadsheet: could not identify OS."; 
    118146  } 
    119147} 
  • trunk/loci/visbio/state/OptionManager.java

    r2622 r2835  
    9696  } 
    9797 
     98  /** Adds an option allowing the user to enter a string. */ 
     99  public StringOption addStringOption(String tab, String text, String tip, 
     100      String value, String label) 
     101  { 
     102    StringOption option = new StringOption(text, tip, value, label); 
     103    addOption(tab, option); 
     104    return option; 
     105  } 
     106 
    98107  /** Adds an option allowing the user to select from a dropdown list. */ 
    99108  public ListOption addListOption(String tab, 
Note: See TracChangeset for help on using the changeset viewer.