KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > group > LinkNode


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20
21 package org.netbeans.modules.group;
22
23 import java.beans.PropertyChangeEvent JavaDoc;
24 import java.beans.PropertyChangeListener JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.text.MessageFormat JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.ListIterator JavaDoc;
30 import org.openide.ErrorManager;
31 import org.openide.filesystems.FileObject;
32 import org.openide.loaders.DataObject;
33 import org.openide.nodes.Children;
34 import org.openide.nodes.FilterNode;
35 import org.openide.nodes.Node;
36 import org.openide.util.NbBundle;
37
38 /** Node representing one link. */
39 class LinkNode extends FilterNode implements PropertyChangeListener JavaDoc {
40
41     /**
42      * pattern for names of nodes - include names of linked objects
43      *
44      * @see #displayFormatWOLink
45      */

46     private static MessageFormat JavaDoc displayFormatWLink;
47     /**
48      * pattern for names of nodes - names of linked objects missing
49      *
50      * @see #displayFormatWLink
51      */

52     private static MessageFormat JavaDoc displayFormatWOLink;
53     /** group this node's link pertains to */
54     private final GroupShadow group;
55     /** */
56     private String JavaDoc linkedFileName = null;
57     /** data object (represented by a node) this link points to */
58     private DataObject linkedDataObj = null;
59
60
61     /**
62      * Creates a new <code>LinkNode</code> pointing to a given node.
63      *
64      * @param original node to create a link to
65      */

66     public LinkNode(GroupShadow group, Node original) {
67         super(original);
68         
69         this.group = group;
70
71         linkedDataObj = (DataObject) original.getCookie(DataObject.class);
72         if (linkedDataObj == null) {
73             return; // should not happen
74
}
75         FileObject originalFO = linkedDataObj.getPrimaryFile();
76         if (originalFO == null) {
77             return; // should not happen
78
}
79         linkedFileName = GroupShadow.getLinkName(originalFO);
80
81         linkedDataObj.addPropertyChangeListener(this);
82     }
83
84
85     /**
86      * LinkNode can be always destroyed, when Group is also allow delete.
87      *
88      * @return <CODE>true</CODE> if Group allow delete.
89      */

90     public boolean canDestroy () {
91         return group.isDeleteAllowed();
92     }
93
94     /** Destroys node. */
95     public void destroy() throws IOException JavaDoc {
96         linkedDataObj.removePropertyChangeListener(this);
97
98         List JavaDoc list = group.readLinks();
99         for (int i = 0; i < list.size(); i++) {
100             String JavaDoc fileName = (String JavaDoc) list.get(i);
101             if (fileName.equals(linkedFileName)) {
102                 list.remove(i);
103                 i--;
104             }
105         }
106         group.writeLinks(list);
107     }
108
109     /**
110      * @return true if the node can be renamed
111      */

112     public boolean canRename () {
113         return super.canRename() && group.isRenameAllowed();
114     }
115
116     /**
117      * @returns true if this object allows cutting.
118      */

119     public boolean canCut () {
120         return super.canCut() && group.isMoveAllowed();
121     }
122
123
124     /**
125      */

126     public String JavaDoc getDisplayName() {
127         String JavaDoc foldername;
128         FileObject primary = linkedDataObj.getPrimaryFile();
129         String JavaDoc fullname = primary.getPath();
130         int index = fullname.lastIndexOf('/');
131         if (index != -1) {
132             foldername = fullname.substring(0, index + 1);
133         } else {
134             foldername = ""; //NOI18N
135
}
136         Object JavaDoc[] objs = new Object JavaDoc[] {linkedDataObj.getName(),
137                                       fullname,
138                                       foldername,
139                                       linkedDataObj.getNodeDelegate()
140                                             .getDisplayName()};
141
142         if (group.showLinks) {
143             if (displayFormatWLink == null) {
144                 String JavaDoc pattern = NbBundle.getMessage(
145                         LinkNode.class,
146                         "FMT_linkWithTarget"); //NOI18N
147
displayFormatWLink = new MessageFormat JavaDoc(pattern);
148             }
149             return displayFormatWLink.format(objs);
150         } else {
151             if (displayFormatWOLink == null) {
152                 String JavaDoc pattern = NbBundle.getMessage(
153                         LinkNode.class,
154                         "FMT_linkWithoutTarget"); //NOI18N
155
displayFormatWOLink = new MessageFormat JavaDoc(pattern);
156             }
157             return displayFormatWOLink.format(objs);
158         }
159     }
160
161     /** Implements <code>PropertyChangeListener</code>. */
162     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
163         String JavaDoc propertyName = evt.getPropertyName();
164         if (propertyName == null) {
165             return;
166         }
167         if (propertyName.equals(DataObject.PROP_PRIMARY_FILE)) {
168             
169             /* the linked DataObject's primary file has been renamed or moved */
170             try {
171                 List JavaDoc fileNames = new ArrayList JavaDoc(group.readLinks());
172                 String JavaDoc newFileName = GroupShadow.getLinkName(
173                         linkedDataObj.getPrimaryFile());
174
175                 for (ListIterator JavaDoc i = fileNames.listIterator(); i.hasNext(); ) {
176                     String JavaDoc fileName = (String JavaDoc) i.next();
177                     if (fileName.equals(linkedFileName)) {
178                         i.set(newFileName);
179                     }
180                 }
181                 linkedFileName = newFileName;
182                 group.writeLinks(fileNames);
183                 fireDisplayNameChange(null, null);
184             } catch (IOException JavaDoc ex) {
185                 ErrorManager errMgr = ErrorManager.getDefault();
186                 String JavaDoc msg = NbBundle.getMessage(
187                         LinkNode.class,
188                         "MSG_Cannot_rename_link", //NOI18N
189
group.getPrimaryFile().getPath());
190                 errMgr.notify(ErrorManager.INFORMATIONAL,
191                               errMgr.annotate(ex, msg));
192             }
193         } else if (propertyName.equals(DataObject.PROP_VALID)) {
194             
195             /* the linked DataObject might have been deleted */
196             if (!linkedDataObj.isValid()) {
197                 linkedDataObj.removePropertyChangeListener(this);
198                 ((GroupNodeChildren) group.getNodeDelegate().getChildren())
199                         .update();
200             }
201         }
202     }
203 }
204
Popular Tags