- Timestamp:
- 2010-05-27T16:29:00+12:00 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/trunk/greenstone3/src/java/org/greenstone/admin/guiext/PropertiesStep.java
r22085 r22185 1 1 package org.greenstone.admin.guiext; 2 2 3 import javax.crypto.Cipher; 4 import javax.crypto.KeyGenerator; 5 import javax.crypto.SecretKey; 6 7 import javax.crypto.spec.SecretKeySpec; 8 9 import javax.swing.AbstractCellEditor; 3 10 import javax.swing.BorderFactory; 11 import javax.swing.ImageIcon; 12 import javax.swing.JButton; 13 import javax.swing.JFileChooser; 14 import javax.swing.JFrame; 4 15 import javax.swing.JLabel; 16 import javax.swing.JOptionPane; 5 17 import javax.swing.JPanel; 6 import javax.swing.JFrame; 7 import javax.swing.JButton; 18 import javax.swing.JPasswordField; 8 19 import javax.swing.JScrollPane; 9 20 import javax.swing.JTable; 21 import javax.swing.JTextField; 10 22 import javax.swing.SwingConstants; 23 24 import javax.swing.event.DocumentListener; 25 import javax.swing.event.DocumentEvent; 11 26 12 27 import javax.swing.table.AbstractTableModel; 13 28 import javax.swing.table.TableCellEditor; 29 import javax.swing.table.TableColumn; 30 import javax.swing.table.TableModel; 14 31 15 32 import java.awt.event.ActionListener; … … 20 37 import java.awt.BorderLayout; 21 38 import java.awt.Color; 39 import java.awt.Component; 22 40 import java.awt.Font; 23 41 import java.awt.GridLayout; 24 42 25 43 import java.util.Properties; 44 import java.util.Arrays; 45 import java.util.HashMap; 26 46 27 47 import java.io.File; … … 39 59 JTable[] _tables = null; 40 60 OptionList[] _modifiedOptionLists = null; 61 62 HashMap _optionListTableMap = new HashMap(); 41 63 42 64 public PropertiesStep(Element propertiesStepElement, SequenceList parent) … … 80 102 } 81 103 } 104 } 105 106 public JTable getTableFromOptionList(OptionList list) 107 { 108 return ((JTable)_optionListTableMap.get(list)); 82 109 } 83 110 … … 114 141 Option[] options = currentList.getOptions(); 115 142 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 117 153 //The line below is necessary as the default grid colour on mac is white, which makes the lines invisible. 118 154 propertiesTable.setGridColor(Color.BLACK); … … 148 184 149 185 _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); 150 441 } 151 442 } … … 225 516 public class CustomTableModel extends AbstractTableModel 226 517 { 227 private String[] _columnNames; 228 private Option[] _data; 518 private String[] _columnNames = null; 519 private Option[] _data = null; 520 private ImageIcon[] _images = null; 229 521 230 522 public CustomTableModel(String[] columnNames, Option[] data){ 231 523 _columnNames = columnNames; 232 524 _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 } 233 530 } 234 531 … … 246 543 247 544 public Object getValueAt(int row, int col) { 248 Option o = _data[row];249 545 if(col == 0){ 250 return o.getName();546 return _data[row].getName(); 251 547 } 252 548 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(); 254 560 } 255 561 else{ … … 264 570 265 571 public boolean isCellEditable(int row, int col) { 266 if (col < 1) { 572 if (col == 1) { 573 return true; 574 } else { 267 575 return false; 268 } else {269 return true;270 576 } 271 577 } 272 578 273 579 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 } 276 587 } 277 588 fireTableDataChanged();
Note:
See TracChangeset
for help on using the changeset viewer.