OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Compare Revisions

Ignore whitespace Rev 184 → Rev 183

/trunk/jOpenCalendar/src/org/jopencalendar/ui/FixedColumnTable.java
33,39 → 33,39
* Specify the number of columns to be fixed and the scroll pane containing the table.
*/
public FixedColumnTable(int fixedColumns, TableModel model) {
this.main = new JTable(model);
this.scrollPane = new JScrollPane(this.main);
main = new JTable(model);
this.scrollPane = new JScrollPane(main);
 
this.main.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
this.main.setAutoCreateColumnsFromModel(false);
this.main.addPropertyChangeListener(this);
main.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
main.setAutoCreateColumnsFromModel(false);
main.addPropertyChangeListener(this);
 
// Use the existing table to create a new table sharing
// the DataModel and ListSelectionModel
this.fixed = new JTable();
this.fixed.setAutoCreateColumnsFromModel(false);
this.fixed.setModel(this.main.getModel());
this.fixed.setSelectionModel(this.main.getSelectionModel());
this.fixed.setFocusable(false);
fixed = new JTable();
fixed.setAutoCreateColumnsFromModel(false);
fixed.setModel(main.getModel());
fixed.setSelectionModel(main.getSelectionModel());
fixed.setFocusable(false);
 
// Remove the fixed columns from the main table
// and add them to the fixed table
for (int i = 0; i < fixedColumns; i++) {
TableColumnModel columnModel = this.main.getColumnModel();
TableColumnModel columnModel = main.getColumnModel();
TableColumn column = columnModel.getColumn(0);
columnModel.removeColumn(column);
this.fixed.getColumnModel().addColumn(column);
fixed.getColumnModel().addColumn(column);
}
 
// Add the fixed table to the scroll pane
this.fixed.setPreferredScrollableViewportSize(this.fixed.getPreferredSize());
this.scrollPane.setRowHeaderView(this.fixed);
this.scrollPane.setCorner(JScrollPane.UPPER_LEFT_CORNER, this.fixed.getTableHeader());
fixed.setPreferredScrollableViewportSize(fixed.getPreferredSize());
scrollPane.setRowHeaderView(fixed);
scrollPane.setCorner(JScrollPane.UPPER_LEFT_CORNER, fixed.getTableHeader());
 
// Synchronize scrolling of the row header with the main table
this.scrollPane.getRowHeader().addChangeListener(this);
scrollPane.getRowHeader().addChangeListener(this);
 
this.fixed.getTableHeader().addMouseListener(new MouseAdapter() {
fixed.getTableHeader().addMouseListener(new MouseAdapter() {
TableColumn column;
int columnWidth;
int pressedX;
76,9 → 76,9
int columnIndex = tcm.getColumnIndexAtX(e.getX());
Cursor cursor = header.getCursor();
if (columnIndex == tcm.getColumnCount() - 1 && cursor == Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR)) {
this.column = tcm.getColumn(columnIndex);
this.columnWidth = this.column.getWidth();
this.pressedX = e.getX();
column = tcm.getColumn(columnIndex);
columnWidth = column.getWidth();
pressedX = e.getX();
header.addMouseMotionListener(this);
}
}
89,8 → 89,8
}
 
public void mouseDragged(MouseEvent e) {
int width = this.columnWidth - this.pressedX + e.getX();
this.column.setPreferredWidth(width);
int width = columnWidth - pressedX + e.getX();
column.setPreferredWidth(width);
JTableHeader header = (JTableHeader) e.getComponent();
JTable table = header.getTable();
table.setPreferredScrollableViewportSize(table.getPreferredSize());
99,19 → 99,19
}
});
this.setLayout(new GridLayout(1, 1));
this.add(this.scrollPane);
this.add(scrollPane);
 
this.main.getModel().addTableModelListener(new TableModelListener() {
main.getModel().addTableModelListener(new TableModelListener() {
 
@Override
public void tableChanged(TableModelEvent e) {
// Reload column names
final TableColumnModel columnModel = FixedColumnTable.this.main.getColumnModel();
final TableColumnModel columnModel = main.getColumnModel();
final int size = columnModel.getColumnCount();
for (int i = 0; i < size; i++) {
columnModel.getColumn(i).setHeaderValue(FixedColumnTable.this.main.getModel().getColumnName(i + 1));
columnModel.getColumn(i).setHeaderValue(main.getModel().getColumnName(i + 1));
}
FixedColumnTable.this.main.getTableHeader().repaint();
main.getTableHeader().repaint();
}
});
 
120,16 → 120,16
public void stateChanged(ChangeEvent e) {
// Sync the scroll pane scrollbar with the row header
JViewport viewport = (JViewport) e.getSource();
this.scrollPane.getVerticalScrollBar().setValue(viewport.getViewPosition().y);
scrollPane.getVerticalScrollBar().setValue(viewport.getViewPosition().y);
}
 
public void propertyChange(PropertyChangeEvent e) {
// Keep the fixed table in sync with the main table
if ("selectionModel".equals(e.getPropertyName())) {
this.fixed.setSelectionModel(this.main.getSelectionModel());
fixed.setSelectionModel(main.getSelectionModel());
}
if ("model".equals(e.getPropertyName())) {
this.fixed.setModel(this.main.getModel());
fixed.setModel(main.getModel());
}
}
 
151,10 → 151,10
public void setColumnWidth(int index, int width) {
getColumn(index).setWidth(width);
if (index == 0) {
final Dimension preferredSize = this.fixed.getPreferredSize();
final Dimension preferredSize = fixed.getPreferredSize();
preferredSize.setSize(width, preferredSize.getHeight());
this.fixed.setPreferredScrollableViewportSize(preferredSize);
JScrollPane scrollPane = (JScrollPane) this.fixed.getParent().getParent();
fixed.setPreferredScrollableViewportSize(preferredSize);
JScrollPane scrollPane = (JScrollPane) fixed.getParent().getParent();
scrollPane.revalidate();
}
}
168,7 → 168,7
}
 
public TableColumn getColumn(int index) {
final int columnCount = this.fixed.getColumnCount();
final int columnCount = fixed.getColumnCount();
if (index < columnCount) {
return this.fixed.getColumnModel().getColumn(index);
} else {
194,7 → 194,7
}
 
public void ensureVisible(int row, int column) {
this.main.scrollRectToVisible(this.main.getCellRect(row, column, true));
main.scrollRectToVisible(main.getCellRect(row, column, true));
}
 
public int rowAtPoint(Point point) {
210,14 → 210,7
}
 
public void setReorderingAllowed(boolean b) {
this.main.getTableHeader().setReorderingAllowed(b);
main.getTableHeader().setReorderingAllowed(b);
}
 
public JTable getMain() {
return this.main;
}
 
public JTable getFixed() {
return this.fixed;
}
}