org.openide.cookies.SaveCookie Java Examples

The following examples show how to use org.openide.cookies.SaveCookie. 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: EjbModuleTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testRenameDisplayName() throws Exception {
    String displayName = "testDisplayName";
    assertNotNull("DDObject not found", ddObj);

    ddObj.showElement(ddObj.getEjbJar()); //open visual editor
    Utils.waitForAWTDispatchThread();
    getDetailPanel().getDisplayNameTextField().setText(displayName);
    // check DD beans
    assertEquals("DD bean isn't updated", displayName, DDProvider.getDefault().getDDRoot(ddFo).getDisplayName(null));
    Thread.sleep(4000);
    checkinXML("<display-name>" + displayName + "</display-name>");

    //save ejb-jar.xml editor
    SaveCookie saveCookie = (SaveCookie) ddObj.getCookie(SaveCookie.class);
    assertNotNull("Save cookie is null, Data object isn't changed!", saveCookie);
    if (saveCookie != null) {
        saveCookie.save();
    }
}
 
Example #2
Source File: BaseJspEditorSupport.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
protected boolean notifyModified() {
    boolean notify = super.notifyModified();
    if (!notify) {
        return false;
    }
    JspDataObject obj = (JspDataObject) getDataObject();
    if (obj.getCookie(SaveCookie.class) == null) {
        obj.addSaveCookie(new SaveCookie() {

            public void save() throws java.io.IOException {
                try {
                    saveDocument();
                } catch (UserCancelException e) {
                    //just ignore
                }
            }
        });
    }
    return true;
}
 
Example #3
Source File: CreatedModifiedFiles.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public void run() throws IOException {
    //#65420 it can happen the manifest is currently being edited. save it
    // and cross fingers because it can be in inconsistent state
    try {
        DataObject dobj = DataObject.find(mfFO);
        SaveCookie safe = dobj.getLookup().lookup(SaveCookie.class);
        if (safe != null) {
            safe.save();
        }
    } catch (DataObjectNotFoundException ex) {
        Util.err.notify(ErrorManager.WARNING, ex);
    }

    EditableManifest em = Util.loadManifest(mfFO);
    em.addSection(dataLoaderClass);
    em.setAttribute("OpenIDE-Module-Class", "Loader", dataLoaderClass); // NOI18N
    if (installBefore != null) {
        em.setAttribute("Install-Before", installBefore, dataLoaderClass); //NOI18N
    }
    Util.storeManifest(mfFO, em);
}
 
Example #4
Source File: PropertiesOpen.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Helper method. Should be called only if the object has SaveCookie
 * @return true if closing this editor whithout saving would result in loss of data
 *  because al least one of the modified files is not open in the code editor
 */
private boolean shouldAskSave() {
    // for each entry : if there is a SaveCookie and no open editor component, return true.
    // if passed for all entries, return false
    BundleStructure structure = bundleStructure;
    PropertiesFileEntry entry;
    SaveCookie savec;
    for (int i = 0; i < structure.getEntryCount(); i++) {
        entry = structure.getNthEntry(i);
        savec = entry.getCookie(SaveCookie.class);
        if ((savec != null) && !entry.getPropertiesEditor().hasOpenedEditorComponent()) {
            return true;
        }
    }
    return false;
}
 
