This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 43233
Collapse All | Expand All

(-)java/project/src/org/netbeans/spi/java/project/support/ui/PackageViewChildren.java (-5 / +151 lines)
Lines 28-35 Link Here
28
import java.util.SortedSet;
28
import java.util.SortedSet;
29
import java.util.TreeMap;
29
import java.util.TreeMap;
30
import java.util.TreeSet;
30
import java.util.TreeSet;
31
import javax.swing.Action;
31
import org.netbeans.api.java.queries.AccessibilityQuery;
32
import org.netbeans.api.java.queries.AccessibilityQuery;
32
import org.openide.ErrorManager;
33
import org.openide.ErrorManager;
34
import org.openide.actions.FileSystemAction;
33
import org.openide.filesystems.FileAttributeEvent;
35
import org.openide.filesystems.FileAttributeEvent;
34
import org.openide.filesystems.FileChangeListener;
36
import org.openide.filesystems.FileChangeListener;
35
import org.openide.filesystems.FileEvent;
37
import org.openide.filesystems.FileEvent;
Lines 46-54 Link Here
46
import org.openide.nodes.FilterNode;
48
import org.openide.nodes.FilterNode;
47
import org.openide.nodes.Node;
49
import org.openide.nodes.Node;
48
import org.openide.util.Lookup;
50
import org.openide.util.Lookup;
51
import org.openide.util.LookupListener;
49
import org.openide.util.NbBundle;
52
import org.openide.util.NbBundle;
50
import org.openide.util.RequestProcessor;
53
import org.openide.util.RequestProcessor;
51
import org.openide.util.Utilities;
54
import org.openide.util.Utilities;
55
import org.openide.util.actions.Presenter;
52
import org.openide.util.lookup.Lookups;
56
import org.openide.util.lookup.Lookups;
53
import org.openide.util.lookup.ProxyLookup;
57
import org.openide.util.lookup.ProxyLookup;
54
import org.openidex.search.SimpleSearchInfo;
58
import org.openidex.search.SimpleSearchInfo;
Lines 331-337 Link Here
331
    }
335
    }
332
     */
336
     */
333
     
337
     
338
    /** For a given Lookup creates new Lookup that converts any PackageNode inside itself into
339
     * all its subnodes.
340
     *
341
     * @param lookup input lookup that may contain, add or remove PackageNode
342
     * @return lookup that is either empty or contains nodes contained in the package
343
     */
344
    public static Lookup packagesToContent (Lookup lookup) {
345
        return new PackageLookup (lookup);
346
    }
334
    
347
    
348
    private static final class PackageLookup extends ProxyLookup
349
    implements org.openide.util.LookupListener {
350
        private static final Lookup.Template NODE = new Lookup.Template (PackageNode.class);
351
        
352
        /** Lookup result we take nodes from */
353
        private Lookup.Result delegate;
354
        
355
        public PackageLookup (Lookup source) {
356
            delegate = source.lookup (NODE);
357
            delegate.addLookupListener (
358
                (LookupListener)org.openide.util.WeakListeners.create (LookupListener.class, this, delegate)
359
            );
360
            resultChanged (null);
361
        }
362
        
363
        public void resultChanged(org.openide.util.LookupEvent ev) {
364
            java.util.Collection instances = delegate.allInstances();
365
            ArrayList lookups = new ArrayList ();
366
            java.util.Iterator it = instances.iterator ();
367
            while (it.hasNext()) {
368
                Node n = (Node)it.next ();
369
                Node[] arr = n.getChildren().getNodes(true);
370
                for (int i = 0; i < arr.length; i++) {
371
                    lookups.add (arr[i].getLookup());
372
                }
373
            }
374
            setLookups ((Lookup[])lookups.toArray (new Lookup[0]));
375
        }        
376
        
377
    }
