private SelectionDialog( RunnerAndConfigurationSettings selectedSettings, @NotNull List<RunnerAndConfigurationSettings> settings) { super(myProject); setTitle(ExecutionBundle.message("before.launch.run.another.configuration.choose")); mySelectedSettings = selectedSettings; mySettings = settings; init(); myJBList.setSelectedValue(mySelectedSettings, true); myJBList.addMouseListener( new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { if (SwingUtilities.isLeftMouseButton(e) && e.getClickCount() == 2) { doOKAction(); } } }); FontMetrics fontMetrics = myJBList.getFontMetrics(myJBList.getFont()); int maxWidth = fontMetrics.stringWidth("m") * 30; for (RunnerAndConfigurationSettings setting : settings) { maxWidth = Math.max(fontMetrics.stringWidth(setting.getConfiguration().getName()), maxWidth); } maxWidth += 24; // icon and gap myJBList.setMinimumSize(new Dimension(maxWidth, myJBList.getPreferredSize().height)); }
private ValidationResult getValidationResult() { if (!myValidationResultValid) { myLastValidationResult = null; try { RunnerAndConfigurationSettings snapshot = getSnapshot(); if (snapshot != null) { snapshot.setName(getNameText()); snapshot.checkSettings(); for (ProgramRunner runner : RunnerRegistry.getInstance().getRegisteredRunners()) { for (Executor executor : ExecutorRegistry.getInstance().getRegisteredExecutors()) { if (runner.canRun(executor.getId(), snapshot.getConfiguration())) { checkConfiguration(runner, snapshot); break; } } } } } catch (RuntimeConfigurationException exception) { myLastValidationResult = exception != null ? new ValidationResult( exception.getLocalizedMessage(), exception.getTitle(), exception.getQuickFix()) : null; } catch (ConfigurationException e) { myLastValidationResult = new ValidationResult( e.getLocalizedMessage(), ExecutionBundle.message("invalid.data.dialog.title"), null); } myValidationResultValid = true; } return myLastValidationResult; }
@Override protected RunnerAndConfigurationSettings findExistingByElement( Location location, @NotNull RunnerAndConfigurationSettings[] existingConfigurations, ConfigurationContext context) { final PsiClass aClass = ApplicationConfigurationType.getMainClass(location.getPsiElement()); if (aClass == null) { return null; } final Module predefinedModule = ((ApplicationConfiguration) ((RunManagerImpl) RunManagerEx.getInstanceEx(location.getProject())) .getConfigurationTemplate(getConfigurationFactory()) .getConfiguration()) .getConfigurationModule() .getModule(); for (RunnerAndConfigurationSettings existingConfiguration : existingConfigurations) { final ApplicationConfiguration appConfiguration = (ApplicationConfiguration) existingConfiguration.getConfiguration(); if (Comparing.equal( JavaExecutionUtil.getRuntimeQualifiedName(aClass), appConfiguration.MAIN_CLASS_NAME)) { if (Comparing.equal( location.getModule(), appConfiguration.getConfigurationModule().getModule())) { return existingConfiguration; } final Module configurationModule = appConfiguration.getConfigurationModule().getModule(); if (Comparing.equal(location.getModule(), configurationModule)) return existingConfiguration; if (Comparing.equal(predefinedModule, configurationModule)) { return existingConfiguration; } } } return null; }
public void reset() { RunnerAndConfigurationSettings configuration = getSettings(); if (configuration != null) { setNameText(configuration.getName()); } super.reset(); }
@NotNull public static Icon getRawIcon(RunnerAndConfigurationSettings settings) { RunConfiguration configuration = settings.getConfiguration(); ConfigurationFactory factory = settings.getFactory(); Icon icon = factory != null ? factory.getIcon(configuration) : null; if (icon == null) icon = AllIcons.RunConfigurations.Unknown; return icon; }
@Override public boolean canExecuteTask(RunConfiguration configuration, RunConfigurableBeforeRunTask task) { RunnerAndConfigurationSettings settings = task.getSettings(); if (settings == null) { return false; } String executorId = DefaultRunExecutor.getRunExecutorInstance().getId(); final ProgramRunner runner = ProgramRunnerUtil.getRunner(executorId, settings); return runner != null && runner.canRun(executorId, settings.getConfiguration()); }
@Nullable private static RunnerAndConfigurationSettings chooseTempSettings(@NotNull Project project) { RunnerAndConfigurationSettings selectedConfiguration = RunManager.getInstance(project).getSelectedConfiguration(); if (selectedConfiguration != null && selectedConfiguration.isTemporary()) { return selectedConfiguration; } Iterator<RunnerAndConfigurationSettings> iterator = RunManager.getInstance(project).getTempConfigurationsList().iterator(); return iterator.hasNext() ? iterator.next() : null; }
@Override public void writeExternal(Element element) { super.writeExternal(element); if (myConfigurationName != null && myConfigurationType != null) { element.setAttribute("run_configuration_name", myConfigurationName); element.setAttribute("run_configuration_type", myConfigurationType); } else if (mySettings != null) { element.setAttribute("run_configuration_name", mySettings.getName()); element.setAttribute("run_configuration_type", mySettings.getType().getId()); } }
private static void checkConfiguration( final ProgramRunner runner, final RunnerAndConfigurationSettings snapshot) throws RuntimeConfigurationException { final RunnerSettings runnerSettings = snapshot.getRunnerSettings(runner); final ConfigurationPerRunnerSettings configurationSettings = snapshot.getConfigurationSettings(runner); try { runner.checkConfiguration(runnerSettings, configurationSettings); } catch (AbstractMethodError e) { // backward compatibility } }
private RunnerAndConfigurationSettings createConfiguration( final PsiClass aClass, final ConfigurationContext context, Location location) { final Project project = aClass.getProject(); RunnerAndConfigurationSettings settings = cloneTemplateConfiguration(project, context); final ApplicationConfiguration configuration = (ApplicationConfiguration) settings.getConfiguration(); configuration.MAIN_CLASS_NAME = JavaExecutionUtil.getRuntimeQualifiedName(aClass); configuration.setName(configuration.getGeneratedName()); setupConfigurationModule(context, configuration); JavaRunConfigurationExtensionManager.getInstance() .extendCreatedConfiguration(configuration, location); return settings; }
@Nullable public static ExecutionEnvironmentBuilder createEnvironment( @NotNull Executor executor, @NotNull RunnerAndConfigurationSettings settings) { try { return ExecutionEnvironmentBuilder.create(executor, settings); } catch (ExecutionException e) { handleExecutionError( settings.getConfiguration().getProject(), executor.getToolWindowId(), settings.getConfiguration().getName(), e); return null; } }
@Nullable public static ProgramRunner getRunner( @NotNull final String executorId, final RunnerAndConfigurationSettings configuration) { return configuration == null ? null : RunnerRegistry.getInstance().getRunner(executorId, configuration.getConfiguration()); }
public static void runConfiguration( @NotNull RunnerAndConfigurationSettings configuration, @NotNull Executor executor) { ExecutionEnvironmentBuilder builder = createEnvironment(executor, configuration); if (builder != null) { ExecutionManager.getInstance(configuration.getConfiguration().getProject()) .restartRunProfile(builder.activeTarget().build()); } }
public SelectConfigAction( final RunnerAndConfigurationSettings configuration, final Project project) { myConfiguration = configuration; myProject = project; String name = configuration.getName(); if (name == null || name.length() == 0) { name = " "; } final Presentation presentation = getTemplatePresentation(); presentation.setText(name, false); final ConfigurationType type = configuration.getType(); if (type != null) { presentation.setDescription( "Select " + type.getConfigurationTypeDescription() + " '" + name + "'"); } updateIcon(presentation); }
private boolean checkRunConfiguration( Executor executor, Project project, RunnerAndConfigurationSettings configuration) { ExecutionTarget target = ExecutionTargetManager.getActiveTarget(project); if (!ExecutionTargetManager.canRun(configuration, target)) { ExecutionUtil.handleExecutionError( project, executor.getToolWindowId(), configuration.getConfiguration(), new ExecutionException( StringUtil.escapeXml( "Cannot run '" + configuration.getName() + "' on '" + target.getDisplayName() + "'"))); return false; } if (!RunManagerImpl.canRunConfiguration(configuration, executor) || configuration.isEditBeforeRun()) { if (!RunDialog.editConfiguration(project, configuration, "Edit configuration", executor)) { return false; } while (!RunManagerImpl.canRunConfiguration(configuration, executor)) { if (0 == Messages.showYesNoDialog( project, "Configuration is still incorrect. Do you want to edit it again?", "Change Configuration Settings", "Edit", "Continue Anyway", Messages.getErrorIcon())) { if (!RunDialog.editConfiguration( project, configuration, "Edit configuration", executor)) { break; } } else { break; } } } return true; }
void init() { if (myInitialized) { return; } if (myConfigurationName != null && myConfigurationType != null) { Collection<RunnerAndConfigurationSettings> configurations = RunManagerImpl.getInstanceImpl(myProject).getSortedConfigurations(); for (RunnerAndConfigurationSettings runConfiguration : configurations) { ConfigurationType type = runConfiguration.getType(); if (myConfigurationName.equals(runConfiguration.getName()) && type != null && myConfigurationType.equals(type.getId())) { setSettings(runConfiguration); return; } } } }
@NotNull private static List<RunnerAndConfigurationSettings> getAvailableConfigurations( RunConfiguration runConfiguration) { Project project = runConfiguration.getProject(); if (project == null || !project.isInitialized()) return Collections.emptyList(); final RunManagerImpl runManager = RunManagerImpl.getInstanceImpl(project); final ArrayList<RunnerAndConfigurationSettings> configurations = new ArrayList<RunnerAndConfigurationSettings>(runManager.getSortedConfigurations()); String executorId = DefaultRunExecutor.getRunExecutorInstance().getId(); for (Iterator<RunnerAndConfigurationSettings> iterator = configurations.iterator(); iterator.hasNext(); ) { RunnerAndConfigurationSettings settings = iterator.next(); final ProgramRunner runner = ProgramRunnerUtil.getRunner(executorId, settings); if (runner == null || settings.getConfiguration() == runConfiguration) iterator.remove(); } return configurations; }
@NotNull private static ExecutionEnvironmentBuilder createEnvironmentBuilder( @NotNull Project project, @NotNull Executor executor, @Nullable RunnerAndConfigurationSettings configuration) { ExecutionEnvironmentBuilder builder = new ExecutionEnvironmentBuilder(project, executor); ProgramRunner runner = RunnerRegistry.getInstance() .getRunner( executor.getId(), configuration != null ? configuration.getConfiguration() : null); if (runner == null && configuration != null) { LOG.error("Cannot find runner for " + configuration.getName()); } else if (runner != null) { assert configuration != null; builder.runnerAndSettings(runner, configuration); } return builder; }
private void runTriggers(Executor executor, RunnerAndConfigurationSettings configuration) { final ConfigurationType configurationType = configuration.getType(); if (configurationType != null) { UsageTrigger.trigger( "execute." + ConvertUsagesUtil.ensureProperKey(configurationType.getId()) + "." + executor.getId()); } }
@Override public void update(final AnActionEvent e) { final Presentation presentation = e.getPresentation(); final Project project = e.getData(CommonDataKeys.PROJECT); if (project == null) { disable(presentation); return; } RunnerAndConfigurationSettings settings = chooseTempSettings(project); if (settings == null) { disable(presentation); } else { presentation.setText( ExecutionBundle.message( "save.temporary.run.configuration.action.name", settings.getName())); presentation.setDescription(presentation.getText()); presentation.setVisible(true); presentation.setEnabled(true); } }
public static Icon getConfigurationIcon( final RunnerAndConfigurationSettings settings, final boolean invalid) { Icon icon = getRawIcon(settings); final Icon configurationIcon = settings.isTemporary() ? getTemporaryIcon(icon) : icon; if (invalid) { return LayeredIcon.create( configurationIcon, AllIcons.RunConfigurations.InvalidConfigurationLayer); } return configurationIcon; }
private static void updatePresentation( @Nullable ExecutionTarget target, @Nullable RunnerAndConfigurationSettings settings, @Nullable Project project, @NotNull Presentation presentation) { if (project != null && target != null && settings != null) { String name = settings.getName(); if (target != DefaultExecutionTarget.INSTANCE) { name += " | " + target.getDisplayName(); } else { if (!settings.canRunOn(target)) { name += " | Nothing to run on"; } } presentation.setText(name, false); setConfigurationIcon(presentation, settings, project); } else { presentation.setText(""); // IDEA-21657 presentation.setIcon(null); } }
@Nullable public static Pair<ProgramRunner, ExecutionEnvironment> createRunner( @NotNull ExternalSystemTaskExecutionSettings taskSettings, @NotNull String executorId, @NotNull Project project, @NotNull ProjectSystemId externalSystemId) { Executor executor = ExecutorRegistry.getInstance().getExecutorById(executorId); if (executor == null) return null; String runnerId = getRunnerId(executorId); if (runnerId == null) return null; ProgramRunner runner = RunnerRegistry.getInstance().findRunnerById(runnerId); if (runner == null) return null; AbstractExternalSystemTaskConfigurationType configurationType = findConfigurationType(externalSystemId); if (configurationType == null) return null; String name = AbstractExternalSystemTaskConfigurationType.generateName(project, taskSettings); RunnerAndConfigurationSettings settings = RunManager.getInstance(project) .createRunConfiguration(name, configurationType.getFactory()); ExternalSystemRunConfiguration runConfiguration = (ExternalSystemRunConfiguration) settings.getConfiguration(); runConfiguration.getSettings().setExternalProjectPath(taskSettings.getExternalProjectPath()); runConfiguration .getSettings() .setTaskNames(ContainerUtil.newArrayList(taskSettings.getTaskNames())); runConfiguration .getSettings() .setTaskDescriptions(ContainerUtil.newArrayList(taskSettings.getTaskDescriptions())); runConfiguration.getSettings().setVmOptions(taskSettings.getVmOptions()); runConfiguration.getSettings().setScriptParameters(taskSettings.getScriptParameters()); runConfiguration.getSettings().setExecutionName(taskSettings.getExecutionName()); return Pair.create(runner, new ExecutionEnvironment(executor, runner, settings, project)); }
@NotNull private List<RunContentDescriptor> getIncompatibleRunningDescriptors( @NotNull RunnerAndConfigurationSettings configurationAndSettings) { final RunConfiguration configurationToCheckCompatibility = configurationAndSettings.getConfiguration(); return getRunningDescriptors( runningConfigurationAndSettings -> { RunConfiguration runningConfiguration = runningConfigurationAndSettings == null ? null : runningConfigurationAndSettings.getConfiguration(); if (runningConfiguration == null || !(runningConfiguration instanceof CompatibilityAwareRunProfile)) { return false; } return ((CompatibilityAwareRunProfile) runningConfiguration) .mustBeStoppedToRun(configurationToCheckCompatibility); }); }
public static void executeConfiguration( @NotNull ExecutionEnvironment environment, boolean showSettings, boolean assignNewId) { if (ExecutorRegistry.getInstance().isStarting(environment)) { return; } RunnerAndConfigurationSettings runnerAndConfigurationSettings = environment.getRunnerAndConfigurationSettings(); if (runnerAndConfigurationSettings != null) { if (!ExecutionTargetManager.canRun(environment)) { ExecutionUtil.handleExecutionError( environment, new ExecutionException( StringUtil.escapeXml( "Cannot run '" + environment.getRunProfile().getName() + "' on '" + environment.getExecutionTarget().getDisplayName() + "'"))); return; } if (!RunManagerImpl.canRunConfiguration(environment) || (showSettings && runnerAndConfigurationSettings.isEditBeforeRun())) { if (!RunDialog.editConfiguration(environment, "Edit configuration")) { return; } while (!RunManagerImpl.canRunConfiguration(environment)) { if (Messages.YES == Messages.showYesNoDialog( environment.getProject(), "Configuration is still incorrect. Do you want to edit it again?", "Change Configuration Settings", "Edit", "Continue Anyway", Messages.getErrorIcon())) { if (!RunDialog.editConfiguration(environment, "Edit configuration")) { return; } } else { break; } } } ConfigurationType configurationType = runnerAndConfigurationSettings.getType(); if (configurationType != null) { UsageTrigger.trigger( "execute." + ConvertUsagesUtil.ensureProperKey(configurationType.getId()) + "." + environment.getExecutor().getId()); } } try { if (assignNewId) { environment.assignNewExecutionId(); } environment.getRunner().execute(environment); } catch (ExecutionException e) { String name = runnerAndConfigurationSettings != null ? runnerAndConfigurationSettings.getName() : null; if (name == null) { name = environment.getRunProfile().getName(); } if (name == null && environment.getContentToReuse() != null) { name = environment.getContentToReuse().getDisplayName(); } if (name == null) { name = "<Unknown>"; } ExecutionUtil.handleExecutionError( environment.getProject(), environment.getExecutor().getToolWindowId(), name, e); } }
private static void start(@NotNull ExecutionEnvironment environment) { RunnerAndConfigurationSettings settings = environment.getRunnerAndConfigurationSettings(); ProgramRunnerUtil.executeConfiguration( environment, settings != null && settings.isEditBeforeRun(), true); }
@Override public void restartRunProfile(@NotNull final ExecutionEnvironment environment) { RunnerAndConfigurationSettings configuration = environment.getRunnerAndConfigurationSettings(); List<RunContentDescriptor> runningIncompatible; if (configuration == null) { runningIncompatible = Collections.emptyList(); } else { runningIncompatible = getIncompatibleRunningDescriptors(configuration); } RunContentDescriptor contentToReuse = environment.getContentToReuse(); final List<RunContentDescriptor> runningOfTheSameType = new SmartList<>(); if (configuration != null && configuration.isSingleton()) { runningOfTheSameType.addAll(getRunningDescriptorsOfTheSameConfigType(configuration)); } else if (isProcessRunning(contentToReuse)) { runningOfTheSameType.add(contentToReuse); } List<RunContentDescriptor> runningToStop = ContainerUtil.concat(runningOfTheSameType, runningIncompatible); if (!runningToStop.isEmpty()) { if (configuration != null) { if (!runningOfTheSameType.isEmpty() && (runningOfTheSameType.size() > 1 || contentToReuse == null || runningOfTheSameType.get(0) != contentToReuse) && !userApprovesStopForSameTypeConfigurations( environment.getProject(), configuration.getName(), runningOfTheSameType.size())) { return; } if (!runningIncompatible.isEmpty() && !userApprovesStopForIncompatibleConfigurations( myProject, configuration.getName(), runningIncompatible)) { return; } } for (RunContentDescriptor descriptor : runningToStop) { stop(descriptor); } } if (myAwaitingRunProfiles.get(environment.getRunProfile()) == environment) { // defense from rerunning exactly the same ExecutionEnvironment return; } myAwaitingRunProfiles.put(environment.getRunProfile(), environment); awaitTermination( new Runnable() { @Override public void run() { if (myAwaitingRunProfiles.get(environment.getRunProfile()) != environment) { // a new rerun has been requested before starting this one, ignore this rerun return; } if ((DumbService.getInstance(myProject).isDumb() && !Registry.is("dumb.aware.run.configurations")) || ExecutorRegistry.getInstance().isStarting(environment)) { awaitTermination(this, 100); return; } for (RunContentDescriptor descriptor : runningOfTheSameType) { ProcessHandler processHandler = descriptor.getProcessHandler(); if (processHandler != null && !processHandler.isProcessTerminated()) { awaitTermination(this, 100); return; } } myAwaitingRunProfiles.remove(environment.getRunProfile()); start(environment); } }, 50); }
private void runConfigurations( final Executor executor, final List<RunConfiguration> runConfigurations, final int index) { if (index >= runConfigurations.size()) { stopRunningMultirunConfiguration.doneStaringConfigurations(); return; } if (!stopRunningMultirunConfiguration.canContinueStartingConfigurations()) { stopRunningMultirunConfiguration.doneStaringConfigurations(); // don't start more configurations if user stopped the plugin work. return; } final RunConfiguration runConfiguration = runConfigurations.get(index); final Project project = runConfiguration.getProject(); final RunnerAndConfigurationSettings configuration = new RunnerAndConfigurationSettingsImpl( RunManagerImpl.getInstanceImpl(project), runConfiguration, false); boolean started = false; try { ProgramRunner runner = RunnerRegistry.getInstance().getRunner(executor.getId(), runConfiguration); if (runner == null) return; if (!checkRunConfiguration(executor, project, configuration)) return; runTriggers(executor, configuration); RunContentDescriptor runContentDescriptor = getRunContentDescriptor(runConfiguration, project); ExecutionEnvironment executionEnvironment = new ExecutionEnvironment( runner, DefaultExecutionTarget.INSTANCE, configuration, runContentDescriptor, project); runner.execute( executor, executionEnvironment, new ProgramRunner.Callback() { @SuppressWarnings("ConstantConditions") @Override public void processStarted(final RunContentDescriptor descriptor) { if (descriptor == null) { if (startOneByOne) { // start next configuration.. runConfigurations(executor, runConfigurations, index + 1); } return; } final ProcessHandler processHandler = descriptor.getProcessHandler(); if (processHandler != null) { processHandler.addProcessListener( new ProcessAdapter() { @SuppressWarnings("ConstantConditions") @Override public void startNotified(ProcessEvent processEvent) { Content content = descriptor.getAttachedContent(); if (content != null) { content.setIcon(descriptor.getIcon()); if (!stopRunningMultirunConfiguration .canContinueStartingConfigurations()) { // Multirun was stopped - destroy processes that are still starting up processHandler.destroyProcess(); if (!content.isPinned() && !startOneByOne) { // checks if not pinned, to avoid destroying already existed tab // checks if start one by one - no need to close the console tab, as // it's won't be shown // as other checks disallow starting it // content.getManager() can be null, if content is removed already as // part of destroy above if (content.getManager() != null) { content.getManager().removeContent(content, false); } } } else { // mark all current console tab as pinned content.setPinned(true); // mark running process tab with * content.setDisplayName(descriptor.getDisplayName() + "*"); } } } @Override public void processTerminated(final ProcessEvent processEvent) { onTermination(processEvent, true); } @Override public void processWillTerminate( ProcessEvent processEvent, boolean willBeDestroyed) { onTermination(processEvent, false); } private void onTermination( final ProcessEvent processEvent, final boolean terminated) { if (descriptor.getAttachedContent() == null) { return; } LaterInvocator.invokeLater( new Runnable() { @Override public void run() { final Content content = descriptor.getAttachedContent(); if (content == null) return; // exit code is 0 if the process completed successfully final boolean completedSuccessfully = (terminated && processEvent.getExitCode() == 0); if (hideSuccessProcess && completedSuccessfully) { // close the tab for the success process and exit - nothing else // could be done if (content.getManager() != null) { content.getManager().removeContent(content, false); return; } } if (!separateTabs && completedSuccessfully) { // un-pin the console tab if re-use is allowed and process // completed successfully, // so the tab could be re-used for other processes content.setPinned(false); } // remove the * used to identify running process content.setDisplayName(descriptor.getDisplayName()); // add the alert icon in case if process existed with non-0 status if (markFailedProcess && processEvent.getExitCode() != 0) { LaterInvocator.invokeLater( new Runnable() { @Override public void run() { content.setIcon( LayeredIcon.create( content.getIcon(), AllIcons.Nodes.TabAlert)); } }); } } }); } }); } stopRunningMultirunConfiguration.addProcess(project, processHandler); if (startOneByOne) { // start next configuration.. runConfigurations(executor, runConfigurations, index + 1); } } }); started = true; } catch (ExecutionException e) { ExecutionUtil.handleExecutionError( project, executor.getToolWindowId(), configuration.getConfiguration(), e); } finally { // start the next one if (!startOneByOne) { runConfigurations(executor, runConfigurations, index + 1); } else if (!started) { // failed to start current, means the chain is broken runConfigurations(executor, runConfigurations, index + 1); } } }