KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > infoviews > CopyToClipboardAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.infoviews;
12
13 import org.eclipse.swt.SWTError;
14 import org.eclipse.swt.dnd.Clipboard;
15 import org.eclipse.swt.dnd.DND;
16 import org.eclipse.swt.dnd.TextTransfer;
17 import org.eclipse.swt.dnd.Transfer;
18
19 import org.eclipse.jface.dialogs.MessageDialog;
20
21 import org.eclipse.jface.text.ITextSelection;
22
23 import org.eclipse.ui.ISharedImages;
24 import org.eclipse.ui.IWorkbenchSite;
25 import org.eclipse.ui.PlatformUI;
26 import org.eclipse.ui.texteditor.IAbstractTextEditorHelpContextIds;
27
28 import org.eclipse.jdt.ui.actions.SelectionDispatchAction;
29
30 class CopyToClipboardAction extends SelectionDispatchAction{
31
32     private final static int MAX_REPEAT_COUNT= 10;
33
34     private Clipboard fClipboard;
35
36     public CopyToClipboardAction(IWorkbenchSite site) {
37         super(site);
38
39         setText(InfoViewMessages.CopyAction_label);
40         setToolTipText(InfoViewMessages.CopyAction_tooltip);
41         setDescription(InfoViewMessages.CopyAction_description);
42
43         ISharedImages workbenchImages= PlatformUI.getWorkbench().getSharedImages();
44         setDisabledImageDescriptor(workbenchImages.getImageDescriptor(ISharedImages.IMG_TOOL_COPY_DISABLED));
45         setImageDescriptor(workbenchImages.getImageDescriptor(ISharedImages.IMG_TOOL_COPY));
46         setHoverImageDescriptor(workbenchImages.getImageDescriptor(ISharedImages.IMG_TOOL_COPY));
47
48         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IAbstractTextEditorHelpContextIds.COPY_ACTION);
49
50         update(getSelection());
51     }
52
53     public void selectionChanged(ITextSelection selection) {
54         setEnabled(selection != null && selection.getLength() > 0);
55     }
56
57     public void run(ITextSelection selection) {
58         fClipboard= new Clipboard(getShell().getDisplay());
59         try {
60             copyToClipboard(selection, 0);
61         } finally {
62             fClipboard.dispose();
63         }
64     }
65
66     private void copyToClipboard(ITextSelection selection, int repeatCount) {
67         try{
68             fClipboard.setContents(new String JavaDoc[] { selection.getText() }, new Transfer[] { TextTransfer.getInstance() });
69         } catch (SWTError e) {
70             if (e.code != DND.ERROR_CANNOT_SET_CLIPBOARD || repeatCount >= MAX_REPEAT_COUNT)
71                 throw e;
72
73             if (MessageDialog.openQuestion(getShell(), InfoViewMessages.CopyToClipboard_error_title, InfoViewMessages.CopyToClipboard_error_message))
74                 copyToClipboard(selection, repeatCount + 1);
75         }
76     }
77 }
78
Popular Tags