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.checklist;
043:
044: import java.awt.event.ActionEvent;
045: import java.awt.event.MouseAdapter;
046: import java.awt.event.MouseEvent;
047: import javax.swing.AbstractAction;
048: import javax.swing.Action;
049: import javax.swing.JComponent;
050: import javax.swing.JList;
051: import javax.swing.KeyStroke;
052:
053: /**
054: * List with checkboxes.
055: */
056: public class CheckList extends JList {
057:
058: private static final long serialVersionUID = 1;
059:
060: /**
061: * Constructs a <code>CheckList</code> that displays the elements in the
062: * specified, non-<code>null</code> model.
063: * All <code>CheckList</code> constructors delegate to this one.
064: *
065: * @param dataModel the data model for this list
066: * @exception IllegalArgumentException if <code>dataModel</code>
067: * is <code>null</code>
068: */
069: public CheckList(CheckListModel dataModel) {
070: super (dataModel);
071: setCellRenderer(new DefaultCheckListCellRenderer());
072: Action action = new CheckAction();
073: getActionMap().put("check", action); //NOI18N
074: registerKeyboardAction(action, KeyStroke.getKeyStroke(' '),
075: JComponent.WHEN_FOCUSED);
076: addMouseListener(new MouseAdapter() {
077: public void mousePressed(MouseEvent e) {
078: JList list = (JList) e.getComponent();
079:
080: int index = list.locationToIndex(e.getPoint());
081: if (index < 0)
082: return;
083:
084: if (e.getX() > 15)
085: return;
086:
087: CheckListModel model = (CheckListModel) getModel();
088: model.setChecked(index, !model.isChecked(index));
089:
090: e.consume();
091: repaint();
092: }
093: });
094: }
095:
096: /**
097: * Constructs a <code>JList</code> that displays the elements in
098: * the specified array. This constructor just delegates to the
099: * <code>ListModel</code> constructor.
100: *
101: * @param state state of the checkboxes
102: * @param listData the array of Objects to be loaded into the data model
103: */
104: public CheckList(boolean[] state, Object[] listData) {
105: this (new DefaultCheckListModel(state, listData));
106: }
107:
108: /**
109: * Constructs a <code>CheckList</code> with an empty model.
110: */
111: public CheckList() {
112: this (new AbstractCheckListModel() {
113: public boolean isChecked(int index) {
114: return false;
115: }
116:
117: public void setChecked(int index, boolean c) {
118: }
119:
120: public int getSize() {
121: return 0;
122: }
123:
124: public Object getElementAt(int index) {
125: return null;
126: }
127: });
128: }
129:
130: /**
131: * Check/uncheck currently selected item
132: */
133: public static class CheckAction extends AbstractAction {
134:
135: private static final long serialVersionUID = 1;
136:
137: public void actionPerformed(ActionEvent e) {
138: JList list = (JList) e.getSource();
139: int index = list.getSelectedIndex();
140: if (index < 0)
141: return;
142: CheckListModel model = (CheckListModel) list.getModel();
143: model.setChecked(index, !model.isChecked(index));
144: }
145: }
146:
147: /**
148: * Sets new model
149: *
150: * @param m new model != null
151: */
152: public void setModel(CheckListModel m) {
153: super.setModel(m);
154: }
155: }
|