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

(-)src/org/openide/actions/SaveAction.java (-4 / +28 lines)
Lines 62-67 Link Here
62
        return false;
62
        return false;
63
    }
63
    }
64
    
64
    
65
    private static Class dataObject;
66
    private static java.lang.reflect.Method getNodeDelegate;
65
    /**
67
    /**
66
     * Extract a suitable after-save message. Will call 
68
     * Extract a suitable after-save message. Will call 
67
     * <code>Node.getValue(&quot;saveName&quot;)</code> to allow the node to
69
     * <code>Node.getValue(&quot;saveName&quot;)</code> to allow the node to
Lines 72-82 Link Here
72
     * @return name that should be printed to the user.
74
     * @return name that should be printed to the user.
73
     */
75
     */
74
    private String getSaveMessage(Node n) {
76
    private String getSaveMessage(Node n) {
75
        String s = (String) n.getValue("saveName"); //NOI18N
77
        if (dataObject == null) {
76
        if (s == null) {
78
            // read the class
77
            s = n.getDisplayName();
79
            ClassLoader l = (ClassLoader)org.openide.util.Lookup.getDefault ().lookup (ClassLoader.class);
80
            if (l == null) {
81
                l = getClass ().getClassLoader ();
82
            }
83
            try {
84
                dataObject = Class.forName ("org.openide.loaders.DataObject", true, l); // NOI18N
85
                getNodeDelegate = dataObject.getMethod ("getNodeDelegate", new Class[0]); // NOI18N
86
            } catch (Exception ex) {
87
                ErrorManager.getDefault ().notify (ex);
88
            }
78
        }
89
        }
79
        return s;
90
        
91
        if (getNodeDelegate != null) {
92
            // try to search for a name on the class
93
            Object obj = n.getCookie (dataObject);
94
            if (obj != null) {
95
                try {
96
                    n = (Node)getNodeDelegate.invoke (obj, new Object[0]);
97
                } catch (Exception ex) {
98
                    ErrorManager.getDefault ().notify (ex);
99
                }
100
            }
101
        }
102
        
103
        return  n.getDisplayName ();
80
    }
104
    }
81
105
82
    protected int mode () {
106
    protected int mode () {
(-)test/unit/src/org/openide/actions/SaveActionTest.java (+143 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-2001 Sun
11
 * Microsystems, Inc. All Rights Reserved.
12
 */
13
14
package org.openide.actions;
15
16
import junit.textui.TestRunner;
17
18
import org.openide.filesystems.*;
19
import org.openide.util.Lookup;
20
import java.io.IOException;
21
import java.util.*;
22
import org.netbeans.junit.*;
23
import java.beans.PropertyChangeListener;
24
import javax.swing.Action;
25
26
/** Test basic functionality of data loaders.
27
 * @author Jesse Glick
28
 */
29
public class SaveActionTest extends NbTestCase {
30
    
31
    public SaveActionTest (String name) {
32
        super(name);
33
    }
34
    
35
    public static void main(String[] args) {
36
        TestRunner.run(new NbTestSuite(SaveActionTest.class));
37
    }
38
    
39
    protected void setUp() throws Exception {
40
        System.setProperty ("org.openide.util.Lookup", "org.openide.actions.SaveActionTest$Lkp");
41
        assertNotNull ("MyDisplayer is used", Lookup.getDefault ().lookup (MyStatusDisplayer.class));
42
    }
43
    /*
44
    protected void tearDown() throws Exception {
45
    }
46
     */
47
    
48
    /** Test for bugfix #23065
49
     */
50
    public void testSaveActionTakesNameOfDataNodeIfAvaible() throws Exception {
51
        try {
52
            FileSystem lfs = TestUtilHid.createLocalFileSystem(getName(), new String[] {
53
                "folder/file.simple",
54
            });
55
            FileObject fo = lfs.findResource("folder/file.simple");
56
            assertNotNull(fo);
57
            final org.openide.loaders.DataObject obj = org.openide.loaders.DataObject.find (fo);
58
59
            SaveAction sa = (SaveAction)SaveAction.get (SaveAction.class);
60
            
61
            class MyNode extends org.openide.nodes.FilterNode 
62
            implements org.openide.cookies.SaveCookie {
63
                public int cnt;
64
                
65
                public MyNode () {
66
                    super (obj.getNodeDelegate ());
67
                    disableDelegation (
68
                        org.openide.nodes.FilterNode.DELEGATE_GET_NAME |
69
                        org.openide.nodes.FilterNode.DELEGATE_GET_DISPLAY_NAME |
70
                        org.openide.nodes.FilterNode.DELEGATE_GET_SHORT_DESCRIPTION |
71
                        org.openide.nodes.FilterNode.DELEGATE_SET_NAME |
72
                        org.openide.nodes.FilterNode.DELEGATE_SET_DISPLAY_NAME |
73
                        org.openide.nodes.FilterNode.DELEGATE_SET_SHORT_DESCRIPTION
74
                    );
75
                    
76
                    setName ("my name");
77
                }
78
                
79
                public org.openide.nodes.Node.Cookie getCookie (Class c) {
80
                    if (c.isInstance (this)) {
81
                        return this;
82
                    }
83
                    return super.getCookie (c);
84
                }
85
                
86
                public void save () {
87
                    cnt++;
88
                }
89
            }
90
            
91
            MyNode myNode = new MyNode ();
92
            Action clone = sa.createContextAwareInstance (org.openide.util.lookup.Lookups.singleton (myNode));
93
            
94
            clone.actionPerformed (new java.awt.event.ActionEvent (this, 0, "waitFinished"));
95
            
96
            assertEquals ("Save called", 1, myNode.cnt);
97
            assertEquals ("One msgs", 1, MyStatusDisplayer.cnt);
98
            if (MyStatusDisplayer.text.indexOf ("file.simple") < 0) {
99
                fail ("Wrong message: " + MyStatusDisplayer.text);
100
            }
101
        } finally {
102
            TestUtilHid.destroyLocalFileSystem(getName());
103
        }
104
    }
105
    
106
    public static class Lkp extends org.openide.util.lookup.AbstractLookup {
107
        public Lkp () {
108
            this (new org.openide.util.lookup.InstanceContent ());
109
        }
110
        
111
        private Lkp (org.openide.util.lookup.InstanceContent ic) {
112
            super (ic);
113
            ic.add (new MyStatusDisplayer ());
114
            ic.add (new org.openide.loaders.DataLoaderPool () {
115
                public Enumeration loaders () {
116
                    return org.openide.util.enum.EmptyEnumeration.EMPTY;
117
                }
118
            });
119
        }
120
    }
121
122
    public static class MyStatusDisplayer extends org.openide.awt.StatusDisplayer {
123
        public static int cnt;
124
        public static String text;
125
        
126
        public void addChangeListener (javax.swing.event.ChangeListener l) {
127
        }
128
        
129
        public String getStatusText () {
130
            return text;
131
        }
132
        
133
        public void removeChangeListener (javax.swing.event.ChangeListener l) {
134
        }
135
        
136
        public void setStatusText (String msg) {
137
            cnt++;
138
            text = msg;
139
        }
140
        
141
    }
142
    
143
}

Return to bug 36616