1 /*******************************************************************************
2  * Copyright (c) 2000, 2012 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 package org.eclipse.jdt.internal.debug.ui.actions;
15 
16 
17 import java.util.Iterator;
18 
19 import org.eclipse.jdt.core.IClasspathEntry;
20 import org.eclipse.jdt.internal.debug.ui.launcher.RuntimeClasspathViewer;
21 import org.eclipse.jdt.launching.IRuntimeClasspathEntry;
22 import org.eclipse.jdt.ui.wizards.BuildPathDialogAccess;
23 import org.eclipse.jface.viewers.IStructuredSelection;
24 import org.eclipse.swt.SWT;
25 
26 /**
27  * Attach source to an archive or variable.
28  */
29 public class AttachSourceAction extends RuntimeClasspathAction {
30 
31 	private IRuntimeClasspathEntry[] fEntries;
32 
33 	/**
34 	 * Creates an action to open a source attachment dialog.
35 	 *
36 	 * @param viewer the viewer the action is associated with or <code>null</code>
37 	 * @param style a button or radio button
38 	 */
AttachSourceAction(RuntimeClasspathViewer viewer, int style)39 	public AttachSourceAction(RuntimeClasspathViewer viewer, int style) {
40 		super((style == SWT.RADIO) ? ActionMessages.AttachSourceAction_2 : ActionMessages.AttachSourceAction_3, viewer); //
41 	}
42 
43 	/**
44 	 * Prompts source attachment.
45 	 *
46 	 * @see org.eclipse.jface.action.IAction#run()
47 	 */
48 	@Override
run()49 	public void run() {
50 		IClasspathEntry classpathEntry = BuildPathDialogAccess.configureSourceAttachment(getShell(), fEntries[0].getClasspathEntry());
51 		if (classpathEntry != null) {
52 			for (IRuntimeClasspathEntry entry : fEntries) {
53 				entry.setSourceAttachmentPath(classpathEntry.getSourceAttachmentPath());
54 				entry.setSourceAttachmentRootPath(classpathEntry.getSourceAttachmentRootPath());
55 				getViewer().refresh(entry);
56 			}
57 			getViewer().notifyChanged();
58 		}
59 	}
60 
61 	/* (non-Javadoc)
62 	 * @see org.eclipse.ui.actions.BaseSelectionListenerAction#updateSelection(org.eclipse.jface.viewers.IStructuredSelection)
63 	 */
64 	@Override
updateSelection(IStructuredSelection selection)65 	protected boolean updateSelection(IStructuredSelection selection) {
66 		fEntries = new IRuntimeClasspathEntry[selection.size()];
67 		Iterator<?> iterator = selection.iterator();
68 		int i = 0;
69 		while (iterator.hasNext()) {
70 			Object selected= iterator.next();
71 			if (selected instanceof IRuntimeClasspathEntry) {
72 				IRuntimeClasspathEntry entry = (IRuntimeClasspathEntry)selected;
73 				int type = entry.getType();
74 				switch (type) {
75 					case IRuntimeClasspathEntry.VARIABLE:
76 					case IRuntimeClasspathEntry.ARCHIVE:
77 						fEntries[i] = entry;
78 						i++;
79 						break;
80 					default:
81 						return false;
82 				}
83 			} else {
84 				return false;
85 			}
86 		}
87 		return selection.size() > 0;
88 	}
89 }
90