| // |
| // Licensed to the Apache Software Foundation (ASF) under one |
| // or more contributor license agreements. See the NOTICE file |
| // distributed with this work for additional information |
| // regarding copyright ownership. The ASF licenses this file |
| // to you under the Apache License, Version 2.0 (the |
| // "License"); you may not use this file except in compliance |
| // with the License. You may obtain a copy of the License at |
| // |
| // http://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, |
| // software distributed under the License is distributed on an |
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| // KIND, either express or implied. See the License for the |
| // specific language governing permissions and limitations |
| // under the License. |
| // |
| |
| = DevFaqListenForSaveEvents |
| :jbake-type: wiki |
| :jbake-tags: wiki, devfaq, needsreview |
| :jbake-status: published |
| :keywords: Apache NetBeans wiki DevFaqListenForSaveEvents |
| :description: Apache NetBeans wiki DevFaqListenForSaveEvents |
| :toc: left |
| :toc-title: |
| :syntax: true |
| |
| === How can I be notified when a file is modified and saved?? |
| |
| The link:http://bits.netbeans.org/dev/javadoc/org-openide-loaders/org/openide/loaders/DataObject.Registry.html[DataObject.Registry] in link:http://bits.netbeans.org/dev/javadoc/org-openide-loaders[LoadersAPI] gives you a set of modified DataObjects. You can also add a listener and be notified when the set of modified objects changes. |
| |
| If you have a FileObject and want to listen for save events, you will need to get its DataObject by calling `link:http://bits.netbeans.org/dev/javadoc/org-openide-loaders/org/openide/loaders/DataObject.html#find(org.openide.filesystems.FileObject)[DataObject.find()]`. |
| |
| To listen on a single DataObject for save events, simply add a `PropertyChangeListener` and listen for changes in `DataObject.PROP_MODIFIED`. |
| |
| Note that listening for something to be saved is _not the same as listening for any changes in the file_ - you are really listening only for (directly or indirectly) user-initiated save events, as in the user pressing CTRL-S when the file is modified and opened in the editor. For notifications about any changes in a file, instead attach a `link:DevFaqListenForChangesInNonExistentFile.asciidoc[FileChangeListener]` to the underlying `link:DevFaqFileObject.asciidoc[FileObject]`. |
| |
| == Track the currently modified files by DataObject Registry listening - Sample code |
| |
| [source,java] |
| ---- |
| |
| DataObject.Registry registries = DataObject.getRegistry(); |
| registries.addChangeListener(new ChangeListener() { |
| public void stateChanged(ChangeEvent e) { |
| System.out.println("ChangedListener: o = " + e.getSource().getClass()); |
| System.out.println("ChangedListener: o.source = " + e.getSource()); |
| } |
| }); |
| DataObject[] objects = registries.getModified(); |
| for (int i = 0; i < objects.length; i++) { |
| DataObject dataObj = objects[I]; |
| System.out.println("data object name = " + dataObj.getName()); |
| System.out.println("data object pimary file name = " + dataObj.getPrimaryFile().getName()); |
| Set fss = dataObj.files(); |
| Iterator iter = fss.iterator(); |
| while (iter.hasNext()) { |
| FileObject fo = (FileObject) iter.next(); |
| System.out.println("\tset file object: " + fo.getName()); |
| } |
| } |
| |
| ---- |
| |
| == Libraries needed |
| |
| * Datasystems API |
| * File System API |
| * Nodes API |
| * Utilities API |
| |
| === Apache Migration Information |
| |
| The content in this page was kindly donated by Oracle Corp. to the |
| Apache Software Foundation. |
| |
| This page was exported from link:http://wiki.netbeans.org/DevFaqListenForSaveEvents[http://wiki.netbeans.org/DevFaqListenForSaveEvents] , |
| that was last modified by NetBeans user Jtulach |
| on 2010-07-24T19:50:39Z. |
| |
| |
| *NOTE:* This document was automatically converted to the AsciiDoc format on 2018-02-07, and needs to be reviewed. |