Annotation of FM2SQL/TableComponent.java, revision 1.4

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: */
1.4     ! rogo       11: 
1.1       rogo       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;
1.4     ! rogo       21:   String columnNames[] = { "no database", "connection" };
        !            22:   String[][] rowData = { { "", "" }
1.1       rogo       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;
1.4     ! rogo      143: 
1.1       rogo      144:         if (editor == null)
                    145:         {
                    146:           editor = getDefaultEditor(getColumnClass(column));
                    147:         }
                    148:         return editor;
                    149:       }
1.4     ! rogo      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 || getColumnClass(column) == IDComboBox.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:        }*/
1.1       rogo      166:     };
1.4     ! rogo      167: 
1.1       rogo      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);
1.4     ! rogo      184: 
1.1       rogo      185:     table.setDefaultEditor(JComboBox.class, new VectorEditor());
                    186:     table.setDefaultRenderer(JComboBox.class, new VectorCellRenderer());
1.4     ! rogo      187: 
1.1       rogo      188:     table.setDefaultEditor(ArrayList.class, new ArrayListEditor());
                    189:     table.setDefaultRenderer(ArrayList.class, new ArrayListCellRenderer());
1.4     ! rogo      190:     /* table.setDefaultEditor(SQLCommand.class,new DefaultCellEditor(new JTextField()) {
        !           191:        public Object getCellEditorValue()
        !           192:         {
        !           193:           return new SQLCommand(super.getCellEditorValue().toString());
        !           194:         }
        !           195:        });*/
1.1       rogo      196:     table.setDefaultEditor(SQLCommand.class, new SQLCommandEditor());
1.4     ! rogo      197: 
1.1       rogo      198:     table.setDefaultRenderer(Boolean.class, new TableCellRenderer()
                    199:     {
                    200:       JCheckBox checkBox = new JCheckBox();
1.4     ! rogo      201:       JLabel label = new JLabel();
1.1       rogo      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:   {
1.3       rogo      337:     //table.setAutoResizeMode(table.AUTO_RESIZE_LAST_COLUMN);
1.1       rogo      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:   }
1.4     ! rogo      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
        !           397:         return null;
        !           398: 
        !           399:     }
        !           400: 
        !           401:     public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)
        !           402:     {
        !           403:       boxValue = (value != null) ? (JComboBox) value : null;
        !           404:       this.row = row;
        !           405:       this.column = column;
        !           406:       if (boxValue != null)
        !           407:         box.setModel(boxValue.getModel());
        !           408:       else
        !           409:         return null;
        !           410:       return box;
        !           411:     }
        !           412: 
        !           413:     public boolean isCellEditable(EventObject anEvent)
        !           414:     {
        !           415:       if (table.isEditing())
        !           416:       {
        !           417: 
1.1       rogo      418:         //     System.out.println(getCellEditorValue());
1.4     ! rogo      419:         if (listener != null)
        !           420:           listener.editingStopped(new ChangeEvent(this));
        !           421: 
        !           422:         //   listener = null;
        !           423:         //  Object local =getCellEditorValue();
        !           424:         //table.getModel().setValueAt(local, table.getEditingRow(), table.getEditingColumn());
        !           425:         //  table.remove(label);   
        !           426:         //  table.validate();
        !           427:         //System.out.println("before "+table.getComponentCount());
        !           428:         //  if(table.getEditor)
        !           429:         // table.editCellAt(row,column);
        !           430:         // table.removeAll();
        !           431:         //return null;
        !           432:       }
        !           433: 
        !           434:       //System.out.println(anEvent);
        !           435:       return true;
        !           436:     }
        !           437: 
        !           438:     public boolean shouldSelectCell(EventObject anEvent)
        !           439:     {
        !           440:       //System.out.println(anEvent);
        !           441:       return true;
        !           442:     }
        !           443: 
        !           444:     public boolean stopCellEditing()
        !           445:     {
        !           446:       return true;
        !           447:     }
        !           448: 
        !           449:     public void cancelCellEditing()
        !           450:     {
        !           451:       listener.editingStopped(new ChangeEvent(this));
        !           452:     }
        !           453: 
        !           454:     public void addCellEditorListener(CellEditorListener l)
        !           455:     {
        !           456:       listener = l;
        !           457:       //System.out.println(l);
        !           458:     }
        !           459: 
        !           460:     public void removeCellEditorListener(CellEditorListener l)
        !           461:     {
        !           462:       //System.out.println("removed listener");
        !           463: 
        !           464:     }
