OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 137 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 137 Rev 184
Line 31... Line 31...
31
 
31
 
32
    /*
32
    /*
33
     * Specify the number of columns to be fixed and the scroll pane containing the table.
33
     * Specify the number of columns to be fixed and the scroll pane containing the table.
34
     */
34
     */
35
    public FixedColumnTable(int fixedColumns, TableModel model) {
35
    public FixedColumnTable(int fixedColumns, TableModel model) {
36
        main = new JTable(model);
36
        this.main = new JTable(model);
37
        this.scrollPane = new JScrollPane(main);
37
        this.scrollPane = new JScrollPane(this.main);
38
 
38
 
39
        main.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
39
        this.main.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
40
        main.setAutoCreateColumnsFromModel(false);
40
        this.main.setAutoCreateColumnsFromModel(false);
41
        main.addPropertyChangeListener(this);
41
        this.main.addPropertyChangeListener(this);
42
 
42
 
43
        // Use the existing table to create a new table sharing
43
        // Use the existing table to create a new table sharing
44
        // the DataModel and ListSelectionModel
44
        // the DataModel and ListSelectionModel
45
        fixed = new JTable();
45
        this.fixed = new JTable();
46
        fixed.setAutoCreateColumnsFromModel(false);
46
        this.fixed.setAutoCreateColumnsFromModel(false);
47
        fixed.setModel(main.getModel());
47
        this.fixed.setModel(this.main.getModel());
48
        fixed.setSelectionModel(main.getSelectionModel());
48
        this.fixed.setSelectionModel(this.main.getSelectionModel());
49
        fixed.setFocusable(false);
49
        this.fixed.setFocusable(false);
50
 
50
 
51
        // Remove the fixed columns from the main table
51
        // Remove the fixed columns from the main table
52
        // and add them to the fixed table
52
        // and add them to the fixed table
53
        for (int i = 0; i < fixedColumns; i++) {
53
        for (int i = 0; i < fixedColumns; i++) {
54
            TableColumnModel columnModel = main.getColumnModel();
54
            TableColumnModel columnModel = this.main.getColumnModel();
55
            TableColumn column = columnModel.getColumn(0);
55
            TableColumn column = columnModel.getColumn(0);
56
            columnModel.removeColumn(column);
56
            columnModel.removeColumn(column);
57
            fixed.getColumnModel().addColumn(column);
57
            this.fixed.getColumnModel().addColumn(column);
58
        }
58
        }
59
 
59
 
60
        // Add the fixed table to the scroll pane
60
        // Add the fixed table to the scroll pane
61
        fixed.setPreferredScrollableViewportSize(fixed.getPreferredSize());
61
        this.fixed.setPreferredScrollableViewportSize(this.fixed.getPreferredSize());
62
        scrollPane.setRowHeaderView(fixed);
62
        this.scrollPane.setRowHeaderView(this.fixed);
63
        scrollPane.setCorner(JScrollPane.UPPER_LEFT_CORNER, fixed.getTableHeader());
63
        this.scrollPane.setCorner(JScrollPane.UPPER_LEFT_CORNER, this.fixed.getTableHeader());
64
 
64
 
65
        // Synchronize scrolling of the row header with the main table
65
        // Synchronize scrolling of the row header with the main table
66
        scrollPane.getRowHeader().addChangeListener(this);
66
        this.scrollPane.getRowHeader().addChangeListener(this);
67
 
67
 
68
        fixed.getTableHeader().addMouseListener(new MouseAdapter() {
68
        this.fixed.getTableHeader().addMouseListener(new MouseAdapter() {
69
            TableColumn column;
69
            TableColumn column;
70
            int columnWidth;
70
            int columnWidth;
71
            int pressedX;
71
            int pressedX;
72
 
72
 
73
            public void mousePressed(MouseEvent e) {
73
            public void mousePressed(MouseEvent e) {
74
                JTableHeader header = (JTableHeader) e.getComponent();
74
                JTableHeader header = (JTableHeader) e.getComponent();
75
                TableColumnModel tcm = header.getColumnModel();
75
                TableColumnModel tcm = header.getColumnModel();
76
                int columnIndex = tcm.getColumnIndexAtX(e.getX());
76
                int columnIndex = tcm.getColumnIndexAtX(e.getX());
77
                Cursor cursor = header.getCursor();
77
                Cursor cursor = header.getCursor();
78
                if (columnIndex == tcm.getColumnCount() - 1 && cursor == Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR)) {
78
                if (columnIndex == tcm.getColumnCount() - 1 && cursor == Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR)) {
79
                    column = tcm.getColumn(columnIndex);
79
                    this.column = tcm.getColumn(columnIndex);
80
                    columnWidth = column.getWidth();
80
                    this.columnWidth = this.column.getWidth();
81
                    pressedX = e.getX();
81
                    this.pressedX = e.getX();
82
                    header.addMouseMotionListener(this);
82
                    header.addMouseMotionListener(this);
83
                }
83
                }
84
            }
84
            }
85
 
85
 
86
            public void mouseReleased(MouseEvent e) {
86
            public void mouseReleased(MouseEvent e) {
87
                JTableHeader header = (JTableHeader) e.getComponent();
87
                JTableHeader header = (JTableHeader) e.getComponent();
88
                header.removeMouseMotionListener(this);
88
                header.removeMouseMotionListener(this);
89
            }
89
            }
90
 
90
 
91
            public void mouseDragged(MouseEvent e) {
91
            public void mouseDragged(MouseEvent e) {
92
                int width = columnWidth - pressedX + e.getX();
92
                int width = this.columnWidth - this.pressedX + e.getX();
93
                column.setPreferredWidth(width);
93
                this.column.setPreferredWidth(width);
94
                JTableHeader header = (JTableHeader) e.getComponent();
94
                JTableHeader header = (JTableHeader) e.getComponent();
95
                JTable table = header.getTable();
95
                JTable table = header.getTable();
96
                table.setPreferredScrollableViewportSize(table.getPreferredSize());
96
                table.setPreferredScrollableViewportSize(table.getPreferredSize());
97
                JScrollPane scrollPane = (JScrollPane) table.getParent().getParent();
97
                JScrollPane scrollPane = (JScrollPane) table.getParent().getParent();
98
                scrollPane.revalidate();
98
                scrollPane.revalidate();
99
            }
99
            }
100
        });
100
        });
