1 /*******************************************************************************
2 * Copyright (c) 2000, 2011 IBM Corporation 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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.refactoring;
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.events.DisposeEvent;
15 import org.eclipse.swt.events.DisposeListener;
16 import org.eclipse.swt.graphics.Image;
17 import org.eclipse.swt.widgets.Composite;
18 import org.eclipse.swt.widgets.Control;
20 import org.eclipse.core.runtime.IAdaptable;
22 import org.eclipse.jface.dialogs.Dialog;
23 import org.eclipse.jface.preference.IPreferenceStore;
24 import org.eclipse.jface.resource.ImageDescriptor;
25 import org.eclipse.jface.resource.JFaceResources;
27 import org.eclipse.jface.text.Document;
28 import org.eclipse.jface.text.IDocument;
29 import org.eclipse.jface.text.source.SourceViewer;
30 import org.eclipse.jface.text.source.SourceViewerConfiguration;
32 import org.eclipse.ui.model.IWorkbenchAdapter;
34 import org.eclipse.ltk.core.refactoring.Change;
35 import org.eclipse.ltk.ui.refactoring.ChangePreviewViewerInput;
36 import org.eclipse.ltk.ui.refactoring.IChangePreviewViewer;
38 import org.eclipse.jdt.internal.corext.refactoring.nls.changes.CreateTextFileChange;
40 import org.eclipse.jdt.ui.PreferenceConstants;
41 import org.eclipse.jdt.ui.text.JavaSourceViewerConfiguration;
42 import org.eclipse.jdt.ui.text.JavaTextTools;
44 import org.eclipse.jdt.internal.ui.JavaPlugin;
45 import org.eclipse.jdt.internal.ui.propertiesfileeditor.IPropertiesFilePartitions;
46 import org.eclipse.jdt.internal.ui.propertiesfileeditor.PropertiesFileDocumentSetupParticipant;
47 import org.eclipse.jdt.internal.ui.propertiesfileeditor.PropertiesFileSourceViewerConfiguration;
48 import org.eclipse.jdt.internal.ui.util.ViewerPane;
51 * Change preview viewer for <code>CreateTextFileChange</code> objects.
53 public final class CreateTextFileChangePreviewViewer implements IChangePreviewViewer {
55 private static class CreateTextFilePreviewer extends ViewerPane {
57 private ImageDescriptor fDescriptor;
61 public CreateTextFilePreviewer(Composite parent, int style) {
63 addDisposeListener(new DisposeListener() {
64 public void widgetDisposed(DisposeEvent e) {
70 /*package*/ void disposeImage() {
76 public void setImageDescriptor(ImageDescriptor imageDescriptor) {
77 fDescriptor= imageDescriptor;
81 public void setText(String text) {
84 if (fDescriptor != null) {
86 fImage= fDescriptor.createImage();
92 if (current != null) {
99 private CreateTextFilePreviewer fPane;
101 private SourceViewer fSourceViewer;
106 public void createControl(Composite parent) {
107 fPane= new CreateTextFilePreviewer(parent, SWT.BORDER | SWT.FLAT);
108 Dialog.applyDialogFont(fPane);
110 fSourceViewer= new SourceViewer(fPane, null, SWT.V_SCROLL | SWT.H_SCROLL | SWT.MULTI | SWT.FULL_SELECTION);
111 fSourceViewer.setEditable(false);
112 fSourceViewer.getControl().setFont(JFaceResources.getFont(PreferenceConstants.EDITOR_TEXT_FONT));
113 fPane.setContent(fSourceViewer.getControl());
119 public Control getControl() {
123 public void refresh() {
124 fSourceViewer.refresh();
131 public void setInput(ChangePreviewViewerInput input) {
132 Change change= input.getChange();
133 if (change != null) {
134 Object element= change.getModifiedElement();
135 if (element instanceof IAdaptable) {
136 IAdaptable adaptable= (IAdaptable) element;
137 IWorkbenchAdapter workbenchAdapter= (IWorkbenchAdapter) adaptable.getAdapter(IWorkbenchAdapter.class);
138 if (workbenchAdapter != null) {
139 fPane.setImageDescriptor(workbenchAdapter.getImageDescriptor(element));
141 fPane.setImageDescriptor(null);
144 fPane.setImageDescriptor(null);
147 if (!(change instanceof CreateTextFileChange)) {
148 fSourceViewer.setInput(null);
149 fPane.setText(""); //$NON-NLS-1$
152 CreateTextFileChange textFileChange= (CreateTextFileChange) change;
153 fPane.setText(textFileChange.getName());
154 IDocument document= new Document(textFileChange.getPreview());
155 // This is a temporary work around until we get the
156 // source viewer registry.
157 fSourceViewer.unconfigure();
158 String textType= textFileChange.getTextType();
159 JavaTextTools textTools= JavaPlugin.getDefault().getJavaTextTools();
160 IPreferenceStore store= JavaPlugin.getDefault().getCombinedPreferenceStore();
161 if ("java".equals(textType)) { //$NON-NLS-1$
162 textTools.setupJavaDocumentPartitioner(document);
163 fSourceViewer.configure(new JavaSourceViewerConfiguration(textTools.getColorManager(), store, null, null));
164 } else if ("properties".equals(textType)) { //$NON-NLS-1$
165 PropertiesFileDocumentSetupParticipant.setupDocument(document);
166 fSourceViewer.configure(new PropertiesFileSourceViewerConfiguration(textTools.getColorManager(), store, null, IPropertiesFilePartitions.PROPERTIES_FILE_PARTITIONING));
168 fSourceViewer.configure(new SourceViewerConfiguration());
170 fSourceViewer.setInput(document);