335
378
336
    private static final class PackageNode extends FilterNode {
379
    private static final class PackageNode extends FilterNode {
337
        
380
        
Lines 355-367 Link Here
355
        private boolean isDefaultPackage;
398
        private boolean isDefaultPackage;
356
399
357
        public PackageNode( FileObject root, DataFolder dataFolder ) {
400
        public PackageNode( FileObject root, DataFolder dataFolder ) {
358
            super( dataFolder.getNodeDelegate(), 
401
            super( 
359
                   isEmpty( dataFolder ) ? Children.LEAF : dataFolder.createNodeChildren( NO_FOLDERS_FILTER ),
402
                dataFolder.getNodeDelegate(), 
360
                   new ProxyLookup(new Lookup[] {dataFolder.getNodeDelegate().getLookup(),
403
                isEmpty( dataFolder ) ? Children.LEAF : dataFolder.createNodeChildren( NO_FOLDERS_FILTER ),
361
                                                 Lookups.singleton(new SimpleSearchInfo(dataFolder, false))}));
404
                new AssignLookup ()
405
            );
362
            this.root = root;
406
            this.root = root;
363
            this.dataFolder = dataFolder;
407
            this.dataFolder = dataFolder;
364
            this.isDefaultPackage = root.equals( dataFolder.getPrimaryFile() );
408
            this.isDefaultPackage = root.equals( dataFolder.getPrimaryFile() );
409
            
410
            AssignLookup asign = (AssignLookup)getLookup ();
411
            asign.assignLookups (new Lookup[] {
412
                dataFolder.getNodeDelegate().getLookup(),
413
                Lookups.singleton(new SimpleSearchInfo(dataFolder, false)),
414
                Lookups.singleton(this),
415
            });
365
        }
416
        }
366
                       
417
                       
367
        public String getName() {
418
        public String getName() {
Lines 490-497 Link Here
490
            return true;
541
            return true;
491
        }
542
        }
492
        
543
        
544
        public javax.swing.Action[] getActions(boolean context) {
545
            javax.swing.Action[] old = super.getActions(context);
546
            javax.swing.Action[] retValue = new javax.swing.Action[old.length];
547
            for (int i = 0; i < retValue.length; i++) {
548
                if (old[i] instanceof org.openide.actions.FileSystemAction) {
549
                    retValue[i] = PkgAction.DEFAULT;
550
                } else {
551
                    retValue[i] = old[i];
552
                }
553
            }
554
            return retValue;
555
        }        
493
        
556
        
494
    }
557
    } // end of PackageNode
495
    
558
    
559
    private static final class AssignLookup extends ProxyLookup {
560
        public AssignLookup () {
561
            super (new Lookup[0]);
562
        }
563
        
564
        public void assignLookups (Lookup[] arr) {
565
            setLookups (arr);
566
        }
567
    } // end of AssignLookup
496
    
568
    
569
    /** Special action that delegates file system operations to the 
570
     * content of package only, not the whole tree.
571
     */
572
    private static final class PkgAction 
573
    implements javax.swing.Action, org.openide.util.ContextAwareAction,
574
    Presenter.Menu, Presenter.Popup {
575
        static final PkgAction DEFAULT = create (
576
            Utilities.actionsGlobalContext()
577
        );
578
        private javax.swing.Action delegate;
579
        
580
        
581
        
582
        private PkgAction (Action d) {
583
            delegate = d;
584
        }
585
        
586
        //
587
        // Presentation stuff
588
        //
589
        
590
        /** @return menu presenter.  */
591
        public javax.swing.JMenuItem getMenuPresenter () {
592
            return ((Presenter.Menu)delegate).getMenuPresenter ();
593
        }
594
595
        /** @return popup presenter.  */
596
        public javax.swing.JMenuItem getPopupPresenter () {
597
            return ((Presenter.Popup)delegate).getPopupPresenter ();
598
        }
599
        
600
        //
601
        // Action stuff
602
        //
603
        
604
        public void actionPerformed(java.awt.event.ActionEvent e) {
605
            delegate.actionPerformed (e);
606
        }        
607
        
608
        public void addPropertyChangeListener(PropertyChangeListener listener) {
609
            delegate.addPropertyChangeListener (listener);
610
        }
611
        
612
        public Object getValue(String key) {
613
            return delegate.getValue (key);
614
        }
615
        
616
        public boolean isEnabled() {
617
            return delegate.isEnabled ();
618
        }
619
        
620
        public void putValue(String key, Object value) {
621
            delegate.putValue (key, value);
622
        }
623
        
624
        public void removePropertyChangeListener(PropertyChangeListener listener) {
625
            delegate.removePropertyChangeListener (listener);
626
        }
627
        
628
        public void setEnabled(boolean b) {
629
            delegate.setEnabled (b);
630
        }
631
        
632
        public javax.swing.Action createContextAwareInstance(Lookup actionContext) {
633
            return create (actionContext);
634
        }
635
        
636
        private static PkgAction create (Lookup context) {
637
            FileSystemAction action = (FileSystemAction)FileSystemAction.get (FileSystemAction.class);
638
            return new PkgAction (
639
                action.createContextAwareInstance (packagesToContent (context))
640
            );
641
        }
642
    } // end of PkgAction
