KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > pmd > ImportPerformer


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.tasklist.pmd;
21
22 import net.sourceforge.pmd.RuleViolation;
23 import pmd.*;
24 import javax.swing.text.*;
25 import javax.swing.event.*;
26 import java.awt.*;
27 import java.awt.event.*;
28 import javax.swing.*;
29 import org.openide.explorer.view.*;
30 import org.openide.nodes.*;
31 import org.openide.loaders.DataObject;
32 import org.openide.text.Line;
33 import org.openide.text.DataEditorSupport;
34 import org.openide.util.NbBundle;
35
36 import org.netbeans.modules.tasklist.core.TLUtils;
37 import org.netbeans.modules.tasklist.core.ConfPanel;
38 import org.netbeans.modules.tasklist.client.Suggestion;
39 import org.netbeans.modules.tasklist.client.SuggestionPerformer;
40
41 /**
42  * Perform import statement confirmation & removal
43  * <p>
44  * @author Tor Norbye
45  */

46
47
48 public class ImportPerformer implements SuggestionPerformer {
49     private Line line;
50     private RuleViolation violation;
51     private boolean comment;
52
53     ImportPerformer(Line line, RuleViolation violation,
54                     boolean comment) {
55         this.line = line;
56         this.violation = violation;
57     }
58
59     public void perform(Suggestion s) {
60         // Remove the particular line
61
if (comment) {
62             TLUtils.commentLine(line, "import "); // NOI18N
63
} else {
64             TLUtils.deleteLine(line, "import "); // NOI18N
65
}
66     }
67     public boolean hasConfirmation() {
68         return true;
69     }
70     public Object JavaDoc getConfirmation(Suggestion s) {
71         DataObject dao = DataEditorSupport.findDataObject(line);
72         int linenumber = line.getLineNumber();
73         String JavaDoc filename = dao.getPrimaryFile().getNameExt();
74         String JavaDoc ruleDesc = violation.getRule().getDescription();
75         String JavaDoc ruleExample = violation.getRule().getExample();
76         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(200);
77         String JavaDoc beforeContents = null;
78         String JavaDoc afterContents = null;
79         String JavaDoc afterDesc = null;
80         String JavaDoc beforeDesc = null;
81         if (comment) {
82             // TODO - something special if DontImportJavaLang
83
beforeDesc = NbBundle.getMessage(ImportPerformer.class,
84                                 "ImportConfirmationBefore"); // NOI18N
85
afterDesc =
86                 NbBundle.getMessage(ImportPerformer.class,
87                                 "ImportConfirmationAfter"); // NOI18N
88

89             Line l = line;
90             sb.append("<html>"); // NOI18N
91
TLUtils.appendSurroundingLine(sb, l, -1);
92             sb.append("<br><b>"); // NOI18N
93
sb.append(line.getText());
94             sb.append("</b><br>"); // NOI18N
95
TLUtils.appendSurroundingLine(sb, l, +1);
96             sb.append("</html>"); // NOI18N
97
beforeContents = sb.toString();
98
99             sb.setLength(0);
100             sb.append("<html>"); // NOI18N
101
TLUtils.appendSurroundingLine(sb, l, -1);
102             sb.append("<br><b><i>// "); // NOI18N
103
sb.append(line.getText());
104             sb.append("</i></b><br>"); // NOI18N
105
TLUtils.appendSurroundingLine(sb, l, +1);
106             sb.append("</html>"); // NOI18N
107
afterContents = sb.toString();
108         } else {
109             String JavaDoc rulename = violation.getRule().getName();
110             if (rulename.equals("UnusedImports")) { // NOI18N
111
beforeDesc = NbBundle.getMessage(ImportPerformer.class,
112                                 "ImportConfirmationUnused"); // NOI18N
113
} else if (rulename.equals("ImportFromSamePackage")) { // NOI18N
114
beforeDesc = NbBundle.getMessage(ImportPerformer.class,
115                                 "ImportConfirmationSame"); // NOI18N
116
} else if (rulename.equals("DontImportJavaLang")) { // NOI18N
117
beforeDesc = NbBundle.getMessage(ImportPerformer.class,
118                                 "ImportConfirmationLang"); // NOI18N
119
} else if (rulename.equals("DuplicateImports")) { // NOI18N
120
beforeDesc = NbBundle.getMessage(ImportPerformer.class,
121                                 "ImportConfirmationDuplicate"); // NOI18N
122
} else {
123                 beforeDesc = NbBundle.getMessage(ImportPerformer.class,
124                                 "ImportConfirmationOther"); // NOI18N
125
}
126
127             Line l = line;
128             sb.append("<html>"); // NOI18N
129
TLUtils.appendSurroundingLine(sb, l, -1);
130             sb.append("<br>");
131             sb.append("<b><strike>"); // NOI18N
132
sb.append(line.getText());
133             sb.append("</strike></b>"); // NOI18N
134
sb.append("<br>"); // NOI18N
135
TLUtils.appendSurroundingLine(sb, l, +1);
136             sb.append("</html>"); // NOI18N
137
beforeContents = sb.toString();
138         }
139         
140         return new ConfPanel(beforeDesc,
141                              beforeContents, afterDesc,
142                              afterContents,
143                              filename, linenumber,
144                              ViolationProvider.getBottomPanel(ruleDesc,
145                                                               ruleExample));
146         
147     }
148 }
149
Popular Tags