1.1       rogo      465:     /* (non-Javadoc)
                    466:      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
                    467:      */
                    468:     public void actionPerformed(ActionEvent e)
                    469:     {
                    470:       // System.out.println(e.getActionCommand());
                    471:       if (table.isEditing())
1.4     ! rogo      472:         if (listener != null)
        !           473:           listener.editingStopped(new ChangeEvent(this));
1.1       rogo      474:     }
1.4     ! rogo      475: 
        !           476:   }
1.1       rogo      477:   class SQLCommandEditor implements TableCellEditor
1.4     ! rogo      478:   {
        !           479:     JTextArea editor = new JTextArea();
        !           480:     JScrollPane pane = new JScrollPane(editor);
        !           481:     JDialog window;
        !           482:     CellEditorListener listener = table;
        !           483:     JLabel label = new JLabel();
        !           484:     int x = 0, y = 0;
        !           485:     String copy = "";
        !           486:     boolean removeCalled = true;
        !           487:     public SQLCommandEditor()
1.1       rogo      488:     {
1.4     ! rogo      489:       //     super(new JCheckBox());
        !           490:       //this.list = list;
        !           491:       editor.getDocument().addDocumentListener(new DocumentListener()
        !           492:       {
1.1       rogo      493: 
                    494:         public void insertUpdate(DocumentEvent e)
                    495:         {
                    496:           //System.out.println(editor.getText().charAt( e.getOffset()));
1.4     ! rogo      497:           copy = editor.getText();
        !           498:           if (copy.charAt(e.getOffset()) == '\n')
1.1       rogo      499:           {
1.4     ! rogo      500:             window.setSize(window.getWidth(), window.getHeight() + 20);
1.1       rogo      501:             window.validate();
                    502:           }
                    503:         }
                    504: 
                    505:         public void removeUpdate(DocumentEvent e)
                    506:         {
1.4     ! rogo      507:           if (copy.charAt(e.getOffset()) == '\n')
1.1       rogo      508:           {
1.4     ! rogo      509:             window.setSize(window.getWidth(), window.getHeight() - 20);
1.1       rogo      510:             window.validate();
                    511:             copy = editor.getText();
                    512:           }
1.4     ! rogo      513: 
1.1       rogo      514:         }
                    515: 
                    516:         public void changedUpdate(DocumentEvent e)
1.4     ! rogo      517:         {
1.1       rogo      518:         }
1.4     ! rogo      519: 
1.1       rogo      520:       });
1.4     ! rogo      521:       table.addMouseListener(new MouseAdapter()
1.1       rogo      522:       {
1.4     ! rogo      523:         public void mouseEntered(MouseEvent e)
        !           524:         {
        !           525:           if (true)
        !           526:             return;
        !           527:           //System.out.println(e);
        !           528:           x = e.getX();
        !           529:           y = e.getY();
        !           530: 
        !           531:           int row = table.rowAtPoint(new Point(e.getX(), e.getY()));
        !           532:           int col = table.columnAtPoint(new Point(e.getX(), e.getY()));
        !           533: 
        !           534:           if (window != null)
        !           535:           {
        !           536:             if (!window.isVisible())
        !           537:               return;
        !           538: 
        !           539:           } else
        !           540:             return;
        !           541:           if (row != table.getEditingRow() || col != table.getEditingColumn())
        !           542:             if (listener != null)
        !           543:             {
        !           544:               listener.editingStopped(new ChangeEvent(this));
        !           545:               listener = null;
        !           546:             }
1.1       rogo      547:         }
1.4     ! rogo      548:         public void mousePressed(MouseEvent e)
        !           549:         {
        !           550: 
        !           551:           // System.out.println(e);
        !           552:           x = e.getX();
        !           553:           y = e.getY();
        !           554: 
        !           555:           if (window != null)
        !           556:             if (!window.isVisible())
        !           557:               return;
        !           558: 
        !           559:           int row = table.rowAtPoint(new Point(x, y));
        !           560:           int col = table.columnAtPoint(new Point(x, y));
        !           561: 
        !           562:           if (row != table.getEditingRow() || col != table.getEditingColumn())
1.1       rogo      563:             if (listener != null)
                    564:             {
                    565:               listener.editingStopped(new ChangeEvent(this));
                    566:               listener = null;
                    567:             }
1.4     ! rogo      568:         }
        !           569: 
        !           570:         public void mouseExited(MouseEvent e)
        !           571:         {
        !           572:           //  System.out.println(e);
        !           573: 
        !           574:         }
1.1       rogo      575:       });
1.4     ! rogo      576:     }
        !           577:     public Object getCellEditorValue()
        !           578:     {
        !           579:       window.setVisible(false);
        !           580:       window.dispose();
        !           581:       //table.repaint();
        !           582:       return new SQLCommand(editor.getText());
        !           583:     }
        !           584: 
        !           585:     public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)
        !           586:     {
        !           587:       // checks if editor was finished if not remove old editor
        !           588:       //this.row=row;
        !           589:       SQLCommand command = (value != null) ? (SQLCommand) value : null;
        !           590:       editor.setText(command.toString());
        !           591:       System.out.println("editor size " + editor.getPreferredSize());
        !           592:       pane.setBorder(new javax.swing.border.LineBorder(Color.red));
        !           593: 
        !           594:       if (window == null)
1.1       rogo      595:       {
1.4     ! rogo      596:         window = new JDialog(FM2SQL.fmInstance);
        !           597:         window.getContentPane().add(pane);
        !           598:         window.addWindowListener(new WindowAdapter()
        !           599:         {
        !           600:           public void windowClosing(WindowEvent e)
1.1       rogo      601:           {
1.4     ! rogo      602:             if (listener != null)
1.1       rogo      603:             {
1.4     ! rogo      604:               listener.editingStopped(new ChangeEvent(this));
        !           605:               listener = null;
        !           606:             }
1.1       rogo      607: 
1.4     ! rogo      608:           }
        !           609:         });
        !           610:       }
        !           611:       Point p = table.getLocationOnScreen();
        !           612:       //      if(label.isShowing())
        !           613:       //      System.out.println( label.getLocationOnScreen());
1.1       rogo      614: 
                    615:       Dimension dimEditor = editor.getPreferredSize();
                    616:       if (dimEditor.width < 260)
                    617:         dimEditor.width = 260;
1.2       rogo      618:       window.setSize((dimEditor.width < 280) ? dimEditor.width + 20 : 280, (dimEditor.height > 50) ? 200 : 70);
1.1       rogo      619:       TableColumnModel model = table.getColumnModel();
                    620:       int offset_x = 0, offset_y = 0;
                    621:       for (int i = 0; i <= column; ++i)
                    622:       {
                    623:         if (i == column)
                    624:           offset_x -= (window.getWidth() - model.getColumn(i).getWidth()) / 2;
                    625:         else
                    626:           offset_x += model.getColumn(i).getWidth();
1.4     ! rogo      627: 
1.1       rogo      628:       }
                    629:       for (int i = 0; i < row; ++i)
                    630:       {
                    631:         offset_y += table.getRowHeight(row);
                    632:       }
1.4     ! rogo      633:       // System.out.println(table.getCellRect(row, column, false));
        !           634:       window.setTitle("Editing row " + row + " column " + column);
        !           635:       window.setLocation(p.x + offset_x, p.y + offset_y);
1.1       rogo      636:       window.setVisible(true);
1.4     ! rogo      637:       System.out.println("row " + row + " col " + column + " location" + window.getLocation());
        !           638:       // window.setVisible(true);
        !           639: 
        !           640:       //label=new JLabel(command.toString());
        !           641:       label.setText(command.toString());
        !           642:       label.setForeground(Color.red);
        !           643:       return label;
        !           644:     }
