Java Code Examples for org.openide.loaders.DataObject#isModified()

The following examples show how to use org.openide.loaders.DataObject#isModified() . 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 check out the related API usage on the sidebar.
Example 1
Source File: RemoveOperationAction.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void removeOperation(Set<MethodModel> methods) throws IOException {
    for(MethodModel method:methods) {
        String methodName = method.getOperationName();
        FileObject implementationClass = getImplementationClass(method);

        //WS from Java
        MethodGenerator.deleteMethod(implementationClass, methodName);
        //save the changes so events will be fired

        DataObject dobj = DataObject.find(implementationClass);
        if(dobj.isModified()) {
            SaveCookie cookie = dobj.getCookie(SaveCookie.class);
            if(cookie!=null) cookie.save();
        }
    }
}
 
Example 2
Source File: CatalogModelImpl.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private Reader getFileStreamFromDocument(File resultFile) {
    FileObject fo = FileUtil.toFileObject(FileUtil.normalizeFile(resultFile));
    if(fo != null){
        DataObject dobj = null;
        try {
            dobj = DataObject.find(fo);
        } catch (DataObjectNotFoundException ex) {
            return null;
        }
        if(dobj.isValid() && dobj.isModified()){
            // DataObjectAdapters does not implement getByteStream
            // so calling this here will effectively return null
            return DataObjectAdapters.inputSource(dobj).getCharacterStream();
        } else{
            //return null so that the validator will use normal file path to access doc
            return null;
        }
    }
    return null;
}
 
Example 3
Source File: AntProjectSupport.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Utility method to get a properly configured XML input source for a script.
 */
public static InputSource createInputSource(final FileObject fo, final StyledDocument document) throws IOException {
    if (fo != null) {
        DataObject d = DataObject.find(fo);
        if (!d.isModified()) {
            // #58194: no need to parse the live document.
            InputSource s = new InputSource();
            s.setSystemId(fo.toURL().toExternalForm());
            s.setByteStream(fo.getInputStream());
            return s;
        }
    }
    final String[] contents = new String[1];
    document.render(new Runnable() {
        public void run() {
            try {
                contents[0] = document.getText(0, document.getLength());
            } catch (BadLocationException e) {
                throw new AssertionError(e);
            }
        }
    });
    InputSource in = new InputSource(new StringReader(contents[0]));
    if (fo != null) { // #10348
        in.setSystemId(fo.toURL().toExternalForm());

        // [PENDING] Ant's ProjectHelper has an elaborate set of work-
        // arounds for inconsistent parser behavior, e.g. file:foo.xml
        // works in Ant but not with Xerces parser. You must use just foo.xml
        // as the system ID. If necessary, Ant's algorithm could be copied
        // here to make the behavior match perfectly, but it ought not be necessary.
    }
    return in;
}
 
Example 4
Source File: SaveableSectionInnerPanel.java    From netbeans with Apache License 2.0 5 votes vote down vote up
protected void setModelDirty(WSDLModel model) {
    try {
        ModelSource ms = model.getModelSource();
        FileObject fo = (FileObject) ms.getLookup().lookup(FileObject.class);
        DataObject wsdlDO = DataObject.find(fo);
        if (!wsdlDO.isModified()) {
            wsdlDO.setModified(true);
        }
    } catch (Exception e) {
        ErrorManager.getDefault().notify(e);
    }
}
 
Example 5
Source File: WSITModelSupport.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static void save(WSDLModel model, Set<FileObject> traversedModels) {
    if (model == null) {
        logger.log(Level.INFO, "Model cannot be null.");
        return;
    }
    FileObject modelFO = Util.getFOForModel(model);
    if (modelFO == null) {
        logger.log(Level.INFO, "Cannot find fileobject in lookup for: " + model.getModelSource());
    }
    // avoid neverending recursion for recursive imports
    if (traversedModels.contains(modelFO)) {
        return;
    }
    traversedModels.add(modelFO);
    try {
        Definitions defs = model.getDefinitions();
        if (defs != null) {
            Collection<Import> imports = defs.getImports();
            for (Import i : imports) {
                WSDLModel importedModel = i.getImportedWSDLModel();
                save(importedModel, traversedModels);
            }

            DataObject wsdlDO = DataObject.find(modelFO);
            if ((wsdlDO != null) && (wsdlDO.isModified())) {
                SaveCookie wsdlSaveCookie = wsdlDO.getCookie(SaveCookie.class);
                if(wsdlSaveCookie != null){
                    wsdlSaveCookie.save();
                }
                wsdlDO.setModified(false);
            }
        }
    } catch (Exception e) {
        logger.log(Level.SEVERE, null, e);
    }
}
 