497
}
643
}
(-)java/project/test/unit/src/org/netbeans/spi/java/project/support/ui/PackageViewLookupTest.java (+83 lines)
Added Link Here
1
/*
2
 *                 Sun Public License Notice
3
 *
4
 * The contents of this file are subject to the Sun Public License
5
 * Version 1.0 (the "License"). You may not use this file except in
6
 * compliance with the License. A copy of the License is available at
7
 * http://www.sun.com/
8
 *
9
 * The Original Code is NetBeans. The Initial Developer of the Original
10
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2004 Sun
11
 * Microsystems, Inc. All Rights Reserved.
12
 */
13
14
package org.netbeans.spi.java.project.support.ui;
15
16
import java.util.*;
17
import junit.framework.*;
18
import org.netbeans.junit.NbTestCase;
19
20
import org.netbeans.api.project.TestUtil;
21
import org.openide.filesystems.FileLock;
22
import org.openide.filesystems.FileObject;
23
import org.openide.filesystems.FileSystem;
24
import org.openide.filesystems.FileUtil;
25
import org.openide.filesystems.LocalFileSystem;
26
import org.openide.loaders.DataObject;
27
import org.openide.nodes.Children;
28
import org.openide.nodes.Node;
29
import org.openide.util.Lookup;
30
import org.openide.util.lookup.InstanceContent;
31
32
public class PackageViewLookupTest extends PackageViewTest {
33
    private static final Lookup.Template DO = new Lookup.Template (DataObject.class);
34
    
35
    public PackageViewLookupTest(String name) {
36
        super( name );
37
    }
38
39
    
40
    protected void setUp () throws Exception {
41
        super.setUp ();
42
    }
43
    
44
    
45
    protected void assertNodes( Children children, String[] nodeNames, int[] childCount ) {
46
        final Node[] nodes = children.getNodes(true);
47
        if (nodes.length == 0) {
48
            return;
49
        }
50
        
51
        final Node[] currentNode = new Node[1];
52
        
53
        class Del implements org.openide.util.Lookup.Provider {
54
            public org.openide.util.Lookup getLookup () {
55
                if (currentNode[0] == null) {
56
                    return org.openide.util.Lookup.EMPTY;
57
                }
58
                return currentNode[0].getLookup();
59
            }
60
        }
61
        currentNode[0] = nodes[0];
62
        Del del = new Del ();
63
        Lookup orig = org.openide.util.lookup.Lookups.proxy (del);
64
        Lookup pkgs = PackageViewChildren.packagesToContent (orig);
65
        
66
        for( int i = 0; i < nodes.length; i++ ) {
67
            currentNode[0] = nodes[i];
68
            orig.lookup (Object.class);
69
            
70
            Collection c = pkgs.lookup (new Lookup.Template (Node.class)).allInstances ();
71
            if (! (c instanceof List)) {
72
                c = new ArrayList (c);
73
            }
74
            
75
            assertEquals (
76
                "Nodes for " + i + "th node are the same the node: " + nodes[i], 
77
                Arrays.asList (nodes[i].getChildren().getNodes (true)),
78
                c
79
            );
80
        }
81
    }
82
    
83
}
(-)java/project/test/unit/src/org/netbeans/spi/java/project/support/ui/PackageViewTest.java (-7 / +14 lines)
Lines 31-52 Link Here
31
        System.setProperty("org.netbeans.spi.java.project.support.ui.packageView.TRUNCATE_PACKAGE_NAMES", "true");
