|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2000, 2010 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* IBM Corporation - initial API and implementation |
| 10 |
*******************************************************************************/ |
| 11 |
package org.eclipse.wst.sse.ui.filter; |
| 12 |
|
| 13 |
import java.util.ArrayList; |
| 14 |
import java.util.Arrays; |
| 15 |
import java.util.HashSet; |
| 16 |
import java.util.Iterator; |
| 17 |
import java.util.List; |
| 18 |
import java.util.Set; |
| 19 |
import java.util.Stack; |
| 20 |
import java.util.StringTokenizer; |
| 21 |
|
| 22 |
import org.eclipse.core.runtime.Assert; |
| 23 |
import org.eclipse.jface.dialogs.IDialogConstants; |
| 24 |
import org.eclipse.jface.viewers.ArrayContentProvider; |
| 25 |
import org.eclipse.jface.viewers.CheckStateChangedEvent; |
| 26 |
import org.eclipse.jface.viewers.CheckboxTableViewer; |
| 27 |
import org.eclipse.jface.viewers.ICheckStateListener; |
| 28 |
import org.eclipse.jface.viewers.ILabelProvider; |
| 29 |
import org.eclipse.jface.viewers.ISelection; |
| 30 |
import org.eclipse.jface.viewers.ISelectionChangedListener; |
| 31 |
import org.eclipse.jface.viewers.IStructuredSelection; |
| 32 |
import org.eclipse.jface.viewers.LabelProvider; |
| 33 |
import org.eclipse.jface.viewers.SelectionChangedEvent; |
| 34 |
import org.eclipse.swt.SWT; |
| 35 |
import org.eclipse.swt.events.SelectionAdapter; |
| 36 |
import org.eclipse.swt.events.SelectionEvent; |
| 37 |
import org.eclipse.swt.events.SelectionListener; |
| 38 |
import org.eclipse.swt.graphics.Image; |
| 39 |
import org.eclipse.swt.layout.GridData; |
| 40 |
import org.eclipse.swt.layout.GridLayout; |
| 41 |
import org.eclipse.swt.widgets.Button; |
| 42 |
import org.eclipse.swt.widgets.Composite; |
| 43 |
import org.eclipse.swt.widgets.Control; |
| 44 |
import org.eclipse.swt.widgets.Label; |
| 45 |
import org.eclipse.swt.widgets.Shell; |
| 46 |
import org.eclipse.swt.widgets.Text; |
| 47 |
import org.eclipse.ui.PlatformUI; |
| 48 |
import org.eclipse.ui.dialogs.SelectionDialog; |
| 49 |
import org.eclipse.wst.sse.ui.internal.SSEUIPlugin; |
| 50 |
|
| 51 |
|
| 52 |
public class SSECustomFiltersDialog extends SelectionDialog { |
| 53 |
|
| 54 |
private static final String SEPARATOR= ","; //$NON-NLS-1$ |
| 55 |
|
| 56 |
private String fViewId; |
| 57 |
private boolean fEnablePatterns; |
| 58 |
private String[] fPatterns; |
| 59 |
private String[] fEnabledFilterIds; |
| 60 |
|
| 61 |
private SSEFilterDescriptor[] fBuiltInFilters; |
| 62 |
|
| 63 |
private CheckboxTableViewer fCheckBoxList; |
| 64 |
private Button fEnableUserDefinedPatterns; |
| 65 |
private Text fUserDefinedPatterns; |
| 66 |
|
| 67 |
private Stack fFilterDescriptorChangeHistory; |
| 68 |
|
| 69 |
|
| 70 |
/** |
| 71 |
* Creates a dialog to customize Java element filters. |
| 72 |
* |
| 73 |
* @param shell the parent shell |
| 74 |
* @param viewId the id of the view |
| 75 |
* @param enablePatterns <code>true</code> if pattern filters are enabled |
| 76 |
* @param patterns the filter patterns |
| 77 |
* @param enabledFilterIds the Ids of the enabled filters |
| 78 |
*/ |
| 79 |
public SSECustomFiltersDialog( |
| 80 |
Shell shell, |
| 81 |
String viewId, |
| 82 |
boolean enablePatterns, |
| 83 |
String[] patterns, |
| 84 |
String[] enabledFilterIds) { |
| 85 |
|
| 86 |
super(shell); |
| 87 |
Assert.isNotNull(viewId); |
| 88 |
Assert.isNotNull(patterns); |
| 89 |
Assert.isNotNull(enabledFilterIds); |
| 90 |
|
| 91 |
fViewId= viewId; |
| 92 |
fPatterns= patterns; |
| 93 |
fEnablePatterns= enablePatterns; |
| 94 |
fEnabledFilterIds= enabledFilterIds; |
| 95 |
|
| 96 |
fBuiltInFilters= SSEFilterDescriptor.getFilterDescriptors(fViewId); |
| 97 |
fFilterDescriptorChangeHistory= new Stack(); |
| 98 |
} |
| 99 |
public static final String CUSTOM_FILTERS_DIALOG= SSEUIPlugin.ID + "." + "open_custom_filters_dialog_context"; //$NON-NLS-1$ |
| 100 |
protected void configureShell(Shell shell) { |
| 101 |
setTitle(SSEFilterMessages.CustomFiltersDialog_title); |
| 102 |
setMessage(SSEFilterMessages.CustomFiltersDialog_filterList_label); |
| 103 |
super.configureShell(shell); |
| 104 |
PlatformUI.getWorkbench().getHelpSystem().setHelp(shell, CUSTOM_FILTERS_DIALOG); |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Overrides method in Dialog |
| 109 |
* |
| 110 |
* @see org.eclipse.jface.dialogs.Dialog#createDialogArea(Composite) |
| 111 |
*/ |
| 112 |
protected Control createDialogArea(Composite parent) { |
| 113 |
initializeDialogUnits(parent); |
| 114 |
// create a composite with standard margins and spacing |
| 115 |
Composite composite= new Composite(parent, SWT.NONE); |
| 116 |
GridLayout layout= new GridLayout(); |
| 117 |
layout.marginHeight= convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN); |
| 118 |
layout.marginWidth= convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN); |
| 119 |
layout.verticalSpacing= convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING); |
| 120 |
layout.horizontalSpacing= convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING); |
| 121 |
composite.setLayout(layout); |
| 122 |
composite.setLayoutData(new GridData(GridData.FILL_BOTH)); |
| 123 |
composite.setFont(parent.getFont()); |
| 124 |
Composite group= composite; |
| 125 |
|
| 126 |
// Checkbox |
| 127 |
fEnableUserDefinedPatterns= new Button(group, SWT.CHECK); |
| 128 |
fEnableUserDefinedPatterns.setFocus(); |
| 129 |
fEnableUserDefinedPatterns.setText(SSEFilterMessages.CustomFiltersDialog_enableUserDefinedPattern); |
| 130 |
|
| 131 |
// Pattern field |
| 132 |
fUserDefinedPatterns= new Text(group, SWT.SINGLE | SWT.BORDER); |
| 133 |
GridData data= new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.GRAB_HORIZONTAL); |
| 134 |
data.widthHint= convertWidthInCharsToPixels(59); |
| 135 |
fUserDefinedPatterns.setLayoutData(data); |
| 136 |
String patterns= convertToString(fPatterns, SEPARATOR); |
| 137 |
fUserDefinedPatterns.setText(patterns); |
| 138 |
SWTUtil.setAccessibilityText(fUserDefinedPatterns, SSEFilterMessages.CustomFiltersDialog_name_filter_pattern_description); |
| 139 |
|
| 140 |
// Info text |
| 141 |
final Label info= new Label(group, SWT.LEFT); |
| 142 |
info.setText(SSEFilterMessages.CustomFiltersDialog_patternInfo); |
| 143 |
|
| 144 |
// Enabling / disabling of pattern group |
| 145 |
fEnableUserDefinedPatterns.setSelection(fEnablePatterns); |
| 146 |
fUserDefinedPatterns.setEnabled(fEnablePatterns); |
| 147 |
info.setEnabled(fEnablePatterns); |
| 148 |
fEnableUserDefinedPatterns.addSelectionListener(new SelectionAdapter() { |
| 149 |
public void widgetSelected(SelectionEvent e) { |
| 150 |
boolean state= fEnableUserDefinedPatterns.getSelection(); |
| 151 |
fUserDefinedPatterns.setEnabled(state); |
| 152 |
info.setEnabled(fEnableUserDefinedPatterns.getSelection()); |
| 153 |
if (state) |
| 154 |
fUserDefinedPatterns.setFocus(); |
| 155 |
} |
| 156 |
}); |
| 157 |
|
| 158 |
// Filters provided by extension point |
| 159 |
if (fBuiltInFilters.length > 0) |
| 160 |
createCheckBoxList(group); |
| 161 |
|
| 162 |
applyDialogFont(parent); |
| 163 |
return parent; |
| 164 |
} |
| 165 |
|
| 166 |
private void createCheckBoxList(Composite parent) { |
| 167 |
// Filler |
| 168 |
new Label(parent, SWT.NONE); |
| 169 |
|
| 170 |
Label info= new Label(parent, SWT.LEFT); |
| 171 |
info.setText(SSEFilterMessages.CustomFiltersDialog_filterList_label); |
| 172 |
|
| 173 |
fCheckBoxList= CheckboxTableViewer.newCheckList(parent, SWT.BORDER); |
| 174 |
GridData data= new GridData(GridData.FILL_BOTH); |
| 175 |
data.heightHint= fCheckBoxList.getTable().getItemHeight() * 10; |
| 176 |
fCheckBoxList.getTable().setLayoutData(data); |
| 177 |
|
| 178 |
fCheckBoxList.setLabelProvider(createLabelPrivder()); |
| 179 |
fCheckBoxList.setContentProvider(new ArrayContentProvider()); |
| 180 |
Arrays.sort(fBuiltInFilters); |
| 181 |
fCheckBoxList.setInput(fBuiltInFilters); |
| 182 |
setInitialSelections(getEnabledFilterDescriptors()); |
| 183 |
|
| 184 |
List initialSelection= getInitialElementSelections(); |
| 185 |
if (initialSelection != null && !initialSelection.isEmpty()) |
| 186 |
checkInitialSelections(); |
| 187 |
|
| 188 |
// Description |
| 189 |
info= new Label(parent, SWT.LEFT); |
| 190 |
info.setText(SSEFilterMessages.CustomFiltersDialog_description_label); |
| 191 |
final Text description= new Text(parent, SWT.LEFT | SWT.WRAP | SWT.MULTI | SWT.READ_ONLY | SWT.BORDER | SWT.V_SCROLL); |
| 192 |
data = new GridData(GridData.FILL_HORIZONTAL); |
| 193 |
data.heightHint= convertHeightInCharsToPixels(3); |
| 194 |
description.setLayoutData(data); |
| 195 |
fCheckBoxList.addSelectionChangedListener(new ISelectionChangedListener() { |
| 196 |
public void selectionChanged(SelectionChangedEvent event) { |
| 197 |
ISelection selection= event.getSelection(); |
| 198 |
if (selection instanceof IStructuredSelection) { |
| 199 |
Object selectedElement= ((IStructuredSelection)selection).getFirstElement(); |
| 200 |
if (selectedElement instanceof SSEFilterDescriptor) |
| 201 |
description.setText(((SSEFilterDescriptor)selectedElement).getDescription()); |
| 202 |
} |
| 203 |
} |
| 204 |
}); |
| 205 |
fCheckBoxList.addCheckStateListener(new ICheckStateListener() { |
| 206 |
/* |
| 207 |
* @see org.eclipse.jface.viewers.ICheckStateListener#checkStateChanged(org.eclipse.jface.viewers.CheckStateChangedEvent) |
| 208 |
*/ |
| 209 |
public void checkStateChanged(CheckStateChangedEvent event) { |
| 210 |
Object element= event.getElement(); |
| 211 |
if (element instanceof SSEFilterDescriptor) { |
| 212 |
// renew if already touched |
| 213 |
if (fFilterDescriptorChangeHistory.contains(element)) |
| 214 |
fFilterDescriptorChangeHistory.remove(element); |
| 215 |
fFilterDescriptorChangeHistory.push(element); |
| 216 |
} |
| 217 |
}}); |
| 218 |
|
| 219 |
addSelectionButtons(parent); |
| 220 |
} |
| 221 |
|
| 222 |
private void addSelectionButtons(Composite composite) { |
| 223 |
Composite buttonComposite= new Composite(composite, SWT.RIGHT); |
| 224 |
GridLayout layout= new GridLayout(); |
| 225 |
layout.numColumns= 2; |
| 226 |
buttonComposite.setLayout(layout); |
| 227 |
GridData data= new GridData(GridData.HORIZONTAL_ALIGN_END | GridData.GRAB_HORIZONTAL); |
| 228 |
data.grabExcessHorizontalSpace= true; |
| 229 |
composite.setData(data); |
| 230 |
|
| 231 |
// Select All button |
| 232 |
String label= SSEFilterMessages.CustomFiltersDialog_SelectAllButton_label; |
| 233 |
Button selectButton= createButton(buttonComposite, IDialogConstants.SELECT_ALL_ID, label, false); |
| 234 |
SWTUtil.setButtonDimensionHint(selectButton); |
| 235 |
SelectionListener listener= new SelectionAdapter() { |
| 236 |
public void widgetSelected(SelectionEvent e) { |
| 237 |
fCheckBoxList.setAllChecked(true); |
| 238 |
fFilterDescriptorChangeHistory.clear(); |
| 239 |
for (int i= 0; i < fBuiltInFilters.length; i++) |
| 240 |
fFilterDescriptorChangeHistory.push(fBuiltInFilters[i]); |
| 241 |
} |
| 242 |
}; |
| 243 |
selectButton.addSelectionListener(listener); |
| 244 |
|
| 245 |
// De-select All button |
| 246 |
label= SSEFilterMessages.CustomFiltersDialog_DeselectAllButton_label; |
| 247 |
Button deselectButton= createButton(buttonComposite, IDialogConstants.DESELECT_ALL_ID, label, false); |
| 248 |
SWTUtil.setButtonDimensionHint(deselectButton); |
| 249 |
listener= new SelectionAdapter() { |
| 250 |
public void widgetSelected(SelectionEvent e) { |
| 251 |
fCheckBoxList.setAllChecked(false); |
| 252 |
fFilterDescriptorChangeHistory.clear(); |
| 253 |
for (int i= 0; i < fBuiltInFilters.length; i++) |
| 254 |
fFilterDescriptorChangeHistory.push(fBuiltInFilters[i]); |
| 255 |
} |
| 256 |
}; |
| 257 |
deselectButton.addSelectionListener(listener); |
| 258 |
} |
| 259 |
|
| 260 |
private void checkInitialSelections() { |
| 261 |
Iterator itemsToCheck= getInitialElementSelections().iterator(); |
| 262 |
while (itemsToCheck.hasNext()) |
| 263 |
fCheckBoxList.setChecked(itemsToCheck.next(),true); |
| 264 |
} |
| 265 |
|
| 266 |
protected void okPressed() { |
| 267 |
if (fBuiltInFilters != null) { |
| 268 |
ArrayList result= new ArrayList(); |
| 269 |
for (int i= 0; i < fBuiltInFilters.length; ++i) { |
| 270 |
if (fCheckBoxList.getChecked(fBuiltInFilters[i])) |
| 271 |
result.add(fBuiltInFilters[i]); |
| 272 |
} |
| 273 |
setResult(result); |
| 274 |
} |
| 275 |
super.okPressed(); |
| 276 |
} |
| 277 |
|
| 278 |
private ILabelProvider createLabelPrivder() { |
| 279 |
return |
| 280 |
new LabelProvider() { |
| 281 |
public Image getImage(Object element) { |
| 282 |
return null; |
| 283 |
} |
| 284 |
public String getText(Object element) { |
| 285 |
if (element instanceof SSEFilterDescriptor) |
| 286 |
return ((SSEFilterDescriptor)element).getName(); |
| 287 |
else |
| 288 |
return null; |
| 289 |
} |
| 290 |
}; |
| 291 |
} |
| 292 |
|
| 293 |
// ---------- result handling ---------- |
| 294 |
|
| 295 |
protected void setResult(List newResult) { |
| 296 |
super.setResult(newResult); |
| 297 |
if (fUserDefinedPatterns.getText().length() > 0) { |
| 298 |
fEnablePatterns= fEnableUserDefinedPatterns.getSelection(); |
| 299 |
fPatterns= convertFromString(fUserDefinedPatterns.getText(), SEPARATOR); |
| 300 |
} else { |
| 301 |
fEnablePatterns= false; |
| 302 |
fPatterns= new String[0]; |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
/** |
| 308 |
* @return the patterns which have been entered by the user |
| 309 |
*/ |
| 310 |
public String[] getUserDefinedPatterns() { |
| 311 |
return fPatterns; |
| 312 |
} |
| 313 |
|
| 314 |
/** |
| 315 |
* @return the Ids of the enabled built-in filters |
| 316 |
*/ |
| 317 |
public String[] getEnabledFilterIds() { |
| 318 |
Object[] result= getResult(); |
| 319 |
Set enabledIds= new HashSet(result.length); |
| 320 |
for (int i= 0; i < result.length; i++) |
| 321 |
enabledIds.add(((SSEFilterDescriptor)result[i]).getId()); |
| 322 |
return (String[]) enabledIds.toArray(new String[enabledIds.size()]); |
| 323 |
} |
| 324 |
|
| 325 |
/** |
| 326 |
* @return <code>true</code> if the user-defined patterns are disabled |
| 327 |
*/ |
| 328 |
public boolean areUserDefinedPatternsEnabled() { |
| 329 |
return fEnablePatterns; |
| 330 |
} |
| 331 |
|
| 332 |
/** |
| 333 |
* @return a stack with the filter descriptor check history |
| 334 |
* @since 3.0 |
| 335 |
*/ |
| 336 |
public Stack getFilterDescriptorChangeHistory() { |
| 337 |
return fFilterDescriptorChangeHistory; |
| 338 |
} |
| 339 |
|
| 340 |
private SSEFilterDescriptor[] getEnabledFilterDescriptors() { |
| 341 |
SSEFilterDescriptor[] filterDescs= fBuiltInFilters; |
| 342 |
List result= new ArrayList(filterDescs.length); |
| 343 |
List enabledFilterIds= Arrays.asList(fEnabledFilterIds); |
| 344 |
for (int i= 0; i < filterDescs.length; i++) { |
| 345 |
String id= filterDescs[i].getId(); |
| 346 |
if (enabledFilterIds.contains(id)) |
| 347 |
result.add(filterDescs[i]); |
| 348 |
} |
| 349 |
return (SSEFilterDescriptor[])result.toArray(new SSEFilterDescriptor[result.size()]); |
| 350 |
} |
| 351 |
|
| 352 |
|
| 353 |
public static String[] convertFromString(String patterns, String separator) { |
| 354 |
StringTokenizer tokenizer= new StringTokenizer(patterns, separator, true); |
| 355 |
int tokenCount= tokenizer.countTokens(); |
| 356 |
List result= new ArrayList(tokenCount); |
| 357 |
boolean escape= false; |
| 358 |
boolean append= false; |
| 359 |
while (tokenizer.hasMoreTokens()) { |
| 360 |
String token= tokenizer.nextToken().trim(); |
| 361 |
if (separator.equals(token)) { |
| 362 |
if (!escape) |
| 363 |
escape= true; |
| 364 |
else { |
| 365 |
addPattern(result, separator); |
| 366 |
append= true; |
| 367 |
} |
| 368 |
} else { |
| 369 |
if (!append) |
| 370 |
result.add(token); |
| 371 |
else |
| 372 |
addPattern(result, token); |
| 373 |
append= false; |
| 374 |
escape= false; |
| 375 |
} |
| 376 |
} |
| 377 |
return (String[])result.toArray(new String[result.size()]); |
| 378 |
} |
| 379 |
|
| 380 |
private static void addPattern(List list, String pattern) { |
| 381 |
if (list.isEmpty()) |
| 382 |
list.add(pattern); |
| 383 |
else { |
| 384 |
int index= list.size() - 1; |
| 385 |
list.set(index, ((String)list.get(index)) + pattern); |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
public static String convertToString(String[] patterns, String separator) { |
| 390 |
int length= patterns.length; |
| 391 |
StringBuffer strBuf= new StringBuffer(); |
| 392 |
if (length > 0) |
| 393 |
strBuf.append(escapeSeparator(patterns[0], separator)); |
| 394 |
else |
| 395 |
return ""; //$NON-NLS-1$ |
| 396 |
int i= 1; |
| 397 |
while (i < length) { |
| 398 |
strBuf.append(separator); |
| 399 |
strBuf.append(" "); //$NON-NLS-1$ |
| 400 |
strBuf.append(escapeSeparator(patterns[i++], separator)); |
| 401 |
} |
| 402 |
return strBuf.toString(); |
| 403 |
} |
| 404 |
|
| 405 |
private static String escapeSeparator(String pattern, String separator) { |
| 406 |
int length= pattern.length(); |
| 407 |
StringBuffer buf= new StringBuffer(length); |
| 408 |
for (int i= 0; i < length; i++) { |
| 409 |
char ch= pattern.charAt(i); |
| 410 |
if (separator.equals(String.valueOf(ch))) |
| 411 |
buf.append(ch); |
| 412 |
buf.append(ch); |
| 413 |
} |
| 414 |
return buf.toString(); |
| 415 |
|
| 416 |
} |
| 417 |
} |