File:  [Repository] / FM2SQL / Attic / TableComponent.java
Revision 1.6: download - view: text, annotated - select for diffs - revision graph
Wed May 26 11:37:32 2004 UTC (20 years, 1 month ago) by rogo
Branches: MAIN
CVS tags: HEAD
new field for source table status added,
now selection for convert etc is done by this field

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

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