Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 779aefe9278132bfec43e3c16a66c5baa2cb107d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*******************************************************************************
 * Copyright (c) 2011 Wind River Systems, Inc. and others. All rights reserved.
 * This program and the accompanying materials are made available under the terms
 * of the Eclipse Public License v1.0 which accompanies this distribution, and is
 * available at http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tcf.te.ui.trees;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.eclipse.core.runtime.Assert;
import org.eclipse.ui.IMemento;

/**
 * The class to describe the tree viewer's state including the visiblity of the 
 * tree columns and the enablement of the viewer filters.
 */
class TreeViewerState {
	// The state of the column's visibility.
	private List<ColumnState> columns;
	// The state of the filter's enablement.
	private List<FilterState> filters;

	/**
	 * Create an instance.
	 */
	public TreeViewerState() {
		columns = Collections.synchronizedList(new ArrayList<ColumnState>());
		filters = Collections.synchronizedList(new ArrayList<FilterState>());
	}

	/**
	 * Restore the viewer's state using the specified memento.
	 * 
	 * @param aMemento The memento to restore the viewer's state.
	 */
	public void restoreState(IMemento aMemento) {
		IMemento[] mColumns = aMemento.getChildren("column"); //$NON-NLS-1$
		if (mColumns != null && mColumns.length > 0) {
			for (IMemento mColumn : mColumns) {
				String columnId = mColumn.getString("id"); //$NON-NLS-1$
				Assert.isNotNull(columnId);
				Boolean value = mColumn.getBoolean("visible"); //$NON-NLS-1$
				boolean visible = value != null && value.booleanValue();
				Integer integer = mColumn.getInteger("width"); //$NON-NLS-1$
				int width = integer.intValue();
				integer = mColumn.getInteger("order"); //$NON-NLS-1$
				int order = integer.intValue();
				ColumnState column = new ColumnState();
				column.setColumnId(columnId);
				column.setVisible(visible);
				column.setWidth(width);
				column.setOrder(order);
				columns.add(column);
			}
		}
		IMemento[] mFilters = aMemento.getChildren("filter"); //$NON-NLS-1$
		if (mFilters != null && mFilters.length > 0) {
			for (IMemento mFilter : mFilters) {
				String filterId = mFilter.getString("id"); //$NON-NLS-1$
				Assert.isNotNull(filterId);
				Boolean value = mFilter.getBoolean("enabled"); //$NON-NLS-1$
				boolean enabled = value != null && value.booleanValue();
				FilterState filter = new FilterState();
				filter.setFilterId(filterId);
				filter.setEnabled(enabled);
				filters.add(filter);
			}
		}
	}

	/**
	 * Save the viewer's state to the specified memento.
	 * 
	 * @param aMemento The memento to save the viewer's state to.
	 */
	public void saveState(IMemento aMemento) {
		if (columns != null) {
			for (ColumnState column : columns) {
				String columnId = column.getColumnId();
				IMemento mColumn = aMemento.createChild("column"); //$NON-NLS-1$
				mColumn.putString("id", columnId); //$NON-NLS-1$
				boolean visible = column.isVisible();
				mColumn.putBoolean("visible", visible); //$NON-NLS-1$
				int width = column.getWidth();
				mColumn.putInteger("width", width); //$NON-NLS-1$
				int order = column.getOrder();
				mColumn.putInteger("order", order); //$NON-NLS-1$
			}
		}
		if (filters != null) {
			for (FilterState filter : filters) {
				IMemento mFilter = aMemento.createChild("filter"); //$NON-NLS-1$
				mFilter.putString("id", filter.getFilterId()); //$NON-NLS-1$
				boolean enabled = filter.isEnabled();
				mFilter.putBoolean("enabled", enabled); //$NON-NLS-1$
			}
		}
	}

	/**
	 * Add a column state based on the specified column descriptor.
	 * 
	 * @param column The column's descriptor.
	 */
	public void addColumn(ColumnDescriptor column) {
		ColumnState state = new ColumnState();
		state.setColumnId(column.getId());
		state.setVisible(column.isVisible());
		state.setWidth(column.getWidth());
		state.setOrder(column.getOrder());
		columns.add(state);
    }

	/**
	 * Add a filter state based on the specified filter descriptor.
	 * 
	 * @param filter The filter's state.
	 */
	public void addFilter(FilterDescriptor filter) {
		FilterState state = new FilterState();
		state.setFilterId(filter.getId());
		state.setEnabled(filter.isEnabled());
		filters.add(state);
    }

	/**
	 * Get the column's state in a list.
	 * 
	 * @return The column's state list.
	 */
	public List<ColumnState> getColumnStates() {
		return columns;
	}
	
	/**
	 * Update the column descriptors using the current column states.
	 * 
	 * @param columnDescriptors The column descriptors to be updated.
	 */
	public void updateColumnDescriptor(ColumnDescriptor[] columnDescriptors) {
		if (columnDescriptors != null) {
			for(int i=0;i<columns.size();i++) {
				ColumnDescriptor columnDescriptor = columnDescriptors[i];
				ColumnState columnState = columns.get(i);
				columnDescriptor.setVisible(columnState.isVisible());
				columnDescriptor.setWidth(columnState.getWidth());
				columnDescriptor.setOrder(columnState.getOrder());
			}
		}
    }

	/**
	 * Update the filter descriptors using the current filter states.
	 * 
	 * @param filterDescriptors The filter descriptors to be updated.
	 */
	public void updateFilterDescriptor(FilterDescriptor[] filterDescriptors) {
		if (filterDescriptors != null) {
			for (int i=0;i<filters.size();i++) {
				FilterDescriptor filterDescriptor = filterDescriptors[i];
				FilterState filterState = filters.get(i);
				filterDescriptor.setEnabled(filterState.isEnabled());
			}
		}
    }

	/**
	 * Update the current column states using the specified column descriptors.
	 * 
	 * @param columnDescriptors The column descriptors which are used to update the column states.
	 */
	public void updateColumnState(ColumnDescriptor[] columnDescriptors) {
		if(columnDescriptors != null) {
			for(int i=0;i<columns.size();i++) {
				ColumnDescriptor columnDescriptor = columnDescriptors[i];
				ColumnState columnState = columns.get(i);
				columnState.setVisible(columnDescriptor.isVisible());
				columnState.setWidth(columnDescriptor.getWidth());
				columnState.setOrder(columnDescriptor.getOrder());
			}
		}
    }

	/**
	 * Update the current filter states using the specified filter descriptors.
	 * 
	 * @param filterDescriptors The filter descriptors which are used to update the filter states.
	 */
	public void updateFilterState(FilterDescriptor[] filterDescriptors) {
		if (filterDescriptors != null) {
			for(int i=0;i<filters.size();i++) {
				FilterDescriptor filterDescriptor = filterDescriptors[i];
				FilterState filterState = filters.get(i);
				filterState.setEnabled(filterDescriptor.isEnabled());
			}
		}	    
    }
}

Back to the top