Example 6
Source File: Group.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static void closeDocuments(Set<DataObject> toCloseDocuments) {
    for (DataObject dobj : toCloseDocuments) {
        if (!dobj.isModified()) {
            //the modified files would force user to decide about saving..
            CloseCookie cook = dobj.getLookup().lookup(CloseCookie.class);
            if (cook != null) {
                cook.close();
            }
        }
    }   
}
 
Example 7
Source File: ExitDialog.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static Set<DataObject> getModifiedFiles (Set<DataObject> openedFiles) {
       Set<DataObject> set = new HashSet<DataObject> (openedFiles.size ());
for (DataObject obj: openedFiles) {
           if (obj.isModified ()) {
               set.add (obj);
           }
       }
       return set;
   }
 
Example 8
Source File: PropertiesEditorSupport.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** 
 * Overrides superclass method.
 * Should test whether all data is saved, and if not, prompt the user
 * to save. Called by my topcomponent when it wants to close its last topcomponent, but the table editor may still be open
 * @return <code>true</code> if everything can be closed
 */
@Override
protected boolean canClose () {
    // if the table is open, can close without worries, don't remove the save cookie
    if (hasOpenedTableComponent()){
        return true;
    }else{
        DataObject propDO = myEntry.getDataObject();
        if ((propDO == null) || !propDO.isModified()) {
            return true;
        }
        return super.canClose();
    }
}
 
Example 9
Source File: SQLCloneableEditor.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@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 10
Source File: CoverageSideBar.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void setCoverage(FileCoverageDetails details) {
    if (details != null) {
        FileCoverageSummary summary = details.getSummary();
        float coverage = summary.getCoveragePercentage();

        if (coverage >= 0.0) {
            coverageBar.setCoveragePercentage(coverage);
        }
        //coverageBar.setStats(summary.getLineCount(), summary.getExecutedLineCount(),
        //        summary.getInferredCount(), summary.getPartialCount());

        long dataModified = details.lastUpdated();
        FileObject fo = details.getFile();
        boolean tooOld = false;
        if (fo != null && dataModified > 0 && dataModified < fo.lastModified().getTime()) {
            tooOld = true;
        } else if (fo != null && fo.isValid()) {
            try {
                DataObject dobj = DataObject.find(fo);
                tooOld = dobj.isModified();
            } catch (DataObjectNotFoundException ex) {
                Exceptions.printStackTrace(ex);
            }
        }
        if (tooOld) {
            warningsLabel.setText(NbBundle.getMessage(CoverageSideBar.class, "DataTooOld"));
        } else {
            warningsLabel.setText("");
        }
    } else {
        coverageBar.setCoveragePercentage(0.0f);
        warningsLabel.setText("");
    }
}
 
Example 11
Source File: TransformPerformer.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** If the Data Object is modified, then is saved.
 *  Fix for issue #61608
 */
private void saveBeforeTransformation  (DataObject dObject){
    if (dObject.isModified()){
        SaveCookie save;
        save = dObject.getCookie(SaveCookie.class);
        if (save != null) {
            try {
                save.save();
            } catch (IOException ex) {
                ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
            }
        }
    }
}
 
Example 12
Source File: AbstractStatefulGLToolAction.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
protected void setModified(final AbstractSceneExplorerNode rootNode, final DataObject dataObject) {
    if (dataObject.isModified())
        return;
    java.awt.EventQueue.invokeLater(new Runnable() {

        public void run() {
            dataObject.setModified(true);
            rootNode.refresh(true);
        }
    });
}
 
