001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.ui.refactoring;
011:
012: import org.eclipse.core.runtime.IAdaptable;
013:
014: import org.eclipse.swt.SWT;
015: import org.eclipse.swt.graphics.Image;
016: import org.eclipse.swt.widgets.Composite;
017: import org.eclipse.swt.widgets.Control;
018:
019: import org.eclipse.jface.dialogs.Dialog;
020: import org.eclipse.jface.preference.IPreferenceStore;
021: import org.eclipse.jface.resource.ImageDescriptor;
022: import org.eclipse.jface.resource.JFaceResources;
023:
024: import org.eclipse.jface.text.Document;
025: import org.eclipse.jface.text.IDocument;
026: import org.eclipse.jface.text.source.SourceViewer;
027: import org.eclipse.jface.text.source.SourceViewerConfiguration;
028:
029: import org.eclipse.ui.model.IWorkbenchAdapter;
030:
031: import org.eclipse.ltk.core.refactoring.Change;
032: import org.eclipse.ltk.ui.refactoring.ChangePreviewViewerInput;
033: import org.eclipse.ltk.ui.refactoring.IChangePreviewViewer;
034:
035: import org.eclipse.jdt.internal.corext.refactoring.nls.changes.CreateTextFileChange;
036:
037: import org.eclipse.jdt.ui.PreferenceConstants;
038: import org.eclipse.jdt.ui.text.JavaSourceViewerConfiguration;
039: import org.eclipse.jdt.ui.text.JavaTextTools;
040:
041: import org.eclipse.jdt.internal.ui.JavaPlugin;
042: import org.eclipse.jdt.internal.ui.propertiesfileeditor.IPropertiesFilePartitions;
043: import org.eclipse.jdt.internal.ui.propertiesfileeditor.PropertiesFileDocumentSetupParticipant;
044: import org.eclipse.jdt.internal.ui.propertiesfileeditor.PropertiesFileSourceViewerConfiguration;
045: import org.eclipse.jdt.internal.ui.util.ViewerPane;
046:
047: /**
048: * Change preview viewer for <code>CreateTextFileChange</code> objects.
049: */
050: public final class CreateTextFileChangePreviewViewer implements
051: IChangePreviewViewer {
052:
053: private static class CreateTextFilePreviewer extends ViewerPane {
054:
055: private ImageDescriptor fDescriptor;
056:
057: private Image fImage;
058:
059: public CreateTextFilePreviewer(Composite parent, int style) {
060: super (parent, style);
061: }
062:
063: public void setImageDescriptor(ImageDescriptor imageDescriptor) {
064: fDescriptor = imageDescriptor;
065: }
066:
067: public void setText(String text) {
068: super .setText(text);
069: Image current = null;
070: if (fDescriptor != null) {
071: current = fImage;
072: fImage = fDescriptor.createImage();
073: } else {
074: current = fImage;
075: fImage = null;
076: }
077: setImage(fImage);
078: if (current != null) {
079: current.dispose();
080: }
081: }
082:
083: }
084:
085: private CreateTextFilePreviewer fPane;
086:
087: private SourceViewer fSourceViewer;
088:
089: /**
090: * {@inheritDoc}
091: */
092: public void createControl(Composite parent) {
093: fPane = new CreateTextFilePreviewer(parent, SWT.BORDER
094: | SWT.FLAT);
095: Dialog.applyDialogFont(fPane);
096:
097: fSourceViewer = new SourceViewer(fPane, null, SWT.V_SCROLL
098: | SWT.H_SCROLL | SWT.MULTI | SWT.FULL_SELECTION);
099: fSourceViewer.setEditable(false);
100: fSourceViewer.getControl().setFont(
101: JFaceResources
102: .getFont(PreferenceConstants.EDITOR_TEXT_FONT));
103: fPane.setContent(fSourceViewer.getControl());
104: }
105:
106: /**
107: * {@inheritDoc}
108: */
109: public Control getControl() {
110: return fPane;
111: }
112:
113: public void refresh() {
114: fSourceViewer.refresh();
115: }
116:
117: /**
118: * {@inheritDoc}
119: */
120: public void setInput(ChangePreviewViewerInput input) {
121: Change change = input.getChange();
122: if (change != null) {
123: Object element = change.getModifiedElement();
124: if (element instanceof IAdaptable) {
125: IAdaptable adaptable = (IAdaptable) element;
126: IWorkbenchAdapter workbenchAdapter = (IWorkbenchAdapter) adaptable
127: .getAdapter(IWorkbenchAdapter.class);
128: if (workbenchAdapter != null) {
129: fPane.setImageDescriptor(workbenchAdapter
130: .getImageDescriptor(element));
131: } else {
132: fPane.setImageDescriptor(null);
133: }
134: } else {
135: fPane.setImageDescriptor(null);
136: }
137: }
138: if (!(change instanceof CreateTextFileChange)) {
139: fSourceViewer.setInput(null);
140: fPane.setText(""); //$NON-NLS-1$
141: return;
142: }
143: CreateTextFileChange textFileChange = (CreateTextFileChange) change;
144: fPane.setText(textFileChange.getName());
145: IDocument document = new Document(textFileChange.getPreview());
146: // This is a temporary work around until we get the
147: // source viewer registry.
148: fSourceViewer.unconfigure();
149: String textType = textFileChange.getTextType();
150: JavaTextTools textTools = JavaPlugin.getDefault()
151: .getJavaTextTools();
152: IPreferenceStore store = JavaPlugin.getDefault()
153: .getCombinedPreferenceStore();
154: if ("java".equals(textType)) { //$NON-NLS-1$
155: textTools.setupJavaDocumentPartitioner(document);
156: fSourceViewer.configure(new JavaSourceViewerConfiguration(
157: textTools.getColorManager(), store, null, null));
158: } else if ("properties".equals(textType)) { //$NON-NLS-1$
159: PropertiesFileDocumentSetupParticipant
160: .setupDocument(document);
161: fSourceViewer
162: .configure(new PropertiesFileSourceViewerConfiguration(
163: textTools.getColorManager(),
164: store,
165: null,
166: IPropertiesFilePartitions.PROPERTIES_FILE_PARTITIONING));
167: } else {
168: fSourceViewer.configure(new SourceViewerConfiguration());
169: }
170: fSourceViewer.setInput(document);
171: }
172: }
|