Example #5
Source File: XmlMultiViewDataObject.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Set whether the object is considered modified.
 * Also fires a change event.
 * If the new value is <code>true</code>, the data object is added into a {@link #getRegistry registry} of opened data objects.
 * If the new value is <code>false</code>,
 * the data object is removed from the registry.
 */
public void setModified(boolean modif) {
    super.setModified(modif);
    //getEditorSupport().updateDisplayName();
    if (isModified()) {
        // Add save cookie
        if (getCookie(SaveCookie.class) == null) {
            getCookieSet().add(saveCookie);
        }
    } else {
        // Remove save cookie
        if(saveCookie.equals(getCookie(SaveCookie.class))) {
            getCookieSet().remove(saveCookie);
        }
        
    }
}
 
Example #6
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 #7
Source File: Utils.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Saves the given document to its underlying {@link FileObject} if the document
 * is not opened in the nb editor, more formally if EditorCookie.getOpenedPanes() == null.
 * 
 * @param document
 * @throws IOException 
 */
public static void saveDocumentIfNotOpened(Document document) throws IOException {

    Object o = document.getProperty(Document.StreamDescriptionProperty);
    if (o == null || !(o instanceof DataObject)) {
        return;
    }
    DataObject dobj = (DataObject) o;
    EditorCookie ec = dobj.getLookup().lookup(EditorCookie.class);
    if (ec != null && ec.getOpenedPanes() == null) {
        //file not open in any editor
        SaveCookie save = dobj.getLookup().lookup(SaveCookie.class);
        if (save != null) {
            save.save();
        }
    }
}
 
Example #8
Source File: CreateRulePanel.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Saves the given document to its underlying {@link FileObject} if the
 * document is not opened in the nb editor, more formally if
 * EditorCookie.getOpenedPanes() == null.
 *
 * @param document
 * @throws IOException
 */
private static void saveDocumentIfNotOpened(Document document) throws IOException {

    Object o = document.getProperty(Document.StreamDescriptionProperty);
    if (o == null || !(o instanceof DataObject)) {
        return;
    }
    DataObject dobj = (DataObject) o;
    EditorCookie ec = dobj.getLookup().lookup(EditorCookie.class);
    if (ec != null && ec.getOpenedPanes() == null) {
        //file not open in any editor
        SaveCookie save = dobj.getLookup().lookup(SaveCookie.class);
        if (save != null) {
            save.save();
        }
    }
}
 
Example #9
Source File: SetModifiedTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testUnmodifyViaSetModified() throws IOException, BadLocationException {
    FileSystem fs = FileUtil.createMemoryFileSystem();
    FileObject fo = fs.getRoot().createData("test.jsp");
    assertNotNull(fo);
    DataObject obj = DataObject.find(fo);

    assertNotNull(obj);
    assertFalse(obj.isModified());
    assertNull(obj.getCookie(SaveCookie.class));

    obj.getCookie(EditorCookie.class).openDocument().insertString(0, "hello", null);
    assertTrue(obj.isModified());
    assertNotNull(obj.getCookie(SaveCookie.class));

    //some QE unit tests needs to silently discard the changed made to the editor document
    obj.setModified(false);

    assertFalse(obj.isModified());
    assertNull(obj.getCookie(SaveCookie.class));
}
 
Example #10
Source File: ExitDialog.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/** Tries to save given data object using its save cookie.
 * Notifies user if excetions appear.
 */
private void save (DataObject dataObject) {
    try {
        SaveCookie sc = dataObject.getLookup().lookup(SaveCookie.class);
        if (sc != null) {
            sc.save();
        }
        listModel.removeElement(dataObject);
    } catch (IOException exc) {
        ErrorManager em = ErrorManager.getDefault();
        Throwable t = em.annotate(
            exc, NbBundle.getBundle(ExitDialog.class).getString("EXC_Save")
        );
        em.notify(ErrorManager.EXCEPTION, t);
    }
}
 
Example #11
Source File: MergeDialogComponent.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void refreshName () {
    Mutex.EVENT.readAccess(new Runnable() {
        @Override
        public void run () {
            String name = org.openide.util.NbBundle.getMessage(MergeDialogComponent.class, "MergeDialogComponent.title");
            Component[] panels;
            synchronized (MergeDialogComponent.this) {
                panels = mergeTabbedPane.getComponents();
            }
            for (int i = 0; i < panels.length; i++) {
                MergePanel panel = (MergePanel) panels[i];
                MergeNode node = nodesForPanels.get(panel);
                if (node.getLookup().lookup(SaveCookie.class) != null) {
                    name = "<html><b>" + name + "*</b></html>"; //NOI18N
                    break;
                }
            }
            setName(name);
        }
    });
}
 
Example #12
Source File: ClientHandlerButtonListener.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static void saveFile(FileObject file) throws IOException {
    DataObject dataObject = DataObject.find(file);
    if (dataObject != null) {
        SaveCookie cookie = dataObject.getCookie(SaveCookie.class);
        if (cookie != null) {
            cookie.save();
        }
    }
}
 
Example #13
Source File: JavaSourceHelper.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public static void saveSource(FileObject[] files) throws IOException {
    for (FileObject f : files) {
        try {
            DataObject dobj = DataObject.find(f);
            SaveCookie sc = dobj.getCookie(SaveCookie.class);
            if (sc != null) {
                sc.save();
            }
        } catch (DataObjectNotFoundException dex) {
            // something really wrong but continue trying to save others
        }
    }
}
 
Example #14
Source File: Utils.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public static void showMethod(FileObject source, String methodName) {
    try {
        DataObject dataObj = DataObject.find(source);          
        JavaSource javaSource = JavaSource.forFileObject(source);
        
        // Force a save to make sure to make sure the line position in
        // the editor is in sync with the java source.
        SaveCookie sc = (SaveCookie) dataObj.getCookie(SaveCookie.class);
 
        if (sc != null) {
            sc.save();
        }
        
        LineCookie lc = (LineCookie) dataObj.getCookie(LineCookie.class);
        
        if (lc != null) {
            final long[] position = JavaSourceHelper.getPosition(javaSource, methodName);
            final Line line = lc.getLineSet().getOriginal((int) position[0]);
            
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    line.show(ShowOpenType.OPEN, ShowVisibilityType.NONE, (int) position[1]);
                }
            });
        }
    } catch (Exception de) {
        Exceptions.printStackTrace(de);
    }    
}
 