Example 13
Source File: DatasourceSupport.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**
 * Perform datasources graph changes defined by the jbossWeb modifier.
 * Update editor content and save changes, if appropriate.
 *
 * @param modifier
 */
private WildflyDatasource modifyDSResource(DSResourceModifier modifier)
        throws ConfigurationException, DatasourceAlreadyExistsException {

    WildflyDatasource ds = null;

    try {
        ensureResourceDirExists();
        ensureDatasourcesFileExists();

        DataObject datasourcesDO = DataObject.find(datasourcesFO);

        EditorCookie editor = (EditorCookie) datasourcesDO.getCookie(EditorCookie.class);
        StyledDocument doc = editor.getDocument();
        if (doc == null) {
            doc = editor.openDocument();
        }

        Datasources newDatasources = null;
        try {  // get the up-to-date model
            // try to create a graph from the editor content
            byte[] docString = doc.getText(0, doc.getLength()).getBytes();
            newDatasources = Datasources.createGraph(new ByteArrayInputStream(docString));
        } catch (RuntimeException e) {
            Datasources oldDatasources = getDatasourcesGraph(true);
            if (oldDatasources == null) {
                // neither the old graph is parseable, there is not much we can do here
                // TODO: should we notify the user?
                throw new ConfigurationException(
                        NbBundle.getMessage(DatasourceSupport.class, "MSG_datasourcesXmlCannotParse", DS_RESOURCE_NAME)); // NOI18N
            }
            // current editor content is not parseable, ask whether to override or not
            NotifyDescriptor notDesc = new NotifyDescriptor.Confirmation(
                    NbBundle.getMessage(DatasourceSupport.class, "MSG_datasourcesXmlNotValid", DS_RESOURCE_NAME), // NOI18N
                    NotifyDescriptor.OK_CANCEL_OPTION);
            Object result = DialogDisplayer.getDefault().notify(notDesc);
            if (result == NotifyDescriptor.CANCEL_OPTION) {
                // keep the old content
                return null;
            }
            // use the old graph
            newDatasources = oldDatasources;
        }

        // perform changes
        ds = modifier.modify(newDatasources);

        // save if appropriate
        boolean modified = datasourcesDO.isModified();
        ResourceConfigurationHelper.replaceDocument(doc, newDatasources);
        if (!modified) {
            SaveCookie cookie = (SaveCookie) datasourcesDO.getCookie(SaveCookie.class);
            cookie.save();
        }

        datasources = newDatasources;

    } catch (DataObjectNotFoundException donfe) {
        Exceptions.printStackTrace(donfe);
    } catch (BadLocationException ble) {
        // this should not occur, just log it if it happens
        Exceptions.printStackTrace(ble);
    } catch (IOException ioe) {
        String msg = NbBundle.getMessage(DatasourceSupport.class, "MSG_CannotUpdateFile", datasourcesFile.getAbsolutePath());
        throw new ConfigurationException(msg, ioe);
    }

    return ds;
}
 
Example 14
Source File: DatasourceSupport.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**
 * Perform datasources graph changes defined by the jbossWeb modifier. Update editor
 * content and save changes, if appropriate.
 *
 * @param modifier
 */
