Java Code Examples for org.openide.filesystems.FileObject.canWrite()

The following are Jave code examples for showing how to use canWrite() of the org.openide.filesystems.FileObject class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: incubator-netbeans   File: SharabilityFilter.java   View Source Code Vote up 6 votes
/**
 */
@Override
public boolean searchFile(FileObject file)
        throws IllegalArgumentException {
    if (file.isFolder()) {
        throw new java.lang.IllegalArgumentException(
                "file (not folder) expected");                      //NOI18N
    }
    File f = FileUtil.toFile(file);
    if (f == null && !file.canWrite()) {
        // non-standard file objects, e.g. ZIP archive items.
        return true;
    } else {
        return SharabilityQuery.getSharability(file)
                != Sharability.NOT_SHARABLE;
    }
}
 
Example 2
Project: incubator-netbeans   File: SharabilityFilter.java   View Source Code Vote up 6 votes
/**
 */
@Override
public FolderResult traverseFolder(FileObject folder)
        throws IllegalArgumentException {
    if (!folder.isFolder()) {
        throw new java.lang.IllegalArgumentException(
                "folder expected");                                 //NOI18N
    }
    File f = FileUtil.toFile(folder);
    if (f == null && !folder.canWrite()) {
        // non-standard file objects, e.g. ZIP archive items.
        return FolderResult.TRAVERSE;
    } else {
        Sharability sharability = SharabilityQuery.getSharability(folder);
        switch (sharability) {
            case NOT_SHARABLE:
                return FolderResult.DO_NOT_TRAVERSE;
            case SHARABLE:
                return FolderResult.TRAVERSE_ALL_SUBFOLDERS;
            default:
                return FolderResult.TRAVERSE;
        }
    }
}
 
Example 3
Project: incubator-netbeans   File: SharabilityFilter.java   View Source Code Vote up 6 votes
/**
 */
@Override
public int traverseFolder(FileObject folder)
        throws IllegalArgumentException {
    if (!folder.isFolder()) {
        throw new java.lang.IllegalArgumentException(
                "folder expected");                                 //NOI18N
    }
    File f = FileUtil.toFile(folder);
    if (f == null && !folder.canWrite()) {
        // non-standard file objects, e.g. ZIP archive items.
        return TRAVERSE;
    } else {
        final int sharability = SharabilityQuery.getSharability(f);
        switch (sharability) {
            case SharabilityQuery.NOT_SHARABLE:
                return DO_NOT_TRAVERSE;
            case SharabilityQuery.SHARABLE:
                return TRAVERSE_ALL_SUBFOLDERS;
            default:
                return TRAVERSE;
        }
    }
}
 
Example 4
Project: incubator-netbeans   File: SourceGroups.java   View Source Code Vote up 6 votes
/**
 * Checks whether the folder identified by the given <code>packageName</code> is
 * writable or is in a writable parent directory but does not exist yet.
 * 
 * @param sourceGroup the source group of the folder; must not be null.
 * @param packageName the package to check; must not be null.
 * @return true if the folder is writable or can be created (i.e. the parent 
 * folder, or the root folder of the given <code>sourceGroup</code> if there is no other 
 * parent for the folder, is writable), false otherwise.
 */ 
public static boolean isFolderWritable(SourceGroup sourceGroup, String packageName) {
    Parameters.notNull("sourceGroup", sourceGroup); //NOI18N
    Parameters.notNull("packageName", packageName); //NOI18N
    try {
        FileObject fo = getFolderForPackage(sourceGroup, packageName, false);

        while ((fo == null) && (packageName.lastIndexOf('.') != -1)) {
            packageName = packageName.substring(0, packageName.lastIndexOf('.'));
            fo = getFolderForPackage(sourceGroup, packageName, false);
        }
        return fo == null ? sourceGroup.getRootFolder().canWrite() : fo.canWrite();
    } catch (IOException ex) {
        LOGGER.log(Level.INFO, null, ex);
        return false;
    }
}
 
Example 5
Project: incubator-netbeans   File: SharabilityFilter.java   View Source Code Vote up 6 votes
/**
 */
@Override
public boolean searchFile(FileObject file)
        throws IllegalArgumentException {
    if (file.isFolder()) {
        throw new java.lang.IllegalArgumentException(
                "file (not folder) expected");                      //NOI18N
    }
    File f = FileUtil.toFile(file);
    if (f == null && !file.canWrite()) {
        // non-standard file objects, e.g. ZIP archive items.
        return true;
    } else {
        return SharabilityQuery.getSharability(FileUtil.toFile(file))
                != SharabilityQuery.NOT_SHARABLE;
    }
}
 
