KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > search > internal > ui > CopyToClipboardAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.search.internal.ui;
12
13 import java.util.Collections JavaDoc;
14 import java.util.Iterator JavaDoc;
15
16 import org.eclipse.core.runtime.Assert;
17
18 import org.eclipse.swt.SWTError;
19 import org.eclipse.swt.dnd.Clipboard;
20 import org.eclipse.swt.dnd.DND;
21 import org.eclipse.swt.dnd.TextTransfer;
22 import org.eclipse.swt.dnd.Transfer;
23 import org.eclipse.swt.graphics.Point;
24 import org.eclipse.swt.widgets.Combo;
25 import org.eclipse.swt.widgets.Event;
26 import org.eclipse.swt.widgets.Shell;
27 import org.eclipse.swt.widgets.Text;
28
29 import org.eclipse.jface.action.Action;
30 import org.eclipse.jface.dialogs.MessageDialog;
31 import org.eclipse.jface.viewers.ILabelProvider;
32 import org.eclipse.jface.viewers.ISelection;
33 import org.eclipse.jface.viewers.IStructuredSelection;
34 import org.eclipse.jface.viewers.StructuredViewer;
35
36 import org.eclipse.ui.ISharedImages;
37 import org.eclipse.ui.PlatformUI;
38
39 public class CopyToClipboardAction extends Action {
40
41     private StructuredViewer fViewer;
42     
43     public CopyToClipboardAction(StructuredViewer viewer) {
44         this();
45         Assert.isNotNull(viewer);
46         fViewer= viewer;
47     }
48     
49     public CopyToClipboardAction() {
50         setText(SearchMessages.CopyToClipboardAction_label);
51         setToolTipText(SearchMessages.CopyToClipboardAction_tooltip);
52         ISharedImages workbenchImages= PlatformUI.getWorkbench().getSharedImages();
53         setDisabledImageDescriptor(workbenchImages.getImageDescriptor(ISharedImages.IMG_TOOL_COPY_DISABLED));
54         setImageDescriptor(workbenchImages.getImageDescriptor(ISharedImages.IMG_TOOL_COPY));
55         setHoverImageDescriptor(workbenchImages.getImageDescriptor(ISharedImages.IMG_TOOL_COPY));
56
57     }
58     
59     /**
60      * @param viewer The viewer to set.
61      */

62     public void setViewer(StructuredViewer viewer) {
63         fViewer= viewer;
64     }
65
66     public void runWithEvent(Event event) {
67         // bugzilla 126062: allow combos and text fields of the view to fill
68
// the clipboard
69
Shell shell= SearchPlugin.getActiveWorkbenchShell();
70         if (shell != null) {
71             String JavaDoc sel= null;
72             if (event.widget instanceof Combo) {
73                 Combo combo= (Combo) event.widget;
74                 sel= combo.getText();
75                 Point selection= combo.getSelection();
76                 sel= sel.substring(selection.x, selection.y);
77             }
78             else if (event.widget instanceof Text) {
79                 Text text= (Text) event.widget;
80                 sel= text.getSelectionText();
81             }
82             if (sel != null) {
83                 if (sel.length() > 0) {
84                     copyToClipboard(sel, shell);
85                 }
86                 return;
87             }
88         }
89
90         run();
91     }
92     
93     /*
94      * Implements method from IAction
95      */

96     public void run() {
97         Shell shell= SearchPlugin.getActiveWorkbenchShell();
98         if (shell == null || fViewer == null)
99             return;
100
101         ILabelProvider labelProvider= (ILabelProvider)fViewer.getLabelProvider();
102         String JavaDoc lineDelim= System.getProperty("line.separator"); //$NON-NLS-1$
103
StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
104         Iterator JavaDoc iter= getSelection();
105         while (iter.hasNext()) {
106             if (buf.length() > 0) {
107                 buf.append(lineDelim);
108             }
109             buf.append(labelProvider.getText(iter.next()));
110         }
111         
112         if (buf.length() > 0) {
113             copyToClipboard(buf.toString(), shell);
114         }
115     }
116
117     private void copyToClipboard(String JavaDoc text, Shell shell) {
118         Clipboard clipboard= new Clipboard(shell.getDisplay());
119         try {
120             copyToClipboard(clipboard, text, shell);
121         } finally {
122             clipboard.dispose();
123         }
124     }
125
126     private Iterator JavaDoc getSelection() {
127         ISelection s= fViewer.getSelection();
128         if (s instanceof IStructuredSelection)
129             return ((IStructuredSelection)s).iterator();
130         return Collections.EMPTY_LIST.iterator();
131     }
132
133     private void copyToClipboard(Clipboard clipboard, String JavaDoc str, Shell shell) {
134         try {
135             clipboard.setContents(new String JavaDoc[] { str }, new Transfer[] { TextTransfer.getInstance() });
136         } catch (SWTError ex) {
137             if (ex.code != DND.ERROR_CANNOT_SET_CLIPBOARD)
138                 throw ex;
139             String JavaDoc title= SearchMessages.CopyToClipboardAction_error_title;
140             String JavaDoc message= SearchMessages.CopyToClipboardAction_error_message;
141             if (MessageDialog.openQuestion(shell, title, message))
142                 copyToClipboard(clipboard, str, shell);
143         }
144     }
145 }
146
Popular Tags