1 13 package org.eclipse.ui.internal.ide.dialogs; 14 15 import com.ibm.icu.text.Collator; 16 import java.util.ArrayList ; 17 import java.util.Collections ; 18 import java.util.Comparator ; 19 20 import org.eclipse.core.resources.ResourcesPlugin; 21 import org.eclipse.jface.dialogs.Dialog; 22 import org.eclipse.jface.dialogs.ErrorDialog; 23 import org.eclipse.jface.dialogs.IDialogConstants; 24 import org.eclipse.jface.viewers.ISelectionChangedListener; 25 import org.eclipse.jface.viewers.IStructuredContentProvider; 26 import org.eclipse.jface.viewers.IStructuredSelection; 27 import org.eclipse.jface.viewers.SelectionChangedEvent; 28 import org.eclipse.jface.viewers.StructuredSelection; 29 import org.eclipse.jface.viewers.TableViewer; 30 import org.eclipse.jface.viewers.Viewer; 31 import org.eclipse.swt.SWT; 32 import org.eclipse.swt.custom.BusyIndicator; 33 import org.eclipse.swt.events.SelectionAdapter; 34 import org.eclipse.swt.events.SelectionEvent; 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.Button; 39 import org.eclipse.swt.widgets.Composite; 40 import org.eclipse.swt.widgets.Control; 41 import org.eclipse.swt.widgets.Group; 42 import org.eclipse.swt.widgets.Label; 43 import org.eclipse.swt.widgets.Shell; 44 import org.eclipse.ui.IPerspectiveDescriptor; 45 import org.eclipse.ui.IPerspectiveRegistry; 46 import org.eclipse.ui.IWorkbenchPage; 47 import org.eclipse.ui.IWorkbenchWindow; 48 import org.eclipse.ui.PlatformUI; 49 import org.eclipse.ui.WorkbenchException; 50 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages; 51 import org.eclipse.ui.internal.ide.IIDEHelpContextIds; 52 import org.eclipse.ui.model.PerspectiveLabelProvider; 53 54 public class ProjectPerspectiveChoiceDialog extends Dialog { 55 private static final int MIN_DIALOG_WIDTH = 200; 56 57 private static final int MIN_DIALOG_HEIGHT = 250; 58 59 private IWorkbenchWindow window; 60 61 private ArrayList persps; 62 63 private IPerspectiveDescriptor chosenPersp; 64 65 private boolean sameWindow = true; 66 67 private TableViewer list; 68 69 private Comparator comparator = new Comparator () { 70 private Collator collator = Collator.getInstance(); 71 72 public int compare(Object ob1, Object ob2) { 73 IPerspectiveDescriptor d1 = (IPerspectiveDescriptor) ob1; 74 IPerspectiveDescriptor d2 = (IPerspectiveDescriptor) ob2; 75 return collator.compare(d1.getLabel(), d2.getLabel()); 76 } 77 }; 78 79 84 public ProjectPerspectiveChoiceDialog(IWorkbenchWindow window, 85 String [] perspIds) { 86 super(window.getShell()); 87 this.window = window; 88 89 IWorkbenchPage page = window.getActivePage(); 90 if (page != null) { 91 chosenPersp = page.getPerspective(); 92 } 93 94 IPerspectiveRegistry reg = window.getWorkbench() 95 .getPerspectiveRegistry(); 96 persps = new ArrayList (perspIds.length); 97 for (int i = 0; i < perspIds.length; i++) { 98 IPerspectiveDescriptor desc; 99 desc = reg.findPerspectiveWithId(perspIds[i]); 100 if (desc != null && desc != chosenPersp) { 101 persps.add(desc); 102 } 103 } 104 Collections.sort(persps, comparator); 105 106 if (chosenPersp != null) { 107 persps.add(0, chosenPersp); 108 } 109 } 110 111 114 protected void configureShell(Shell shell) { 115 super.configureShell(shell); 116 shell.setText(IDEWorkbenchMessages.ProjectPerspectiveChoiceDialog_title); 117 PlatformUI.getWorkbench().getHelpSystem().setHelp(shell, 118 IIDEHelpContextIds.SHOW_PROJECT_PERSPECTIVE_DIALOG); 119 } 120 121 124 protected void createButtonsForButtonBar(Composite parent) { 125 createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, 126 true); 127 } 128 129 132 protected Control createDialogArea(Composite parent) { 133 Composite composite = (Composite) super.createDialogArea(parent); 135 136 composite.setLayout(new GridLayout()); 137 GridData data = new GridData(GridData.FILL_BOTH); 138 data.widthHint = MIN_DIALOG_WIDTH; 139 data.heightHint = MIN_DIALOG_HEIGHT; 140 composite.setLayoutData(data); 141 142 createPerspectiveGroup(composite); 143 createOptionGroup(composite); 144 145 if (chosenPersp != null) { 146 list.setSelection(new StructuredSelection(chosenPersp)); 147 } 148 149 return composite; 150 } 151 152 157 private void createPerspectiveGroup(Composite parent) { 158 Font font = parent.getFont(); 159 Label label = new Label(parent, SWT.NONE); 161 label.setFont(font); 162 label.setText(IDEWorkbenchMessages.ProjectPerspectiveChoiceDialog_choosePerspective); 163 164 list = new TableViewer(parent, SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER); 166 list.setLabelProvider(new PerspectiveLabelProvider(false)); 167 list.setContentProvider(new PerspectiveContentProvider()); 168 list.setInput(persps); 169 list.addSelectionChangedListener(new SelectionListener()); 170 list.getTable().setLayoutData(new GridData(GridData.FILL_BOTH)); 171 list.getTable().setFont(font); 172 } 173 174 179 private void createOptionGroup(Composite parent) { 180 Font font = parent.getFont(); 181 Group optionGroup = new Group(parent, SWT.LEFT); 183 GridLayout layout = new GridLayout(); 184 optionGroup.setLayout(layout); 185 GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL 186 | GridData.GRAB_HORIZONTAL); 187 optionGroup.setLayoutData(data); 188 optionGroup.setFont(font); 189 optionGroup.setText(IDEWorkbenchMessages.ProjectPerspectiveChoiceDialog_options); 190 191 Button button = new Button(optionGroup, SWT.RADIO | SWT.LEFT); 193 button.setText(IDEWorkbenchMessages.ProjectPerspectiveChoiceDialog_sameWindow); 194 data = new GridData(); 195 button.setLayoutData(data); 196 button.setFont(font); 197 button.setSelection(sameWindow); 198 button.addSelectionListener(new SelectionAdapter() { 199 public void widgetSelected(SelectionEvent e) { 200 sameWindow = true; 201 } 202 }); 203 204 button = new Button(optionGroup, SWT.RADIO | SWT.LEFT); 206 button.setText(IDEWorkbenchMessages.ProjectPerspectiveChoiceDialog_newWindow); 207 data = new GridData(); 208 button.setLayoutData(data); 209 button.setFont(font); 210 button.setSelection(!sameWindow); 211 button.addSelectionListener(new SelectionAdapter() { 212 public void widgetSelected(SelectionEvent e) { 213 sameWindow = false; 214 } 215 }); 216 } 217 218 224 public IWorkbenchWindow showChosenPerspective() { 225 if (chosenPersp == null) { 226 return window; 227 } 228 229 final IWorkbenchWindow[] results = new IWorkbenchWindow[1]; 230 final WorkbenchException[] errors = new WorkbenchException[1]; 231 BusyIndicator.showWhile(window.getShell().getDisplay(), new Runnable () { 232 public void run() { 233 if (sameWindow) { 234 results[0] = window; 235 IWorkbenchPage page = window.getActivePage(); 236 if (page != null) { 237 page.setPerspective(chosenPersp); 238 } 239 } else { 240 try { 241 results[0] = window.getWorkbench().openWorkbenchWindow( 242 chosenPersp.getId(), 243 ResourcesPlugin.getWorkspace().getRoot()); 244 } catch (WorkbenchException e) { 245 errors[0] = e; 246 } 247 } 248 } 249 }); 250 251 IWorkbenchWindow result = results[0]; 252 results[0] = null; 253 254 if (errors[0] != null) { 255 ErrorDialog 256 .openError( 257 window.getShell(), 258 IDEWorkbenchMessages.ProjectPerspectiveChoiceDialog_errorTitle, 259 IDEWorkbenchMessages.ProjectPerspectiveChoiceDialog_errorMessage, 260 errors[0].getStatus()); 261 errors[0] = null; 262 return window; 263 } 264 265 return result; 266 } 267 268 class PerspectiveContentProvider implements IStructuredContentProvider { 269 public PerspectiveContentProvider() { 270 super(); 271 } 272 273 public void dispose() { 274 } 275 276 public Object [] getElements(Object element) { 277 if (element instanceof ArrayList ) { 278 return ((ArrayList ) element).toArray(); 279 } 280 return null; 281 } 282 283 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { 284 } 285 286 public boolean isDeleted(Object element) { 287 return false; 288 } 289 } 290 291 class SelectionListener implements ISelectionChangedListener { 292 public SelectionListener() { 293 super(); 294 } 295 296 public void selectionChanged(SelectionChangedEvent event) { 297 IStructuredSelection selection = (IStructuredSelection) event 298 .getSelection(); 299 if (selection != null) { 300 chosenPersp = (IPerspectiveDescriptor) selection 301 .getFirstElement(); 302 } 303 } 304 } 305 } 306 | Popular Tags |