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 53295
Collapse All | Expand All

(-)openide/loaders/src/org/openide/loaders/FolderRenameHandler.java (+24 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-2005 Sun
11
 * Microsystems, Inc. All Rights Reserved.
12
 */
13
14
package org.openide.loaders;
15
16
import org.openide.nodes.Node;
17
18
/**
19
 *
20
 * @author Jan Becicka
21
 */
22
public interface FolderRenameHandler {
23
    void handleRename(DataFolder folder, String newName) throws IllegalArgumentException ;
24
}
(-)openide/loaders/src/org/openide/loaders/DataFolder.java (+33 lines)
Lines 29-34 Link Here
29
29
30
import org.openide.ErrorManager;
30
import org.openide.ErrorManager;
31
import org.openide.NotifyDescriptor;
31
import org.openide.NotifyDescriptor;
32
import org.openide.util.LookupEvent;
33
import org.openide.util.LookupListener;
32
import org.openide.util.datatransfer.*;
34
import org.openide.util.datatransfer.*;
33
import org.openide.cookies.*;
35
import org.openide.cookies.*;
34
import org.openide.filesystems.*;
36
import org.openide.filesystems.*;
Lines 1034-1039 Link Here
1034
        return folderName.indexOf ('/') == -1 && folderName.indexOf ('\\') == -1;
1036
        return folderName.indexOf ('/') == -1 && folderName.indexOf ('\\') == -1;
1035
    }
1037
    }
1036
    
1038
    
1039
    private static Lookup.Result renameImplementations;
1040
    private static List renameHandlersCache;
1041
    
1042
    private static synchronized FolderRenameHandler getRenameHandler() {
1043
        if (renameImplementations == null) {
1044
            renameImplementations = Lookup.getDefault().lookup(new Lookup.Template(FolderRenameHandler.class));
1045
            renameImplementations.addLookupListener(new LookupListener() {
1046
                public void resultChanged (LookupEvent ev) {
1047
                    synchronized (FolderNode.class) {
1048
                        renameHandlersCache = null;
1049
                    }
1050
                }});
1051
        }
1052
        if (renameHandlersCache == null) {
1053
            renameHandlersCache = new ArrayList(renameImplementations.allInstances());
1054
        }
1055
        assert (renameHandlersCache.size() <=1) : "Multiple instances of FolderRenameHandler found in Lookup";
1056
        if (renameHandlersCache.size()==0)
1057
            return null;
1058
        return (FolderRenameHandler) renameHandlersCache.get(0);
1059
    }
1060
    
1037
    /** Node for a folder.
1061
    /** Node for a folder.
1038
    */
1062
    */
1039
    public class FolderNode extends DataNode {
1063
    public class FolderNode extends DataNode {
Lines 1183-1188 Link Here
1183
                return new NewType[] { new NewFolder () };
1207
                return new NewType[] { new NewFolder () };
1184
            }
1208
            }
1185
 */
1209
 */
1210
        }
1211
        
1212
        public void setName(String name) {
1213
            FolderRenameHandler handler = getRenameHandler();
1214
            if (handler == null) {
1215
                super.setName(name);
1216
            } else {
1217
                handler.handleRename(DataFolder.this, name);
1218
            }
1186
        }
1219
        }
1187
        
1220
        
1188
        /* May add some paste types for objects being added to folders.
1221
        /* May add some paste types for objects being added to folders.
(-)openide/loaders/test/unit/src/org/openide/loaders/FolderRenameHandlerTest.java (+100 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-2005 Sun
11
 * Microsystems, Inc. All Rights Reserved.
12
 */
13
14
package org.openide.loaders;
15
16
import junit.framework.*;
17
import org.openide.filesystems.*;
18
import org.openide.nodes.Node;
19
20
/**
21
 * @author Jan Becicka
22
 */
