2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
42
package org.openide.loaders;
45
import org.openide.filesystems.*;
46
import org.openide.util.Lookup;
47
import org.openide.util.NbBundle;
49
/** Entry that works with plain files. Copies, moves,
50
* renames and deletes them without any modification.
52
* @author Jaroslav Tulach
54
public class FileEntry extends MultiDataObject.Entry {
55
/** generated Serialized Version UID */
56
static final long serialVersionUID = 5972727204237511983L;
58
/** Creates new file entry initially attached to a given file object.
59
* @param obj the data object this entry belongs to
60
* @param fo the file object for the entry
62
public FileEntry(MultiDataObject obj, FileObject fo) {
66
/* Makes a copy to given folder.
67
* @param f the folder to copy to
68
* @param suffix the suffix to add to the name of original file
70
public FileObject copy (FileObject f, String suffix) throws IOException {
71
FileObject fo = getFile();
72
String newName = fo.getName() + suffix;
73
return fo.copy (f, newName, fo.getExt ());
77
public FileObject copyRename(FileObject f, String name, String ext) throws IOException {
78
FileObject fo = getFile();
79
return fo.copy (f, name, ext);
82
/* Renames underlying fileobject. This implementation return the
85
* @param name new name
86
* @return file object with renamed file
88
public FileObject rename (String name) throws IOException {
89
boolean locked = isLocked ();
91
FileLock lock = takeLock();
93
getFile().rename(lock, name, getFile().getExt());
101
/* Moves file to another folder
102
* @param f the folder
103
* @param suffix the suffix to append to original name of the file
104
* @return new file object for the file
106
public FileObject move (FileObject f, String suffix) throws IOException {
107
boolean locked = isLocked ();
109
FileObject fo = getFile();
110
FileLock lock = takeLock ();
112
String newName = fo.getName() + suffix;
113
FileObject dest = fo.move (lock, f, newName, fo.getExt ());
121
/* Deletes file object
123
public void delete () throws IOException {
124
/* JST: This fixes bug 4660. But I am not sure whether this will not
125
* create another or open some old bug.
128
throw new IOException(NbBundle.getBundle (FileEntry.class).getString ("EXC_SharedAccess"));
130
boolean locked = isLocked ();
132
FileLock lock = takeLock();
134
getFile().delete(lock);
142
/* Creates dataobject from template. Copies the file
143
* @param f the folder to create instance in
144
* @param name name of the file or null if it should be choosen automaticly
146
public FileObject createFromTemplate (FileObject f, String name) throws IOException {
148
name = FileUtil.findFreeFileName(
150
getFile ().getName (), getFile ().getExt ()
155
FileObject fo = null;
156
for (CreateFromTemplateHandler h : Lookup.getDefault().lookupAll(CreateFromTemplateHandler.class)) {
157
if (h.accept(getFile())) {
158
fo = h.createFromTemplate(getFile(), f, name,
159
DataObject.CreateAction.enhanceParameters(
160
DataObject.CreateAction.findParameters(name),
161
name, getFile().getExt()));
168
fo = getFile().copy (f, name, getFile().getExt ());
172
// unmark template state
173
DataObject.setTemplate (fo, false);
178
/** Specialized entry that simplifies substitution when a file entry
179
* is created from template.
180
* Subclasses must implement
181
* {@link #createFormat} and return a valid text format that
182
* will be used for converting the lines of the original file
183
* to lines in the newly created one.
185
public abstract static class Format extends FileEntry {
186
static final long serialVersionUID =8896750589709521197L;
187
/** Create a new entry initially attached to a given file object.
188
* @param obj the data object this entry belongs to
189
* @param fo the file object for the entry
191
public Format (MultiDataObject obj, FileObject fo) {
195
/* Creates dataobject from template. Copies the file and applyes substitutions
196
* provided by the createFormat method.
198
* @param f the folder to create instance in
199
* @param name name of the file or null if it should be choosen automaticly
201
public FileObject createFromTemplate (FileObject f, String name) throws IOException {
202
String ext = getFile ().getExt ();
205
name = FileUtil.findFreeFileName(
207
getFile ().getName (), ext
211
FileObject fo = null;
212
for (CreateFromTemplateHandler h : Lookup.getDefault().lookupAll(CreateFromTemplateHandler.class)) {
213
if (h.accept(getFile())) {
214
fo = h.createFromTemplate(
216
DataObject.CreateAction.enhanceParameters(
217
DataObject.CreateAction.findParameters(name),
218
name, getFile().getExt()));
225
// unmark template state
226
DataObject.setTemplate (fo, false);
230
fo = f.createData (name, ext);
232
java.text.Format frm = createFormat (f, name, ext);
234
BufferedReader r = new BufferedReader (new InputStreamReader (getFile ().getInputStream ()));
236
FileLock lock = fo.lock ();
238
BufferedWriter w = new BufferedWriter (new OutputStreamWriter (fo.getOutputStream (lock)));
242
while ((current = r.readLine ()) != null) {
243
w.write (frm.format (current));
258
FileUtil.copyAttributes (getFile (), fo);
260
// unmark template state
261
DataObject.setTemplate (fo, false);
266
/** Provide a suitable format for
267
* substitution of lines.
269
* @param target the target folder of the installation
270
* @param n the name the file will have
271
* @param e the extension the file will have
272
* @return a format to use for formatting lines
274
protected abstract java.text.Format createFormat (FileObject target, String n, String e);
279
/** Simple file entry variant. It does nearly nothing.
280
* When a file is copied, it does nothing. If it is moved
281
* or renamed it deletes the file.
283
* Useful for representing useless files.
285
public final static class Numb extends MultiDataObject.Entry {
286
/** generated Serialized Version UID */
287
static final long serialVersionUID = -6572157492885890612L;
290
* Create a dummy entry.
291
* @param obj the data object this entry belongs to
292
* @param fo the file object to create an entry for
294
public Numb (MultiDataObject obj, FileObject fo) {
298
/** Is not important at all.
301
public boolean isImportant () {
307
* @param suffix ignored
308
* @return <code>null</code>
310
public FileObject copy (FileObject f, String suffix) {
315
* @param name ignored
316
* @return <code>null</code>
317
* @throws IOException in case of problem
319
public FileObject rename (String name) throws IOException {
326
* @param suffix ignored
327
* @return <code>null</code>
328
* @throws IOException in case of problem
330
public FileObject move (FileObject f, String suffix) throws IOException {
336
* @throws IOException in case of problem
338
public void delete () throws IOException {
343
* @throws IOException in case of problem
345
private void stdBehaving () throws IOException {
346
if (getFile() == null)
350
throw new IOException (NbBundle.getBundle (FileEntry.class).getString ("EXC_SharedAccess"));
352
FileLock lock = takeLock();
354
getFile().delete(lock);
363
* @param name ignored
364
* @return <code>null</code>
366
public FileObject createFromTemplate (FileObject f, String name) {
370
/** Simple entry for handling folders, on copy, move and createFromTemplate
371
* it creates new empty folder and copies attributes of source folder.
372
* Operation on children should be performed explicitly by DataObject
376
public final static class Folder extends MultiDataObject.Entry {
378
/** Creates new FolderEntry */
379
public Folder (MultiDataObject obj, FileObject fo) {
383
/** Creates new folder and copies attributes.
384
* @param f the folder to create this entry in
385
* @param suffix suffix appended to the new name to use
386
* @return the copied <code>FileObject</code> or <code>null</code> if it cannot be copied
387
* @exception IOException when the operation fails
389
public FileObject copy (FileObject f, String suffix) throws IOException {
390
String add = suffix + ((getFile ().getExt ().length () > 0) ? "." + getFile ().getExt () : "");
392
FileObject fo = FileUtil.createFolder (f, getFile ().getName () + add);
393
FileUtil.copyAttributes (getFile (), fo);
398
/** Nearly the same like {@link #copy (FileObject f, String suffix)}.
399
* @param f the folder to move this entry to
400
* @param suffix suffix appended to the new name to use
401
* @return the moved <code>FileObject</code> or <code>null</code> if it has been deleted
402
* @exception IOException when the operation fails
404
public FileObject move (FileObject f, String suffix) throws IOException {
405
return copy (f, suffix);
408
/** Creates new folder and copies attributes, the template flag is cleared.
409
* @param f the folder to create this entry in
410
* @param name the new name to use
411
* @return the copied <code>FileObject</code> or <code>null</code> if it cannot be copied
412
* @exception IOException when the operation fails
414
public FileObject createFromTemplate (FileObject f, String name) throws IOException {
416
name = FileUtil.findFreeFileName(
418
getFile ().getName (), getFile ().getExt ()
421
FileObject fo = FileUtil.createFolder (f, name);
423
FileUtil.copyAttributes (getFile (), fo);
424
DataObject.setTemplate (fo, false);
430
* @param name the new name
431
* @return the renamed <code>FileObject</code> or <code>null</code> if it has been deleted
432
* @exception IOException when the operation fails
434
public FileObject rename (String name) throws IOException {
435
boolean locked = isLocked ();
436
FileLock lock = takeLock ();
438
getFile ().rename (lock, name, null);
446
/** Deletes folder associated with entry. Although filesystems delete
447
* folders recursively, it is better to delete children DataObjects before
448
* the {@link #FileEntry.Folder} entry is deleted.
449
* @exception IOException when the operation fails
451
public void delete () throws IOException {
452
boolean locked = isLocked ();
453
FileLock lock = takeLock ();
455
getFile ().delete (lock);