1 11 12 package org.eclipse.ui.views.properties; 13 14 import org.eclipse.jface.dialogs.MessageDialog; 15 import org.eclipse.jface.viewers.IStructuredSelection; 16 import org.eclipse.swt.SWTError; 17 import org.eclipse.swt.dnd.Clipboard; 18 import org.eclipse.swt.dnd.DND; 19 import org.eclipse.swt.dnd.TextTransfer; 20 import org.eclipse.swt.dnd.Transfer; 21 import org.eclipse.ui.PlatformUI; 22 import org.eclipse.ui.internal.views.properties.PropertiesMessages; 23 24 27 class CopyPropertyAction extends PropertySheetAction { 28 31 private Clipboard clipboard; 32 33 40 public CopyPropertyAction(PropertySheetViewer viewer, String name, 41 Clipboard clipboard) { 42 super(viewer, name); 43 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, 44 IPropertiesHelpContextIds.COPY_PROPERTY_ACTION); 45 this.clipboard = clipboard; 46 } 47 48 51 public void run() { 52 IStructuredSelection selection = (IStructuredSelection) getPropertySheet() 54 .getSelection(); 55 if (selection.isEmpty()) { 56 return; 57 } 58 IPropertySheetEntry entry = (IPropertySheetEntry) selection 60 .getFirstElement(); 61 62 StringBuffer buffer = new StringBuffer (); 64 buffer.append(entry.getDisplayName()); 65 buffer.append("\t"); buffer.append(entry.getValueAsString()); 67 68 setClipboard(buffer.toString()); 69 } 70 71 76 public void selectionChanged(IStructuredSelection sel) { 77 setEnabled(!sel.isEmpty()); 78 } 79 80 private void setClipboard(String text) { 81 try { 82 Object [] data = new Object [] { text }; 83 Transfer[] transferTypes = new Transfer[] { TextTransfer 84 .getInstance() }; 85 clipboard.setContents(data, transferTypes); 86 } catch (SWTError e) { 87 if (e.code != DND.ERROR_CANNOT_SET_CLIPBOARD) { 88 throw e; 89 } 90 if (MessageDialog.openQuestion(getPropertySheet().getControl() 91 .getShell(), PropertiesMessages.CopyToClipboardProblemDialog_title, 92 PropertiesMessages.CopyToClipboardProblemDialog_message)) { 93 setClipboard(text); 94 } 95 } 96 } 97 } 98 99 | Popular Tags |