File:  [Repository] / FM2SQL / Attic / TableComponent.java
Revision 1.5: download - view: text, annotated - select for diffs - revision graph
Tue Mar 23 11:57:06 2004 UTC (20 years, 3 months ago) by rogo
Branches: MAIN
CVS tags: HEAD
GPL Disclaimer

    1: /*
    2:  * TableComponent.java -- GUI Table class
    3:  * Filemake to SQL Converter 
    4:  * Copyright (C) 2004 Robert Gordesch (rogo@mpiwg-berlin.mpg.de) 
    5:  * This program is free software; you can redistribute it and/or modify it
    6:  * under the terms of the GNU General Public License as published by the Free
    7:  * Software Foundation; either version 2 of the License, or (at your option)
    8:  * any later version.  Please read license.txt for the full details. A copy of
    9:  * the GPL may be found at http://www.gnu.org/copyleft/lgpl.html  You should
   10:  * have received a copy of the GNU General Public License along with this
   11:  * program; if not, write to the Free Software Foundation, Inc., 59 Temple
   12:  * Place, Suite 330, Boston, MA 02111-1307 USA  Created on 15.09.2003 by
   13:  * rogo  
   14:  */
   15: import javax.swing.*;
   16: import java.awt.event.*;
   17: 
   18: import java.util.*;
   19: import java.awt.*;
   20: import javax.swing.table.*;
   21: import javax.swing.event.*;
   22: /**
   23:  Table component 
   24: */
   25: 
   26: public class TableComponent extends JPanel implements TableModelListener
   27: {
   28: 
   29:   JScrollPane tableScroller;
   30:   JTable table;
   31:   DefaultTableModel tableModel;
   32:   JPanel content;
   33:   int row, col;
   34:   int realCount = 0;
   35:   String columnNames[] = { "no database", "connection" };
   36:   String[][] rowData = { { "", "" }
   37:   };
   38:   boolean callState = false;
   39:   Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
   40:   static JLabel label;
   41:   int selIndex = 0;
   42:   Icon trueIcon = null, falseIcon = null, empty = null;
   43:   TableComponent tableComp = this;
   44:   /**
   45:    * Constructs an  empty table with default header and  dummy data
   46:    */
   47: 
   48:   public TableComponent()
   49:   {
   50: 
   51:     try
   52:     {
   53:       trueIcon = new ImageIcon(getClass().getResource("icons/ok.gif"));
   54: 
   55:       falseIcon = new Icon()
   56:       {
   57:         public int getIconHeight()
   58:         {
   59:           return 23;
   60:         }
   61:         public int getIconWidth()
   62:         {
   63:           return 27;
   64:         }
   65:         public void paintIcon(Component c, Graphics g, int x, int y)
   66:         {
   67:           Graphics2D g2 = (Graphics2D) g;
   68:           g2.setColor(Color.blue);
   69:           g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
   70:           g2.setStroke(new BasicStroke(2.0f));
   71:           int xr = x + 20, yr = y + 15;
   72:           int xl = x + 5, yl = y + 5;
   73:           g2.drawLine(xl, yl, xr, yr);
   74:           g2.drawLine(xl, yr, xr, yl);
   75: 
   76:         }
   77:       };
   78:       empty = new Icon()
   79:       {
   80:         public int getIconHeight()
   81:         {
   82:           return 23;
   83:         }
   84:         public int getIconWidth()
   85:         {
   86:           return 27;
   87:         }
   88:         public void paintIcon(Component c, Graphics g, int x, int y)
   89:         {
   90: 
   91:         }
   92:       };
   93: 
   94:     } catch (Exception e)
   95:     {
   96:       System.out.println("Error while loading ok.gif");
   97:     }
   98: 
   99:     content = this;
  100:     tableModel = new DefaultTableModel(rowData, columnNames)
  101:     {
  102:       boolean firstCall = true;
  103:       public Class getColumnClass(int column)
  104:       {
  105:         if (row > dataVector.size())
  106:           row = dataVector.size() - 1;
  107:         Vector rowVector = (Vector) dataVector.elementAt(row);
  108:         if (rowVector.isEmpty())
  109:           return Object.class;
  110:         if (rowVector.elementAt(column) == null)
  111:           return Object.class;
  112:         else
  113:           return rowVector.elementAt(column).getClass();
  114: 
  115:       }
  116:       public Object getValueAt(int i, int j)
  117:       {
  118:         if (((Vector) dataVector.elementAt(i)).isEmpty())
  119:           return null;
  120:         else
  121:           return super.getValueAt(i, j);
  122:       }
  123:       public void setDataVector(Vector rows, Vector columnNames)
  124:       {
  125:         // System.out.println(firstCall);
  126: 
  127:         if (firstCall)
  128:         {
  129:           realCount = rows.size();
  130:           //System.out.println(realCount);
  131:         } else
  132:           firstCall = true;
  133:         super.setDataVector(rows, columnNames);
  134: 
  135:         //realCount =(rows==null) ? 0:rows.size();
  136:       }
  137:     };
  138: 
  139:     table = new JTable(tableModel)
  140:     {
  141:       public TableCellRenderer getCellRenderer(int row, int column)
  142:       {
  143: 
  144:         tableComp.row = row;
  145:         TableCellRenderer renderer = null;
  146:         if (renderer == null)
  147:         {
  148:           renderer = getDefaultRenderer(getColumnClass(column));
  149:         }
  150:         return renderer;
  151:       }
  152:       public TableCellEditor getCellEditor(int row, int column)
  153:       {
  154:         tableComp.row = row;
  155: 
  156:         TableCellEditor editor = null;
  157: 
  158:         if (editor == null)
  159:         {
  160:           editor = getDefaultEditor(getColumnClass(column));
  161:         }
  162:         return editor;
  163:       }
  164:       public boolean isCellEditable(int row, int column)
  165:       {
  166:         tableComp.row = row;
  167:         boolean value = false;
  168:         if (getColumnClass(column) == ArrayList.class || getColumnClass(column) == JComboBox.class || getColumnClass(column) == SQLCommand.class || getColumnClass(column) == IDComboBox.class)
  169:           value = true;
  170:         //System.out.println("tried to edit at row "+row+" column "+column+" "+value); 
  171: 
  172:         return value;
  173:       }
  174: 
  175:       /* public boolean editCellAt(int row, int column, EventObject e){
  176:        System.out.println(getCellEditor());
  177:        if(isEditing()) editingStopped(null);
  178:        return super.editCellAt(row,col,e);
  179:        }*/
  180:     };
  181: 
  182:     table.getColumnModel().getColumn(0).setPreferredWidth(200);
  183:     table.getColumnModel().getColumn(1).setPreferredWidth(50);
  184:     row = tableModel.getRowCount();
  185:     col = tableModel.getColumnCount();
  186:     ((DefaultTableModel) table.getModel()).setRowCount(10);
  187: 
  188:     tableScroller = new JScrollPane(table);
  189:     Dimension d = table.getPreferredSize();
  190:     tableScroller.setPreferredSize(new Dimension(d.width, d.height + 9));
  191: 
  192:     content.add(tableScroller);
  193:     ((DefaultTableModel) table.getModel()).setRowCount(74);
  194: 
  195:     tableModel.addTableModelListener(this);
  196:     setLayout(new FlowLayout(FlowLayout.LEFT));
  197:     //this.add(content);
  198: 
  199:     table.setDefaultEditor(JComboBox.class, new VectorEditor());
  200:     table.setDefaultRenderer(JComboBox.class, new VectorCellRenderer());
  201: 
  202:     table.setDefaultEditor(ArrayList.class, new ArrayListEditor());
  203:     table.setDefaultRenderer(ArrayList.class, new ArrayListCellRenderer());
  204:     /*	table.setDefaultEditor(SQLCommand.class,new DefaultCellEditor(new JTextField()) {
  205:     	public Object getCellEditorValue()
  206:     	 {
  207:     	   return new SQLCommand(super.getCellEditorValue().toString());
  208:     	 }
  209:     	});*/
  210:     table.setDefaultEditor(SQLCommand.class, new SQLCommandEditor());
  211: 
  212:     table.setDefaultRenderer(Boolean.class, new TableCellRenderer()
  213:     {
  214:       JCheckBox checkBox = new JCheckBox();
  215:       JLabel label = new JLabel();
  216:       public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
  217:       {
  218:         if (value instanceof Integer)
  219:         {
  220:           label.setText(value.toString());
  221:           label.setHorizontalAlignment(JLabel.RIGHT);
  222: 
  223:           return label;
  224:         } else if (value instanceof String)
  225:         {
  226:           label.setText(value.toString());
  227:           label.setHorizontalAlignment(JLabel.CENTER);
  228:           return label;
  229:         }
  230:         checkBox.setHorizontalAlignment(JLabel.CENTER);
  231:         checkBox.setVerticalAlignment(JLabel.CENTER);
  232: 
  233:         checkBox.setSelectedIcon(trueIcon);
  234:         if (value == null)
  235:           checkBox.setIcon(empty);
  236:         else
  237:           checkBox.setIcon(falseIcon);
  238:         checkBox.setForeground(table.getForeground());
  239:         checkBox.setBackground(table.getBackground());
  240: 
  241:         checkBox.setSelected((value != null && ((Boolean) value).booleanValue()));
  242:         return checkBox;
  243: 
  244:       }
  245: 
  246:     });
  247: 
  248:     table.setRowHeight(20);
  249: 
  250:   }
  251: 
  252:   /**
  253:    *  Fills  the  table  with the array of Objects
  254:    *@param array of Objects 
  255:    * 
  256:    */
  257: 
  258:   public void setData(Object[][] data)
  259:   {
  260:     callState = true;
  261:     clear();
  262:     for (int i = 0; i < tableModel.getColumnCount(); ++i)
  263:     {
  264:       for (int j = 0; j < data[0].length; ++j)
  265:       {
  266:         tableModel.setValueAt(data[i][j], j, i);
  267:       }
  268:     }
  269:     callState = false;
  270:     //tableModel.setDataVector(data,columnNames);
  271:     //tableModel.setRowCount(100);
  272: 
  273:   } // to method
  274:   public void clear()
  275:   {
  276:     callState = true;
  277:     for (int i = 0; i < tableModel.getColumnCount(); ++i)
  278:     {
  279:       for (int j = 0; j < tableModel.getRowCount(); ++j)
  280:       {
  281:         tableModel.setValueAt("", j, i);
  282:       }
  283:     }
  284:     //tableModel.setDataVector(rowData,columnNames);
  285:     //tableModel.setRowCount(100);
  286:   }
  287:   public void disable(boolean state)
  288:   {
  289:     if (state)
  290:     {
  291:       table.setEnabled(state);
  292:     } else
  293:     {
  294:       clear();
  295:       table.setEnabled(state);
  296: 
  297:     }
  298: 
  299:   }
  300: 
  301:   public void setEnabled(boolean state)
  302:   {
  303:     table.setEnabled(state);
  304:   }
  305: 
  306:   public void tableChanged(TableModelEvent e)
  307:   {
  308:   }
  309:   public int getMaxHeaderWidth()
  310:   {
  311:     TableColumnModel model = table.getColumnModel();
  312:     int maxWidth = -10000000;
  313:     for (int i = 0; i < model.getColumnCount(); ++i)
  314:     {
  315:       label = new JLabel(model.getColumn(i).getHeaderValue().toString());
  316:       int width = label.getPreferredSize().width + 20;
  317:       if (width > maxWidth)
  318:         maxWidth = width;
  319:     }
  320:     return maxWidth;
  321:   }
  322:   public void enlarge(int windowWidth, int windowHeight)
  323:   {
  324: 
  325:     //  Class tre=table.getColumnModel().getColumn(1).getHeaderValue().getClass();
  326:     //Graphics2D g2=((Graphics2D)getGraphics());
  327:     table.setAutoResizeMode(table.AUTO_RESIZE_OFF);
  328: 
  329:     TableColumnModel model = table.getColumnModel();
  330:     for (int i = 0; i < model.getColumnCount(); ++i)
  331:     {
  332:       label = new JLabel(model.getColumn(i).getHeaderValue().toString());
  333:       model.getColumn(i).setPreferredWidth(label.getPreferredSize().width + 50);
  334: 
  335:     }
  336:     Dimension d = table.getPreferredSize();
  337:     Dimension d2 = tableScroller.getPreferredSize();
  338:     tableScroller.setPreferredSize(new Dimension((d.width >= screenSize.width) ? (screenSize.width - 50) : (d.width), d2.height));
  339:     // tableModel.setRowCount(80);
  340:     if (d.width < windowWidth)
  341:       model.getColumn(model.getColumnCount() - 1).setPreferredWidth((windowWidth - d.width) + model.getColumn(model.getColumnCount() - 1).getPreferredWidth());
  342: 
  343:     table.setGridColor(Color.red);
  344:     d2 = tableScroller.getPreferredSize();
  345:     table.validate();
  346:     content.setPreferredSize(new Dimension(d2.width, d2.height + 10));
  347:     content.validate();
  348:   }
  349:   public void sizeToFit(int windowWidth, int windowHeight)
  350:   {
  351:     //table.setAutoResizeMode(table.AUTO_RESIZE_LAST_COLUMN);
  352: 
  353:     TableColumnModel model = table.getColumnModel();
  354:     Dimension d2 = getPreferredSize();
  355: 
  356:     int columnWidth = (windowWidth) / model.getColumnCount();
  357:     for (int i = 0; i < model.getColumnCount(); ++i)
  358:     {
  359:       model.getColumn(i).setPreferredWidth(columnWidth);
  360:       //model.getColumn (i).setWidth(columnWidth);
  361:     }
  362:     // System.out.println(table.getPreferredSize()+" "+model.getTotalColumnWidth()+" "+columnWidth);
  363:     /* Dimension d = table.getPreferredSize();
  364:      d2=getPreferredSize();
  365:     
  366:     
  367:      System.out.println("window width"+windowWidth+" width "+d2.width+" "+model.getTotalColumnWidth());
  368:     //  d2=getPreferredSize();
  369:      tableScroller.setPreferredSize(new Dimension( windowWidth, d.height));
  370:     
  371:     
  372:     // System.out.println("window width"+windowWidth+" "+windowHeight+" "+getWidth()+" "+getHeight());
  373:     
  374:      table.setAutoResizeMode(table.AUTO_RESIZE_OFF);
  375:     */
  376:     Dimension d = table.getPreferredSize();
  377:     if (columnWidth < getMaxHeaderWidth() || d.width > screenSize.width)
  378:       enlarge(windowWidth, windowHeight);
  379:     table.validate();
  380:     table.setGridColor(Color.red);
  381: 
  382:   }
  383:   class VectorEditor implements TableCellEditor, ActionListener
  384:   {
  385:     JComboBox box = new JComboBox();
  386:     DefaultComboBoxModel model = new DefaultComboBoxModel();
  387:     JComboBox boxValue;
  388:     CellEditorListener listener = table;
  389:     int row, column;
  390:     public VectorEditor()
  391:     {
  392:       //     super(new JCheckBox());
  393: 
  394:       box.addActionListener(this);
  395:       /*	 	box.addFocusListener(new FocusAdapter() {
  396:       	 	public void focusLost(FocusEvent e)
  397:       	 	{
  398:       			 VectorEditor.this.listener.editingStopped(new ChangeEvent(VectorEditor.this));
  399:       		 	System.out.println("lost focus");
  400:       	 	}
  401:       	 	});*/
  402:     }
  403:     public Object getCellEditorValue()
  404:     {
  405:       //selIndex = box.getSelectedIndex();
  406:       System.out.println("Called " + selIndex);
  407:       //tableModel.fireTableCellUpdated(row,column);
  408:       if (box.getModel().getSize() > 0)
  409:         return boxValue;
  410:       else
  411:         return null;
  412: 
  413:     }
  414: 
  415:     public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)
  416:     {
  417:       boxValue = (value != null) ? (JComboBox) value : null;
  418:       this.row = row;
  419:       this.column = column;
  420:       if (boxValue != null)
  421:         box.setModel(boxValue.getModel());
  422:       else
  423:         return null;
  424:       return box;
  425:     }
  426: 
  427:     public boolean isCellEditable(EventObject anEvent)
  428:     {
  429:       if (table.isEditing())
  430:       {
  431: 
  432:         //     System.out.println(getCellEditorValue());
  433:         if (listener != null)
  434:           listener.editingStopped(new ChangeEvent(this));
  435: 
  436:         //   listener = null;
  437:         //  Object local =getCellEditorValue();
  438:         //table.getModel().setValueAt(local, table.getEditingRow(), table.getEditingColumn());
  439:         //  table.remove(label);   
  440:         //  table.validate();
  441:         //System.out.println("before "+table.getComponentCount());
  442:         //  if(table.getEditor)
  443:         // table.editCellAt(row,column);
  444:         // table.removeAll();
  445:         //return null;
  446:       }
  447: 
  448:       //System.out.println(anEvent);
  449:       return true;
  450:     }
  451: 
  452:     public boolean shouldSelectCell(EventObject anEvent)
  453:     {
  454:       //System.out.println(anEvent);
  455:       return true;
  456:     }
  457: 
  458:     public boolean stopCellEditing()
  459:     {
  460:       return true;
  461:     }
  462: 
  463:     public void cancelCellEditing()
  464:     {
  465:       listener.editingStopped(new ChangeEvent(this));
  466:     }
  467: 
  468:     public void addCellEditorListener(CellEditorListener l)
  469:     {
  470:       listener = l;
  471:       //System.out.println(l);
  472:     }
  473: 
  474:     public void removeCellEditorListener(CellEditorListener l)
  475:     {
  476:       //System.out.println("removed listener");
  477: 
  478:     }
  479:     /* (non-Javadoc)
  480:      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
  481:      */
  482:     public void actionPerformed(ActionEvent e)
  483:     {
  484:       // System.out.println(e.getActionCommand());
  485:       if (table.isEditing())
  486:         if (listener != null)
  487:           listener.editingStopped(new ChangeEvent(this));
  488:     }
  489: 
  490:   }
  491:   class SQLCommandEditor implements TableCellEditor
  492:   {
  493:     JTextArea editor = new JTextArea();
  494:     JScrollPane pane = new JScrollPane(editor);
  495:     JDialog window;
  496:     CellEditorListener listener = table;
  497:     JLabel label = new JLabel();
  498:     int x = 0, y = 0;
  499:     String copy = "";
  500:     boolean removeCalled = true;
  501:     public SQLCommandEditor()
  502:     {
  503:       //     super(new JCheckBox());
  504:       //this.list = list;
  505:       editor.getDocument().addDocumentListener(new DocumentListener()
  506:       {
  507: 
  508:         public void insertUpdate(DocumentEvent e)
  509:         {
  510:           //System.out.println(editor.getText().charAt( e.getOffset()));
  511:           copy = editor.getText();
  512:           if (copy.charAt(e.getOffset()) == '\n')
  513:           {
  514:             window.setSize(window.getWidth(), window.getHeight() + 20);
  515:             window.validate();
  516:           }
  517:         }
  518: 
  519:         public void removeUpdate(DocumentEvent e)
  520:         {
  521:           if (copy.charAt(e.getOffset()) == '\n')
  522:           {
  523:             window.setSize(window.getWidth(), window.getHeight() - 20);
  524:             window.validate();
  525:             copy = editor.getText();
  526:           }
  527: 
  528:         }
  529: 
  530:         public void changedUpdate(DocumentEvent e)
  531:         {
  532:         }
  533: 
  534:       });
  535:       table.addMouseListener(new MouseAdapter()
  536:       {
  537:         public void mouseEntered(MouseEvent e)
  538:         {
  539:           if (true)
  540:             return;
  541:           //System.out.println(e);
  542:           x = e.getX();
  543:           y = e.getY();
  544: 
  545:           int row = table.rowAtPoint(new Point(e.getX(), e.getY()));
  546:           int col = table.columnAtPoint(new Point(e.getX(), e.getY()));
  547: 
  548:           if (window != null)
  549:           {
  550:             if (!window.isVisible())
  551:               return;
  552: 
  553:           } else
  554:             return;
  555:           if (row != table.getEditingRow() || col != table.getEditingColumn())
  556:             if (listener != null)
  557:             {
  558:               listener.editingStopped(new ChangeEvent(this));
  559:               listener = null;
  560:             }
  561:         }
  562:         public void mousePressed(MouseEvent e)
  563:         {
  564: 
  565:           // System.out.println(e);
  566:           x = e.getX();
  567:           y = e.getY();
  568: 
  569:           if (window != null)
  570:             if (!window.isVisible())
  571:               return;
  572: 
  573:           int row = table.rowAtPoint(new Point(x, y));
  574:           int col = table.columnAtPoint(new Point(x, y));
  575: 
  576:           if (row != table.getEditingRow() || col != table.getEditingColumn())
  577:             if (listener != null)
  578:             {
  579:               listener.editingStopped(new ChangeEvent(this));
  580:               listener = null;
  581:             }
  582:         }
  583: 
  584:         public void mouseExited(MouseEvent e)
  585:         {
  586:           //  System.out.println(e);
  587: 
  588:         }
  589:       });
  590:     }
  591:     public Object getCellEditorValue()
  592:     {
  593:       window.setVisible(false);
  594:       window.dispose();
  595:       //table.repaint();
  596:       return new SQLCommand(editor.getText());
  597:     }
  598: 
  599:     public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)
  600:     {
  601:       // checks if editor was finished if not remove old editor
  602:       //this.row=row;
  603:       SQLCommand command = (value != null) ? (SQLCommand) value : null;
  604:       editor.setText(command.toString());
  605:       System.out.println("editor size " + editor.getPreferredSize());
  606:       pane.setBorder(new javax.swing.border.LineBorder(Color.red));
  607: 
  608:       if (window == null)
  609:       {
  610:         window = new JDialog(FM2SQL.fmInstance);
  611:         window.getContentPane().add(pane);
  612:         window.addWindowListener(new WindowAdapter()
  613:         {
  614:           public void windowClosing(WindowEvent e)
  615:           {
  616:             if (listener != null)
  617:             {
  618:               listener.editingStopped(new ChangeEvent(this));
  619:               listener = null;
  620:             }
  621: 
  622:           }
  623:         });
  624:       }
  625:       Point p = table.getLocationOnScreen();
  626:       //      if(label.isShowing())
  627:       //      System.out.println( label.getLocationOnScreen());
  628: 
  629:       Dimension dimEditor = editor.getPreferredSize();
  630:       if (dimEditor.width < 260)
  631:         dimEditor.width = 260;
  632:       window.setSize((dimEditor.width < 280) ? dimEditor.width + 20 : 280, (dimEditor.height > 50) ? 200 : 70);
  633:       TableColumnModel model = table.getColumnModel();
  634:       int offset_x = 0, offset_y = 0;
  635:       for (int i = 0; i <= column; ++i)
  636:       {
  637:         if (i == column)
  638:           offset_x -= (window.getWidth() - model.getColumn(i).getWidth()) / 2;
  639:         else
  640:           offset_x += model.getColumn(i).getWidth();
  641: 
  642:       }
  643:       for (int i = 0; i < row; ++i)
  644:       {
  645:         offset_y += table.getRowHeight(row);
  646:       }
  647:       // System.out.println(table.getCellRect(row, column, false));
  648:       window.setTitle("Editing row " + row + " column " + column);
  649:       window.setLocation(p.x + offset_x, p.y + offset_y);
  650:       window.setVisible(true);
  651:       System.out.println("row " + row + " col " + column + " location" + window.getLocation());
  652:       // window.setVisible(true);
  653: 
  654:       //label=new JLabel(command.toString());
  655:       label.setText(command.toString());
  656:       label.setForeground(Color.red);
  657:       return label;
  658:     }
  659: 
  660:     public boolean isCellEditable(EventObject anEvent)
  661:     {
  662:       if (table.isEditing())
  663:       {
  664: 
  665:         if (listener == null)
  666:           listener = table;
  667:         if (listener != null)
  668:           listener.editingStopped(new ChangeEvent(this));
  669:       }
  670: 
  671:       return true;
  672:     }
  673: 
  674:     public boolean shouldSelectCell(EventObject anEvent)
  675:     {
  676: 
  677:       return true;
  678:     }
  679: 
  680:     public boolean stopCellEditing()
  681:     {
  682:       return true;
  683:     }
  684: 
  685:     public void cancelCellEditing()
  686:     {
  687:       System.out.println("cancel was called");
  688:     }
  689: 
  690:     public void addCellEditorListener(CellEditorListener l)
  691:     {
  692:       removeCalled = false;
  693:       listener = l;
  694:     }
  695: 
  696:     public void removeCellEditorListener(CellEditorListener l)
  697:     {
  698:       //if(listener!=null)
  699:       removeCalled = true;
  700:       //table.removeAll();
  701: 
  702:       System.out.println("remove was called");
  703:     }
  704: 
  705:   }
  706: 
  707:   class ArrayListEditor implements TableCellEditor
  708:   {
  709:     JList list = new JList();
  710:     ArrayList vec = new ArrayList();
  711:     DefaultListModel model = new DefaultListModel();
  712:     JScrollPane pane = new JScrollPane(list);
  713:     JWindow window;
  714:     CellEditorListener listener = table;
  715:     int row, x, y;
  716:     public ArrayListEditor()
  717:     {
  718:       //     super(new JCheckBox());
  719:       //this.list = list;
  720:       table.addMouseListener(new MouseAdapter()
  721:       {
  722:         public void mouseEntered(MouseEvent e)
  723:         {
  724:           // if(true) return;
  725:           //System.out.println(e);
  726:           x = e.getX();
  727:           y = e.getY();
  728: 
  729:           int row = table.rowAtPoint(new Point(e.getX(), e.getY()));
  730:           int col = table.columnAtPoint(new Point(e.getX(), e.getY()));
  731: 
  732:           if (window != null)
  733:           {
  734:             if (!window.isVisible())
  735:               return;
  736: 
  737:           } else
  738:             return;
  739: 
  740:           if (row != table.getEditingRow() || col != table.getEditingColumn())
  741:             if (listener != null)
  742:             {
  743:               listener.editingStopped(new ChangeEvent(this));
  744:               //listener = null;
  745:             }
  746:         }
  747:         public void mousePressed(MouseEvent e)
  748:         {
  749: 
  750:           // System.out.println(e);
  751:           x = e.getX();
  752:           y = e.getY();
  753: 
  754:           if (window != null)
  755:             if (!window.isVisible())
  756:               return;
  757: 
  758:           int row = table.rowAtPoint(new Point(x, y));
  759:           int col = table.columnAtPoint(new Point(x, y));
  760: 
  761:           if (row != table.getEditingRow() || col != table.getEditingColumn())
  762:             if (listener != null)
  763:             {
  764:               listener.editingStopped(new ChangeEvent(this));
  765:               //listener = null;
  766:             }
  767:         }
  768: 
  769:         public void mouseExited(MouseEvent e)
  770:         {
  771:           //  System.out.println(e);
  772: 
  773:         }
  774:       });
  775: 
  776:       pane.addFocusListener(new FocusAdapter()
  777:       {
  778:         public void focusLost(FocusEvent e)
  779:         {
  780:           listener.editingStopped(new ChangeEvent(this));
  781:           //		table.setRowHeight(row,20);
  782:           System.out.println("lost focus");
  783:         }
  784: 
  785:       });
  786:       pane.addMouseListener(new MouseAdapter()
  787:       {
  788:         public void mouseExited(MouseEvent e)
  789:         {
  790:           //  System.out.println(e);
  791:           listener.editingStopped(new ChangeEvent(this));
  792:         }
  793:       });
  794:     }
  795:     public Object getCellEditorValue()
  796:     {
  797:       selIndex = list.getSelectedIndex();
  798:       window.setVisible(false);
  799:       window.dispose();
  800:       //	table.setRowHeight(row,20);
  801: 
  802:       return vec;
  803:     }
  804: 
  805:     public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)
  806:     {
  807:       this.row = row;
  808:       vec = (value != null) ? (ArrayList) value : new ArrayList();
  809:       model.clear();
  810:       for (int i = 0; i < vec.size(); ++i)
  811:         model.addElement(vec.get(i));
  812:       list.setModel(model);
  813:       list.setModel(model);
  814:       pane.setBorder(new javax.swing.border.LineBorder(Color.red));
  815:       //if(!isSelected)
  816:       //table.setRowHeight(row,80);
  817:       if (window == null)
  818:       {
  819:         window = new JWindow(FM2SQL.fmInstance);
  820:         window.getContentPane().add(pane);
  821:       }
  822:       Point p = table.getLocationOnScreen();
  823:       //      if(label.isShowing())
  824:       //      System.out.println( label.getLocationOnScreen());
  825: 
  826:       Dimension dimEditor = pane.getPreferredSize();
  827:       window.setSize((dimEditor.width < 250) ? dimEditor.width + 20 : 250, (dimEditor.height > 50) ? 200 : 50);
  828:       TableColumnModel model = table.getColumnModel();
  829:       int offset_x = 0, offset_y = 0;
  830:       for (int i = 0; i <= column; ++i)
  831:       {
  832:         if (i == column)
  833:           offset_x -= (window.getWidth() - model.getColumn(i).getWidth()) / 2;
  834:         else
  835:           offset_x += model.getColumn(i).getWidth();
  836: 
  837:       }
  838:       for (int i = 0; i < row; ++i)
  839:       {
  840:         offset_y += table.getRowHeight(row);
  841:       }
  842:       window.setVisible(true);
  843:       // System.out.println(table.getCellRect(row, column, false));
  844: 
  845:       window.setLocation(p.x + offset_x, p.y + offset_y);
  846:       // System.out.println("row "+row+" col "+column+" location"+ window.getBounds());
  847:       // window.setVisible(true);
  848:       //label=new JLabel(command.toString());
  849:       label.setText("editing");
  850:       label.setForeground(Color.red);
  851:       return label;
  852: 
  853:       // return pane;
  854:     }
  855: 
  856:     public boolean isCellEditable(EventObject anEvent)
  857:     {
  858:       if (table.isEditing())
  859:       {
  860:         System.out.println("Editing is in progress");
  861: 
  862:         // if (listener == null)
  863:         // listener = table;
  864:         if (listener != null)
  865:           listener.editingStopped(new ChangeEvent(this));
  866: 
  867:       }
  868:       return true;
  869:     }
  870: 
  871:     public boolean shouldSelectCell(EventObject anEvent)
  872:     {
  873:       return true;
  874:     }
  875: 
  876:     public boolean stopCellEditing()
  877:     {
  878:       return true;
  879:     }
  880: 
  881:     public void cancelCellEditing()
  882:     {
  883:     }
  884: 
  885:     public void addCellEditorListener(CellEditorListener l)
  886:     {
  887:       listener = l;
  888:     }
  889: 
  890:     public void removeCellEditorListener(CellEditorListener l)
  891:     {
  892:       System.out.println(l);
  893:     }
  894: 
  895:   }
  896:   class VectorCellRenderer implements TableCellRenderer
  897:   {
  898:     JList list = new JList();
  899:     JComboBox box;
  900:     JLabel label = new JLabel();
  901:     DefaultListModel model = new DefaultListModel();
  902:     JScrollPane listScroller = new JScrollPane(list);
  903:     Color noIDColor=new Color(50,50,200);
  904:     
  905:     public VectorCellRenderer()
  906:     {
  907:       //list = new JList();
  908:       // vec = new Vector();
  909:       label.setForeground(Color.blue);
  910:     }
  911: 
  912:     public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
  913:     {
  914:       box = (JComboBox) value;
  915:       if (box == null)
  916:         return null; //label.setText("");
  917:       else if (box.getSelectedItem() != null)
  918:       {
  919:         String text = box.getSelectedItem().toString();
  920:         if (text == "")
  921:         {
  922:           if (box instanceof IDComboBox)
  923:           {
  924:             text = "\"no  ID\"";
  925:             label.setForeground(noIDColor);
  926:           } else
  927:           {
  928:             text = "\"no Layout\"";
  929:             label.setForeground(Color.red);
  930:           }
  931:         } else
  932:         {
  933:           if (box instanceof IDComboBox)
  934:             label.setForeground(Color.darkGray);
  935: 
  936:           else
  937:             label.setForeground(Color.blue);
  938:         }
  939:         label.setText(text);
  940: 
  941:       } else
  942:         label.setText("");
  943:       return label;
  944:     }
  945: 
  946:   }
  947:   class ArrayListCellRenderer implements TableCellRenderer
  948:   {
  949:     JList list = new JList();
  950:     ArrayList vec;
  951:     JLabel label = new JLabel();
  952:     DefaultListModel model = new DefaultListModel();
  953:     JScrollPane listScroller = new JScrollPane(list);
  954: 
  955:     public ArrayListCellRenderer()
  956:     {
  957:       //list = new JList();
  958:       vec = new ArrayList();
  959:       label.setForeground(Color.blue);
  960:     }
  961: 
  962:     public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
  963:     {
  964:       vec = (value != null) ? (ArrayList) value : new ArrayList();
  965:       model.clear();
  966:       for (int i = 0; i < vec.size(); ++i)
  967:         model.addElement(vec.get(i));
  968:       list.setModel(model);
  969: 
  970:       // if (selIndex < vec.size() && selIndex >= 0)
  971:       // list.setSelectedIndex(selIndex);
  972:       /*	if (hasFocus) {
  973:       				label.setBorder( UIManager.getBorder("Table.focusCellHighlightBorder") );
  974:       				if (table.isCellEditable(row, column)) {
  975:       						label.setForeground( UIManager.getColor("Table.focusCellForeground") );
  976:       						label.setBackground( UIManager.getColor("Table.focusCellBackground") );
  977:       				}
  978:       		} else {
  979:       				label.setBorder(BorderFactory.createEmptyBorder());
  980:       		}*/
  981:       /*
  982:       			if (vec.size() >0)
  983:       			{
  984:       				label.setText(vec.get(0).toString());
  985:       				return label;
  986:       			} else if (vec.isEmpty())
  987:       			{
  988:       				label.setText("null");
  989:       				return label;
  990:       
  991:       			} else
  992:       				return list;
  993:       		}*/
  994: 
  995:       label.setText((vec.isEmpty()) ? "" : vec.get(0).toString());
  996:       return label;
  997:     }
  998:   }
  999: 
 1000:   /**
 1001:    * exports the current data in this tablecomponent to HTML
 1002:    *  
 1003:    * @return StringBuffer containing the HTML code
 1004:    */
 1005: 
 1006:   public StringBuffer exportToHTML()
 1007:   {
 1008:     StringBuffer buff = new StringBuffer();
 1009:     buff.append(" <table border cellspacing=0 width=\"50%\">");
 1010:     // table Header
 1011:     buff.append("<tr>");
 1012:     for (int i = 0; i < tableModel.getColumnCount(); ++i)
 1013:     {
 1014:       buff.append("<th>");
 1015:       buff.append(tableModel.getColumnName(i));
 1016:       buff.append("</th>");
 1017:     }
 1018:     buff.append("</tr>");
 1019:     for (int i = 0; i < realCount; ++i)
 1020:     {
 1021:       for (int j = 0; j < tableModel.getColumnCount(); ++j)
 1022:       {
 1023:         buff.append("<td>");
 1024:         String val = (tableModel.getValueAt(i, j) == null) ? "&nbsp" : tableModel.getValueAt(i, j).toString();
 1025:         buff.append(val);
 1026:         buff.append("</td>");
 1027:       }
 1028:       buff.append("<tr>");
 1029:       buff.append("</tr>\n");
 1030:     }
 1031:     buff.append("</table>");
 1032: 
 1033:     return convertUml(buff);
 1034:   }
 1035:   /**
 1036:    * MacOs and Windows Version of the Method 
 1037:    * converts german umlaute to html &umlaute;
 1038:    */
 1039:   public static StringBuffer convertUml(StringBuffer newName)
 1040:   {
 1041:     StringBuffer alterMe = newName; //new StringBuffer(newName.trim());
 1042:     int length = alterMe.length();
 1043:     int j = 0;
 1044:     while (j < length)
 1045:     {
 1046:       //if(Character.isSpaceChar(alterMe.charAt(j)))
 1047:       //  alterMe.setCharAt(j,'_');
 1048:       if (alterMe.charAt(j) == 'š' || alterMe.charAt(j) == 'ö')
 1049:       {
 1050:         alterMe.setCharAt(j, '&');
 1051:         alterMe.insert(j + 1, "ouml;");
 1052:         length = length + 5;
 1053:       }
 1054:       if (alterMe.charAt(j) == '…' || alterMe.charAt(j) == 'Ö')
 1055:       {
 1056:         alterMe.setCharAt(j, '&');
 1057:         alterMe.insert(j + 1, "Ouml;");
 1058:         length = length + 5;
 1059: 
 1060:       }
 1061:       if (alterMe.charAt(j) == 'Š' || alterMe.charAt(j) == 'ä')
 1062:       {
 1063:         alterMe.setCharAt(j, '&');
 1064:         alterMe.insert(j + 1, "auml;");
 1065:         length = length + 5;
 1066: 
 1067:       }
 1068:       if (alterMe.charAt(j) == '€' || alterMe.charAt(j) == 'Ä')
 1069:       {
 1070:         alterMe.setCharAt(j, '&');
 1071:         alterMe.insert(j + 1, "Auml;");
 1072:         length = length + 5;
 1073:       }
 1074:       if (alterMe.charAt(j) == 'Ÿ' || alterMe.charAt(j) == 'ü')
 1075:       {
 1076:         alterMe.setCharAt(j, '&');
 1077:         alterMe.insert(j + 1, "uuml;");
 1078:         length = length + 5;
 1079:       }
 1080:       if (alterMe.charAt(j) == '†' || alterMe.charAt(j) == 'Ü')
 1081:       {
 1082:         alterMe.setCharAt(j, '&');
 1083:         alterMe.insert(j + 1, "&Uuml;");
 1084:         length = length + 5;
 1085:       }
 1086:       if (alterMe.charAt(j) == '§' || alterMe.charAt(j) == 'ß')
 1087:       {
 1088:         alterMe.setCharAt(j, '&');
 1089:         alterMe.insert(j + 1, "szlig;");
 1090:         length = length + 6;
 1091:       }
 1092: 
 1093:       /*
 1094:       if(Character.isSpaceChar(alterMe.charAt(j))
 1095:           alterMe.setCharAt(j,'_');
 1096:       */
 1097:       ++j;
 1098:     }
 1099:     return alterMe;
 1100:   }
 1101:   public static class SQLCommand
 1102:   {
 1103:     String command = "";
 1104: 
 1105:     public SQLCommand()
 1106:     {
 1107:     }
 1108:     public SQLCommand(String command)
 1109:     {
 1110:       this.command = command;
 1111:     }
 1112:     public String toString()
 1113:     {
 1114:       return command;
 1115:     }
 1116:   }
 1117:   public static class IDComboBox extends JComboBox
 1118:   {
 1119:     public IDComboBox(Vector vec)
 1120:     {
 1121:       super(vec);
 1122:     }
 1123: 
 1124:   }
 1125: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>