Ignore:
Timestamp:
2009-04-08T13:27:32+12:00 (15 years ago)
Author:
kjdon
Message:

added support for new plugin option type: enumstring, which has a list of predefined values, but can actually take any value - an editbale combobox in gli

File:
1 edited

Legend:

Unmodified
Added
Removed
  • gli/trunk/src/org/greenstone/gatherer/cdm/ArgumentControl.java

    r18910 r18911  
    9292
    9393    switch(argument.getType()) {
     94    case Argument.ENUM_STRING:
    9495    case Argument.ENUM:
    9596      ArrayList option_list = argument.getOptions();
    96       value_control = new GComboBox(option_list.toArray(), false, false);
     97      boolean editable = false;
     98      if (argument.getType() == Argument.ENUM_STRING) {
     99    editable = true;
     100      }
     101      value_control = new GComboBox(option_list.toArray(), editable, false);
    97102      value_control.setComponentOrientation(Dictionary.getOrientation());
    98103           
    99       selectValue((JComboBox)value_control, initial_value); // also sets the tooltip
     104      boolean found = selectValue((JComboBox)value_control, initial_value); // also sets the tooltip
     105      if (!found && argument.getType() == Argument.ENUM_STRING) {
     106    // Its a custom item
     107    ((JComboBox) value_control).addItem(initial_value);
     108    ((JComboBox) value_control).setSelectedItem(initial_value);
     109      }
     110
    100111      ((JComboBox)value_control).addActionListener(new ToolTipUpdater());
    101112      break;
     
    158169      }
    159170      if (existing_value != null && existing_value.length() > 0) {
    160     boolean found = selectValue((JComboBox) value_control, existing_value);
     171    found = selectValue((JComboBox) value_control, existing_value);
    161172    // It's possible that this is a custom value and so doesn't exist in the combobox
    162173    if (!found) {
     
    244255    // Listener
    245256    if(value_control != null) {
    246       if (argument.getType() != Argument.ENUM) {
     257      if (argument.getType() != Argument.ENUM && argument.getType() != Argument.ENUM_STRING) {
    247258    // enums have already set tooltips based on option value
    248259    value_control.setToolTipText(tip);
     
    325336      case Argument.ENUM:
    326337    Argument.ArgumentOption option = (Argument.ArgumentOption)((JComboBox)value_control).getSelectedItem();
    327     if(option != null && option.name.length() > 0) {
    328       argument.setValue(option.name);
    329     }
    330     else {
    331       String args[] = new String[1];
    332       args[0] = argument.getName();
    333       if(argument.isRequired()) {
    334         JOptionPane.showMessageDialog(this, Dictionary.get("CDM.ArgumentConfiguration.Required_Argument", args), Dictionary.get("CDM.ArgumentConfiguration.Error_Title"), JOptionPane.ERROR_MESSAGE);
    335       }
    336       // They've left the field blank
    337       else {
    338         JOptionPane.showMessageDialog(this, Dictionary.get("CDM.ArgumentConfiguration.No_Value", args), Dictionary.get("CDM.ArgumentConfiguration.Error_Title"), JOptionPane.ERROR_MESSAGE);
    339         argument.setValue(null);
    340       }
    341       args = null;
    342       return false;
    343     }
     338    // its impossible not to choose an entry
     339    argument.setValue(option.name);
    344340    argument.setAssigned(true);
    345341    return true;
    346       case Argument.FLAG:
    347     // Should have already been handled above.
    348     argument.setAssigned(true);
    349     return true;
    350       case Argument.INTEGER:
    351     result = ((JSpinner)value_control).getValue().toString();
    352     if(result.length() > 0) {
    353       // Test if the value entered is a valid int.
    354       try {
    355         int x = Integer.parseInt(result);
    356       }
    357       catch(NumberFormatException nfe) {
    358         String args[] = new String[2];
    359         args[0] = argument.getName();
    360         args[1] = result;
    361         JOptionPane.showMessageDialog(this, Dictionary.get("CDM.ArgumentConfiguration.Bad_Integer", args), Dictionary.get("CDM.ArgumentConfiguration.Error_Title"), JOptionPane.ERROR_MESSAGE);
    362         args = null;
    363         return false;
    364       }
    365       argument.setValue(result);
    366     }
    367     else {
    368       String args[] = new String[1];
    369       args[0] = argument.getName();
    370       if(argument.isRequired()) {
    371         JOptionPane.showMessageDialog(this, Dictionary.get("CDM.ArgumentConfiguration.Required_Argument", args), Dictionary.get("CDM.ArgumentConfiguration.Error_Title"), JOptionPane.ERROR_MESSAGE);
    372       }
    373       // They've left the field blank
    374       else {
    375         JOptionPane.showMessageDialog(this, Dictionary.get("CDM.ArgumentConfiguration.No_Value", args), Dictionary.get("CDM.ArgumentConfiguration.Error_Title"), JOptionPane.ERROR_MESSAGE);
    376         argument.setValue(null);
    377       }
    378       args = null;
    379       return false;
    380     }
    381     argument.setAssigned(true);
    382     return true;
    383       case Argument.LANGUAGE:
    384     String language = (((JComboBox)value_control).getSelectedItem()).toString();
    385     argument.setValue(language);
    386     // Kinda lucked out here. Its impossible not to choose an entry from these comboboxes as they are restricted.
    387     argument.setAssigned(true);
    388     return true;
    389       case Argument.METADATUM:
    390       case Argument.METADATA:
    391     Object new_value_raw = ((JComboBox) value_control).getSelectedItem();
    392     if (new_value_raw instanceof MetadataElement) {
    393       argument.setValue(((MetadataElement) new_value_raw).getFullName());
    394     }
    395     else {
    396       // But we have to be careful as an arbitary string object could be zero length
     342      case Argument.ENUM_STRING:
     343    Object new_value_raw = ((JComboBox)value_control).getSelectedItem();
     344    if (new_value_raw instanceof Argument.ArgumentOption) {
     345      argument.setValue(((Argument.ArgumentOption)new_value_raw).name);
     346    } else {
     347      // have entered a new string
    397348      String new_value = new_value_raw.toString();
    398       ///ystem.err.println("The current value is: " + new_value);
    399       if(new_value.length() > 0) {
     349      if (new_value.length() > 0) {
    400350        argument.setValue(new_value);
    401351      }
     
    417367    argument.setAssigned(true);
    418368    return true;
     369      case Argument.FLAG:
     370    // Should have already been handled above.
     371    argument.setAssigned(true);
     372    return true;
     373      case Argument.INTEGER:
     374    result = ((JSpinner)value_control).getValue().toString();
     375    if(result.length() > 0) {
     376      // Test if the value entered is a valid int.
     377      try {
     378        int x = Integer.parseInt(result);
     379      }
     380      catch(NumberFormatException nfe) {
     381        String args[] = new String[2];
     382        args[0] = argument.getName();
     383        args[1] = result;
     384        JOptionPane.showMessageDialog(this, Dictionary.get("CDM.ArgumentConfiguration.Bad_Integer", args), Dictionary.get("CDM.ArgumentConfiguration.Error_Title"), JOptionPane.ERROR_MESSAGE);
     385        args = null;
     386        return false;
     387      }
     388      argument.setValue(result);
     389    }
     390    else {
     391      String args[] = new String[1];
     392      args[0] = argument.getName();
     393      if(argument.isRequired()) {
     394        JOptionPane.showMessageDialog(this, Dictionary.get("CDM.ArgumentConfiguration.Required_Argument", args), Dictionary.get("CDM.ArgumentConfiguration.Error_Title"), JOptionPane.ERROR_MESSAGE);
     395      }
     396      // They've left the field blank
     397      else {
     398        JOptionPane.showMessageDialog(this, Dictionary.get("CDM.ArgumentConfiguration.No_Value", args), Dictionary.get("CDM.ArgumentConfiguration.Error_Title"), JOptionPane.ERROR_MESSAGE);
     399        argument.setValue(null);
     400      }
     401      args = null;
     402      return false;
     403    }
     404    argument.setAssigned(true);
     405    return true;
     406      case Argument.LANGUAGE:
     407    String language = (((JComboBox)value_control).getSelectedItem()).toString();
     408    argument.setValue(language);
     409    // Kinda lucked out here. Its impossible not to choose an entry from these comboboxes as they are restricted.
     410    argument.setAssigned(true);
     411    return true;
     412      case Argument.METADATUM:
     413      case Argument.METADATA:
     414    new_value_raw = ((JComboBox) value_control).getSelectedItem();
     415    if (new_value_raw instanceof MetadataElement) {
     416      argument.setValue(((MetadataElement) new_value_raw).getFullName());
     417    }
     418    else {
     419      // But we have to be careful as an arbitary string object could be zero length
     420      String new_value = new_value_raw.toString();
     421      ///ystem.err.println("The current value is: " + new_value);
     422      if(new_value.length() > 0) {
     423        argument.setValue(new_value);
     424      }
     425      else {
     426        String args[] = new String[1];
     427        args[0] = argument.getName();
     428        if(argument.isRequired()) {
     429          JOptionPane.showMessageDialog(this, Dictionary.get("CDM.ArgumentConfiguration.Required_Argument", args), Dictionary.get("CDM.ArgumentConfiguration.Error_Title"), JOptionPane.ERROR_MESSAGE);
     430        }
     431        // They've left the field blank
     432        else {
     433          JOptionPane.showMessageDialog(this, Dictionary.get("CDM.ArgumentConfiguration.No_Value", args), Dictionary.get("CDM.ArgumentConfiguration.Error_Title"), JOptionPane.ERROR_MESSAGE);
     434          argument.setValue(null);
     435        }
     436        args = null;
     437        return false;
     438      }
     439    }
     440    argument.setAssigned(true);
     441    return true;
    419442      case Argument.REGEXP:
    420443      case Argument.STRING:
Note: See TracChangeset for help on using the changeset viewer.