3232import org .netbeans .swing .plaf .LFCustoms ;
3333import org .openide .filesystems .FileObject ;
3434import org .openide .filesystems .FileUtil ;
35+ import org .openide .loaders .DataObject ;
36+ import org .openide .util .NbBundle ;
3537import org .openide .util .NbPreferences ;
36- import org .openide .windows .Mode ;
3738import org .openide .windows .OnShowing ;
39+ import org .openide .windows .TopComponent ;
3840import org .openide .windows .WindowManager ;
3941
4042/**
4143 * For lifecycle tasks.
44+ *
4245 * @author mbien
4346 */
4447public final class Module {
4548
4649 private static final String INITIAL_OPEN_DONE_KEY = "initial-open-done" ; //NOI18N
50+ private static final String INITIAL_OPEN_BRANDING_KEY = "Favorites.openOnFirstFile" ; //NOI18N
4751
4852 private Module () {}
4953
5054 @ OnShowing
5155 public final static class EDTInit implements Runnable {
5256
57+ private final boolean openOnFirstFile ;
58+
59+ public EDTInit () {
60+ openOnFirstFile = Boolean .parseBoolean (
61+ NbBundle .getMessage (Module .class , INITIAL_OPEN_BRANDING_KEY ));
62+ }
63+
5364 @ Override
5465 public void run () {
5566 registerFavAppenderFunction ();
@@ -73,19 +84,23 @@ private void registerFavAppenderFunction() {
7384 UIManager .put (LFCustoms .FILECHOOSER_SHORTCUTS_FILESFUNCTION , favAppender );
7485 }
7586
76- // very first on- editor-open event will also open the Favorites tab
87+ // very first file editor opened will also open the Favorites tab
7788 private void attachFirstEditorOpenListener () {
89+ if (!openOnFirstFile ) {
90+ return ;
91+ }
7892 Preferences prefs = NbPreferences .forModule (Module .class );
7993 if (prefs .getBoolean (INITIAL_OPEN_DONE_KEY , false )) {
8094 return ;
8195 }
82- WindowManager wm = WindowManager . getDefault ();
83- wm .addPropertyChangeListener (new PropertyChangeListener () {
96+ TopComponent . Registry registry = TopComponent . getRegistry ();
97+ registry .addPropertyChangeListener (new PropertyChangeListener () {
8498 @ Override
8599 public void propertyChange (PropertyChangeEvent evt ) {
86- if ("activeMode" .equals (evt .getPropertyName ()) //NOI18N
87- && evt .getNewValue () instanceof Mode mode
88- && wm .isEditorMode (mode )) {
100+ if (TopComponent .Registry .PROP_TC_OPENED .equals (evt .getPropertyName ())
101+ && evt .getNewValue () instanceof TopComponent tc
102+ && hasFileReference (tc )
103+ && WindowManager .getDefault ().isEditorTopComponent (tc )) {
89104 try {
90105 Tab favTab = Tab .findDefault ();
91106 if (favTab != null && !favTab .wasOpened () && !favTab .isOpened ()) {
@@ -95,13 +110,18 @@ public void propertyChange(PropertyChangeEvent evt) {
95110 prefs .putBoolean (INITIAL_OPEN_DONE_KEY , true );
96111 PropertyChangeListener thisListener = this ;
97112 SwingUtilities .invokeLater (() -> {
98- wm .removePropertyChangeListener (thisListener );
113+ registry .removePropertyChangeListener (thisListener );
99114 });
100115 }
101116 }
117+
102118 }
103119 });
104120 }
121+
122+ private boolean hasFileReference (TopComponent tc ) {
123+ return tc .getLookup ().lookup (DataObject .class ) != null ;
124+ }
105125 }
106126
107127}
0 commit comments