gt:             int position = ((Integer) selected.getParentKey()).intValue();
context: /***********************************************************************
   *
   * $CVSHeader$
   *
   * This file is part of WebScarab, an Open Web Application Security
   * Project utility. For details, please see http://www.owasp.org/
   *
   * Copyright (c) 2002 - 2004 Rogan Dawes
   *
   * This program is free software; you can redistribute it and/or
   * modify it under the terms of the GNU General Public License
   * as published by the Free Software Foundation; either version 2
   * of the License, or (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with this program; if not, write to the Free Software
   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
   *
   * Getting Source
   * ==============
   *
   * Source for this application is maintained at Sourceforge.net, a
   * repository for free software projects.
   * 
   * For details, please see http://www.sourceforge.net/projects/owasp
   *
   */
  
  /*
   * ObjectEditorPanel.java
   *
   * Created on 15 November 2003, 02:56
   */
  
  package org.owasp.webscarab.ui.swing.editors;
  
  import org.owasp.webscarab.util.swing.JTreeTable;
  import org.owasp.webscarab.util.swing.treetable.DefaultTreeTableModel;
  
  import javax.swing.tree.DefaultMutableTreeNode;
  import javax.swing.tree.TreeNode;
  import javax.swing.table.TableCellRenderer;
  import javax.swing.table.TableCellEditor;
  import javax.swing.JOptionPane;
  
  import java.util.Map;
  import java.util.Collection;
  import java.util.List;
  import java.util.Iterator;
  import java.util.Date;
  import java.text.DateFormat;
  
  import javax.swing.CellEditor;
  import java.awt.Component;
  
  /** Provides a Swing Component that can be used to edit and modify a fairly
   * arbitrary object structure. A Tree hierarchy is formed from Map and Collection
   * classes. All of the java.lang types are supported, and many others as well.
   * Fairly arbitrary classes can be instantiated (if they provide void constructors)
   * however, the code will need to be modified to provide renderers and editors (or
   * String-to-class convertors) in order to modify them successfully.
   * @author rdawes
   */
  public class ObjectPanel extends javax.swing.JPanel {
      
      /**
  	 * 
  	 */
  	private static final long serialVersionUID = -1149080437965386611L;
  	private JTreeTable _tt;
      private ObjectTreeTableModel _ottm;
      private boolean _editable = false;
      
      /** Creates new form ObjectPanel */
      public ObjectPanel() {
          initComponents();
          setName("Object");
          _ottm = new ObjectTreeTableModel();
          _tt = new JTreeTable(_ottm) {
              /**
  			 * 
  			 */
  			private static final long serialVersionUID = -6733180628013589307L;
  			public TableCellRenderer getCellRenderer(int row, int column) {
                  if (column == 2) {
                      Object o = ((ObjectTreeNode)getValueAt(row, 0)).getUserObject();
                      if (o != null) {
                          return getDefaultRenderer(o.getClass());
                      }
                  }
                  return super.getCellRenderer(row, column);
              }
              public TableCellEditor getCellEditor(int row, int column) {
                  if (column == 2) {
                      Object o = ((ObjectTreeNode)getValueAt(row, 0)).getUserObject();
                      if (o != null) {
                          return getDefaultEditor(o.getClass());
                      }
                  }
                  return super.getCellEditor(row, column);
              }
          };
          _tt.setDefaultRenderer(Date.class, new DateRenderer());
          ArrayRenderer ar = new ArrayRenderer();
          _tt.setDefaultRenderer(byte[].class, ar);
          
          _tt.setDefaultEditor(Date.class, new DateEditor());
          _tt.setCellSelectionEnabled(true);
          ttScrollPane.setViewportView(_tt);
          setEditable(false);
      }
      
      /** Supplies the object to be edited
       * @param object the object to be edited/displayed
       */    
      public void setObject(Object object) {
          _ottm.setObject(object);
      }
      
      private void stopEditing() {
          Component comp = _tt.getEditorComponent();
          if (comp != null && comp instanceof CellEditor) {
              ((CellEditor) comp).stopCellEditing();
          }
      }
      
      /** Returns the object that is currently being displayed
       * @return The object
       */    
      public Object getObject() {
          if (_editable) stopEditing();
          return _ottm.getObject();
      }
      
      /** Allows one to specify whether the object may be edited or not.
       * @param editable whether to allow editing
       */    
      public void setEditable(boolean editable) {
          _editable = editable;
          _ottm.setEditable(editable);
          insertButton.setVisible(editable);
          childButton.setVisible(editable);
          deleteButton.setVisible(editable);
          revalidate();
          repaint();
      }
      
      public boolean isModified() {
          if (_editable) stopEditing();
          return _editable && _ottm.isModified();
      }
      
      /** This method is called from within the constructor to
       * initialize the form.
       * WARNING: Do NOT modify this code. The content of this method is
       * always regenerated by the Form Editor.
       */
      private void initComponents() {//GEN-BEGIN:initComponents
          java.awt.GridBagConstraints gridBagConstraints;
  
          ttScrollPane = new javax.swing.JScrollPane();
          insertButton = new javax.swing.JButton();
          childButton = new javax.swing.JButton();
          deleteButton = new javax.swing.JButton();
  
          setLayout(new java.awt.GridBagLayout());
  
          ttScrollPane.setMinimumSize(new java.awt.Dimension(300, 200));
          ttScrollPane.setPreferredSize(new java.awt.Dimension(300, 200));
          gridBagConstraints = new java.awt.GridBagConstraints();
          gridBagConstraints.gridheight = 3;
          gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
          gridBagConstraints.weightx = 1.0;
          gridBagConstraints.weighty = 1.0;
          add(ttScrollPane, gridBagConstraints);
  
          insertButton.setText("Insert");
          insertButton.addActionListener(new java.awt.event.ActionListener() {
              public void actionPerformed(java.awt.event.ActionEvent evt) {
                  insertButtonActionPerformed(evt);
              }
          });
  
          gridBagConstraints = new java.awt.GridBagConstraints();
          gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
          gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTH;
          gridBagConstraints.weighty = 1.0;
          add(insertButton, gridBagConstraints);
  
          childButton.setText("New Child");
          childButton.addActionListener(new java.awt.event.ActionListener() {
              public void actionPerformed(java.awt.event.ActionEvent evt) {
                  childButtonActionPerformed(evt);
              }
          });
  
          gridBagConstraints = new java.awt.GridBagConstraints();
          gridBagConstraints.gridx = 1;
          gridBagConstraints.gridy = 1;
          gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
          add(childButton, gridBagConstraints);
  
          deleteButton.setText("Delete");
          deleteButton.addActionListener(new java.awt.event.ActionListener() {
              public void actionPerformed(java.awt.event.ActionEvent evt) {
                  deleteButtonActionPerformed(evt);
              }
          });
  
          gridBagConstraints = new java.awt.GridBagConstraints();
          gridBagConstraints.gridx = 1;
          gridBagConstraints.gridy = 2;
          gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
          gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
          gridBagConstraints.weighty = 1.0;
          add(deleteButton, gridBagConstraints);
  
      }//GEN-END:initComponents
  
      private void childButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_childButtonActionPerformed
          javax.swing.tree.TreePath path = _tt.getTree().getSelectionPath();
          if (path == null) {
              JOptionPane.showMessageDialog(null, "Please select a row", "No selection", JOptionPane.ERROR_MESSAGE);
              return;
          }
          ObjectTreeNode selected = (ObjectTreeNode) path.getLastPathComponent();
          Object userObject = selected.getUserObject();
          if (userObject instanceof Map) {
              Object key = JOptionPane.showInputDialog("Please input a key value");
              if (key == null) return;
              @SuppressWarnings("unchecked")
  			Map<Object, ?> map = (Map<Object, ?>) userObject;
              if (map.containsKey(key)) {
                  JOptionPane.showMessageDialog(null, "The Map already contains " + key, "Key exists", JOptionPane.ERROR_MESSAGE);
                  return;
              }
              map.put(key, null);
              Iterator<Object> it = map.keySet().iterator();
              int position = 0;
              while (it.hasNext() && it.next() != key) {
                  position++;
              }
              ObjectTreeNode newNode = new ObjectTreeNode(null);
              newNode.setParentKey(key);
              selected.insert(newNode, position);
              _ottm.nodesWereInserted(selected, new int[] {position});
          } else if (userObject instanceof List) {
              List<?> list = (List<?>) userObject;
              int position = list.size();
              list.add(position, null);
              ObjectTreeNode newNode = new ObjectTreeNode(null);
              newNode.setParentKey(new Integer(position));
              selected.insert(newNode, position);
              _ottm.nodesWereInserted(selected, new int[] {position});
          }
      }//GEN-LAST:event_childButtonActionPerformed
  
      private void insertButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_insertButtonActionPerformed
          javax.swing.tree.TreePath path = _tt.getTree().getSelectionPath();
          if (path == null) {
              JOptionPane.showMessageDialog(null, "Please select a row", "No selection", JOptionPane.ERROR_MESSAGE);
              return;
          }
          ObjectTreeNode selected = (ObjectTreeNode) path.getLastPathComponent();
          ObjectTreeNode parent = (ObjectTreeNode) selected.getParent();
          if (parent == null) {
              JOptionPane.showMessageDialog(null, "It is not possible to insert a node at the root", "Error", JOptionPane.ERROR_MESSAGE);
              return;
          }
          Object parentObject = parent.getUserObject();
          if (parentObject instanceof List) {
              List<?> list = (List<?>) parentObject;
  