Ignore:
Timestamp:
2010-05-27T16:29:00+12:00 (14 years ago)
Author:
sjm84
Message:

Added password, browsing and checking functionality as well as some basic extension state checking

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone3/src/java/org/greenstone/admin/guiext/Option.java

    r21991 r22185  
    11package org.greenstone.admin.guiext;
     2
     3import java.lang.reflect.Constructor;
     4import java.lang.reflect.Method;
     5
     6import javax.swing.ImageIcon;
     7import javax.swing.JTable;
     8
     9import org.greenstone.admin.GAI;
    210
    311import org.w3c.dom.Element;
     
    513public class Option
    614{
    7     String _name = null;
    8     String _id = null;
    9     String _value = null;   
    10     OptionList _parent = null;
     15    protected String _type = null;
     16    protected String _name = null;
     17    protected String _id = null;
     18    protected String _value = null;   
     19    protected OptionList _parent = null;
     20   
     21    protected Object _classObject = null;
     22    protected Method _checkMethod = null;
     23
     24    protected ImageIcon _image = null;
     25
     26    private boolean _canCheck = false;
     27    private int _checkResult = -1;
     28
     29    private final String GOOD_ICON = GAI.getGSDL3Home() + System.getProperty("file.separator") + "resources" + System.getProperty("file.separator") + "images" + System.getProperty("file.separator") + "icongreentick.png";
     30    private final String BAD_ICON = GAI.getGSDL3Home() + System.getProperty("file.separator") + "resources" + System.getProperty("file.separator") + "images" + System.getProperty("file.separator") + "iconredexclamation.png";
     31    private final String ERROR_ICON = GAI.getGSDL3Home() + System.getProperty("file.separator") + "resources" + System.getProperty("file.separator") + "images" + System.getProperty("file.separator") + "iconorangetriange.png";
    1132
    1233    public Option(Element optionElement, OptionList parent)
     
    3051        _value = "";
    3152        }
     53
     54        String checkClass = optionElement.getAttribute("checkClass");
     55        String checkMethod = optionElement.getAttribute("checkMethod");
     56
     57        if(!checkClass.equals("") && !checkMethod.equals("")){
     58        loadCheckMethod(checkClass, checkMethod);
     59        }
     60
     61        performCheck();
     62
     63        _type = optionElement.getAttribute("type");
     64       
     65        if(!(_type.equals("") || _type.equals("password") || _type.equals("filebrowse") || _type.equals("folderbrowse"))){
     66        System.err.println("This option element specifies a type that is not \"password\", \"filebrowse\" or \"folderbrowse\"");       
     67        }
    3268    }
    3369    else{
     
    3571    }
    3672    }
     73
     74    private void loadCheckMethod(String checkClassString, String checkMethodString)
     75    {
     76    _parent.getParent().getParent().getParent().loadGuiExtFile();
     77   
     78    Class checkClass = null;
     79    try{
     80        checkClass = Class.forName(checkClassString);
     81    }
     82    catch(Exception ex){
     83        System.err.println("Could not get an instance of the check class, either the class name is incorrect or the class does not exist inside the guiext.jar file");
     84        return;
     85    }
     86   
     87    try{
     88        Constructor classConstructor = checkClass.getConstructor(new Class[0]);
     89        _classObject = classConstructor.newInstance(new Object[0]);
     90    }
     91    catch(Exception ex){
     92        System.err.println("Could not instantiate the check class, either the class name is incorrect or the class does not exist inside the guiext.jar file");
     93        return;
     94    }
     95
     96    try{
     97        _checkMethod = checkClass.getDeclaredMethod(checkMethodString, new Class[]{Object.class});
     98    }
     99    catch(Exception ex){
     100        System.err.println("Could not get the check method, either the method name is incorrect or the method does not take an Object as a parameter");
     101        return;
     102    }
     103       
     104    if(!(_checkMethod.getReturnType().equals(Boolean.TYPE))){
     105        System.err.println("The check method does not return a boolean value as is required");
     106        return;
     107    }
     108    _canCheck = true;
     109    }
     110
     111    private void setImageFromCheck()
     112    {
     113    if(_canCheck){
     114        if(_checkResult == 0){
     115        _image = new ImageIcon(GOOD_ICON);
     116        }
     117        else if(_checkResult == 1){
     118        _image = new ImageIcon(BAD_ICON);
     119        }
     120        else{
     121        _image = new ImageIcon(ERROR_ICON);
     122        }
     123    }
     124    else{
     125        _image = new ImageIcon(GOOD_ICON);
     126    }
     127    }
     128   
     129    private void performCheck()
     130    {
     131    if(_canCheck){
     132        Boolean result = null;
     133        try{
     134        result = ((Boolean)_checkMethod.invoke(_classObject, new Object[]{_value}));
     135        }
     136        catch(Exception ex){
     137        _checkResult = -1;
     138        }
     139        if(result){
     140        _checkResult = 0;
     141        }
     142        else{
     143        _checkResult = 1;
     144        }
     145    }
     146    setImageFromCheck();
     147    }
     148
     149    public boolean isCheckable()
     150    {
     151    return _canCheck;
     152    }
     153
     154    public int getCheckResult()
     155    {
     156    return _checkResult;
     157    }
    37158   
    38159    public String getName()
     
    49170    {
    50171    _value = value;
     172    performCheck();
     173
     174    JTable table = _parent.getParent().getTableFromOptionList(_parent);
     175   
     176    if(table != null){
     177        table.repaint();
     178        table.revalidate();
     179    }
    51180    }
    52181
     
    60189    return _parent;
    61190    }
     191   
     192    public String getType()
     193    {
     194    return _type;
     195    }
     196
     197    public ImageIcon getImage()
     198    {
     199    return _image;
     200    }
    62201}
Note: See TracChangeset for help on using the changeset viewer.