1 /*******************************************************************************
2  * Copyright (c) 2000, 2015 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 
15 package org.eclipse.ui.views.properties;
16 
17 import org.eclipse.jface.dialogs.MessageDialog;
18 import org.eclipse.jface.viewers.IStructuredSelection;
19 import org.eclipse.swt.SWTError;
20 import org.eclipse.swt.dnd.Clipboard;
21 import org.eclipse.swt.dnd.DND;
22 import org.eclipse.swt.dnd.TextTransfer;
23 import org.eclipse.swt.dnd.Transfer;
24 import org.eclipse.ui.PlatformUI;
25 import org.eclipse.ui.internal.views.properties.PropertiesMessages;
26 
27 /**
28  * Copies a property to the clipboard.
29  */
30 /*package*/class CopyPropertyAction extends PropertySheetAction {
31 	/**
32 	 * System clipboard
33 	 */
34 	private Clipboard clipboard;
35 
36 	/**
37 	 * Creates the action.
38 	 *
39 	 * @param viewer the viewer
40 	 * @param name the name
41 	 * @param clipboard the clipboard
42 	 */
CopyPropertyAction(PropertySheetViewer viewer, String name, Clipboard clipboard)43 	public CopyPropertyAction(PropertySheetViewer viewer, String name,
44 			Clipboard clipboard) {
45 		super(viewer, name);
46 		PlatformUI.getWorkbench().getHelpSystem().setHelp(this,
47 				IPropertiesHelpContextIds.COPY_PROPERTY_ACTION);
48 		this.clipboard = clipboard;
49 	}
50 
51 	/**
52 	 * Performs this action.
53 	 */
54 	@Override
run()55 	public void run() {
56 		// Get the selected property
57 		IStructuredSelection selection = (IStructuredSelection) getPropertySheet()
58 				.getSelection();
59 		if (selection.isEmpty()) {
60 			return;
61 		}
62 		// Assume single selection
63 		IPropertySheetEntry entry = (IPropertySheetEntry) selection
64 				.getFirstElement();
65 
66 		// Place text on the clipboard
67 		StringBuilder buffer = new StringBuilder();
68 		buffer.append(entry.getDisplayName());
69 		buffer.append("\t"); //$NON-NLS-1$
70 		buffer.append(entry.getValueAsString());
71 
72 		setClipboard(buffer.toString());
73 	}
74 
75 	/**
76 	 * Updates enablement based on the current selection.
77 	 *
78 	 * @param sel the selection
79 	 */
selectionChanged(IStructuredSelection sel)80 	public void selectionChanged(IStructuredSelection sel) {
81 		setEnabled(!sel.isEmpty());
82 	}
83 
setClipboard(String text)84 	private void setClipboard(String text) {
85 		try {
86 			Object[] data = new Object[] { text };
87 			Transfer[] transferTypes = new Transfer[] { TextTransfer
88 					.getInstance() };
89 			clipboard.setContents(data, transferTypes);
90 		} catch (SWTError e) {
91 			if (e.code != DND.ERROR_CANNOT_SET_CLIPBOARD) {
92 				throw e;
93 			}
94 			if (MessageDialog.openQuestion(getPropertySheet().getControl()
95 					.getShell(), PropertiesMessages.CopyToClipboardProblemDialog_title,
96 					PropertiesMessages.CopyToClipboardProblemDialog_message)) {
97 				setClipboard(text);
98 			}
99 		}
100 	}
101 }
102 
103