Example #15
Source File: SaveBeforeClosingDiffConfirmation.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public static boolean allSaved(SaveCookie[] saveCookies) {
    SaveBeforeClosingDiffConfirmation confirmation
            = new SaveBeforeClosingDiffConfirmation(saveCookies);
    Object selectedOption = confirmation.displayDialog();
    return (selectedOption == confirmation.btnSaveAll)
            || (selectedOption == confirmation.btnKeepModifications);
}
 
Example #16
Source File: PropertiesOpen.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private boolean isModified() {
    PropertiesFileEntry entry;
    for (int i=0;i<bundleStructure.getEntryCount();i++) {
        entry = bundleStructure.getNthEntry(i);
        if(entry != null && entry.getDataObject().getLookup().lookup(SaveCookie.class) != null) {
            return true;
        }
    }
    return false;
}
 
Example #17
Source File: DDEditorTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testFinalSave() throws IOException {
    initDataObject();
    SaveCookie cookie = (SaveCookie) dObj.getCookie(SaveCookie.class);
    if (cookie != null) {
        cookie.save();
    }
}
 
Example #18
Source File: TomcatInstallUtil.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public static void updateDocument(DataObject dobj, org.w3c.dom.Document doc)
    throws javax.swing.text.BadLocationException, java.io.IOException {
    org.openide.cookies.EditorCookie editor = (EditorCookie)dobj.getCookie(EditorCookie.class);
    javax.swing.text.Document textDoc = editor.getDocument();
    if (textDoc == null) {
        textDoc = editor.openDocument();
    }
    TomcatInstallUtil.updateDocument(textDoc,TomcatInstallUtil.getDocumentText(doc),"<Server"); //NOI18N
    SaveCookie savec = (SaveCookie) dobj.getCookie(SaveCookie.class);
    if (savec!=null) savec.save();
}
 
Example #19
Source File: TLDEditorSupport.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** Helper method. Removes save cookie from the data object. */
private void removeSaveCookie() {
    TLDDataObject obj = (TLDDataObject)getDataObject();
    
    // Remove save cookie from the data object.
    Cookie cookie = obj.getCookie(SaveCookie.class);

    if(cookie != null && cookie.equals(saveCookie)) {
        obj.getCookieSet0().remove(saveCookie);
        obj.setModified(false);
    }
}
 
Example #20
Source File: EjbModuleTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testAddSmallIcon() throws Exception {
    String smallIcon = "/tmp/test/small";
    getDetailPanel().getSmallIconTextField().setText(smallIcon);
    // check DD beans
    assertEquals("DD bean isn't updated", smallIcon, DDProvider.getDefault().getDDRoot(ddFo).getSmallIcon());

    checkinXML("<small-icon>" + smallIcon + "</small-icon>");

    //save ejb-jar.xml editor
    SaveCookie saveCookie = (SaveCookie) ddObj.getCookie(SaveCookie.class);
    assertNotNull("Save cookie is null, Data object isn't changed!", saveCookie);
    if (saveCookie != null) {
        saveCookie.save();
    }
}
 
Example #21
Source File: PropertiesOpen.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** Removes save cookie from the dataobject. */
@Deprecated
private void removeSaveCookie() {
    if(propDataObject.getCookie(SaveCookie.class) == this) {
        propDataObject.getCookieSet0().remove(this);
    }
}
 
Example #22
Source File: XmlMultiViewEditorTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
     * Used for running test from inside the IDE by internal execution.
     *
     * @param args the command line arguments
     */
