Java Code Examples for org.openide.filesystems.FileObject#isLocked()

The following examples show how to use org.openide.filesystems.FileObject#isLocked() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: jeddict   File: FileUtil.java    License: Apache License 2.0 6 votes vote down vote up
public static void expandTemplate(InputStream template, FileObject toFile, Map<String, Object> values) throws IOException {
    Charset targetEncoding = FileEncodingQuery.getEncoding(toFile);
    if (toFile.isLocked()) {
        LOG.log(Level.SEVERE, "File {0} is locked", new Object[]{toFile.getName()});
        return;
    }
    FileLock lock = toFile.lock();
    try (Writer writer = new OutputStreamWriter(toFile.getOutputStream(lock), targetEncoding)) {
        expandTemplate(new InputStreamReader(template), writer, values, targetEncoding);
    } finally {
        lock.releaseLock();
    }
    DataObject dob = DataObject.find(toFile);
    if (dob != null) {
        reformat(dob);
    }
}
 
Example 2
public static void writeWithFilesystemAPI(FileInfo info, List<String> lines)
        throws FileAccessException {
  FileLock lock = FileLock.NONE;
  FileObject fo = info.getFileObject();

  // write file
  try {
    lock = fo.lock();
    if (fo.isLocked()) {
      Files.write(Paths.get(fo.toURI()), lines, info.getCharset());
      fo.setAttribute(ENCODING_SETTING, info.getCharset().name());
    }
  } catch (IOException ex) {
    throw new FileAccessException("Document could not be written: " + ex.getMessage());
  } finally {
    lock.releaseLock();
  }
}
 
Example 3
Source Project: netbeans   File: BackupFacility2.java    License: Apache License 2.0 4 votes vote down vote up
private boolean tryUndoOrRedo(@NonNull final FileObject fileObj, @NonNull final BackupEntry entry) throws DataObjectNotFoundException {
    DataObject dob = DataObject.find(fileObj);
    if (dob != null) {
        CloneableEditorSupport ces = dob.getLookup().lookup(CloneableEditorSupport.class);
        if(ces == null) {
            return false;
        }
        final org.openide.awt.UndoRedo.Manager manager;
        try {
            manager = (org.openide.awt.UndoRedo.Manager) undoRedo.get(ces);
            final BaseDocument doc = (BaseDocument) ces.getDocument();
            if (doc==null) {
                return false;
            }
            if (doc.isAtomicLock() || fileObj.isLocked()) {
                //undo already performed
                if (entry.isUndo()) {
                    entry.setUndo(false);
                } else {
                    entry.setUndo(true);
                }
            } else {
                if ((entry.isUndo() && manager.canUndo()) || (!entry.isUndo() && manager.canRedo())) {
                    doc.runAtomic(new Runnable() {

                        @Override
                        public void run() {
                            if (entry.isUndo()) {
                                manager.undo();
                                entry.setUndo(false);
                            } else {
                                manager.redo();
                                entry.setUndo(true);
                            }
                        }
                    });
                } else {
                    return false;
                }
            }
            return true;
        } catch (IllegalArgumentException | IllegalAccessException ex) {
            Exceptions.printStackTrace(ex);
        }
    }
    return false;
}