Annotation of FM2SQL/TableComponent.java, revision 1.1.1.1

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

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