//
// IdDialog.java
//

/*
LOCI Plugins for ImageJ: a collection of ImageJ plugins including the
Bio-Formats Importer, Bio-Formats Exporter, Bio-Formats Macro Extensions,
Data Browser and Stack Slicer. Copyright (C) 2005-@year@ Melissa Linkert,
Curtis Rueden and Christopher Peterson.

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; either version 2 of the License, or
(at your option) any later version.

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 loci.plugins.in;

import ij.IJ;
import ij.gui.GenericDialog;
import ij.io.OpenDialog;

import java.awt.TextField;
import java.util.Vector;

import loci.common.Location;

/**
 * Bio-Formats Importer id chooser dialog box.
 *
 * <dl><dt><b>Source code:</b></dt>
 * <dd><a href="http://trac.openmicroscopy.org.uk/ome/browser/bioformats.git/components/loci-plugins/src/loci/plugins/in/IdDialog.java">Trac</a>,
 * <a href="http://git.openmicroscopy.org/?p=bioformats.git;a=blob;f=components/loci-plugins/src/loci/plugins/in/IdDialog.java;hb=HEAD">Gitweb</a></dd></dl>
 */
public class IdDialog extends ImporterDialog {

  // -- Fields --

  private OpenDialog od;
  private String name;

  // -- Constructor --

  /** Creates an id chooser dialog for the Bio-Formats Importer. */
  public IdDialog(ImportProcess process) {
    super(process);
  }

  // -- ImporterDialog methods --

  @Override
  protected boolean needPrompt() {
    return options.getId() == null;
  }

  @Override
  protected GenericDialog constructDialog() {
    GenericDialog gd = null;
    if (options.isHTTP()) {
      gd = new GenericDialog("Bio-Formats URL");
      gd.addStringField("URL: ", "http://", 30);
    }
    else if (options.isOMERO()) {
      gd = new GenericDialog("OMERO Server Credentials");
      gd.addStringField("Server: ", "", 80);
      gd.addStringField("Port: ", "4064", 80);
      gd.addStringField("Username: ", "", 80);
      gd.addStringField("Password: ", "", 80);
      gd.addStringField("Image ID: ", "", 80);

      Vector v = gd.getStringFields();
      ((TextField) v.get(3)).setEchoChar('*');
    }
    return gd;
  }

  /**
   * Asks user whether Bio-Formats should automatically check for upgrades,
   * and if so, checks for an upgrade and prompts user to install it.
   *
   * @return status of operation
   */
  @Override
  protected boolean displayDialog(GenericDialog gd) {
    if (options.isLocal()) {
      if (options.isFirstTime() && IJ.isMacOSX() && !options.isQuiet()) {
        String osVersion = System.getProperty("os.version");
        if (osVersion == null ||
          osVersion.startsWith("10.4.") ||
          osVersion.startsWith("10.3.") ||
          osVersion.startsWith("10.2."))
        {
          // present user with one-time dialog box
          IJ.showMessage("Bio-Formats",
            "One-time warning: There is a bug in Java on Mac OS X with the " +
            "native file chooser\nthat crashes ImageJ if you click on a file " +
            "in CXD, IPW, OIB or ZVI format while in\ncolumn view mode. You " +
            "can work around the problem in one of two ways:\n \n" +
            "    1. Switch to list view (press Command+2)\n" +
            "    2. Check \"Use JFileChooser to Open/Save\" under " +
            "Edit>Options>Input/Output...");
        }
      }

      String idLabel = options.getLabel(ImporterOptions.KEY_ID);
      od = new OpenDialog(idLabel, options.getId());
      name = od.getFileName();
      if (name == null) return false;
    }
    else if (options.isHTTP() || options.isOMERO()) {
      gd.showDialog();
      if (gd.wasCanceled()) return false;
    }
    return true;
  }

  @Override
  protected boolean harvestResults(GenericDialog gd) {
    String id = null;
    if (options.isLocal()) {
      String dir = od.getDirectory();
      // NB: do not use od.getFileName() here.  That method has been called
      // above, so the macro recorder will record the file path twice if we
      // call od.getFileName() again.  See ticket #596.
      if (dir != null || name == null) {
        id = dir + name;
      }

      // verify validity
      Location idLoc = new Location(id);
      if (!idLoc.exists() && !id.toLowerCase().endsWith(".fake")) {
        if (!options.isQuiet()) {
          IJ.error("Bio-Formats",
            "The specified file (" + id + ") does not exist.");
        }
        return false;
      }
    }
    else if (options.isHTTP()) {
      id = gd.getNextString();
      if (id == null) {
        if (!options.isQuiet()) {
          IJ.error("Bio-Formats", "No URL was specified.");
        }
        return false;
      }
    }
    else if (options.isOMERO()) {
      StringBuffer omero = new StringBuffer("omero:");
      omero.append("server=");
      omero.append(gd.getNextString());
      omero.append("\nport=");
      omero.append(gd.getNextString());
      omero.append("\nuser=");
      omero.append(gd.getNextString());
      omero.append("\npass=");
      omero.append(gd.getNextString());
      omero.append("\niid=");
      omero.append(gd.getNextString());
      id = omero.toString();
    }
    options.setId(id);
    return true;
  }

}

@@ -2422,32 +2422,86 @@
 rd: %22, %22%22, 80);%0A
+      gd.addStringField(%22Group:    %22, %22default%22, 80);%0A
       gd.addStri
@@ -5520,32 +5520,366 @@
 etNextString());
+%0A%0A      String group = gd.getNextString();%0A      Long groupID = null;%0A      try %7B%0A        groupID = new Long(group);%0A      %7D%0A      catch (NumberFormatException e) %7B %7D%0A%0A      omero.append(%22%5CngroupName=%22);%0A      omero.append(group);%0A      if (groupID != null) %7B%0A        omero.append(%22%5CngroupID=%22);%0A        omero.append(groupID);%0A      %7D
 %0A      omero.app
@@ -5929,24 +5929,25 @@
 tString());%0A
+%0A
       id = o

Add "Group" box to the OMERO credentials dialog