]>
Commit | Line | Data |
---|---|---|
1b2798f6 EK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2000, 2012 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.proposals; | |
13 | ||
14 | import org.eclipse.core.runtime.CoreException; | |
15 | ||
16 | import org.eclipse.text.edits.DeleteEdit; | |
17 | import org.eclipse.text.edits.InsertEdit; | |
18 | import org.eclipse.text.edits.ReplaceEdit; | |
19 | import org.eclipse.text.edits.TextEdit; | |
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.internal.corext.codemanipulation.StubUtility; | |
30 | import org.eclipse.jdt.internal.corext.util.Messages; | |
31 | ||
32 | import org.eclipse.jdt.ui.JavaElementLabels; | |
33 | import org.eclipse.jdt.ui.text.java.IProblemLocation; | |
34 | import org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal; | |
35 | ||
36 | import org.eclipse.jdt.internal.ui.JavaPlugin; | |
37 | import org.eclipse.jdt.internal.ui.JavaPluginImages; | |
38 | import org.eclipse.jdt.internal.ui.text.correction.CorrectionMessages; | |
39 | import org.eclipse.jdt.internal.ui.viewsupport.BasicElementLabels; | |
40 | ||
41 | public class CorrectPackageDeclarationProposal extends CUCorrectionProposal { | |
42 | ||
43 | private IProblemLocation fLocation; | |
44 | ||
45 | public CorrectPackageDeclarationProposal(ICompilationUnit cu, IProblemLocation location, int relevance) { | |
46 | super(CorrectionMessages.CorrectPackageDeclarationProposal_name, cu, relevance, JavaPluginImages.get(JavaPluginImages.IMG_OBJS_PACKDECL)); | |
47 | fLocation= location; | |
48 | } | |
49 | ||
50 | /* (non-Javadoc) | |
51 | * @see org.eclipse.jdt.internal.ui.text.correction.CUCorrectionProposal#addEdits(org.eclipse.jdt.internal.corext.textmanipulation.TextBuffer) | |
52 | */ | |
53 | @Override | |
54 | protected void addEdits(IDocument doc, TextEdit root) throws CoreException { | |
55 | super.addEdits(doc, root); | |
56 | ||
57 | ICompilationUnit cu= getCompilationUnit(); | |
58 | ||
59 | IPackageFragment parentPack= (IPackageFragment) cu.getParent(); | |
60 | IPackageDeclaration[] decls= cu.getPackageDeclarations(); | |
61 | ||
62 | if (parentPack.isDefaultPackage() && decls.length > 0) { | |
63 | for (int i= 0; i < decls.length; i++) { | |
64 | ISourceRange range= decls[i].getSourceRange(); | |
65 | root.addChild(new DeleteEdit(range.getOffset(), range.getLength())); | |
66 | } | |
67 | return; | |
68 | } | |
69 | if (!parentPack.isDefaultPackage() && decls.length == 0) { | |
70 | String lineDelim= StubUtility.getLineDelimiterUsed(cu); | |
71 | String str= "package " + parentPack.getElementName() + ';' + lineDelim + lineDelim; //$NON-NLS-1$ | |
72 | root.addChild(new InsertEdit(0, str)); | |
73 | return; | |
74 | } | |
75 | ||
76 | root.addChild(new ReplaceEdit(fLocation.getOffset(), fLocation.getLength(), parentPack.getElementName())); | |
77 | } | |
78 | ||
79 | /* (non-Javadoc) | |
80 | * @see org.eclipse.jdt.internal.ui.text.correction.proposals.ChangeCorrectionProposal#getName() | |
81 | */ | |
82 | @Override | |
83 | public String getName() { | |
84 | ICompilationUnit cu= getCompilationUnit(); | |
85 | IPackageFragment parentPack= (IPackageFragment) cu.getParent(); | |
86 | try { | |
87 | IPackageDeclaration[] decls= cu.getPackageDeclarations(); | |
88 | if (parentPack.isDefaultPackage() && decls.length > 0) { | |
89 | return Messages.format(CorrectionMessages.CorrectPackageDeclarationProposal_remove_description, BasicElementLabels.getJavaElementName(decls[0].getElementName())); | |
90 | } | |
91 | if (!parentPack.isDefaultPackage() && decls.length == 0) { | |
92 | return (Messages.format(CorrectionMessages.CorrectPackageDeclarationProposal_add_description, JavaElementLabels.getElementLabel(parentPack, JavaElementLabels.ALL_DEFAULT))); | |
93 | } | |
94 | } catch(JavaModelException e) { | |
95 | JavaPlugin.log(e); | |
96 | } | |
97 | return (Messages.format(CorrectionMessages.CorrectPackageDeclarationProposal_change_description, JavaElementLabels.getElementLabel(parentPack, JavaElementLabels.ALL_DEFAULT))); | |
98 | } | |
99 | } |