1 11 12 package org.eclipse.ui.texteditor; 13 14 import java.util.ResourceBundle ; 15 16 import org.eclipse.swt.SWT; 17 import org.eclipse.swt.layout.GridData; 18 import org.eclipse.swt.layout.GridLayout; 19 import org.eclipse.swt.widgets.Button; 20 import org.eclipse.swt.widgets.Composite; 21 import org.eclipse.swt.widgets.Control; 22 import org.eclipse.swt.widgets.Shell; 23 24 import org.eclipse.core.resources.IResource; 25 26 import org.eclipse.jface.dialogs.Dialog; 27 import org.eclipse.jface.dialogs.DialogPage; 28 import org.eclipse.jface.dialogs.IDialogConstants; 29 import org.eclipse.jface.dialogs.MessageDialog; 30 import org.eclipse.jface.preference.IPreferenceStore; 31 import org.eclipse.jface.preference.PreferenceStore; 32 33 import org.eclipse.ui.editors.text.IEncodingSupport; 34 35 import org.eclipse.ui.ide.dialogs.AbstractEncodingFieldEditor; 36 import org.eclipse.ui.ide.dialogs.EncodingFieldEditor; 37 import org.eclipse.ui.ide.dialogs.ResourceEncodingFieldEditor; 38 39 53 public class ChangeEncodingAction extends TextEditorAction { 54 55 private static final int APPLY_ID= IDialogConstants.OK_ID + IDialogConstants.CANCEL_ID + 1; 56 57 private String fDialogTitle; 58 private static final String ENCODING_PREF_KEY= "encoding"; 60 71 public ChangeEncodingAction(ResourceBundle bundle, String prefix, ITextEditor editor) { 72 super(bundle, prefix, editor); 73 74 String key= "dialog.title"; if (prefix != null && prefix.length() > 0) 76 key= prefix + key; 77 78 fDialogTitle= getString(bundle, key, null); 79 } 80 81 84 public void run() { 85 final IResource resource= getResource(); 86 final Shell parentShell= getTextEditor().getSite().getShell(); 87 final IEncodingSupport encodingSupport= getEncodingSupport(); 88 if (resource == null && encodingSupport == null) { 89 MessageDialog.openInformation(parentShell, fDialogTitle, TextEditorMessages.ChangeEncodingAction_message_noEncodingSupport); 90 return; 91 } 92 93 Dialog dialog= new Dialog(parentShell) { 94 private AbstractEncodingFieldEditor fEncodingEditor; 95 private IPreferenceStore store= null; 96 97 100 protected void configureShell(Shell newShell) { 101 super.configureShell(newShell); 102 newShell.setText(fDialogTitle); 103 } 104 105 108 protected Control createDialogArea(Composite parent) { 109 Control composite= super.createDialogArea(parent); 110 if (!(composite instanceof Composite)) { 111 composite.dispose(); 112 composite= new Composite(parent, SWT.NONE); 113 } 114 115 GridLayout layout= new GridLayout(); 116 layout.marginHeight= convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN); 117 layout.marginWidth= convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN); 118 layout.verticalSpacing= convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING); 119 layout.horizontalSpacing= convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING); 120 parent.setLayout(layout); 121 122 GridData data = new GridData(GridData.FILL_BOTH); 123 composite.setLayoutData(data); 124 composite.setFont(parent.getFont()); 125 126 DialogPage page= new MessageDialogPage((Composite)composite) { 127 public void setErrorMessage(String newMessage) { 128 super.setErrorMessage(newMessage); 129 setButtonEnabledState(IDialogConstants.OK_ID, newMessage == null); 130 setButtonEnabledState(APPLY_ID, newMessage == null); 131 } 132 133 private void setButtonEnabledState(int id, boolean state) { 134 Button button= getButton(id); 135 if (button != null) 136 button.setEnabled(state); 137 } 138 }; 139 140 if (resource != null) { 141 fEncodingEditor= new ResourceEncodingFieldEditor("", (Composite)composite, resource); fEncodingEditor.setPage(page); 143 fEncodingEditor.load(); 144 } else { 145 fEncodingEditor= new EncodingFieldEditor(ENCODING_PREF_KEY, "", (Composite)composite); store= new PreferenceStore(); 147 String defaultEncoding= encodingSupport.getDefaultEncoding(); 148 store.setDefault(ENCODING_PREF_KEY, defaultEncoding); 149 String encoding= encodingSupport.getEncoding(); 150 if (encoding != null) 151 store.setValue(ENCODING_PREF_KEY, encoding); 152 fEncodingEditor.setPreferenceStore(store); 153 154 fEncodingEditor.setPage(page); 155 fEncodingEditor.load(); 156 157 if (encoding == null || encoding.equals(defaultEncoding) || encoding.length() == 0) 158 fEncodingEditor.loadDefault(); 159 } 160 161 return composite; 162 } 163 164 167 protected void createButtonsForButtonBar(Composite parent) { 168 createButton(parent, APPLY_ID, TextEditorMessages.ChangeEncodingAction_button_apply_label, false); 169 super.createButtonsForButtonBar(parent); 170 } 171 172 175 protected void buttonPressed(int buttonId) { 176 if (buttonId == APPLY_ID) 177 apply(); 178 else 179 super.buttonPressed(buttonId); 180 } 181 182 185 protected void okPressed() { 186 apply(); 187 super.okPressed(); 188 } 189 190 private void apply() { 191 fEncodingEditor.store(); 192 193 if (resource == null) { 194 String encoding= fEncodingEditor.getPreferenceStore().getString(fEncodingEditor.getPreferenceName()); 195 encodingSupport.setEncoding(encoding); 196 } 197 } 198 }; 199 dialog.open(); 200 } 201 202 205 public void update() { 206 setEnabled((getResource() != null || getEncodingSupport() != null) && !getTextEditor().isDirty()); 207 } 208 209 214 private IResource getResource() { 215 if (getTextEditor() != null && getTextEditor().getEditorInput() != null) 216 return (IResource)getTextEditor().getEditorInput().getAdapter(IResource.class); 217 218 return null; 219 } 220 221 226 private IEncodingSupport getEncodingSupport() { 227 if (getTextEditor() != null) 228 return (IEncodingSupport)getTextEditor().getAdapter(IEncodingSupport.class); 229 230 return null; 231 } 232 } 233 | Popular Tags |