1 13 14 package org.eclipse.ui.internal.dialogs; 15 16 import org.eclipse.jface.dialogs.Dialog; 17 import org.eclipse.jface.dialogs.IDialogConstants; 18 import org.eclipse.jface.viewers.DoubleClickEvent; 19 import org.eclipse.jface.viewers.IDoubleClickListener; 20 import org.eclipse.jface.viewers.ISelectionChangedListener; 21 import org.eclipse.jface.viewers.IStructuredSelection; 22 import org.eclipse.jface.viewers.SelectionChangedEvent; 23 import org.eclipse.jface.viewers.TableViewer; 24 import org.eclipse.jface.viewers.ViewerComparator; 25 import org.eclipse.swt.SWT; 26 import org.eclipse.swt.events.SelectionAdapter; 27 import org.eclipse.swt.events.SelectionEvent; 28 import org.eclipse.swt.layout.GridData; 29 import org.eclipse.swt.widgets.Button; 30 import org.eclipse.swt.widgets.Composite; 31 import org.eclipse.swt.widgets.Control; 32 import org.eclipse.swt.widgets.Shell; 33 import org.eclipse.ui.IPerspectiveDescriptor; 34 import org.eclipse.ui.IPerspectiveRegistry; 35 import org.eclipse.ui.PlatformUI; 36 import org.eclipse.ui.activities.ITriggerPoint; 37 import org.eclipse.ui.activities.WorkbenchActivityHelper; 38 import org.eclipse.ui.internal.IWorkbenchHelpContextIds; 39 import org.eclipse.ui.internal.WorkbenchMessages; 40 import org.eclipse.ui.internal.activities.ws.ActivityMessages; 41 import org.eclipse.ui.internal.activities.ws.ActivityViewerFilter; 42 import org.eclipse.ui.internal.activities.ws.WorkbenchTriggerPoints; 43 import org.eclipse.ui.model.PerspectiveLabelProvider; 44 45 48 public class SelectPerspectiveDialog extends Dialog implements 49 ISelectionChangedListener { 50 51 final private static int LIST_HEIGHT = 300; 52 53 final private static int LIST_WIDTH = 300; 54 55 private TableViewer list; 56 57 private Button okButton; 58 59 private IPerspectiveDescriptor perspDesc; 60 61 private IPerspectiveRegistry perspReg; 62 63 private ActivityViewerFilter activityViewerFilter = new ActivityViewerFilter(); 64 65 private Button showAllButton; 66 67 73 public SelectPerspectiveDialog(Shell parentShell, 74 IPerspectiveRegistry perspReg) { 75 super(parentShell); 76 this.perspReg = perspReg; 77 setShellStyle(getShellStyle() | SWT.RESIZE); 78 } 79 80 85 protected void cancelPressed() { 86 perspDesc = null; 87 super.cancelPressed(); 88 } 89 90 95 protected void configureShell(Shell shell) { 96 super.configureShell(shell); 97 shell.setText(WorkbenchMessages.SelectPerspective_shellTitle); 98 PlatformUI.getWorkbench().getHelpSystem().setHelp(shell, 99 IWorkbenchHelpContextIds.SELECT_PERSPECTIVE_DIALOG); 100 } 101 102 112 protected void createButtonsForButtonBar(Composite parent) { 113 okButton = createButton(parent, IDialogConstants.OK_ID, 114 IDialogConstants.OK_LABEL, true); 115 createButton(parent, IDialogConstants.CANCEL_ID, 116 IDialogConstants.CANCEL_LABEL, false); 117 updateButtons(); 118 } 119 120 127 protected Control createDialogArea(Composite parent) { 128 Composite composite = (Composite) super.createDialogArea(parent); 130 composite.setFont(parent.getFont()); 131 132 createViewer(composite); 133 layoutTopControl(list.getControl()); 134 if (needsShowAllButton()) { 135 createShowAllButton(composite); 136 } 137 return composite; 139 } 140 141 144 private boolean needsShowAllButton() { 145 return activityViewerFilter.getHasEncounteredFilteredItem(); 146 } 147 148 153 private void createShowAllButton(Composite parent) { 154 showAllButton = new Button(parent, SWT.CHECK); 155 showAllButton 156 .setText(ActivityMessages.Perspective_showAll); 157 showAllButton.addSelectionListener(new SelectionAdapter() { 158 159 162 public void widgetSelected(SelectionEvent e) { 163 if (showAllButton.getSelection()) { 164 list.resetFilters(); 165 } else { 166 list.addFilter(activityViewerFilter); 167 } 168 } 169 }); 170 171 } 172 173 178 private void createViewer(Composite parent) { 179 list = new TableViewer(parent, SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL 181 | SWT.BORDER); 182 list.getTable().setFont(parent.getFont()); 183 list.setLabelProvider(new PerspectiveLabelProvider()); 184 list.setContentProvider(new PerspContentProvider()); 185 list.addFilter(activityViewerFilter); 186 list.setComparator(new ViewerComparator()); 187 list.setInput(perspReg); 188 list.addSelectionChangedListener(this); 189 list.addDoubleClickListener(new IDoubleClickListener() { 190 public void doubleClick(DoubleClickEvent event) { 191 handleDoubleClickEvent(); 192 } 193 }); 194 } 195 196 201 public IPerspectiveDescriptor getSelection() { 202 return perspDesc; 203 } 204 205 208 protected void handleDoubleClickEvent() { 209 okPressed(); 210 } 211 212 217 private void layoutTopControl(Control control) { 218 GridData spec = new GridData(GridData.FILL_BOTH); 219 spec.widthHint = LIST_WIDTH; 220 spec.heightHint = LIST_HEIGHT; 221 control.setLayoutData(spec); 222 } 223 224 229 public void selectionChanged(SelectionChangedEvent event) { 230 updateSelection(event); 231 updateButtons(); 232 } 233 234 237 protected void updateButtons() { 238 okButton.setEnabled(getSelection() != null); 239 } 240 241 244 protected void updateSelection(SelectionChangedEvent event) { 245 perspDesc = null; 246 IStructuredSelection sel = (IStructuredSelection) event.getSelection(); 247 if (!sel.isEmpty()) { 248 Object obj = sel.getFirstElement(); 249 if (obj instanceof IPerspectiveDescriptor) { 250 perspDesc = (IPerspectiveDescriptor) obj; 251 } 252 } 253 } 254 255 258 protected void okPressed() { 259 ITriggerPoint triggerPoint = PlatformUI.getWorkbench() 260 .getActivitySupport().getTriggerPointManager().getTriggerPoint( 261 WorkbenchTriggerPoints.OPEN_PERSPECITVE_DIALOG); 262 if (WorkbenchActivityHelper.allowUseOf(triggerPoint, getSelection())) { 263 super.okPressed(); 264 } 265 } 266 } 267 | Popular Tags |