Example 6
Project: incubator-netbeans   File: ReadOnlyFilesHighlighting.java   View Source Code Vote up 5 votes
private void checkFileStatus(FileObject fo) {
    boolean update = false;
    synchronized (this) {
        if (lastFile == null || lastFile.get() != fo) {
            lastFile = new WeakReference<FileObject>(fo);
            update = true;
        }
    }
    if (update) {
        fo.addFileChangeListener(WeakListeners.create(FileChangeListener.class, this, fo));
        boolean readOnly = !fo.canWrite(); // Access without monitor on this class and document's lock
        updateFileReadOnly(readOnly);
    }
}
 
Example 7
Project: incubator-netbeans   File: AXIModel.java   View Source Code Vote up 5 votes
/**
 * Returns true if the underlying document is read-only, false otherwise.
 */
public boolean isReadOnly() {
    ModelSource ms = getModelSource();
    assert(ms != null);
    if (ms.isEditable()) {
        FileObject fo = (FileObject) ms.getLookup().lookup(FileObject.class);
        assert(fo != null);
        return !fo.canWrite();
    }
    return true;
}
 
Example 8
Project: incubator-netbeans   File: DiffStreamSource.java   View Source Code Vote up 5 votes
private boolean isBaseFileWritable () {
    if (canWriteBaseFile == null) {
        FileObject fo = FileUtil.toFileObject(baseFile);
        canWriteBaseFile = fo != null && fo.canWrite();
    }
    return canWriteBaseFile;
}
 
Example 9
Project: incubator-netbeans   File: DiffStreamSource.java   View Source Code Vote up 5 votes
private boolean isBaseFileWritable () {
    if (canWriteBaseFile == null) {
        FileObject fo = FileUtil.toFileObject(baseFile);
        canWriteBaseFile = fo != null && fo.canWrite();
    }
    return canWriteBaseFile;
}
 
Example 10
Project: incubator-netbeans   File: Utilities.java   View Source Code Vote up 5 votes
public static Set<Severity> disableErrors(FileObject file) {
    if (file.getAttribute(DISABLE_ERRORS) != null) {
        return EnumSet.allOf(Severity.class);
    }
    if (!file.canWrite() && FileUtil.getArchiveFile(file) != null) {
        return EnumSet.allOf(Severity.class);
    }

    return EnumSet.noneOf(Severity.class);
}
 
Example 11
Project: incubator-netbeans   File: DiffStreamSource.java   View Source Code Vote up 5 votes
private boolean isBaseFileWritable () {
    if (canWriteBaseFile == null) {
        FileObject fo = FileUtil.toFileObject(baseFile);
        canWriteBaseFile = fo != null && fo.canWrite();
    }
    return canWriteBaseFile;
}
 
Example 12
Project: incubator-netbeans   File: ProvidedExtensionsTest.java   View Source Code Vote up 5 votes
public void testImplsCanWrite() throws IOException {
    FileObject fo = FileUtil.toFileObject(getWorkDir());
    assertNotNull(fo);
    assertNotNull(iListener);
    assertNotNull(iListenerNoCanWrite);
    int nCalls = iListener.implsCanWriteCalls;
    FileObject toChange = fo.createData("cw");
    assertNotNull(toChange);
    boolean cw = toChange.canWrite();
    assertEquals(nCalls + 1, iListener.implsCanWriteCalls);
    assertEquals(0, iListenerNoCanWrite.implsCanWriteCalls);
}
 
Example 13
Project: incubator-netbeans   File: FixDescription.java   View Source Code Vote up 5 votes
public void implement() throws Exception {
    final FileObject file = err.getFile();
    if (!file.canWrite()) {
        NotifyDescriptor d = new NotifyDescriptor.Message(
                NbBundle.getMessage(FixDescription.class, "CTL_File_Not_Writable", file.getNameExt()), //NOI18N
                NotifyDescriptor.ERROR_MESSAGE);
        DialogDisplayer.getDefault().notify(d);
        return ;
    }

    fix.implement();
    fixed.set(true);
    cs.fireChange();
}
 
