ColumnPropertiesDialog.java
Go to the documentation of this file.00001 package edu.rice.cs.hpc.viewer.util;
00002
00003 import org.osgi.service.prefs.Preferences;
00004 import org.eclipse.jface.dialogs.IMessageProvider;
00005 import org.eclipse.jface.dialogs.TitleAreaDialog;
00006 import org.eclipse.jface.layout.GridDataFactory;
00007 import org.eclipse.jface.layout.GridLayoutFactory;
00008 import org.eclipse.jface.viewers.CheckStateChangedEvent;
00009 import org.eclipse.jface.viewers.ICheckStateListener;
00010 import org.eclipse.jface.viewers.IStructuredContentProvider;
00011 import org.eclipse.jface.viewers.Viewer;
00012 import org.eclipse.jface.viewers.CheckboxTableViewer;
00013 import org.eclipse.jface.viewers.ILabelProvider;
00014 import org.eclipse.jface.viewers.ILabelProviderListener;
00015 import org.eclipse.jface.viewers.ViewerFilter;
00016
00017 import org.eclipse.swt.layout.GridLayout;
00018 import org.eclipse.swt.widgets.TreeColumn;
00019 import org.eclipse.swt.SWT;
00020 import org.eclipse.swt.layout.FillLayout;
00021 import org.eclipse.swt.layout.GridData;
00022 import org.eclipse.swt.widgets.Composite;
00023 import org.eclipse.swt.widgets.Control;
00024 import org.eclipse.swt.widgets.Display;
00025 import org.eclipse.swt.widgets.Label;
00026 import org.eclipse.swt.widgets.Shell;
00027 import org.eclipse.swt.widgets.TableItem;
00028 import org.eclipse.swt.widgets.Text;
00029 import org.eclipse.swt.events.ModifyEvent;
00030 import org.eclipse.swt.events.ModifyListener;
00031 import org.eclipse.swt.events.SelectionAdapter;
00032 import org.eclipse.swt.events.SelectionEvent;
00033 import org.eclipse.swt.graphics.Image;
00034 import org.eclipse.swt.widgets.Button;
00035 import org.eclipse.swt.widgets.Table;
00036
00037 import edu.rice.cs.hpc.common.util.UserInputHistory;
00038
00039 import java.util.ArrayList;
00040 import java.util.Hashtable;
00041
00042
00047 public class ColumnPropertiesDialog extends TitleAreaDialog {
00048
00049 static final private String HISTORY_COLUMN_PROPERTY = "column_property";
00050 static final private String HISTORY_APPLY_ALL = "apply-all";
00051
00052 protected ColumnCheckTableViewer objCheckBoxTable ;
00053 protected TreeColumn []objColumns;
00054 protected boolean[] results;
00055 protected Button btnApplyToAllViews;
00056 protected boolean isAppliedToAllViews = false;
00057 protected Text objSearchText;
00058
00059 protected ArrayList<PropertiesModel> arrElements;
00060
00061
00062
00063
00069 public ColumnPropertiesDialog(Shell parentShell) {
00070 super(parentShell);
00071
00072 }
00073
00074
00080 public ColumnPropertiesDialog(Shell shell, TreeColumn []columns) {
00081 super(shell);
00082 this.objColumns = columns;
00083 }
00084
00085
00086
00087
00088
00095 protected Control createContents(Composite parent) {
00096 Control contents = super.createContents(parent);
00097
00098
00099 setTitle("Column Selection");
00100
00101
00102 setMessage("Check columns to be shown and uncheck columns to be hidden", IMessageProvider.INFORMATION);
00103
00104 return contents;
00105 }
00106
00113 protected Control createDialogArea(Composite aParent) {
00114 Composite composite = new Composite(aParent, SWT.BORDER);
00115
00116 GridLayout grid = new GridLayout();
00117 grid.numColumns=1;
00118 composite.setLayoutData(new GridData(GridData.FILL_BOTH));
00119 composite.setLayout(grid);
00120
00121
00122 GridLayout gridButtons = new GridLayout();
00123 gridButtons.numColumns=3;
00124 Composite groupButtons = new Composite(composite, SWT.BORDER);
00125 groupButtons.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
00126 groupButtons.setLayout(gridButtons);
00127
00128
00129 Button btnCheckAll = new Button(groupButtons, SWT.NONE);
00130 btnCheckAll.setText("Check all");
00131 btnCheckAll.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false));
00132 btnCheckAll.addSelectionListener(new SelectionAdapter() {
00133 public void widgetSelected(SelectionEvent e) {
00134 objCheckBoxTable.setAllChecked(true);
00135 }
00136 });
00137
00138 Button btnUnCheckAll = new Button(groupButtons, SWT.NONE);
00139 btnUnCheckAll.setText("Uncheck all");
00140 btnUnCheckAll.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false));
00141 btnUnCheckAll.addSelectionListener(new SelectionAdapter() {
00142 public void widgetSelected(SelectionEvent e) {
00143 objCheckBoxTable.setAllChecked(false);
00144 }
00145 });
00146
00147 btnApplyToAllViews = new Button(groupButtons, SWT.CHECK);
00148 btnApplyToAllViews.setText("Apply to all views");
00149
00150 btnApplyToAllViews.setSelection( this.getHistory() );
00151 btnApplyToAllViews.setLayoutData(new GridData(SWT.END, SWT.CENTER, false, false));
00152
00153
00154 Composite groupFilter = new Composite(composite, SWT.BORDER);
00155 groupFilter.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
00156 GridLayoutFactory.fillDefaults().numColumns(2).applyTo(groupFilter);
00157
00158
00159 Label lblFilter = new Label (groupFilter, SWT.FLAT);
00160 lblFilter.setText("Filter:");
00161
00162 objSearchText = new Text (groupFilter, SWT.BORDER);
00163
00164 GridDataFactory.fillDefaults().grab(true, false).applyTo(objSearchText);
00165 objSearchText.addModifyListener(new ModifyListener() {
00166 public void modifyText(ModifyEvent e) {
00167
00168 ColumnFilter objFilter = (ColumnFilter) objCheckBoxTable.getFilters()[0];
00169
00170 objFilter.setKey(objSearchText.getText());
00171 objCheckBoxTable.refresh();
00172 objCheckBoxTable.setCheckedElements(getCheckedItemsFromGlobalVariable());
00173 }
00174 });
00175
00176
00177
00178 Table table = new Table(composite, SWT.CHECK | SWT.BORDER);
00179 table.setLayoutData(new GridData(GridData.FILL_BOTH));
00180
00181 this.objCheckBoxTable = new ColumnCheckTableViewer(table) ;
00182 objCheckBoxTable.getTable().setLayoutData(new GridData(GridData.FILL_BOTH));
00183
00184 objCheckBoxTable.setContentProvider(new IStructuredContentProvider() {
00185 public Object[] getElements(Object parent) {
00186 if (parent instanceof ArrayList<?>)
00187 return ((ArrayList<?>)parent).toArray();
00188 else
00189 return null;
00190 }
00191 public void dispose() {
00192
00193 }
00194 public void inputChanged(Viewer v, Object oldInput, Object newInput) {
00195
00196 }
00197 });
00198
00199
00200 objCheckBoxTable.addCheckStateListener(new ICheckStateListener() {
00201
00202 public void checkStateChanged(CheckStateChangedEvent event) {
00203 PropertiesModel objItem = (PropertiesModel) event.getElement();
00204 objItem.isVisible = event.getChecked();
00205 if (arrElements.get(objItem.iIndex) != objItem) {
00206 arrElements.get(objItem.iIndex).isVisible = objItem.isVisible;
00207 }
00208 }
00209
00210 });
00211 this.objCheckBoxTable.setLabelProvider(new CheckLabelProvider());
00212
00213 ColumnFilter objFilter = new ColumnFilter();
00214 this.objCheckBoxTable.addFilter(objFilter);
00215
00216 this.updateContent();
00217 return composite;
00218 }
00219
00220
00221
00222
00227 protected Object[] getCheckedItemsFromGlobalVariable () {
00228 int nb = this.arrElements.size();
00229 ArrayList<PropertiesModel> arrCheckedElements = new ArrayList<PropertiesModel>();
00230
00231 for (int i=0; i<nb; i++) {
00232 if (arrElements.get(i).isVisible)
00233 arrCheckedElements.add(this.arrElements.get(i));
00234 }
00235 return arrCheckedElements.toArray();
00236
00237 }
00238
00239
00240
00241
00242
00243 protected boolean getHistory() {
00244 return UserInputHistory.getPreference(HISTORY_COLUMN_PROPERTY).getBoolean(HISTORY_APPLY_ALL, true);
00245 }
00246
00247
00248
00249
00250
00251 protected void setHistory( boolean value ) {
00252 Preferences pref = UserInputHistory.getPreference(HISTORY_COLUMN_PROPERTY);
00253 pref.putBoolean(HISTORY_APPLY_ALL, value);
00254 UserInputHistory.setPreference(pref);
00255 }
00256
00257
00258
00259
00260 protected void okPressed() {
00261
00262 for (int i=0; i<arrElements.size(); i++) {
00263 boolean isVisible = (this.arrElements.get(i).isVisible);
00264 results[i] = isVisible;
00265 }
00266
00267 this.isAppliedToAllViews = this.btnApplyToAllViews.getSelection();
00268 this.setHistory(isAppliedToAllViews);
00269
00270 super.okPressed();
00271 }
00272
00273
00274
00275
00276
00282 public void setData(TreeColumn []columns) {
00283 this.objColumns = columns;
00284 }
00285
00289 public void updateContent() {
00290 if(this.objColumns == null)
00291 return;
00292 int nbColumns = this.objColumns.length;
00293
00294 this.arrElements = new ArrayList<PropertiesModel>(nbColumns);
00295 ArrayList<PropertiesModel> arrColumns = new ArrayList<PropertiesModel>();
00296
00297 int index = 0;
00298 for(int i=0;i<nbColumns;i++) {
00299 if (objColumns[i].getData() != null) {
00300 TreeColumn column = this.objColumns[i];
00301 boolean isVisible = column.getWidth() > 1;
00302 String sTitle = column.getText();
00303
00304 PropertiesModel model = new PropertiesModel(isVisible, sTitle, index);
00305 index++;
00306
00307 arrElements.add( model );
00308
00309 if(isVisible) {
00310 arrColumns.add(model);
00311 }
00312 }
00313 }
00314
00315 this.results = new boolean[index];
00316 for(int i=0; i<index; i++) {
00317 results[i] = false;
00318 }
00319
00320 this.objCheckBoxTable.setInput(arrElements);
00321 this.objCheckBoxTable.setCheckedElements(arrColumns.toArray());
00322 }
00323
00328 public boolean[] getResult() {
00329 return results;
00330 }
00331
00336 public boolean getStatusApplication() {
00337 return this.isAppliedToAllViews;
00338 }
00339
00340
00341
00342
00343
00344
00350 protected class CheckLabelProvider implements ILabelProvider {
00351 public void addListener(ILabelProviderListener arg0) {
00352
00353 }
00354 public void dispose() {
00355
00356 }
00357 public boolean isLabelProperty(Object arg0, String arg1) {
00358 return false;
00359 }
00360 public void removeListener(ILabelProviderListener arg0) {
00361
00362 }
00363 public Image getImage(Object arg0) {
00364 return null;
00365 }
00366 public String getText(Object arg0) {
00367 PropertiesModel prop = (PropertiesModel) arg0;
00368 return prop.sTitle;
00369 }
00370 }
00371
00372
00378 protected class PropertiesModel {
00379 public boolean isVisible;
00380 public String sTitle;
00381 public int iIndex;
00382
00383 public PropertiesModel(boolean b, String s, int i) {
00384 this.isVisible = b;
00385 this.sTitle = s;
00386 this.iIndex = i;
00387 }
00388 }
00389
00393 protected class ColumnFilter extends ViewerFilter {
00394
00395 private String sKeyToMatch;
00396
00397 public boolean select(Viewer viewer, Object parentElement,
00398 Object element) {
00399
00400 if ( (sKeyToMatch != null) && (sKeyToMatch.length()>0) ){
00401
00402 assert (element instanceof PropertiesModel);
00403 PropertiesModel objProperty = (PropertiesModel) element;
00404
00405 boolean bSelect = objProperty.sTitle.toUpperCase().contains(sKeyToMatch);
00406 return bSelect;
00407 }
00408 return true;
00409 }
00410
00415 public void setKey ( String sKey ) {
00416 sKeyToMatch = sKey.toUpperCase();
00417 }
00418 }
00419
00424 protected class ColumnCheckTableViewer extends CheckboxTableViewer {
00425
00429 public ColumnCheckTableViewer(Table table) {
00430 super(table);
00431
00432 }
00433
00434
00435
00436
00437
00438 public void setAllChecked(boolean state) {
00439 super.setAllChecked(state);
00440
00441 TableItem[] items = getTable().getItems();
00442 for (int i=0; i<items.length; i++) {
00443 TableItem objItem = items[i];
00444 PropertiesModel objModel = (PropertiesModel) objItem.getData();
00445 objModel.isVisible = state;
00446 }
00447 }
00448
00449
00450
00451
00452
00453 public void setCheckedElements(Object[] elements) {
00454 assertElementsNotNull(elements);
00455 TableItem[] items = getTable().getItems();
00456
00457 Hashtable<Object, TableItem> set = new Hashtable<Object, TableItem>(items.length * 2 + 1);
00458 for (int i = 0; i < items.length; ++i) {
00459 set.put(items[i].getData(), items[i]);
00460 }
00461
00462 for (int i=0; i<elements.length; i++) {
00463 TableItem objItem = set.get(elements[i]);
00464 if (objItem != null) {
00465 objItem.setChecked(true);
00466 }
00467 }
00468 }
00469
00470 }
00471
00472
00473
00474
00475
00479 public void setExample() {
00480 PropertiesModel []objModels = new PropertiesModel[4];
00481 objModels[0] = new PropertiesModel(true, "one", 0);
00482 objModels[1] = new PropertiesModel(false, "two", 1);
00483 objModels[2] = new PropertiesModel(true, "three", 2);
00484 objModels[3] = new PropertiesModel(true, "four", 3);
00485 this.objCheckBoxTable.setInput(objModels);
00486 PropertiesModel ch[] = new PropertiesModel[3];
00487 ch[0] = objModels[0];
00488 ch[1] = objModels[2];
00489 ch[2] = objModels[3];
00490 this.objCheckBoxTable.setCheckedElements(ch);
00491 }
00492
00496 public static void main(String []args) {
00497 Display display = new Display();
00498
00499 Shell shell = new Shell(display);
00500 shell.setLayout(new FillLayout());
00501
00502 ColumnPropertiesDialog objProp = new ColumnPropertiesDialog(shell);
00503
00504 objProp.open();
00505 }
00506 }