Skip to content

Commit

Permalink
Simply setting of the plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Kane committed Jan 5, 2015
1 parent f58e4d1 commit 1230f5e
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 59 deletions.
6 changes: 5 additions & 1 deletion META-INF/plugin.xml
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
<idea-plugin version="2">
<id>JythonHelper</id>
<name>JythonHelper</name>
<version>1.1</version>
<version>1.2</version>
<vendor email="gzkaneg@gmail.com">Kane</vendor>
<idea-version since-build="117.105"/>
<depends>com.intellij.modules.python</depends>
<description>
Goal:Generate Python skeleton for Java classes in Jython projects.
Usage: Add skeleton output directory in setting: 'Jython Helper' and Java libraries path(jars). Then apply
'Jython Helper' by right clicking for single Jython file or directory containing Jython files.
Code at github: https://github.com/kaneg/JythonHelper
</description>
<change-notes>
1.2: No longer needs to manually set Target Directory. All skeletons can be generated into SDK's binary folder.
</change-notes>
<depends>com.intellij.modules.python</depends>
<actions>
<!-- Add your actions here -->
Expand Down
20 changes: 19 additions & 1 deletion src/gz/jythonhelper/EasyJython.java
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ abstract class ClassGenerator {
{"wait", "toString", "hashCode", "notify", "notifyAll", "getClass", "yield"}));

private String outputDir;
public static final String INIT_TEMPLATE = "# encoding: utf-8\n" +
"# module %s\n" +
"# from (built-in)\n" +
"# by generator 999.999\n";