23
public class FolderRenameHandlerTest extends TestCase {
24
    static {
25
        System.setProperty ("org.openide.util.Lookup", "org.openide.loaders.FolderRenameHandlerTest$Lkp"); // NOI18N
26
    }
27
    
28
    private FileObject fo;
29
    private Node n;
30
    private FolderRenameHandlerImpl frh = new FolderRenameHandlerImpl();
31
    private boolean lookupAdded;
32
    
33
    
34
    public FolderRenameHandlerTest (String testName) {
35
        super (testName);
36
    }
37
    
38
    public static junit.framework.Test suite () {
39
        junit.framework.TestSuite suite = new junit.framework.TestSuite(FolderRenameHandlerTest.class);
40
        
41
        return suite;
42
    }
43
    
44
    public void setUp() throws Exception {
45
        FileObject root = Repository.getDefault ().getDefaultFileSystem ().getRoot ();
46
        fo = FileUtil.createFolder (root, "test");// NOI18N
47
        
48
        DataObject obj = DataObject.find (fo);
49
        if (!  (obj instanceof DataFolder)) {
50
            fail ("It should be DataFolder: " + obj);// NOI18N
51
        }
52
        
53
        assertNotNull(obj);
54
        n = obj.getNodeDelegate();
55
        assertNotNull(n);
56
    }
57
    
58
    public void tearDown() throws Exception {
59
         fo.delete();
60
    }
61
62
    public void testRenameHandlerNotCalled () throws Exception {
63
        if (lookupAdded) {
64
            Lkp.ic.remove(frh);
65
            lookupAdded = false;
66
        }
67
        frh.called = false;
68
        
69
        n.setName("blabla");
70
        assertFalse(frh.called);
71
    }
72
    
73
    public void testRenameHandlerCalled () throws Exception {
74
        Lkp.ic.add (frh);
75
        lookupAdded = true;
76
        frh.called = false;
77
        
78
        n.setName("foo");// NOI18N
79
        assertTrue(frh.called);
80
    }
81
    
82
    public static final class Lkp extends org.openide.util.lookup.AbstractLookup {
83
        static org.openide.util.lookup.InstanceContent ic = new org.openide.util.lookup.InstanceContent ();
84
        public Lkp () {
85
            this (ic);
86
        }
87
        
88
        private Lkp (org.openide.util.lookup.InstanceContent ic) {
89
            super (ic);
90
        }
91
    }
92
    
93
    private static final class FolderRenameHandlerImpl implements FolderRenameHandler {
94
        boolean called  = false;
95
        public void handleRename(DataFolder folder, String newName) throws IllegalArgumentException {
96
            called = true;
97
        }
98
    }
99
    
100
}
(-)java/project/src/org/netbeans/spi/java/project/support/ui/PackageViewChildren.java (+29 lines)
Lines 52-57 Link Here
52
import org.openide.nodes.FilterNode;
52
import org.openide.nodes.FilterNode;
53
import org.openide.nodes.Node;
53
import org.openide.nodes.Node;
54
import org.openide.util.Lookup;
54
import org.openide.util.Lookup;
55
import org.openide.util.LookupEvent;
56
import org.openide.util.LookupListener;
55
import org.openide.util.NbBundle;
57
import org.openide.util.NbBundle;
56
import org.openide.util.RequestProcessor;
58
import org.openide.util.RequestProcessor;
57
import org.openide.util.Utilities;
59
import org.openide.util.Utilities;
Lines 642-649 Link Here
642
            return false;
644
            return false;
643
        }
645
        }
644
646
647
        private static Lookup.Result renameImplementations;
648
        private static List renameHandlersCache;
649
650
        private static synchronized PackageRenameHandler getRenameHandler() {
651
            if (renameImplementations == null) {
652
                renameImplementations = Lookup.getDefault().lookup(new Lookup.Template(PackageRenameHandler.class));
653
                renameImplementations.addLookupListener(new LookupListener() {
654
                    public void resultChanged (LookupEvent ev) {
655
                        synchronized (PackageNode.class) {
656
                            renameHandlersCache = null;
657
                        }
658
                    }});
659
            }
660
            if (renameHandlersCache == null) {
661
                renameHandlersCache = new ArrayList(renameImplementations.allInstances());
662
            }
663
            assert (renameHandlersCache.size() <=1) : "Multiple instances of PackageRenameHandler found in Lookup";
664
            if (renameHandlersCache.size()==0)
665
                return null;
666
            return (PackageRenameHandler) renameHandlersCache.get(0);
667
        }
645
        
668
        
