Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e333b90b6fe7d73c5722fda3c41f0cf807c9c625 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*******************************************************************************
 * Copyright (c) 2018 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.debug.internal.ui.launchConfigurations;

import java.util.Set;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunch;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchDelegate;
import org.eclipse.debug.core.Launch;
import org.eclipse.debug.core.model.ILaunchConfigurationDelegate;
import org.eclipse.debug.core.model.ILaunchConfigurationDelegate2;
import org.eclipse.debug.internal.core.DebugCoreMessages;
import org.eclipse.debug.internal.ui.DebugUIPlugin;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.swt.SWT;
import org.eclipse.swt.dnd.Clipboard;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.ibm.icu.text.MessageFormat;

/**
 * Allows the user to specify to see and copy the command line to be executed
 * for the launch.
 *
 * @since 3.13
 */
public class ShowCommandLineDialog extends Dialog {
	Text fModuleArgumentsText;
	ILaunchConfiguration flaunchConfiguration;
	String fMode;


	public ShowCommandLineDialog(Shell parentShell, String mode, ILaunchConfiguration config) {
		super(parentShell);
		fMode = mode;
		setShellStyle(SWT.RESIZE | getShellStyle());
		flaunchConfiguration = config;
	}


	@Override
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText(LaunchConfigurationsMessages.LaunchConfigurationDialog_ShowCommandLine_Title);
	}

	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.OK_ID,
				LaunchConfigurationsMessages.LaunchConfigurationDialog_ShowCommandLine_Copy, true);
		createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
	}

	@Override
	protected Control createDialogArea(Composite parent) {
		Composite comp = (Composite) super.createDialogArea(parent);
		Font font = parent.getFont();

		Group group = new Group(comp, SWT.NONE);
		GridLayout topLayout = new GridLayout();
		group.setLayout(topLayout);
		GridData gd = new GridData(GridData.FILL_BOTH);
		gd.heightHint = convertHeightInCharsToPixels(15);
		gd.widthHint = convertWidthInCharsToPixels(50);
		group.setLayoutData(gd);
		group.setFont(font);

		fModuleArgumentsText = new Text(group, SWT.MULTI | SWT.WRAP | SWT.BORDER | SWT.V_SCROLL);
		gd = new GridData(GridData.FILL_BOTH);
		gd.heightHint = convertHeightInCharsToPixels(10);
		gd.widthHint = convertWidthInCharsToPixels(60);
		fModuleArgumentsText.setLayoutData(gd);

		String command = ""; //$NON-NLS-1$
		try {
			Set<String> modes = flaunchConfiguration.getModes();
			modes.add(fMode);
			ILaunchDelegate[] delegates = flaunchConfiguration.getType().getDelegates(modes);
			if (delegates.length ==1) {
				ILaunchConfigurationDelegate delegate = delegates[0].getDelegate();
				ILaunchConfigurationDelegate2 delegate2;
				ILaunch launch = null;
				if (delegate instanceof ILaunchConfigurationDelegate2) {
					delegate2 = (ILaunchConfigurationDelegate2) delegate;
					if (delegate2 != null) {
						launch = delegate2.getLaunch(flaunchConfiguration, fMode);
					}
					if (launch == null) {
						launch = new Launch(flaunchConfiguration, fMode, null);
					} else {
						// ensure the launch mode is valid
						if (!fMode.equals(launch.getLaunchMode())) {
							IStatus status = new Status(IStatus.ERROR, DebugPlugin.getUniqueIdentifier(),
									DebugPlugin.ERROR, MessageFormat.format(DebugCoreMessages.LaunchConfiguration_14,
											fMode, launch.getLaunchMode()),
									null);
							throw new CoreException(status);
						}
					}
				}
				command = delegate.showCommandLine(flaunchConfiguration, fMode, launch,
						null);

			}
		} catch (CoreException e) {
			e.printStackTrace();
		}
		if (command == null || (command != null && command.length() == 0)) {
			command = LaunchConfigurationsMessages.LaunchConfigurationDialog_ShowCommandLine_Default;
		}
		fModuleArgumentsText.setText(command);
		fModuleArgumentsText.setEditable(false);

		return comp;
	}

	@Override
	protected void buttonPressed(int buttonId) {
		if (buttonId == OK) {
			Clipboard clipboard = new Clipboard(null);
			try {
				TextTransfer textTransfer = TextTransfer.getInstance();
				Transfer[] transfers = new Transfer[] { textTransfer };
				Object[] data = new Object[] { fModuleArgumentsText.getText() };
				clipboard.setContents(data, transfers);
			} finally {
				clipboard.dispose();
			}
		}
		super.buttonPressed(buttonId);
	}

	@Override
	protected IDialogSettings getDialogBoundsSettings() {
		IDialogSettings settings = DebugUIPlugin.getDefault().getDialogSettings();
		IDialogSettings section = settings.getSection(getDialogSettingsSectionName());
		if (section == null) {
			section = settings.addNewSection(getDialogSettingsSectionName());
		}
		return section;
	}

	/**
	 * @return the name to use to save the dialog settings
	 */
	protected String getDialogSettingsSectionName() {
		return "SHOW_COMMAND_LINE_DIALOG"; //$NON-NLS-1$
	}

}

Back to the top