View Javadoc

1   /**
2    * Copyright (C) 2008 Atlassian
3    *
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
7    *
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    *
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.
15   */
16  
17  package com.atlassian.theplugin.idea.ui;
18  
19  import com.atlassian.theplugin.idea.TableColumnInfo;
20  import com.intellij.ui.table.TableView;
21  import com.intellij.util.config.Storage;
22  import com.intellij.util.ui.ListTableModel;
23  
24  import javax.swing.event.TableModelEvent;
25  import javax.swing.table.TableCellRenderer;
26  import javax.swing.table.TableColumnModel;
27  import javax.swing.*;
28  import java.awt.event.MouseAdapter;
29  import java.awt.event.MouseEvent;
30  import java.awt.*;
31  import java.util.*;
32  import java.util.List;
33  
34  public class AtlassianTableView extends TableView {
35  	private static final int DEFAULT_ROW_HEIGHT = 20;
36  	private boolean autoAdjustHeight = true;
37  	private static final int MAX_DISPLAYED_ROW_COUNT = 15;
38  	private final java.util.List<TableItemSelectedListener> listenerList = new ArrayList<TableItemSelectedListener>();
39  	private UserTableContext state = new UserTableContext();
40  
41  
42  	public AtlassianTableView(TableColumnProvider columnProvider, ListTableModel listTableModel, final Storage storage) {
43  		super(listTableModel);
44  		setBorder(BorderFactory.createEmptyBorder());
45  		getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
46  		getColumnModel().setColumnMargin(0);
47  
48  		setMinRowHeight(DEFAULT_ROW_HEIGHT);
49  		setAutoResizeMode(TableView.AUTO_RESIZE_OFF);
50  		prepareColumns(columnProvider);
51  		getTableHeader().addMouseListener(new MouseAdapter() {
52  			public void mouseReleased(MouseEvent e) {
53  				// stores table configuration in Storage object
54  				if (storage != null) {
55  					TableView.store(storage, AtlassianTableView.this);
56  				}
57  			}
58  		});
59  	}
60  
61  	public AtlassianTableView(TableColumnProvider columnProvider, ListTableModel listTableModel, final Storage storage,
62  							  final String popupMenuPlace, final String popupMenuName) {
63  		this(columnProvider, listTableModel, storage);
64  		if (popupMenuPlace != null && popupMenuName != null && popupMenuName.length() > 0) {
65  			addMouseListener(new ShowPopupMouseAdapter(this, popupMenuName));
66  		}
67  	}
68  
69  	public ListTableModel getListTableModel() {
70  		return super.getListTableModel();
71  	}
72  
73  
74  	public void prepareColumns(TableColumnProvider tableColumnProvider) {
75  		TableColumnInfo[] cols = tableColumnProvider.makeColumnInfo();
76  		TableCellRenderer[] renderers = tableColumnProvider.makeRendererInfo();
77  		TableColumnModel model = getColumnModel();
78  		for (int i = 0; i < model.getColumnCount(); ++i) {
79  			model.getColumn(i).setResizable(true);
80  			model.getColumn(i).setPreferredWidth(cols[i].getPrefferedWidth());
81  			if (renderers[i] != null) {
82  				model.getColumn(i).setCellRenderer(renderers[i]);
83  
84  			}
85  		}
86  	}
87  
88  	/**
89  	 * Restores table properties from Storage object into current table instance
90  	 *
91  	 * @param storage object with table properties
92  	 */
93  	public void restore(Storage storage) {
94  		if (storage != null) {
95  			TableView.restore(storage, this);
96  		}
97  	}
98  
99  	/**
100 	 * Stores current table properties into Storage object
101 	 *
102 	 * @param storage object to store table properties
103 	 */
104 	public void store(Storage storage) {
105 		TableView.store(storage, this);
106 	}
107 
108 
109 	public Dimension getTableDimension() {
110 		int tableHeight = 0, tableWidth = 0;
111 		tableHeight = Math.min(getModel().getRowCount(), MAX_DISPLAYED_ROW_COUNT) * getRowHeight();
112 		// Resize width
113 		for (int col = 0; col < getColumnModel().getColumnCount(); col++) {
114 			tableWidth += (getColumnModel().getColumn(col).getPreferredWidth());
115 		}
116 
117 		if (getTableHeader() != null) {
118 			Dimension tableHeaderDimension = getTableHeader().getPreferredSize();
119 			tableHeight += tableHeaderDimension.height;
120 		}
121 
122 		return new Dimension(tableWidth, tableHeight);
123 	}
124 
125 
126 	public void tableChanged(TableModelEvent e) {
127 
128 		Dimension prefered = getTableDimension();
129 		setPreferredScrollableViewportSize(prefered);
130 		super.tableChanged(e);
131 	}
132 
133 	public void addItemSelectedListener(TableItemSelectedListener listener) {
134 		listenerList.add(listener);
135 	}
136 
137 	public void removeItemSelectedListener(TableItemSelectedListener listener) {
138 		listenerList.remove(listener);
139 	}
140 
141 	public List<TableItemSelectedListener> getListenerList() {
142 		return Collections.unmodifiableList(listenerList);
143 	}
144 
145 	public UserTableContext getStateContext() {
146 		return state;
147 	}
148 
149 	public void setStateContext(UserTableContext context) {
150 		state = context;
151 	}
152 }