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 public void controlMoved(ControlEvent e) { 77 } 78 79 public void controlResized(ControlEvent e) { 80 // get the new width 81 int w = ((TableColumn)e.widget).getWidth(); 82 83 // store in pref store 84 prefs.setValue(pref_name, w); 85 } 86 }); 87 } 88 89 return col; 90 } 91 92 /** 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. 97 * 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 102 * value is missing 103 * @param pref_name The preference entry name for column width 104 * @param prefs The preference store 105 */ createTreeColumn(Tree parent, String header, int style, String sample_text, final String pref_name, final IPreferenceStore prefs)106 public static void createTreeColumn(Tree parent, String header, int style, 107 String sample_text, final String pref_name, 108 final IPreferenceStore prefs) { 109 110 // create the column 111 TreeColumn col = new TreeColumn(parent, style); 112 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); 118 col.pack(); 119 120 // init the prefs store with the current value 121 if (prefs != null) { 122 prefs.setValue(pref_name, col.getWidth()); 123 } 124 } else { 125 col.setWidth(prefs.getInt(pref_name)); 126 } 127 128 // set the header 129 col.setText(header); 130 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) { 136 } 137 138 public void controlResized(ControlEvent e) { 139 // get the new width 140 int w = ((TreeColumn)e.widget).getWidth(); 141 142 // store in pref store 143 prefs.setValue(pref_name, w); 144 } 145 }); 146 } 147 } 148 149 /** 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. 154 * 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 161 */ createTreeColumn(Tree parent, String header, int style, int width, final String pref_name, final IPreferenceStore prefs)162 public static void createTreeColumn(Tree parent, String header, int style, 163 int width, final String pref_name, 164 final IPreferenceStore prefs) { 165 166 // create the column 167 TreeColumn col = new TreeColumn(parent, style); 168 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) { 173 col.setWidth(width); 174 175 // init the prefs store with the current value 176 if (prefs != null) { 177 prefs.setValue(pref_name, width); 178 } 179 } else { 180 col.setWidth(prefs.getInt(pref_name)); 181 } 182 183 // set the header 184 col.setText(header); 185 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) { 191 } 192 193 public void controlResized(ControlEvent e) { 194 // get the new width 195 int w = ((TreeColumn)e.widget).getWidth(); 196 197 // store in pref store 198 prefs.setValue(pref_name, w); 199 } 200 }); 201 } 202 } 203 } 204