2 * Copyright (C) 2007 The Android Open Source Project
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.
17 package com.android.ddmuilib;
19 import org.eclipse.jface.preference.IPreferenceStore;
20 import org.eclipse.swt.events.ControlEvent;
21 import org.eclipse.swt.events.ControlListener;
22 import org.eclipse.swt.widgets.Table;
23 import org.eclipse.swt.widgets.TableColumn;
24 import org.eclipse.swt.widgets.Tree;
25 import org.eclipse.swt.widgets.TreeColumn;
28 * Utility class to help using Table objects.
31 public final class TableHelper {
33 * Create a TableColumn with the specified parameters. If a
34 * <code>PreferenceStore</code> object and a preference entry name String
35 * object are provided then the column will listen to change in its width
36 * and update the preference store accordingly.
38 * @param parent The Table parent object
39 * @param header The header string
40 * @param style The column style
41 * @param sample_text A sample text to figure out column width if preference
43 * @param pref_name The preference entry name for column width
44 * @param prefs The preference store
45 * @return The TableColumn object that was created
47 public static TableColumn createTableColumn(Table parent, String header,
48 int style, String sample_text, final String pref_name,
49 final IPreferenceStore prefs) {
52 TableColumn col = new TableColumn(parent, style);
54 // if there is no pref store or the entry is missing, we use the sample
55 // text and pack the column.
56 // Otherwise we just read the width from the prefs and apply it.
57 if (prefs == null || prefs.contains(pref_name) == false) {
58 col.setText(sample_text);
61 // init the prefs store with the current value
63 prefs.setValue(pref_name, col.getWidth());
66 col.setWidth(prefs.getInt(pref_name));
72 // if there is a pref store and a pref entry name, then we setup a
73 // listener to catch column resize to put store the new width value.
74 if (prefs != null && pref_name != null) {
75 col.addControlListener(new ControlListener() {
76 public void controlMoved(ControlEvent e) {
79 public void controlResized(ControlEvent e) {
81 int w = ((TableColumn)e.widget).getWidth();
83 // store in pref store
84 prefs.setValue(pref_name, w);
93 * Create a TreeColumn with the specified parameters. If a
94 * <code>PreferenceStore</code> object and a preference entry name String
95 * object are provided then the column will listen to change in its width
96 * and update the preference store accordingly.
98 * @param parent The Table parent object
99 * @param header The header string
100 * @param style The column style
101 * @param sample_text A sample text to figure out column width if preference
103 * @param pref_name The preference entry name for column width
104 * @param prefs The preference store
106 public static void createTreeColumn(Tree parent, String header, int style,
107 String sample_text, final String pref_name,
108 final IPreferenceStore prefs) {
111 TreeColumn col = new TreeColumn(parent, style);
113 // if there is no pref store or the entry is missing, we use the sample
114 // text and pack the column.
115 // Otherwise we just read the width from the prefs and apply it.
116 if (prefs == null || prefs.contains(pref_name) == false) {
117 col.setText(sample_text);
120 // init the prefs store with the current value
122 prefs.setValue(pref_name, col.getWidth());
125 col.setWidth(prefs.getInt(pref_name));
131 // if there is a pref store and a pref entry name, then we setup a
132 // listener to catch column resize to put store the new width value.
133 if (prefs != null && pref_name != null) {
134 col.addControlListener(new ControlListener() {
135 public void controlMoved(ControlEvent e) {
138 public void controlResized(ControlEvent e) {
140 int w = ((TreeColumn)e.widget).getWidth();
142 // store in pref store
143 prefs.setValue(pref_name, w);
150 * Create a TreeColumn with the specified parameters. If a
151 * <code>PreferenceStore</code> object and a preference entry name String
152 * object are provided then the column will listen to change in its width
153 * and update the preference store accordingly.
155 * @param parent The Table parent object
156 * @param header The header string
157 * @param style The column style
158 * @param width the width of the column if the preference value is missing
159 * @param pref_name The preference entry name for column width
160 * @param prefs The preference store
162 public static void createTreeColumn(Tree parent, String header, int style,
163 int width, final String pref_name,
164 final IPreferenceStore prefs) {
167 TreeColumn col = new TreeColumn(parent, style);
169 // if there is no pref store or the entry is missing, we use the sample
170 // text and pack the column.
171 // Otherwise we just read the width from the prefs and apply it.
172 if (prefs == null || prefs.contains(pref_name) == false) {
175 // init the prefs store with the current value
177 prefs.setValue(pref_name, width);
180 col.setWidth(prefs.getInt(pref_name));
186 // if there is a pref store and a pref entry name, then we setup a
187 // listener to catch column resize to put store the new width value.
188 if (prefs != null && pref_name != null) {
189 col.addControlListener(new ControlListener() {
190 public void controlMoved(ControlEvent e) {
193 public void controlResized(ControlEvent e) {
195 int w = ((TreeColumn)e.widget).getWidth();
197 // store in pref store
198 prefs.setValue(pref_name, w);