Remove TranslateToJsAction.

Improve configuration UI.
Add directory to store generated files setting.
This commit is contained in:
Pavel V. Talanov
2012-03-21 15:22:59 +04:00
parent 253f606dc4
commit 487fc95564
7 changed files with 129 additions and 86 deletions
@@ -28,7 +28,6 @@ import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFileManager;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.plugin.actions.TranslateToJsAction;
/**
* @author Pavel Talanov
@@ -46,10 +45,10 @@ public final class K2JSBrowserProgramRunner extends GenericProgramRunner {
try {
K2JSConfigurationSettings configurationSettings = getSettings(state);
try {
TranslateToJsAction.doPerform(project);
K2JSRunnerUtils.translateAndSave(project, configurationSettings.getGeneratedFilePath());
}
catch (Throwable e) {
TranslateToJsAction.notifyFailure(e);
K2JSRunnerUtils.notifyFailure(e);
return null;
}
openBrowser(configurationSettings);
@@ -61,7 +60,10 @@ public final class K2JSBrowserProgramRunner extends GenericProgramRunner {
}
private static void openBrowser(@NotNull K2JSConfigurationSettings configurationSettings) {
String filePath = configurationSettings.getFilePath();
if (!configurationSettings.isShouldOpenInBrowserAfterTranslation()) {
return;
}
String filePath = configurationSettings.getPageToOpenFilePath();
String url = VirtualFileManager.constructUrl(LocalFileSystem.PROTOCOL, filePath);
BrowsersConfiguration.launchBrowser(configurationSettings.getBrowserFamily(), url);
}
@@ -17,19 +17,33 @@
package org.jetbrains.jet.plugin.k2jsrun;
import com.intellij.ide.browsers.BrowsersConfiguration;
import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.NotNull;
/**
* @author Pavel Talanov
*/
public class K2JSConfigurationSettings {
public final class K2JSConfigurationSettings {
@NotNull
private String filePath = "";
private String pageToOpenFilePath = "";
@NotNull
private String generatedFilePath;
private boolean shouldOpenInBrowserAfterTranslation = false;
@NotNull
private BrowsersConfiguration.BrowserFamily browserFamily = BrowsersConfiguration.getInstance().getActiveBrowsers().get(0);
public K2JSConfigurationSettings(@NotNull Project project) {
generatedFilePath = project.getBasePath() + "/generated.js";
}
public K2JSConfigurationSettings() {
generatedFilePath = "";
}
@NotNull
public BrowsersConfiguration.BrowserFamily getBrowserFamily() {
return browserFamily;
@@ -40,11 +54,28 @@ public class K2JSConfigurationSettings {
}
@NotNull
public String getFilePath() {
return filePath;
public String getPageToOpenFilePath() {
return pageToOpenFilePath;
}
public void setFilePath(@NotNull String filePath) {
this.filePath = filePath;
public void setPageToOpenFilePath(@NotNull String pageToOpenFilePath) {
this.pageToOpenFilePath = pageToOpenFilePath;
}
@NotNull
public String getGeneratedFilePath() {
return generatedFilePath;
}
public void setGeneratedFilePath(@NotNull String generatedFilePath) {
this.generatedFilePath = generatedFilePath;
}
public boolean isShouldOpenInBrowserAfterTranslation() {
return shouldOpenInBrowserAfterTranslation;
}
public void setShouldOpenInBrowserAfterTranslation(boolean shouldOpenInBrowserAfterTranslation) {
this.shouldOpenInBrowserAfterTranslation = shouldOpenInBrowserAfterTranslation;
}
}
@@ -42,11 +42,11 @@ import java.util.Collection;
/**
* @author Pavel Talanov
*/
public class K2JSRunConfiguration extends ModuleBasedConfiguration<RunConfigurationModule>
public final class K2JSRunConfiguration extends ModuleBasedConfiguration<RunConfigurationModule>
implements RunConfigurationWithSuppressedDefaultRunAction {
@NotNull
private K2JSConfigurationSettings settings = new K2JSConfigurationSettings();
private K2JSConfigurationSettings settings = new K2JSConfigurationSettings(getProject());
public K2JSRunConfiguration(String name, RunConfigurationModule runConfigurationModule, ConfigurationFactory factory) {
super(name, runConfigurationModule, factory);
@@ -8,7 +8,7 @@
<properties/>
<border type="none"/>
<children>
<grid id="aad78" layout-manager="GridLayoutManager" row-count="2" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="aad78" layout-manager="GridLayoutManager" row-count="4" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
@@ -16,15 +16,15 @@
<properties/>
<border type="none"/>
<children>
<component id="5ba6" class="com.intellij.openapi.ui.TextFieldWithBrowseButton" binding="chooseFile">
<component id="5ba6" class="com.intellij.openapi.ui.TextFieldWithBrowseButton" binding="htmlChooseFile">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
</component>
<component id="cf71a" class="javax.swing.JLabel">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="HTML file"/>
@@ -32,7 +32,7 @@
</component>
<component id="f4230" class="javax.swing.JLabel">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<labelFor value="a2cc0"/>
@@ -41,7 +41,29 @@
</component>
<component id="a2cc0" class="javax.swing.JComboBox" binding="browserComboBox">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<grid row="3" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
</component>
<component id="beb35" class="javax.swing.JCheckBox" binding="openInBrowserCheckBox">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Open in browser after translation"/>
</properties>
</component>
<component id="f6a73" class="javax.swing.JLabel">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Generated file directory"/>
</properties>
</component>
<component id="12973" class="com.intellij.openapi.ui.TextFieldWithBrowseButton" binding="generatedChooseFile">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
</component>
@@ -26,10 +26,13 @@ import com.intellij.openapi.options.SettingsEditor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ProjectRootManager;
import com.intellij.openapi.ui.TextFieldWithBrowseButton;
import com.intellij.openapi.util.io.FileUtil;
import org.jetbrains.annotations.NotNull;
import javax.swing.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import static com.intellij.openapi.util.io.FileUtil.toSystemIndependentName;
/**
* @author Pavel Talanov
@@ -37,8 +40,10 @@ import javax.swing.*;
public final class K2JSRunConfigurationEditor extends SettingsEditor<K2JSRunConfiguration> {
private JPanel mainPanel;
private TextFieldWithBrowseButton chooseFile;
private TextFieldWithBrowseButton htmlChooseFile;
private JComboBox browserComboBox;
private JCheckBox openInBrowserCheckBox;
private TextFieldWithBrowseButton generatedChooseFile;
@NotNull
private final Project project;
@@ -47,31 +52,60 @@ public final class K2JSRunConfigurationEditor extends SettingsEditor<K2JSRunConf
}
@Override
protected void resetEditorFrom(K2JSRunConfiguration configuration) {
chooseFile.setText(configuration.settings().getFilePath());
protected void resetEditorFrom(@NotNull K2JSRunConfiguration configuration) {
htmlChooseFile.setText(toSystemIndependentName(configuration.settings().getPageToOpenFilePath()));
browserComboBox.setSelectedItem(configuration.settings().getBrowserFamily());
generatedChooseFile.setText(toSystemIndependentName(configuration.settings().getGeneratedFilePath()));
openInBrowserCheckBox.setSelected(configuration.settings().isShouldOpenInBrowserAfterTranslation());
}
@Override
protected void applyEditorTo(@NotNull K2JSRunConfiguration configuration) throws ConfigurationException {
configuration.settings().setFilePath(FileUtil.toSystemIndependentName(chooseFile.getText()));
K2JSConfigurationSettings settings = configuration.settings();
settings.setPageToOpenFilePath(toSystemIndependentName(htmlChooseFile.getText()));
Object item = browserComboBox.getSelectedItem();
if (item instanceof BrowsersConfiguration.BrowserFamily) {
configuration.settings().setBrowserFamily((BrowsersConfiguration.BrowserFamily)item);
settings.setBrowserFamily((BrowsersConfiguration.BrowserFamily)item);
}
settings.setGeneratedFilePath(toSystemIndependentName(generatedChooseFile.getText()));
settings.setShouldOpenInBrowserAfterTranslation(openInBrowserCheckBox.isSelected());
}
@NotNull
@Override
protected JComponent createEditor() {
FileChooserDescriptor fileChooserDescriptor = FileChooserDescriptorFactory.createSingleFileDescriptor(StdFileTypes.HTML);
fileChooserDescriptor.setRoots(ProjectRootManager.getInstance(project).getContentRootUrls());
chooseFile.addBrowseFolderListener("Choose file", "Yeah!", project, fileChooserDescriptor);
setupBrowserCombobox();
setUpShowInBrowserCheckBox();
setUpChooseHtmlToShow();
setUpBrowserCombobox();
setUpChooseGenerateFilePath();
return mainPanel;
}
private void setupBrowserCombobox() {
private void setUpChooseGenerateFilePath() {
FileChooserDescriptor fileChooserDescriptor =
FileChooserDescriptorFactory.getDirectoryChooserDescriptor("directory where generated files will be stored");
fileChooserDescriptor.setRoots(ProjectRootManager.getInstance(project).getContentRootUrls());
generatedChooseFile.addBrowseFolderListener(null, null, project, fileChooserDescriptor);
}
private void setUpShowInBrowserCheckBox() {
openInBrowserCheckBox.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
boolean selected = openInBrowserCheckBox.isSelected();
htmlChooseFile.setEnabled(selected);
browserComboBox.setEnabled(selected);
}
});
}
private void setUpChooseHtmlToShow() {
FileChooserDescriptor fileChooserDescriptor = FileChooserDescriptorFactory.createSingleFileDescriptor(StdFileTypes.HTML);
fileChooserDescriptor.setRoots(ProjectRootManager.getInstance(project).getContentRootUrls());
htmlChooseFile.addBrowseFolderListener("Choose file to show after translation is finished", null, project, fileChooserDescriptor);
}
private void setUpBrowserCombobox() {
for (BrowsersConfiguration.BrowserFamily family : BrowsersConfiguration.getInstance().getActiveBrowsers()) {
browserComboBox.addItem(family);
}
@@ -14,17 +14,13 @@
* limitations under the License.
*/
package org.jetbrains.jet.plugin.actions;
package org.jetbrains.jet.plugin.k2jsrun;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.intellij.notification.Notification;
import com.intellij.notification.NotificationType;
import com.intellij.notification.Notifications;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.project.Project;
@@ -33,9 +29,7 @@ import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiManager;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.plugin.JetMainDetector;
import org.jetbrains.k2js.facade.K2JSTranslator;
import java.util.Collection;
@@ -49,7 +43,7 @@ import static org.jetbrains.jet.plugin.actions.JavaToKotlinActionUtil.allVirtual
/**
* @author Pavel Talanov
*/
public final class TranslateToJsAction extends AnAction {
public final class K2JSRunnerUtils {
public static void notifyFailure(@NotNull Throwable exception) {
Notifications.Bus.notify(new Notification("JsTranslator", "Translation failed.",
@@ -63,36 +57,18 @@ public final class TranslateToJsAction extends AnAction {
NotificationType.INFORMATION));
}
public void actionPerformed(final AnActionEvent event) {
Runnable task = new Runnable() {
@Override
public void run() {
try {
performAction(event);
}
catch (Throwable e) {
e.printStackTrace();
notifyFailure(e);
}
}
};
ApplicationManager.getApplication().runWriteAction(task);
}
private static void performAction(@NotNull AnActionEvent event) throws Exception {
final Project project = PlatformDataKeys.PROJECT.getData(event.getDataContext());
assert project != null;
doPerform(project);
}
public static void doPerform(@NotNull Project project) throws Exception {
public static void translateAndSave(@NotNull Project project, @NotNull String outputDirPath) throws Exception {
Set<VirtualFile> allVirtualFiles = getAllProjectVirtualFiles(project);
List<JetFile> kotlinFiles = getJetFiles(allVirtualFiles, project);
String outputPath = getOutputPath(JetMainDetector.getFileWithMain(kotlinFiles));
String outputFilePath = constructPathToGeneratedFile(project, outputDirPath);
K2JSTranslator.translateWithCallToMainAndSaveToFile(kotlinFiles,
outputPath,
outputFilePath,
project);
notifySuccess(outputPath);
notifySuccess(outputDirPath);
}
private static String constructPathToGeneratedFile(Project project, String outputDirPath) {
return outputDirPath + "/" + project.getName() + ".js";
}
@NotNull
@@ -120,26 +96,4 @@ public final class TranslateToJsAction extends AnAction {
}
return kotlinFiles;
}
@NotNull
private static String getOutputPath(@Nullable PsiFile psiFile) {
if (psiFile == null) {
throw new AssertionError("Main was not detected.");
}
VirtualFile virtualFile = psiFile.getVirtualFile();
assert virtualFile != null : "Internal error: Psi file should correspond to actual virtual file";
String originalFilePath = virtualFile.getPath();
//TODO: make platform independent
String pathToDir = originalFilePath.substring(0, originalFilePath.lastIndexOf("/") + 1);
String generatedFileName = ((JetFile)psiFile).getNamespaceHeader().getName() + ".js";
return pathToDir + generatedFileName;
}
@Override
public void update(AnActionEvent e) {
// Editor editor = e.getData(PlatformDataKeys.EDITOR);
// e.getPresentation().setEnabled(editor != null);
}
}
@@ -29,7 +29,7 @@ public final class K2JSRunConfigurationType extends ConfigurationTypeBase {
}
public K2JSRunConfigurationType() {
super("K2JSConfigurationType", "K2JS", "Kotlin to Javascript", JetFileType.INSTANCE.getIcon());
super("K2JSConfigurationType", "K2JS", "Kotlin to Javascript configuration", JetFileType.INSTANCE.getIcon());
addFactory(new K2JSConfigurationFactory());
}