KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > javaparser > 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.javaparser;
21
22 import javax.swing.text.*;
23 import javax.swing.event.*;
24 import java.awt.*;
25 import java.awt.event.*;
26 import javax.swing.*;
27 import java.util.List JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Arrays JavaDoc;
30 import java.util.Collections JavaDoc;
31 import java.util.Comparator JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import org.openide.ErrorManager;
34 import org.openide.cookies.SourceCookie;
35 import org.openide.explorer.view.*;
36 import org.openide.nodes.*;
37 import org.netbeans.modules.java.*;
38
39
40
41 import org.openide.src.Identifier;
42 import org.openide.src.Import;
43 import org.openide.src.SourceElement;
44 import org.openide.src.SourceException;
45 import org.openide.util.NbBundle;
46 import org.openide.util.Utilities;
47
48 import org.openide.cookies.LineCookie;
49 import org.openide.loaders.DataObject;
50 import org.openide.text.Line;
51 import org.openide.ErrorManager;
52
53 import java.util.TreeSet JavaDoc;
54 import java.lang.reflect.Modifier JavaDoc;
55 import org.netbeans.editor.ext.java.*;
56 import org.netbeans.modules.editor.java.*;
57
58 import org.netbeans.modules.tasklist.core.ConfPanel;
59 import org.netbeans.modules.tasklist.core.TLUtils;
60 import org.netbeans.modules.tasklist.client.Suggestion;
61 import org.netbeans.modules.tasklist.client.SuggestionPerformer;
62
63
64 /**
65  * This class performs class import suggestions
66  *
67  * @author Tor Norbye
68  */

69 class ImportPerformer implements SuggestionPerformer {
70     
71     private DataObject dobj;
72     private Document doc;
73     private String JavaDoc beforeDesc;
74     private JCClass importClass;
75     private int lineno;
76
77     ImportPerformer(int lineno, DataObject dobj,
78                     Document doc, JCClass importClass) {
79         this.lineno = lineno;
80         this.dobj = dobj;
81         this.doc = doc;
82         this.importClass = importClass;
83
84     }
85
86     // Yay - it's a casing-error
87
public void perform(Suggestion s) {
88         importClass(doc, importClass);
89     }
90
91      public boolean hasConfirmation() {
92          return true;
93      }
94      
95      public Object JavaDoc getConfirmation(Suggestion s) {
96          String JavaDoc filename =
97              dobj.getPrimaryFile().getNameExt();
98          String JavaDoc beforeDesc =
99              NbBundle.getMessage(ImportPerformer.class,
100                                  "ImportClassConfirmation"); // NOI18N
101
String JavaDoc beforeContents =
102              "<html><b>import " + // NOI18N
103
importClass.toString() +
104              "</b></html>"; //NOI18N
105
return new ConfPanel(beforeDesc,
106                               beforeContents, null,
107                               null,
108                               filename, lineno,
109                               null);
110      }
111
112
113     /** Import a particular class into the given document */
114     static void importClass(Document doc, Object JavaDoc item) {
115         if (item instanceof JCClass || item instanceof JCPackage) {
116             Object JavaDoc o = doc.getProperty(Document.StreamDescriptionProperty);
117             if (o instanceof DataObject) {
118                 DataObject dob = (DataObject)o;
119                 SourceCookie sc = (SourceCookie)dob.getCookie(SourceCookie.class);
120                 if (sc != null) {
121                     SourceElement se = sc.getSource();
122                     if (se != null) {
123                         try {
124                             if (item instanceof JCClass){
125                                 JCClass cls = (JCClass)item;
126                                 se.addImport(new Import(Identifier.create(cls.getFullName()), false));
127                             }else if (item instanceof JCPackage){
128                                 JCPackage pkg = (JCPackage)item;
129                                 se.addImport(new Import(Identifier.create(pkg.getName()), true));
130                             }
131                         } catch (SourceException e) {
132                         }
133                     }
134                 }
135             }
136         }
137     }
138     
139
140 }
141
142
Popular Tags