package com.c2kernel.gui.tabs; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.Enumeration; import javax.swing.*; import javax.swing.table.AbstractTableModel; import com.c2kernel.entity.proxy.ItemProxy; import com.c2kernel.gui.MainFrame; import com.c2kernel.lookup.DomainPath; import com.c2kernel.process.Gateway; import com.c2kernel.utils.Logger; /************************************************************************** * * $Revision: 1.3 $ * $Date: 2004/10/21 08:02:21 $ * * Copyright (C) 2003 CERN - European Organization for Nuclear Research * All rights reserved. **************************************************************************/ public class DomainPathAdmin extends Box implements ActionListener { ItemProxy entity; JTable table; DomainPathTableModel model; JButton addButton; JButton removeButton; public DomainPathAdmin() { super(BoxLayout.Y_AXIS); model = new DomainPathTableModel(this); table = new JTable(model); add(new JScrollPane(table)); add(Box.createVerticalGlue()); Box buttonBox = Box.createHorizontalBox(); addButton = new JButton("Add"); buttonBox.add(addButton); buttonBox.add(Box.createHorizontalGlue()); removeButton = new JButton("Remove"); buttonBox.add(removeButton); buttonBox.add(Box.createHorizontalGlue()); add(buttonBox); addButton.setActionCommand("add"); addButton.addActionListener(this); removeButton.setActionCommand("remove"); removeButton.addActionListener(this); } public void setEntity(ItemProxy entity) { this.entity = entity; model.loadPaths(); } public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("add")) { String newPath = JOptionPane.showInputDialog(this, "Enter new path,", "Add Domain Path", JOptionPane.PLAIN_MESSAGE); addDomainPath(new DomainPath(newPath)); model.loadPaths(); } else if (e.getActionCommand().equals("remove")) { if (table.getSelectedRow() > -1) { DomainPath oldPath = model.getPath(table.getSelectedRow()); removeDomainPath(oldPath); model.loadPaths(); } } } public boolean removeDomainPath(DomainPath oldPath) { return alterDomainPath(oldPath, "Remove"); } public boolean addDomainPath(DomainPath newPath) { return alterDomainPath(newPath, "Add"); } public boolean alterDomainPath(DomainPath path, String action) { if (JOptionPane.showConfirmDialog(this, action+" "+path+"?", action+" Domain Path", JOptionPane.YES_NO_OPTION) != JOptionPane.YES_OPTION) return false; String[] params = new String[1]; params[0] = path.toString(); try { MainFrame.userAgent.execute(entity, action+"DomainPath", params); } catch (Exception e) { Logger.exceptionDialog(e); return false; } return true; } private class DomainPathTableModel extends AbstractTableModel { ArrayList domPaths; DomainPathAdmin parent; public DomainPathTableModel(DomainPathAdmin parent) { this.parent = parent; domPaths = new ArrayList(); } public void loadPaths() { domPaths.clear(); for (Enumeration currentPaths = Gateway.getLDAPLookup().search(new DomainPath(), entity.getName()); currentPaths.hasMoreElements();) { DomainPath thisPath = (DomainPath)currentPaths.nextElement(); if (thisPath.getSysKey() == entity.getSystemKey()) domPaths.add(thisPath); } fireTableDataChanged(); } public DomainPath getPath(int rowIndex) { return (DomainPath)domPaths.get(rowIndex); } public void setValueAt(Object aValue, int rowIndex, int columnIndex) { DomainPath oldPath = (DomainPath)domPaths.get(rowIndex); DomainPath newPath = new DomainPath((String)aValue); boolean success = parent.addDomainPath(newPath); if (success) success = parent.removeDomainPath(oldPath); if (success) { oldPath.setPath(newPath); fireTableDataChanged(); } } public Class getColumnClass(int columnIndex) { return String.class; } public int getColumnCount() { return 1; } public String getColumnName(int column) { return "Path"; } public int getRowCount() { return domPaths.size(); } public Object getValueAt(int rowIndex, int columnIndex) { return domPaths.get(rowIndex).toString(); } public boolean isCellEditable(int rowIndex, int columnIndex) { return true; } } }