/* * Copyright 2010-2015 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.jetbrains.kotlin.idea.run; import com.intellij.application.options.ModulesComboBox; import com.intellij.execution.JavaExecutionUtil; import com.intellij.execution.ui.CommonJavaParametersPanel; import com.intellij.execution.ui.ConfigurationModuleSelector; import com.intellij.execution.ui.DefaultJreSelector; import com.intellij.execution.ui.JrePathEditor; import com.intellij.openapi.options.ConfigurationException; import com.intellij.openapi.options.SettingsEditor; import com.intellij.openapi.project.Project; import com.intellij.openapi.ui.LabeledComponent; import com.intellij.psi.PsiClass; import com.intellij.ui.PanelWithAnchor; import com.intellij.util.ui.UIUtil; import org.jetbrains.annotations.NotNull; import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class JetRunConfigurationEditor extends SettingsEditor<JetRunConfiguration> implements PanelWithAnchor { private JPanel mainPanel; private LabeledComponent<JTextField> mainClass; private CommonJavaParametersPanel commonProgramParameters; private LabeledComponent<ModulesComboBox> moduleChooser; private JrePathEditor jrePathEditor; private final ConfigurationModuleSelector moduleSelector; private JComponent anchor; public JetRunConfigurationEditor(Project project) { moduleSelector = new ConfigurationModuleSelector(project, moduleChooser.getComponent()); jrePathEditor.setDefaultJreSelector(DefaultJreSelector.fromModuleDependencies(moduleChooser.getComponent(), false)); commonProgramParameters.setModuleContext(moduleSelector.getModule()); moduleChooser.getComponent().addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { commonProgramParameters.setModuleContext(moduleSelector.getModule()); } }); anchor = UIUtil.mergeComponentsWithAnchor(mainClass, commonProgramParameters, jrePathEditor, jrePathEditor, moduleChooser); } @Override protected void applyEditorTo(JetRunConfiguration configuration) throws ConfigurationException { commonProgramParameters.applyTo(configuration); moduleSelector.applyTo(configuration); String className = mainClass.getComponent().getText(); PsiClass aClass = moduleSelector.findClass(className); configuration.setRunClass(aClass != null ? JavaExecutionUtil.getRuntimeQualifiedName(aClass) : className); configuration.setAlternativeJrePath(jrePathEditor.getJrePathOrName()); configuration.setAlternativeJrePathEnabled(jrePathEditor.isAlternativeJreSelected()); } @Override protected void resetEditorFrom(JetRunConfiguration configuration) { commonProgramParameters.reset(configuration); moduleSelector.reset(configuration); mainClass.getComponent().setText(configuration.MAIN_CLASS_NAME != null ? configuration.MAIN_CLASS_NAME.replaceAll("\\$", "\\.") : ""); jrePathEditor.setPathOrName(configuration.ALTERNATIVE_JRE_PATH, configuration.ALTERNATIVE_JRE_PATH_ENABLED); } @NotNull @Override protected JComponent createEditor() { return mainPanel; } private void createUIComponents() { mainClass = new LabeledComponent<JTextField>(); JTextField myMainClassField = new JTextField(); mainClass.setComponent(myMainClassField); } @Override public JComponent getAnchor() { return anchor; } @Override public void setAnchor(JComponent anchor) { this.anchor = anchor; mainClass.setAnchor(anchor); commonProgramParameters.setAnchor(anchor); jrePathEditor.setAnchor(anchor); moduleChooser.setAnchor(anchor); } }