646
        public void setName(String name) {
669
        public void setName(String name) {
670
            PackageRenameHandler handler = getRenameHandler();
671
            if (handler!=null) {
672
                handler.handleRename(this, name);
673
                return;
674
            }
675
            
647
            if (isDefaultPackage) {
676
            if (isDefaultPackage) {
648
                return;
677
                return;
649
            }
678
            }
(-)java/project/src/org/netbeans/spi/java/project/support/ui/PackageRenameHandler.java (+24 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-2005 Sun
11
 * Microsystems, Inc. All Rights Reserved.
12
 */
13
14
package org.netbeans.spi.java.project.support.ui;
15
16
import org.openide.nodes.Node;
17
18
/**
19
 *
20
 * @author Jan Becicka
21
 */
22
public interface PackageRenameHandler {
23
    void handleRename(Node node, String newName) throws IllegalArgumentException;
24
}
(-)java/project/test/unit/src/org/netbeans/spi/java/project/support/ui/PackageRenameHandlerTest.java (+137 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-2005 Sun
11
 * Microsystems, Inc. All Rights Reserved.
12
 */
13
14
package org.netbeans.spi.java.project.support.ui;
15
16
import java.beans.PropertyChangeListener;
17
import javax.swing.Icon;
18
import junit.framework.*;
19
import org.netbeans.api.project.SourceGroup;
20
import org.openide.filesystems.*;
21
import org.openide.nodes.Children;
22
import org.openide.nodes.Node;
23
24
/**
25
 * @author Jan Becicka
26
 */
27
public class PackageRenameHandlerTest extends TestCase {
28
    static {
29
        System.setProperty ("org.openide.util.Lookup", "org.netbeans.spi.java.project.support.ui.PackageRenameHandlerTest$Lkp"); // NOI18N
30
    }
31
    
32
    private FileObject fo;
33
    private Node n;
34
    private PackageRenameHandlerImpl frh = new PackageRenameHandlerImpl();
35
    private boolean lookupAdded;
36
    
37
    
38
    public PackageRenameHandlerTest (String testName) {
39
        super (testName);
40
    }
41
    
42
    public static junit.framework.Test suite () {
43
        junit.framework.TestSuite suite = new junit.framework.TestSuite(PackageRenameHandlerTest.class);
44
        
45
        return suite;
46
    }
47
    
48
    public void setUp() throws Exception {
49
        FileObject root = Repository.getDefault ().getDefaultFileSystem ().getRoot ();
50
        fo = FileUtil.createFolder (root, "test");// NOI18N
51
52
        SourceGroup group = new SimpleSourceGroup( FileUtil.createFolder( root, "src" ) );
53
        Children ch = PackageView.createPackageView( group ).getChildren();
54
        
55
        // Create folder
56
	FileUtil.createFolder( root, "src/foo" );
57
        n = ch.findChild( "foo" );           
58
        
59
        assertNotNull(n);
60
    }
61
    
62
    public void tearDown() throws Exception {
63
         fo.delete();
64
    }
65
66
    public void testRenameHandlerNotCalled () throws Exception {
67
        if (lookupAdded) {
68
            Lkp.ic.remove(frh);
69
            lookupAdded = false;
70
        }
71
        frh.called = false;
72
        
73
        n.setName("blabla");
74
        assertFalse(frh.called);
75
    }
76
    
77
    public void testRenameHandlerCalled () throws Exception {
78
        Lkp.ic.add (frh);
79
        lookupAdded = true;
80
        frh.called = false;
81
        
82
        n.setName("foo");// NOI18N
83
        assertTrue(frh.called);
84
    }
85
    
86
    public static final class Lkp extends org.openide.util.lookup.AbstractLookup {
87
        static org.openide.util.lookup.InstanceContent ic = new org.openide.util.lookup.InstanceContent ();
88
        public Lkp () {
89
            this (ic);
90
        }
91
        
92
        private Lkp (org.openide.util.lookup.InstanceContent ic) {
93
            super (ic);
94
        }
95
    }
96
    
97
    private static final class PackageRenameHandlerImpl implements PackageRenameHandler {
98
        boolean called  = false;
99
        public void handleRename(Node n, String newName) throws IllegalArgumentException {
100
            called = true;
101
        }
102
    }
103
    private static class SimpleSourceGroup implements SourceGroup {
104
        
105
        private FileObject root;
106
        
107
        public SimpleSourceGroup( FileObject root ) {
108
            this.root = root;
109
        }
110
        
111
        public FileObject getRootFolder() {
112
            return root;
113
        }
114
        
115
        public String getName() {
116
            return "TestGroup";
117
        }
118
        
119
        public String getDisplayName() {
120
            return getName();
121
        }
122
        
123
        public Icon getIcon(boolean opened) {
124
            return null;
125
        }
126
127
        public boolean contains(FileObject file) throws IllegalArgumentException {
128
            return FileUtil.isParentOf( root, file );
129
        }
130
    
131
        public void addPropertyChangeListener(PropertyChangeListener listener) {}
132
133
        public void removePropertyChangeListener(PropertyChangeListener listener) {}
134
        
135
    }    
136
    
137
}
(-)java/src/org/netbeans/modules/java/JavaNode.java (-11 / +39 lines)
Lines 14-19 Link Here
14
package org.netbeans.modules.java;
14
package org.netbeans.modules.java;
15
15
16
import java.awt.Image;
16
import java.awt.Image;
17
import java.awt.datatransfer.Transferable;
17
import java.beans.PropertyChangeEvent;
18
import java.beans.PropertyChangeEvent;
18
import java.beans.PropertyChangeListener;
19
import java.beans.PropertyChangeListener;
19
import java.beans.PropertyEditor;
20
import java.beans.PropertyEditor;
Lines 26-39 Link Here
26
import java.lang.reflect.Modifier;
27
import java.lang.reflect.Modifier;
27
import java.net.URI;
28
import java.net.URI;
28
import java.net.URL;
29
import java.net.URL;
29
import java.nio.charset.Charset;
30
import java.text.MessageFormat;
30
import java.text.MessageFormat;
31
import java.util.ArrayList;
31
import java.util.*;
32
import java.util.Arrays;
33
import java.util.HashSet;
34
import java.util.Iterator;
35
import java.util.List;
36
import java.util.Set;
37
import javax.jmi.reflect.InvalidObjectException;
32
import javax.jmi.reflect.InvalidObjectException;
38
import javax.swing.SwingUtilities;
33
import javax.swing.SwingUtilities;
39
import javax.swing.event.ChangeEvent;
34
import javax.swing.event.ChangeEvent;
Lines 63-79 Link Here
63
import org.openide.filesystems.FileObject;
58
import org.openide.filesystems.FileObject;
64
import org.openide.filesystems.FileStateInvalidException;
59
import org.openide.filesystems.FileStateInvalidException;
65
import org.openide.filesystems.FileUtil;
60
import org.openide.filesystems.FileUtil;
61
import org.openide.loaders.DataFolder;
66
import org.openide.loaders.DataNode;
62
import org.openide.loaders.DataNode;
67
import org.openide.loaders.DataObject;
63
import org.openide.loaders.DataObject;
68
import org.openide.nodes.Children;
64
import org.openide.nodes.Children;
69
import org.openide.nodes.Node;
65
import org.openide.nodes.Node;
66
import org.openide.nodes.NodeTransfer;
70
import org.openide.nodes.PropertySupport;
67
import org.openide.nodes.PropertySupport;
71
import org.openide.nodes.Sheet;
68
import org.openide.nodes.Sheet;
72
import org.openide.util.NbBundle;
69
import org.openide.util.*;
73
import org.openide.util.RequestProcessor;
74
import org.openide.util.Utilities;
75
import org.openide.util.WeakSet;
76
import org.openide.util.datatransfer.NewType;
70
import org.openide.util.datatransfer.NewType;
71
import org.openide.util.datatransfer.PasteType;
72
import org.openide.util.lookup.AbstractLookup;
73
import org.openide.util.lookup.InstanceContent;
77
74
78
/**
75
/**
79
 * The node representation of Java source files.
76
 * The node representation of Java source files.
Lines 423-428 Link Here
423
    
418
    
424
    public Image getOpenedIcon(int type) {
419
    public Image getOpenedIcon(int type) {
425
        return iconCache.getIcon(super.getOpenedIcon(type), getBadges());
420
        return iconCache.getIcon(super.getOpenedIcon(type), getBadges());
421
    }
422
    
423
    public void setName(String name) {
424
        RenameHandler handler = getRenameHandler();
425
        if (handler == null) {
426
            super.setName(name);
427
        } else {
428
            handler.handleRename(JavaNode.this, name);
429
        }
430
    }
431
    
432
    private static Lookup.Result renameImplementations;
433
    private static List renameHandlersCache;
434
    
435
    private static synchronized RenameHandler getRenameHandler() {
436
        if (renameImplementations == null) {
437
            renameImplementations = Lookup.getDefault().lookup(new Lookup.Template(RenameHandler.class));
438
            renameImplementations.addLookupListener(new LookupListener() {
439
                public void resultChanged(LookupEvent ev) {
440
                    synchronized (JavaNode.class) {
441
                        renameHandlersCache = null;
442
                    }
443
                }});
444
        }
445
        if (renameHandlersCache == null) {
446
            renameHandlersCache = new ArrayList(renameImplementations.allInstances());
447
        }
448
        assert (renameHandlersCache.size() <=1) : "Multiple instances of RenameHandler found in Lookup";
449
        if (renameHandlersCache.size()==0)
450
            return null;
451
        return (RenameHandler) renameHandlersCache.get(0);
426
    }
452
    }
427
453
428
    /** Get the icon base.
454
    /** Get the icon base.
(-)java/src/org/netbeans/modules/java/RenameHandler.java (+24 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-2005 Sun
11
 * Microsystems, Inc. All Rights Reserved.
12
 */
13
14
package org.netbeans.modules.java;
15
16
import org.openide.nodes.Node;
17
18
/**
19
 *
20
 * @author Jan Becicka
21
 */
22
public interface RenameHandler {
23
    void handleRename(Node node, String newName) throws IllegalArgumentException;
24
}

Return to bug 53295