Added
Link Here
|
1 |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN"> |
2 |
<html lang="en"> |
3 |
<head> |
4 |
<meta name="copyright" content="Copyright (c) IBM Corporation and others 2009. This page is made available under license. For full details see the LEGAL in the documentation book that contains this page." > |
5 |
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> |
6 |
<meta http-equiv="Content-Style-Type" content="text/css"> |
7 |
<link rel="STYLESHEET" href="../book.css" charset="ISO-8859-1" type="text/css"> |
8 |
<title>Contributing a simple clean up and a simple save action using the org.eclipse.jdt.ui.cleanUps extension point</title> |
9 |
<link rel="stylesheet" type="text/css" href="../book.css"> |
10 |
</head> |
11 |
<body> |
12 |
<h2> Contributing a clean up and a save action using the <b>org.eclipse.jdt.ui.cleanUps</b> extension point</h2> |
13 |
<p>The <a href="../reference/api/org/eclipse/jdt/ui/cleanup/package-summary.html"><b>org.eclipse.jdt.ui.cleanUps</b></a> extension point enables you to contribute your own Java code clean ups and Java editor save actions. |
14 |
The clean ups help in resolving the problems for a compilation unit. A save action is a special clean up which also resolves problems by doing the selected actions on save automatically. |
15 |
</p> |
16 |
<h3> Using the extension point</h3> |
17 |
<p>To create a new extension for the <a href="../reference/api/org/eclipse/jdt/ui/cleanup/package-summary.html"><strong>org.eclipse.jdt.ui.cleanUps</strong></a> extension point |
18 |
you need to first provide the required extensions in the plugin.xml. There are 3 extensions that need to be declared as shown below with the example of a clean up |
19 |
which updates the copyrights for a file on save: </p> |
20 |
<p> |
21 |
<pre class="CleanUp"><span class="code SchemaTag"> |
22 |
<extension |
23 |
point=</span><span class="code SchemaCstring">"org.eclipse.jdt.ui.cleanUps"</span><span class="code SchemaTag">> |
24 |
<cleanUp |
25 |
id=</span><span class="code SchemaCstring">"org.eclipse.jdt.copyrightsaveaction.copyright_clean_up"</span><span class="code SchemaTag"> |
26 |
class=</span><span class="code SchemaCstring">"org.eclipse.jdt.ui.internal.copyrightupdater.CopyrightUpdaterCleanUp"</span><span class="code SchemaTag">> |
27 |
</cleanUp> |
28 |
|
29 |
<cleanUpOptionsInitializer |
30 |
class=</span><span class="code SchemaCstring">"org.eclipse.jdt.ui.internal.copyrightupdater.CopyrightOnSaveOptionsInitializer"</span><span class="code SchemaTag"> |
31 |
cleanUpKind=</span><span class="code SchemaCstring">"saveAction"</span><span class="code SchemaTag">> |
32 |
</cleanUpOptionsInitializer> |
33 |
|
34 |
<cleanUpConfigurationUI |
35 |
class=</span><span class="code SchemaCstring">"org.eclipse.jdt.ui.internal.copyrightupdater.CopyrightTabPage"</span><span class="code SchemaTag"> |
36 |
name=</span><span class="code SchemaCstring">"%cleanUpConfigurationUI.name"</span><span class="code SchemaTag"> |
37 |
cleanUpKind=</span><span class="code SchemaCstring">"saveAction"</span><span class="code SchemaTag">> |
38 |
</cleanUpConfigurationUI> |
39 |
</extension> |
40 |
</span></pre> |
41 |
|
42 |
For a description of the attributes list please refer to the extension point document. |
43 |
</p> |
44 |
<h3>Contributing a clean up</h3> |
45 |
<p>To contribute a clean up, you need to first create the class that implements the <a href="../reference/api/org/eclipse/jdt/ui/cleanup/ICleanUp.html"><b>ICleanUp</b></a> Interface. |
46 |
Lets create the <code>CopyrightUpdaterCleanUp</code> class for our example clean up and implement the inherited methods :</p> |
47 |
<pre><font color="#4444cc"> public class CopyrightUpdaterCleanUp implements ICleanUp { |
48 |
|
49 |
private CleanUpOptions fOptions; |
50 |
private RefactoringStatus fStatus; |
51 |
|
52 |
public CopyrightUpdaterCleanUp() { |
53 |
|
54 |
}</font></pre> |
55 |
|
56 |
<p>The CleanUpRequirements contain various requirements for the clean up such as an AST or a fresh AST containing changes from other clean ups, compiler options and changed |
57 |
regions, which are used by the <code>CleanUpContext</code> to create the fix. It can be obtained using the method getRequirements() |
58 |
<pre><font color="#4444cc"> public CleanUpRequirements getRequirements() { |
59 |
boolean changedRegionsRequired= false; |
60 |
Map compilerOptions= null; |
61 |
boolean isUpdateCopyrights= fOptions.isEnabled("cleanup.update_copyrights");//$NON-NLS-1$ |
62 |
return new CleanUpRequirements(isUpdateCopyrights, isUpdateCopyrights, changedRegionsRequired, compilerOptions); |
63 |
}</font></pre> |
64 |
|
65 |
A human readable description can be set for each step of the clean up using getStepDescriptions() |
66 |
<pre><font color="#4444cc"> public String[] getStepDescriptions() { |
67 |
if (fOptions.isEnabled("cleanup.update_copyrights"))//$NON-NLS-1$ |
68 |
return new String[] {"Update Copyrights"};//$NON-NLS-1$ |
69 |
|
70 |
return null; |
71 |
}</font></pre> |
72 |
|
73 |
The CleanUpOptions for the given options keys need to be set using the setOptions(...) |
74 |
<pre><font color="#4444cc"> public void setOptions(CleanUpOptions options) { |
75 |
Assert.isLegal(options != null); |
76 |
Assert.isTrue(fOptions == null); |
77 |
fOptions= options; |
78 |
}</font></pre> |
79 |
|
80 |
The clean up pre-conditions and post-conditions should be checked for an OK status using checkPreConditions(...) and checkPostConditions(...) |
81 |
<pre><font color="#4444cc"> public RefactoringStatus checkPreConditions(IJavaProject project, ICompilationUnit[] compilationUnits, IProgressMonitor monitor) throws CoreException { |
82 |
if (fOptions.isEnabled("cleanup.update_copyrights")) { //$NON-NLS-1$ |
83 |
fStatus= new RefactoringStatus(); |
84 |
} |
85 |
return new RefactoringStatus(); |
86 |
}</font></pre> |
87 |
|
88 |
<pre><font color="#4444cc"> public RefactoringStatus checkPostConditions(IProgressMonitor monitor) throws CoreException { |
89 |
try { |
90 |
if (fStatus == null || fStatus.isOK()) { |
91 |
return new RefactoringStatus(); |
92 |
} else { |
93 |
return fStatus; |
94 |
} |
95 |
} finally { |
96 |
fStatus= null; |
97 |
} |
98 |
}</font></pre> |
99 |
|
100 |
Finally a <a href="../reference/api/org/eclipse/jdt/ui/cleanup/ICleanUpFix.html"><b>ICleanUpFix</b></a> is to be created which fixes all the problems for the given context using createFix(...) |
101 |
<pre><font color="#4444cc"> public ICleanUpFix createFix(CleanUpContext context) throws CoreException { |
102 |
CompilationUnit compilationUnit= context.getAST(); |
103 |
if (compilationUnit == null) |
104 |
return null; |
105 |
|
106 |
return CopyrightsFix.createCleanUp(compilationUnit, fOptions.isEnabled("cleanup.update_copyrights"));//$NON-NLS-1$ |
107 |
}</font></pre> |
108 |
</p> |
109 |
|
110 |
<h3>Contributing a clean up options provider</h3> |
111 |
<p>To create the UI for the clean up , an options provider tab page has to be created by implementing the <a href="../reference/api/org/eclipse/jdt/ui/cleanup/ICleanUpConfigurationUI.html"><b>ICleanUpConfigurationUI</b></a> interface. |
112 |
The page can be created by implementing the createContents(...) method in the <code>CopyrightTabPage</code> class |
113 |
<pre><font color="#4444cc"> public class CopyrightTabPage implements ICleanUpConfigurationUI { |
114 |
private PixelConverter fPixelConverter; |
115 |
private CleanUpOptions fOptions; |
116 |
|
117 |
public CopyrightTabPage() { |
118 |
super(); |
119 |
} |
120 |
|
121 |
public Composite createContents(Composite parent) { |
122 |
final int numColumns= 4; |
123 |
|
124 |
if (fPixelConverter == null) { |
125 |
fPixelConverter= new PixelConverter(parent); |
126 |
} |
127 |
|
128 |
final SashForm sashForm = new SashForm(parent, SWT.HORIZONTAL); |
129 |
sashForm.setFont(parent.getFont()); |
130 |
|
131 |
Composite scrollContainer = new Composite(sashForm, SWT.NONE); |
132 |
|
133 |
GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true); |
134 |
scrollContainer.setLayoutData(gridData); |
135 |
|
136 |
GridLayout layout= new GridLayout(2, false); |
137 |
layout.marginHeight= 0; |
138 |
layout.marginWidth= 0; |
139 |
layout.horizontalSpacing= 0; |
140 |
layout.verticalSpacing= 0; |
141 |
scrollContainer.setLayout(layout); |
142 |
|
143 |
ScrolledComposite scroll= new ScrolledComposite(scrollContainer, SWT.V_SCROLL | SWT.H_SCROLL); |
144 |
scroll.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); |
145 |
scroll.setExpandHorizontal(true); |
146 |
scroll.setExpandVertical(true); |
147 |
|
148 |
final Composite settingsContainer= new Composite(scroll, SWT.NONE); |
149 |
settingsContainer.setFont(sashForm.getFont()); |
150 |
|
151 |
scroll.setContent(settingsContainer); |
152 |
|
153 |
settingsContainer.setLayout(new PageLayout(scroll, 400, 400)); |
154 |
settingsContainer.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); |
155 |
|
156 |
Composite settingsPane= new Composite(settingsContainer, SWT.NONE); |
157 |
settingsPane.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); |
158 |
|
159 |
layout= new GridLayout(numColumns, false); |
160 |
layout.verticalSpacing= (int)(1.5 * fPixelConverter.convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING)); |
161 |
layout.horizontalSpacing= fPixelConverter.convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING); |
162 |
layout.marginHeight= fPixelConverter.convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN); |
163 |
layout.marginWidth= fPixelConverter.convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN); |
164 |
settingsPane.setLayout(layout); |
165 |
doCreatePreferences(settingsPane); |
166 |
|
167 |
settingsContainer.setSize(settingsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT)); |
168 |
|
169 |
scroll.addControlListener(new ControlListener() { |
170 |
|
171 |
public void controlMoved(ControlEvent e) { |
172 |
} |
173 |
|
174 |
public void controlResized(ControlEvent e) { |
175 |
settingsContainer.setSize(settingsContainer.computeSize(SWT.DEFAULT, SWT.DEFAULT)); |
176 |
} |
177 |
}); |
178 |
|
179 |
Label sashHandle = new Label(scrollContainer, SWT.SEPARATOR | SWT.VERTICAL); |
180 |
gridData= new GridData(SWT.RIGHT, SWT.FILL, false, true); |
181 |
sashHandle.setLayoutData(gridData); |
182 |
|
183 |
return sashForm; |
184 |
} |
185 |
|
186 |
protected void doCreatePreferences(Composite composite) { |
187 |
Group group= new Group(composite, SWT.NONE); |
188 |
group.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false)); |
189 |
group.setLayout(new GridLayout(1, false)); |
190 |
group.setText("Copyright Update"); //$NON-NLS-1$ |
191 |
|
192 |
final Button updateCheckbox= new Button(group, SWT.CHECK); |
193 |
updateCheckbox.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false)); |
194 |
updateCheckbox.setText("Update the Copyrights"); //$NON-NLS-1$ |
195 |
updateCheckbox.setSelection(fOptions.isEnabled("cleanup.update_copyrights")); //$NON-NLS-1$ |
196 |
updateCheckbox.addSelectionListener(new SelectionAdapter() { |
197 |
/* |
198 |
* @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent) |
199 |
*/ |
200 |
public void widgetSelected(SelectionEvent e) { |
201 |
fOptions.setOption("cleanup.update_copyrights", updateCheckbox.getSelection() ? CleanUpOptions.TRUE : CleanUpOptions.FALSE); //$NON-NLS-1$ |
202 |
} |
203 |
}); |
204 |
}</font></pre> |
205 |
Optionally a code snippet of the new clean up with the given options can be shown in the clean up preview tab using getPreview() |
206 |
<pre><font color="#4444cc"> public String getPreview() { |
207 |
StringBuffer buf= new StringBuffer(); |
208 |
|
209 |
if (fOptions.isEnabled("cleanup.update_copyrights")) {//$NON-NLS-1$ |
210 |
buf.append("/* Copyright (c) 2007, 2009 IBM Corporation and others.*/"); //$NON-NLS-1$ |
211 |
} else { |
212 |
buf.append("/* Copyright (c) 2007, 2008 IBM Corporation and others.*/"); //$NON-NLS-1$ |
213 |
} |
214 |
|
215 |
return buf.toString(); |
216 |
}</font></pre> |
217 |
If the CleanUpOptions get modified in the UI, they need to be set again using setOptions(...) |
218 |
<pre><font color="#4444cc"> public void setOptions(CleanUpOptions options) { |
219 |
fOptions= options; |
220 |
} |
221 |
}</font></pre> |
222 |
|
223 |
<p> To see the full UI code please refer to the link <a href="../reference/misc/CopyrightTabPage.html"><b>CleanUpTabPage</b></a> |
224 |
<h3> Contributing a clean up options initializer</h3> |
225 |
<p>A clean up options initializer, which returns the default options for each clean up kind can be created by implementing the <a href="../reference/api/org/eclipse/jdt/ui/cleanup/ICleanUpOptionsInitializer.html"><b>ICleanUpOptionsInitializer</b></a>. |
226 |
The options initializer can either be for a normal code clean up or for a save action. |
227 |
<pre><font color="#4444cc"> public class CopyrightOnSaveOptionsInitializer implements ICleanUpOptionsInitializer { |
228 |
|
229 |
public CopyrightOnSaveOptionsInitializer() { |
230 |
}</font></pre> |
231 |
|
232 |
The Default options for this initializer can be set using setDefaultoptions(...) |
233 |
<pre><font color="#4444cc"> public void setDefaultOptions(CleanUpOptions options) { |
234 |
options.setOption("cleanup.update_copyrights", CleanUpOptions.TRUE); |
235 |
} |
236 |
}</font></pre> |
237 |
</body> |
238 |
</html> |