gt:                     if (defValue != null) {
context: /*
   * org.openmicroscopy.shoola.agents.metadata.util.ScriptingDialog 
   *
   *------------------------------------------------------------------------------
   *  Copyright (C) 2006-2014 University of Dundee. All rights reserved.
   *
   *
   *  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.,
   *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
   *
   *------------------------------------------------------------------------------
   */
  package org.openmicroscopy.shoola.agents.util.ui;
  
  //Java imports
  import java.awt.BorderLayout;
  import java.awt.Color;
  import java.awt.Component;
  import java.awt.Container;
  import java.awt.Dimension;
  import java.awt.Font;
  import java.awt.Frame;
  import java.awt.Point;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.awt.event.MouseAdapter;
  import java.awt.event.MouseEvent;
  import java.beans.PropertyChangeEvent;
  import java.beans.PropertyChangeListener;
  import java.util.ArrayList;
  import java.util.HashMap;
  import java.util.Iterator;
  import java.util.LinkedHashMap;
  import java.util.List;
  import java.util.Map;
  import java.util.Map.Entry;
  import javax.swing.BorderFactory;
  import javax.swing.Box;
  import javax.swing.BoxLayout;
  import javax.swing.JButton;
  import javax.swing.JCheckBox;
  import javax.swing.JComboBox;
  import javax.swing.JComponent;
  import javax.swing.JDialog;
  import javax.swing.JFrame;
  import javax.swing.JLabel;
  import javax.swing.JPanel;
  import javax.swing.JPopupMenu;
  import javax.swing.JScrollPane;
  import javax.swing.JTextField;
  import javax.swing.event.DocumentEvent;
  import javax.swing.event.DocumentListener;
  
  //Third-party libraries
  import info.clearthought.layout.TableLayout;
  
  import org.apache.commons.collections.CollectionUtils;
  import org.apache.commons.lang.StringUtils;
  
  //Application-internal dependencies
  import org.openmicroscopy.shoola.agents.util.EditorUtil;
  import org.openmicroscopy.shoola.agents.util.ViewerSorter;
  import org.openmicroscopy.shoola.env.data.model.ParamData;
  import org.openmicroscopy.shoola.env.data.model.ScriptObject;
  import org.openmicroscopy.shoola.util.ui.IconManager;
  import org.openmicroscopy.shoola.util.ui.NumericalTextField;
  import org.openmicroscopy.shoola.util.ui.TitlePanel;
  import org.openmicroscopy.shoola.util.ui.UIUtilities;
  import org.openmicroscopy.shoola.util.ui.omeeditpane.OMEWikiComponent;
  import org.openmicroscopy.shoola.util.ui.tdialog.TinyDialog;
  
  import pojos.DataObject;
  
  /** 
   * Dialog to run the selected script. The UI is created on the fly.
   *
   * @author  Jean-Marie Burel &nbsp;&nbsp;&nbsp;&nbsp;
   * <a href="mailto:j.burel@dundee.ac.uk">j.burel@dundee.ac.uk</a>
   * @author Donald MacDonald &nbsp;&nbsp;&nbsp;&nbsp;
   * <a href="mailto:donald@lifesci.dundee.ac.uk">donald@lifesci.dundee.ac.uk</a>
   * @version 3.0
   * @since 3.0-Beta4
   */
  public class ScriptingDialog
      extends JDialog
      implements ActionListener, DocumentListener, PropertyChangeListener
  {
  
      /** Message indicating that script can only run on objects.*/
      public static final String WARNING = 
         "This script can only run on data from the same group.\n" +
         "Please perform a separate run of the script for each group,\n" +
         "selecting only data from a single group to run the script on each time.";
  
      /** Bound property indicating to run the script. */
      public static final String RUN_SELECTED_SCRIPT_PROPERTY = 
              "runSelectedScript";
  
      /** Bound property indicating to download the script. */
      public static final String DOWNLOAD_SELECTED_SCRIPT_PROPERTY =
              "downloadSelectedScript";
  
      /** Bound property indicating to view the script. */
      public static final String VIEW_SELECTED_SCRIPT_PROPERTY =
              "viewSelectedScript";
  
      /** Bound property indicating to close the dialog. */
      public static final String CLOSE_SCRIPT_PROPERTY = "closeScript";
  
      /** The background of the description. */
      static final Color BG_COLOR = Color.LIGHT_GRAY;
  
      /** 
       * The size of the invisible components used to separate buttons
       * horizontally.
       */
      private static final Dimension H_SPACER_SIZE = new Dimension(5, 10);
  
      /** Title of the dialog. */
      private static final String TITLE = "Run Script";
  
      /** The text displayed in the header. */
      private static final String TEXT = "Set the parameters of the " +
              "selected script.";
  
      /** The text displayed in the header. */
      private static final String TEXT_END = EditorUtil.MANDATORY_SYMBOL +
              " indicates the required parameter.";
  
      /** Indicates to close the dialog. */
      private static final int CANCEL = 0;
  
      /** Indicates to run the script. */
      private static final int APPLY = 1;
  
      /** Indicates to download the script. */
      private static final int DOWNLOAD = 2;
  
      /** Indicates to view the script. */
      private static final int VIEW = 3;
  
      /** Close the dialog. */
      private JButton cancelButton;
  
      /** Run the script. */
      private JButton applyButton;
  
      /** Menu offering the ability to download or view the script. */
      private JButton menuButton;
  
      /** The object to handle. */
      private ScriptObject script;
  
      /** The components to display. */
      private Map<String, ScriptComponent> components;
  
      /** The components to display. */
      private Map<String, ScriptComponent> componentsAll;
  
      /** Used to sort collections. */
      private ViewerSorter sorter;
  
      /** The menu offering various options to manipulate the script. */
      private JPopupMenu optionMenu;
  
      /** The objects of reference.*/
      private List<DataObject> refObjects;
  
      /** The component displaying the data types.*/
      private JComboBox dataTypes;
  
      /** The component related to the identifier.*/
      private IdentifierParamPane identifier;
  
      /** Flag indicating if the binary data are available.*/
      private boolean binaryAvailable;
  
      /** Populates the changes of data types.*/
      private void handleDataTypeChanges()
      {
          if (identifier == null || dataTypes == null) return;
          Object o = dataTypes.getSelectedItem();
          Class<?> c = script.convertDataType(o.toString());
          if (c != null && CollectionUtils.isNotEmpty(refObjects)) {
              DataObject object = refObjects.get(0);
              if (object.getClass().equals(c))
                  identifier.setValues(refObjects);
              else identifier.setValues(null);
          }
      }
  
      /** Sets the type according to the selected objects.*/
      private void setSelectedDataType()
      {
          if (identifier == null || dataTypes == null ||
                  CollectionUtils.isEmpty(refObjects))
              return;
          DataObject object = refObjects.get(0);
          Class<?> klass = object.getClass();
          Class<?> c;
          int selected = dataTypes.getSelectedIndex();
          for (int i = 0; i < dataTypes.getItemCount(); i++) {
              c = script.convertDataType(dataTypes.getItemAt(i).toString());
              if (klass.equals(c)) {
                  selected = i;
                  break;
              }
          }
          dataTypes.setSelectedIndex(selected);
      }
  
      /** 
       * Creates the option menu.
       * 
       * @return See above.
       */
      private JPopupMenu createOptionMenu()
      {
          if (optionMenu != null) return optionMenu;
          optionMenu = new JPopupMenu();
          optionMenu.add(createButton("Download", DOWNLOAD));
          optionMenu.add(createButton("View", VIEW));
          return optionMenu;
      }
  
      /**
       * Creates a button.
       * 
       * @param text The text of the button.
       * @param actionID The action command id.
       * @param l The action listener.
       * @return See above.
       */
      private JButton createButton(String text, int actionID)
      {
          JButton b = new JButton(text);
          b.setActionCommand(""+actionID);
          b.addActionListener(this);
          b.setOpaque(false);
          UIUtilities.unifiedButtonLookAndFeel(b);
          return b;
      }
  
      /** Closes the dialog. */
      private void close()
      {
          setVisible(false);
          firePropertyChange(CLOSE_SCRIPT_PROPERTY, Boolean.valueOf(false),
                  Boolean.valueOf(true));
      }
  
      /**
       * Displays information of the identifier.
       * 
       * @param location Indicates where to display the component.
       */
      private void displayIdentifierInformation(Point location)
      {
          JLabel l = new JLabel(IdentifierParamPane.INFO_TEXT);
          TinyDialog d = new TinyDialog((Frame) getOwner(), l,
                  TinyDialog.CLOSE_ONLY);
          d.pack();
          d.setLocation(location);
          d.setVisible(true);
      }
  
      /**
       * Sets the enabled flag of the {@link #applyButton} to <code>true</code>
       * if the script can run i.e. all required fields are filled, to 
       * <code>false</code> otherwise.
       */
      private void canRunScript()
      {
          Iterator<Entry<String, ScriptComponent>>
          i = components.entrySet().iterator();
          Entry<String, ScriptComponent> entry;
          ScriptComponent c;
          int required = 0;
          int valueSet = 0;
          Object value;
          while (i.hasNext()) {
              entry = i.next();
              c = entry.getValue();
              if (c.isRequired()) {
                  required++;
                  value = c.getValue();
                  if (value != null) {
                      if (value instanceof String) {
                          if (StringUtils.isNotBlank((String) value)) valueSet++;
                      } else if (value instanceof List) {
                          List<Object> l = (List<Object>) value;
                          if (l.size() > 0) valueSet++;
                      } else if (value instanceof Map) {
                          Map<Object, Object> m = (Map<Object, Object>) value;
                          if (m.size() > 0) valueSet++;
                      } else valueSet++;
                  }
              }
          }
          applyButton.setEnabled(required == valueSet);
          if (!binaryAvailable) applyButton.setEnabled(false);
      }
  
      /** Collects the data and fires a property.*/
      private void runScript()
      {
          Entry<String, ScriptComponent> entry;
          ScriptComponent c;
          Iterator<Entry<String, ScriptComponent>> i =
                  componentsAll.entrySet().iterator();
          Map<String, ParamData> inputs = script.getInputs();
          ParamData param;
          while (i.hasNext()) {
              entry = i.next();
              c = entry.getValue();
              param = inputs.get(entry.getKey());
              param.setValueToPass(c.getValue());
          }
          firePropertyChange(RUN_SELECTED_SCRIPT_PROPERTY, null, script);
          close();
      }
  
      /** 
       * Creates a component displaying the various options.
       * 
       * @param values The values to display.
       * @param defValue The default value.
       * @return See above.
       */
      private JComboBox createValuesBox(List<Object> values, Object defValue)
      {
          if (values == null) return null;
          Object[] v = new Object[values.size()];
          Iterator<Object> i = values.iterator();
          int j = 0;
          int index = 0;
          while (i.hasNext()) {
              v[j] = i.next();
              if (v[j].equals(defValue)) {
                  index = j;
              }
              j++;
          }
          JComboBox box = new JComboBox(v);
          box.setSelectedIndex(index);
          return box;
      }
  
      /** Initializes the components. */
      private void initComponents()
      {
          sorter = new ViewerSorter();
          cancelButton = new JButton("Cancel");
          cancelButton.setToolTipText("Close the dialog.");
          cancelButton.setActionCommand(""+CANCEL);
          cancelButton.addActionListener(this);
          applyButton = new JButton("Run Script");
          applyButton.setToolTipText("Run the selected script.");
          applyButton.setActionCommand(""+APPLY);
          applyButton.addActionListener(this);
          IconManager icons = IconManager.getInstance();
          menuButton = new JButton(icons.getIcon(IconManager.FILTER_MENU));
          menuButton.setText("Script");
          menuButton.setHorizontalTextPosition(JButton.LEFT);
          menuButton.addMouseListener(new MouseAdapter() {
  
              public void mouseReleased(MouseEvent e) {
                  Object src = e.getSource();
                  if (src instanceof Component) {
                      Point p = e.getPoint();
                      createOptionMenu().show((Component) src, p.x, p.y);
                  }
              }
          });
          components = new LinkedHashMap<String, ScriptComponent>();
          componentsAll = new LinkedHashMap<String, ScriptComponent>();
          Map<String, ParamData> types = script.getInputs();
          if (types == null) return;
          List <ScriptComponent> results = new ArrayList<ScriptComponent>();
          Entry<String, ParamData> entry;
          ParamData param;
          JComponent comp;
          ScriptComponent c;
          String name;
          Class<?> type;
          Object defValue ;
          Iterator<Entry<String, ParamData>> i = types.entrySet().iterator();
          List<Object> values;
          Number n;
          String details = "";
          String text = "";
          String grouping;
          String parent;
          Map<String, List<ScriptComponent>> childrenMap =
                  new HashMap<String, List<ScriptComponent>>();
          List<ScriptComponent> l;
          int length;
          boolean columnsSet;
          while (i.hasNext()) {
              text = "";
              columnsSet = false;
              comp = null;
              entry = i.next();
              param = entry.getValue();
              name = entry.getKey();
              type = param.getPrototype();
              values = param.getValues();
              defValue = param.getDefaultValue();
              if (CollectionUtils.isNotEmpty(values)) {
                  comp = createValuesBox(values, defValue);
              }
              if (Long.class.equals(type) || Integer.class.equals(type) ||
                      Float.class.equals(type) || Double.class.equals(type)) {
                  if (comp == null) {
                      comp = new NumericalTextField();
                      ((NumericalTextField) comp).setNumberType(type);
                      n = param.getMinValue();
                      if (n != null) {
                          if (Long.class.equals(type)) {
                              text += "Min: "+n.longValue()+" ";
                              ((NumericalTextField) comp).setMinimum(
                                      n.longValue());
                          } else if (Integer.class.equals(type)) {
                              text += "Min: "+n.intValue()+" ";
                              ((NumericalTextField) comp).setMinimum(
                                      n.intValue());
                          } else if (Double.class.equals(type)) {
                              text += "Min: "+n.doubleValue()+" ";
                              ((NumericalTextField) comp).setMinimum(
                                      n.doubleValue());
                          } else if (Float.class.equals(type)) {
                              text += "Min: "+n.floatValue()+" ";
                              ((NumericalTextField) comp).setMinimum(
                                      n.floatValue());
                          } 
                      } else 
                          ((NumericalTextField) comp).setNegativeAccepted(true);
                      n = param.getMaxValue();
                      if (n != null) {
                          if (Long.class.equals(type)) {
                              text += "Max: "+n.longValue()+" ";
                              ((NumericalTextField) comp).setMaximum(
                                      n.longValue());
                          } else if (Integer.class.equals(type)) {
                              text += "Max: "+n.intValue()+" ";
                              ((NumericalTextField) comp).setMaximum(
                                      n.intValue());
                          } else if (Double.class.equals(type)) {
                              text += "Max: "+n.doubleValue()+" ";
                              ((NumericalTextField) comp).setMaximum(
                                      n.doubleValue());
                          } else if (Float.class.equals(type)) {
                              text += "Max: "+n.floatValue()+" ";
                              ((NumericalTextField) comp).setMaximum(
                                      n.floatValue());
                          } 
                      }
                      if (defValue != null)
                          ((NumericalTextField) comp).setText(""+defValue);
                  }
              } else if (String.class.equals(type)) {
                  if (comp == null) {
                      comp = new JTextField();
  