2 * Copyright 2000-2007 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com
.intellij
.ui
.table
;
18 import com
.intellij
.openapi
.diagnostic
.Logger
;
19 import com
.intellij
.util
.config
.Storage
;
20 import com
.intellij
.util
.ui
.ColumnInfo
;
21 import com
.intellij
.util
.ui
.ListTableModel
;
22 import com
.intellij
.util
.ui
.SortableColumnModel
;
23 import com
.intellij
.util
.ui
.Table
;
24 import org
.jetbrains
.annotations
.NonNls
;
27 import javax
.swing
.event
.TableModelEvent
;
28 import javax
.swing
.event
.TableModelListener
;
29 import javax
.swing
.table
.JTableHeader
;
30 import javax
.swing
.table
.TableColumn
;
31 import javax
.swing
.table
.TableColumnModel
;
32 import javax
.swing
.table
.TableModel
;
33 import java
.awt
.event
.MouseAdapter
;
34 import java
.awt
.event
.MouseEvent
;
35 import java
.util
.ArrayList
;
36 import java
.util
.Arrays
;
39 * Do NOT add code wich assumes that table has same number of rows as model. It isn't true!
41 public abstract class BaseTableView
extends Table
{
42 private static final Logger LOG
= Logger
.getInstance("#com.intellij.ui.table.BaseTableView");
44 public BaseTableView() {
45 this(new ListTableModel(ColumnInfo
.EMPTY_ARRAY
));
48 public BaseTableView(final ListTableModel model
) {
50 final JTableHeader tableHeader
= getTableHeader();
51 if (tableHeader
!= null) {
52 tableHeader
.addMouseListener(new MouseAdapter() {
53 public void mouseClicked(final MouseEvent e
) {
54 final int column
= convertColumnIndexToModel(tableHeader
.columnAtPoint(e
.getPoint()));
56 onHeaderClicked(column
);
61 model
.addTableModelListener(new TableModelListener(){
62 public void tableChanged(final TableModelEvent e
) {
63 JTableHeader header
= getTableHeader();
72 protected abstract void onHeaderClicked(int column
);
74 protected ListTableModel
getListTableModel() {
75 return (ListTableModel
) getModel();
78 public void setModel(final TableModel dataModel
) {
79 LOG
.assertTrue(dataModel
instanceof SortableColumnModel
);
80 super.setModel(dataModel
);
84 private static String
orderPropertyName(final int index
) {
89 private static String
widthPropertyName(final int index
) {
90 return "Width" + index
;
93 public static void store(final Storage storage
, final JTable table
) {
94 final TableColumnModel model
= table
.getTableHeader().getColumnModel();
95 final int columnCount
= model
.getColumnCount();
96 final boolean[] storedColumns
= new boolean[columnCount
];
97 Arrays
.fill(storedColumns
, false);
98 for (int i
= 0; i
< columnCount
; i
++) {
99 final TableColumn column
= model
.getColumn(i
);
100 storage
.put(widthPropertyName(i
), String
.valueOf(column
.getWidth()));
101 final int modelIndex
= column
.getModelIndex();
102 storage
.put(orderPropertyName(i
), String
.valueOf(modelIndex
));
103 if (storedColumns
[modelIndex
]) {
104 LOG
.assertTrue(false,
105 "columnCount: " + columnCount
+ " current: " + i
+ " modelINdex: " + modelIndex
);
107 storedColumns
[modelIndex
] = true;
111 public static void storeWidth(final Storage storage
, final TableColumnModel columns
) {
112 for (int i
= 0; i
< columns
.getColumnCount(); i
++) {
113 storage
.put(widthPropertyName(i
), String
.valueOf(columns
.getColumn(i
).getWidth()));
117 public static void restore(final Storage storage
, final JTable table
) {
118 final TableColumnModel columnModel
= table
.getTableHeader().getColumnModel();
120 final ArrayList
<String
> columnIndices
= new ArrayList
<String
>();
122 final String order
= storage
.get(orderPropertyName(index
));
123 if (order
== null) break;
124 columnIndices
.add(order
);
126 if (index
== table
.getColumnCount()) break;
129 for (final String columnIndex
: columnIndices
) {
130 columnModel
.moveColumn(indexbyModelIndex(columnModel
, Integer
.parseInt(columnIndex
)), index
);
133 for (int i
= 0; i
< columnIndices
.size(); i
++) {
134 final String width
= storage
.get(widthPropertyName(i
));
135 if (width
!= null && width
.length() > 0) {
137 columnModel
.getColumn(i
).setPreferredWidth(Integer
.parseInt(width
));
138 } catch(NumberFormatException e
) {
139 LOG
.error("Bad width: " + width
+ " at column: "+ i
+ " from: " + storage
+
140 " actual columns count: " + columnModel
.getColumnCount() +
141 " info count: " + columnIndices
.size(), e
);
147 public static void restoreWidth(final Storage storage
, final TableColumnModel columns
) {
148 for (int index
= 0; true; index
++) {
149 final String widthValue
= storage
.get(widthPropertyName(index
));
150 if (widthValue
== null) break;
152 columns
.getColumn(index
).setPreferredWidth(Integer
.parseInt(widthValue
));
153 } catch(NumberFormatException e
) {
154 LOG
.error("Bad width: " + widthValue
+ " at column: " + index
+ " from: " + storage
, e
);
159 private static int indexbyModelIndex(final TableColumnModel model
, final int index
) {
160 for (int i
= 0; i
< model
.getColumnCount(); i
++)
161 if (model
.getColumn(i
).getModelIndex() == index
)
163 LOG
.assertTrue(false, "Total: " + model
.getColumnCount() + " index: "+ index
);