| // |
| // 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. |
| // |
| |
| = DevFaqListenForOpenEvents |
| :jbake-type: wiki |
| :jbake-tags: wiki, devfaq, needsreview |
| :jbake-status: published |
| :keywords: Apache NetBeans wiki DevFaqListenForOpenEvents |
| :description: Apache NetBeans wiki DevFaqListenForOpenEvents |
| :toc: left |
| :toc-title: |
| :syntax: true |
| |
| === How can I be notified when a file is opened? |
| |
| [source,java] |
| ---- |
| |
| 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); |
| } |
| }); |
| } |
| } |
| } |
| } |
| } |
| } |
| }); |
| } |
| |
| } |
| ---- |
| |
| See also link:http://java.net/projects/nbwicketsupport/sources/nbwicketsupport/content/WicketSuite/WicketCore/src/org/netbeans/modules/web/wicket/installer/Installer.java[http://java.net/projects/nbwicketsupport/sources/nbwicketsupport/content/WicketSuite/WicketCore/src/org/netbeans/modules/web/wicket/installer/Installer.java] |
| |
| Taken from nbdev-mailing list - thanks to Geertjan. |
| |
| === 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/DevFaqListenForOpenEvents[http://wiki.netbeans.org/DevFaqListenForOpenEvents] , |
| that was last modified by NetBeans user Markiewb |
| on 2013-02-03T16:11:47Z. |
| |
| |
| *NOTE:* This document was automatically converted to the AsciiDoc format on 2018-02-07, and needs to be reviewed. |