From 48b6c4c9464b7ee1ab6bd5d8007de9588d14fdc5 Mon Sep 17 00:00:00 2001
From: Jonah Graham
- * This class may be instantiated. This class is not intended to be subclassed. - *
- * @since 2.0 - */ -@Deprecated // see https://git.eclipse.org/r/#/c/68010/ -public class CMainTab extends CAbstractMainTab { - - /** - * Tab identifier used for ordering of tabs added using the - *org.eclipse.debug.ui.launchConfigurationTabs
- * extension point.
- *
- * @since 6.0
- */
- public static final String TAB_ID = "org.eclipse.cdt.cdi.launch.mainTab"; //$NON-NLS-1$
-
- private final boolean fWantsTerminalOption;
- protected Button fTerminalButton;
-
- private final boolean dontCheckProgram;
- private final boolean fSpecifyCoreFile;
-
- public static final int WANTS_TERMINAL = 1;
- public static final int DONT_CHECK_PROGRAM = 2;
- /** @since 6.0 */
- public static final int SPECIFY_CORE_FILE = 4;
-
- public CMainTab() {
- this(WANTS_TERMINAL);
- }
-
- public CMainTab(boolean terminalOption) {
- this(terminalOption ? WANTS_TERMINAL : 0);
- }
-
- public CMainTab(int flags) {
- fWantsTerminalOption = (flags & WANTS_TERMINAL) != 0;
- dontCheckProgram = (flags & DONT_CHECK_PROGRAM) != 0;
- fSpecifyCoreFile = (flags & SPECIFY_CORE_FILE) != 0;
- }
-
- @Override
- public void createControl(Composite parent) {
- Composite comp = new Composite(parent, SWT.NONE);
- setControl(comp);
-
- LaunchUIPlugin.getDefault().getWorkbench().getHelpSystem().setHelp(getControl(),
- ICDTLaunchHelpContextIds.LAUNCH_CONFIGURATION_DIALOG_MAIN_TAB);
-
- GridLayout topLayout = new GridLayout();
- comp.setLayout(topLayout);
-
- createVerticalSpacer(comp, 1);
- createExeFileGroup(comp, 1);
- createProjectGroup(comp, 1);
- createBuildOptionGroup(comp, 1);
- createVerticalSpacer(comp, 1);
- if (fSpecifyCoreFile) {
- createCoreFileGroup(comp, 1);
- }
- if (wantsTerminalOption() /* && ProcessFactory.supportesTerminal() */) {
- createTerminalOption(comp, 1);
- }
- LaunchUIPlugin.setDialogShell(parent.getShell());
- }
-
- protected boolean wantsTerminalOption() {
- return fWantsTerminalOption;
- }
-
- protected void createTerminalOption(Composite parent, int colSpan) {
- Composite mainComp = new Composite(parent, SWT.NONE);
- GridLayout mainLayout = new GridLayout();
- mainLayout.numColumns = 1;
- mainLayout.marginHeight = 0;
- mainLayout.marginWidth = 0;
- mainComp.setLayout(mainLayout);
- GridData gd = new GridData(GridData.FILL_HORIZONTAL);
- gd.horizontalSpan = colSpan;
- mainComp.setLayoutData(gd);
-
- fTerminalButton = createCheckButton(mainComp, LaunchMessages.CMainTab_UseTerminal);
- fTerminalButton.addSelectionListener(new SelectionAdapter() {
-
- @Override
- public void widgetSelected(SelectionEvent evt) {
- updateLaunchConfigurationDialog();
- }
- });
- fTerminalButton.setEnabled(PTY.isSupported(PTY.Mode.CONSOLE));
- }
-
- @Override
- public void initializeFrom(ILaunchConfiguration config) {
- filterPlatform = getPlatform(config);
- updateProjectFromConfig(config);
- updateProgramFromConfig(config);
- updateCoreFromConfig(config);
- updateBuildOptionFromConfig(config);
- updateTerminalFromConfig(config);
- }
-
- protected void updateTerminalFromConfig(ILaunchConfiguration config) {
- if (fTerminalButton != null) {
- boolean useTerminal = true;
- try {
- useTerminal = config.getAttribute(ICDTLaunchConfigurationConstants.ATTR_USE_TERMINAL,
- ICDTLaunchConfigurationConstants.USE_TERMINAL_DEFAULT);
- } catch (CoreException e) {
- LaunchUIPlugin.log(e);
- }
- fTerminalButton.setSelection(useTerminal);
- }
- }
-
- /** @since 6.0 */
- protected void updateCoreFromConfig(ILaunchConfiguration config) {
- if (fCoreText != null) {
- String coreName = EMPTY_STRING;
- try {
- coreName = config.getAttribute(ICDTLaunchConfigurationConstants.ATTR_COREFILE_PATH, EMPTY_STRING);
- } catch (CoreException ce) {
- LaunchUIPlugin.log(ce);
- }
- fCoreText.setText(coreName);
- }
- }
-
- @Override
- public void performApply(ILaunchConfigurationWorkingCopy config) {
- super.performApply(config);
- ICProject cProject = this.getCProject();
- if (cProject != null && cProject.exists()) {
- config.setMappedResources(new IResource[] { cProject.getProject() });
- } else {
- // the user typed in a non-existent project name. Ensure that
- // won't be suppressed from the dialog. This matches JDT behaviour
- config.setMappedResources(null);
- }
- config.setAttribute(ICDTLaunchConfigurationConstants.ATTR_PROJECT_NAME, fProjText.getText());
- if (fProgText != null) {
- config.setAttribute(ICDTLaunchConfigurationConstants.ATTR_PROGRAM_NAME, fProgText.getText());
- }
- if (fCoreText != null) {
- config.setAttribute(ICDTLaunchConfigurationConstants.ATTR_COREFILE_PATH, fCoreText.getText());
- }
- if (fTerminalButton != null) {
- config.setAttribute(ICDTLaunchConfigurationConstants.ATTR_USE_TERMINAL, fTerminalButton.getSelection());
- }
- }
-
- /**
- * Show a dialog that lists all main types
- */
- @Override
- protected void handleSearchButtonSelected() {
- if (getCProject() == null) {
- MessageDialog.openInformation(getShell(), LaunchMessages.CMainTab_Project_required,
- LaunchMessages.CMainTab_Enter_project_before_searching_for_program);
- return;
- }
- ILabelProvider programLabelProvider = new CElementLabelProvider() {
-
- @Override
- public String getText(Object element) {
- if (element instanceof IBinary) {
- IBinary bin = (IBinary) element;
- StringBuilder name = new StringBuilder();
- name.append(bin.getPath().lastSegment());
- return name.toString();
- }
- return super.getText(element);
- }
-
- @Override
- public Image getImage(Object element) {
- if (!(element instanceof ICElement)) {
- return super.getImage(element);
- }
- ICElement celement = (ICElement) element;
-
- if (celement.getElementType() == ICElement.C_BINARY) {
- IBinary belement = (IBinary) celement;
- if (belement.isExecutable()) {
- return DebugUITools.getImage(IDebugUIConstants.IMG_ACT_RUN);
- }
- }
-
- return super.getImage(element);
- }
- };
-
- ILabelProvider qualifierLabelProvider = new CElementLabelProvider() {
-
- @Override
- public String getText(Object element) {
- if (element instanceof IBinary) {
- IBinary bin = (IBinary) element;
- StringBuilder name = new StringBuilder();
- name.append(bin.getCPU()).append(bin.isLittleEndian() ? "le" : "be"); //$NON-NLS-1$ //$NON-NLS-2$
- name.append(" - "); //$NON-NLS-1$
- name.append(bin.getPath().toString());
- return name.toString();
- }
- return super.getText(element);
- }
- };
-
- TwoPaneElementSelector dialog = new TwoPaneElementSelector(getShell(), programLabelProvider,
- qualifierLabelProvider);
- dialog.setElements(getBinaryFiles(getCProject()));
- dialog.setMessage(LaunchMessages.CMainTab_Choose_program_to_run);
- dialog.setTitle(LaunchMessages.CMainTab_Program_Selection);
- dialog.setUpperListLabel(LaunchMessages.Launch_common_BinariesColon);
- dialog.setLowerListLabel(LaunchMessages.Launch_common_QualifierColon);
- dialog.setMultipleSelection(false);
- // dialog.set
- if (dialog.open() == Window.OK) {
- IBinary binary = (IBinary) dialog.getFirstResult();
- fProgText.setText(binary.getResource().getProjectRelativePath().toString());
- }
- }
-
- /**
- * @since 6.0
- */
- @Override
- protected void createProjectGroup(Composite parent, int colSpan) {
- Composite projComp = new Composite(parent, SWT.NONE);
- GridLayout projLayout = new GridLayout();
- projLayout.numColumns = 2;
- projLayout.marginHeight = 0;
- projLayout.marginWidth = 0;
- projComp.setLayout(projLayout);
- GridData gd = new GridData(GridData.FILL_HORIZONTAL);
- gd.horizontalSpan = colSpan;
- projComp.setLayoutData(gd);
-
- fProjLabel = new Label(projComp, SWT.NONE);
- fProjLabel.setText(LaunchMessages.CMainTab_ProjectColon);
- gd = new GridData();
- gd.horizontalSpan = 2;
- fProjLabel.setLayoutData(gd);
-
- fProjText = new Text(projComp, SWT.SINGLE | SWT.BORDER);
- gd = new GridData(GridData.FILL_HORIZONTAL);
- fProjText.setLayoutData(gd);
- fProjText.addModifyListener(new ModifyListener() {
-
- @Override
- public void modifyText(ModifyEvent evt) {
- // if project changes, invalidate program name cache
- fPreviouslyCheckedProgram = null;
-
- updateBuildConfigCombo(""); //$NON-NLS-1$
- updateLaunchConfigurationDialog();
- }
- });
-
- fProjButton = createPushButton(projComp, LaunchMessages.Launch_common_Browse_1, null);
- fProjButton.addSelectionListener(new SelectionAdapter() {
-
- @Override
- public void widgetSelected(SelectionEvent evt) {
- handleProjectButtonSelected();
- updateLaunchConfigurationDialog();
- }
- });
- }
-
- protected void createExeFileGroup(Composite parent, int colSpan) {
- Composite mainComp = new Composite(parent, SWT.NONE);
- GridLayout mainLayout = new GridLayout();
- mainLayout.marginHeight = 0;
- mainLayout.marginWidth = 0;
- mainComp.setLayout(mainLayout);
- GridData gd = new GridData(GridData.FILL_HORIZONTAL);
- gd.horizontalSpan = colSpan;
- mainComp.setLayoutData(gd);
- fProgLabel = new Label(mainComp, SWT.NONE);
- fProgLabel.setText(LaunchMessages.CMainTab_C_Application);
- gd = new GridData();
- fProgLabel.setLayoutData(gd);
- fProgText = new Text(mainComp, SWT.SINGLE | SWT.BORDER);
- gd = new GridData(GridData.FILL_HORIZONTAL);
- fProgText.setLayoutData(gd);
- fProgText.addModifyListener(new ModifyListener() {
- @Override
- public void modifyText(ModifyEvent evt) {
- updateLaunchConfigurationDialog();
- }
- });
-
- Composite buttonComp = new Composite(mainComp, SWT.NONE);
- GridLayout layout = new GridLayout(3, false);
- layout.marginHeight = 0;
- layout.marginWidth = 0;
- buttonComp.setLayout(layout);
- gd = new GridData(GridData.HORIZONTAL_ALIGN_END);
- buttonComp.setLayoutData(gd);
- buttonComp.setFont(parent.getFont());
-
- createVariablesButton(buttonComp, LaunchMessages.CMainTab_Variables, fProgText);
- fSearchButton = createPushButton(buttonComp, LaunchMessages.CMainTab_Search, null);
- fSearchButton.addSelectionListener(new SelectionAdapter() {
- @Override
- public void widgetSelected(SelectionEvent evt) {
- handleSearchButtonSelected();
- updateLaunchConfigurationDialog();
- }
- });
-
- Button fBrowseForBinaryButton;
- fBrowseForBinaryButton = createPushButton(buttonComp, LaunchMessages.Launch_common_Browse_2, null);
- fBrowseForBinaryButton.addSelectionListener(new SelectionAdapter() {
- @Override
- public void widgetSelected(SelectionEvent evt) {
- handleBinaryBrowseButtonSelected();
- updateLaunchConfigurationDialog();
- }
- });
- }
-
- /**
- * Show a dialog that lets the user select a project. This in turn provides context for the main
- * type, allowing the user to key a main type name, or constraining the search for main types to
- * the specified project.
- */
- protected void handleBinaryBrowseButtonSelected() {
- FileDialog fileDialog = new FileDialog(getShell(), SWT.NONE);
- fileDialog.setFileName(fProgText.getText());
- String text = fileDialog.open();
- if (text != null) {
- fProgText.setText(text);
- }
- }
-
- @Override
- public boolean isValid(ILaunchConfiguration config) {
- setErrorMessage(null);
- setMessage(null);
-
- if (!dontCheckProgram) {
- String name = fProjText.getText().trim();
- if (name.length() == 0) {
- setErrorMessage(LaunchMessages.CMainTab_Project_not_specified);
- return false;
- }
- if (!ResourcesPlugin.getWorkspace().getRoot().getProject(name).exists()) {
- setErrorMessage(LaunchMessages.Launch_common_Project_does_not_exist);
- return false;
- }
- IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(name);
- if (!project.isOpen()) {
- setErrorMessage(LaunchMessages.CMainTab_Project_must_be_opened);
- return false;
- }
-
- name = fProgText.getText().trim();
- try {
- name = VariablesPlugin.getDefault().getStringVariableManager().performStringSubstitution(name);
- } catch (CoreException e) {
- // Silently ignore substitution failure (for consistency with "Arguments" and "Work directory" fields)
- }
- if (name.length() == 0) {
- setErrorMessage(LaunchMessages.CMainTab_Program_not_specified);
- return false;
- }
- if (name.equals(".") || name.equals("..")) { //$NON-NLS-1$ //$NON-NLS-2$
- setErrorMessage(LaunchMessages.CMainTab_Program_does_not_exist);
- return false;
- }
- // Avoid constantly checking the binary if nothing relevant has
- // changed (binary or project name). See bug 277663.
- if (name.equals(fPreviouslyCheckedProgram)) {
- if (fPreviouslyCheckedProgramErrorMsg != null) {
- setErrorMessage(fPreviouslyCheckedProgramErrorMsg);
- }
- return fPreviouslyCheckedProgramIsValid;
- } else {
- fPreviouslyCheckedProgram = name;
- fPreviouslyCheckedProgramIsValid = true; // we'll flip this below if not true
- fPreviouslyCheckedProgramErrorMsg = null; // we'll set this below if there's an error
- IPath exePath = new Path(name);
- if (!exePath.isAbsolute()) {
- IPath location = project.getLocation();
- if (location == null) {
- setErrorMessage(
- fPreviouslyCheckedProgramErrorMsg = LaunchMessages.CMainTab_Program_does_not_exist);
- return (fPreviouslyCheckedProgramIsValid = false);
- }
-
- exePath = location.append(name);
- if (!exePath.toFile().exists()) {
- // Try the old way, which is required to support linked resources.
- IFile projFile = null;
- try {
- projFile = project.getFile(name);
- } catch (IllegalArgumentException e) {
- // thrown if relative path that resolves to a root file ("..\somefile")
- }
- if (projFile == null || !projFile.exists()) {
- setErrorMessage(
- fPreviouslyCheckedProgramErrorMsg = LaunchMessages.CMainTab_Program_does_not_exist);
- return (fPreviouslyCheckedProgramIsValid = false);
- } else {
- exePath = projFile.getLocation();
- }
- }
- }
- if (!exePath.toFile().exists()) {
- setErrorMessage(fPreviouslyCheckedProgramErrorMsg = LaunchMessages.CMainTab_Program_does_not_exist);
- return (fPreviouslyCheckedProgramIsValid = false);
- }
- // Notice that we don't check if exePath points to a valid executable since such
- // check is too expensive to be done on the UI thread.
- // See "https://bugs.eclipse.org/bugs/show_bug.cgi?id=328012".
- // We only verify that the program path represents a file.
- if (!exePath.toFile().isFile()) {
- setErrorMessage(fPreviouslyCheckedProgramErrorMsg = LaunchMessages.CMainTab_Selection_must_be_file);
- return (fPreviouslyCheckedProgramIsValid = false);
- }
- }
- }
-
- if (fCoreText != null) {
- String coreName = fCoreText.getText().trim();
- // We accept an empty string. This should trigger a prompt to the user
- // This allows to re-use the launch, with a different core file.
- if (!coreName.equals(EMPTY_STRING)) {
- if (coreName.equals(".") || coreName.equals("..")) { //$NON-NLS-1$ //$NON-NLS-2$
- setErrorMessage(LaunchMessages.CMainTab_Core_does_not_exist);
- return false;
- }
- IPath corePath = new Path(coreName);
- if (!corePath.toFile().exists()) {
- setErrorMessage(LaunchMessages.CMainTab_Core_does_not_exist);
- return false;
- }
- }
- }
-
- return true;
- }
-
- @Override
- public void setDefaults(ILaunchConfigurationWorkingCopy config) {
- // We set empty attributes for project & program so that when one config
- // is
- // compared to another, the existence of empty attributes doesn't cause
- // an
- // incorrect result (the performApply() method can result in empty
- // values
- // for these attributes being set on a config if there is nothing in the
- // corresponding text boxes)
- // plus getContext will use this to base context from if set.
- config.setAttribute(ICDTLaunchConfigurationConstants.ATTR_PROJECT_NAME, EMPTY_STRING);
- config.setAttribute(ICDTLaunchConfigurationConstants.ATTR_PROJECT_BUILD_CONFIG_ID, EMPTY_STRING);
- config.setAttribute(ICDTLaunchConfigurationConstants.ATTR_COREFILE_PATH, EMPTY_STRING);
-
- // Set the auto choose build configuration to true for new configurations.
- // Existing configurations created before this setting was introduced will have this disabled.
- config.setAttribute(ICDTLaunchConfigurationConstants.ATTR_PROJECT_BUILD_CONFIG_AUTO, true);
-
- ICElement cElement = null;
- cElement = getContext(config, getPlatform(config));
- if (cElement != null) {
- initializeCProject(cElement, config);
- initializeProgramName(cElement, config);
- } else {
- // don't want to remember the interim value from before
- config.setMappedResources(null);
- }
- if (wantsTerminalOption()) {
- config.setAttribute(ICDTLaunchConfigurationConstants.ATTR_USE_TERMINAL,
- ICDTLaunchConfigurationConstants.USE_TERMINAL_DEFAULT);
- }
- }
-
- /**
- * Set the program name attributes on the working copy based on the ICElement
- */
- protected void initializeProgramName(ICElement cElement, ILaunchConfigurationWorkingCopy config) {
- boolean renamed = false;
-
- if (!(cElement instanceof IBinary)) {
- cElement = cElement.getCProject();
- }
-
- if (cElement instanceof ICProject) {
- IProject project = cElement.getCProject().getProject();
- String name = project.getName();
- ICProjectDescription projDes = CCorePlugin.getDefault().getProjectDescription(project);
- if (projDes != null) {
- String buildConfigName = projDes.getActiveConfiguration().getName();
- // Bug 234951
- name = NLS.bind(LaunchMessages.CMainTab_Configuration_name, name, buildConfigName);
- }
- name = getLaunchConfigurationDialog().generateName(name);
- config.rename(name);
- renamed = true;
- }
-
- IBinary binary = null;
- if (cElement instanceof ICProject) {
- IBinary[] bins = getBinaryFiles((ICProject) cElement);
- if (bins != null && bins.length == 1) {
- binary = bins[0];
- }
- } else if (cElement instanceof IBinary) {
- binary = (IBinary) cElement;
- }
-
- if (binary != null) {
- String path;
- path = binary.getResource().getProjectRelativePath().toOSString();
- config.setAttribute(ICDTLaunchConfigurationConstants.ATTR_PROGRAM_NAME, path);
- if (!renamed) {
- String name = binary.getElementName();
- int index = name.lastIndexOf('.');
- if (index > 0) {
- name = name.substring(0, index);
- }
- name = getLaunchConfigurationDialog().generateName(name);
- config.rename(name);
- renamed = true;
- }
- }
-
- if (!renamed) {
- String name = getLaunchConfigurationDialog().generateName(cElement.getCProject().getElementName());
- config.rename(name);
- }
- }
-
- @Override
- public String getId() {
- return TAB_ID;
- }
-
- @Override
- public String getName() {
- return LaunchMessages.CMainTab_Main;
- }
-
- @Override
- public Image getImage() {
- return LaunchImages.get(LaunchImages.IMG_VIEW_MAIN_TAB);
- }
-}