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