KickJava   Java API By Example, From Geeks To Geeks.

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


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20
21 package org.netbeans.modules.group;
22
23 import java.io.BufferedReader JavaDoc;
24 import java.io.BufferedWriter JavaDoc;
25 import java.io.File JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.InputStreamReader JavaDoc;
28 import java.io.OutputStreamWriter JavaDoc;
29 import java.text.MessageFormat JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.HashSet JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.Set JavaDoc;
35 import org.openide.ErrorManager;
36 import org.openide.filesystems.FileChangeAdapter;
37 import org.openide.filesystems.FileEvent;
38 import org.openide.filesystems.FileLock;
39 import org.openide.filesystems.FileObject;
40 import org.openide.filesystems.FileUtil;
41 import org.openide.loaders.DataFolder;
42 import org.openide.loaders.DataLoader;
43 import org.openide.loaders.DataObject;
44 import org.openide.loaders.DataObjectExistsException;
45 import org.openide.loaders.TemplateWizard;
46 import org.openide.nodes.Node;
47 import org.openide.nodes.Node.Cookie;
48 import org.openide.util.HelpCtx;
49 import org.openide.util.NbBundle;
50
51 /**
52  * Group shadow - <code>DataObject</code> representing a group of
53  * <code>DataObject</code>s on a filesystem.
54  * It also defines rules for templating of group members:
55  * <ul>
56  * <li>property Template All - if <code>true</code> the group is result,
57  * otherwise bunch of group members
58  * </li>
59  * <li>property Template Pattern - defines <code>MessageFormat</code> where:
60  * <blockquote>
61  * <table>
62  * <tr><td><code>{0}</code></td>
63  * <td>is a member name</td></tr>
64  * <tr><td><code>{1}</code></td>
65  * <td>is a name entered by user
66  * (during template instantiation)</td></tr>
67  * <tr><td valign="baseline"><code>{2}</code></td>
68  * <td>is a posfix got from <code>{0}</code> by using part
69  * following the last &quot;__&quot;<br />
70  * Examples:<br />
71  * &nbsp;&nbsp;- postfix of &quot;hello__World&quot;
72  * is &quot;World&quot;<br />
73  * &nbsp;&nbsp;- postfix of &quot;helloWorld&quot;
74  * is &quot;&quot; (an empty string)</td></tr>
75  * <tr><td><code>{3}</code></td>
76  * <td>backward substitution result
77  * (i.e. __somethingBetween__ =&gt; <code>{1}</code>)
78  * </td></tr>
79  * </table>
80  * </blockquote>
81  * </li>
82  * </ul>
83  *
84  * @author Martin Ryzl
85  * @author Marian Petras
86  * @see org.openide.loaders.DataObject
87  */

