View Javadoc

1   /*
2    *  XNap Commons
3    *
4    *  Copyright (C) 2005  Steffen Pingel
5    *
6    *  This library is free software; you can redistribute it and/or
7    *  modify it under the terms of the GNU Lesser General Public
8    *  License as published by the Free Software Foundation; either
9    *  version 2.1 of the License, or (at your option) any later version.
10   *
11   *  This library is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   *  Lesser General Public License for more details.
15   *
16   *  You should have received a copy of the GNU Lesser General Public
17   *  License along with this library; if not, write to the Free Software
18   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   */
20  package org.xnap.commons.gui.table;
21  
22  import java.awt.event.ActionEvent;
23  import java.util.ArrayList;
24  import java.util.List;
25  import javax.swing.JMenuItem;
26  import javax.swing.JTable;
27  import org.xnap.commons.gui.action.AbstractXNapAction;
28  import org.xnap.commons.i18n.I18nFactory;
29  import org.xnap.commons.settings.SettingStore;
30  
31  public class TableLayoutManager {
32  
33  	private List<ColumnProperties> columnProperties = new ArrayList<ColumnProperties>();
34  	private int currentIndex = 0;
35  	private List<String> defaultColumns = new ArrayList<String>();
36  	private ResetTableLayoutAction resetTableLayoutAction;
37  	private TableLayout tableLayout;
38  	
39  	public TableLayoutManager(JTable table)
40  	{
41  		this.tableLayout = new TableLayout(table);
42  		
43  		this.resetTableLayoutAction = new ResetTableLayoutAction();
44  		this.tableLayout.getHeaderPopupMenu().add(new JMenuItem(resetTableLayoutAction));
45  	}
46  	
47  	public void addColumnProperties(String key, String name, int width, boolean defaultColumn)
48  	{
49  		ColumnProperties props = new ColumnProperties();
50  		props.index = currentIndex;
51  		props.key = key;
52  		props.name = name;
53  		props.width = width;
54  		if (defaultColumn) {
55  			defaultColumns.add(key);
56  		}
57  		columnProperties.add(props);
58  
59  		currentIndex++;
60  	}
61  	
62  	private String[] getDefaultColumns()
63  	{
64  		return defaultColumns.toArray(new String[0]);
65  	}
66  	
67  	public ResetTableLayoutAction getResetTableLayoutAction()
68  	{
69  		return resetTableLayoutAction;
70  	}
71  
72  	public TableLayout getTableLayout()
73  	{
74  		return tableLayout;
75  	}
76  	
77  	public void initializeTableLayout() 
78  	{
79  		for (ColumnProperties props : columnProperties) {
80  			getTableLayout().setColumnProperties(props.index, props.key, props.width);
81  			getTableLayout().setColumnName(props.index, props.name);
82  		}
83  	}
84  	
85  	public void resetTableLayout() 
86  	{
87  		for (ColumnProperties props : columnProperties) {
88  			getTableLayout().setColumnProperties(props.index, props.key, props.width);
89  		}
90  		
91  		getTableLayout().setColumnsVisible(getDefaultColumns());	
92  		getTableLayout().getTable().getTableHeader().revalidate();
93  	}
94  
95  	public void restoreLayout(SettingStore store, String key) 
96  	{
97  		store.restoreTable(key, getDefaultColumns(), getTableLayout());
98  	}	
99  
100 	public void saveLayout(SettingStore store, String key) 
101 	{
102 		store.saveTable(key, getTableLayout());
103 	}
104 
105 	private static class ColumnProperties {
106 		boolean defaultColumn;
107 		int index;
108 		String key;
109 		public String name;
110 		int width;
111 	}
112 
113 	private class ResetTableLayoutAction extends AbstractXNapAction {
114 		
115 		public ResetTableLayoutAction() 
116 		{
117 			putValue(NAME, I18nFactory.getI18n(TableLayoutManager.class).
118 					tr("Reset Columns"));
119 		}
120 
121 		public void actionPerformed(ActionEvent event) 
122 		{
123 			resetTableLayout();
124 		}
125 		
126 	}
127 	
128 }