Annotation of FM2SQL/TableComponent.java, revision 1.5

1.5     ! rogo        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:  */
1.1       rogo       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: */
1.4       rogo       25: 
1.1       rogo       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;
1.4       rogo       35:   String columnNames[] = { "no database", "connection" };
                     36:   String[][] rowData = { { "", "" }
1.1       rogo       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;
1.4       rogo      157: 
1.1       rogo      158:         if (editor == null)
                    159:         {
                    160:           editor = getDefaultEditor(getColumnClass(column));
                    161:         }
                    162:         return editor;
                    163:       }
1.4       rogo      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:        }*/
1.1       rogo      180:     };
1.4       rogo      181: 
1.1       rogo      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);
1.4       rogo      198: 
1.1       rogo      199:     table.setDefaultEditor(JComboBox.class, new VectorEditor());
                    200:     table.setDefaultRenderer(JComboBox.class, new VectorCellRenderer());
1.4       rogo      201: 
1.1       rogo      202:     table.setDefaultEditor(ArrayList.class, new ArrayListEditor());
                    203:     table.setDefaultRenderer(ArrayList.class, new ArrayListCellRenderer());
1.4       rogo      204:     /* table.setDefaultEditor(SQLCommand.class,new DefaultCellEditor(new JTextField()) {
                    205:        public Object getCellEditorValue()
                    206:         {
                    207:           return new SQLCommand(super.getCellEditorValue().toString());
                    208:         }
                    209:        });*/
1.1       rogo      210:     table.setDefaultEditor(SQLCommand.class, new SQLCommandEditor());
1.4       rogo      211: 
1.1       rogo      212:     table.setDefaultRenderer(Boolean.class, new TableCellRenderer()
                    213:     {
                    214:       JCheckBox checkBox = new JCheckBox();
1.4       rogo      215:       JLabel label = new JLabel();
1.1       rogo      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:   {
1.3       rogo      351:     //table.setAutoResizeMode(table.AUTO_RESIZE_LAST_COLUMN);
1.1       rogo      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:   }
1.4       rogo      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: 
1.1       rogo      432:         //     System.out.println(getCellEditorValue());
1.4       rogo      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:     }
1.1       rogo      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())
1.4       rogo      486:         if (listener != null)
                    487:           listener.editingStopped(new ChangeEvent(this));
1.1       rogo      488:     }
1.4       rogo      489: 
                    490:   }
1.1       rogo      491:   class SQLCommandEditor implements TableCellEditor
1.4       rogo      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()
1.1       rogo      502:     {
1.4       rogo      503:       //     super(new JCheckBox());
                    504:       //this.list = list;
                    505:       editor.getDocument().addDocumentListener(new DocumentListener()
                    506:       {
1.1       rogo      507: 
                    508:         public void insertUpdate(DocumentEvent e)
                    509:         {
                    510:           //System.out.println(editor.getText().charAt( e.getOffset()));
1.4       rogo      511:           copy = editor.getText();
                    512:           if (copy.charAt(e.getOffset()) == '\n')
1.1       rogo      513:           {
1.4       rogo      514:             window.setSize(window.getWidth(), window.getHeight() + 20);
1.1       rogo      515:             window.validate();
                    516:           }
                    517:         }
                    518: 
                    519:         public void removeUpdate(DocumentEvent e)
                    520:         {
1.4       rogo      521:           if (copy.charAt(e.getOffset()) == '\n')
1.1       rogo      522:           {
1.4       rogo      523:             window.setSize(window.getWidth(), window.getHeight() - 20);
1.1       rogo      524:             window.validate();
                    525:             copy = editor.getText();
                    526:           }
1.4       rogo      527: 
1.1       rogo      528:         }
                    529: 
                    530:         public void changedUpdate(DocumentEvent e)
1.4       rogo      531:         {
1.1       rogo      532:         }
1.4       rogo      533: 
1.1       rogo      534:       });
1.4       rogo      535:       table.addMouseListener(new MouseAdapter()
1.1       rogo      536:       {
1.4       rogo      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:             }
1.1       rogo      561:         }
1.4       rogo      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())
1.1       rogo      577:             if (listener != null)
                    578:             {
                    579:               listener.editingStopped(new ChangeEvent(this));
                    580:               listener = null;
                    581:             }