88 public class GroupShadow extends DataObject
89                          implements DataObject.Container {
90
91     /** Generated serial version UID. */
92     static final long serialVersionUID =-5086491126656157958L;
93
94     /** extension of files representing groups */
95     public static final String JavaDoc GS_EXTENSION = GroupShadowLoader.GS_EXTENSION;
96
97     /** Name of the Show Links Property. */
98     public static final String JavaDoc PROP_SHOW_LINKS = "showlinks"; // NOI18N
99

100     /** Name of the Template All property. */
101     public static final String JavaDoc PROP_TEMPLATE_ALL = "templateall"; // NOI18N
102

103     /** Name of the Template Pattern property. */
104     public static final String JavaDoc PROP_TEMPLATE_PATTERN = "templatepattern"; // NOI18N
105

106     /** Name of the use template pattern property. */
107     public static final String JavaDoc PROP_USE_PATTERN = "usepattern"; // NOI18N
108

109     /** If true, GroupShadow will show targets for all links. */
110     static boolean showLinks = true;
111
112     // http://www.netbeans.org/issues/show_bug.cgi?id=23350
113
private static TemplateWizard.Iterator groupTemplateIterator = null;
114     
115     /** Anti-loop detection. */
116     private GroupShadow gsprocessed = null;
117
118     /** Returns a template wizard iterator. */
119     private static synchronized TemplateWizard.Iterator
120                                 getGroupTemplateIterator() {
121         if (groupTemplateIterator == null) {
122             groupTemplateIterator = new GroupTemplateIterator();
123         }
124         return groupTemplateIterator;
125     }
126
127     
128     /**
129      * Creates a new group shadow data object.
130      *
131      * @param fo primary file for the new shadow data object
132      * @param dl data loader which caused this constructor to be caused
133      */

134     public GroupShadow(final FileObject fo, DataLoader dl)
135             throws DataObjectExistsException,
136                    IllegalArgumentException JavaDoc,
137                    IOException JavaDoc {
138         super(fo, dl);
139         fo.addFileChangeListener(
140                 new FileChangeAdapter() {
141                     public void fileChanged(FileEvent e) { //group file changed
142
GroupShadow.this.primaryFileChanged();
143                    }
144                 });
145     }
146
147     
148     /** Creates a group node representing this group shadow node. */
149     protected Node createNodeDelegate() {
150         GroupNode node = new GroupNode(this, new GroupNodeChildren(this));
151         addPropertyChangeListener(node);
152         return node;
153     }
154
155     /**
156      */

157     public boolean isDeleteAllowed () {
158         return !getPrimaryFile ().isReadOnly ();
159     }
160
161     /**
162      */

163     public boolean isCopyAllowed () {
164         return true;
165     }
166
167     /**
168      */

169     public boolean isMoveAllowed () {
170         return !getPrimaryFile ().isReadOnly ();
171     }
172
173     /**
174      */

175     public boolean isRenameAllowed () {
176         return !getPrimaryFile ().isReadOnly ();
177     }
178
179     /**
180      * Copies this <code>GroupShadow</code> to a given folder.
181      * If the target folder already contains a file having this object's name,
182      * a similar name is used instead, as described in
183      * {@link FileUtil#findFreeFileName FileUtil.findFreeFileName(...)}.
184      *
185      * @param f target folder
186      * @return new copy of this <code>GroupShadow</code>
187      */

188     protected DataObject handleCopy (DataFolder f) throws IOException JavaDoc {
189         return handleCopy(f, getName());
190     }
191
192     /**
193      * Copies this <code>GroupShadow</code> to a given folder
194      * and sets it a given name.
195      * If the target folder already contains a file having the given name,
196      * a similar name is used instead, as described in
197      * {@link FileUtil#findFreeFileName FileUtil.findFreeFileName(...)}.
198      *
199      * @param f target folder
200      * @param name new name of the file (may be the current name)
201      * @return new copy of this <code>GroupShadow</code>
202      */

203     protected DataObject handleCopy (DataFolder f,
204                                      String JavaDoc name) throws IOException JavaDoc {
205         String JavaDoc newname = FileUtil.findFreeFileName(f.getPrimaryFile(),
206                                                    name,
207                                                    GS_EXTENSION);
208         FileObject fo = FileUtil.copyFile(getPrimaryFile(),
209                                           f.getPrimaryFile(),
210                                           newname);
211         /*
212          * PENDING:
213          * Is it correct? Is it enough to just copy the file
214          * (without any modification)?
215          */

216         
217         return new GroupShadow(fo, getLoader());
218     }
219
220     /**
221      * Deletes this <code>GroupShadow</code>.
222      */

223     protected void handleDelete () throws IOException JavaDoc {
224         FileLock lock = getPrimaryFile ().lock ();
225         try {
226             getPrimaryFile ().delete (lock);
227         } finally {
228             lock.releaseLock ();
229         }
230     }
231
232     /**
233      * Renames this <code>GroupShadow</code>.
234      */

235     protected FileObject handleRename (String JavaDoc name) throws IOException JavaDoc {
236         FileLock lock = getPrimaryFile ().lock ();
237         try {
238             getPrimaryFile ().rename (lock, name, GS_EXTENSION);
239         } finally {
240             lock.releaseLock ();
241         }
242         return getPrimaryFile ();
243     }
244
245     /**
246      * Moves this <code>GroupShadow</code> to a given folder.
247      * If the target folder already contains a file having this object's name,
248      * a similar name is used instead, as described in
249      * {@link FileUtil#findFreeFileName FileUtil.findFreeFileName(...)}.
250      *
251      * @param f target folder
252      * @return new primary file of this <code>GroupShadow</code>
253      */

254     protected FileObject handleMove (DataFolder f) throws IOException JavaDoc {
255         String JavaDoc name = FileUtil.findFreeFileName(f.getPrimaryFile(),
256                                                 getName(),
257                                                 GS_EXTENSION);
258         /*
259          * PENDING:
260          * Is it correct? Is it enough to just move the file
261          * (without any modification)?
262          */

263         
264         return FileUtil.moveFile (getPrimaryFile (), f.getPrimaryFile (), name);
265     }
266
267
268     /*
269      */

270     public HelpCtx getHelpCtx() {
271         return new HelpCtx (GroupShadow.class);
272     }
273
274     /**
275      * Returns a given type of cookie from this object.
276      *
277      * @param cookie type of cookie to be returned
278      * @return if the requested cookie type is
279      * <code>TemplateWizard.Iterator</code>, returns
280      * this group's template wizard iterator;
281      * otherwise it delegates to <code>DataObject</code>'s
282      * <code>getCookie(Class)</code>
283      * @see DataObject#getCookie(Class)
284      */

285     public Cookie getCookie(Class JavaDoc cookie) {
286         if (cookie == TemplateWizard.Iterator.class) {
287             return getGroupTemplateIterator();
288         } else {
289             return super.getCookie (cookie);
290         }
291     }
292
293     /**
294      * Reads names of files contained from a given file.
295      *
296      * @param fo <code>FileObject</code> containing names of contained
297      * files (this object's primary file)
298      * @return names of <code>FileObject</code>s,
299      * each <code>FileObject</code>'s name is relative to the
300      * filesystem the <code>FileObject</code> pertains to
301      * @exception java.io.IOException if an error occured during reading
302      * the given file
303      */

304     public static List JavaDoc readLinks(FileObject fo) throws IOException JavaDoc {
305         List JavaDoc list = new ArrayList JavaDoc();
306         BufferedReader JavaDoc br = null;
307         try {
308             br = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(fo.getInputStream()));
309             String JavaDoc line;
310             while ((line = br.readLine()) != null) {
311                 File JavaDoc f = new File JavaDoc(line);
312                 /* convert relative to absolute files */
313                 if (!f.isAbsolute()) {
314                     /* I would prefer this but it does not work for "../foo" */
315                     // f = FileUtil.toFile(fo.getParent().getFileObject(line));
316
File JavaDoc parentFolder = FileUtil.toFile(fo.getParent());
317                     /* use this node's parent as starting reference for the path */
318                     f = new File JavaDoc(parentFolder, line).getAbsoluteFile();
319                 }
320                 try {
321                     /* also "foo/../bar" is problematic, so canonicalize */
322                     f = f.getCanonicalFile();
323                 } catch (IOException JavaDoc ex) {
324                     ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
325                 }
326                 line = f.getAbsolutePath();
327                 line = line.replace('\\', '/');
328                 list.add(line);
329             }
330         } finally {
331             if (br != null) {
332                 try {
333                     br.close();
334                 } catch (IOException JavaDoc ex) {
335                     /* give up */
336                 }
337             }
338         }
339         return list;
340     }
341
342     /**
343      * Reads names of contained files from the primary file.
344      *
345      * @return list of names of contained files - each file name is relative
346      * to the filesystem the corresponding file is contained in
347      * @exception java.io.IOException if an error occured during reading
348      * the primary file
349      */

