KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > text > correction > CorrectPackageDeclarationProposal


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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
12 package org.eclipse.jdt.internal.ui.text.correction;
13
14 import org.eclipse.text.edits.DeleteEdit;
15 import org.eclipse.text.edits.InsertEdit;
16 import org.eclipse.text.edits.ReplaceEdit;
17 import org.eclipse.text.edits.TextEdit;
18
19 import org.eclipse.core.runtime.CoreException;
20
21 import org.eclipse.jface.text.IDocument;
22
23 import org.eclipse.jdt.core.ICompilationUnit;
24 import org.eclipse.jdt.core.IPackageDeclaration;
25 import org.eclipse.jdt.core.IPackageFragment;
26 import org.eclipse.jdt.core.ISourceRange;
27 import org.eclipse.jdt.core.JavaModelException;
28
29 import org.eclipse.jdt.ui.text.java.IProblemLocation;
30
31 import org.eclipse.jdt.internal.corext.codemanipulation.StubUtility;
32 import org.eclipse.jdt.internal.corext.util.Messages;
33
34 import org.eclipse.jdt.internal.ui.JavaPlugin;
35 import org.eclipse.jdt.internal.ui.JavaPluginImages;
36
37 public class CorrectPackageDeclarationProposal extends CUCorrectionProposal {
38
39     private IProblemLocation fLocation;
40
41     public CorrectPackageDeclarationProposal(ICompilationUnit cu, IProblemLocation location, int relevance) {
42         super(CorrectionMessages.CorrectPackageDeclarationProposal_name, cu, relevance,
43             JavaPluginImages.get(JavaPluginImages.IMG_OBJS_PACKDECL));
44         fLocation= location;
45     }
46
47     /* (non-Javadoc)
48      * @see org.eclipse.jdt.internal.ui.text.correction.CUCorrectionProposal#addEdits(org.eclipse.jdt.internal.corext.textmanipulation.TextBuffer)
49      */

50     protected void addEdits(IDocument doc, TextEdit root) throws CoreException {
51         super.addEdits(doc, root);
52
53         ICompilationUnit cu= getCompilationUnit();
54
55         IPackageFragment parentPack= (IPackageFragment) cu.getParent();
56         IPackageDeclaration[] decls= cu.getPackageDeclarations();
57
58         if (parentPack.isDefaultPackage() && decls.length > 0) {
59             for (int i= 0; i < decls.length; i++) {
60                 ISourceRange range= decls[i].getSourceRange();
61                 root.addChild(new DeleteEdit(range.getOffset(), range.getLength()));
62             }
63             return;
64         }
65         if (!parentPack.isDefaultPackage() && decls.length == 0) {
66             String JavaDoc lineDelim= StubUtility.getLineDelimiterUsed(cu);
67             String JavaDoc str= "package " + parentPack.getElementName() + ';' + lineDelim + lineDelim; //$NON-NLS-1$
68
root.addChild(new InsertEdit(0, str));
69             return;
70         }
71
72         root.addChild(new ReplaceEdit(fLocation.getOffset(), fLocation.getLength(), parentPack.getElementName()));
73     }
74
75     /*
76      * @see ICompletionProposal#getDisplayString()
77      */

78     public String JavaDoc getDisplayString() {
79         ICompilationUnit cu= getCompilationUnit();
80         IPackageFragment parentPack= (IPackageFragment) cu.getParent();
81         try {
82             IPackageDeclaration[] decls= cu.getPackageDeclarations();
83             if (parentPack.isDefaultPackage() && decls.length > 0) {
84                 return Messages.format(CorrectionMessages.CorrectPackageDeclarationProposal_remove_description, decls[0].getElementName());
85             }
86             if (!parentPack.isDefaultPackage() && decls.length == 0) {
87                 return (Messages.format(CorrectionMessages.CorrectPackageDeclarationProposal_add_description, parentPack.getElementName()));
88             }
89         } catch(JavaModelException e) {
90             JavaPlugin.log(e);
91         }
92         return (Messages.format(CorrectionMessages.CorrectPackageDeclarationProposal_change_description, parentPack.getElementName()));
93     }
94 }
95
Popular Tags