1 11 package org.eclipse.ui.ide.dialogs; 12 13 import org.eclipse.core.resources.IContainer; 14 import org.eclipse.core.resources.IFile; 15 import org.eclipse.core.resources.IResource; 16 import org.eclipse.core.resources.IWorkspaceRoot; 17 import org.eclipse.core.runtime.Assert; 18 import org.eclipse.core.runtime.CoreException; 19 import org.eclipse.core.runtime.IProgressMonitor; 20 import org.eclipse.core.runtime.IStatus; 21 import org.eclipse.core.runtime.Status; 22 import org.eclipse.core.runtime.content.IContentDescription; 23 import org.eclipse.core.runtime.jobs.Job; 24 import org.eclipse.jface.dialogs.DialogPage; 25 import org.eclipse.jface.dialogs.IDialogConstants; 26 import org.eclipse.jface.dialogs.MessageDialog; 27 import org.eclipse.osgi.util.NLS; 28 import org.eclipse.swt.SWT; 29 import org.eclipse.swt.layout.GridData; 30 import org.eclipse.swt.widgets.Composite; 31 import org.eclipse.swt.widgets.Control; 32 import org.eclipse.swt.widgets.Group; 33 import org.eclipse.swt.widgets.Label; 34 import org.eclipse.swt.widgets.Shell; 35 import org.eclipse.ui.WorkbenchEncoding; 36 import org.eclipse.ui.ide.IDEEncoding; 37 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages; 38 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin; 39 40 49 public final class ResourceEncodingFieldEditor extends 50 AbstractEncodingFieldEditor { 51 52 55 private IResource resource; 56 57 private Composite group; 58 59 73 public ResourceEncodingFieldEditor(String labelText, Composite parent, 74 IResource charsetResource) { 75 super(); 76 setLabelAndResource(labelText, charsetResource); 77 createControl(parent); 78 } 79 80 100 public ResourceEncodingFieldEditor(String labelText, Composite parent, 101 IResource charsetResource,String groupTitle) { 102 super(); 103 setLabelAndResource(labelText, charsetResource); 104 setGroupTitle(groupTitle); 105 createControl(parent); 106 } 107 108 114 private void setLabelAndResource(String labelText, IResource charsetResource) { 115 Assert.isTrue(charsetResource instanceof IContainer 116 || charsetResource instanceof IFile); 117 setLabelText(labelText); 118 this.resource = charsetResource; 119 } 120 121 126 protected String getStoredValue() { 127 try { 128 if (resource instanceof IContainer) { 129 return ((IContainer) resource).getDefaultCharset(false); 130 } 131 return ((IFile) resource).getCharset(false); 132 133 } catch (CoreException e) { IDEWorkbenchPlugin 135 .log( 136 IDEWorkbenchMessages.ResourceEncodingFieldEditor_ErrorLoadingMessage, 137 e.getStatus()); 138 return WorkbenchEncoding.getWorkbenchDefaultEncoding(); 139 } 140 141 } 142 143 148 protected void doStore() { 149 150 String encoding = getSelectedEncoding(); 151 152 if (isDefaultSelected()) { 154 encoding = null; 155 } 156 if (hasSameEncoding(encoding)) { 158 return; 159 } 160 161 String descriptionCharset = getCharsetFromDescription(); 162 if (descriptionCharset != null 163 && !(descriptionCharset.equals(encoding)) && encoding != null) { 164 Shell shell = null; 165 DialogPage page = getPage(); 166 if (page != null) { 167 shell = page.getShell(); 168 } 169 170 MessageDialog dialog = new MessageDialog( 171 shell, 172 IDEWorkbenchMessages.ResourceEncodingFieldEditor_EncodingConflictTitle, 173 null, 174 NLS 175 .bind( 176 IDEWorkbenchMessages.ResourceEncodingFieldEditor_EncodingConflictMessage, 177 encoding, descriptionCharset), 178 MessageDialog.WARNING, new String [] { 179 IDialogConstants.YES_LABEL, 180 IDialogConstants.NO_LABEL }, 0); if (dialog.open() > 0) { 183 return; 184 } 185 } 186 187 IDEEncoding.addIDEEncoding(encoding); 188 189 final String finalEncoding = encoding; 190 191 Job charsetJob = new Job(IDEWorkbenchMessages.IDEEncoding_EncodingJob) { 192 197 protected IStatus run(IProgressMonitor monitor) { 198 try { 199 if (resource instanceof IContainer) { 200 ((IContainer) resource).setDefaultCharset( 201 finalEncoding, monitor); 202 } else { 203 ((IFile) resource).setCharset(finalEncoding, monitor); 204 } 205 return Status.OK_STATUS; 206 } catch (CoreException e) { IDEWorkbenchPlugin 209 .log( 210 IDEWorkbenchMessages.ResourceEncodingFieldEditor_ErrorStoringMessage, 211 e.getStatus()); 212 return e.getStatus(); 213 } 214 } 215 }; 216 217 charsetJob.schedule(); 218 219 } 220 221 226 public void store() { doStore(); 229 } 230 231 236 public void load() { setPresentsDefaultValue(false); 239 doLoad(); 240 } 241 242 247 public void loadDefault() { 248 setPresentsDefaultValue(true); 249 doLoadDefault(); 250 refreshValidState(); 251 } 252 253 258 protected String findDefaultEncoding() { 259 260 if (resource instanceof IWorkspaceRoot) { 261 return super.findDefaultEncoding(); 262 } 263 264 String defaultCharset = getCharsetFromDescription(); 265 defaultCharset = getCharsetFromDescription(); 266 267 if (defaultCharset != null && defaultCharset.length() > 0) { 268 return defaultCharset; 269 } 270 try { 271 defaultCharset = resource.getParent().getDefaultCharset(true); 273 } catch (CoreException exception) { 274 } 276 277 if (defaultCharset != null && defaultCharset.length() > 0) { 278 return defaultCharset; 279 } 280 281 return super.findDefaultEncoding(); 282 } 283 284 289 private String getCharsetFromDescription() { 290 IContentDescription description = getContentDescription(); 291 if (description != null) { 292 return description.getCharset(); 293 } 294 return null; 295 } 296 297 302 protected String defaultButtonText() { 303 304 if (resource instanceof IWorkspaceRoot) { 305 return super.defaultButtonText(); 306 } 307 308 if (resource instanceof IFile) { 309 try { 310 IContentDescription description = ((IFile) resource) 311 .getContentDescription(); 312 if (description == null || description.getCharset() == null) { 315 return NLS 316 .bind( 317 IDEWorkbenchMessages.ResourceInfo_fileContainerEncodingFormat, 318 getDefaultEnc()); 319 } 320 321 return NLS 322 .bind( 323 IDEWorkbenchMessages.ResourceInfo_fileContentEncodingFormat, 324 getDefaultEnc()); 325 326 } catch (CoreException exception) { 327 } 330 } 331 332 return NLS.bind( 333 IDEWorkbenchMessages.ResourceInfo_containerEncodingFormat, 334 getDefaultEnc()); 335 336 } 337 338 344 protected Composite createEncodingGroup(Composite parent, int numColumns) { 345 group = super.createEncodingGroup(parent, numColumns); 346 String byteOrderLabel = IDEEncoding 347 .getByteOrderMarkLabel(getContentDescription()); 348 if (byteOrderLabel != null) { 349 Label label = new Label(group, SWT.NONE); 350 label 351 .setText(NLS 352 .bind( 353 IDEWorkbenchMessages.WorkbenchPreference_encoding_encodingMessage, 354 byteOrderLabel)); 355 GridData layoutData = new GridData(); 356 layoutData.horizontalSpan = numColumns + 1; 357 label.setLayoutData(layoutData); 358 359 } 360 return group; 361 } 362 363 370 private IContentDescription getContentDescription() { 371 try { 372 if (resource instanceof IFile) { 373 return (((IFile) resource).getContentDescription()); 374 } 375 } catch (CoreException exception) { 376 } 378 return null; 379 } 380 381 384 public void setEnabled(boolean enabled, Composite parent) { 385 super.setEnabled(enabled, parent); 386 group.setEnabled(enabled); 387 Control[] children = group.getChildren(); 388 for (int i = 0; i < children.length; i++) { 389 children[i].setEnabled(enabled); 390 391 } 392 } 393 394 } 395 | Popular Tags |