350     public List JavaDoc readLinks() throws IOException JavaDoc {
351         return readLinks(getPrimaryFile());
352     }
353
354     /**
355      * Writes a given list (of file names) to a given file.
356      *
357      * @param list list of <code>String</code>s - file names
358      * @param fo file to save the list to
359      * @exception java.io.IOException if an error occured during writing
360      * to the file
361      */

362     public static void writeLinks(List JavaDoc list, FileObject fo) throws IOException JavaDoc {
363         BufferedWriter JavaDoc bw = null;
364         FileLock lock = null;
365         try {
366             lock = fo.lock();
367             bw = new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(
368                     fo.getOutputStream(lock)));
369             for (Iterator JavaDoc i = list.iterator(); i.hasNext(); ) {
370                 String JavaDoc line = (String JavaDoc) i.next();
371                 File JavaDoc f = new File JavaDoc(line);
372                 FileObject link = FileUtil.toFileObject(f);
373                 /* try to convert into a relative file */
374                 if (f.isAbsolute()) {
375                     FileObject parent = fo.getParent();
376                     /* this is simple if this node's parent is the parent of the file */
377                     if (FileUtil.isParentOf(parent, link)) {
378                         line = FileUtil.getRelativePath(parent, link);
379                     } else {
380                         /* otherwise check if this node and the file have the same root */
381                         FileObject root = fo.getFileSystem().getRoot();
382                         if (root.equals(link.getFileSystem().getRoot())) {
383                             /* step up the parents of this node */
384                             StringBuffer JavaDoc lineBuf = new StringBuffer JavaDoc(80);
385                             while (!root.equals(parent)) {
386                                 parent = parent.getParent();
387                                 /* prefix "../" for every step */
388                                 lineBuf.append("../"); //NOI18N
389
/* break if a parent of the file is found is found */
390                                 if (FileUtil.isParentOf(parent, link)) {
391                                     lineBuf.append(FileUtil.getRelativePath(parent, link));
392                                     line = lineBuf.toString();
393                                     break;
394                                 }
395                             }
396                         }
397                     }
398                 }
399                 bw.write(line);
400                 bw.newLine();
401             }
402         } finally {
403             if (bw != null) {
404                 try {
405                     bw.close();
406                 } finally {
407                     if (lock != null) {
408                         lock.releaseLock();
409                     }
410                 }
411             }
412         }
413     }
414
415     /**
416      * Writes a given list (of file names) to this data object's primary file.
417      *
418      * @param list list of <code>String</code>s - file names
419      * @exception java.io.IOException if an error occured during writing
420      * to the file
421      */