101
        this.setLayout(new GridLayout(1, 1));
101
        this.setLayout(new GridLayout(1, 1));
102
        this.add(scrollPane);
102
        this.add(this.scrollPane);
103
 
103
 
104
        main.getModel().addTableModelListener(new TableModelListener() {
104
        this.main.getModel().addTableModelListener(new TableModelListener() {
105
 
105
 
106
            @Override
106
            @Override
107
            public void tableChanged(TableModelEvent e) {
107
            public void tableChanged(TableModelEvent e) {
108
                // Reload column names
108
                // Reload column names
109
                final TableColumnModel columnModel = main.getColumnModel();
109
                final TableColumnModel columnModel = FixedColumnTable.this.main.getColumnModel();
110
                final int size = columnModel.getColumnCount();
110
                final int size = columnModel.getColumnCount();
111
                for (int i = 0; i < size; i++) {
111
                for (int i = 0; i < size; i++) {
112
                    columnModel.getColumn(i).setHeaderValue(main.getModel().getColumnName(i + 1));
112
                    columnModel.getColumn(i).setHeaderValue(FixedColumnTable.this.main.getModel().getColumnName(i + 1));
113
                }
113
                }
114
                main.getTableHeader().repaint();
114
                FixedColumnTable.this.main.getTableHeader().repaint();
115
            }
115
            }
116
        });
116
        });
117
 
117
 
118
    }
118
    }
119
 
119
 
120
    public void stateChanged(ChangeEvent e) {
120
    public void stateChanged(ChangeEvent e) {
121
        // Sync the scroll pane scrollbar with the row header
121
        // Sync the scroll pane scrollbar with the row header
122
        JViewport viewport = (JViewport) e.getSource();
122
        JViewport viewport = (JViewport) e.getSource();
123
        scrollPane.getVerticalScrollBar().setValue(viewport.getViewPosition().y);
123
        this.scrollPane.getVerticalScrollBar().setValue(viewport.getViewPosition().y);
124
    }
124
    }
125
 
125
 