31
        System.setProperty("org.netbeans.spi.java.project.support.ui.packageView.TRUNCATE_PACKAGE_NAMES", "true");
32
    }
32
    }
33
    
33
    
34
    /** Root directory of the project */
35
    protected FileObject root;
36
    /** children that represent packages in such project */
37
    protected Children ch;
38
    
34
    public PackageViewTest( String name ) {
39
    public PackageViewTest( String name ) {
35
        super( name );
40
        super( name );
36
    }
41
    }
37
    
42
    
38
    
43
    protected void setUp () throws Exception {
39
    public void testFolders() throws Exception {
40
        
44
        
41
        // Prepare test data
45
        // Prepare test data
42
        FileObject root = TestUtil.makeScratchDir( this );
46
        root = TestUtil.makeScratchDir( this );
43
        // System.out.println("root " + root.getFileSystem().getClass() );
47
        // System.out.println("root " + root.getFileSystem().getClass() );
44
        
48
        
45
        
49
        
46
	// Create children
50
	// Create children
47
        FileUtil.createFolder( root, "src" );
51
        FileUtil.createFolder( root, "src" );
48
        Children ch = PackageView.createPackageView( root.getFileObject( "src" ) );
52
        ch = PackageView.createPackageView( root.getFileObject( "src" ) );
49
        
53
    }
54
    
55
    
56
    public void testFolders() throws Exception {
50
        
57
        
51
	FileUtil.createFolder( root, "src/a/b/c" );
58
	FileUtil.createFolder( root, "src/a/b/c" );
52
        assertNodes( ch, 
59
        assertNodes( ch, 
Lines 168-178 Link Here
168
    }
175
    }
169
    
176
    
170
        
177
        
171
    public static void assertNodes( Children children, String[] nodeNames ) {
178
    private void assertNodes( Children children, String[] nodeNames ) {
172
        assertNodes( children, nodeNames, null );
179
        assertNodes( children, nodeNames, null );
173
    }
180
    }
174
    
181
    
175
    public static void assertNodes( Children children, String[] nodeNames, int[] childCount ) {
182
    protected void assertNodes( Children children, String[] nodeNames, int[] childCount ) {
176
        Node[] nodes = children.getNodes();
183
        Node[] nodes = children.getNodes();
177
        assertEquals( "Wrong number of nodes.", nodeNames.length, nodes.length );
184
        assertEquals( "Wrong number of nodes.", nodeNames.length, nodes.length );
178
        
185
        
(-)vcscore/src/org/netbeans/modules/vcscore/VcsFSCommandsAction.java (-8 / +106 lines)
Lines 61-66 Link Here
61
import org.netbeans.modules.vcscore.util.Table;
61
import org.netbeans.modules.vcscore.util.Table;
62
import org.netbeans.modules.vcscore.util.WeakList;
62
import org.netbeans.modules.vcscore.util.WeakList;
63
import org.openide.filesystems.FileSystem;
63
import org.openide.filesystems.FileSystem;
64
import org.openide.util.Lookup;
65
import org.openide.util.LookupListener;
64
66
65
/**
67
/**
66
 * The system action with VCS commands, that are provided by the FileSystem.
68
 * The system action with VCS commands, that are provided by the FileSystem.
Lines 86-95 Link Here
86
    /**
88
    /**
87
     * @return a map of array of FileObjects and their messages if any.
89
     * @return a map of array of FileObjects and their messages if any.
88
     */
90
     */
89
    private Map getSelectedFileObjectsFromActiveNodes() {
91
    private Map getSelectedFileObjectsFromActiveNodes (Lookup lookup) {
90
        Map filesWithMessages = new Table();
92
        Map filesWithMessages = new Table();
91
        ArrayList files = new ArrayList();
93
        ArrayList files = new ArrayList();
92
        Node[] nodes = getActivatedNodes();
94
        Node[] nodes = (Node[])lookup.lookup (new Lookup.Template (Node.class)).allInstances().toArray (new Node[0]);
93
        for (int i = 0; i < nodes.length; i++) {
95
        for (int i = 0; i < nodes.length; i++) {
94
            GroupCookie gc = (GroupCookie) nodes[i].getCookie(GroupCookie.class);
96
            GroupCookie gc = (GroupCookie) nodes[i].getCookie(GroupCookie.class);
95
            if (gc != null) {
97
            if (gc != null) {
Lines 206-224 Link Here
206
     * Get a menu item that can present this action in a <code>JMenu</code>.
208
     * Get a menu item that can present this action in a <code>JMenu</code>.
207
     */
209
     */
208
    public JMenuItem getMenuPresenter() {
210
    public JMenuItem getMenuPresenter() {
209
        return getPresenter(true);
211
        return getPresenter(true, org.openide.util.Utilities.actionsGlobalContext ());
210
    }
212
    }
211
    
213
    
212
    /**
214
    /**
213
     * Get a menu item that can present this action in a <code>JPopupMenu</code>.
215
     * Get a menu item that can present this action in a <code>JPopupMenu</code>.
214
     */
216
     */
215
    public JMenuItem getPopupPresenter() {
217
    public JMenuItem getPopupPresenter() {
216
        return getPresenter(false);
218
        return getPresenter(false, org.openide.util.Utilities.actionsGlobalContext());
217
    }
219
    }
218
    
220
    
219
    private JMenuItem getPresenter(boolean inMenu) {
221
    private JMenuItem getPresenter(boolean inMenu, Lookup lookup) {
220
        JInlineMenu menu = new JInlineMenu();
222
        JInlineMenu menu = new JInlineMenu();
221
        JMenuItem[] items = createMenuItems(inMenu);
223
        JMenuItem[] items = createMenuItems(inMenu, lookup);
222
        if (items.length == 0) return menu;
224
        if (items.length == 0) return menu;
223
        menu.setMenuItems(items);
225
        menu.setMenuItems(items);
224
        if (inMenu && menu != null) {
226
        if (inMenu && menu != null) {
Lines 227-234 Link Here
227
        return menu;
229
        return menu;
228
    }
230
    }
229
    
231
    
230
    public JMenuItem[] createMenuItems(boolean inMenu) {
232
    public JMenuItem[] createMenuItems(boolean inMenu, Lookup lookup) {
231
        Map filesWithMessages = getSelectedFileObjectsFromActiveNodes();
233
        Map filesWithMessages = getSelectedFileObjectsFromActiveNodes (lookup);
232
        //System.out.println("VcsFSCommandsAction.getPresenter(): selected filesWithMessages: "+filesWithMessages);
234
        //System.out.println("VcsFSCommandsAction.getPresenter(): selected filesWithMessages: "+filesWithMessages);
233
        switchableList = new ArrayList();
235
        switchableList = new ArrayList();
234
        ArrayList menuItems = new ArrayList();
236
        ArrayList menuItems = new ArrayList();
Lines 479-484 Link Here
479
        return new HelpCtx(VcsFSCommandsAction.class);
481
        return new HelpCtx(VcsFSCommandsAction.class);
480
    }
482
    }
481
    
483
    
484
    public javax.swing.Action createContextAwareInstance(Lookup actionContext) {
485
        return new DelegateAction (this, actionContext);
486
    }
487
    
482
    private static final class MergedCommandSupport extends CommandSupport {
488
    private static final class MergedCommandSupport extends CommandSupport {
483
        
489
        
484
        private CommandSupport cmdSupport1;
490
        private CommandSupport cmdSupport1;
Lines 634-637 Link Here
634
        
640
        
635
    }
641
    }
636
642
643
    /** A delegate action that is usually associated with a specific lookup and
644
     * extract the nodes it operates on from it. Otherwise it delegates to the
645
     * regular NodeAction.
646
     */
647
    static class DelegateAction extends Object
648
    implements javax.swing.Action, org.openide.util.LookupListener,
649
    org.openide.util.actions.Presenter.Menu, org.openide.util.actions.Presenter.Popup {
650
        /** action to delegate too */
651
        private VcsFSCommandsAction delegate;
652
        /** lookup we are associated with (or null) */
653
        private org.openide.util.Lookup.Result result;
654
        /** lookup to work with */
655
        private Lookup lookup;
656
        /** previous state of enabled */
657
        private boolean enabled = true;
658
        /** support for listeners */
659
        private java.beans.PropertyChangeSupport support = new java.beans.PropertyChangeSupport (this);
660
        
661
        public DelegateAction (VcsFSCommandsAction a, Lookup actionContext) {
662
            this.delegate = a;
663
            
664
            this.lookup = actionContext;
665
            this.result = actionContext.lookup (new org.openide.util.Lookup.Template (
666
                Node.class
667
            ));
668
            this.result.addLookupListener ((LookupListener)org.openide.util.WeakListeners.create (
669
                LookupListener.class, this, this.result
670
            ));
671
            resultChanged (null);
672
        }
673
        
674
        
675
        /** Overrides superclass method, adds delegate description. */
676
        public String toString() {
677
            return super.toString() + "[delegate=" + delegate + "]"; // NOI18N
678
        }
679
680
        private static final Node[] EMPTY_NODE_ARRAY = new Node[0];
681
        
682
        /** Nodes are taken from the lookup if any.
683
         */
684
        public final synchronized Node[] nodes () {
685
            if (result != null) {
686
                return (Node[])result.allInstances().toArray(EMPTY_NODE_ARRAY);
687
            } else {
688
                return EMPTY_NODE_ARRAY;
689
            }
690
        }
691
        
692
        /** Invoked when an action occurs.
693
         */
694
        public void actionPerformed(java.awt.event.ActionEvent e) {
695
        }
696
        
697
        public void addPropertyChangeListener(java.beans.PropertyChangeListener listener) {
698
            support.addPropertyChangeListener (listener);
699
        }
700
        
701
        public void removePropertyChangeListener(java.beans.PropertyChangeListener listener) {
702
            support.removePropertyChangeListener (listener);
703
        }
704
705
        public void putValue(String key, Object o) {}
706
        
707
        public Object getValue(String key) {
708
            return delegate.getValue(key);
709
        }
710
        
711
        public boolean isEnabled() {
712
            return delegate.enable(nodes ());
713
        }
714
        
715
        public void setEnabled(boolean b) {
716
        }
717
718
        public void resultChanged(org.openide.util.LookupEvent ev) {
719
            boolean newEnabled = delegate.enable(nodes ());
720
            if (newEnabled != enabled) {
721
                support.firePropertyChange (PROP_ENABLED, enabled, newEnabled);
722
                enabled = newEnabled;
723
            }
724
        }
725
        
726
        public javax.swing.JMenuItem getMenuPresenter() {
727
            return delegate.getPresenter (true, lookup);
728
        }
729
        
730
        public javax.swing.JMenuItem getPopupPresenter() {
731
            return delegate.getPresenter (false, lookup);
732
        }
733
    } // end of DelegateAction
734
    
637
}
735
}
(-)vcscore/src/org/netbeans/modules/vcscore/actions/VcsAllCommandsAction.java (-1 / +1 lines)
Lines 131-137 Link Here
131
    
131
    
132
    private static JMenuItem[] getContextMenu(boolean popup) {
132
    private static JMenuItem[] getContextMenu(boolean popup) {
133
        VcsFSCommandsAction contextAction = (VcsFSCommandsAction) VcsFSCommandsAction.get(VcsFSCommandsAction.class);
133
        VcsFSCommandsAction contextAction = (VcsFSCommandsAction) VcsFSCommandsAction.get(VcsFSCommandsAction.class);
134
        JMenuItem[] contextMenu = contextAction.createMenuItems(popup);
134
        JMenuItem[] contextMenu = contextAction.createMenuItems(popup, org.openide.util.Utilities.actionsGlobalContext());
135
        return contextMenu;
135
        return contextMenu;
136
    }
136
    }
137
    
137
    

Return to bug 43233