422     protected void writeLinks(List JavaDoc list) throws IOException JavaDoc {
423         writeLinks(list, getPrimaryFile());
424     }
425
426     /**
427      * Creates a string representation of a link to a given file.
428      *
429      * @param fo file object to get a link string for
430      * @return string representing a link to the given file
431      */

432     public static String JavaDoc getLinkName(FileObject fo) {
433         return fo.getPath();
434     }
435
436     /**
437      * Finds a <code>DataObject</code> for a given file name.
438      * Lookup for a file having the specified file name is performed
439      * in just one filesystem.
440      * @param filename name of the file, relative to the filesystem it
441      * pertains to
442      * @param ref a reference file (look in the same filesystem as this)
443      * @return the found <code>DataObject</code>, or <code>null</code>
444      * if a file having the specified name was not found
445      * @throws IOException if there was a problem looking for the file
446      */

447     static DataObject getDataObjectByName(String JavaDoc filename, FileObject ref)
448             throws IOException JavaDoc {
449         FileObject file = ref.getFileSystem().findResource(filename);
450         return (file != null) ? DataObject.find(file) : null;
451     }
452
453     /* interface DataObject.Container */
454     /**
455      * Returns <code>DataObject</code>s contained in this group.
456      * Broken links of the group are ignored.
457      *
458      * @return array of <code>DataObject</code>s contained in this group
459      */