1.1       rogo      645: 
1.4     ! rogo      646:     public boolean isCellEditable(EventObject anEvent)
        !           647:     {
        !           648:       if (table.isEditing())
1.1       rogo      649:       {
                    650: 
1.4     ! rogo      651:         if (listener == null)
        !           652:           listener = table;
        !           653:         if (listener != null)
        !           654:           listener.editingStopped(new ChangeEvent(this));
1.1       rogo      655:       }
                    656: 
1.4     ! rogo      657:       return true;
        !           658:     }
        !           659: 
        !           660:     public boolean shouldSelectCell(EventObject anEvent)
        !           661:     {
        !           662: 
        !           663:       return true;
        !           664:     }
        !           665: 
        !           666:     public boolean stopCellEditing()
        !           667:     {
        !           668:       return true;
        !           669:     }
1.1       rogo      670: 
1.4     ! rogo      671:     public void cancelCellEditing()
        !           672:     {
        !           673:       System.out.println("cancel was called");
        !           674:     }
1.1       rogo      675: 
1.4     ! rogo      676:     public void addCellEditorListener(CellEditorListener l)
        !           677:     {
        !           678:       removeCalled = false;
        !           679:       listener = l;
        !           680:     }
1.1       rogo      681: 
1.4     ! rogo      682:     public void removeCellEditorListener(CellEditorListener l)
        !           683:     {
        !           684:       //if(listener!=null)
        !           685:       removeCalled = true;
        !           686:       //table.removeAll();
1.1       rogo      687: 
1.4     ! rogo      688:       System.out.println("remove was called");
1.1       rogo      689:     }
                    690: 
