|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2013 Rabea Gransberger and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* Rabea Gransberger - initial API and implementation |
| 10 |
******************************************************************************/ |
| 11 |
|
| 12 |
package org.eclipse.ui.dialogs; |
| 13 |
|
| 14 |
import java.text.MessageFormat; |
| 15 |
|
| 16 |
import org.eclipse.core.resources.IFile; |
| 17 |
import org.eclipse.jface.dialogs.IDialogConstants; |
| 18 |
import org.eclipse.jface.dialogs.IMessageProvider; |
| 19 |
import org.eclipse.jface.dialogs.TitleAreaDialog; |
| 20 |
import org.eclipse.jface.layout.GridDataFactory; |
| 21 |
import org.eclipse.swt.SWT; |
| 22 |
import org.eclipse.swt.events.SelectionAdapter; |
| 23 |
import org.eclipse.swt.events.SelectionEvent; |
| 24 |
import org.eclipse.swt.layout.GridData; |
| 25 |
import org.eclipse.swt.layout.GridLayout; |
| 26 |
import org.eclipse.swt.widgets.Button; |
| 27 |
import org.eclipse.swt.widgets.Combo; |
| 28 |
import org.eclipse.swt.widgets.Composite; |
| 29 |
import org.eclipse.swt.widgets.Control; |
| 30 |
import org.eclipse.swt.widgets.Shell; |
| 31 |
import org.eclipse.swt.widgets.Text; |
| 32 |
import org.eclipse.ui.IFileEditorMapping; |
| 33 |
import org.eclipse.ui.commands.ResetEditorHandler; |
| 34 |
import org.eclipse.ui.commands.ResetEditorHandler.IExtensionSelection; |
| 35 |
import org.eclipse.ui.internal.WorkbenchPlugin; |
| 36 |
import org.eclipse.ui.internal.ide.IDEWorkbenchMessages; |
| 37 |
|
| 38 |
/** |
| 39 |
* Dialog to select the extensions for the {@link ResetEditorHandler}. |
| 40 |
* |
| 41 |
* @since 3.9 |
| 42 |
*/ |
| 43 |
public class ResetEditorExtensionDialog extends TitleAreaDialog { |
| 44 |
|
| 45 |
private Button allButton; |
| 46 |
|
| 47 |
private Button comboButton; |
| 48 |
|
| 49 |
private Combo extensionCombo; |
| 50 |
|
| 51 |
private Button textButton; |
| 52 |
|
| 53 |
private Text extensionInputText; |
| 54 |
|
| 55 |
private IExtensionSelection selection; |
| 56 |
|
| 57 |
private final String resourceNames; |
| 58 |
|
| 59 |
|
| 60 |
/** |
| 61 |
* Constructs a new dialog to select file extensions for editor reset. |
| 62 |
* |
| 63 |
* @param parentShell The parent shell for this dialog. |
| 64 |
* @param resourceNames Names of the selected resources |
| 65 |
*/ |
| 66 |
public ResetEditorExtensionDialog(Shell parentShell, String resourceNames) { |
| 67 |
super(parentShell); |
| 68 |
this.resourceNames= resourceNames; |
| 69 |
} |
| 70 |
|
| 71 |
/* (non-Javadoc) |
| 72 |
* @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell) |
| 73 |
*/ |
| 74 |
protected void configureShell(Shell shell) { |
| 75 |
super.configureShell(shell); |
| 76 |
shell.setText(IDEWorkbenchMessages.ResetEditorExtensionDialog_shellTitle); |
| 77 |
//TODO |
| 78 |
// PlatformUI.getWorkbench().getHelpSystem().setHelp(shell, helpContextId); |
| 79 |
} |
| 80 |
|
| 81 |
/* (non-Javadoc) |
| 82 |
* @see org.eclipse.jface.window.Window#canHandleShellCloseEvent() |
| 83 |
*/ |
| 84 |
protected boolean canHandleShellCloseEvent() { |
| 85 |
return false; |
| 86 |
} |
| 87 |
|
| 88 |
/* (non-Javadoc) |
| 89 |
* @see org.eclipse.jface.dialogs.TitleAreaDialog#createDialogArea(org.eclipse.swt.widgets.Composite) |
| 90 |
*/ |
| 91 |
protected Control createDialogArea(Composite parent) { |
| 92 |
setTitle(IDEWorkbenchMessages.ResetEditorExtensionDialog_dialogTitle); |
| 93 |
setMessage(MessageFormat.format(IDEWorkbenchMessages.ResetEditorExtensionDialog_dialogMessage, new Object[] { resourceNames })); |
| 94 |
|
| 95 |
Composite result= new Composite(parent, SWT.NONE); |
| 96 |
GridLayout layout= new GridLayout(); |
| 97 |
layout.marginHeight= convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN); |
| 98 |
layout.marginWidth= convertVerticalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN); |
| 99 |
layout.verticalSpacing= convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING); |
| 100 |
layout.horizontalSpacing= convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING); |
| 101 |
result.setLayout(layout); |
| 102 |
|
| 103 |
this.allButton= addRadioButton(result, IDEWorkbenchMessages.ResetEditorExtensionDialog_buttonAll); |
| 104 |
this.allButton.setToolTipText(IDEWorkbenchMessages.ResetEditorExtensionDialog_buttonAll_Tooltip); |
| 105 |
|
| 106 |
this.comboButton= addRadioButton(result, IDEWorkbenchMessages.ResetEditorExtensionDialog_buttonSelection); |
| 107 |
this.comboButton.setToolTipText(IDEWorkbenchMessages.ResetEditorExtensionDialog_buttonSelection_toolip); |
| 108 |
|
| 109 |
this.extensionCombo= new Combo(result, SWT.READ_ONLY); |
| 110 |
this.extensionCombo.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL)); |
| 111 |
GridDataFactory.fillDefaults().indent(15, SWT.DEFAULT).applyTo(this.extensionCombo); |
| 112 |
|
| 113 |
this.textButton= addRadioButton(result, IDEWorkbenchMessages.ResetEditorExtensionDialog_buttonText); |
| 114 |
this.textButton.setToolTipText(IDEWorkbenchMessages.ResetEditorExtensionDialog_buttonText_tooltip); |
| 115 |
this.extensionInputText= new Text(result, SWT.BORDER); |
| 116 |
GridDataFactory.fillDefaults().indent(15, SWT.DEFAULT).applyTo(this.extensionInputText); |
| 117 |
|
| 118 |
this.extensionInputText.setEnabled(false); |
| 119 |
this.extensionCombo.setEnabled(false); |
| 120 |
|
| 121 |
fillExtensionCombo(); |
| 122 |
|
| 123 |
this.comboButton.addSelectionListener(new SelectionAdapter() { |
| 124 |
public void widgetSelected(SelectionEvent e) { |
| 125 |
extensionCombo.setEnabled(comboButton.getSelection()); |
| 126 |
} |
| 127 |
}); |
| 128 |
|
| 129 |
this.textButton.addSelectionListener(new SelectionAdapter() { |
| 130 |
public void widgetSelected(SelectionEvent e) { |
| 131 |
extensionInputText.setEnabled(textButton.getSelection()); |
| 132 |
} |
| 133 |
}); |
| 134 |
return result; |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
/* (non-Javadoc) |
| 140 |
* @see org.eclipse.jface.dialogs.Dialog#okPressed() |
| 141 |
*/ |
| 142 |
protected void okPressed() { |
| 143 |
this.selection= null; |
| 144 |
if (this.allButton.getSelection()) { |
| 145 |
this.selection= new AllExtensionMatcher(); |
| 146 |
} |
| 147 |
else if (this.comboButton.getSelection()) { |
| 148 |
String temp= (String) this.extensionCombo.getData(this.extensionCombo.getText()); |
| 149 |
this.selection= new OneExtensionMatcher(temp); |
| 150 |
} |
| 151 |
else { |
| 152 |
String temp= this.extensionInputText.getText().trim(); |
| 153 |
if (!temp.startsWith(".")) { //$NON-NLS-1$ |
| 154 |
setMessage(IDEWorkbenchMessages.ResetEditorExtensionDialog_wrongText, IMessageProvider.ERROR); |
| 155 |
return; |
| 156 |
} |
| 157 |
this.selection= new OneExtensionMatcher(temp); |
| 158 |
} |
| 159 |
|
| 160 |
super.okPressed(); |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
private void fillExtensionCombo() { |
| 165 |
IFileEditorMapping[] array= WorkbenchPlugin.getDefault() |
| 166 |
.getEditorRegistry().getFileEditorMappings(); |
| 167 |
String[] extensions= new String[array.length]; |
| 168 |
for (int i= 0; i < array.length; i++) { |
| 169 |
extensions[i]= array[i].getLabel(); |
| 170 |
extensionCombo.setData(array[i].getLabel(), array[i].getExtension()); |
| 171 |
} |
| 172 |
extensionCombo.setItems(extensions); |
| 173 |
extensionCombo.select(0); |
| 174 |
} |
| 175 |
|
| 176 |
private Button addRadioButton(Composite parent, String label) { |
| 177 |
GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_FILL); |
| 178 |
|
| 179 |
Button button= new Button(parent, SWT.RADIO); |
| 180 |
button.setText(label); |
| 181 |
button.setLayoutData(gd); |
| 182 |
|
| 183 |
return button; |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Gets the selected extension. |
| 188 |
* |
| 189 |
* @return The selected extensions, never <code>null</code>. |
| 190 |
*/ |
| 191 |
public IExtensionSelection getExtension() { |
| 192 |
//TODO non API return type |
| 193 |
return this.selection; |
| 194 |
} |
| 195 |
|
| 196 |
private static class AllExtensionMatcher implements IExtensionSelection { |
| 197 |
|
| 198 |
/* (non-Javadoc) |
| 199 |
* @see org.eclipse.ui.commands.ResetEditorHandler.ExtensionMatcher#matches(org.eclipse.core.resources.IFile) |
| 200 |
*/ |
| 201 |
public boolean matches(IFile resource) { |
| 202 |
return true; |
| 203 |
} |
| 204 |
|
| 205 |
/* (non-Javadoc) |
| 206 |
* @see org.eclipse.ui.commands.ResetEditorHandler.ExtensionMatcher#getDescription() |
| 207 |
*/ |
| 208 |
public String getDescription() { |
| 209 |
return IDEWorkbenchMessages.ResetEditorExtensionDialog_allDescription; |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
private static class OneExtensionMatcher implements IExtensionSelection { |
| 214 |
|
| 215 |
private final String extension; |
| 216 |
|
| 217 |
public OneExtensionMatcher(String extension) { |
| 218 |
this.extension= extension; |
| 219 |
} |
| 220 |
|
| 221 |
/* (non-Javadoc) |
| 222 |
* @see org.eclipse.ui.commands.ResetEditorHandler.ExtensionMatcher#matches(org.eclipse.core.resources.IFile) |
| 223 |
*/ |
| 224 |
public boolean matches(IFile resource) { |
| 225 |
return extension.equals(resource.getFileExtension()); |
| 226 |
} |
| 227 |
|
| 228 |
/* (non-Javadoc) |
| 229 |
* @see org.eclipse.ui.commands.ResetEditorHandler.ExtensionMatcher#getDescription() |
| 230 |
*/ |
| 231 |
public String getDescription() { |
| 232 |
return MessageFormat.format(IDEWorkbenchMessages.ResetEditorExtensionDialog_extensionDescription, new Object[] { extension }); |
| 233 |
} |
| 234 |
|
| 235 |
} |
| 236 |
|
| 237 |
} |