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.modules.tasklist.ui;
043:
044: import java.util.Comparator;
045: import java.util.List;
046: import javax.swing.table.AbstractTableModel;
047: import org.netbeans.modules.tasklist.impl.Accessor;
048: import org.netbeans.modules.tasklist.impl.TaskComparator;
049: import org.netbeans.spi.tasklist.Task;
050: import org.netbeans.modules.tasklist.impl.TaskList;
051: import org.netbeans.modules.tasklist.trampoline.TaskGroup;
052: import org.openide.filesystems.FileObject;
053: import org.openide.util.NbBundle;
054:
055: /**
056: *
057: * @author S. Aubrecht
058: */
059: class TaskListModel extends AbstractTableModel implements
060: TaskList.Listener {
061:
062: protected TaskList list;
063:
064: protected static final int COL_GROUP = 0;
065: protected static final int COL_DESCRIPTION = 1;
066: protected static final int COL_FILE = 2;
067: protected static final int COL_LOCATION = 4;
068: protected static final int COL_LINE = 3;
069:
070: /** Creates a new instance of TaskListModel */
071: public TaskListModel(TaskList taskList) {
072: this .list = taskList;
073:
074: sortingCol = Settings.getDefault().getSortingColumn();
075: ascending = Settings.getDefault().isAscendingSort();
076: sortTaskList();
077: }
078:
079: public int getRowCount() {
080: return null == list ? 0 : list.size();
081: }
082:
083: public int getColumnCount() {
084: return 5;
085: }
086:
087: @Override
088: public Class<?> getColumnClass(int column) {
089: if (COL_GROUP == column)
090: return TaskGroup.class;
091: return super .getColumnClass(column);
092: }
093:
094: @Override
095: public String getColumnName(int column) {
096: switch (column) {
097: case COL_GROUP: //group icon
098: return " "; //NOI18N
099: case COL_DESCRIPTION:
100: return NbBundle.getMessage(TaskListModel.class,
101: "LBL_COL_Description"); //NOI18N
102: case COL_FILE:
103: return NbBundle.getMessage(TaskListModel.class,
104: "LBL_COL_File"); //NOI18N
105: case COL_LOCATION:
106: return NbBundle.getMessage(TaskListModel.class,
107: "LBL_COL_Location"); //NOI18N
108: case COL_LINE:
109: return NbBundle.getMessage(TaskListModel.class,
110: "LBL_COL_Line"); //NOI18N
111: }
112: return super .getColumnName(column);
113: }
114:
115: @Override
116: public boolean isCellEditable(int rowIndex, int columnIndex) {
117: return false;
118: }
119:
120: public Object getValueAt(int row, int col) {
121: Task t = getTaskAtRow(row);
122: if (null != t) {
123: switch (col) {
124: case COL_GROUP: //group icon
125: return Accessor.getGroup(t);
126: case COL_DESCRIPTION:
127: return Accessor.getDescription(t);
128: case COL_FILE: {
129: FileObject fo = Accessor.getResource(t);
130: if (null == fo || fo.isFolder())
131: return null;
132: return fo.getNameExt();
133: }
134: case COL_LOCATION: {
135: FileObject fo = Accessor.getResource(t);
136: if (null == fo)
137: return null;
138: if (fo.isFolder())
139: return fo.getPath();
140: fo = fo.getParent();
141: if (null == fo)
142: return null;
143: return fo.getPath();
144: }
145: case COL_LINE:
146: int lineNo = Accessor.getLine(t);
147: return lineNo > 0 ? String.valueOf(lineNo) : null;
148: }
149: }
150: return null;
151: }
152:
153: protected Task getTaskAtRow(int row) {
154: return list.getTask(row);
155: }
156:
157: public void tasksAdded(List<? extends Task> tasks) {
158: if (tasks.isEmpty())
159: return;
160: int startRow = list.indexOf(tasks.get(0));
161: int endRow = list.indexOf(tasks.get(tasks.size() - 1));
162: fireTableRowsInserted(startRow, endRow);
163: }
164:
165: public void tasksRemoved(List<? extends Task> tasks) {
166: if (tasks.isEmpty())
167: return;
168: int startRow = list.indexOf(tasks.get(0));
169: int endRow = list.indexOf(tasks.get(tasks.size() - 1));
170: fireTableRowsDeleted(startRow, endRow);
171: }
172:
173: public void cleared() {
174: fireTableDataChanged();
175: }
176:
177: protected int sortingCol = -1;
178: protected boolean ascending = true;
179:
180: public void toggleSort(int column) {
181: if (column != sortingCol) {
182: sortingCol = column;
183: ascending = true;
184: } else {
185: if (ascending) {
186: ascending = false;
187: } else {
188: sortingCol = -1;
189: }
190: }
191:
192: sortTaskList();
193: }
194:
195: protected void sortTaskList() {
196: Comparator<Task> comparator = null;
197: switch (sortingCol) {
198: case COL_DESCRIPTION:
199: comparator = TaskComparator
200: .getDescriptionComparator(ascending);
201: break;
202: case COL_LINE:
203: comparator = TaskComparator.getLineComparator(ascending);
204: break;
205: case COL_LOCATION:
206: comparator = TaskComparator
207: .getLocationComparator(ascending);
208: break;
209: case COL_FILE:
210: comparator = TaskComparator.getFileComparator(ascending);
211: break;
212: default:
213: comparator = TaskComparator.getDefault();
214: break;
215: }
216: list.setComparator(comparator);
217:
218: Settings.getDefault().setSortingColumn(sortingCol);
219: Settings.getDefault().setAscendingSort(ascending);
220:
221: fireTableDataChanged();
222: }
223:
224: public int getSortingColumnn() {
225: return sortingCol;
226: }
227:
228: public boolean isAscendingSort() {
229: return ascending;
230: }
231:
232: public void setAscendingSort(boolean asc) {
233: if (sortingCol >= 0) {
234: ascending = asc;
235:
236: sortTaskList();
237: }
238: }
239:
240: void attach() {
241: list.addListener(this );
242: }
243:
244: void detach() {
245: list.removeListener(this);
246: }
247: }
|