1.4       rogo      582:         }
                    583: 
                    584:         public void mouseExited(MouseEvent e)
                    585:         {
                    586:           //  System.out.println(e);
                    587: 
                    588:         }
1.1       rogo      589:       });
1.4       rogo      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)
1.1       rogo      609:       {
1.4       rogo      610:         window = new JDialog(FM2SQL.fmInstance);
                    611:         window.getContentPane().add(pane);
                    612:         window.addWindowListener(new WindowAdapter()
                    613:         {
                    614:           public void windowClosing(WindowEvent e)
1.1       rogo      615:           {
1.4       rogo      616:             if (listener != null)
1.1       rogo      617:             {
1.4       rogo      618:               listener.editingStopped(new ChangeEvent(this));
                    619:               listener = null;
                    620:             }
1.1       rogo      621: 
1.4       rogo      622:           }
                    623:         });
                    624:       }
                    625:       Point p = table.getLocationOnScreen();
                    626:       //      if(label.isShowing())
                    627:       //      System.out.println( label.getLocationOnScreen());
1.1       rogo      628: 
                    629:       Dimension dimEditor = editor.getPreferredSize();
                    630:       if (dimEditor.width < 260)
                    631:         dimEditor.width = 260;
1.2       rogo      632:       window.setSize((dimEditor.width < 280) ? dimEditor.width + 20 : 280, (dimEditor.height > 50) ? 200 : 70);
1.1       rogo      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();
1.4       rogo      641: 
1.1       rogo      642:       }
                    643:       for (int i = 0; i < row; ++i)
                    644:       {
                    645:         offset_y += table.getRowHeight(row);
                    646:       }
1.4       rogo      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);
1.1       rogo      650:       window.setVisible(true);
1.4       rogo      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:     }
1.1       rogo      659: 
1.4       rogo      660:     public boolean isCellEditable(EventObject anEvent)
                    661:     {
                    662:       if (table.isEditing())
1.1       rogo      663:       {
                    664: 
1.4       rogo      665:         if (listener == null)
                    666:           listener = table;
                    667:         if (listener != null)
                    668:           listener.editingStopped(new ChangeEvent(this));
1.1       rogo      669:       }
                    670: 
1.4       rogo      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:     }
1.1       rogo      684: 
1.4       rogo      685:     public void cancelCellEditing()
                    686:     {
                    687:       System.out.println("cancel was called");
                    688:     }
1.1       rogo      689: 
1.4       rogo      690:     public void addCellEditorListener(CellEditorListener l)
                    691:     {
                    692:       removeCalled = false;
                    693:       listener = l;
                    694:     }
1.1       rogo      695: 
1.4       rogo      696:     public void removeCellEditorListener(CellEditorListener l)
                    697:     {
                    698:       //if(listener!=null)
                    699:       removeCalled = true;
                    700:       //table.removeAll();
1.1       rogo      701: 
1.4       rogo      702:       System.out.println("remove was called");
1.1       rogo      703:     }
                    704: 