private JBossDatasource modifyDSResource(DSResourceModifier modifier) 
throws ConfigurationException, DatasourceAlreadyExistsException {

    JBossDatasource ds = null;
    
    try {
        ensureResourceDirExists();
        ensureDatasourcesFileExists();
        
        DataObject datasourcesDO = DataObject.find(datasourcesFO);

        EditorCookie editor = (EditorCookie)datasourcesDO.getCookie(EditorCookie.class);
        StyledDocument doc = editor.getDocument();
        if (doc == null)
            doc = editor.openDocument();

        Datasources newDatasources = null;
        try {  // get the up-to-date model
            // try to create a graph from the editor content
            byte[] docString = doc.getText(0, doc.getLength()).getBytes();
            newDatasources = Datasources.createGraph(new ByteArrayInputStream(docString));
        } catch (RuntimeException e) {
            Datasources oldDatasources = getDatasourcesGraph(true);
            if (oldDatasources == null) {
                // neither the old graph is parseable, there is not much we can do here
                // TODO: should we notify the user?
                throw new ConfigurationException(
                        NbBundle.getMessage(DatasourceSupport.class, "MSG_datasourcesXmlCannotParse", DS_RESOURCE_NAME)); // NOI18N
            }
            // current editor content is not parseable, ask whether to override or not
            NotifyDescriptor notDesc = new NotifyDescriptor.Confirmation(
                    NbBundle.getMessage(DatasourceSupport.class, "MSG_datasourcesXmlNotValid", DS_RESOURCE_NAME),       // NOI18N
                    NotifyDescriptor.OK_CANCEL_OPTION);
            Object result = DialogDisplayer.getDefault().notify(notDesc);
            if (result == NotifyDescriptor.CANCEL_OPTION) {
                // keep the old content
                return null;
            }
            // use the old graph
            newDatasources = oldDatasources;
        }

        // perform changes
        ds = modifier.modify(newDatasources);

        // save if appropriate
        boolean modified = datasourcesDO.isModified();
        ResourceConfigurationHelper.replaceDocument(doc, newDatasources);
        if (!modified) {
            SaveCookie cookie = (SaveCookie)datasourcesDO.getCookie(SaveCookie.class);
            cookie.save();
        }

        datasources = newDatasources;

    } catch(DataObjectNotFoundException donfe) {
        Exceptions.printStackTrace(donfe);
    } catch (BadLocationException ble) {
        // this should not occur, just log it if it happens
        Exceptions.printStackTrace(ble);
    } catch (IOException ioe) {
        String msg = NbBundle.getMessage(DatasourceSupport.class, "MSG_CannotUpdateFile", datasourcesFile.getAbsolutePath());
        throw new ConfigurationException(msg, ioe);
    }
    
    return ds;
}
 
Example 15
Source File: SelectFileDialog.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**
 * Get file object that have user selected
 * @throws IOException if cancelled or invalid data entered
 */
public FileObject getFileObject () throws IOException {
    FileObject newFO = null;

    while ( newFO == null ) {
        DialogDisplayer.getDefault().notify(selectDD);
        if (selectDD.getValue() != NotifyDescriptor.OK_OPTION) {
            throw new UserCancelException();
        }
        final String newName = selectDD.getInputText();

        newFO = folder.getFileObject (newName, ext);
    
        if ( ( newFO == null ) ||
             ( newFO.isVirtual() == true ) ) {

            FileSystem fs = folder.getFileSystem();
            final FileObject tempFile = newFO;
            
            fs.runAtomicAction (new FileSystem.AtomicAction () {
                    public void run () throws IOException {

                        if ( ( tempFile != null ) &&
                             tempFile.isVirtual() ) {
                            tempFile.delete();
                        }

                        try {
                            folder.createData (newName, ext);                                
                        } catch (IOException exc) {
                            NotifyDescriptor desc = new NotifyDescriptor.Message
                                (NbBundle.getMessage(SelectFileDialog.class, "MSG_cannot_create_data", newName + "." + ext), NotifyDescriptor.WARNING_MESSAGE);
                            DialogDisplayer.getDefault().notify (desc);

                            //if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug (exc);
                        }
                    }
                });
            
            newFO = folder.getFileObject (newName, ext);

        } else if (newFO != null) {
            DataObject data = DataObject.find(newFO);
            if (data.isModified() || data.isValid() == false) {
                NotifyDescriptor message = new NotifyDescriptor.Message(NbBundle.getMessage(SelectFileDialog.class, "BK0001"), NotifyDescriptor.WARNING_MESSAGE);
                DialogDisplayer.getDefault().notify(message);
                throw new UserCancelException();
            } else if (! GuiUtil.confirmAction (NbBundle.getMessage(SelectFileDialog.class, "PROP_replaceMsg",
                                                            newName, ext ) )) {
                throw new UserCancelException();
            }
        }
    } // while

    return newFO;
}