[nbdev] Re: Listening for file open/close/save from a module

  • From: Benno Markiewicz < >
  • To:
  • Subject: [nbdev] Re: Listening for file open/close/save from a module
  • Date: Sun, 03 Feb 2013 17:16:05 +0100

Thanks Geertjan, i added a FAQ entry for this question at http://wiki.netbeans.org/DevFaqListenForOpenEvents

Greetings markiewb

Am 03.02.2013 10:47, schrieb Geertjan Wielenga:


Based on the link below, here's how to detect when a Java source file is open and when it is saved, in less than 50 lines of code, which includes import statements:

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.HashSet;
import org.openide.awt.StatusDisplayer;
import org.openide.filesystems.FileChangeAdapter;
import org.openide.filesystems.FileEvent;
import org.openide.filesystems.FileObject;
import org.openide.loaders.DataObject;
import org.openide.windows.OnShowing;
import org.openide.windows.TopComponent;
import org.openide.windows.WindowManager;

@OnShowing
public class Installer implements Runnable {
    @Override
    public void run() {
WindowManager.getDefault().getRegistry().addPropertyChangeListener(new PropertyChangeListener() {
            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                if (evt.getPropertyName().equals("opened")) {
HashSet<TopComponent> newHashSet = (HashSet<TopComponent>) evt.getNewValue();
HashSet<TopComponent> oldHashSet = (HashSet<TopComponent>) evt.getOldValue();
                    for (TopComponent topComponent : newHashSet) {
                        if (!oldHashSet.contains(topComponent)) {
DataObject dObj = topComponent.getLookup().lookup(DataObject.class);
                            if (dObj != null) {
                                FileObject currentFile = 
dObj.getPrimaryFile();
if (currentFile != null && currentFile.getMIMEType().equals("text/x-java")) {
StatusDisplayer.getDefault().setStatusText("Hurray! "
+ "Opened " + currentFile.getNameExt(), 1);
currentFile.addFileChangeListener(new FileChangeAdapter() {
                                        @Override
                                        public void fileChanged(FileEvent fe) 
{
StatusDisplayer.getDefault().setStatusText("Hurray! "
+ "Saved " + fe.getFile().getNameExt(), 1);
                                        }
                                    });
                                }
                            }
                        }
                    }
                }
            }
        });
    }

}


Gj



On 02/03/2013 09:52 AM, Geertjan Wielenga wrote:
On 02/03/2013 08:54 AM, cmann wrote:
Oh sorry, that was a mistake. I really thought that there would be some simple way to determine when a file (of any type) is opened in the IDE, but it would seem that this is not possible.




Really? Why do you think that?

All I've been doing is trying to find out what your use case is. Doesn't seem like you're clear about that yourself.

Anyway, this is a starting point for what you seem to want to do:

http://java.net/projects/nbwicketsupport/sources/nbwicketsupport/content/WicketSuite/WicketCore/src/org/netbeans/modules/web/wicket/installer/Installer.java

Gj




[nbdev] Re: Listening for file open/close/save from a module

(continued)

[nbdev] Re: Listening for file open/close/save from a module

Geertjan Wielenga 02/01/2013

[nbdev] Listening for file open/close/save from a module

cmann 02/01/2013

[nbdev] Re: Listening for file open/close/save from a module

Geertjan Wielenga 02/01/2013

[nbdev] Listening for file open/close/save from a module

cmann 02/01/2013

[nbdev] Re: Listening for file open/close/save from a module

Geertjan Wielenga 02/02/2013

[nbdev] Listening for file open/close/save from a module

cmann 02/02/2013

[nbdev] Re: Listening for file open/close/save from a module

Geertjan Wielenga 02/02/2013

[nbdev] Listening for file open/close/save from a module

cmann 02/03/2013

[nbdev] Re: Listening for file open/close/save from a module

Geertjan Wielenga 02/03/2013

[nbdev] Re: Listening for file open/close/save from a module

Geertjan Wielenga 02/03/2013

[nbdev] Re: Listening for file open/close/save from a module

Benno Markiewicz 02/03/2013

[nbdev] Re: Listening for file open/close/save from a module

Thomas Schneider 02/04/2013

[nbdev] Re: Listening for file open/close/save from a module

Benno Markiewicz 02/03/2013

[nbdev] Listening for file open/close/save from a module

cmann 02/03/2013

[nbdev] Re: Listening for file open/close/save from a module

Geertjan Wielenga 02/03/2013

[nbdev] Re: Listening for file open/close/save from a module

Benno Markiewicz 02/03/2013

[nbdev] Listening for file open/close/save from a module

cmann 02/04/2013

[nbdev] Re: Listening for file open/close/save from a module

Geertjan Wielenga 02/04/2013

[nbdev] Listening for file open/close/save from a module

cmann 02/04/2013

[nbdev] Re: Listening for file open/close/save from a module

Geertjan Wielenga 02/05/2013

[nbdev] Re: Listening for file open/close/save from a module

Geertjan Wielenga 02/05/2013

Project Features

About this Project

www was started in November 2009, is owned by jpirek, and has 21 members.
By use of this website, you agree to the NetBeans Policies and Terms of Use (revision 20140418.2d69abc). © 2013, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo
 
 
Close
loading
Please Confirm
Close