001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.spi.tasklist;
043:
044: import java.awt.event.ActionListener;
045: import java.util.HashSet;
046: import java.util.Map;
047: import java.util.Set;
048: import org.netbeans.modules.tasklist.trampoline.TaskGroupFactory;
049: import java.util.logging.Level;
050: import java.util.logging.Logger;
051: import org.netbeans.modules.tasklist.trampoline.Accessor;
052: import org.netbeans.modules.tasklist.trampoline.TaskGroup;
053: import org.openide.filesystems.FileObject;
054: import org.openide.util.NbBundle;
055:
056: /**
057: * A class holding the description of a single Task that will appear in TaskList's window.
058: *
059: * @author S. Aubrecht
060: */
061: public final class Task {
062:
063: private FileObject resource;
064: private TaskGroup group;
065: private String description;
066: private int line;
067: private ActionListener al;
068:
069: static {
070: Accessor.DEFAULT = new AccessorImpl();
071: }
072:
073: /**
074: * Create a new Task
075: *
076: * @param resource File or folder which the Task applies to, cannot be null.
077: * @param groupName Name of the group this task belongs to (error, warning, todo, etc).
078: * @param description A brief summary of the task (one line if possible), cannot be null.
079: * @param line Line number in a text file, use negative value if line number is not applicable.
080: *
081: * @return New task.
082: */
083: public static Task create(FileObject resource, String groupName,
084: String description, int line) {
085: return new Task(resource, getTaskGroup(groupName), description,
086: line, null);
087: }
088:
089: /**
090: * Create a new Task
091: *
092: * @param resource File or folder which the Task applies to, cannot be null.
093: * @param groupName Name of the group this task belongs to (error, warning, todo, etc).
094: * @param description A brief summary of the task (one line if possible), cannot be null.
095: * @param al Task's default action, e.g. double-click or Enter key in the Task List window.
096: *
097: * @return New task.
098: */
099: public static Task create(FileObject resource, String groupName,
100: String description, ActionListener al) {
101: return new Task(resource, getTaskGroup(groupName), description,
102: -1, al);
103: }
104:
105: /** Creates a new instance of Task */
106: private Task(FileObject resource, TaskGroup group,
107: String description, int line, ActionListener al) {
108: assert null != group;
109: assert null != description;
110: assert null != resource;
111:
112: this .resource = resource;
113: this .group = group;
114: this .description = description;
115: this .line = line;
116: this .al = al;
117: }
118:
119: /**
120: * Resource (file or folder) this taks applies to.
121: * @return Resource (file or folder) this taks applies to.
122: */
123: FileObject getResource() {
124: return resource;
125: }
126:
127: /**
128: * The group this task belongs to (error, warning, todo, etc)
129: * @return The group this task belongs to (error, warning, todo, etc)
130: */
131: TaskGroup getGroup() {
132: return group;
133: }
134:
135: /**
136: * Task description.
137: * @return Task description.
138: */
139: String getDescription() {
140: return description;
141: }
142:
143: /**
144: * One-based line number in a text file this task applies to, -1 if the line number is not applicable.
145: * @return One-based line number in a text file this task applies to, -1 if the line number is not applicable.
146: */
147: int getLine() {
148: return line;
149: }
150:
151: /**
152: * Action to be invoked when user double-clicks the task in the Task List window.
153: * @return Task's default action or null if not available.
154: */
155: ActionListener getActionListener() {
156: return al;
157: }
158:
159: /**
160: * Create a new TaskGroup, called from XML layer.
161: */
162: static TaskGroup createGroup(Map<String, String> attrs) {
163: return TaskGroupFactory.create(attrs);
164: }
165:
166: private static Set<String> unknownTaskGroups;
167:
168: private static TaskGroup getTaskGroup(String groupName) {
169: TaskGroup group = TaskGroupFactory.getDefault().getGroup(
170: groupName);
171: if (null == group) {
172: if (null == unknownTaskGroups
173: || !unknownTaskGroups.contains(groupName)) {
174: //show only one warning that the group name is not supported
175: Logger.getLogger(Task.class.getName()).log(
176: Level.INFO,
177: NbBundle.getMessage(Task.class,
178: "Err_UnknownGroupName"), groupName); //NOI18N
179: if (null == unknownTaskGroups)
180: unknownTaskGroups = new HashSet<String>(10);
181: unknownTaskGroups.add(groupName);
182: }
183:
184: group = TaskGroupFactory.getDefault().getDefaultGroup();
185: }
186: return group;
187: }
188:
189: @Override
190: public boolean equals(Object o) {
191: if (o == null)
192: return false;
193: if (getClass() != o.getClass())
194: return false;
195: final Task test = (Task) o;
196:
197: if (this .line != test.line)
198: return false;
199: if (this .description != test.description
200: && this .description != null
201: && !this .description.equals(test.description))
202: return false;
203: if (this .group != test.group && this .group != null
204: && !this .group.equals(test.group))
205: return false;
206: if (this .resource != test.resource && this .resource != null
207: && !this .resource.equals(test.resource))
208: return false;
209: return true;
210: }
211:
212: @Override
213: public int hashCode() {
214: int hash = 7;
215:
216: hash = 17 * hash + this .line;
217: hash = 17
218: * hash
219: + (this .description != null ? this .description
220: .hashCode() : 0);
221: hash = 17 * hash
222: + (this .group != null ? this .group.hashCode() : 0);
223: hash = 17
224: * hash
225: + (this .resource != null ? this .resource.hashCode() : 0);
226: return hash;
227: }
228:
229: @Override
230: public String toString() {
231: StringBuffer buffer = new StringBuffer();
232: buffer.append("[");
233: buffer.append(getResource());
234: buffer.append(", ");
235: buffer.append(getLine());
236: buffer.append(", ");
237: buffer.append(getDescription());
238: buffer.append(", ");
239: buffer.append(getGroup());
240: buffer.append("]");
241: return buffer.toString();
242: }
243: }
|