1.4     ! rogo      691:   }
1.1       rogo      692: 
                    693:   class ArrayListEditor implements TableCellEditor
                    694:   {
                    695:     JList list = new JList();
1.4     ! rogo      696:     ArrayList vec = new ArrayList();
1.1       rogo      697:     DefaultListModel model = new DefaultListModel();
1.4     ! rogo      698:     JScrollPane pane = new JScrollPane(list);
        !           699:     JWindow window;
        !           700:     CellEditorListener listener = table;
        !           701:     int row, x, y;
1.1       rogo      702:     public ArrayListEditor()
                    703:     {
1.4     ! rogo      704:       //     super(new JCheckBox());
1.1       rogo      705:       //this.list = list;
1.4     ! rogo      706:       table.addMouseListener(new MouseAdapter()
        !           707:       {
        !           708:         public void mouseEntered(MouseEvent e)
        !           709:         {
1.1       rogo      710:           // if(true) return;
1.4     ! rogo      711:           //System.out.println(e);
        !           712:           x = e.getX();
        !           713:           y = e.getY();
        !           714: 
        !           715:           int row = table.rowAtPoint(new Point(e.getX(), e.getY()));
        !           716:           int col = table.columnAtPoint(new Point(e.getX(), e.getY()));
        !           717: 
        !           718:           if (window != null)
        !           719:           {
        !           720:             if (!window.isVisible())
        !           721:               return;
        !           722: 
        !           723:           } else
        !           724:             return;
        !           725: 
        !           726:           if (row != table.getEditingRow() || col != table.getEditingColumn())
        !           727:             if (listener != null)
        !           728:             {
        !           729:               listener.editingStopped(new ChangeEvent(this));
        !           730:               //listener = null;
        !           731:             }
        !           732:         }
        !           733:         public void mousePressed(MouseEvent e)
        !           734:         {
        !           735: 
        !           736:           // System.out.println(e);
        !           737:           x = e.getX();
        !           738:           y = e.getY();
        !           739: 
        !           740:           if (window != null)
        !           741:             if (!window.isVisible())
        !           742:               return;
        !           743: 
        !           744:           int row = table.rowAtPoint(new Point(x, y));
        !           745:           int col = table.columnAtPoint(new Point(x, y));
        !           746: 
        !           747:           if (row != table.getEditingRow() || col != table.getEditingColumn())
        !           748:             if (listener != null)
        !           749:             {
        !           750:               listener.editingStopped(new ChangeEvent(this));
        !           751:               //listener = null;
        !           752:             }
        !           753:         }
        !           754: 
        !           755:         public void mouseExited(MouseEvent e)
        !           756:         {
        !           757:           //  System.out.println(e);
        !           758: 
        !           759:         }
        !           760:       });
        !           761: 
        !           762:       pane.addFocusListener(new FocusAdapter()
        !           763:       {
        !           764:         public void focusLost(FocusEvent e)
        !           765:         {
        !           766:           listener.editingStopped(new ChangeEvent(this));
        !           767:           //       table.setRowHeight(row,20);
        !           768:           System.out.println("lost focus");
        !           769:         }
1.1       rogo      770: 
1.4     ! rogo      771:       });
        !           772:       pane.addMouseListener(new MouseAdapter()
1.1       rogo      773:       {
1.4     ! rogo      774:         public void mouseExited(MouseEvent e)
        !           775:         {
        !           776:           //  System.out.println(e);
        !           777:           listener.editingStopped(new ChangeEvent(this));
        !           778:         }
        !           779:       });
        !           780:     }
1.1       rogo      781:     public Object getCellEditorValue()
                    782:     {
                    783:       selIndex = list.getSelectedIndex();
1.4     ! rogo      784:       window.setVisible(false);
1.1       rogo      785:       window.dispose();
1.4     ! rogo      786:       //   table.setRowHeight(row,20);
        !           787: 
        !           788:       return vec;
1.1       rogo      789:     }
                    790: 
                    791:     public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)
                    792:     {
1.4     ! rogo      793:       this.row = row;
1.1       rogo      794:       vec = (value != null) ? (ArrayList) value : new ArrayList();
1.4     ! rogo      795:       model.clear();
        !           796:       for (int i = 0; i < vec.size(); ++i)
        !           797:         model.addElement(vec.get(i));
        !           798:       list.setModel(model);
        !           799:       list.setModel(model);
1.1       rogo      800:       pane.setBorder(new javax.swing.border.LineBorder(Color.red));
1.4     ! rogo      801:       //if(!isSelected)
        !           802:       //table.setRowHeight(row,80);
        !           803:       if (window == null)
        !           804:       {
        !           805:         window = new JWindow(FM2SQL.fmInstance);
        !           806:         window.getContentPane().add(pane);
        !           807:       }
        !           808:       Point p = table.getLocationOnScreen();
        !           809:       //      if(label.isShowing())
        !           810:       //      System.out.println( label.getLocationOnScreen());
        !           811: 
        !           812:       Dimension dimEditor = pane.getPreferredSize();
        !           813:       window.setSize((dimEditor.width < 250) ? dimEditor.width + 20 : 250, (dimEditor.height > 50) ? 200 : 50);
        !           814:       TableColumnModel model = table.getColumnModel();
        !           815:       int offset_x = 0, offset_y = 0;
        !           816:       for (int i = 0; i <= column; ++i)
        !           817:       {
        !           818:         if (i == column)
        !           819:           offset_x -= (window.getWidth() - model.getColumn(i).getWidth()) / 2;
        !           820:         else
        !           821:           offset_x += model.getColumn(i).getWidth();
        !           822: 
        !           823:       }
        !           824:       for (int i = 0; i < row; ++i)
        !           825:       {
        !           826:         offset_y += table.getRowHeight(row);
        !           827:       }
        !           828:       window.setVisible(true);
        !           829:       // System.out.println(table.getCellRect(row, column, false));
1.1       rogo      830: 
1.4     ! rogo      831:       window.setLocation(p.x + offset_x, p.y + offset_y);
        !           832:       // System.out.println("row "+row+" col "+column+" location"+ window.getBounds());
        !           833:       // window.setVisible(true);
        !           834:       //label=new JLabel(command.toString());
        !           835:       label.setText("editing");
        !           836:       label.setForeground(Color.red);
        !           837:       return label;
1.1       rogo      838: 
1.4     ! rogo      839:       // return pane;
1.1       rogo      840:     }
                    841: 
