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: /*
043: * Created on Jun 9, 2003
044: *
045: */
046: package org.netbeans.modules.uml.ui.swing.treetable;
047:
048: import javax.swing.ListSelectionModel;
049: import javax.swing.event.ListSelectionEvent;
050: import javax.swing.event.ListSelectionListener;
051: import javax.swing.tree.DefaultTreeSelectionModel;
052:
053: /**
054: * @author sumitabhk
055: *
056: */
057: /**
058: * ListToTreeSelectionModelWrapper extends DefaultTreeSelectionModel
059: * to listen for changes in the ListSelectionModel it maintains. Once
060: * a change in the ListSelectionModel happens, the paths are updated
061: * in the DefaultTreeSelectionModel.
062: */
063: class TreeTableSelectionModel1 extends DefaultTreeSelectionModel {
064:
065: /** Set to true when we are updating the ListSelectionModel. */
066: protected boolean updatingListSelectionModel;
067:
068: /**
069: *
070: */
071: public TreeTableSelectionModel1() {
072: super ();
073: getListSelectionModel().addListSelectionListener(
074: createListSelectionListener());
075: }
076:
077: /**
078: * Returns the list selection model. ListToTreeSelectionModelWrapper
079: * listens for changes to this model and updates the selected paths
080: * accordingly.
081: */
082: ListSelectionModel getListSelectionModel() {
083: return listSelectionModel;
084: }
085:
086: /**
087: * This is overridden to set <code>updatingListSelectionModel</code>
088: * and message super. This is the only place DefaultTreeSelectionModel
089: * alters the ListSelectionModel.
090: */
091: public void resetRowSelection() {
092: if (!updatingListSelectionModel) {
093: updatingListSelectionModel = true;
094: try {
095: super .resetRowSelection();
096: } finally {
097: updatingListSelectionModel = false;
098: }
099: }
100: // Notice how we don't message super if
101: // updatingListSelectionModel is true. If
102: // updatingListSelectionModel is true, it implies the
103: // ListSelectionModel has already been updated and the
104: // paths are the only thing that needs to be updated.
105: }
106:
107: /**
108: * Creates and returns an instance of ListSelectionHandler.
109: */
110: protected ListSelectionListener createListSelectionListener() {
111: return new ListSelectionHandler();
112: }
113:
114: /**
115: * If <code>updatingListSelectionModel</code> is false, this will
116: * reset the selected paths from the selected rows in the list
117: * selection model.
118: */
119: protected void updateSelectedPathsFromSelectedRows() {
120: if (!updatingListSelectionModel) {
121: updatingListSelectionModel = true;
122: try {
123: // This is way expensive, ListSelectionModel needs an
124: // enumerator for iterating.
125: int min = listSelectionModel.getMinSelectionIndex();
126: int max = listSelectionModel.getMaxSelectionIndex();
127:
128: clearSelection();
129: if (min != -1 && max != -1) {
130: for (int counter = min; counter <= max; counter++) {
131: if (listSelectionModel.isSelectedIndex(counter)) {
132: // TreePath selPath = tree.getPathForRow
133: // (counter);
134: //
135: // if(selPath != null) {
136: // addSelectionPath(selPath);
137: // }
138: }
139: }
140: }
141: } finally {
142: updatingListSelectionModel = false;
143: }
144: }
145: }
146:
147: /**
148: * Class responsible for calling updateSelectedPathsFromSelectedRows
149: * when the selection of the list changse.
150: */
151: class ListSelectionHandler implements ListSelectionListener {
152: public void valueChanged(ListSelectionEvent e) {
153: updateSelectedPathsFromSelectedRows();
154: }
155: }
156: }
|