KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > suggestions > SuggestionImpl


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.suggestions;
21
22 import java.awt.Image JavaDoc;
23 import org.openide.filesystems.FileObject;
24 import org.openide.util.Utilities;
25 import org.netbeans.modules.tasklist.core.Task;
26 import org.netbeans.modules.tasklist.client.SuggestionPerformer;
27 import org.netbeans.modules.tasklist.client.SuggestionPriority;
28 import org.openide.loaders.DataObject;
29 import org.openide.nodes.Node;
30 import org.openide.text.Line;
31 import org.openide.text.DataEditorSupport;
32
33 // XXX todo: fire property change whenever anything changes in the node...
34

35 /**
36  * Class which represents a task in the
37  * tasklist.
38  * @author Tor Norbye
39  */

40 public class SuggestionImpl extends Task implements Node.Cookie {
41     private Object JavaDoc seed = null;
42     private SuggestionType stype = null;
43
44     protected SuggestionImpl() {
45     }
46
47     /**
48      * @param fo FileObject associated with this suggestion or null
49      */

50     public SuggestionImpl(FileObject fo,
51         String JavaDoc summary, SuggestionType stype,
52         SuggestionPerformer action, Object JavaDoc data) {
53         super(summary, null);
54         setFileObject(fo);
55         this.seed = data;
56         this.stype = stype;
57         setAction(action);
58         if (stype != null) {
59             setType(stype.getName());
60         }
61     }
62
63     /**
64      * Return the name of the file associated with this
65      * task, or the empty string if none.
66      *
67      * @return basename, or empty string
68      */

69     public String JavaDoc getFileBaseName() {
70         Line l = getLine();
71         if (l != null) {
72             DataObject dobj = (DataObject) l.getLookup().lookup(DataObject.class);
73             if (dobj != null) {
74                 return dobj.getPrimaryFile().getNameExt();
75             }
76         }
77         return "";
78     }
79     
80     /**
81      * Return line number associated with the task.
82      *
83      * @return Line number, or "0" if no particular line is
84      * associated. Will always be 0 if there is no
85      * associated file.
86      */

87     public int getLineNumber() {
88         Line l = getLine();
89         if (l == null) {
90             return 0;
91         } else {
92             return l.getLineNumber()+1;
93         }
94     }
95
96     /**
97      * Class used to represent location with correctly defined order
98      * and to string.
99      */

100     public static class Location implements Comparable JavaDoc {
101       public String JavaDoc filename;
102       public int line;
103
104       public Location(String JavaDoc filename, int line) {
105     this.filename = filename;
106     this.line = line;
107       }
108
109       public int compareTo(Object JavaDoc o) {
110     Location rhs = (Location)o;
111     int c1 = this.filename.compareTo(rhs.filename);
112     if (c1 != 0) return c1;
113     else return (this.line < rhs.line)? -1 : ((this.line == rhs.line)?0:1);
114       }
115
116       public String JavaDoc toString() {
117     return filename + ":" + (line+1);
118       }
119     }
120
121     /** @return path/file:line location or null. */
122     public Location getLocation() {
123         Line l = getLine();
124         if (l != null) {
125             DataObject dobj = (DataObject) l.getLookup().lookup(DataObject.class);
126             if (dobj != null) {
127                 return new Location(dobj.getPrimaryFile().getPath(),l.getLineNumber());
128             }
129         }
130         return null;
131     }
132
133     /**
134      * Generate a string summary of the task; only used
135      * for debugging. DO NOT depend on this format for anything!
136      * Use generate() instead.
137      *
138      * @return summary string
139      */

140     public String JavaDoc toString() {
141         return "SuggestionImpl#" + System.identityHashCode(this) + /*(" + super.toString() + ")" */ "[\"" + getSummary() + "\", " + getFileBaseName() + ":" + getLineNumber() + /* ", " + stype + */ "]"; // NOI18N
142
}
143
144     /**
145      * Create a node for this item
146      */

147     public Node [] createNode() {
148         if (hasSubtasks()) {
149             return new Node[] {new SuggestionNode(this, new SuggestionChildren(this))};
150         } else {
151             return new Node[] {new SuggestionNode(this)};
152         }
153     }
154
155     /**
156      * Create an identical copy of a task (a deep copy, e.g. the
157      * list of subtasks will be cloned as well
158      */

159     protected Object JavaDoc clone() {
160         SuggestionImpl t = new SuggestionImpl();
161         t.copyFrom(this);
162         return t;
163     }
164
165     /**
166      * Copy all the fields in the given task into this object.
167      * Should only be called on an object of the EXACT same type.
168      * Thus, if you're implementing a subclass of Task, say
169      * UserTask, you can implement copy assuming that the passed
170      * in Task parameter is of type UserTask. When overriding,
171      * remember to call super.copyFrom.
172      * <p>
173      * Make a deep copy - except when that doesn't make sense.
174      * For example, you can share the same icon reference.
175      * And in particular, the tasklist reference should be the same.
176      * But the list of subitems should be unique. You get the idea.
177      */

178     protected void copyFrom(SuggestionImpl from) {
179         super.copyFrom(from);
180
181         seed = from.seed;
182         stype = from.stype;
183         //highlighted = from.highlighted;
184

185         // TODO XXX Copy fields from Suggestion as well!
186
}
187
188     /** Return the category. Derived from the SuggestionType. */
189     public String JavaDoc getCategory() {
190         if (stype != null) {
191             return stype.getLocalizedName();
192         } else {
193             return "";
194         }
195     }
196     
197     /** Return the NUMERIC value of the priority. Derived from getPriority(). */
198     public int getPriorityNumber() {
199         return getPriority().intValue();
200     }
201
202     /**
203      * "Re"defined here to allow access in this package, not just
204      * the api package. Just calls super.
205      */

206     protected void setType(String JavaDoc type) {
207         super.setType(type);
208     }
209
210     public SuggestionType getSType() {
211         return stype;
212     }
213     
214     void setSType(SuggestionType stype) {
215         this.stype = stype;
216         setType(stype.getName());
217     }
218
219     public Image JavaDoc getIcon() {
220         if (super.getIcon() != null) {
221             return super.getIcon();
222         } else if ((stype != null) && (stype.getIconImage() != null)) {
223         return stype.getIconImage();
224     } else {
225         return null;
226     }
227  
228     }
229
230     /**
231      * Get the providedData which created this suggestion.
232      * May be null (since not only SuggestionProviders
233      * are allowed to register suggestions)
234      */

235     public Object JavaDoc getSeed() {
236          return seed;
237     }
238
239     /**
240      * Get the priority of the task.
241      * This method could also return null if it is a category item.
242      *
243      * @return The priority of the task or null
244      */

245     public SuggestionPriority getPriority() {
246         if (seed == SuggestionList.CATEGORY_NODE_SEED)
247             return null;
248         else
249             return super.getPriority();
250     }
251     
252 /*
253     public boolean isHighlighted() {
254         return highlighted;
255     }
256     public void setHighlighted(boolean highlight) {
257         //if (highlight) {
258         // setSummary("<html><b>" + getSummary() + "</b></html>");
259         //} else {
260         // String desc = getSummary();
261         // setSummary(desc.substring(9, desc.length()-11)); // remove <html><b></b></html>
262         //}
263         //
264         //// getIcon will get called and will report new icon
265         //
266         updatedValues(); // TODO - just set this on the setIcon method?
267     }
268 */

269
270     // XXX expose protected method
271
public void setIcon(Image JavaDoc image) {
272         super.setIcon(image);
273     }
274 }
275
276
277
278
Popular Tags