1.4     ! rogo      842:     public boolean isCellEditable(EventObject anEvent)
        !           843:     {
1.1       rogo      844:       if (table.isEditing())
                    845:       {
                    846:         System.out.println("Editing is in progress");
1.4     ! rogo      847: 
        !           848:         // if (listener == null)
        !           849:         // listener = table;
1.1       rogo      850:         if (listener != null)
                    851:           listener.editingStopped(new ChangeEvent(this));
                    852: 
                    853:       }
                    854:       return true;
1.4     ! rogo      855:     }
        !           856: 
        !           857:     public boolean shouldSelectCell(EventObject anEvent)
        !           858:     {
        !           859:       return true;
        !           860:     }
        !           861: 
        !           862:     public boolean stopCellEditing()
        !           863:     {
        !           864:       return true;
        !           865:     }
        !           866: 
        !           867:     public void cancelCellEditing()
        !           868:     {
        !           869:     }
        !           870: 
        !           871:     public void addCellEditorListener(CellEditorListener l)
        !           872:     {
        !           873:       listener = l;
        !           874:     }
        !           875: 
        !           876:     public void removeCellEditorListener(CellEditorListener l)
        !           877:     {
        !           878:       System.out.println(l);
        !           879:     }
1.1       rogo      880: 
                    881:   }
                    882:   class VectorCellRenderer implements TableCellRenderer
                    883:   {
1.4     ! rogo      884:     JList list = new JList();
        !           885:     JComboBox box;
1.1       rogo      886:     JLabel label = new JLabel();
1.4     ! rogo      887:     DefaultListModel model = new DefaultListModel();
        !           888:     JScrollPane listScroller = new JScrollPane(list);
        !           889:     Color noIDColor=new Color(50,50,200);
        !           890:     
1.1       rogo      891:     public VectorCellRenderer()
                    892:     {
                    893:       //list = new JList();
1.4     ! rogo      894:       // vec = new Vector();
1.1       rogo      895:       label.setForeground(Color.blue);
                    896:     }
                    897: 
                    898:     public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
                    899:     {
1.4     ! rogo      900:       box = (JComboBox) value;
        !           901:       if (box == null)
        !           902:         return null; //label.setText("");
        !           903:       else if (box.getSelectedItem() != null)
        !           904:       {
        !           905:         String text = box.getSelectedItem().toString();
        !           906:         if (text == "")
        !           907:         {
        !           908:           if (box instanceof IDComboBox)
        !           909:           {
        !           910:             text = "\"no  ID\"";
        !           911:             label.setForeground(noIDColor);
        !           912:           } else
        !           913:           {
        !           914:             text = "\"no Layout\"";
        !           915:             label.setForeground(Color.red);
        !           916:           }
        !           917:         } else
        !           918:         {
        !           919:           if (box instanceof IDComboBox)
        !           920:             label.setForeground(Color.darkGray);
        !           921: 
        !           922:           else
        !           923:             label.setForeground(Color.blue);
        !           924:         }
        !           925:         label.setText(text);
        !           926: 
        !           927:       } else
        !           928:         label.setText("");
        !           929:       return label;
1.1       rogo      930:     }
                    931: 
                    932:   }