460     public DataObject[] getChildren() {
461         Object JavaDoc[] links = getLinks();
462         
463         if (links.length == 0) {
464             return new DataObject[0];
465         }
466         
467         int childrenCount = 0;
468         DataObject[] children = new DataObject[links.length];
469         for (int i = 0; i < links.length; i++) {
470             Object JavaDoc link = links[i];
471             if (link.getClass() == String JavaDoc.class) { //broken link
472
continue;
473             }
474             children[childrenCount++] = (DataObject) link;
475         }
476         
477         /* If there was at least one broken link, shrink the resulting array: */
478         if (childrenCount != links.length) {
479             DataObject[] oldChildren = children;
480             children = new DataObject[childrenCount];
481             System.arraycopy(oldChildren, 0, children, 0, childrenCount);
482         }
483         
484         return children;
485     }
486     
487     /**
488      * Called when a change of this group's primary file is detected.
489      *
490      * Notifies all registered listeners about a change of property
491      * {@link DataObject.Container#PROP_CHILDREN}.
492      */

493     void primaryFileChanged() {
494         
495         /* Implementation of interface DataObject.Container: */
496         firePropertyChange(DataObject.Container.PROP_CHILDREN, null, null);
497     }
498     
499     /**
500      * Returns <code>DataObject</code>s contained in this group.
501      * Reads contents of this group's primary file (names of nested
502      * <code>DataObject</code>s' primary files) and asks for the corresponding
503      * <code>DataObject</code>s. In case of broken links (names of non-existing
504      * files), names of the files are returned instead of
505      * <code>DataObject</code>s. Files that exist but there is no
506      * <code>DataObject</code> for them, are silently ignored.
507      *
508      * @return array containing <code>DataObject</code>s
509      * and names of broken links
510      */

511     public Object JavaDoc[] getLinks() {
512         List JavaDoc filenames;
513         try {
514             filenames = readLinks(getPrimaryFile());
515         } catch (IOException JavaDoc ex) {
516             ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, ex);
517             return new Object JavaDoc[0];
518         }
519         
520         Set JavaDoc set = new HashSet JavaDoc();
521         for (Iterator JavaDoc it = filenames.iterator(); it.hasNext(); ) {
522             String JavaDoc filename = (String JavaDoc) it.next();
523             try {
524                 DataObject obj = getDataObjectByName(filename, getPrimaryFile());
525                 set.add(obj != null ? (Object JavaDoc) obj
526                                     : (Object JavaDoc) new String JavaDoc(filename));
527             } catch (IOException JavaDoc ex) {
528                 ex.printStackTrace();
529                 // can be thrown when the link is not recognized by any data loader
530
// in this case I can't help so ignore it
531
}
532         }
533         return set.toArray();
534     }
535
536     /**
537      * Creates a derivation of a given name by replacing the name's prefix
538      * with a new one. If the given name doesn't start with a given prefix,
539      * no change is done and the original (instance of) string is returned.
540      * If the given name does start with a given prefix, a new instance
541      * of string is returned, even if the prefix replacement is equal to
542      * the prefix to be replaced.
543      *
544      * @param name name whose derivation is to be created
545      * @param oldPrefix prefix to be replaced
546      * @param newPrefix replacement for the original prefix
547      * @return derivation of the original name;
548      * or the original name if it doesn't start with the given prefix
549      */

550     public static String JavaDoc createName(String JavaDoc name,
551                                     String JavaDoc oldPrefix,
552                                     String JavaDoc newPrefix) {
553         if (name.startsWith(oldPrefix)) {
554             return newPrefix + name.substring(oldPrefix.length());
555         }
556         return name;
557     }
558
559     /**
560      * Replaces name according to naming pattern defined by property
561      * {@link #PROP_TEMPLATE_PATTERN}, or falls to {@link #replaceName0()}
562      * if value of the property is <code>null</code>.
563      */

564     private String JavaDoc replaceName(String JavaDoc name, String JavaDoc replacement) {
565         String JavaDoc fmt = getTemplatePattern();
566         
567         if (!isUsePattern() || fmt == null) {
568             return name.replaceAll("__.*?__", replacement); //NOI18N
569
}
570
571         /* filter out all characters before "__" */
572         int i = name.lastIndexOf("__"); //NOI18N
573
String JavaDoc postfix = (i > 0) ? name.substring(i + 2) : ""; //NOI18N
574

575         String JavaDoc subst = string3(name, replacement);
576         return MessageFormat.format(
577                 fmt,
578                 new String JavaDoc[] {name, replacement, postfix, subst});
579     }
580
581     /**
582      * Substitution wrapper for special cases.
583      *
584      * @returns String representing new name after substitution
585      */

