1 11 package org.eclipse.ui.internal.dialogs; 12 13 import org.eclipse.jface.dialogs.IDialogConstants; 14 import org.eclipse.jface.dialogs.MessageDialog; 15 import org.eclipse.jface.viewers.ISelectionChangedListener; 16 import org.eclipse.jface.viewers.IStructuredSelection; 17 import org.eclipse.jface.viewers.SelectionChangedEvent; 18 import org.eclipse.jface.viewers.StructuredSelection; 19 import org.eclipse.jface.viewers.TableViewer; 20 import org.eclipse.jface.viewers.ViewerComparator; 21 import org.eclipse.osgi.util.NLS; 22 import org.eclipse.swt.SWT; 23 import org.eclipse.swt.events.ModifyListener; 24 import org.eclipse.swt.graphics.Font; 25 import org.eclipse.swt.layout.GridData; 26 import org.eclipse.swt.layout.GridLayout; 27 import org.eclipse.swt.widgets.Button; 28 import org.eclipse.swt.widgets.Composite; 29 import org.eclipse.swt.widgets.Control; 30 import org.eclipse.swt.widgets.Label; 31 import org.eclipse.swt.widgets.Shell; 32 import org.eclipse.swt.widgets.Text; 33 import org.eclipse.ui.IPerspectiveDescriptor; 34 import org.eclipse.ui.PlatformUI; 35 import org.eclipse.ui.internal.IWorkbenchHelpContextIds; 36 import org.eclipse.ui.internal.WorkbenchMessages; 37 import org.eclipse.ui.internal.activities.ws.ActivityViewerFilter; 38 import org.eclipse.ui.internal.registry.PerspectiveRegistry; 39 import org.eclipse.ui.model.PerspectiveLabelProvider; 40 41 47 public class SavePerspectiveDialog extends org.eclipse.jface.dialogs.Dialog 48 implements ISelectionChangedListener, ModifyListener { 49 private Text text; 50 51 private TableViewer list; 52 53 private Button okButton; 54 55 private PerspectiveRegistry perspReg; 56 57 private String perspName; 58 59 private IPerspectiveDescriptor persp; 60 61 private IPerspectiveDescriptor initialSelection; 62 63 private boolean ignoreSelection = false; 64 65 final private static int LIST_WIDTH = 40; 66 67 final private static int TEXT_WIDTH = 40; 68 69 final private static int LIST_HEIGHT = 14; 70 71 76 public SavePerspectiveDialog(Shell parentShell, PerspectiveRegistry perspReg) { 77 super(parentShell); 78 this.perspReg = perspReg; 79 } 80 81 84 protected void configureShell(Shell shell) { 85 super.configureShell(shell); 86 shell 87 .setText(WorkbenchMessages.SavePerspective_shellTitle); 88 PlatformUI.getWorkbench().getHelpSystem().setHelp(shell, 89 IWorkbenchHelpContextIds.SAVE_PERSPECTIVE_DIALOG); 90 } 91 92 97 protected void createButtonsForButtonBar(Composite parent) { 98 okButton = createButton(parent, IDialogConstants.OK_ID, 99 IDialogConstants.OK_LABEL, true); 100 createButton(parent, IDialogConstants.CANCEL_ID, 101 IDialogConstants.CANCEL_LABEL, false); 102 updateButtons(); 103 text.setFocus(); 104 } 105 106 113 protected Control createDialogArea(Composite parent) { 114 Font font = parent.getFont(); 115 Composite composite = (Composite) super.createDialogArea(parent); 117 118 Label descLabel = new Label(composite, SWT.WRAP); 120 descLabel.setText(WorkbenchMessages.SavePerspectiveDialog_description); 121 descLabel.setFont(parent.getFont()); 122 123 Label label = new Label(composite, SWT.NONE); 125 GridData data = new GridData(); 126 data.heightHint = 8; 127 label.setLayoutData(data); 128 129 Composite nameGroup = new Composite(composite, SWT.NONE); 131 nameGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 132 GridLayout layout = new GridLayout(); 133 layout.numColumns = 2; 134 layout.marginWidth = layout.marginHeight = 0; 135 nameGroup.setLayout(layout); 136 137 label = new Label(nameGroup, SWT.NONE); 139 label.setText(WorkbenchMessages.SavePerspective_name); 140 label.setFont(font); 141 142 text = new Text(nameGroup, SWT.BORDER); 144 text.setFocus(); 145 data = new GridData(GridData.FILL_HORIZONTAL); 146 data.widthHint = convertWidthInCharsToPixels(TEXT_WIDTH); 147 text.setLayoutData(data); 148 text.setFont(font); 149 text.addModifyListener(this); 150 151 label = new Label(composite, SWT.NONE); 153 data = new GridData(); 154 data.heightHint = 5; 155 label.setLayoutData(data); 156 157 label = new Label(composite, SWT.NONE); 159 label.setText(WorkbenchMessages.SavePerspective_existing); 160 label.setFont(font); 161 162 list = new TableViewer(composite, SWT.H_SCROLL | SWT.V_SCROLL 164 | SWT.BORDER); 165 list.setLabelProvider(new PerspectiveLabelProvider()); 166 list.setContentProvider(new PerspContentProvider()); 167 list.addFilter(new ActivityViewerFilter()); 168 list.setComparator(new ViewerComparator()); 169 list.setInput(perspReg); 170 list.addSelectionChangedListener(this); 171 list.getTable().setFont(font); 172 173 Control ctrl = list.getControl(); 175 GridData spec = new GridData(GridData.FILL_BOTH); 176 spec.widthHint = convertWidthInCharsToPixels(LIST_WIDTH); 177 spec.heightHint = convertHeightInCharsToPixels(LIST_HEIGHT); 178 ctrl.setLayoutData(spec); 179 180 if (initialSelection != null) { 182 StructuredSelection sel = new StructuredSelection(initialSelection); 183 list.setSelection(sel, true); 184 } 185 text.selectAll(); 186 187 return composite; 189 } 190 191 196 public IPerspectiveDescriptor getPersp() { 197 return persp; 198 } 199 200 205 public String getPerspName() { 206 return perspName; 207 } 208 209 212 public void modifyText(org.eclipse.swt.events.ModifyEvent e) { 213 perspName = text.getText(); 215 216 ignoreSelection = true; 218 persp = perspReg.findPerspectiveWithLabel(perspName); 219 if (persp == null) { 220 StructuredSelection sel = new StructuredSelection(); 221 list.setSelection(sel); 222 } else { 223 StructuredSelection sel = new StructuredSelection(persp); 224 list.setSelection(sel); 225 } 226 ignoreSelection = false; 227 228 updateButtons(); 229 } 230 231 239 protected void okPressed() { 240 perspName = text.getText(); 241 persp = perspReg.findPerspectiveWithLabel(perspName); 242 if (persp != null) { 243 String message = NLS.bind(WorkbenchMessages.SavePerspective_overwriteQuestion,perspName ); 245 String [] buttons = new String [] { IDialogConstants.YES_LABEL, 246 IDialogConstants.NO_LABEL, IDialogConstants.CANCEL_LABEL }; 247 MessageDialog d = new MessageDialog(this.getShell(), 248 WorkbenchMessages.SavePerspective_overwriteTitle, 249 null, message, MessageDialog.QUESTION, buttons, 0); 250 251 switch (d.open()) { 252 case 0: break; 254 case 1: return; 256 case 2: cancelPressed(); 258 return; 259 default: 260 return; 261 } 262 } 263 264 super.okPressed(); 265 } 266 267 272 public void selectionChanged(SelectionChangedEvent event) { 273 if (ignoreSelection) { 275 return; 276 } 277 278 IStructuredSelection sel = (IStructuredSelection) list.getSelection(); 280 persp = null; 281 if (!sel.isEmpty()) { 282 persp = (IPerspectiveDescriptor) sel.getFirstElement(); 283 } 284 285 if (persp != null) { 287 perspName = persp.getLabel(); 288 text.setText(perspName); 289 } 290 291 updateButtons(); 292 } 293 294 299 public void setInitialSelection(IPerspectiveDescriptor selectedElement) { 300 initialSelection = selectedElement; 301 } 302 303 306 private void updateButtons() { 307 if (okButton != null) { 308 String label = text.getText(); 309 okButton.setEnabled(perspReg.validateLabel(label)); 310 } 311 } 312 } 313 | Popular Tags |