126
    public void propertyChange(PropertyChangeEvent e) {
126
    public void propertyChange(PropertyChangeEvent e) {
127
        // Keep the fixed table in sync with the main table
127
        // Keep the fixed table in sync with the main table
128
        if ("selectionModel".equals(e.getPropertyName())) {
128
        if ("selectionModel".equals(e.getPropertyName())) {
129
            fixed.setSelectionModel(main.getSelectionModel());
129
            this.fixed.setSelectionModel(this.main.getSelectionModel());
130
        }
130
        }
131
        if ("model".equals(e.getPropertyName())) {
131
        if ("model".equals(e.getPropertyName())) {
132
            fixed.setModel(main.getModel());
132
            this.fixed.setModel(this.main.getModel());
133
        }
133
        }
134
    }
134
    }
135
 
135
 
136
    public void setRowHeight(int height) {
136
    public void setRowHeight(int height) {
137
        this.fixed.setRowHeight(height);
137
        this.fixed.setRowHeight(height);
Line 149... Line 149...
149
    }
149
    }
150
 
150
 
151
    public void setColumnWidth(int index, int width) {
151
    public void setColumnWidth(int index, int width) {
152
        getColumn(index).setWidth(width);
152
        getColumn(index).setWidth(width);
153
        if (index == 0) {
153
        if (index == 0) {
154
            final Dimension preferredSize = fixed.getPreferredSize();
154
            final Dimension preferredSize = this.fixed.getPreferredSize();
155
            preferredSize.setSize(width, preferredSize.getHeight());
155
            preferredSize.setSize(width, preferredSize.getHeight());
156
            fixed.setPreferredScrollableViewportSize(preferredSize);
156
            this.fixed.setPreferredScrollableViewportSize(preferredSize);
157
            JScrollPane scrollPane = (JScrollPane) fixed.getParent().getParent();
157
            JScrollPane scrollPane = (JScrollPane) this.fixed.getParent().getParent();
158
            scrollPane.revalidate();
158
            scrollPane.revalidate();
159
        }
159
        }
160
    }
160
    }
161
 
161
 
162
    public void setColumnMinWidth(int index, int width) {
162
    public void setColumnMinWidth(int index, int width) {
Line 166... Line 166...
166
    public void setColumnMaxWidth(int index, int width) {
166
    public void setColumnMaxWidth(int index, int width) {
167
        getColumn(index).setMaxWidth(width);
167
        getColumn(index).setMaxWidth(width);
168
    }
168
    }
169
 
169
 
170
    public TableColumn getColumn(int index) {
170
    public TableColumn getColumn(int index) {
171
        final int columnCount = fixed.getColumnCount();
171
        final int columnCount = this.fixed.getColumnCount();
172
        if (index < columnCount) {
172
        if (index < columnCount) {
173
            return this.fixed.getColumnModel().getColumn(index);
173
            return this.fixed.getColumnModel().getColumn(index);
174
        } else {
174
        } else {
175
            return this.main.getColumnModel().getColumn(index - columnCount);
175
            return this.main.getColumnModel().getColumn(index - columnCount);
176
        }
176
        }
Line 192... Line 192...
192
    public int getSelectedRow() {
192
    public int getSelectedRow() {
193
        return this.main.getSelectedRow();
193
        return this.main.getSelectedRow();
194
    }
194
    }
195
 
195
 
196
    public void ensureVisible(int row, int column) {
196
    public void ensureVisible(int row, int column) {
197
        main.scrollRectToVisible(main.getCellRect(row, column, true));
197
        this.main.scrollRectToVisible(this.main.getCellRect(row, column, true));
198
    }
198
    }
199
 
199
 
200
    public int rowAtPoint(Point point) {
200
    public int rowAtPoint(Point point) {
201
        return this.main.rowAtPoint(point);
201
        return this.main.rowAtPoint(point);
202
    }
202
    }
Line 208... Line 208...
208
    public void setToolTipTextOnHeader(String text) {
208
    public void setToolTipTextOnHeader(String text) {
209
        this.main.setToolTipText(text);
209
        this.main.setToolTipText(text);
210
    }
210
    }
211
 
211
 
212
    public void setReorderingAllowed(boolean b) {
212
    public void setReorderingAllowed(boolean b) {
213
        main.getTableHeader().setReorderingAllowed(b);
213
        this.main.getTableHeader().setReorderingAllowed(b);
214
    }
214
    }
215
 
215
 
-
 
216
    public JTable getMain() {
-
 
217
        return this.main;
-
 
218
    }
-
 
219
 
-
 
220
    public JTable getFixed() {
-
 
221
        return this.fixed;
-
 
222
    }
216
}
223
}
217
224