KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > actions > PasteAction


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.openide.actions;
21
22 import java.awt.MenuShortcut JavaDoc;
23 import java.awt.datatransfer.Clipboard JavaDoc;
24 import java.awt.datatransfer.ClipboardOwner JavaDoc;
25 import java.awt.datatransfer.StringSelection JavaDoc;
26 import java.awt.datatransfer.Transferable JavaDoc;
27 import java.awt.event.ActionEvent JavaDoc;
28 import java.beans.PropertyChangeEvent JavaDoc;
29 import java.beans.PropertyChangeListener JavaDoc;
30 import java.beans.PropertyVetoException JavaDoc;
31 import java.util.ArrayList JavaDoc;
32 import java.util.Arrays JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.logging.Level JavaDoc;
35 import java.util.logging.Logger JavaDoc;
36 import javax.swing.Action JavaDoc;
37 import javax.swing.ActionMap JavaDoc;
38 import javax.swing.event.EventListenerList JavaDoc;
39 import org.openide.awt.Actions;
40 import org.openide.explorer.ExplorerManager;
41 import org.openide.nodes.Node;
42 import org.openide.nodes.NodeEvent;
43 import org.openide.nodes.NodeListener;
44 import org.openide.nodes.NodeMemberEvent;
45 import org.openide.nodes.NodeReorderEvent;
46 import org.openide.util.Exceptions;
47 import org.openide.util.HelpCtx;
48 import org.openide.util.Lookup;
49 import org.openide.util.LookupListener;
50 import org.openide.util.NbBundle;
51 import org.openide.util.UserCancelException;
52 import org.openide.util.WeakListeners;
53 import org.openide.util.actions.CallbackSystemAction;
54 import org.openide.util.actions.Presenter;
55 import org.openide.util.datatransfer.PasteType;
56 import org.openide.windows.TopComponent;
57
58 /** Paste from clipboard. This is a callback system action,
59 * with enhanced behaviour. Others can plug in by adding
60 * <PRE>
61 * topcomponent.getActionMap ().put (javax.swing.text.DefaultEditorKit.pasteAction, theActualAction);
62 * </PRE>
63 * or by using the now deprecated <code>setPasteTypes</code> and <code>setActionPerformer</code>
64 * methods.
65 * <P>
66 * There is a special support for more than one type of paste to be enabled at once.
67 * If the <code>theActualAction</code> returns array of actions from
68 * <code>getValue ("delegates")</code> than those actions are offered as
69 * subelements by the paste action presenter.
70 */

