1 /*******************************************************************************
2  * Copyright (c) 2009, 2018 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  *     Red Hat Inc. - Bug 460967
14  ******************************************************************************/
15 
16 package org.eclipse.equinox.internal.p2.ui.dialogs;
17 
18 import org.eclipse.core.expressions.*;
19 import org.eclipse.equinox.internal.p2.ui.model.ProvElement;
20 import org.eclipse.equinox.internal.p2.ui.viewers.IUDetailsLabelProvider;
21 import org.eclipse.equinox.p2.ui.ICopyable;
22 import org.eclipse.swt.widgets.Control;
23 import org.eclipse.ui.ISources;
24 import org.eclipse.ui.PlatformUI;
25 import org.eclipse.ui.handlers.IHandlerActivation;
26 import org.eclipse.ui.handlers.IHandlerService;
27 import org.eclipse.ui.swt.IFocusService;
28 
29 public class CopyUtils {
30 	public static final String NEWLINE = System.lineSeparator();
31 	public static final String DELIMITER = "\t"; //$NON-NLS-1$
32 	private static final String NESTING_INDENT = "  "; //$NON-NLS-1$
33 
34 	// We never test the control ID so we can use the same ID for all controls
35 	private static final String CONTROL_ID = "org.eclipse.equinox.p2.ui.CopyControlId"; //$NON-NLS-1$
36 
getIndentedClipboardText(Object[] elements, IUDetailsLabelProvider labelProvider)37 	public static String getIndentedClipboardText(Object[] elements, IUDetailsLabelProvider labelProvider) {
38 		StringBuilder buffer = new StringBuilder();
39 		for (int i = 0; i < elements.length; i++) {
40 			if (i > 0)
41 				buffer.append(NEWLINE);
42 			appendIndention(buffer, elements[i]);
43 			buffer.append(labelProvider.getClipboardText(elements[i], DELIMITER));
44 		}
45 		return buffer.toString();
46 	}
47 
48 	/**
49 	 * Install a copy popup menu on the specified control and activate the copy
50 	 * handler for the control when the control has focus. The handler will be
51 	 * deactivated when the control is disposed.
52 	 *
53 	 * @param copyable the copyable that will perform the copy
54 	 * @param control  the control on which to install the menu and handler
55 	 */
activateCopy(ICopyable copyable, final Control control)56 	public static void activateCopy(ICopyable copyable, final Control control) {
57 		IFocusService fs = PlatformUI.getWorkbench().getService(IFocusService.class);
58 		final IHandlerService hs = PlatformUI.getWorkbench().getService(IHandlerService.class);
59 		new CopyPopup(copyable, control);
60 		if (fs != null && hs != null) {
61 			fs.addFocusTracker(control, CONTROL_ID);
62 			final IHandlerActivation handlerActivation = hs.activateHandler(CopyHandler.ID, new CopyHandler(copyable),
63 					new Expression() {
64 						@Override
65 						public EvaluationResult evaluate(IEvaluationContext context) {
66 							return context.getVariable(ISources.ACTIVE_FOCUS_CONTROL_NAME) == control
67 									? EvaluationResult.TRUE
68 									: EvaluationResult.FALSE;
69 						}
70 
71 						@Override
72 						public void collectExpressionInfo(final ExpressionInfo info) {
73 							info.addVariableNameAccess(ISources.ACTIVE_FOCUS_CONTROL_NAME);
74 						}
75 
76 					});
77 			control.addDisposeListener(e -> hs.deactivateHandler(handlerActivation));
78 		}
79 	}
80 
appendIndention(StringBuilder buffer, Object element)81 	private static void appendIndention(StringBuilder buffer, Object element) {
82 		Object parent;
83 		while (element instanceof ProvElement && (parent = ((ProvElement) element).getParent(element)) != null) {
84 			buffer.append(NESTING_INDENT);
85 			element = parent;
86 		}
87 
88 	}
89 }