1 11 package org.eclipse.jdt.internal.ui.refactoring; 12 13 import org.eclipse.core.runtime.IAdaptable; 14 15 import org.eclipse.swt.SWT; 16 import org.eclipse.swt.graphics.Image; 17 import org.eclipse.swt.widgets.Composite; 18 import org.eclipse.swt.widgets.Control; 19 20 import org.eclipse.jface.dialogs.Dialog; 21 import org.eclipse.jface.preference.IPreferenceStore; 22 import org.eclipse.jface.resource.ImageDescriptor; 23 import org.eclipse.jface.resource.JFaceResources; 24 25 import org.eclipse.jface.text.Document; 26 import org.eclipse.jface.text.IDocument; 27 import org.eclipse.jface.text.source.SourceViewer; 28 import org.eclipse.jface.text.source.SourceViewerConfiguration; 29 30 import org.eclipse.ui.model.IWorkbenchAdapter; 31 32 import org.eclipse.ltk.core.refactoring.Change; 33 import org.eclipse.ltk.ui.refactoring.ChangePreviewViewerInput; 34 import org.eclipse.ltk.ui.refactoring.IChangePreviewViewer; 35 36 import org.eclipse.jdt.internal.corext.refactoring.nls.changes.CreateTextFileChange; 37 38 import org.eclipse.jdt.ui.PreferenceConstants; 39 import org.eclipse.jdt.ui.text.JavaSourceViewerConfiguration; 40 import org.eclipse.jdt.ui.text.JavaTextTools; 41 42 import org.eclipse.jdt.internal.ui.JavaPlugin; 43 import org.eclipse.jdt.internal.ui.propertiesfileeditor.IPropertiesFilePartitions; 44 import org.eclipse.jdt.internal.ui.propertiesfileeditor.PropertiesFileDocumentSetupParticipant; 45 import org.eclipse.jdt.internal.ui.propertiesfileeditor.PropertiesFileSourceViewerConfiguration; 46 import org.eclipse.jdt.internal.ui.util.ViewerPane; 47 48 51 public final class CreateTextFileChangePreviewViewer implements IChangePreviewViewer { 52 53 private static class CreateTextFilePreviewer extends ViewerPane { 54 55 private ImageDescriptor fDescriptor; 56 57 private Image fImage; 58 59 public CreateTextFilePreviewer(Composite parent, int style) { 60 super(parent, style); 61 } 62 63 public void setImageDescriptor(ImageDescriptor imageDescriptor) { 64 fDescriptor= imageDescriptor; 65 } 66 67 public void setText(String text) { 68 super.setText(text); 69 Image current= null; 70 if (fDescriptor != null) { 71 current= fImage; 72 fImage= fDescriptor.createImage(); 73 } else { 74 current= fImage; 75 fImage= null; 76 } 77 setImage(fImage); 78 if (current != null) { 79 current.dispose(); 80 } 81 } 82 83 } 84 85 private CreateTextFilePreviewer fPane; 86 87 private SourceViewer fSourceViewer; 88 89 92 public void createControl(Composite parent) { 93 fPane= new CreateTextFilePreviewer(parent, SWT.BORDER | SWT.FLAT); 94 Dialog.applyDialogFont(fPane); 95 96 fSourceViewer= new SourceViewer(fPane, null, SWT.V_SCROLL | SWT.H_SCROLL | SWT.MULTI | SWT.FULL_SELECTION); 97 fSourceViewer.setEditable(false); 98 fSourceViewer.getControl().setFont(JFaceResources.getFont(PreferenceConstants.EDITOR_TEXT_FONT)); 99 fPane.setContent(fSourceViewer.getControl()); 100 } 101 102 105 public Control getControl() { 106 return fPane; 107 } 108 109 public void refresh() { 110 fSourceViewer.refresh(); 111 } 112 113 116 public void setInput(ChangePreviewViewerInput input) { 117 Change change= input.getChange(); 118 if (change != null) { 119 Object element= change.getModifiedElement(); 120 if (element instanceof IAdaptable) { 121 IAdaptable adaptable= (IAdaptable) element; 122 IWorkbenchAdapter workbenchAdapter= (IWorkbenchAdapter) adaptable.getAdapter(IWorkbenchAdapter.class); 123 if (workbenchAdapter != null) { 124 fPane.setImageDescriptor(workbenchAdapter.getImageDescriptor(element)); 125 } else { 126 fPane.setImageDescriptor(null); 127 } 128 } else { 129 fPane.setImageDescriptor(null); 130 } 131 } 132 if (!(change instanceof CreateTextFileChange)) { 133 fSourceViewer.setInput(null); 134 fPane.setText(""); return; 136 } 137 CreateTextFileChange textFileChange= (CreateTextFileChange) change; 138 fPane.setText(textFileChange.getName()); 139 IDocument document= new Document(textFileChange.getPreview()); 140 fSourceViewer.unconfigure(); 143 String textType= textFileChange.getTextType(); 144 JavaTextTools textTools= JavaPlugin.getDefault().getJavaTextTools(); 145 IPreferenceStore store= JavaPlugin.getDefault().getCombinedPreferenceStore(); 146 if ("java".equals(textType)) { textTools.setupJavaDocumentPartitioner(document); 148 fSourceViewer.configure(new JavaSourceViewerConfiguration(textTools.getColorManager(), store, null, null)); 149 } else if ("properties".equals(textType)) { PropertiesFileDocumentSetupParticipant.setupDocument(document); 151 fSourceViewer.configure(new PropertiesFileSourceViewerConfiguration(textTools.getColorManager(), store, null, IPropertiesFilePartitions.PROPERTIES_FILE_PARTITIONING)); 152 } else { 153 fSourceViewer.configure(new SourceViewerConfiguration()); 154 } 155 fSourceViewer.setInput(document); 156 } 157 } 158 | Popular Tags |