1.4       rogo      705:   }
1.1       rogo      706: 
                    707:   class ArrayListEditor implements TableCellEditor
                    708:   {
                    709:     JList list = new JList();
1.4       rogo      710:     ArrayList vec = new ArrayList();
1.1       rogo      711:     DefaultListModel model = new DefaultListModel();
1.4       rogo      712:     JScrollPane pane = new JScrollPane(list);
                    713:     JWindow window;
                    714:     CellEditorListener listener = table;
                    715:     int row, x, y;
1.1       rogo      716:     public ArrayListEditor()
                    717:     {
1.4       rogo      718:       //     super(new JCheckBox());
1.1       rogo      719:       //this.list = list;
1.4       rogo      720:       table.addMouseListener(new MouseAdapter()
                    721:       {
                    722:         public void mouseEntered(MouseEvent e)
                    723:         {
1.1       rogo      724:           // if(true) return;
1.4       rogo      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:         }
1.1       rogo      784: 
1.4       rogo      785:       });
                    786:       pane.addMouseListener(new MouseAdapter()
1.1       rogo      787:       {
1.4       rogo      788:         public void mouseExited(MouseEvent e)
                    789:         {
                    790:           //  System.out.println(e);
                    791:           listener.editingStopped(new ChangeEvent(this));
                    792:         }
                    793:       });
                    794:     }
1.1       rogo      795:     public Object getCellEditorValue()
                    796:     {
                    797:       selIndex = list.getSelectedIndex();
1.4       rogo      798:       window.setVisible(false);
1.1       rogo      799:       window.dispose();
1.4       rogo      800:       //   table.setRowHeight(row,20);
                    801: 
                    802:       return vec;
1.1       rogo      803:     }
                    804: 
                    805:     public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)
                    806:     {
1.4       rogo      807:       this.row = row;
1.1       rogo      808:       vec = (value != null) ? (ArrayList) value : new ArrayList();
1.4       rogo      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);
1.1       rogo      814:       pane.setBorder(new javax.swing.border.LineBorder(Color.red));
1.4       rogo      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));
1.1       rogo      844: 
1.4       rogo      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;
1.1       rogo      852: 
1.4       rogo      853:       // return pane;
1.1       rogo      854:     }
                    855: 
1.4       rogo      856:     public boolean isCellEditable(EventObject anEvent)
                    857:     {
1.1       rogo      858:       if (table.isEditing())
                    859:       {
                    860:         System.out.println("Editing is in progress");
1.4       rogo      861: 
                    862:         // if (listener == null)
                    863:         // listener = table;
1.1       rogo      864:         if (listener != null)
                    865:           listener.editingStopped(new ChangeEvent(this));
                    866: 
                    867:       }
                    868:       return true;
1.4       rogo      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:     }
1.1       rogo      894: 
                    895:   }
                    896:   class VectorCellRenderer implements TableCellRenderer
                    897:   {
1.4       rogo      898:     JList list = new JList();
                    899:     JComboBox box;
1.1       rogo      900:     JLabel label = new JLabel();
1.4       rogo      901:     DefaultListModel model = new DefaultListModel();
                    902:     JScrollPane listScroller = new JScrollPane(list);
                    903:     Color noIDColor=new Color(50,50,200);
                    904:     
1.1       rogo      905:     public VectorCellRenderer()
                    906:     {
                    907:       //list = new JList();
1.4       rogo      908:       // vec = new Vector();
1.1       rogo      909:       label.setForeground(Color.blue);
                    910:     }
                    911: 
                    912:     public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
                    913:     {
1.4       rogo      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;
1.1       rogo      944:     }
                    945: 
                    946:   }
1.4       rogo      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;
1.1       rogo      997:     }
                    998:   }
                    999: 
                   1000:   /**
                   1001:    * exports the current data in this tablecomponent to HTML
                   1002:    *  
                   1003:    * @return StringBuffer containing the HTML code
                   1004:    */
1.4       rogo     1005: 
1.1       rogo     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 = "";
1.4       rogo     1104: 
1.1       rogo     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:   }
1.4       rogo     1117:   public static class IDComboBox extends JComboBox
                   1118:   {
                   1119:     public IDComboBox(Vector vec)
                   1120:     {
                   1121:       super(vec);
                   1122:     }
1.1       rogo     1123: 
1.4       rogo     1124:   }
1.1       rogo     1125: }

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