protected ClassGenerator(String outputDir) {
this.outputDir = outputDir;
Expand Down Expand Up @@ -221,14 +225,27 @@ String indents(String line) {
return sb.toString();
}

void writePyHeader(FileWriter fw, String name) {
try {
fw.write(String.format(INIT_TEMPLATE, name));
} catch (IOException e) {
e.printStackTrace();
}
}

void ensureInitPy(File dir) {
File initFile = new File(dir, INIT_PY);
// if (initFile.exists() && initFile.lastModified() < startTime) {
// initFile.delete();
// }
if (!initFile.exists()) {
try {
initFile.createNewFile();
boolean newFile = initFile.createNewFile();
if (newFile) {
FileWriter fw = new FileWriter(initFile);
writePyHeader(fw, initFile.getName());
fw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
Expand Down Expand Up @@ -326,6 +343,7 @@ private Map<String, StringBuilder> readClassesFromInitPy(File initFile) throws I

private void writeClassesFromInitPy(Map<String, StringBuilder> classStringMap, File initPy) throws IOException {
FileWriter fileWriter = new FileWriter(initPy);
writePyHeader(fileWriter, initPy.getName());
for (StringBuilder classContent : classStringMap.values()) {
fileWriter.write(classContent.toString());
}
Expand Down
27 changes: 20 additions & 7 deletions src/gz/jythonhelper/JythonHelperAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.options.ShowSettingsUtil;
import com.intellij.openapi.application.PathManager;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleUtil;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import com.jetbrains.python.sdk.PythonSdkType;
import org.jetbrains.annotations.NotNull;

/**
* Created by IntelliJ IDEA.
Expand All @@ -16,7 +21,7 @@
* Time: 10:37 AM
*/
public class JythonHelperAction extends AnAction {
public void actionPerformed(AnActionEvent event) {
public void actionPerformed(@NotNull AnActionEvent event) {
final Project project = event.getData(PlatformDataKeys.PROJECT);
final VirtualFile[] vFiles = event.getData(PlatformDataKeys.VIRTUAL_FILE_ARRAY);
if (vFiles == null || vFiles.length == 0) {
Expand All @@ -26,18 +31,26 @@ public void actionPerformed(AnActionEvent event) {
VirtualFile target = vFiles[0];
JythonHelperConfig config = JythonHelperConfig.getConfig(project);
JythonHelperConfig.State state = config.getState();
if (state == null || state.targetDirectory == null || state.targetDirectory.isEmpty()) {
Messages.showInfoMessage("You need to set output directory and Java library first. Click OK to setting.", "Info");
ShowSettingsUtil.getInstance().showSettingsDialog(project, JythonHelperConfigForm.JYTHON_HELPER);
String targetDirectory = getTargetDirectory(target, project);
if (targetDirectory == null) {
return;
}
String targetDirectory = state.targetDirectory;
String[] javaLibs = state.javaLibs;
generatePy(targetDirectory, javaLibs, target.getPath());
Messages.showInfoMessage("Python files have been generated in directory:" + state.targetDirectory, "Info");
Messages.showInfoMessage("Python files have been generated in directory:" + targetDirectory, "Info");
LocalFileSystem.getInstance().refresh(false);
}

private String getTargetDirectory(VirtualFile vf, Project project) {
Module module = ModuleUtil.findModuleForFile(vf, project);
Sdk sdk = PythonSdkType.findPythonSdk(module);
if (sdk == null) {
Messages.showErrorDialog(project, "No SDK FOUND", "No SDK FOUND");
return null;
}
return PythonSdkType.getSkeletonsPath(PathManager.getSystemPath(), sdk.getHomePath());
}

private void generatePy(String outputDir, String[] libs, String... files) {
EasyJython ej = new EasyJython(outputDir);
ej.setLibs(libs);
Expand Down
1 change: 0 additions & 1 deletion src/gz/jythonhelper/JythonHelperConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
@State(name = "JythonHelperConfig", storages = {@com.intellij.openapi.components.Storage(file = "$WORKSPACE_FILE$")})
public class JythonHelperConfig implements PersistentStateComponent<JythonHelperConfig.State> {
public static class State {
public String targetDirectory;
public String javaLibs[] = new String[0];
}

Expand Down
28 changes: 2 additions & 26 deletions src/gz/jythonhelper/JythonHelperConfigForm.form
Original file line number Diff line number Diff line change
Expand Up @@ -31,39 +31,15 @@
</component>
</children>
</toolbar>
<grid id="c274b" layout-manager="GridLayoutManager" row-count="2" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="c274b" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints border-constraint="North"/>
<properties/>
<border type="none"/>
<children>
<component id="2cba3" class="javax.swing.JTextField" binding="targetDirectoryField" default-binding="true">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="6" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="291" height="28"/>
</grid>
</constraints>
<properties/>
</component>
<component id="2a4e3" class="javax.swing.JButton" binding="chooseFileButton" default-binding="true">
<constraints>
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="..."/>
</properties>
</component>
<component id="3420e" 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="Jython Out Directory:"/>
</properties>
</component>
<component id="a98de" 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="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="Java Library:"/>
Expand Down
23 changes: 0 additions & 23 deletions src/gz/jythonhelper/JythonHelperConfigForm.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ public class JythonHelperConfigForm implements Configurable {
public static final String JYTHON_HELPER = "Jython Helper";
private JPanel mainPane;
private JList<String> libList;
private JTextField targetDirectoryField;
private JButton addLibButton;
private JButton chooseFileButton;
private JButton removeLibButton;
private Project project;

Expand Down Expand Up @@ -62,25 +60,6 @@ public void actionPerformed(ActionEvent e) {
}
}
});
chooseFileButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
FileChooserDescriptor fcd = FileChooserDescriptorFactory.createSingleFolderDescriptor();
VirtualFile fileByUrl = null;
String targetDirectory = getState().targetDirectory;
if (targetDirectory != null) {
fileByUrl = VirtualFileManager.getInstance().findFileByUrl(targetDirectory);
}
FileChooser.chooseFiles(fcd, project, fileByUrl, new Consumer<List<VirtualFile>>() {
@Override
public void consume(List<VirtualFile> virtualFiles) {
for (VirtualFile virtualFile : virtualFiles) {
JythonHelperConfigForm.this.targetDirectoryField.setText(virtualFile.getPresentableUrl());
}
}
});
}
});
}

@Nls
Expand Down Expand Up @@ -109,7 +88,6 @@ public boolean isModified() {
@Override
public void apply() throws ConfigurationException {
JythonHelperConfig.State myState = getState();
myState.targetDirectory = this.targetDirectoryField.getText();
ListModel model = this.libList.getModel();
int size = model.getSize();
String[] values = new String[size];
Expand All @@ -127,7 +105,6 @@ private JythonHelperConfig.State getState() {
@Override
public void reset() {
JythonHelperConfig.State myState = getState();
this.targetDirectoryField.setText(myState.targetDirectory);
DefaultListModel<String> model = new DefaultListModel<String>();
String[] values = new String[0];
if (myState.javaLibs != null) {
Expand Down

0 comments on commit 1230f5e

Please sign in to comment.