1 11 package org.eclipse.ui.internal.dialogs; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 16 import org.eclipse.jface.dialogs.Dialog; 17 import org.eclipse.jface.dialogs.IDialogConstants; 18 import org.eclipse.jface.dialogs.IDialogSettings; 19 import org.eclipse.jface.viewers.DoubleClickEvent; 20 import org.eclipse.jface.viewers.IDoubleClickListener; 21 import org.eclipse.jface.viewers.ISelectionChangedListener; 22 import org.eclipse.jface.viewers.IStructuredSelection; 23 import org.eclipse.jface.viewers.ITreeContentProvider; 24 import org.eclipse.jface.viewers.SelectionChangedEvent; 25 import org.eclipse.jface.viewers.StructuredSelection; 26 import org.eclipse.jface.viewers.TreeViewer; 27 import org.eclipse.swt.SWT; 28 import org.eclipse.swt.events.DisposeEvent; 29 import org.eclipse.swt.events.DisposeListener; 30 import org.eclipse.swt.graphics.Color; 31 import org.eclipse.swt.graphics.RGB; 32 import org.eclipse.swt.layout.GridData; 33 import org.eclipse.swt.widgets.Button; 34 import org.eclipse.swt.widgets.Composite; 35 import org.eclipse.swt.widgets.Control; 36 import org.eclipse.swt.widgets.Shell; 37 import org.eclipse.swt.widgets.Text; 38 import org.eclipse.ui.IWorkbenchWindow; 39 import org.eclipse.ui.PlatformUI; 40 import org.eclipse.ui.dialogs.FilteredTree; 41 import org.eclipse.ui.dialogs.PatternFilter; 42 import org.eclipse.ui.internal.IWorkbenchHelpContextIds; 43 import org.eclipse.ui.internal.WorkbenchMessages; 44 import org.eclipse.ui.internal.WorkbenchPlugin; 45 import org.eclipse.ui.internal.registry.ViewRegistry; 46 import org.eclipse.ui.views.IViewCategory; 47 import org.eclipse.ui.views.IViewDescriptor; 48 import org.eclipse.ui.views.IViewRegistry; 49 50 53 public class ShowViewDialog extends Dialog implements 54 ISelectionChangedListener, IDoubleClickListener { 55 56 private static final String DIALOG_SETTING_SECTION_NAME = "ShowViewDialog"; 58 private static final int LIST_HEIGHT = 300; 59 60 private static final int LIST_WIDTH = 250; 61 62 private static final String STORE_EXPANDED_CATEGORIES_ID = DIALOG_SETTING_SECTION_NAME 63 + ".STORE_EXPANDED_CATEGORIES_ID"; 65 private static final String STORE_SELECTED_VIEW_ID = DIALOG_SETTING_SECTION_NAME 66 + ".STORE_SELECTED_VIEW_ID"; 68 private FilteredTree filteredTree; 69 70 private Button okButton; 71 72 private IViewDescriptor[] viewDescs = new IViewDescriptor[0]; 73 74 private IViewRegistry viewReg; 75 76 private IWorkbenchWindow window; 77 78 private Color dimmedForeground; 79 80 86 public ShowViewDialog(IWorkbenchWindow window, IViewRegistry viewReg) { 87 super(window.getShell()); 88 this.window = window; 89 this.viewReg = viewReg; 90 setShellStyle(getShellStyle() | SWT.RESIZE); 91 } 92 93 96 protected void buttonPressed(int buttonId) { 97 if (buttonId == IDialogConstants.OK_ID) { 98 saveWidgetValues(); 99 } 100 super.buttonPressed(buttonId); 101 } 102 103 106 protected void cancelPressed() { 107 viewDescs = new IViewDescriptor[0]; 108 super.cancelPressed(); 109 } 110 111 116 protected void configureShell(Shell shell) { 117 super.configureShell(shell); 118 shell.setText(WorkbenchMessages.ShowView_shellTitle); 119 PlatformUI.getWorkbench().getHelpSystem().setHelp(shell, 120 IWorkbenchHelpContextIds.SHOW_VIEW_DIALOG); 121 } 122 123 133 protected void createButtonsForButtonBar(Composite parent) { 134 okButton = createButton(parent, IDialogConstants.OK_ID, 135 IDialogConstants.OK_LABEL, true); 136 createButton(parent, IDialogConstants.CANCEL_ID, 137 IDialogConstants.CANCEL_LABEL, false); 138 updateButtons(); 139 } 140 141 148 protected Control createDialogArea(Composite parent) { 149 Composite composite = (Composite) super.createDialogArea(parent); 151 composite.setFont(parent.getFont()); 152 153 createFilteredTreeViewer(composite); 154 155 layoutTopControl(filteredTree); 156 157 restoreWidgetValues(); 159 160 return composite; 162 } 163 164 177 private static RGB blend(RGB c1, RGB c2, int ratio) { 178 int r = blend(c1.red, c2.red, ratio); 179 int g = blend(c1.green, c2.green, ratio); 180 int b = blend(c1.blue, c2.blue, ratio); 181 return new RGB(r, g, b); 182 } 183 184 private static int blend(int v1, int v2, int ratio) { 185 int b = (ratio * v1 + (100 - ratio) * v2) / 100; 186 return Math.min(255, b); 187 } 188 189 194 private void createFilteredTreeViewer(Composite parent) { 195 PatternFilter filter = new ViewPatternFilter(); 196 int styleBits = SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER; 197 filteredTree = new FilteredTree(parent, styleBits, filter); 198 filteredTree.setBackground(parent.getDisplay().getSystemColor( 199 SWT.COLOR_WIDGET_BACKGROUND)); 200 201 TreeViewer treeViewer = filteredTree.getViewer(); 202 Control treeControl = treeViewer.getControl(); 203 RGB dimmedRGB = blend(treeControl.getForeground().getRGB(), treeControl.getBackground().getRGB(), 60); 204 dimmedForeground = new Color(treeControl.getDisplay(), dimmedRGB); 205 treeControl.addDisposeListener(new DisposeListener() { 206 public void widgetDisposed(DisposeEvent e) { 207 dimmedForeground.dispose(); 208 } 209 }); 210 211 treeViewer.setLabelProvider(new ViewLabelProvider(window, dimmedForeground)); 212 treeViewer.setContentProvider(new ViewContentProvider()); 213 treeViewer.setComparator(new ViewComparator((ViewRegistry) viewReg)); 214 treeViewer.setInput(viewReg); 215 treeViewer.addSelectionChangedListener(this); 216 treeViewer.addDoubleClickListener(this); 217 treeViewer.addFilter(new CapabilityFilter()); 218 219 if (hasAtMostOneView(filteredTree.getViewer())) { 221 Text filterText = filteredTree.getFilterControl(); 222 if (filterText != null) { 223 filterText.setEnabled(false); 224 } 225 } 226 227 applyDialogFont(filteredTree); 228 } 229 230 236 private boolean hasAtMostOneView(TreeViewer tree) { 237 ITreeContentProvider contentProvider = (ITreeContentProvider) tree 238 .getContentProvider(); 239 Object [] children = contentProvider.getElements(tree.getInput()); 240 241 if (children.length <= 1) { 242 if (children.length == 0) { 243 return true; 244 } 245 return !contentProvider.hasChildren(children[0]); 246 } 247 return false; 248 } 249 250 255 public void doubleClick(DoubleClickEvent event) { 256 IStructuredSelection s = (IStructuredSelection) event.getSelection(); 257 Object element = s.getFirstElement(); 258 if (filteredTree.getViewer().isExpandable(element)) { 259 filteredTree.getViewer().setExpandedState(element, !filteredTree.getViewer().getExpandedState(element)); 260 } else if (viewDescs.length > 0) { 261 saveWidgetValues(); 262 setReturnCode(OK); 263 close(); 264 } 265 } 266 267 270 protected IDialogSettings getDialogSettings() { 271 IDialogSettings workbenchSettings = WorkbenchPlugin.getDefault() 272 .getDialogSettings(); 273 IDialogSettings section = workbenchSettings 274 .getSection(DIALOG_SETTING_SECTION_NAME); 275 if (section == null) { 276 section = workbenchSettings 277 .addNewSection(DIALOG_SETTING_SECTION_NAME); 278 } 279 return section; 280 } 281 282 287 public IViewDescriptor[] getSelection() { 288 return viewDescs; 289 } 290 291 296 private void layoutTopControl(Control control) { 297 GridData spec = new GridData(GridData.FILL_BOTH); 298 spec.widthHint = LIST_WIDTH; 299 spec.heightHint = LIST_HEIGHT; 300 control.setLayoutData(spec); 301 } 302 303 307 protected void restoreWidgetValues() { 308 IDialogSettings settings = getDialogSettings(); 309 310 String [] expandedCategoryIds = settings 311 .getArray(STORE_EXPANDED_CATEGORIES_ID); 312 if (expandedCategoryIds == null) { 313 return; 314 } 315 316 ViewRegistry reg = (ViewRegistry) viewReg; 317 ArrayList categoriesToExpand = new ArrayList (expandedCategoryIds.length); 318 for (int i = 0; i < expandedCategoryIds.length; i++) { 319 IViewCategory category = reg.findCategory(expandedCategoryIds[i]); 320 if (category != null) { 321 categoriesToExpand.add(category); 322 } 323 } 324 325 if (!categoriesToExpand.isEmpty()) { 326 filteredTree.getViewer().setExpandedElements(categoriesToExpand.toArray()); 327 } 328 329 String selectedViewId = settings.get(STORE_SELECTED_VIEW_ID); 330 if (selectedViewId != null) { 331 IViewDescriptor viewDesc = reg.find(selectedViewId); 332 if (viewDesc != null) { 333 filteredTree.getViewer().setSelection(new StructuredSelection(viewDesc), true); 334 } 335 } 336 } 337 338 342 protected void saveWidgetValues() { 343 IDialogSettings settings = getDialogSettings(); 344 345 Object [] expandedElements = filteredTree.getViewer().getExpandedElements(); 347 String [] expandedCategoryIds = new String [expandedElements.length]; 348 for (int i = 0; i < expandedElements.length; ++i) { 349 expandedCategoryIds[i] = ((IViewCategory) expandedElements[i]).getId(); 350 } 351 352 settings.put(STORE_EXPANDED_CATEGORIES_ID, expandedCategoryIds); 354 355 String selectedViewId = ""; if (viewDescs.length > 0) { 357 selectedViewId = viewDescs[0].getId(); 360 } 361 settings.put(STORE_SELECTED_VIEW_ID, selectedViewId); 362 } 363 364 369 public void selectionChanged(SelectionChangedEvent event) { 370 updateSelection(event); 371 updateButtons(); 372 } 373 374 377 protected void updateButtons() { 378 if (okButton != null) { 379 okButton.setEnabled(getSelection().length > 0); 380 } 381 } 382 383 386 protected void updateSelection(SelectionChangedEvent event) { 387 ArrayList descs = new ArrayList (); 388 IStructuredSelection sel = (IStructuredSelection) event.getSelection(); 389 for (Iterator i = sel.iterator(); i.hasNext();) { 390 Object o = i.next(); 391 if (o instanceof IViewDescriptor) { 392 descs.add(o); 393 } 394 } 395 viewDescs = new IViewDescriptor[descs.size()]; 396 descs.toArray(viewDescs); 397 } 398 } 399 | Popular Tags |