//    public static void main(String[] args) {
//        TestRunner.run(new NbTestSuite(XmlMultiViewEditorTest.class));
//    }
    

    public void testSetModifiedNestedChange() throws Exception {
        File f = Helper.getBookFile(getDataDir(), getWorkDir());
        FileObject fo = FileUtil.toFileObject(f);
        assertNotNull(fo);

        doSetPreferredLoader(fo, loader);
        final DataObject dob = DataObject.find(fo);

        assertTrue("The right object", dob instanceof XmlMultiViewDataObject);
        dob.getLookup().lookup(EditorCookie.class).openDocument().insertString(0,
                "modified", null);
        assertTrue("Should be modified.", dob.isModified());
        dob.addPropertyChangeListener(new PropertyChangeListener() {
            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                String s = evt.getPropertyName();
                if (DataObject.PROP_MODIFIED.equals(s) && !dob.isModified()) {
                    dob.setModified(true);
                }
            }
        });
        dob.setModified(false);
        assertTrue("Should be still modified.", dob.isModified());
        assertNotNull("Still should have save cookie.",
                dob.getLookup().lookup(SaveCookie.class));
    }
 
Example #23
Source File: GradleDataObject.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
protected boolean notifyModified() {
    if (!super.notifyModified()) {
        return false;
    }
    if (getLookup().lookup(SaveCookie.class) == null) {
        getCookieSet().add(save);
        setModified(true);
    }
    return true;
}
 
Example #24
Source File: MultiDiffPanel.java    From netbeans with Apache License 2.0 5 votes vote down vote up
boolean canClose() {
    if (setups == null) {
        return true;
    }

    SaveCookie[] saveCookies = getSaveCookies(true);

    return (saveCookies.length == 0)
           || SaveBeforeClosingDiffConfirmation.allSaved(saveCookies);
}
 
Example #25
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 #26
Source File: Issue71764Test.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public TestNode() {
    super(Children.LEAF);
    getCookieSet().add(new SaveCookie() {
        public void save() throws IOException {
            System.out.println("Save cookie called");
        }
    });
}
 
Example #27
Source File: ActionUtils.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static SaveCookie getSaveCookie(TopComponent tc) {
    Lookup lookup = tc.getLookup();
    Object obj = lookup.lookup(SaveCookie.class);
    if(obj instanceof SaveCookie) {
        return (SaveCookie)obj;
    }
    
    return null;
}
 
Example #28
Source File: ActionUtils.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static void saveDocument(TopComponent tc) {
    SaveCookie sc = getSaveCookie(tc);
    if(sc != null) {
        try {
            sc.save();
        } catch(IOException ioe) {
            Exceptions.printStackTrace(ioe);
        }
    }
}
 
Example #29
Source File: DocumentsDlg.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void saveDocuments(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveDocuments
    // Add your handling code here:
    Node[] selNodes = explorer.getSelectedNodes();
    if (selNodes.length == 0) {
        return;
    }
    for (int i = 0; i < selNodes.length; i++) {
        TopComponent tc = ((TopComponentNode) selNodes[i]).getTopComponent();
        Lookup l = tc.getLookup();
        SaveCookie sc = (SaveCookie) l.lookup(SaveCookie.class);
        if (sc != null) {
            try {
                sc.save();
            } catch (IOException exc) {
                Logger.getAnonymousLogger().log(Level.WARNING,
                "[WinSys.DocumentsDlg.saveDocuments]" // NOI18N
                + " Warning: Cannot save content of TopComponent: [" // NOI18N
                + WindowManagerImpl.getInstance().getTopComponentDisplayName(tc) + "]" // NOI18N
                + " [" + tc.getClass().getName() + "]", exc); // NOI18N
            }
            //Refresh name of node because TopComponent name is probably changed too
            //('*' is removed)
            ((TopComponentNode) selNodes[i]).refresh();
        }
    }
    jButtonSave.setEnabled(false);
}
 
Example #30
Source File: MultiDiffPanelController.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private Map<File, SaveCookie> getSaveCookies(EditorCookie[] editorCookies) {
    Map<File, SaveCookie> proResult = new HashMap<>();
    Set<EditorCookie> editorCookieSet = new HashSet<>(Arrays.asList(editorCookies));
    for (Map.Entry<File, EditorCookie> e : this.editorCookies.entrySet()) {
        if (editorCookieSet.contains(e.getValue())) {
            File baseFile = e.getKey();
            FileObject fileObj = FileUtil.toFileObject(baseFile);
            if (fileObj == null) {
                continue;
            }
            proResult.put(baseFile, new EditorSaveCookie(e.getValue(), fileObj.getNameExt()));
        }
    }
    return proResult;
}