• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007 The Android Open Source Project
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.android.ddmuilib;
18 
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;
26 
27 /**
28  * Utility class to help using Table objects.
29  *
30  */
31 public final class TableHelper {
32     /**
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.
37      *
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
42      *            value is missing
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
46      */
createTableColumn(Table parent, String header, int style, String sample_text, final String pref_name, final IPreferenceStore prefs)47     public static TableColumn createTableColumn(Table parent, String header,
48             int style, String sample_text, final String pref_name,
49             final IPreferenceStore prefs) {
50 
51         // create the column
52         TableColumn col = new TableColumn(parent, style);
53 
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);
59             col.pack();
60 
61             // init the prefs store with the current value
62             if (prefs != null) {
63                 prefs.setValue(pref_name, col.getWidth());
64             }
65         } else {
66             col.setWidth(prefs.getInt(pref_name));
67         }
68 
69         // set the header
70         col.setText(header);
71 
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                 @Override
77                 public void controlMoved(ControlEvent e) {
78                 }
79 
80                 @Override
81                 public void controlResized(ControlEvent e) {
82                     // get the new width
83                     int w = ((TableColumn)e.widget).getWidth();
84 
85                     // store in pref store
86                     prefs.setValue(pref_name, w);
87                 }
88             });
89         }
90 
91         return col;
92     }
93 
94     /**
95      * Create a TreeColumn with the specified parameters. If a
96      * <code>PreferenceStore</code> object and a preference entry name String
97      * object are provided then the column will listen to change in its width
98      * and update the preference store accordingly.
99      *
100      * @param parent The Table parent object
101      * @param header The header string
102      * @param style The column style
103      * @param sample_text A sample text to figure out column width if preference
104      *            value is missing
105      * @param pref_name The preference entry name for column width
106      * @param prefs The preference store
107      */
createTreeColumn(Tree parent, String header, int style, String sample_text, final String pref_name, final IPreferenceStore prefs)108     public static void createTreeColumn(Tree parent, String header, int style,
109             String sample_text, final String pref_name,
110             final IPreferenceStore prefs) {
111 
112         // create the column
113         TreeColumn col = new TreeColumn(parent, style);
114 
115         // if there is no pref store or the entry is missing, we use the sample
116         // text and pack the column.
117         // Otherwise we just read the width from the prefs and apply it.
118         if (prefs == null || prefs.contains(pref_name) == false) {
119             col.setText(sample_text);
120             col.pack();
121 
122             // init the prefs store with the current value
123             if (prefs != null) {
124                 prefs.setValue(pref_name, col.getWidth());
125             }
126         } else {
127             col.setWidth(prefs.getInt(pref_name));
128         }
129 
130         // set the header
131         col.setText(header);
132 
133         // if there is a pref store and a pref entry name, then we setup a
134         // listener to catch column resize to put store the new width value.
135         if (prefs != null && pref_name != null) {
136             col.addControlListener(new ControlListener() {
137                 @Override
138                 public void controlMoved(ControlEvent e) {
139                 }
140 
141                 @Override
142                 public void controlResized(ControlEvent e) {
143                     // get the new width
144                     int w = ((TreeColumn)e.widget).getWidth();
145 
146                     // store in pref store
147                     prefs.setValue(pref_name, w);
148                 }
149             });
150         }
151     }
152 
153     /**
154      * Create a TreeColumn with the specified parameters. If a
155      * <code>PreferenceStore</code> object and a preference entry name String
156      * object are provided then the column will listen to change in its width
157      * and update the preference store accordingly.
158      *
159      * @param parent The Table parent object
160      * @param header The header string
161      * @param style The column style
162      * @param width the width of the column if the preference value is missing
163      * @param pref_name The preference entry name for column width
164      * @param prefs The preference store
165      */
createTreeColumn(Tree parent, String header, int style, int width, final String pref_name, final IPreferenceStore prefs)166     public static void createTreeColumn(Tree parent, String header, int style,
167             int width, final String pref_name,
168             final IPreferenceStore prefs) {
169 
170         // create the column
171         TreeColumn col = new TreeColumn(parent, style);
172 
173         // if there is no pref store or the entry is missing, we use the sample
174         // text and pack the column.
175         // Otherwise we just read the width from the prefs and apply it.
176         if (prefs == null || prefs.contains(pref_name) == false) {
177             col.setWidth(width);
178 
179             // init the prefs store with the current value
180             if (prefs != null) {
181                 prefs.setValue(pref_name, width);
182             }
183         } else {
184             col.setWidth(prefs.getInt(pref_name));
185         }
186 
187         // set the header
188         col.setText(header);
189 
190         // if there is a pref store and a pref entry name, then we setup a
191         // listener to catch column resize to put store the new width value.
192         if (prefs != null && pref_name != null) {
193             col.addControlListener(new ControlListener() {
194                 @Override
195                 public void controlMoved(ControlEvent e) {
196                 }
197 
198                 @Override
199                 public void controlResized(ControlEvent e) {
200                     // get the new width
201                     int w = ((TreeColumn)e.widget).getWidth();
202 
203                     // store in pref store
204                     prefs.setValue(pref_name, w);
205                 }
206             });
207         }
208     }
209 }
210