1 11 package org.eclipse.search.internal.ui; 12 13 import java.util.Collections ; 14 import java.util.Iterator ; 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 62 public void setViewer(StructuredViewer viewer) { 63 fViewer= viewer; 64 } 65 66 public void runWithEvent(Event event) { 67 Shell shell= SearchPlugin.getActiveWorkbenchShell(); 70 if (shell != null) { 71 String 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 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 lineDelim= System.getProperty("line.separator"); StringBuffer buf= new StringBuffer (); 104 Iterator 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 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 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 str, Shell shell) { 134 try { 135 clipboard.setContents(new String [] { str }, new Transfer[] { TextTransfer.getInstance() }); 136 } catch (SWTError ex) { 137 if (ex.code != DND.ERROR_CANNOT_SET_CLIPBOARD) 138 throw ex; 139 String title= SearchMessages.CopyToClipboardAction_error_title; 140 String message= SearchMessages.CopyToClipboardAction_error_message; 141 if (MessageDialog.openQuestion(shell, title, message)) 142 copyToClipboard(clipboard, str, shell); 143 } 144 } 145 } 146 | Popular Tags |