KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ltk > internal > ui > refactoring > model > RefactoringDescriptorViewer


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.ltk.internal.ui.refactoring.model;
12
13 import org.eclipse.core.runtime.Assert;
14 import org.eclipse.core.runtime.NullProgressMonitor;
15
16 import org.eclipse.ltk.core.refactoring.RefactoringDescriptor;
17 import org.eclipse.ltk.core.refactoring.RefactoringDescriptorProxy;
18
19 import org.eclipse.ltk.internal.ui.refactoring.util.HTMLPrinter;
20
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.browser.Browser;
23 import org.eclipse.swt.graphics.Font;
24 import org.eclipse.swt.graphics.FontData;
25 import org.eclipse.swt.widgets.Composite;
26 import org.eclipse.swt.widgets.Control;
27 import org.eclipse.swt.widgets.Display;
28
29 import org.eclipse.jface.resource.JFaceResources;
30 import org.eclipse.jface.text.TextSelection;
31 import org.eclipse.jface.viewers.ISelection;
32 import org.eclipse.jface.viewers.Viewer;
33
34 /**
35  * Viewer which displays a summary of a pending refactoring.
36  *
37  * @since 3.2
38  */

39 public class RefactoringDescriptorViewer extends Viewer {
40
41     /** The viewer control */
42     protected final Browser fBrowser;
43
44     /** The viewer input, or <code>null</code> */
45     private RefactoringDescriptorProxy fDescriptor= null;
46
47     /**
48      * Creates a new refactoring descriptor viewer.
49      *
50      * @param parent
51      * the parent control
52      * @param style
53      * the style
54      */

55     public RefactoringDescriptorViewer(final Composite parent, final int style) {
56         Assert.isNotNull(parent);
57         fBrowser= new Browser(parent, style);
58         final Display display= parent.getDisplay();
59         fBrowser.setForeground(display.getSystemColor(SWT.COLOR_LIST_FOREGROUND));
60         fBrowser.setBackground(display.getSystemColor(SWT.COLOR_LIST_BACKGROUND));
61     }
62
63     /**
64      * {@inheritDoc}
65      */

66     public Control getControl() {
67         return fBrowser;
68     }
69
70     /**
71      * {@inheritDoc}
72      */

73     public Object JavaDoc getInput() {
74         return fDescriptor;
75     }
76
77     /**
78      * Returns the input text for the specified refactoring descriptor proxy.
79      *
80      * @param proxy
81      * the refactoring descriptor proxy, or <code>null</code>
82      * @return the input text
83      */

84     protected String JavaDoc getInputText(final RefactoringDescriptorProxy proxy) {
85         final StringBuffer JavaDoc buffer= new StringBuffer JavaDoc();
86         final Font font= JFaceResources.getDialogFont();
87         final FontData[] data= font.getFontData();
88         String JavaDoc face= data[0].getName();
89         int index= face.indexOf('-');
90         if (index >= 0 && index < face.length() - 1)
91             face= face.substring(index + 1);
92         final int size= data[0].getHeight();
93         HTMLPrinter.insertPageProlog(buffer, face, size, 0, fBrowser.getBackground().getRGB());
94         if (proxy != null) {
95             HTMLPrinter.addSmallHeader(buffer, face, size, HTMLPrinter.convertToHTMLContent(proxy.getDescription()));
96             final RefactoringDescriptor descriptor= proxy.requestDescriptor(new NullProgressMonitor());
97             if (descriptor != null) {
98                 final String JavaDoc comment= descriptor.getComment();
99                 if (comment != null && !"".equals(comment)) //$NON-NLS-1$
100
HTMLPrinter.addParagraph(buffer, face, size, HTMLPrinter.convertToHTMLContent(comment));
101                 HTMLPrinter.startBulletList(buffer);
102                 final int flags= descriptor.getFlags();
103                 if ((flags & RefactoringDescriptor.BREAKING_CHANGE) > 0)
104                     HTMLPrinter.addBullet(buffer, face, size, ModelMessages.RefactoringDescriptorViewer_breaking_change_message);
105                 if ((flags & RefactoringDescriptor.STRUCTURAL_CHANGE) > 0)
106                     HTMLPrinter.addBullet(buffer, face, size, ModelMessages.RefactoringDescriptorViewer_structural_change_message);
107                 if ((flags & RefactoringDescriptor.MULTI_CHANGE) > 0)
108                     HTMLPrinter.addBullet(buffer, face, size, ModelMessages.RefactoringDescriptorViewer_closure_change_message);
109                 HTMLPrinter.endBulletList(buffer);
110             }
111         }
112         HTMLPrinter.addPageEpilog(buffer);
113         return buffer.toString();
114     }
115
116     /**
117      * {@inheritDoc}
118      */

119     public ISelection getSelection() {
120         return new TextSelection(0, 0);
121     }
122
123     /**
124      * {@inheritDoc}
125      */

126     public void refresh() {
127         String JavaDoc text= getInputText(fDescriptor);
128         if (text != null && text.length() > 0) {
129             if ((fBrowser.getShell().getStyle() & SWT.RIGHT_TO_LEFT) != 0) {
130                 final StringBuffer JavaDoc buffer= new StringBuffer JavaDoc(text);
131                 HTMLPrinter.insertStyles(buffer, new String JavaDoc[] { "direction:rtl", "overflow:hidden"}); //$NON-NLS-1$ //$NON-NLS-2$
132
text= buffer.toString();
133             }
134         }
135         fBrowser.setText(text);
136     }
137
138     /**
139      * {@inheritDoc}
140      */

141     public void setInput(final Object JavaDoc input) {
142         if (input instanceof RefactoringDescriptorProxy) {
143             fDescriptor= (RefactoringDescriptorProxy) input;
144             refresh();
145         } else
146             fDescriptor= null;
147     }
148
149     /**
150      * {@inheritDoc}
151      */

152     public void setSelection(final ISelection selection, final boolean reveal) {
153         // Do nothing
154
}
155 }
156
Popular Tags