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 threaddemo.views;
043:
044: import java.io.IOException;
045: import java.util.Collections;
046: import javax.swing.Action;
047: import org.openide.actions.DeleteAction;
048: import org.openide.actions.NewAction;
049: import org.openide.actions.OpenAction;
050: import org.openide.actions.RenameAction;
051: import org.openide.actions.SaveAction;
052: import org.openide.actions.ToolsAction;
053: import org.openide.nodes.AbstractNode;
054: import org.openide.nodes.Children;
055: import org.openide.nodes.Node;
056: import org.openide.util.WeakListeners;
057: import org.openide.util.actions.SystemAction;
058: import org.openide.util.datatransfer.NewType;
059: import threaddemo.data.PhadhailLookups;
060: import threaddemo.data.PhadhailNewType;
061: import threaddemo.model.Phadhail;
062: import threaddemo.model.PhadhailEvent;
063: import threaddemo.model.PhadhailListener;
064: import threaddemo.model.PhadhailNameEvent;
065:
066: // XXX view of DOM tree too
067:
068: /**
069: * A plain node view of a phadhail tree.
070: * @author Jesse Glick
071: */
072: final class PhadhailNode extends AbstractNode implements
073: PhadhailListener {
074:
075: private final Phadhail ph;
076:
077: public PhadhailNode(Phadhail ph) {
078: super (ph.hasChildren() ? new PhadhailChildren(ph)
079: : Children.LEAF, PhadhailLookups.getLookup(ph));
080: this .ph = ph;
081: ph.addPhadhailListener((PhadhailListener) WeakListeners.create(
082: PhadhailListener.class, this , ph));
083: }
084:
085: public String getName() {
086: return ph.getName();
087: }
088:
089: public String getDisplayName() {
090: return ph.getPath();
091: }
092:
093: public void childrenChanged(PhadhailEvent ev) {
094: assert ev.getPhadhail().lock().canRead();
095: ((PhadhailChildren) getChildren()).update();
096: }
097:
098: public void nameChanged(PhadhailNameEvent ev) {
099: assert ev.getPhadhail().lock().canRead();
100: fireNameChange(ev.getOldName(), ev.getNewName());
101: fireDisplayNameChange(null, ev.getPhadhail().getPath());
102: }
103:
104: public boolean canRename() {
105: return true;
106: }
107:
108: public void setName(String s) {
109: try {
110: ph.rename(s);
111: } catch (IOException e) {
112: throw new IllegalArgumentException(e.toString());
113: }
114: }
115:
116: public boolean canDestroy() {
117: return true;
118: }
119:
120: public void destroy() throws IOException {
121: ph.delete();
122: }
123:
124: public NewType[] getNewTypes() {
125: if (ph.hasChildren()) {
126: return new NewType[] { new PhadhailNewType(ph, false),
127: new PhadhailNewType(ph, true), };
128: } else {
129: return new NewType[0];
130: }
131: }
132:
133: public Action[] getActions(boolean context) {
134: return new Action[] { SystemAction.get(OpenAction.class),
135: SystemAction.get(SaveAction.class), null,
136: SystemAction.get(NewAction.class), null,
137: SystemAction.get(DeleteAction.class),
138: SystemAction.get(RenameAction.class),
139: SystemAction.get(ToolsAction.class), };
140: }
141:
142: private static final class PhadhailChildren extends
143: Children.Keys<Phadhail> {
144:
145: private final Phadhail ph;
146:
147: public PhadhailChildren(Phadhail ph) {
148: this .ph = ph;
149: }
150:
151: protected void addNotify() {
152: update();
153: }
154:
155: public void update() {
156: setKeys(ph.getChildren());
157: }
158:
159: protected void removeNotify() {
160: setKeys(Collections.<Phadhail> emptySet());
161: }
162:
163: protected Node[] createNodes(Phadhail ph) {
164: return new Node[] { new PhadhailNode(ph) };
165: }
166:
167: }
168:
169: }
|