Example 14
Project: incubator-netbeans   File: Utilities.java   View Source Code Vote up 5 votes
/**
    * Detects if targets file is non-null and writable
    * @return true if target's file is writable
    */
   public static boolean isTargetWritable(@NonNull TypeElement target, @NonNull CompilationInfo info) {
       TypeElement outermostType = info.getElementUtilities().outermostTypeElement(target);
       FileObject fo = SourceUtils.getFile(ElementHandle.create(outermostType), info.getClasspathInfo());
if(fo != null && fo.canWrite())
    return true;
else
    return false;
   }
 
Example 15
Project: incubator-netbeans   File: SQLCloneableEditor.java   View Source Code Vote up 5 votes
@Messages({
     "MSG_SaveModified=File {0} is modified. Save?"
 })
 @Override
 public CloseOperationState canCloseElement() {
     if (sqlEditorSupport().isConsole()) {
         return CloseOperationState.STATE_OK;
     } else {
         DataObject sqlDO = sqlEditorSupport().getDataObject();
         FileObject sqlFO = sqlEditorSupport().getDataObject().getPrimaryFile();
         if (sqlDO.isModified()) {
             if (sqlFO.canWrite()) {
                 Savable sav = sqlDO.getLookup().lookup(Savable.class);
                 if (sav != null) {
                     AbstractAction save = new AbstractAction() {
                         @Override
                         public void actionPerformed(ActionEvent e) {
                             try {
                                 sqlEditorSupport().saveDocument();
                             } catch (IOException ex) {
                                 Exceptions.printStackTrace(ex);
                             }
                         }
                     };
                     save.putValue(Action.LONG_DESCRIPTION, Bundle.MSG_SaveModified(sqlFO.getNameExt()));
                     return MultiViewFactory.createUnsafeCloseState("editor", save, null);
                 }
             }
         }
     }
     return CloseOperationState.STATE_OK;
}
 
Example 16
Project: incubator-netbeans   File: FormDataObject.java   View Source Code Vote up 4 votes
public boolean isReadOnly() {
    FileObject javaFO = getPrimaryFile();
    FileObject formFO = formEntry.getFile();
    return !javaFO.canWrite() || !formFO.canWrite();
}
 
Example 17
Project: incubator-netbeans   File: JAXBRefreshAction.java   View Source Code Vote up 4 votes
protected void performAction(Node[] nodes) {
    Node node = nodes[ 0 ];
    FileObject fo = node.getLookup().lookup( FileObject.class );
    Project proj = node.getLookup().lookup(Project.class);
    String origLoc = (String) node.getValue(
            JAXBWizModuleConstants.ORIG_LOCATION);
    Boolean origLocIsURL = (Boolean) node.getValue(
            JAXBWizModuleConstants.ORIG_LOCATION_TYPE);
    FileObject locSchemaRoot = (FileObject) node.getValue(
            JAXBWizModuleConstants.LOC_SCHEMA_ROOT);
    
    if ( ( fo != null ) && ( origLoc != null ) ) {
        // XXX TODO run in separate non-awt thread.
         try {
             if (fo.canWrite()){
                 if (origLocIsURL){
                    URL url = new URL(origLoc);
                     ProjectHelper.retrieveResource(locSchemaRoot, 
                             url.toURI());                        
                 } else {
                     File projDir = FileUtil.toFile(
                             proj.getProjectDirectory());
                     //File srcFile = new File(origLoc);
                     File srcFile = FileSysUtil.Relative2AbsolutePath(
                             projDir, origLoc);
                     ProjectHelper.retrieveResource(fo.getParent(), 
                             srcFile.toURI());
                 }
             } else {
                 String msg = NbBundle.getMessage(this.getClass(),
                         "MSG_CanNotRefreshFile"); //NOI18N
                 NotifyDescriptor d = new NotifyDescriptor.Message(
                         msg, NotifyDescriptor.INFORMATION_MESSAGE);
                 d.setTitle(NbBundle.getMessage(this.getClass(), 
                         "LBL_RefreshFile")); //NOI18N
                 DialogDisplayer.getDefault().notify(d);
             }
         } catch (Exception ex){
             log(ex);
         } 
    }
}
 
