This is an automated email from the ASF dual-hosted git repository. geertjan pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/netbeans.git
The following commit(s) were added to refs/heads/master by this push: new c450d14 NETBEANS-888 Open Recent file not working (#1216) c450d14 is described below commit c450d145f94574809d4712c852b2d7c3403a2fa3 Author: mck <m...@thelastpickle.com> AuthorDate: Thu May 2 19:13:08 2019 +1000 NETBEANS-888 Open Recent file not working (#1216) - update menuItems under RecentFileAction via a RecentFiles property change listener, rather than popup menu listener --- .../modules/openfile/RecentFileAction.java | 97 ++++++++++------------ .../org/netbeans/modules/openfile/RecentFiles.java | 16 ++++ 2 files changed, 61 insertions(+), 52 deletions(-) diff --git a/ide/utilities/src/org/netbeans/modules/openfile/RecentFileAction.java b/ide/utilities/src/org/netbeans/modules/openfile/RecentFileAction.java index 118aa8a..2138d1a 100644 --- a/ide/utilities/src/org/netbeans/modules/openfile/RecentFileAction.java +++ b/ide/utilities/src/org/netbeans/modules/openfile/RecentFileAction.java @@ -25,19 +25,18 @@ import java.awt.Point; import java.awt.PointerInfo; import java.awt.Toolkit; import java.awt.event.ActionEvent; +import java.beans.PropertyChangeEvent; +import java.beans.PropertyChangeListener; import java.io.File; import java.util.List; import javax.swing.*; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; -import javax.swing.event.PopupMenuEvent; -import javax.swing.event.PopupMenuListener; import org.netbeans.modules.openfile.RecentFiles.HistoryItem; import org.openide.awt.*; import org.openide.filesystems.FileUtil; import org.openide.util.NbBundle; import org.openide.util.RequestProcessor; -import org.openide.util.Utilities; import org.openide.util.actions.Presenter; /** @@ -51,10 +50,9 @@ import org.openide.util.actions.Presenter; @ActionID(category="System", id="org.netbeans.modules.openfile.RecentFileAction") @ActionReference(path="Menu/File", position=900) public class RecentFileAction extends AbstractAction - implements Presenter.Menu, PopupMenuListener, ChangeListener { + implements Presenter.Menu, ChangeListener, PropertyChangeListener { - private static final RequestProcessor RP = - new RequestProcessor(RecentFileAction.class); + private static final RequestProcessor RP = new RequestProcessor(RecentFileAction.class); /** property of menu items where we store fileobject to open */ private static final String PATH_PROP = @@ -73,12 +71,15 @@ public class RecentFileAction extends AbstractAction "OFMSG_NO_RECENT_FILE"); // NOI18N private JMenu menu; + private boolean recreate = true; public RecentFileAction() { super(NbBundle.getMessage(RecentFileAction.class, "LBL_RecentFileAction_Name")); // NOI18N + + RecentFiles.addPropertyChangeListener(this); } - + /********* Presenter.Menu impl **********/ @Override @@ -87,70 +88,60 @@ public class RecentFileAction extends AbstractAction menu = new UpdatingMenu(this); menu.setMnemonic(NbBundle.getMessage(RecentFileAction.class, "MNE_RecentFileAction_Name").charAt(0)); // NOI18N - // #115277 - workaround, PopupMenuListener don't work on Mac - if (!Utilities.isMac()) { - menu.getPopupMenu().addPopupMenuListener(this); - } else { - menu.addChangeListener(this); - } + + menu.getModel().addChangeListener(this); + fillSubMenu(); } return menu; } - - /******* PopupMenuListener impl *******/ - /* Fills submenu when popup is about to be displayed. - * Note that argument may be null on Mac due to #115277 fix - */ - @Override - public void popupMenuWillBecomeVisible(PopupMenuEvent arg0) { - fillSubMenu(); - } + // Implementation of change listener --------------------------------------- - /* Clears submenu when popup is about to be hidden. - * Note that argument may be null on Mac due to #115277 fix - */ @Override - public void popupMenuWillBecomeInvisible(PopupMenuEvent arg0) { - menu.removeAll(); - } - - @Override - public void popupMenuCanceled(PopupMenuEvent arg0) { + public void propertyChange(PropertyChangeEvent e) { + if ( RecentFiles.PROPERTY_RECENT_FILES.equals( e.getPropertyName() ) ) { + SwingUtilities.invokeLater(new Runnable() { + @Override + public void run() { + recreate = true; + } + }); + } } /******** ChangeListener impl *********/ - /** Delegates to popupMenuListener based on menu current selection status */ @Override public void stateChanged(ChangeEvent e) { - if (menu.isSelected()) { - popupMenuWillBecomeVisible(null); - } else { - popupMenuWillBecomeInvisible(null); + if (menu.getModel().isSelected()) { + fillSubMenu(); } } /** Fills submenu with recently closed files got from RecentFiles support */ private void fillSubMenu () { - List<HistoryItem> files = RecentFiles.getRecentFiles(); - boolean first = true; - for (final HistoryItem hItem : files) { - try { // #188403 - JMenuItem jmi = newSubMenuItem(hItem); - menu.add(jmi); - if( first ) { - Object accel = getValue( Action.ACCELERATOR_KEY ); - if( accel instanceof KeyStroke ) { - jmi.setAccelerator( (KeyStroke)accel ); + if (recreate && RecentFiles.hasRecentFiles()) { + menu.removeAll(); + List<HistoryItem> files = RecentFiles.getRecentFiles(); + boolean first = true; + for (final HistoryItem hItem : files) { + try { // #188403 + JMenuItem jmi = newSubMenuItem(hItem); + menu.add(jmi); + if( first ) { + Object accel = getValue( Action.ACCELERATOR_KEY ); + if( accel instanceof KeyStroke ) { + jmi.setAccelerator( (KeyStroke)accel ); + } + first = false; } - first = false; + } catch (Exception ex) { + continue; } - } catch (Exception ex) { - continue; } + ensureSelected(); + recreate = false; } - ensureSelected(); } /** @@ -270,7 +261,6 @@ public class RecentFileAction extends AbstractAction @Override public JComponent[] getMenuPresenters() { - setEnabled(RecentFiles.hasRecentFiles()); return content; } @@ -278,6 +268,9 @@ public class RecentFileAction extends AbstractAction public JComponent[] synchMenuPresenters(JComponent[] items) { return getMenuPresenters(); } - } + @Override public boolean isEnabled() { + return RecentFiles.hasRecentFiles(); + } + } } diff --git a/ide/utilities/src/org/netbeans/modules/openfile/RecentFiles.java b/ide/utilities/src/org/netbeans/modules/openfile/RecentFiles.java index dc95106..16fddcb 100644 --- a/ide/utilities/src/org/netbeans/modules/openfile/RecentFiles.java +++ b/ide/utilities/src/org/netbeans/modules/openfile/RecentFiles.java @@ -22,6 +22,7 @@ package org.netbeans.modules.openfile; import java.beans.BeanInfo; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; +import java.beans.PropertyChangeSupport; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; @@ -59,6 +60,8 @@ import org.openide.windows.WindowManager; */ public final class RecentFiles { + static final String PROPERTY_RECENT_FILES = "RecentFiles"; + /** List of recently closed files */ private static List<HistoryItem> history = new ArrayList<HistoryItem>(); /** Request processor */ @@ -80,10 +83,19 @@ public final class RecentFiles { RecentFiles.class.getName()); private static final String RECENT_FILE_KEY = "nb.recent.file.path"; // NOI18N + private static final PropertyChangeSupport PCH_SUPPORT = new PropertyChangeSupport(PROPERTY_RECENT_FILES); private RecentFiles() { } + static void addPropertyChangeListener(PropertyChangeListener l) { + PCH_SUPPORT.addPropertyChangeListener(l); + } + + static void removePropertyChangeListener(PropertyChangeListener l) { + PCH_SUPPORT.removePropertyChangeListener(l); + } + /** Starts to listen for recently closed files */ public static void init() { WindowManager.getDefault().invokeWhenUIReady(new Runnable() { @@ -93,6 +105,7 @@ public final class RecentFiles { List<HistoryItem> loaded = load(); synchronized (HISTORY_LOCK) { history.addAll(0, loaded); + PCH_SUPPORT.firePropertyChange(PROPERTY_RECENT_FILES, null, null); if (windowRegistryListener == null) { windowRegistryListener = new WindowRegistryL(); TopComponent.getRegistry().addPropertyChangeListener( @@ -208,6 +221,7 @@ public final class RecentFiles { try { synchronized (HISTORY_LOCK) { history.clear(); + PCH_SUPPORT.firePropertyChange(PROPERTY_RECENT_FILES, null, null); getPrefs().clear(); getPrefs().flush(); } @@ -251,6 +265,7 @@ public final class RecentFiles { history.remove(i); } newItem.setIcon(findIconForPath(newItem.getPath())); + PCH_SUPPORT.firePropertyChange(PROPERTY_RECENT_FILES, null, null); store(); } } @@ -268,6 +283,7 @@ public final class RecentFiles { HistoryItem hItem = findHistoryItem(path); if (hItem != null) { history.remove(hItem); + PCH_SUPPORT.firePropertyChange(PROPERTY_RECENT_FILES, null, null); } store(); } --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@netbeans.apache.org For additional commands, e-mail: commits-h...@netbeans.apache.org For further information about the NetBeans mailing lists, visit: https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists