Changeset 22185


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

Location:
main/trunk/greenstone3/src/java/org/greenstone/admin/guiext
Files:
5 edited

Legend:

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

    r22063 r22185  
    3232        }
    3333        else{
    34             _osCommands.put(os, ExtXMLHelper.getValueFromSingleElement(currentCommand, true));
     34            _osCommands.put(os, ExtXMLHelper.getValueFromSingleElement(currentCommand, false));
    3535        }
    3636        }
  • 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}
  • main/trunk/greenstone3/src/java/org/greenstone/admin/guiext/PropertiesStep.java

    r22085 r22185  
    11package org.greenstone.admin.guiext;
    22
     3import javax.crypto.Cipher;
     4import javax.crypto.KeyGenerator;
     5import javax.crypto.SecretKey;
     6
     7import javax.crypto.spec.SecretKeySpec;
     8
     9import javax.swing.AbstractCellEditor;
    310import javax.swing.BorderFactory;
     11import javax.swing.ImageIcon;
     12import javax.swing.JButton;
     13import javax.swing.JFileChooser;
     14import javax.swing.JFrame;
    415import javax.swing.JLabel;
     16import javax.swing.JOptionPane;
    517import javax.swing.JPanel;
    6 import javax.swing.JFrame;
    7 import javax.swing.JButton;
     18import javax.swing.JPasswordField;
    819import javax.swing.JScrollPane;
    920import javax.swing.JTable;
     21import javax.swing.JTextField;
    1022import javax.swing.SwingConstants;
     23
     24import javax.swing.event.DocumentListener;
     25import javax.swing.event.DocumentEvent;
    1126
    1227import javax.swing.table.AbstractTableModel;
    1328import javax.swing.table.TableCellEditor;
     29import javax.swing.table.TableColumn;
     30import javax.swing.table.TableModel;
    1431
    1532import java.awt.event.ActionListener;
     
    2037import java.awt.BorderLayout;
    2138import java.awt.Color;
     39import java.awt.Component;
    2240import java.awt.Font;
    2341import java.awt.GridLayout;
    2442
    2543import java.util.Properties;
     44import java.util.Arrays;
     45import java.util.HashMap;
    2646
    2747import java.io.File;
     
    3959    JTable[] _tables = null;
    4060    OptionList[] _modifiedOptionLists = null;
     61   
     62    HashMap _optionListTableMap = new HashMap();
    4163
    4264    public PropertiesStep(Element propertiesStepElement, SequenceList parent)
     
    80102        }
    81103    }
     104    }
     105
     106    public JTable getTableFromOptionList(OptionList list)
     107    {
     108    return ((JTable)_optionListTableMap.get(list));
    82109    }
    83110
     
    114141        Option[] options = currentList.getOptions();       
    115142
    116         JTable propertiesTable = new JTable(new CustomTableModel(new String[]{"Setting", "Value"}, options));
     143        CustomTableModel tableModel = new CustomTableModel(new String[]{"Setting", "Value", "Check"}, options);
     144        PropertyTable propertiesTable = new PropertyTable(tableModel, currentList);
     145        TableColumn column = propertiesTable.getColumnModel().getColumn(2);
     146        column.setPreferredWidth(32);
     147        column.setMinWidth(32);
     148        column.setMaxWidth(32);
     149        column.setResizable(false);
     150
     151        _optionListTableMap.put(currentList, propertiesTable);
     152
    117153        //The line below is necessary as the default grid colour on mac is white, which makes the lines invisible.
    118154        propertiesTable.setGridColor(Color.BLACK);
     
    148184       
    149185        _parent.getParent().changeExtPane(mainPanel);
     186    }
     187    }
     188
     189    public class BrowseButtonListener implements ActionListener
     190    {
     191    Option _option = null;
     192    JTextField _path = null;
     193    JFileChooser _browser = null;
     194   
     195    public BrowseButtonListener(Option option, JTextField path)
     196    {
     197        _option = option;
     198        _path = path;
     199        _browser = new JFileChooser();
     200    }
     201   
     202    public void actionPerformed(ActionEvent e)
     203    {
     204        if(e.getActionCommand().equals("edit")){
     205        if(_option.getType().equalsIgnoreCase("folderbrowse")){
     206            _browser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
     207        }
     208        int returnValue = _browser.showOpenDialog(null);
     209
     210        if(returnValue == JFileChooser.APPROVE_OPTION){
     211            _option.setValue(_browser.getSelectedFile().getAbsolutePath());
     212            _path.setText(_option.getValue());
     213        }
     214        }
     215    }
     216    }
     217
     218    public class PathBoxListener implements DocumentListener
     219    {
     220    Option _option = null;
     221    JTextField _path = null;
     222
     223    public PathBoxListener(Option option, JTextField path)
     224    {
     225        _option = option;
     226        _path = path;
     227    }
     228   
     229    public void changedUpdate(DocumentEvent e)
     230    {
     231        _option.setValue(_path.getText());
     232    }
     233   
     234    public void insertUpdate(DocumentEvent e)
     235    {
     236        _option.setValue(_path.getText());
     237    }
     238
     239    public void removeUpdate(DocumentEvent e)
     240    {
     241        _option.setValue(_path.getText());
     242    }
     243    }
     244
     245    public class PasswordOKButtonListener implements ActionListener
     246    {
     247    JPasswordField _password = null;
     248    JPasswordField _confirm = null;
     249    Option _option = null;
     250    JFrame _passwordFrame = null;
     251
     252    public PasswordOKButtonListener(JPasswordField password, JPasswordField confirm, Option option, JFrame passwordFrame)
     253    {
     254        _password = password;
     255        _confirm = confirm;
     256        _option = option;
     257        _passwordFrame = passwordFrame;
     258    }
     259
     260    public void actionPerformed(ActionEvent e)
     261    {
     262        if(Arrays.equals(_password.getPassword(), _confirm.getPassword())){
     263        try{
     264            _option.setValue(new String(encrypt(_password.getPassword())));
     265        }
     266        catch(Exception ex){
     267            System.err.println("Error encrypting password");
     268        }
     269        _passwordFrame.dispose();
     270        }
     271        else{
     272        JOptionPane.showMessageDialog(null, "The passwords you entered do not match.");
     273        _password.setText("");
     274        _confirm.setText("");
     275        }
     276    }
     277    } 
     278
     279    public static char[] encrypt(char[] value) throws Exception
     280    {
     281    byte[] salt = {'T', 'p'};
     282    int count = 20;
     283
     284    KeyGenerator keygen = KeyGenerator.getInstance("AES");
     285        SecretKey secretKey = keygen.generateKey();
     286        byte[] raw = secretKey.getEncoded();
     287        SecretKeySpec secretKeySpec = new SecretKeySpec(raw, "AES");
     288
     289    Cipher cipher = Cipher.getInstance("AES");
     290    cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
     291   
     292    byte[] encryptedPassword = cipher.doFinal((new String(value)).getBytes("UTF-8"));
     293
     294    return new String(encryptedPassword).toCharArray();
     295    }
     296
     297    public class PasswordCancelButtonListener implements ActionListener
     298    {
     299    JFrame _passwordFrame = new JFrame();
     300
     301    public PasswordCancelButtonListener(JFrame passwordFrame)
     302    {
     303        _passwordFrame = passwordFrame;
     304    }
     305
     306    public void actionPerformed(ActionEvent e)
     307    {
     308        _passwordFrame.dispose();
     309    }
     310    }
     311
     312    public class PasswordFieldListener implements ActionListener
     313    {
     314    JPasswordField _password = new JPasswordField(10);
     315    JPasswordField _confirm = new JPasswordField(10);
     316    JFrame _passwordFrame = new JFrame("Enter password");
     317
     318    public PasswordFieldListener(Option option)
     319    {
     320        JButton okButton = new JButton("Change");
     321        JButton cancelButton = new JButton("Cancel");
     322       
     323        okButton.addActionListener(new PasswordOKButtonListener(_password, _confirm, option, _passwordFrame));
     324        cancelButton.addActionListener(new PasswordCancelButtonListener(_passwordFrame));
     325
     326        _passwordFrame.getContentPane().setLayout(new GridLayout(3,2));
     327        _passwordFrame.getContentPane().add(new JLabel("Password:"));
     328        _passwordFrame.getContentPane().add(_password);
     329        _passwordFrame.getContentPane().add(new JLabel("Confirm:"));
     330        _passwordFrame.getContentPane().add(_confirm);
     331        _passwordFrame.getContentPane().add(okButton);
     332        _passwordFrame.getContentPane().add(cancelButton);
     333        _passwordFrame.pack();
     334    }
     335   
     336    public void actionPerformed(ActionEvent e)
     337    {
     338        _password.setText("");
     339        _confirm.setText("");
     340        _passwordFrame.setLocationRelativeTo(null);
     341        _passwordFrame.setVisible(true);
     342    }
     343    }
     344
     345    public class PasswordEditor extends AbstractCellEditor implements TableCellEditor
     346    {
     347    JButton _onClickButton = new JButton("Change Password?");
     348
     349    public PasswordEditor(Option option)
     350    {
     351        _onClickButton.addActionListener(new PasswordFieldListener(option));
     352    }
     353
     354    public Object getCellEditorValue()
     355    {
     356        return "";
     357    }
     358
     359    public Component getTableCellEditorComponent(JTable table, Object path, boolean isSelected, int row, int column)
     360    {
     361        return _onClickButton;
     362    }
     363    }
     364
     365    public class BrowseEditor extends AbstractCellEditor implements TableCellEditor
     366    {
     367    JPanel _browserPanel = null;
     368    JTextField _path = null;
     369    JButton _browserButton = null;
     370    Option _option = null;
     371    String _type = null;
     372
     373    public BrowseEditor(Option option, String type)
     374    {
     375        _option = option;
     376        _type = type;
     377       
     378        _path = new JTextField(_option.getValue());
     379        _path.getDocument().addDocumentListener(new PathBoxListener(_option, _path));
     380
     381        _browserButton = new JButton("Browse");
     382        _browserButton.setActionCommand("edit");
     383        _browserButton.addActionListener(new BrowseButtonListener(_option, _path));
     384        _browserButton.setBorderPainted(false);
     385
     386        _browserPanel = new JPanel();
     387        _browserPanel.setLayout(new BorderLayout());
     388        _browserPanel.add(_path, BorderLayout.CENTER);
     389        _browserPanel.add(_browserButton, BorderLayout.EAST);
     390    }
     391
     392    public Object getCellEditorValue()
     393    {
     394        _path.setText(_option.getValue());
     395        return _option.getValue();
     396    }
     397
     398    public Component getTableCellEditorComponent(JTable table, Object path, boolean isSelected, int row, int column)
     399    {
     400        return _browserPanel;
     401    }
     402    }
     403
     404    public class PropertyTable extends JTable
     405    {
     406    OptionList _properties = null;
     407    TableCellEditor[] _editors = null;
     408
     409    public PropertyTable(TableModel tm, OptionList properties)
     410    {
     411        super(tm);
     412        _properties = properties;
     413       
     414        Option[] options = _properties.getOptions();
     415        _editors = new TableCellEditor[options.length];
     416
     417        for(int i = 0; i < options.length; i++){
     418        Option currentOption = options[i];
     419
     420        if(currentOption.getType().equals("filebrowse")){
     421            _editors[i] = new BrowseEditor(currentOption, "file");
     422        }
     423        else if(currentOption.getType().equals("folderbrowse")){
     424            _editors[i] = new BrowseEditor(currentOption, "folder");
     425        }
     426        else if(currentOption.getType().equals("password")){
     427            _editors[i] = new PasswordEditor(currentOption);
     428        }
     429        else{
     430            _editors[i] = null;
     431        }
     432        }
     433    }
     434
     435    public TableCellEditor getCellEditor(int row, int column)
     436    {
     437        if(column == 1 && row < _editors.length && _editors[row] != null){
     438        return _editors[row];
     439        }
     440        return super.getCellEditor(row, column);
    150441    }
    151442    }
     
    225516    public class CustomTableModel extends AbstractTableModel
    226517    {
    227     private String[] _columnNames;
    228         private Option[] _data;
     518    private String[] _columnNames = null;
     519        private Option[] _data = null;
     520    private ImageIcon[] _images = null;
    229521   
    230522        public CustomTableModel(String[] columnNames, Option[] data){
    231523        _columnNames = columnNames;
    232524        _data = data;
     525       
     526        _images = new ImageIcon[_data.length];
     527        for(int i = 0; i < _data.length; i++){
     528        _images[i] = _data[i].getImage();
     529        }
    233530        }
    234531   
     
    246543
    247544        public Object getValueAt(int row, int col) {
    248         Option o = _data[row];
    249545        if(col == 0){
    250         return o.getName();
     546        return _data[row].getName();
    251547        }
    252548        else if(col == 1){
    253         return o.getValue();
     549        if(_data[row].getType().equals("password") && !_data[row].getValue().equals("")){
     550            return "Password Set";
     551        }
     552        else if(_data[row].getType().equals("password") && _data[row].getValue().equals("")){
     553            return "No Password Set";
     554        }
     555       
     556        return _data[row].getValue();
     557        }
     558        else if(col == 2){
     559        return _data[row].getImage();
    254560        }
    255561            else{
     
    264570
    265571    public boolean isCellEditable(int row, int col) {
    266         if (col < 1) {
     572        if (col == 1) {
     573                return true;
     574            } else {
    267575                return false;
    268             } else {
    269                 return true;
    270576            }
    271577        }
    272578   
    273579        public void setValueAt(Object value, int row, int col) {
    274         if(isCellEditable(row, col)){
    275         _data[row].setValue((String)value);
     580        Option o = _data[row];
     581        if(isCellEditable(row, col) && !o.getType().equals("password")){
     582        o.setValue((String)value);
     583       
     584        if(o.isCheckable()){
     585            _images[row] = o.getImage();
     586        }
    276587        }
    277588        fireTableDataChanged();
  • main/trunk/greenstone3/src/java/org/greenstone/admin/guiext/SequenceList.java

    r21919 r22185  
    201201    {
    202202    loadExtensionStatesFromFile();
    203    
     203
    204204    ExtensionInformation currentExtension = _parent;
    205205    String fileStem = currentExtension.getFileStem();
     206
     207    File extDir = new File(currentExtension.getExtensionDirectory());
     208    if(!extDir.exists()){
     209        rollbackTo("");
     210    }
    206211   
    207212    for(int i = 0; i < _steps.size(); i++){
  • main/trunk/greenstone3/src/java/org/greenstone/admin/guiext/Step.java

    r21919 r22185  
    99import javax.swing.JTextArea;
    1010import javax.swing.JPanel;
    11 
    12 /*
    13 if(_commonMessageArea == null){
    14         _commonMessageArea = new JTextArea();
    15         _commonMessageArea.setEditable(false);
    16         _commonMessageArea.setAutoscrolls(true);
    17         _commonMessageArea.setLineWrap(true);
    18         _commonMessageArea.setWrapStyleWord(true);
    19         _commonMessageArea.setMargin(new Insets(0,5,0,0));
    20     }
    21 */
    2211
    2312public class Step
Note: See TracChangeset for help on using the changeset viewer.