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.settings;
21  
22  import org.xnap.commons.gui.table.SortableModel;
23  import org.xnap.commons.gui.table.TableLayout;
24  import org.xnap.commons.gui.table.TableLayoutListener;
25  
26  
27  /***
28   * @author Steffen Pingel 
29   */
30  public class TableSettingDirector implements TableLayoutListener
31  {
32  
33  	private EnumSetting<SortableModel.Order> sortOrderSetting;
34  	private IntSetting sortedColumnSetting;
35  	private IntArraySetting columnWidthsSetting;
36  	private StringArraySetting visibleColumnsSetting;
37  	private BooleanSetting maintainSortOrderSetting;
38  	private TableLayout tableLayout;
39  	
40  	public TableSettingDirector(SettingResource backend, String key, TableLayout tableLayout)
41  	{
42  		setTableLayout(tableLayout);
43  		
44  		columnWidthsSetting = new IntArraySetting(backend, "table." + key + ".columnWidths", null);
45  		sortOrderSetting = new EnumSetting<SortableModel.Order>(backend, "table." + key + ".sortOrder", SortableModel.Order.UNSORTED);
46  		sortedColumnSetting = new IntSetting(backend, "table." + key + ".sortedColumn", -1);
47  		visibleColumnsSetting = new StringArraySetting(backend, "table." + key + ".visibleColumns", new String[0]);
48  		maintainSortOrderSetting = new BooleanSetting(backend, "table." + key + ".maintainSortOrder", false);
49  	}
50  	
51  	public TableSettingDirector(SettingResource backend, String key)
52  	{
53  		this(backend, key, null);
54  	}
55  	
56  	public TableSettingDirector setDefaults(String[] visibleColumns) 
57  	{
58  		visibleColumnsSetting.setDefaultValue(visibleColumns);
59  		return this;
60  	}
61  	
62  	public void setTableLayout(TableLayout tableLayout) 
63  	{
64  		if (this.tableLayout != null) {
65  			this.tableLayout.removeTableLayoutListener(this);
66  		}
67  		this.tableLayout = tableLayout;
68  		if (this.tableLayout != null) {
69  			this.tableLayout.addTableLayoutListener(this);
70  		}
71  	}
72  
73  	public void restore(TableLayout tableLayout)
74  	{
75  		for (int i = 0; i < tableLayout.getColumnCount(); i++) {
76  			tableLayout.setColumnVisible(i, false);
77  		}
78  		String[] visibleColumns = visibleColumnsSetting.getValue();
79  		tableLayout.setAllColumnsVisible(false);
80  		boolean visible = false;
81  		for (int i = 0; i < visibleColumns.length; i++) {
82  			int index = tableLayout.getColumnIndex(visibleColumns[i]);
83  			if (index != -1) {
84  				tableLayout.setColumnVisible(index, true);
85  				visible = true;
86  			}
87  		}
88  		if (!visible && tableLayout.getColumnCount() > 0) {
89  			// make sure at least a single column is visible
90  			tableLayout.setColumnVisible(0, true);
91  		}
92  		
93  		Integer[] columnWidths = columnWidthsSetting.getValue();
94  		if (columnWidths != null) {
95  			for (int i = 0; i < columnWidths.length && i < tableLayout.getColumnCount(); i++) {
96  				tableLayout.getColumnAt(i).setPreferredWidth(columnWidths[i]);
97  				tableLayout.getColumnAt(i).setWidth(columnWidths[i]);			
98  			}
99  		}
100 		
101 		int column = sortedColumnSetting.getValue();
102 		if (column >= 0 && column < tableLayout.getColumnCount()) {	
103 			tableLayout.sortByColumn(sortedColumnSetting.getValue(),
104 					sortOrderSetting.getValue(), false);
105 		}
106 		
107 		tableLayout.setMaintainSortOrder(maintainSortOrderSetting.getValue());
108 	}
109 	
110 	public void restore() {
111 		restore(tableLayout);
112 	}
113 	
114 	public void save(TableLayout tableLayout) 
115 	{
116 		Integer[] widths = new Integer[tableLayout.getColumnCount()];
117 		int visibleColumnsCount = 0;
118 		for (int i = 0; i < widths.length; i++) {
119 			widths[i] = tableLayout.getColumnAt(i).getWidth();
120 		}
121 		columnWidthsSetting.setValue(widths);
122 
123 		String[] visibleColumns = new String[tableLayout.getVisibleColumnsCount()];
124 		for (int i = 0; i < visibleColumns.length; i++) {
125 			visibleColumns[i] = tableLayout.getTable().getColumnModel().getColumn(i).getIdentifier().toString();
126 		}
127 		visibleColumnsSetting.setValue(visibleColumns);
128 		
129 		sortOrderSetting.setValue(tableLayout.getSortOrder());
130 		sortedColumnSetting.setValue(tableLayout.getSortedColumn());
131 		
132 		maintainSortOrderSetting.setValue(tableLayout.getMaintainSortOrder());
133 	}
134 	
135 	public void save() {
136 		save(tableLayout);
137 	}
138 	
139 	public void columnLayoutChanged()
140 	{
141 		save();
142 	}
143 
144 	public void sortedColumnChanged()
145 	{
146 		save();
147 	}
148 
149 	public void columnOrderChanged()
150 	{
151 		save();
152 	}
153 
154 	public void columnNameChanged(int index, String newName)
155 	{
156 		// does not concern us as this is the user visible name, not the identifier
157 	}
158 
159 	public void columnVisibilityChanged(int index, boolean visible)
160 	{
161 		save();
162 	}
163 
164 	public void maintainSortOrderChanged(boolean newValue)
165 	{
166 	}
167 	
168 }