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: package org.netbeans.modules.refactoring.spi.impl;
042:
043: import java.io.IOException;
044: import java.util.Enumeration;
045: import javax.swing.Icon;
046: import javax.swing.tree.*;
047: import org.netbeans.modules.refactoring.spi.ui.TreeElement;
048: import org.openide.text.PositionBounds;
049: import org.netbeans.modules.refactoring.api.RefactoringElement;
050: import org.netbeans.modules.refactoring.plugins.RefactoringTreeElement;
051: import org.openide.filesystems.FileObject;
052: import org.openide.util.NbBundle;
053:
054: /**
055: * @author Pavel Flaska
056: */
057: public class CheckNode extends DefaultMutableTreeNode {
058:
059: public final static int SINGLE_SELECTION = 0;
060: public final static int DIG_IN_SELECTION = 4;
061:
062: private int selectionMode;
063: private boolean isSelected;
064:
065: private String nodeLabel;
066: private Icon icon;
067:
068: private boolean disabled = false;
069: private boolean needsRefresh = false;
070:
071: public CheckNode(Object userObject, String nodeLabel, Icon icon) {
072: super (userObject, !(userObject instanceof RefactoringElement));
073: this .isSelected = true;
074: setSelectionMode(DIG_IN_SELECTION);
075: this .nodeLabel = nodeLabel;
076: this .icon = icon;
077: if (userObject instanceof TreeElement) {
078: if (((TreeElement) userObject).getUserObject() instanceof RefactoringElement) {
079: RefactoringElement ree = (RefactoringElement) ((TreeElement) userObject)
080: .getUserObject();
081: int s = ree.getStatus();
082: if (s == RefactoringElement.GUARDED
083: || s == RefactoringElement.READ_ONLY) {
084: isSelected = false;
085: disabled = true;
086: this .nodeLabel = "[<font color=#CC0000>" // NOI18N
087: + NbBundle
088: .getMessage(
089: CheckNode.class,
090: s == RefactoringElement.GUARDED ? "LBL_InGuardedBlock"
091: : "LBL_InReadOnlyFile")
092: + "</font>]" + this .nodeLabel; // NOI18N
093: }
094: }
095: }
096: }
097:
098: String getLabel() {
099: return nodeLabel;
100: }
101:
102: Icon getIcon() {
103: return icon;
104: }
105:
106: public void setDisabled() {
107: disabled = true;
108: isSelected = false;
109: removeAllChildren();
110: }
111:
112: boolean isDisabled() {
113: return disabled;
114: }
115:
116: void setNeedsRefresh() {
117: needsRefresh = true;
118: setDisabled();
119: }
120:
121: boolean needsRefresh() {
122: return needsRefresh;
123: }
124:
125: public void setSelectionMode(int mode) {
126: selectionMode = mode;
127: }
128:
129: public int getSelectionMode() {
130: return selectionMode;
131: }
132:
133: public void setSelected(boolean isSelected) {
134: this .isSelected = isSelected;
135: if (userObject instanceof TreeElement) {
136: Object ob = ((TreeElement) userObject).getUserObject();
137: if (ob instanceof RefactoringElement) {
138: ((RefactoringElement) ob).setEnabled(isSelected);
139: }
140: }
141: if ((selectionMode == DIG_IN_SELECTION) && (children != null)) {
142: Enumeration e = children.elements();
143: while (e.hasMoreElements()) {
144: CheckNode node = (CheckNode) e.nextElement();
145: node.setSelected(isSelected);
146: }
147: }
148: }
149:
150: public boolean isSelected() {
151: if (userObject instanceof TreeElement) {
152: Object ob = ((TreeElement) userObject).getUserObject();
153: if (ob instanceof RefactoringElement) {
154: return ((RefactoringElement) ob).isEnabled()
155: && !((((RefactoringElement) ob).getStatus() == RefactoringElement.GUARDED) || (((RefactoringElement) ob)
156: .getStatus() == RefactoringElement.READ_ONLY));
157: }
158: }
159: return isSelected;
160: }
161:
162: public PositionBounds getPosition() {
163: if (userObject instanceof TreeElement) {
164: Object re = ((TreeElement) userObject).getUserObject();
165: if (re instanceof RefactoringElement)
166: return ((RefactoringElement) re).getPosition();
167: }
168: return null;
169: }
170:
171: private String tooltip;
172:
173: public String getToolTip() {
174: if (tooltip == null) {
175: if (userObject instanceof TreeElement) {
176: Object re = ((TreeElement) userObject).getUserObject();
177: if (re instanceof RefactoringElement) {
178: RefactoringElement ree = (RefactoringElement) re;
179: PositionBounds bounds = getPosition();
180: FileObject file = ree.getParentFile();
181: if (bounds != null && file != null) {
182: int line;
183: try {
184: line = bounds.getBegin().getLine() + 1;
185: } catch (IOException ioe) {
186: return null;
187: }
188: tooltip = file.getPath() + ':' + line;
189: }
190: }
191: }
192: return null;
193: }
194: return tooltip;
195: }
196: }
|