1.4     ! rogo      933:   class ArrayListCellRenderer implements TableCellRenderer
        !           934:   {
        !           935:     JList list = new JList();
        !           936:     ArrayList vec;
        !           937:     JLabel label = new JLabel();
        !           938:     DefaultListModel model = new DefaultListModel();
        !           939:     JScrollPane listScroller = new JScrollPane(list);
        !           940: 
        !           941:     public ArrayListCellRenderer()
        !           942:     {
        !           943:       //list = new JList();
        !           944:       vec = new ArrayList();
        !           945:       label.setForeground(Color.blue);
        !           946:     }
        !           947: 
        !           948:     public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
        !           949:     {
        !           950:       vec = (value != null) ? (ArrayList) value : new ArrayList();
        !           951:       model.clear();
        !           952:       for (int i = 0; i < vec.size(); ++i)
        !           953:         model.addElement(vec.get(i));
        !           954:       list.setModel(model);
        !           955: 
        !           956:       // if (selIndex < vec.size() && selIndex >= 0)
        !           957:       // list.setSelectedIndex(selIndex);
        !           958:       /*   if (hasFocus) {
        !           959:                    label.setBorder( UIManager.getBorder("Table.focusCellHighlightBorder") );
        !           960:                    if (table.isCellEditable(row, column)) {
        !           961:                            label.setForeground( UIManager.getColor("Table.focusCellForeground") );
        !           962:                            label.setBackground( UIManager.getColor("Table.focusCellBackground") );
        !           963:                    }
        !           964:            } else {
        !           965:                    label.setBorder(BorderFactory.createEmptyBorder());
        !           966:            }*/
        !           967:       /*
        !           968:                if (vec.size() >0)
        !           969:                {
        !           970:                    label.setText(vec.get(0).toString());
        !           971:                    return label;
        !           972:                } else if (vec.isEmpty())
        !           973:                {
        !           974:                    label.setText("null");
        !           975:                    return label;
        !           976:       
        !           977:                } else
        !           978:                    return list;
        !           979:            }*/
        !           980: 
        !           981:       label.setText((vec.isEmpty()) ? "" : vec.get(0).toString());
        !           982:       return label;
1.1       rogo      983:     }
                    984:   }
                    985: 
                    986:   /**
                    987:    * exports the current data in this tablecomponent to HTML
                    988:    *  
                    989:    * @return StringBuffer containing the HTML code
                    990:    */
