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-2007 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.awt.event.ActionEvent;
045: import javax.swing.AbstractAction;
046: import org.netbeans.modules.tasklist.impl.*;
047: import javax.swing.Action;
048: import javax.swing.ImageIcon;
049: import javax.swing.JCheckBoxMenuItem;
050: import javax.swing.JMenu;
051: import javax.swing.JPopupMenu;
052: import javax.swing.JRadioButtonMenuItem;
053: import org.netbeans.spi.tasklist.Task;
054: import org.netbeans.spi.tasklist.TaskScanningScope;
055: import org.openide.util.NbBundle;
056:
057: /**
058: *
059: * @author S. Aubrecht
060: */
061: class Util {
062:
063: /** Creates a new instance of Util */
064: private Util() {
065: }
066:
067: /**
068: *
069: * @param t
070: * @return
071: */
072: public static Action getDefaultAction(Task t) {
073: return new OpenTaskAction(t);
074: }
075:
076: public static JPopupMenu createPopup(TaskListTable table) {
077: JPopupMenu popup = new JPopupMenu();
078: //TODO fix
079: //show source
080: Task t = table.getSelectedTask();
081: if (null != t) {
082: popup.add(getDefaultAction(t));
083: popup.addSeparator();
084: }
085: //scope
086: JMenu scopeMenu = new JMenu(NbBundle.getMessage(Util.class,
087: "LBL_Scope")); //NOI18N
088: ScanningScopeList scopeList = ScanningScopeList.getDefault();
089: for (TaskScanningScope scope : scopeList
090: .getTaskScanningScopes()) {
091: JRadioButtonMenuItem item = new JRadioButtonMenuItem(
092: new SwitchScopeAction(scope));
093: item.setSelected(scope.equals(TaskManagerImpl.getInstance()
094: .getScope()));
095: scopeMenu.add(item);
096: }
097: popup.add(scopeMenu);
098: //filter
099: JMenu filterMenu = new JMenu(NbBundle.getMessage(Util.class,
100: "LBL_Filter")); //NOI18N
101: FiltersMenuButton.fillMenu(null, filterMenu);
102: popup.add(filterMenu);
103:
104: popup.addSeparator();
105: //refresh
106: popup.add(new RefreshAction());
107: popup.addSeparator();
108: //list options
109: JMenu sortMenu = createSortMenu(table);
110: popup.add(sortMenu);
111:
112: return popup;
113: }
114:
115: private static JMenu createSortMenu(TaskListTable table) {
116: JMenu res = new JMenu(NbBundle.getMessage(Util.class,
117: "LBL_SortBy")); //NOI18N
118: for (int i = 1; i < table.getColumnCount(); i++) {
119: JCheckBoxMenuItem item = new JCheckBoxMenuItem(
120: new SwitchSortAction(table, i));
121: item.setSelected(i == table.getSortColumn());
122: res.add(item);
123: }
124: res.addSeparator();
125: JRadioButtonMenuItem item = new JRadioButtonMenuItem(
126: new SwitchSortOrderAction(table, true));
127: item.setSelected(table.isAscendingSort());
128: res.add(item);
129: item = new JRadioButtonMenuItem(new SwitchSortOrderAction(
130: table, false));
131: item.setSelected(!table.isAscendingSort());
132: res.add(item);
133: return res;
134: }
135:
136: private static class SwitchScopeAction extends AbstractAction {
137: private TaskScanningScope scope;
138:
139: public SwitchScopeAction(TaskScanningScope scope) {
140: super (Accessor.getDisplayName(scope), new ImageIcon(
141: Accessor.getIcon(scope)));
142: this .scope = scope;
143: }
144:
145: public void actionPerformed(ActionEvent e) {
146: TaskManagerImpl tm = TaskManagerImpl.getInstance();
147: tm.observe(scope, tm.getFilter());
148: }
149: }
150:
151: private static class RefreshAction extends AbstractAction {
152: public RefreshAction() {
153: super (NbBundle.getMessage(Util.class, "LBL_Refresh")); //NOI18N
154: }
155:
156: public void actionPerformed(ActionEvent e) {
157: TaskManagerImpl tm = TaskManagerImpl.getInstance();
158: tm.refresh(tm.getScope());
159: }
160: }
161:
162: private static class SwitchSortAction extends AbstractAction {
163: private TaskListTable table;
164: private int col;
165:
166: public SwitchSortAction(TaskListTable table, int col) {
167: super (table.getModel().getColumnName(col));
168: this .table = table;
169: this .col = col;
170: }
171:
172: public void actionPerformed(ActionEvent e) {
173: if (col == table.getSortColumn())
174: table.setSortColumn(-1);
175: else
176: table.setSortColumn(col);
177: table.getTableHeader().repaint();
178: }
179: }
180:
181: private static class SwitchSortOrderAction extends AbstractAction {
182: private TaskListTable table;
183: private boolean asc;
184:
185: public SwitchSortOrderAction(TaskListTable table, boolean asc) {
186: super (asc ? NbBundle.getMessage(Util.class, "LBL_Asc") //NOI18N
187: : NbBundle.getMessage(Util.class, "LBL_Desc")); //NOI18N
188: this .table = table;
189: this .asc = asc;
190: }
191:
192: public void actionPerformed(ActionEvent e) {
193: table.setAscendingSort(asc);
194: table.getTableHeader().repaint();
195: }
196: }
197: }
|