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.mobility.end2end.ui.treeview;
043:
044: import java.util.List;
045: import javax.swing.event.ChangeEvent;
046: import javax.swing.event.ChangeListener;
047:
048: import java.awt.event.FocusListener;
049: import java.awt.event.MouseListener;
050: import java.util.ArrayList;
051: import javax.swing.SwingUtilities;
052: import javax.swing.UIManager;
053: import javax.swing.tree.DefaultTreeModel;
054: import javax.swing.tree.TreePath;
055: import org.netbeans.modules.mobility.end2end.util.ServiceNodeManager;
056: import org.openide.nodes.Children;
057: import org.openide.nodes.Children;
058: import org.openide.nodes.Node;
059: import org.openide.explorer.view.BeanTreeView;
060: import org.openide.nodes.AbstractNode;
061:
062: /**
063: * User: suchys
064: * Date: Dec 12, 2003
065: * Time: 3:57:41 PM
066: */
067: public class MethodCheckedTreeBeanView extends BeanTreeView implements
068: Runnable {
069:
070: private Node root;
071: private MethodCheckedNodeEditor editor;
072:
073: private final List<ChangeListener> listeners = new ArrayList<ChangeListener>();
074:
075: public static final String rootPath = "#root"; // NOI18N
076:
077: private final Node waitNode = new AbstractNode(Children.LEAF);
078:
079: public MethodCheckedTreeBeanView() {
080: super ();
081: FocusListener[] fl = tree.getFocusListeners();
082: for (int i = 0; i < fl.length; i++) {
083: if (fl[i].getClass().getName().startsWith("org.openide")) { //NOI18N
084: tree.removeFocusListener(fl[i]);
085: }
086: }
087: MouseListener[] ml = tree.getMouseListeners();
088: for (int i = 0; i < ml.length; i++) {
089: if (ml[i].getClass().getName().startsWith("org.openide")) { //NOI18N
090: tree.removeMouseListener(ml[i]);
091: }
092: }
093: }
094:
095: public Node getWaitNode() {
096: return waitNode;
097: }
098:
099: public void setEditable(final boolean editable) {
100: tree.setEditable(editable);
101: tree.setBackground(UIManager.getDefaults().getColor(
102: editable ? "Tree.background"
103: : "TextField.inactiveBackground")); //NOI18N
104: }
105:
106: public void setRoot(final Node root) {
107: this .root = root;
108: tree.setCellRenderer(new MethodCheckedNodeRenderer());
109: tree.setCellEditor(editor = new MethodCheckedNodeEditor(tree));
110: tree.setEditable(true);
111: editor.setContentStorage(this );
112: updateMixedStates(root);
113: }
114:
115: public synchronized void updateTreeNodeStates(Node n) {
116: if (root == null)
117: return;
118: if (n != null)
119: forceState(
120: n.getChildren(),
121: (MultiStateCheckBox.State) n
122: .getValue(ServiceNodeManager.NODE_SELECTION_ATTRIBUTE));
123: updateMixedStates(root);
124: fireChange();
125: SwingUtilities.invokeLater(this );
126: }
127:
128: public void run() {
129: TreePath tp = tree.getSelectionPath();
130: ((DefaultTreeModel) tree.getModel()).reload();
131: if (tp != null)
132: tree.scrollPathToVisible(tp);
133: }
134:
135: private void forceState(Children ch, MultiStateCheckBox.State state) {
136: for (Node n : ch.getNodes()) {
137: n.setValue(ServiceNodeManager.NODE_SELECTION_ATTRIBUTE,
138: state);
139: forceState(n.getChildren(), state);
140: }
141: }
142:
143: private MultiStateCheckBox.State updateMixedStates(Node pn) {
144: Children ch = pn.getChildren();
145: MultiStateCheckBox.State ret = null;
146: for (Node n : pn.getChildren().getNodes()) {
147: if (n.getValue(ServiceNodeManager.NODE_VALIDITY_ATTRIBUTE) != Boolean.FALSE) {
148: MultiStateCheckBox.State state = updateMixedStates(n);
149: if (ret == null)
150: ret = state;
151: else if (state != ret) {
152: ret = MultiStateCheckBox.State.MIXED;
153: }
154: }
155: }
156: if (ret == null)
157: ret = (MultiStateCheckBox.State) pn
158: .getValue(ServiceNodeManager.NODE_SELECTION_ATTRIBUTE);
159: if (ret == null)
160: ret = MultiStateCheckBox.State.UNSELECTED;
161: pn.setValue(ServiceNodeManager.NODE_SELECTION_ATTRIBUTE, ret);
162: return ret;
163: }
164:
165: private void fireChange() {
166: final ChangeEvent e = new ChangeEvent(this );
167: for (ChangeListener cl : listeners) {
168: cl.stateChanged(e);
169: }
170: }
171:
172: public void addChangeListener(final ChangeListener l) {
173: listeners.add(l);
174: }
175:
176: public void removeChangeListener(final ChangeListener l) {
177: listeners.remove(l);
178: }
179: }
|