586     private String JavaDoc string3(String JavaDoc name, String JavaDoc replacement) {
587
588         String JavaDoc patch;
589         if (name.startsWith("__")) { //NOI18N
590
patch = name;
591         } else {
592             patch = "__" + name; //NOI18N
593
}
594
595         String JavaDoc s3 = substitute(patch, replacement);
596         
597         if (s3.startsWith("__")) { //NOI18N
598
s3 = s3.substring(2);
599         }
600         return s3;
601     }
602
603     /**
604      * Global backward substitution of substrings matching pattern
605      * &quot;<code>__.*?__</code>&quot;. The name is searched for the rightmost
606      * occurence of a substring matching the pattern. If it is found,
607      * the substring is replaced with a specified replacement and the same
608      * process is performed on the result of the substitution, until no matching
609      * substring is found.
610      *
611      * @param name string to process with a substitution
612      * @param replacement string to put in place of matching substrings
613      * of the name
614      * @return result of the substitutions
615      */

616     private String JavaDoc substitute(String JavaDoc name, String JavaDoc replacement) {
617         int last;
618         int lastButOne;
619         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(name);
620         while ((last = sb.lastIndexOf("__")) >= 0 //NOI18N
621
&& (lastButOne = sb.lastIndexOf("__", last - 2)) >= 0) { //NOI18N
622
sb.replace(lastButOne, last + 2, replacement);
623         }
624         return sb.toString();
625     }
626
627     /** Implementation of handleCreateFromTemplate for GroupShadow.
628      * All members of this group are called to create new objects. */

629     protected DataObject handleCreateFromTemplate(DataFolder df, String JavaDoc name) throws IOException JavaDoc {
630         List JavaDoc createdObjs = createGroupFromTemplate(df, name, true);
631         return createdObjs != null && createdObjs.size() > 0
632                ? (DataObject) createdObjs.get(0)
633                : this;
634     }
635
636     /**
637      * Creates new objects from all members of this group.
638      *
639      * @returns list of created objects
640      */

641     List JavaDoc createGroupFromTemplate(DataFolder folder,
642                                          String JavaDoc name,
643                                          boolean root) throws IOException JavaDoc {
644         if (gsprocessed == null) {
645             gsprocessed = this;
646         } else {
647             return null;
648         }
649
650         if (name == null) {// name is not specified
651
name = FileUtil.findFreeFileName(folder.getPrimaryFile(),
652                                              getPrimaryFile().getName(),
653                                              GS_EXTENSION);
654         }
655         Object JavaDoc[] objs = getLinks();
656         ArrayList JavaDoc createdObjs = new ArrayList JavaDoc(objs.length + 1);
657         ArrayList JavaDoc linksList = new ArrayList JavaDoc(objs.length);
658
659         try {
660             for (int i = 0; i < objs.length; i++) {
661                 if (objs[i] instanceof DataObject) {
662                     DataObject original = (DataObject) objs[i];
663
664                     if (original instanceof GroupShadow) {
665                         GroupShadow gs = (GroupShadow) original;
666                         List JavaDoc items = gs.createGroupFromTemplate(folder, name, false);
667                         if (items != null) {
668                             for (int j = 0, n = items.size(); j < n; j++) {
669                                 DataObject obj = (DataObject) items.get(j);
670                                 createdObjs.add(obj);
671                                 linksList.add(getLinkName(obj.getPrimaryFile()));
672                                 if (j == 0
673                                         && obj instanceof GroupShadow
674                                         && gs.getTemplateAll())
675                                     break;
676                             }
677 // createdObjs.addAll(items);
678
}
679                     } else {
680                         String JavaDoc repName = replaceName(original.getName(), name);
681                         DataObject newObj = original.createFromTemplate(folder, repName);
682                         createdObjs.add(newObj);
683                         linksList.add(getLinkName(newObj.getPrimaryFile()));
684                     }
685                 }
686             }
687
688             if (objs.length == 0 || getTemplateAll()) { // create also the group
689
String JavaDoc repName = root ? name : replaceName(getName(), name);
690                 FileObject fo = folder.getPrimaryFile().createData(repName, GS_EXTENSION);
691                 writeLinks(linksList, fo);
692                 GroupShadow gs = (GroupShadow) DataObject.find(fo);
693                 if (gs == null) {
694                     gs = new GroupShadow(fo, getLoader());
695                 }
696                 createdObjs.add(0, gs);
697             }
698         }
699         catch (IOException JavaDoc ex) {
700             throw ex;
701         }
702         catch (Error JavaDoc er) {
703             er.printStackTrace();
704             throw er;
705         }
706         finally {
707             gsprocessed = null;
708         }
709
710         return createdObjs;
711     }
712
713     /**
714      * Setter for showLinks property.
715      *
716      * @param show if true also show real packages and names of targets */

