Lines 36-42
Link Here
|
36 |
public void testActionClone () throws Exception { |
36 |
public void testActionClone () throws Exception { |
37 |
CloneAction original = new CloneAction (); |
37 |
CloneAction original = new CloneAction (); |
38 |
|
38 |
|
39 |
javax.swing.Action clone = org.openide.util.Utilities.actionClone (original, Lookup.EMPTY); |
39 |
javax.swing.Action clone = original.createContextAwareInstance(Lookup.EMPTY); |
40 |
|
40 |
|
41 |
assertTrue ("Clone is instance of desired class", clone instanceof CloneAction); |
41 |
assertTrue ("Clone is instance of desired class", clone instanceof CloneAction); |
42 |
assertNull ("Original has empty lookup", original.lookup); |
42 |
assertNull ("Original has empty lookup", original.lookup); |
Lines 50-56
Link Here
|
50 |
public JMenuItem item = new JMenuItem ("Ahoj"); |
50 |
public JMenuItem item = new JMenuItem ("Ahoj"); |
51 |
public int called; |
51 |
public int called; |
52 |
|
52 |
|
53 |
protected Action cloneAction () { |
53 |
protected CloneAction cloneAction () { |
54 |
return new MyAction (); |
54 |
return new MyAction (); |
55 |
} |
55 |
} |
56 |
|
56 |
|
Lines 83-93
Link Here
|
83 |
} |
83 |
} |
84 |
|
84 |
|
85 |
public void testActionsToPopupFromComponent () throws Exception { |
85 |
public void testActionsToPopupFromComponent () throws Exception { |
86 |
final org.openide.util.Lookup lookup = org.openide.util.lookup.Lookups.singleton (this); |
86 |
Lookup lookup = org.openide.util.lookup.Lookups.singleton (this); |
|
|
87 |
final Lookup[] returnedLookup = {lookup}; |
87 |
|
88 |
|
88 |
class Provider extends javax.swing.JComponent implements Lookup.Provider { |
89 |
class Provider extends javax.swing.JComponent implements Lookup.Provider { |
89 |
public Lookup getLookup () { |
90 |
public Lookup getLookup () { |
90 |
return lookup; |
91 |
return returnedLookup[0]; |
91 |
} |
92 |
} |
92 |
} |
93 |
} |
93 |
|
94 |
|
Lines 117-127
Link Here
|
117 |
|
118 |
|
118 |
javax.swing.JPopupMenu menu = org.openide.util.Utilities.actionsToPopup (arr, menuOwner); |
119 |
javax.swing.JPopupMenu menu = org.openide.util.Utilities.actionsToPopup (arr, menuOwner); |
119 |
|
120 |
|
120 |
Lookup actionLookup = ((CloneAction)arr[0].clone).lookup; |
121 |
Lookup actionLookup = arr[0].clone.lookup; |
|
|
122 |
assertNotNull("Clone lookup is not null", actionLookup); |
121 |
assertEquals ("The lookup returned by 'provider' is assigned to the clonned actions", |
123 |
assertEquals ("The lookup returned by 'provider' is assigned to the clonned actions", |
122 |
this, actionLookup.lookup (this.getClass ()) |
124 |
this, actionLookup.lookup (this.getClass ()) |
123 |
); |
125 |
); |
124 |
|
126 |
|
|
|
127 |
// Now try it without a (valid) Lookup.Provider in the component hierarchy. |
128 |
returnedLookup[0] = null; |
129 |
arr[0].clone = null; |
130 |
menu = org.openide.util.Utilities.actionsToPopup (arr, menuOwner); |
131 |
|
132 |
actionLookup = arr[0].clone.lookup; |
133 |
assertNotNull("Clone lookup is not null", actionLookup); |
134 |
|
125 |
javax.swing.ActionMap map = (javax.swing.ActionMap)actionLookup.lookup (javax.swing.ActionMap.class); |
135 |
javax.swing.ActionMap map = (javax.swing.ActionMap)actionLookup.lookup (javax.swing.ActionMap.class); |
126 |
assertNotNull ("Action map is in the lookup", map); |
136 |
assertNotNull ("Action map is in the lookup", map); |
127 |
assertNull ("ActionMap of parent of Lookup.Provider is not included", map.get ("1")); |
137 |
assertNull ("ActionMap of parent of Lookup.Provider is not included", map.get ("1")); |
Lines 131-164
Link Here
|
131 |
assertNull ("But of course the sibling's one is not", map.get ("5")); |
141 |
assertNull ("But of course the sibling's one is not", map.get ("5")); |
132 |
} |
142 |
} |
133 |
|
143 |
|
134 |
class CloneAction extends javax.swing.AbstractAction { |
144 |
class CloneAction extends javax.swing.AbstractAction implements ContextAwareAction { |
135 |
public Lookup lookup; |
145 |
public Lookup lookup; |
136 |
public Action clone; |
146 |
public CloneAction clone; |
137 |
|
147 |
|
138 |
public void actionPerformed (java.awt.event.ActionEvent ev) { |
148 |
public void actionPerformed (java.awt.event.ActionEvent ev) { |
139 |
} |
149 |
} |
140 |
|
150 |
|
141 |
protected Action cloneAction () { |
151 |
protected CloneAction cloneAction () { |
142 |
return new CloneAction (); |
152 |
return new CloneAction (); |
143 |
} |
153 |
} |
144 |
|
154 |
|
145 |
public void putValue (String name, Object value) { |
155 |
public Action createContextAwareInstance(Lookup actionContext) { |
146 |
if (name.equals ("lookup")) { |
156 |
assertNull ("We support just one clone right now", clone); |
147 |
assertNull ("Not yet assigned", lookup); |
157 |
clone = cloneAction (); |
148 |
lookup = (Lookup)value; |
158 |
clone.lookup = actionContext; |
149 |
return; |
159 |
return clone; |
150 |
} |
|
|
151 |
super.putValue (name, value); |
152 |
} |
153 |
|
154 |
public Object getValue (String name) { |
155 |
if (name.equals ("actionClone")) { |
156 |
assertNull ("We support just one clone right now", clone); |
157 |
clone = cloneAction (); |
158 |
return clone; |
159 |
} |
160 |
return super.getValue (name); |
161 |
} |
160 |
} |
|
|
161 |
|
162 |
} // end of CloneAction |
162 |
} // end of CloneAction |
163 |
|
163 |
|
164 |
} |
164 |
} |