version 1.1, 2003/12/05 11:46:39
|
version 1.8, 2004/11/18 11:17:43
|
Line 1
|
Line 1
|
|
/* |
|
* TableComponent.java -- GUI Table class |
|
* Filemake to SQL Converter |
|
* Copyright (C) 2004 Robert Gordesch (rogo@mpiwg-berlin.mpg.de) |
|
* This program is free software; you can redistribute it and/or modify it |
|
* under the terms of the GNU General Public License as published by the Free |
|
* Software Foundation; either version 2 of the License, or (at your option) |
|
* any later version. Please read license.txt for the full details. A copy of |
|
* the GPL may be found at http://www.gnu.org/copyleft/lgpl.html You should |
|
* have received a copy of the GNU General Public License along with this |
|
* program; if not, write to the Free Software Foundation, Inc., 59 Temple |
|
* Place, Suite 330, Boston, MA 02111-1307 USA Created on 15.09.2003 by |
|
* rogo |
|
*/ |
import javax.swing.*; |
import javax.swing.*; |
import java.awt.event.*; |
import java.awt.event.*; |
|
|
Line 27 public class TableComponent extends JPan
|
Line 41 public class TableComponent extends JPan
|
int selIndex = 0; |
int selIndex = 0; |
Icon trueIcon = null, falseIcon = null, empty = null; |
Icon trueIcon = null, falseIcon = null, empty = null; |
TableComponent tableComp = this; |
TableComponent tableComp = this; |
|
BooleanRenderer booleanRenderer = new BooleanRenderer(); |
/** |
/** |
* Constructs an empty table with default header and dummy data |
* Constructs an empty table with default header and dummy data |
*/ |
*/ |
Line 151 public class TableComponent extends JPan
|
Line 166 public class TableComponent extends JPan
|
{ |
{ |
tableComp.row=row; |
tableComp.row=row; |
boolean value=false; |
boolean value=false; |
if(getColumnClass(column)==ArrayList.class||getColumnClass(column)==JComboBox.class||getColumnClass(column)== SQLCommand.class) |
if (getColumnClass(column) == ArrayList.class || getColumnClass(column) == JComboBox.class || getColumnClass(column) == SQLCommand.class || getColumnClass(column) == IDComboBox.class|| getColumnClass(column) == Boolean.class) |
value=true; |
value=true; |
//System.out.println("tried to edit at row "+row+" column "+column+" "+value); |
//System.out.println("tried to edit at row "+row+" column "+column+" "+value); |
|
|
Line 195 public class TableComponent extends JPan
|
Line 210 public class TableComponent extends JPan
|
});*/ |
});*/ |
table.setDefaultEditor(SQLCommand.class, new SQLCommandEditor()); |
table.setDefaultEditor(SQLCommand.class, new SQLCommandEditor()); |
|
|
table.setDefaultRenderer(Boolean.class, new TableCellRenderer() |
table.setDefaultRenderer(Boolean.class, booleanRenderer); |
{ |
|
JCheckBox checkBox = new JCheckBox(); |
|
JLabel label = new JLabel(); |
|
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) |
|
{ |
|
if (value instanceof Integer) |
|
{ |
|
label.setText(value.toString()); |
|
label.setHorizontalAlignment(JLabel.RIGHT); |
|
|
|
return label; |
|
} else if (value instanceof String) |
|
{ |
|
label.setText(value.toString()); |
|
label.setHorizontalAlignment(JLabel.CENTER); |
|
return label; |
|
} |
|
checkBox.setHorizontalAlignment(JLabel.CENTER); |
|
checkBox.setVerticalAlignment(JLabel.CENTER); |
|
|
|
checkBox.setSelectedIcon(trueIcon); |
|
if (value == null) |
|
checkBox.setIcon(empty); |
|
else |
|
checkBox.setIcon(falseIcon); |
|
checkBox.setForeground(table.getForeground()); |
|
checkBox.setBackground(table.getBackground()); |
|
|
|
checkBox.setSelected((value != null && ((Boolean) value).booleanValue())); |
|
return checkBox; |
|
|
|
} |
|
|
|
}); |
|
|
|
table.setRowHeight(20); |
table.setRowHeight(20); |
|
|
Line 310 public class TableComponent extends JPan
|
Line 291 public class TableComponent extends JPan
|
|
|
// Class tre=table.getColumnModel().getColumn(1).getHeaderValue().getClass(); |
// Class tre=table.getColumnModel().getColumn(1).getHeaderValue().getClass(); |
//Graphics2D g2=((Graphics2D)getGraphics()); |
//Graphics2D g2=((Graphics2D)getGraphics()); |
table.setAutoResizeMode(table.AUTO_RESIZE_OFF); |
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); |
|
|
TableColumnModel model = table.getColumnModel(); |
TableColumnModel model = table.getColumnModel(); |
for (int i = 0; i < model.getColumnCount(); ++i) |
for (int i = 0; i < model.getColumnCount(); ++i) |
{ |
{ |
label = new JLabel(model.getColumn(i).getHeaderValue().toString()); |
label = new JLabel(model.getColumn(i).getHeaderValue().toString()); |
|
//System.out.println("labelWith "+label.getPreferredSize().width); |
|
if(label.getText().equals(" ")) |
|
model.getColumn(i).setPreferredWidth(20); |
|
else |
model.getColumn(i).setPreferredWidth(label.getPreferredSize().width + 50); |
model.getColumn(i).setPreferredWidth(label.getPreferredSize().width + 50); |
|
|
} |
} |
Line 334 public class TableComponent extends JPan
|
Line 319 public class TableComponent extends JPan
|
} |
} |
public void sizeToFit(int windowWidth, int windowHeight) |
public void sizeToFit(int windowWidth, int windowHeight) |
{ |
{ |
table.setAutoResizeMode(table.AUTO_RESIZE_LAST_COLUMN); |
//table.setAutoResizeMode(table.AUTO_RESIZE_LAST_COLUMN); |
|
|
TableColumnModel model = table.getColumnModel(); |
TableColumnModel model = table.getColumnModel(); |
Dimension d2 = getPreferredSize(); |
Dimension d2 = getPreferredSize(); |
Line 393 public class TableComponent extends JPan
|
Line 378 public class TableComponent extends JPan
|
//tableModel.fireTableCellUpdated(row,column); |
//tableModel.fireTableCellUpdated(row,column); |
if(box.getModel().getSize()>0) |
if(box.getModel().getSize()>0) |
return boxValue; |
return boxValue; |
else return null; |
else |
|
return null; |
|
|
} |
} |
|
|
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) |
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) |
{ |
{ |
boxValue = (value != null) ? (JComboBox) value : null; |
boxValue = (value != null) ? (JComboBox) value : null; |
this.row=row;this.column=column; |
this.row = row; |
if(boxValue!=null) box.setModel(boxValue.getModel()); |
this.column = column; |
else return null; |
if (boxValue != null) |
|
box.setModel(boxValue.getModel()); |
|
else |
|
return null; |
return box; |
return box; |
} |
} |
|
|
Line 478 public class TableComponent extends JPan
|
Line 467 public class TableComponent extends JPan
|
CellEditorListener listener=table; |
CellEditorListener listener=table; |
JLabel label = new JLabel(); |
JLabel label = new JLabel(); |
int x=0,y=0; |
int x=0,y=0; |
String copy="";boolean removeCalled=true; |
String copy = ""; |
|
boolean removeCalled = true; |
public SQLCommandEditor() |
public SQLCommandEditor() |
{ |
{ |
// super(new JCheckBox()); |
// super(new JCheckBox()); |
//this.list = list; |
//this.list = list; |
editor.getDocument().addDocumentListener(new DocumentListener() { |
editor.getDocument().addDocumentListener(new DocumentListener() |
|
{ |
|
|
public void insertUpdate(DocumentEvent e) |
public void insertUpdate(DocumentEvent e) |
{ |
{ |
Line 512 public class TableComponent extends JPan
|
Line 503 public class TableComponent extends JPan
|
} |
} |
|
|
}); |
}); |
table.addMouseListener(new MouseAdapter() { |
table.addMouseListener(new MouseAdapter() |
|
{ |
public void mouseEntered(MouseEvent e) |
public void mouseEntered(MouseEvent e) |
{ |
{ |
if(true) return; |
if (true) |
|
return; |
//System.out.println(e); |
//System.out.println(e); |
x = e.getX(); |
x = e.getX(); |
y = e.getY(); |
y = e.getY(); |
Line 525 public class TableComponent extends JPan
|
Line 518 public class TableComponent extends JPan
|
|
|
if(window!=null) |
if(window!=null) |
{ |
{ |
if(!window.isVisible()) return; |
if (!window.isVisible()) |
|
return; |
|
|
}else return; |
} else |
|
return; |
if(row!=table.getEditingRow()||col!=table.getEditingColumn()) |
if(row!=table.getEditingRow()||col!=table.getEditingColumn()) |
if (listener != null) |
if (listener != null) |
{ |
{ |
Line 543 public class TableComponent extends JPan
|
Line 538 public class TableComponent extends JPan
|
y = e.getY(); |
y = e.getY(); |
|
|
if(window!=null) |
if(window!=null) |
if(!window.isVisible()) return; |
if (!window.isVisible()) |
|
return; |
|
|
int row = table.rowAtPoint(new Point(x,y)); |
int row = table.rowAtPoint(new Point(x,y)); |
int col = table.columnAtPoint(new Point(x,y)); |
int col = table.columnAtPoint(new Point(x,y)); |
Line 556 public class TableComponent extends JPan
|
Line 552 public class TableComponent extends JPan
|
} |
} |
} |
} |
|
|
public void mouseExited(MouseEvent e) { |
public void mouseExited(MouseEvent e) |
|
{ |
// System.out.println(e); |
// System.out.println(e); |
|
|
} |
} |
Line 600 public class TableComponent extends JPan
|
Line 597 public class TableComponent extends JPan
|
// if(label.isShowing()) |
// if(label.isShowing()) |
// System.out.println( label.getLocationOnScreen()); |
// System.out.println( label.getLocationOnScreen()); |
|
|
|
|
Dimension dimEditor = editor.getPreferredSize(); |
Dimension dimEditor = editor.getPreferredSize(); |
if (dimEditor.width < 260) |
if (dimEditor.width < 260) |
dimEditor.width = 260; |
dimEditor.width = 260; |
window.setSize((dimEditor.width < 280) ? dimEditor.width + 20 : 280, (dimEditor.height > 50) ? 200 : 60); |
window.setSize((dimEditor.width < 280) ? dimEditor.width + 20 : 280, (dimEditor.height > 50) ? 200 : 70); |
TableColumnModel model = table.getColumnModel(); |
TableColumnModel model = table.getColumnModel(); |
int offset_x = 0, offset_y = 0; |
int offset_x = 0, offset_y = 0; |
for (int i = 0; i <= column; ++i) |
for (int i = 0; i <= column; ++i) |
Line 679 public class TableComponent extends JPan
|
Line 675 public class TableComponent extends JPan
|
|
|
} |
} |
|
|
|
|
class ArrayListEditor implements TableCellEditor |
class ArrayListEditor implements TableCellEditor |
{ |
{ |
JList list = new JList(); |
JList list = new JList(); |
Line 693 public class TableComponent extends JPan
|
Line 688 public class TableComponent extends JPan
|
{ |
{ |
// super(new JCheckBox()); |
// super(new JCheckBox()); |
//this.list = list; |
//this.list = list; |
table.addMouseListener(new MouseAdapter() { |
table.addMouseListener(new MouseAdapter() |
|
{ |
public void mouseEntered(MouseEvent e) |
public void mouseEntered(MouseEvent e) |
{ |
{ |
// if(true) return; |
// if(true) return; |
Line 706 public class TableComponent extends JPan
|
Line 702 public class TableComponent extends JPan
|
|
|
if(window!=null) |
if(window!=null) |
{ |
{ |
if(!window.isVisible()) return; |
if (!window.isVisible()) |
|
return; |
|
|
} else return; |
} else |
|
return; |
|
|
if(row!=table.getEditingRow()||col!=table.getEditingColumn()) |
if(row!=table.getEditingRow()||col!=table.getEditingColumn()) |
if (listener != null) |
if (listener != null) |
Line 725 public class TableComponent extends JPan
|
Line 723 public class TableComponent extends JPan
|
y = e.getY(); |
y = e.getY(); |
|
|
if(window!=null) |
if(window!=null) |
if(!window.isVisible()) return; |
if (!window.isVisible()) |
|
return; |
|
|
int row = table.rowAtPoint(new Point(x,y)); |
int row = table.rowAtPoint(new Point(x,y)); |
int col = table.columnAtPoint(new Point(x,y)); |
int col = table.columnAtPoint(new Point(x,y)); |
Line 738 public class TableComponent extends JPan
|
Line 737 public class TableComponent extends JPan
|
} |
} |
} |
} |
|
|
public void mouseExited(MouseEvent e) { |
public void mouseExited(MouseEvent e) |
|
{ |
// System.out.println(e); |
// System.out.println(e); |
|
|
} |
} |
Line 794 public class TableComponent extends JPan
|
Line 794 public class TableComponent extends JPan
|
// if(label.isShowing()) |
// if(label.isShowing()) |
// System.out.println( label.getLocationOnScreen()); |
// System.out.println( label.getLocationOnScreen()); |
|
|
|
|
Dimension dimEditor = pane.getPreferredSize(); |
Dimension dimEditor = pane.getPreferredSize(); |
window.setSize((dimEditor.width<250) ? dimEditor.width+20:250,(dimEditor.height>50) ? 200:50); |
window.setSize((dimEditor.width<250) ? dimEditor.width+20:250,(dimEditor.height>50) ? 200:50); |
TableColumnModel model = table.getColumnModel(); |
TableColumnModel model = table.getColumnModel(); |
Line 872 public class TableComponent extends JPan
|
Line 871 public class TableComponent extends JPan
|
JLabel label = new JLabel(); |
JLabel label = new JLabel(); |
DefaultListModel model=new DefaultListModel(); |
DefaultListModel model=new DefaultListModel(); |
JScrollPane listScroller=new JScrollPane(list); |
JScrollPane listScroller=new JScrollPane(list); |
|
Color noIDColor=new Color(50,50,200); |
|
|
public VectorCellRenderer() |
public VectorCellRenderer() |
{ |
{ |
Line 883 public class TableComponent extends JPan
|
Line 883 public class TableComponent extends JPan
|
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) |
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) |
{ |
{ |
box = (JComboBox) value ; |
box = (JComboBox) value ; |
if(box==null) return null;//label.setText(""); |
if (box == null) |
else |
return null; //label.setText(""); |
if(box.getSelectedItem()!=null) |
else if (box.getSelectedItem() != null) |
{ |
{ |
String text=box.getSelectedItem().toString(); |
String text=box.getSelectedItem().toString(); |
if(text=="") |
if(text=="") |
{ |
{ |
|
if (box instanceof IDComboBox) |
|
{ |
|
text = "\"no ID\""; |
|
label.setForeground(noIDColor); |
|
} else |
|
{ |
text="\"no Layout\""; |
text="\"no Layout\""; |
label.setForeground(Color.red); |
label.setForeground(Color.red); |
|
} |
|
} else |
|
{ |
|
if (box instanceof IDComboBox) |
|
label.setForeground(Color.darkGray); |
|
|
}else label.setForeground(Color.blue); |
else |
|
label.setForeground(Color.blue); |
|
} |
label.setText(text); |
label.setText(text); |
|
|
} |
} else |
else label.setText(""); |
label.setText(""); |
return label; |
return label; |
} |
} |
|
|
Line 1056 return label;
|
Line 1069 return label;
|
} |
} |
return alterMe; |
return alterMe; |
} |
} |
|
public static class BooleanRenderer implements TableCellRenderer |
|
{ |
|
Icon trueIcon,falseIcon,empty; |
|
// renders false state with empty icon |
|
boolean renderFalseEmpty=false; |
|
public BooleanRenderer(){ |
|
try |
|
{ |
|
trueIcon = new ImageIcon(getClass().getResource("icons/ok.gif")); |
|
|
|
falseIcon = new Icon() |
|
{ |
|
public int getIconHeight() |
|
{ |
|
return 23; |
|
} |
|
public int getIconWidth() |
|
{ |
|
return 27; |
|
} |
|
public void paintIcon(Component c, Graphics g, int x, int y) |
|
{ |
|
Graphics2D g2 = (Graphics2D) g; |
|
g2.setColor(Color.blue); |
|
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); |
|
g2.setStroke(new BasicStroke(2.0f)); |
|
int xr = x + 20, yr = y + 15; |
|
int xl = x + 5, yl = y + 5; |
|
g2.drawLine(xl, yl, xr, yr); |
|
g2.drawLine(xl, yr, xr, yl); |
|
|
|
} |
|
}; |
|
empty = new Icon() |
|
{ |
|
public int getIconHeight() |
|
{ |
|
return 23; |
|
} |
|
public int getIconWidth() |
|
{ |
|
return 27; |
|
} |
|
public void paintIcon(Component c, Graphics g, int x, int y) |
|
{ |
|
|
|
} |
|
}; |
|
|
|
} catch (Exception e) |
|
{ |
|
System.out.println("Error while loading ok.gif"); |
|
} |
|
|
|
|
|
} |
|
JCheckBox checkBox = new JCheckBox(); |
|
JLabel label = new JLabel(); |
|
|
|
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) |
|
{ |
|
if (value instanceof Integer) |
|
{ |
|
label.setText(value.toString()); |
|
label.setHorizontalAlignment(JLabel.RIGHT); |
|
|
|
return label; |
|
} else if (value instanceof String) |
|
{ |
|
label.setText(value.toString()); |
|
label.setHorizontalAlignment(JLabel.CENTER); |
|
return label; |
|
} |
|
checkBox.setHorizontalAlignment(JLabel.CENTER); |
|
checkBox.setVerticalAlignment(JLabel.CENTER); |
|
|
|
checkBox.setSelectedIcon(trueIcon); |
|
if(renderFalseEmpty|| value==null) |
|
checkBox.setIcon(empty); |
|
else checkBox.setIcon(falseIcon); |
|
checkBox.setForeground(table.getForeground()); |
|
checkBox.setBackground(table.getBackground()); |
|
|
|
checkBox.setSelected((value != null && ((Boolean) value).booleanValue())); |
|
return checkBox; |
|
|
|
} |
|
} |
public static class SQLCommand |
public static class SQLCommand |
{ |
{ |
String command = ""; |
String command = ""; |
Line 1072 return label;
|
Line 1173 return label;
|
return command; |
return command; |
} |
} |
} |
} |
|
public static class IDComboBox extends JComboBox |
|
{ |
|
public IDComboBox(Vector vec) |
|
{ |
|
super(vec); |
|
} |
|
|
} |
} |
|
|
|
} |
|
|