717     public void setShowLinks(boolean show) {
718         showLinks = show;
719     }
720
721     /** Getter for showLinks property. */
722     public boolean getShowLinks() {
723         return showLinks;
724     }
725
726     /**
727      * Setter for template pattern.
728      *
729      * @exception IOException if error occured
730      */

731     public void setTemplatePattern(String JavaDoc templatePattern) throws IOException JavaDoc{
732         final FileObject fo = getPrimaryFile();
733         String JavaDoc old = getTemplatePattern();
734
735         fo.setAttribute(PROP_TEMPLATE_PATTERN, templatePattern);
736
737         if (old != templatePattern) {
738             firePropertyChange(PROP_TEMPLATE_PATTERN, old, templatePattern);
739         }
740
741     }
742
743     /** Getter for template pattern. */
744     public String JavaDoc getTemplatePattern(){
745         Object JavaDoc value = getPrimaryFile().getAttribute(GroupShadow.PROP_TEMPLATE_PATTERN);
746         return (value instanceof String JavaDoc) ? (String JavaDoc) value
747                                          : "{1}"; //NOI18N
748
}
749
750     
751     /** Getter for use pattern property. */
752     public boolean isUsePattern() {
753         Object JavaDoc o = getPrimaryFile().getAttribute(GroupShadow.PROP_USE_PATTERN);
754         return Boolean.TRUE.equals(o);
755     }
756
757     
758     /** Setter for use pattern property. */
759     public void setUsePattern(boolean usePattern) throws IOException JavaDoc {
760         FileObject fileObject = getPrimaryFile();
761         boolean oldValue = isUsePattern();
762         
763         if (usePattern == oldValue) {
764             return;
765         }
766         fileObject.setAttribute(PROP_USE_PATTERN, Boolean.valueOf(usePattern));
767         
768         firePropertyChange(PROP_USE_PATTERN, Boolean.valueOf(oldValue),
769                                              Boolean.valueOf(usePattern));
770     }
771     
772     /** Getter for template all. */
773     public boolean getTemplateAll() {
774         Object JavaDoc o = getPrimaryFile().getAttribute(GroupShadow.PROP_TEMPLATE_ALL);
775         return Boolean.TRUE.equals(o);
776     }
777
778
779     /** Setter for template all. */
780     public void setTemplateAll(boolean templateAll) throws IOException JavaDoc {
781         final FileObject fo = getPrimaryFile();
782         boolean oldtempl = getTemplateAll();
783
784         fo.setAttribute(PROP_TEMPLATE_ALL, (templateAll ? Boolean.TRUE : null));
785
786         if (oldtempl != templateAll) {
787             firePropertyChange(PROP_TEMPLATE_ALL, Boolean.valueOf(oldtempl),
788                                                   Boolean.valueOf(templateAll));
789         }
790     }
791
792     /** Getter for resources */
793     static String JavaDoc getLocalizedString (String JavaDoc s) {
794         return NbBundle.getBundle (GroupShadow.class).getString (s);
795     }
796
797 }
798
Popular Tags