1 11 package org.eclipse.ui.internal.dialogs; 12 13 import java.util.ArrayList ; 14 import java.util.Collection ; 15 import java.util.Iterator ; 16 17 import org.eclipse.core.runtime.jobs.Job; 18 import org.eclipse.jface.action.ToolBarManager; 19 import org.eclipse.jface.dialogs.IDialogConstants; 20 import org.eclipse.jface.preference.IPreferenceNode; 21 import org.eclipse.jface.preference.IPreferencePage; 22 import org.eclipse.jface.preference.PreferenceContentProvider; 23 import org.eclipse.jface.preference.PreferenceDialog; 24 import org.eclipse.jface.preference.PreferenceManager; 25 import org.eclipse.jface.preference.PreferencePage; 26 import org.eclipse.jface.resource.JFaceResources; 27 import org.eclipse.jface.viewers.ISelectionChangedListener; 28 import org.eclipse.jface.viewers.ITreeContentProvider; 29 import org.eclipse.jface.viewers.SelectionChangedEvent; 30 import org.eclipse.jface.viewers.StructuredSelection; 31 import org.eclipse.jface.viewers.TreeViewer; 32 import org.eclipse.jface.viewers.ViewerFilter; 33 import org.eclipse.osgi.util.NLS; 34 import org.eclipse.swt.SWT; 35 import org.eclipse.swt.graphics.Font; 36 import org.eclipse.swt.layout.GridData; 37 import org.eclipse.swt.layout.GridLayout; 38 import org.eclipse.swt.widgets.Composite; 39 import org.eclipse.swt.widgets.Control; 40 import org.eclipse.swt.widgets.Shell; 41 import org.eclipse.swt.widgets.Text; 42 import org.eclipse.ui.activities.WorkbenchActivityHelper; 43 import org.eclipse.ui.dialogs.FilteredTree; 44 import org.eclipse.ui.dialogs.PatternFilter; 45 import org.eclipse.ui.internal.WorkbenchMessages; 46 import org.eclipse.ui.internal.misc.StatusUtil; 47 import org.eclipse.ui.preferences.IWorkbenchPreferenceContainer; 48 import org.eclipse.ui.preferences.IWorkingCopyManager; 49 import org.eclipse.ui.preferences.WorkingCopyManager; 50 import org.eclipse.ui.statushandlers.StatusManager; 51 import org.osgi.service.prefs.BackingStoreException; 52 53 59 public abstract class FilteredPreferenceDialog extends PreferenceDialog implements IWorkbenchPreferenceContainer{ 60 61 protected class PreferenceFilteredTree extends FilteredTree{ 62 65 private ViewerFilter viewerFilter; 66 67 71 private String cachedTitle; 72 73 80 PreferenceFilteredTree(Composite parent, int treeStyle, 81 PatternFilter filter) { 82 super(parent, treeStyle, filter); 83 } 84 85 92 protected void addFilter(ViewerFilter filter) { 93 viewerFilter = filter; 94 getViewer().addFilter(filter); 95 setInitialText(WorkbenchMessages.FilteredTree_FilterMessage); 96 97 if(filterText != null){ 98 setFilterText(WorkbenchMessages.FilteredTree_FilterMessage); 99 textChanged(); 100 } 101 102 cachedTitle = getShell().getText(); 103 getShell().setText( 104 NLS.bind( 105 WorkbenchMessages.FilteredTree_FilteredDialogTitle, 106 cachedTitle)); 107 } 108 109 113 protected void updateToolbar(boolean visible) { 114 if (filterToolBar != null) { 115 filterToolBar.getControl().setVisible( 116 viewerFilter != null || visible); 117 } 118 } 119 120 123 protected void clearText() { 124 setFilterText(""); if(viewerFilter != null){ 127 getViewer().removeFilter(viewerFilter); 128 viewerFilter = null; 129 getShell().setText(cachedTitle); 130 } 131 textChanged(); 132 } 133 } 134 135 protected PreferenceFilteredTree filteredTree; 136 137 private Object pageData; 138 139 IWorkingCopyManager workingCopyManager; 140 141 private Collection updateJobs = new ArrayList (); 142 143 148 PreferencePageHistory history; 149 150 159 public FilteredPreferenceDialog(Shell parentShell, PreferenceManager manager) { 160 super(parentShell, manager); 161 history = new PreferencePageHistory(this); 162 } 163 164 172 protected IPreferenceNode findNodeMatching(String nodeId) { 173 IPreferenceNode node = super.findNodeMatching(nodeId); 174 if (WorkbenchActivityHelper.filterItem(node)) { 175 return null; 176 } 177 return node; 178 } 179 180 183 protected TreeViewer createTreeViewer(Composite parent) { 184 int styleBits = SWT.SINGLE | SWT.H_SCROLL; 185 filteredTree = new PreferenceFilteredTree(parent, styleBits, 186 new PreferencePatternFilter()); 187 GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true); 188 gd.horizontalIndent = IDialogConstants.HORIZONTAL_MARGIN; 189 filteredTree.setBackground(parent.getDisplay().getSystemColor( 190 SWT.COLOR_LIST_BACKGROUND)); 191 192 TreeViewer tree = filteredTree.getViewer(); 193 194 setContentAndLabelProviders(tree); 195 tree.setInput(getPreferenceManager()); 196 197 if(hasAtMostOnePage(tree)){ 199 Text filterText = filteredTree.getFilterControl(); 200 if (filterText != null) { 201 filteredTree.getFilterControl().setEnabled(false); 202 } 203 } 204 205 tree.addFilter(new CapabilityFilter()); 206 207 tree.addSelectionChangedListener(new ISelectionChangedListener() { 208 213 public void selectionChanged(SelectionChangedEvent event) { 214 handleTreeSelectionChanged(event); 215 } 216 }); 217 218 super.addListeners(filteredTree.getViewer()); 219 return filteredTree.getViewer(); 220 } 221 222 223 229 private boolean hasAtMostOnePage(TreeViewer tree){ 230 ITreeContentProvider contentProvider = (ITreeContentProvider ) tree.getContentProvider(); 231 Object [] children= contentProvider.getElements(tree.getInput()); 232 233 if(children.length <= 1){ 234 if(children.length == 0) { 235 return true; 236 } 237 return !contentProvider.hasChildren(children[0]); 238 } 239 return false; 240 } 241 246 protected void setContentAndLabelProviders(TreeViewer treeViewer) { 247 treeViewer.setLabelProvider(new PreferenceBoldLabelProvider(filteredTree)); 248 treeViewer.setContentProvider(new PreferenceContentProvider()); 249 } 250 251 255 protected void handleTreeSelectionChanged(SelectionChangedEvent event) { 256 } 258 259 262 protected Control createTreeAreaContents(Composite parent) { 263 Composite leftArea = new Composite(parent, SWT.NONE); 264 leftArea.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_LIST_BACKGROUND)); 265 leftArea.setFont(parent.getFont()); 266 GridLayout leftLayout = new GridLayout(); 267 leftLayout.numColumns = 1; 268 leftLayout.marginHeight = 0; 269 leftLayout.marginTop = IDialogConstants.VERTICAL_MARGIN; 270 leftLayout.marginWidth = 0; 271 leftLayout.marginLeft = IDialogConstants.HORIZONTAL_MARGIN; 272 leftLayout.horizontalSpacing = 0; 273 leftLayout.verticalSpacing = 0; 274 leftArea.setLayout(leftLayout); 275 276 TreeViewer viewer = createTreeViewer(leftArea); 278 setTreeViewer(viewer); 279 280 updateTreeFont(JFaceResources.getDialogFont()); 281 GridData viewerData = new GridData(GridData.FILL_BOTH | GridData.GRAB_VERTICAL); 282 viewer.getControl().getParent().setLayoutData(viewerData); 283 284 layoutTreeAreaControl(leftArea); 285 286 return leftArea; 287 } 288 289 290 291 296 public void showOnly(String [] filteredIds) { 297 filteredTree.addFilter(new PreferenceNodeFilter(filteredIds)); 298 } 299 300 304 public void setPageData(Object pageData) { 305 this.pageData = pageData; 306 } 307 308 311 protected void createPage(IPreferenceNode node) { 312 313 super.createPage(node); 314 if (this.pageData == null) { 315 return; 316 } 317 IPreferencePage page = node.getPage(); 319 if (page instanceof PreferencePage) { 320 ((PreferencePage) page).applyData(this.pageData); 321 } 322 323 } 324 325 328 public IPreferencePage getCurrentPage() { 329 return super.getCurrentPage(); 330 } 331 332 335 public boolean openPage(String pageId, Object data) { 336 setPageData(data); 337 setCurrentPageId(pageId); 338 IPreferencePage page = getCurrentPage(); 339 if (page instanceof PreferencePage) { 340 ((PreferencePage) page).applyData(data); 341 } 342 return true; 343 } 344 345 353 public final void setCurrentPageId(final String preferencePageId) { 354 final IPreferenceNode node = findNodeMatching(preferencePageId); 355 if (node != null) { 356 getTreeViewer().setSelection(new StructuredSelection(node)); 357 showPage(node); 358 } 359 } 360 361 364 public IWorkingCopyManager getWorkingCopyManager() { 365 if(workingCopyManager == null){ 366 workingCopyManager = new WorkingCopyManager(); 367 } 368 return workingCopyManager; 369 } 370 373 protected void okPressed() { 374 super.okPressed(); 375 376 if(getReturnCode() == FAILED) { 377 return; 378 } 379 380 if (workingCopyManager != null) { 381 try { 382 workingCopyManager.applyChanges(); 383 } catch (BackingStoreException e) { 384 String msg = e.getMessage(); 385 if (msg == null) { 386 msg = WorkbenchMessages.FilteredPreferenceDialog_PreferenceSaveFailed; 387 } 388 StatusUtil 389 .handleStatus( 390 WorkbenchMessages.PreferencesExportDialog_ErrorDialogTitle 391 + ": " + msg, e, StatusManager.SHOW, getShell()); 393 } 394 } 395 396 Iterator updateIterator = updateJobs.iterator(); 398 while (updateIterator.hasNext()) { 399 ((Job) updateIterator.next()).schedule(); 400 } 401 } 402 403 406 public void registerUpdateJob(Job job){ 407 updateJobs.add(job); 408 } 409 410 415 Control getContainerToolBar(Composite composite) { 416 417 ToolBarManager historyManager = new ToolBarManager(SWT.HORIZONTAL | SWT.FLAT); 418 historyManager.createControl(composite); 419 420 history.createHistoryControls(historyManager.getControl(), historyManager); 421 422 historyManager.update(false); 423 424 return historyManager.getControl(); 425 } 426 427 428 429 432 protected boolean showPage(IPreferenceNode node) { 433 final boolean success = super.showPage(node); 434 if (success) { 435 history.addHistoryEntry(new PreferenceHistoryEntry(node.getId(), node.getLabelText(), 436 null)); 437 } 438 return success; 439 } 440 441 444 public boolean close() { 445 history.dispose(); 446 return super.close(); 447 } 448 449 452 protected Composite createTitleArea(Composite parent) { 453 454 GridLayout parentLayout = (GridLayout) parent.getLayout(); 455 parentLayout.numColumns = 2; 456 parentLayout.marginHeight = 0; 457 parentLayout.marginTop = IDialogConstants.VERTICAL_MARGIN; 458 parent.setLayout(parentLayout); 459 460 Composite titleComposite = super.createTitleArea(parent); 461 462 Composite toolbarArea=new Composite(parent, SWT.NONE); 463 GridLayout toolbarLayout = new GridLayout(); 464 toolbarLayout.marginHeight = 0; 465 toolbarLayout.verticalSpacing = 0; 466 toolbarArea.setLayout(toolbarLayout); 467 toolbarArea.setLayoutData(new GridData(SWT.END, SWT.FILL, false, true)); 468 Control topBar = getContainerToolBar(toolbarArea); 469 topBar.setLayoutData(new GridData(SWT.END, SWT.FILL, false, true)); 470 471 return titleComposite; 472 } 473 474 protected void selectSavedItem() { 475 getTreeViewer().setInput(getPreferenceManager()); 476 super.selectSavedItem(); 477 if(getTreeViewer().getTree().getItemCount() > 1) { 478 Text filterText = filteredTree.getFilterControl(); 480 if (filterText != null) { 481 filterText.setFocus(); 482 } 483 } 484 } 485 486 489 protected void updateTreeFont(Font dialogFont) { 490 applyDialogFont(filteredTree); 491 } 492 } 493 | Popular Tags |