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 java.awt.event.ActionListener;
046: import java.beans.PropertyChangeEvent;
047: import java.beans.PropertyChangeListener;
048: import java.io.IOException;
049: import java.util.List;
050: import java.util.logging.Level;
051: import java.util.logging.Logger;
052: import javax.swing.AbstractAction;
053: import javax.swing.ImageIcon;
054: import javax.swing.JMenu;
055: import javax.swing.JPopupMenu;
056: import javax.swing.JRadioButtonMenuItem;
057: import org.netbeans.modules.tasklist.filter.FilterEditor;
058: import org.netbeans.modules.tasklist.filter.FilterRepository;
059: import org.netbeans.modules.tasklist.filter.TaskFilter;
060: import org.netbeans.modules.tasklist.impl.TaskManagerImpl;
061: import org.openide.util.NbBundle;
062: import org.openide.util.Utilities;
063:
064: /**
065: *
066: * @author S. Aubrecht
067: */
068: public class FiltersMenuButton extends MenuToggleButton implements
069: PropertyChangeListener {
070:
071: private TaskManagerImpl taskManager;
072:
073: /** Creates a new instance of FiltersMenuButton */
074: public FiltersMenuButton(TaskFilter currentFilter) {
075: super (
076: new ImageIcon(
077: Utilities
078: .loadImage("org/netbeans/modules/tasklist/ui/resources/filter.png")), //NOI18N
079: new ImageIcon(
080: Utilities
081: .loadImage("org/netbeans/modules/tasklist/ui/resources/filter_rollover.png")),
082: 4); //NOI18N
083: taskManager = TaskManagerImpl.getInstance();
084:
085: updateState(currentFilter);
086:
087: addActionListener(new ActionListener() {
088: public void actionPerformed(ActionEvent arg0) {
089: if (!isSelected()) {
090: taskManager.observe(taskManager.getScope(),
091: TaskFilter.EMPTY);
092: } else {
093: openFilterEditor();
094: updateState(taskManager.getFilter());
095: }
096: }
097: });
098: }
099:
100: @Override
101: public void addNotify() {
102: super .addNotify();
103: taskManager.addPropertyChangeListener(
104: TaskManagerImpl.PROP_FILTER, this );
105: }
106:
107: @Override
108: public void removeNotify() {
109: super .removeNotify();
110: taskManager.removePropertyChangeListener(
111: TaskManagerImpl.PROP_FILTER, this );
112: }
113:
114: @Override
115: protected JPopupMenu getPopupMenu() {
116: JPopupMenu popup = new JPopupMenu();
117:
118: fillMenu(popup, null);
119:
120: return popup;
121: }
122:
123: static void fillMenu(JPopupMenu popup, JMenu menu) {
124: assert null != popup || null != menu;
125: FilterRepository filterRep = FilterRepository.getDefault();
126: TaskManagerImpl tm = TaskManagerImpl.getInstance();
127: TaskFilter activeFilter = tm.getFilter();
128:
129: JRadioButtonMenuItem item = new JRadioButtonMenuItem(
130: new CancelFilterAction());
131: item.setSelected(TaskFilter.EMPTY.equals(activeFilter));
132: if (null == popup)
133: menu.add(item);
134: else
135: popup.add(item);
136:
137: if (null == popup)
138: menu.addSeparator();
139: else
140: popup.addSeparator();
141:
142: List<TaskFilter> allFilters = filterRep.getAllFilters();
143: for (TaskFilter tf : allFilters) {
144: item = new JRadioButtonMenuItem(new SetFilterAction(tf));
145: item.setSelected(activeFilter.equals(tf));
146: if (null == popup)
147: menu.add(item);
148: else
149: popup.add(item);
150: }
151: if (allFilters.size() > 0) {
152: if (null == popup)
153: menu.addSeparator();
154: else
155: popup.addSeparator();
156: }
157:
158: if (null == popup)
159: menu.add(new ManageFiltersAction());
160: else
161: popup.add(new ManageFiltersAction());
162: }
163:
164: private static class CancelFilterAction extends AbstractAction {
165:
166: public CancelFilterAction() {
167: super (NbBundle.getMessage(FiltersMenuButton.class,
168: "LBL_CancelFilter")); //NOI18N
169: }
170:
171: public void actionPerformed(ActionEvent e) {
172: FilterRepository.getDefault().setActive(null);
173: try {
174: FilterRepository.getDefault().save();
175: } catch (IOException ioE) {
176: Logger.getLogger(FiltersMenuButton.class.getName())
177: .log(Level.INFO, ioE.getMessage(), ioE);
178: }
179: TaskManagerImpl tm = TaskManagerImpl.getInstance();
180: tm.observe(tm.getScope(), TaskFilter.EMPTY);
181: }
182: }
183:
184: private static class SetFilterAction extends AbstractAction {
185: private TaskFilter filter;
186:
187: public SetFilterAction(TaskFilter filter) {
188: super (filter.getName());
189: this .filter = filter;
190: }
191:
192: public void actionPerformed(ActionEvent e) {
193: FilterRepository.getDefault().setActive(filter);
194: try {
195: FilterRepository.getDefault().save();
196: } catch (IOException ioE) {
197: Logger.getLogger(FiltersMenuButton.class.getName())
198: .log(Level.INFO, ioE.getMessage(), ioE);
199: }
200: TaskManagerImpl tm = TaskManagerImpl.getInstance();
201: tm.observe(tm.getScope(), filter);
202: }
203: }
204:
205: private static class ManageFiltersAction extends AbstractAction {
206: public ManageFiltersAction() {
207: super (NbBundle.getMessage(FiltersMenuButton.class,
208: "LBL_EditFilters")); //NOI18N
209: }
210:
211: public void actionPerformed(ActionEvent arg0) {
212: openFilterEditor();
213: }
214: }
215:
216: public void propertyChange(PropertyChangeEvent e) {
217: updateState(taskManager.getFilter());
218: }
219:
220: private void updateState(TaskFilter filter) {
221: if (null == filter || TaskFilter.EMPTY.equals(filter)) {
222: setSelected(false);
223: setToolTipText(NbBundle.getMessage(FiltersMenuButton.class,
224: "HINT_SelectFilter")); //NOI18N
225: FilterRepository.getDefault().setActive(null);
226: } else {
227: setSelected(true);
228: setToolTipText(filter.getName());
229: FilterRepository.getDefault().setActive(filter);
230: }
231: }
232:
233: private static void openFilterEditor() {
234: FilterRepository filterRep = FilterRepository.getDefault();
235: FilterRepository clone = (FilterRepository) filterRep.clone();
236: FilterEditor fe = new FilterEditor(clone);
237: if (fe.showWindow()) {
238: filterRep.assign(clone);
239: TaskManagerImpl tm = TaskManagerImpl.getInstance();
240: tm.observe(tm.getScope(), filterRep.getActive());
241: try {
242: filterRep.save();
243: } catch (IOException ioE) {
244: Logger.getLogger(FiltersMenuButton.class.getName())
245: .log(Level.INFO, ioE.getMessage(), ioE);
246: }
247: }
248: }
249: }
|