71 public final class PasteAction extends CallbackSystemAction {
72     /** Imlementation of ActSubMenuInt */
73     private static ActSubMenuModel globalModel;
74
75     /** All currently possible paste types. */
76     private static PasteType[] types;
77
78     /** Lazy initializtion of the global model */
79     private static synchronized ActSubMenuModel model() {
80         if (globalModel == null) {
81             globalModel = new ActSubMenuModel(null);
82         }
83
84         return globalModel;
85     }
86
87     protected void initialize() {
88         super.initialize();
89
90         setEnabled(false);
91     }
92
93     public String JavaDoc getName() {
94         return NbBundle.getMessage(PasteAction.class, "Paste");
95     }
96
97     public HelpCtx getHelpCtx() {
98         return new HelpCtx(PasteAction.class);
99     }
100
101     protected String JavaDoc iconResource() {
102         return "org/openide/resources/actions/paste.gif"; // NOI18N
103
}
104
105     public javax.swing.JMenuItem JavaDoc getMenuPresenter() {
106         return new Actions.SubMenu(this, model(), false);
107     }
108
109     public javax.swing.JMenuItem JavaDoc getPopupPresenter() {
110         return new Actions.SubMenu(this, model(), true);
111     }
112
113     public Action JavaDoc createContextAwareInstance(Lookup actionContext) {
114         return new DelegateAction(this, actionContext);
115     }
116
117     public Object JavaDoc getActionMapKey() {
118         return javax.swing.text.DefaultEditorKit.pasteAction;
119     }
120
121     public void actionPerformed(java.awt.event.ActionEvent JavaDoc ev) {
122         PasteType t;
123
124         if (ev.getSource() instanceof PasteType) {
125             t = (PasteType) ev.getSource();
126         } else {
127             PasteType[] arr = getPasteTypes();
128
129             if ((arr != null) && (arr.length > 0)) {
130                 t = arr[0];
131             } else {
132                 t = null;
133             }
134         }
135
136         if (t == null) {
137             // Try to find paste action 'performer' from activated TopComponent.
138
Action JavaDoc ac = findActionFromActivatedTopComponentMap();
139
140             if (ac != null) {
141                 // XXX Hack to get paste types from action 'performer',
142
// which in fact doesn't perform the paste.
143
// Look at ExplorerActions.OwnPaste#getValue method.
144
Object JavaDoc obj = ac.getValue("delegates"); // NOI18N
145

146                 if (obj instanceof PasteType []) {
147                     PasteType [] arr = (PasteType []) obj;
148                     if (arr.length > 0) {
149                         t = arr [0];
150                     }
151                 } else if (obj instanceof Action JavaDoc []) {
152                     Action JavaDoc [] arr = (Action JavaDoc []) obj;
153                     if (arr.length > 0) {
154                         arr [0].actionPerformed (ev);
155                     }
156                 } else {
157                     ac.actionPerformed(ev);
158
159                     return;
160                 }
161             }
162         }
163
164         if (t != null) {
165             // posts the action in RP thread
166
new ActionPT(t, ev.getActionCommand());
167         } else {
168             Logger.getLogger(PasteAction.class.getName()).log(Level.WARNING, null,
169                               new IllegalStateException JavaDoc("No paste types available when performing paste action")); // NOI18N
170
}
171     }
172
173     protected boolean asynchronous() {
174         return false;
175     }
176
177     /** Set possible paste types.
178     * Automatically enables or disables the paste action according to whether there are any.
179     * @deprecated Use <code>TopComponent.getActionMap ().put (javax.swing.text.DefaultEditorKit.pasteAction, yourPasteAction);</code>
180     * If you want register more paste types then use an action which delegates to
181     * an array of <code>PasteAction</code> or also can delegate to an array of
182      * <code>org.openide.util.datatransfer.PasteType</code>.
183     * @param types the new types to allow, or <code>null</code>
184     */

185     public void setPasteTypes(PasteType[] types) {
186         this.types = types;
187
188         if ((types == null) || (types.length == 0)) {
189             setEnabled(false);
190         } else {
191             setEnabled(true);
192         }
193
194         model().checkStateChanged(true);
195     }
196
197     /** Get all paste types.
198      * @return all possible paste types, or <code>null</code> */

199     public PasteType[] getPasteTypes() {
200         return types;
201     }
202
203     /** Finds paste action from currently activated TopComponent's action map. */
204     private static Action JavaDoc findActionFromActivatedTopComponentMap() {
205         TopComponent tc = TopComponent.getRegistry().getActivated();
206
207         if (tc != null) {
208             ActionMap JavaDoc map = tc.getActionMap();
209
210             return findActionFromMap(map);
211         }
212
213         return null;
214     }
215
216     /** Finds paste action from provided map. */
217     private static javax.swing.Action JavaDoc findActionFromMap(ActionMap JavaDoc map) {
218         if (map != null) {
219             return map.get(javax.swing.text.DefaultEditorKit.pasteAction);
220         }
221
222         return null;
223     }
224
225     /** If our clipboard is not found return the default system clipboard. */
226     private static Clipboard JavaDoc getClipboard() {
227         Clipboard JavaDoc c = (java.awt.datatransfer.Clipboard JavaDoc) org.openide.util.Lookup.getDefault().lookup(
228                 java.awt.datatransfer.Clipboard JavaDoc.class
229             );
230
231         if (c == null) {
232             c = java.awt.Toolkit.getDefaultToolkit().getSystemClipboard();
233         }
234
235         return c;
236     }
237
238     /** Utilitity method for finding the currently selected explorer manager.
239      * it uses reflection because it should work without
240      * the rest of the IDE classes.
241      *
242      * @return current explorer manager or null
243      */

244     static ExplorerManager findExplorerManager() {
245         Throwable JavaDoc t = null;
246
247         try {
248             Class JavaDoc c = Class.forName("org.openide.windows.TopComponent"); // NOI18N
249

250             // use reflection now
251
java.lang.reflect.Method JavaDoc m = c.getMethod("getRegistry", // NOI18N
252
new Class JavaDoc[0]
253                 );
254             Object JavaDoc o = m.invoke(null, new Object JavaDoc[0]);
255
256             c = Class.forName("org.openide.windows.TopComponent$Registry"); // NOI18N
257

258             // use reflection now
259
m = c.getMethod("getActivated", // NOI18N
260
new Class JavaDoc[0]
261                 );
262             o = m.invoke(o, new Object JavaDoc[0]);
263
264             if (o instanceof ExplorerManager.Provider) {
265                 return ((ExplorerManager.Provider) o).getExplorerManager();
266             }
267         }
268         // exceptions from forName:
269
catch (ClassNotFoundException JavaDoc x) {
270         } catch (ExceptionInInitializerError JavaDoc x) {
271         } catch (LinkageError JavaDoc x) {
272         }
273         // exceptions from getMethod:
274
catch (SecurityException JavaDoc x) {
275             t = x;
276         } catch (NoSuchMethodException JavaDoc x) {
277             t = x;
278         }
279         // exceptions from invoke
280
catch (IllegalAccessException JavaDoc x) {
281             t = x;
282         } catch (IllegalArgumentException JavaDoc x) {
283             t = x;
284         } catch (java.lang.reflect.InvocationTargetException JavaDoc x) {
285             t = x;
286         }
287
288         if (t != null) {
289             Logger.getLogger(PasteAction.class.getName()).log(Level.WARNING, null, t);
290         }
291
292         return null;
293     }
294
295     /** General implementation of Actions.SubMenuModel that works
296      * with provided lookup or without it. With lookup it attaches
297      * to changes in the lookup and updates its state according to
298      * it. Without it listens on TopComponent.getActivated() and
299      * works with it.
300      */

301     private static class ActSubMenuModel extends EventListenerList JavaDoc implements Actions.SubMenuModel, LookupListener,
302         PropertyChangeListener JavaDoc {
303         /** lookup we are attached to or null we we should work globally */
304         private Lookup.Result result;
305
306         /** previous enabled state */
307         private boolean enabled;
308
309         /** weak listener for action */
310         private PropertyChangeListener JavaDoc actionWeakL;
311
312         /** weak listener for paste type */
313         private PropertyChangeListener JavaDoc pasteTypeWeakL;
314
315         /** weak lookup listener */
316         private LookupListener weakLookup;
317
318         /** @param lookup can be null */
319         public ActSubMenuModel(Lookup lookup) {
320             attachListenerToChangesInMap(lookup);
321         }
322
323         /** Finds appropriate map to work with.
324          * @return map from lookup or from activated TopComponent, null no available
325          */

326         private ActionMap JavaDoc map() {
327             if (result == null) {
328                 org.openide.windows.TopComponent tc = org.openide.windows.TopComponent.getRegistry().getActivated();
329
330                 if (tc != null) {
331                     return tc.getActionMap();
332                 }
333             } else {
334                 java.util.Iterator JavaDoc it = result.allItems().iterator();
335
336                 while (it.hasNext()) {
337                     Object JavaDoc o = ((Lookup.Item) it.next()).getInstance();
338
339                     if (o instanceof ActionMap JavaDoc) {
340                         return (ActionMap JavaDoc) o;
341                     }
342                 }
343             }
344
345             return null;
346         }
347
348         /** Adds itself as a listener for changes in current ActionMap.
349          * If the lookup is null then it means to listen on TopComponent
350          * otherwise to listen on the lookup itself.
351          *
352          * @param lookup lookup to listen on or null
353          */

354         private void attachListenerToChangesInMap(Lookup lookup) {
355             if (lookup == null) {
356                 org.openide.windows.TopComponent.getRegistry().addPropertyChangeListener(
357                     org.openide.util.WeakListeners.propertyChange(this, org.openide.windows.TopComponent.getRegistry())
358                 );
359             } else {
360                 result = lookup.lookupResult(ActionMap JavaDoc.class);
361                 weakLookup = (LookupListener) WeakListeners.create(LookupListener.class, this, result);
362                 result.addLookupListener(weakLookup);
363             }
364
365             checkStateChanged(false);
366         }
367
368         /** Finds the currently active items this method should delegate to.
369          * For historical reasons one can use PasteType by PasteAction.setPasteTypes
370          * in the new implementation it is expected that such paste types
371          * will be replaced by Actions (obtained from getValue("delegates")).
372          *
373          *
374          * @param actionToWorkWith array of size 1 or null. Will be filled
375          * with action that we actually delegate to (either the global or local
376          * found in action map)
377          * @return array of either PasteTypes or Actions
378          */

379         private Object JavaDoc[] getPasteTypesOrActions(Action JavaDoc[] actionToWorkWith) {
380             Action JavaDoc x = findActionFromMap(map());
381
382             if (x == null) {
383                 // No context action use the global one.
384
PasteAction a = (PasteAction) findObject(PasteAction.class);
385
386                 if (actionToWorkWith != null) {
387                     actionToWorkWith[0] = a;
388                 }
389
390                 Object JavaDoc[] arr = a.getPasteTypes();
391
392                 if (arr != null) {
393                     return arr;
394                 } else {
395                     return new Object JavaDoc[0];
396                 }
397             }
398
399             if (actionToWorkWith != null) {
400                 actionToWorkWith[0] = x;
401             }
402
403             Object JavaDoc obj = x.getValue("delegates"); // NOI18N
404

405             if (obj instanceof Object JavaDoc[]) {
406                 return (Object JavaDoc[]) obj;
407             } else {
408                 return new Object JavaDoc[] { x };
409             }
410         }
411
412         public boolean isEnabled() {
413             Object JavaDoc[] arr = getPasteTypesOrActions(null);
414
415             if ((arr.length == 1) && arr[0] instanceof Action JavaDoc) {
416                 return ((Action JavaDoc) arr[0]).isEnabled();
417             } else {
418                 return arr.length > 0;
419             }
420         }
421
422         public int getCount() {
423             return getPasteTypesOrActions(null).length;
424         }
425
426         public String JavaDoc getLabel(int index) {
427             Object JavaDoc[] arr = getPasteTypesOrActions(null);
428
429             if (arr.length <= index) {
430                 return null;
431             }
432
433             if (arr[index] instanceof PasteType) {
434                 return ((PasteType) arr[index]).getName();
435             } else {
436                 // is Action
437
return (String JavaDoc) ((Action JavaDoc) arr[index]).getValue(Action.NAME);
438             }
439         }
440
441         public HelpCtx getHelpCtx(int index) {
442             Object JavaDoc[] arr = getPasteTypesOrActions(null);
443
444             if (arr.length <= index) {
445                 return null;
446             }
447
448             if (arr[index] instanceof PasteType) {
449                 return ((PasteType) arr[index]).getHelpCtx();
450             } else {
451                 // is action
452
Object JavaDoc helpID = ((Action JavaDoc) arr[index]).getValue("helpID"); // NOI18N
453

454                 if (helpID instanceof String JavaDoc) {
455                     return new HelpCtx((String JavaDoc) helpID);
456                 } else {
457                     return null;
458                 }
459             }
460         }
461
462         public MenuShortcut JavaDoc getMenuShortcut(int index) {
463             return null;
464         }
465
466         public void performActionAt(int index) {
467             performActionAt(index, null);
468         }
469
470         public void performActionAt(int index, ActionEvent JavaDoc ev) {
471             Action JavaDoc[] action = new Action JavaDoc[1];
472
473             Object JavaDoc[] arr = getPasteTypesOrActions(action);
474
475             if (arr.length <= index) {
476                 return;
477             }
478
479             if (arr[index] instanceof PasteType) {
480                 PasteType t = (PasteType) arr[index];
481
482                 // posts the action is RP thread
483
new ActionPT(t, (ev == null) ? null : ev.getActionCommand());
484
485                 return;
486             } else {
487                 // is action
488
Action JavaDoc a = (Action JavaDoc) arr[index];
489                 a.actionPerformed(new ActionEvent JavaDoc(a, ActionEvent.ACTION_PERFORMED, a.NAME));
490
491                 return;
492             }
493         }
494
495         /** Registers .ChangeListener to receive events.
496          *@param listener The listener to register.
497          */

498         public synchronized void addChangeListener(javax.swing.event.ChangeListener JavaDoc listener) {
499             add(javax.swing.event.ChangeListener JavaDoc.class, listener);
500         }
501
502         /** Removes .ChangeListener from the list of listeners.
503          *@param listener The listener to remove.
504          */

505         public synchronized void removeChangeListener(javax.swing.event.ChangeListener JavaDoc listener) {
506             remove(javax.swing.event.ChangeListener JavaDoc.class, listener);
507         }
508
509         /** Notifies all registered listeners about the event.
510          *
511          *@param param1 Parameter #1 of the <CODE>.ChangeEvent<CODE> constructor.
512          */

513         protected void checkStateChanged(boolean fire) {
514             Action JavaDoc[] listen = new Action JavaDoc[1];
515             Object JavaDoc[] arr = getPasteTypesOrActions(listen);
516
517             Action JavaDoc a = null;
518
519             if ((arr.length == 1) && arr[0] instanceof Action JavaDoc) {
520                 a = (Action JavaDoc) arr[0];
521                 a.removePropertyChangeListener(pasteTypeWeakL);
522                 pasteTypeWeakL = WeakListeners.propertyChange(this, a);
523                 a.addPropertyChangeListener(pasteTypeWeakL);
524             }
525
526             // plus always make sure we are listening on the actions
527
if (listen[0] != a) {
528                 listen[0].removePropertyChangeListener(actionWeakL);
529                 actionWeakL = WeakListeners.propertyChange(this, listen[0]);
530                 listen[0].addPropertyChangeListener(actionWeakL);
531             }
532
533             boolean en = isEnabled();
534
535             if (en == enabled) {
536                 return;
537             }
538
539             enabled = en;
540
541             // and fire if requested....
542
if (!fire) {
543                 return;
544             }
545
546             Object JavaDoc[] listeners = getListenerList();
547
548             if (listeners.length == 0) {
549                 return;
550             }
551
552             javax.swing.event.ChangeEvent JavaDoc e = new javax.swing.event.ChangeEvent JavaDoc(this);
553
554             for (int i = listeners.length - 1; i >= 0; i -= 2) {
555                 ((javax.swing.event.ChangeListener JavaDoc) listeners[i]).stateChanged(e);
556             }
557         }
558
559         public void propertyChange(java.beans.PropertyChangeEvent JavaDoc evt) {
560             checkStateChanged(true);
561         }
562
563         public void resultChanged(org.openide.util.LookupEvent ev) {
564             checkStateChanged(true);
565         }
566     }
567
568     /** Class that listens on a given node and when invoked listen on changes
569      * and after that tries to select the desired node.
570      */

571     static final class NodeSelector extends Object JavaDoc implements NodeListener, Runnable JavaDoc {
572         /** All added children */
573         private List JavaDoc added;
574
575         /** node we are listening to */
576         private Node node;
577
578         /** manager to work with */
579         private ExplorerManager em;
580
581         /** children */
582         private Node[] children;
583
584         /** @param em explorer manager to work with
585          * @param n nodes to attach to or null if em's nodes should be used
586          */

587         public NodeSelector(ExplorerManager em, Node[] n) {
588             this.em = em;
589
590             if ((n != null) && (n.length > 0)) {
591                 this.node = n[0];
592             } else {
593                 Node[] arr = em.getSelectedNodes();
594
595                 if (arr.length != 0) {
596                     this.node = arr[0];
597                 } else {
598                     // do not initialize
599
return;
600                 }
601             }
602
603             this.children = node.getChildren().getNodes(true);
604
605             this.added = new ArrayList JavaDoc();
606             this.node.addNodeListener(this);
607         }
608
609         /** Selects the added nodes */
610         public void select() {
611             if (added != null) {
612                 // if initialized => wait till finished update
613
node.getChildren().getNodes(true);
614
615                 // and select the right nodes
616
org.openide.nodes.Children.MUTEX.readAccess(this);
617             }
618         }
619
620         public void run() {
621             this.node.removeNodeListener(this);
622
623             if (added.isEmpty()) {
624                 return;
625             }
626
627             Node[] arr = (Node[]) added.toArray(new Node[added.size()]);
628
629
630 // bugfix #22698, don't select the added nodes
631
// when the nodes not under managed explorer's root node
632
bigloop:
633             for (int i = 0; i < arr.length; i++) {
634                 Node node = arr[i];
635
636                 while (node != null) {
637                     if (node.equals(em.getRootContext())) {
638                         continue bigloop;
639                     }
640
641                     node = node.getParentNode();
642                 }
643
644                 return;
645             }
646
647             try {
648                 em.setSelectedNodes(arr);
649             } catch (PropertyVetoException JavaDoc ex) {
650                 Logger.getLogger(PasteAction.class.getName()).log(Level.WARNING, null, ex);
651             } catch (IllegalStateException JavaDoc ex) {
652                 Logger.getLogger(PasteAction.class.getName()).log(Level.WARNING, null, ex);
653             }
654         }
655
656         /** Fired when a set of new children is added.
657          * @param ev event describing the action
658          */

659         public void childrenAdded(NodeMemberEvent ev) {
660             added.addAll(Arrays.asList(ev.getDelta()));
661         }
662
663         /** Fired when a set of children is removed.
664          * @param ev event describing the action
665          */

666         public void childrenRemoved(NodeMemberEvent ev) {
667         }
668
669         /** Fired when the order of children is changed.
670          * @param ev event describing the change
671          */

672         public void childrenReordered(NodeReorderEvent ev) {
673         }
674
675         /** Fired when the node is deleted.
676          * @param ev event describing the node
677          */

678         public void nodeDestroyed(NodeEvent ev) {
679         }
680
681         /** This method gets called when a bound property is changed.
682          * @param evt A PropertyChangeEvent object describing the event source
683          * and the property that has changed.
684          */

685         public void propertyChange(PropertyChangeEvent JavaDoc evt) {
686         }
687     }
688      // end of NodeSelector
689

690     /** A delegate action that is usually associated with a specific lookup and
691      * extract the nodes it operates on from it. Otherwise it delegates to the
692      * regular NodeAction.
693      */

694     private static final class DelegateAction extends javax.swing.AbstractAction JavaDoc implements Presenter.Menu,
695         Presenter.Popup, Presenter.Toolbar, javax.swing.event.ChangeListener JavaDoc {
696         /** action to delegate too */
697         private PasteAction delegate;
698
699         /** model to work with */
700         private ActSubMenuModel model;
701
702         public DelegateAction(PasteAction a, Lookup actionContext) {
703             this.delegate = a;
704             this.model = new ActSubMenuModel(actionContext);
705             this.model.addChangeListener(this);
706         }
707
708         /** Overrides superclass method, adds delegate description. */
709         public String JavaDoc toString() {
710             return super.toString() + "[delegate=" + delegate + "]"; // NOI18N
711
}
712
713         public void putValue(String JavaDoc key, Object JavaDoc value) {
714         }
715
716         /** Invoked when an action occurs.
717          */

718         public void actionPerformed(java.awt.event.ActionEvent JavaDoc e) {
719             if (model != null) {
720                 model.performActionAt(0, e);
721             }
722         }
723
724         public boolean isEnabled() {
725             return (model != null) && model.isEnabled();
726         }
727
728         public Object JavaDoc getValue(String JavaDoc key) {
729             return delegate.getValue(key);
730         }
731
732         public void setEnabled(boolean b) {
733         }
734
735         public javax.swing.JMenuItem JavaDoc getMenuPresenter() {
736             return new org.openide.awt.Actions.SubMenu(this, model, false);
737         }
738
739         public javax.swing.JMenuItem JavaDoc getPopupPresenter() {
740             return new org.openide.awt.Actions.SubMenu(this, model, true);
741         }
742
743         public java.awt.Component JavaDoc getToolbarPresenter() {
744             return new Actions.ToolbarButton(this);
745         }
746
747         public void stateChanged(javax.swing.event.ChangeEvent JavaDoc evt) {
748             super.firePropertyChange("enabled", null, null);
749         }
750     }
751      // end of DelegateAction
752

753     /** Action that wraps paste type.
754      */

755     private static final class ActionPT extends javax.swing.AbstractAction JavaDoc implements Runnable JavaDoc {
756         private PasteType t;
757         private NodeSelector sel;
758         private boolean secondInvocation;
759
760         public ActionPT(PasteType t, String JavaDoc command) {
761             this.t = t;
762
763             ExplorerManager em = findExplorerManager();
764
765             if (em != null) {
766                 this.sel = new NodeSelector(em, null);
767             }
768
769             if ("waitFinished".equals(command)) { // NOI18N
770
run();
771             } else {
772                 org.openide.util.RequestProcessor.getDefault().post(this);
773             }
774         }
775
776         public void actionPerformed(java.awt.event.ActionEvent JavaDoc ev) {
777             try {
778                 Transferable JavaDoc trans = t.paste();
779                 Clipboard JavaDoc clipboard = getClipboard();
780
781                 if (trans != null) {
782                     ClipboardOwner JavaDoc owner = (trans instanceof ClipboardOwner JavaDoc) ? (ClipboardOwner JavaDoc) trans
783                                                                              : new StringSelection JavaDoc(""); // NOI18N
784
clipboard.setContents(trans, owner);
785                 }
786             } catch (UserCancelException exc) {
787                 // ignore - user just pressed cancel in some dialog....
788
} catch (java.io.IOException JavaDoc e) {
789                 Exceptions.printStackTrace(e);
790             } finally {
791                 javax.swing.SwingUtilities.invokeLater(this);
792             }
793         }
794
795         public void run() {
796             if (secondInvocation) {
797                 if (sel != null) {
798                     sel.select();
799                 }
800             } else {
801                 secondInvocation = true;
802                 ActionManager.getDefault().invokeAction(
803                     this, new ActionEvent JavaDoc(t, ActionEvent.ACTION_PERFORMED, javax.swing.Action.NAME)
804                 );
805             }
806         }
807
808         public boolean isEnabled() {
809             return ((PasteAction) PasteAction.get(PasteAction.class)).isEnabled();
810         }
811
812         public Object JavaDoc getValue(String JavaDoc key) {
813             return ((PasteAction) PasteAction.get(PasteAction.class)).getValue(key);
814         }
815     }
816 }
817
Popular Tags