Example 18
Project: incubator-netbeans   File: J2SEProject.java   View Source Code Vote up 4 votes
@NonNull
private Runnable newUpdateCopyLibsAction() {
    return new Runnable() {
        private final String LIB_COPY_LIBS = "CopyLibs"; //NOI18N
        private final String PROP_VERSION = "version";   //NOI18N
        private final String VOL_CP = "classpath";       //NOI18N

        @Override
        public void run() {
            final LibraryManager projLibManager = refHelper.getProjectLibraryManager();
            if (projLibManager == null) {
                return;
            }
            final Library globalCopyLibs = LibraryManager.getDefault().getLibrary(LIB_COPY_LIBS);
            final Library projectCopyLibs = projLibManager.getLibrary(LIB_COPY_LIBS);
            if (globalCopyLibs == null || projectCopyLibs == null) {
                return;
            }
            final String globalStr = globalCopyLibs.getProperties().get(PROP_VERSION);
            if (globalStr == null) {
                return;
            }
            try {
                final SpecificationVersion globalVersion = new SpecificationVersion(globalStr);
                final String projectStr = projectCopyLibs.getProperties().get(PROP_VERSION);
                if (projectStr != null && globalVersion.compareTo(new SpecificationVersion(projectStr)) <= 0) {
                    return;
                }

                final List<URL> content = projectCopyLibs.getContent(VOL_CP);
                projLibManager.removeLibrary(projectCopyLibs);
                final FileObject projLibLoc = URLMapper.findFileObject(projLibManager.getLocation());
                if (projLibLoc != null) {
                    final FileObject libFolder = projLibLoc.getParent();
                    boolean canDelete = libFolder.canWrite();
                    FileObject container = null;
                    for (URL u : content) {
                        FileObject fo = toFile(u);
                        if (fo != null) {
                            canDelete &= fo.canWrite();
                            if (container == null) {
                                container = fo.getParent();
                                canDelete &= container.canWrite();
                                canDelete &= LIB_COPY_LIBS.equals(container.getName());
                                canDelete &= libFolder.equals(container.getParent());
                            } else {
                                canDelete &= container.equals(fo.getParent());
                            }
                        }
                    }
                    if (canDelete && container != null) {
                        container.delete();
                    }
                }
                refHelper.copyLibrary(globalCopyLibs);

            } catch (IllegalArgumentException iae) {
                LOG.log(
                        Level.WARNING,
                        "Cannot update {0} due to invalid version.", //NOI18N
                        projectCopyLibs.getDisplayName());
            } catch (IOException ioe) {
                Exceptions.printStackTrace(ioe);
            }
        }

        @CheckForNull
        private FileObject toFile(@NonNull final URL url) {
            final URL file = FileUtil.getArchiveFile(url);
            return URLMapper.findFileObject(file != null ? file : url);
        }
    };
}
 
Example 19
Project: incubator-netbeans   File: FormEditorSupport.java   View Source Code Vote up 4 votes
@Override
protected boolean notifyModified () {
    boolean alreadyModified = isModified();
    boolean retVal = super.notifyModified();
    
    if (retVal) { // java source modification
        addSaveCookie();
    }
    
    if (!alreadyModified) {
        FileObject formFile = formDataObject.getFormFile();
        if (!formFile.canWrite()) { // Issue 74092
            FileLock lock = null;
            try {
                lock = formFile.lock();
            } catch (UserQuestionException uqex) {
                NotifyDescriptor nd = new NotifyDescriptor.Confirmation(
                        uqex.getLocalizedMessage(),
                        FormUtils.getBundleString("TITLE_UserQuestion"), // NOI18N
                        NotifyDescriptor.YES_NO_OPTION);
                DialogDisplayer.getDefault().notify(nd);
                if (NotifyDescriptor.YES_OPTION.equals(nd.getValue())) {
                    try {
                        uqex.confirmed();
                        EventQueue.invokeLater(new Runnable() {
                            @Override
                            public void run()  {
                                reloadForm();
                            }
                        });
                    } catch (IOException ioex) {
                        ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioex);
                    }
                }
            } catch (IOException ex) {
                ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
            } finally {
                if (lock != null) {
                    lock.releaseLock();
                }
            }
        }
        updateMVTCDisplayName();
    }
    return retVal;
}
 
Example 20
Project: incubator-netbeans   File: JavaRefactoringUtils.java   View Source Code Vote up 2 votes
/**
 * returns true if file's mime type is text/x-java and file is on know source path
 * @param file 
 * @return 
 */
@SuppressWarnings("deprecation")
public static boolean isRefactorable(FileObject file) {
    return RefactoringUtils.isRefactorable(file) && file.canWrite() && file.canRead();
}