KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > refactoring > CreateTextFileChangePreviewViewer


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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 /**
49  * Change preview viewer for <code>CreateTextFileChange</code> objects.
50  */

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 JavaDoc 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     /**
90      * {@inheritDoc}
91      */

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     /**
103      * {@inheritDoc}
104      */

105     public Control getControl() {
106         return fPane;
107     }
108
109     public void refresh() {
110         fSourceViewer.refresh();
111     }
112
113     /**
114      * {@inheritDoc}
115      */

116     public void setInput(ChangePreviewViewerInput input) {
117         Change change= input.getChange();
118         if (change != null) {
119             Object JavaDoc 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(""); //$NON-NLS-1$
135
return;
136         }
137         CreateTextFileChange textFileChange= (CreateTextFileChange) change;
138         fPane.setText(textFileChange.getName());
139         IDocument document= new Document(textFileChange.getPreview());
140         // This is a temporary work around until we get the
141
// source viewer registry.
142
fSourceViewer.unconfigure();
143         String JavaDoc textType= textFileChange.getTextType();
144         JavaTextTools textTools= JavaPlugin.getDefault().getJavaTextTools();
145         IPreferenceStore store= JavaPlugin.getDefault().getCombinedPreferenceStore();
146         if ("java".equals(textType)) { //$NON-NLS-1$
147
textTools.setupJavaDocumentPartitioner(document);
148             fSourceViewer.configure(new JavaSourceViewerConfiguration(textTools.getColorManager(), store, null, null));
149         } else if ("properties".equals(textType)) { //$NON-NLS-1$
150
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