1.4     ! rogo      991: 
1.1       rogo      992:   public StringBuffer exportToHTML()
                    993:   {
                    994:     StringBuffer buff = new StringBuffer();
                    995:     buff.append(" <table border cellspacing=0 width=\"50%\">");
                    996:     // table Header
                    997:     buff.append("<tr>");
                    998:     for (int i = 0; i < tableModel.getColumnCount(); ++i)
                    999:     {
                   1000:       buff.append("<th>");
                   1001:       buff.append(tableModel.getColumnName(i));
                   1002:       buff.append("</th>");
                   1003:     }
                   1004:     buff.append("</tr>");
                   1005:     for (int i = 0; i < realCount; ++i)
                   1006:     {
                   1007:       for (int j = 0; j < tableModel.getColumnCount(); ++j)
                   1008:       {
                   1009:         buff.append("<td>");
                   1010:         String val = (tableModel.getValueAt(i, j) == null) ? "&nbsp" : tableModel.getValueAt(i, j).toString();
                   1011:         buff.append(val);
                   1012:         buff.append("</td>");
                   1013:       }
                   1014:       buff.append("<tr>");
                   1015:       buff.append("</tr>\n");
                   1016:     }
                   1017:     buff.append("</table>");
                   1018: 
                   1019:     return convertUml(buff);
                   1020:   }
                   1021:   /**
                   1022:    * MacOs and Windows Version of the Method 
                   1023:    * converts german umlaute to html &umlaute;
                   1024:    */
                   1025:   public static StringBuffer convertUml(StringBuffer newName)
                   1026:   {
                   1027:     StringBuffer alterMe = newName; //new StringBuffer(newName.trim());
                   1028:     int length = alterMe.length();
                   1029:     int j = 0;
                   1030:     while (j < length)
                   1031:     {
                   1032:       //if(Character.isSpaceChar(alterMe.charAt(j)))
                   1033:       //  alterMe.setCharAt(j,'_');
                   1034:       if (alterMe.charAt(j) == 'š' || alterMe.charAt(j) == 'ö')
                   1035:       {
                   1036:         alterMe.setCharAt(j, '&');
                   1037:         alterMe.insert(j + 1, "ouml;");
                   1038:         length = length + 5;
                   1039:       }
                   1040:       if (alterMe.charAt(j) == '…' || alterMe.charAt(j) == 'Ö')
                   1041:       {
                   1042:         alterMe.setCharAt(j, '&');
                   1043:         alterMe.insert(j + 1, "Ouml;");
                   1044:         length = length + 5;
                   1045: 
                   1046:       }
                   1047:       if (alterMe.charAt(j) == 'Š' || alterMe.charAt(j) == 'ä')
                   1048:       {
                   1049:         alterMe.setCharAt(j, '&');
                   1050:         alterMe.insert(j + 1, "auml;");
                   1051:         length = length + 5;
                   1052: 
                   1053:       }
                   1054:       if (alterMe.charAt(j) == '€' || alterMe.charAt(j) == 'Ä')
                   1055:       {
                   1056:         alterMe.setCharAt(j, '&');
                   1057:         alterMe.insert(j + 1, "Auml;");
                   1058:         length = length + 5;
                   1059:       }
                   1060:       if (alterMe.charAt(j) == 'Ÿ' || alterMe.charAt(j) == 'ü')
                   1061:       {
                   1062:         alterMe.setCharAt(j, '&');
                   1063:         alterMe.insert(j + 1, "uuml;");
                   1064:         length = length + 5;
                   1065:       }
                   1066:       if (alterMe.charAt(j) == '†' || alterMe.charAt(j) == 'Ü')
                   1067:       {
                   1068:         alterMe.setCharAt(j, '&');
                   1069:         alterMe.insert(j + 1, "&Uuml;");
                   1070:         length = length + 5;
                   1071:       }
                   1072:       if (alterMe.charAt(j) == '§' || alterMe.charAt(j) == 'ß')
                   1073:       {
                   1074:         alterMe.setCharAt(j, '&');
                   1075:         alterMe.insert(j + 1, "szlig;");
                   1076:         length = length + 6;
                   1077:       }
                   1078: 
                   1079:       /*
                   1080:       if(Character.isSpaceChar(alterMe.charAt(j))
                   1081:           alterMe.setCharAt(j,'_');
                   1082:       */
                   1083:       ++j;
                   1084:     }
                   1085:     return alterMe;
                   1086:   }
                   1087:   public static class SQLCommand
                   1088:   {
                   1089:     String command = "";
1.4     ! rogo     1090: 
1.1       rogo     1091:     public SQLCommand()
                   1092:     {
                   1093:     }
                   1094:     public SQLCommand(String command)
                   1095:     {
                   1096:       this.command = command;
                   1097:     }
                   1098:     public String toString()
                   1099:     {
                   1100:       return command;
                   1101:     }
                   1102:   }
1.4     ! rogo     1103:   public static class IDComboBox extends JComboBox
        !          1104:   {
        !          1105:     public IDComboBox(Vector vec)
        !          1106:     {
        !          1107:       super(vec);
        !          1108:     }
1.1       rogo     1109: 
1.4     ! rogo     1110:   }
1.1       rogo     1111: }

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