Skip to content

Commit

Permalink
Fix project creation when plugin is installed
Browse files Browse the repository at this point in the history
Remove help buttons on dialogs
  • Loading branch information
ze committed Apr 8, 2017
1 parent a89198a commit 3cbeac6
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 55 deletions.
15 changes: 15 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@

<extensions defaultExtensionNs="com.intellij">
<projectTemplatesFactory implementation="com.team334.frcplugin.project.ProjectFactory"/>
<projectService serviceInterface="com.team334.frcplugin.Settings"
serviceImplementation="com.team334.frcplugin.Settings"/>
<applicationService serviceInterface="com.team334.frcplugin.Settings"
serviceImplementation="com.team334.frcplugin.Settings"
/>
</extensions>

<depends optional="false">AntSupport</depends>
Expand Down
5 changes: 3 additions & 2 deletions src/com/team334/frcplugin/Icons.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

public final class Icons {

private Icons() {}
private Icons() {
}

public static final Icon WPI = IconLoader.findIcon("/icons/wpi.png" );
public static final Icon WPI = IconLoader.findIcon("/icons/wpi.png");
}
24 changes: 8 additions & 16 deletions src/com/team334/frcplugin/Settings.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
package com.team334.frcplugin;

import com.intellij.openapi.components.PersistentStateComponent;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.components.State;
import com.intellij.openapi.components.Storage;
import com.intellij.openapi.components.StoragePathMacros;
import com.intellij.util.xmlb.XmlSerializerUtil;
import org.jetbrains.annotations.Nullable;

import java.io.File;

import static java.io.File.separator;

@State(
name = "Settings",
storages = {
@Storage(StoragePathMacros.WORKSPACE_FILE),
@Storage("settings.xml")
}
)
public final class Settings implements PersistentStateComponent<Settings> {
@State(name = "Settings", storages = @Storage("other.xml"))
public class Settings implements PersistentStateComponent<Settings> {
private static String teamNumber = String.valueOf(0);
private static String version = "current";
private static String projectPackage = "org.usfirst.frc.team" + teamNumber + ".robot";
Expand All @@ -28,9 +20,9 @@ public final class Settings implements PersistentStateComponent<Settings> {

public static boolean installed = false;

public static final Settings INSTANCE = new Settings();

private Settings() {}
public static Settings getInstance() {
return ServiceManager.getService(Settings.class);
}

public String getTeamNumber() {
return teamNumber;
Expand Down Expand Up @@ -60,7 +52,7 @@ public Settings getState() {
}

@Override
public void loadState(Settings Settings) {
XmlSerializerUtil.copyBean(Settings, this);
public void loadState(Settings settings) {
XmlSerializerUtil.copyBean(settings, this);
}
}
16 changes: 10 additions & 6 deletions src/com/team334/frcplugin/panels/PropertiesControl.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.intellij.openapi.ui.Messages;
import com.team334.frcplugin.Settings;
import com.team334.frcplugin.wizard.Properties;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
Expand All @@ -14,26 +15,29 @@

public class PropertiesControl extends DialogWrapper {
private Settings settings;
private Properties prop;

private JPanel mainPanel;

private JFormattedTextField teamNumberField;

private JComboBox versionField;

public PropertiesControl() {
public PropertiesControl(Settings settings) {
super(false);
setTitle("Set Properties");

settings = Settings.INSTANCE;
prop = new Properties();

this.settings = settings;
versionField.setSelectedItem(settings.getVersion());

init();
}

@NotNull
@Override
protected Action[] createActions() {
return new Action[]{getOKAction(), getCancelAction()};
}

public JFormattedTextField getTeamNumberField() {
return teamNumberField;
}
Expand All @@ -49,7 +53,7 @@ protected void doOKAction() {

if (Settings.installed) {
try {
prop.serialize(WPI_PATH);
Properties.serialize(WPI_PATH);
} catch (IOException io) {
Messages.showErrorDialog("Cannot create wpilib.properties.", "Settings Serialization");
}
Expand Down
4 changes: 2 additions & 2 deletions src/com/team334/frcplugin/panels/RobotProject.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import javax.swing.*;

public class RobotProject {
private Settings settings = Settings.INSTANCE;
private Settings settings = Settings.getInstance();

private JPanel robotProject;

Expand All @@ -29,7 +29,7 @@ private void setPackageField() {
}

private JRadioButton[] getRadioButtons() {
return new JRadioButton[] {iterativeBased, commandBased, sampleBased};
return new JRadioButton[]{iterativeBased, commandBased, sampleBased};
}

public JRadioButton getSelectedRadioButton() {
Expand Down
4 changes: 2 additions & 2 deletions src/com/team334/frcplugin/project/ProjectFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ public class ProjectFactory extends ProjectTemplatesFactory {
@NotNull
@Override
public String[] getGroups() {
return new String[] {"WPILib"};
return new String[]{"WPILib"};
}

@NotNull
@Override
public ProjectTemplate[] createTemplates(@Nullable String s, WizardContext wizardContext) {
return new ProjectTemplate[] {new RobotTemplate(), new ExampleTemplate()};
return new ProjectTemplate[]{new RobotTemplate(), new ExampleTemplate()};
}

@Override
Expand Down
43 changes: 23 additions & 20 deletions src/com/team334/frcplugin/project/RobotModuleBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@
import com.intellij.openapi.module.JavaModuleType;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.util.Pair;
import com.intellij.xml.actions.xmlbeans.FileUtils;
import com.team334.frcplugin.Settings;
import com.team334.frcplugin.panels.RobotProject;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand All @@ -27,7 +26,7 @@ public ModuleWizardStep modifySettingsStep(@NotNull SettingsStep settingsStep) {
}

RobotModuleBuilder() {
String[] modules = new String[] {
String[] modules = new String[]{
"WPILib", "NetworkTables", "opencv", "cscore"
};

Expand All @@ -40,29 +39,37 @@ private class RobotStep extends ModuleWizardStep {
private ModuleWizardStep javaStep;
private RobotProject robotProject;

private Settings settings = Settings.INSTANCE;
private Settings settings = Settings.getInstance();
private String pkg = settings.getPackage();
private String folderStructure = pkg.replaceAll("\\.", File.separator);

private URL resourceUrl = this.getClass().getClassLoader().getResource("templates/");
private File resourceFolder = new File(resourceUrl.getPath());

RobotStep(SettingsStep settingsStep) {
javaStep = JavaModuleType.getModuleType().modifyProjectTypeStep(settingsStep, RobotModuleBuilder.this);
robotProject = new RobotProject();

settingsStep.addSettingsComponent(robotProject.getPanel());
}

private void getFiles() {
for (File file : resourceFolder.listFiles()) {
if (file.isFile()) {
File copy = new File(getContentEntryPath() + File.separator + file.getName());
FileUtils.copyFile(file, copy);
private File getFileFromTemplates(String resourcePath, String fileName) {
InputStream in = getClass().getResourceAsStream("/templates/" + resourcePath);

replaceStrings(file, "\\$package", pkg);
}
File f = new File(fileName);
try {
Files.copy(in, f.toPath());
} catch (IOException e) {
Messages.showErrorDialog("Failed to copy build files.", "Project Creation Error");
}

return f;
}

private void getBuildFiles() {
String fileBase = getContentEntryPath() + File.separator + "build.";
File properties = getFileFromTemplates("build.properties", fileBase + "properties");
File xml = getFileFromTemplates("build.xml", fileBase + "xml");

replaceStrings(xml, "\\$package", pkg);
replaceStrings(properties, "\\$package", pkg);
}

private void copyRobotFile(JRadioButton button) {
Expand All @@ -80,12 +87,8 @@ private void copyRobotFile(JRadioButton button) {
break;
}

File file = new File(resourceFolder, end);

String path = Pair.getFirst(getSourcePaths().get(0)) + File.separator + folderStructure;
File robot = new File(path + File.separator + "Robot.java");

FileUtils.copyFile(file, robot);
File robot = getFileFromTemplates(end, path + File.separator + "Robot.java");

replaceStrings(robot, "\\$package", pkg);
}
Expand Down Expand Up @@ -114,7 +117,7 @@ public JComponent getComponent() {
public void updateDataModel() {
javaStep.updateDataModel();

getFiles();
getBuildFiles();
if (new File(Pair.getFirst(getSourcePaths().get(0)), folderStructure).mkdirs()) {
copyRobotFile(robotProject.getSelectedRadioButton());
}
Expand Down
5 changes: 3 additions & 2 deletions src/com/team334/frcplugin/wizard/Installer.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

public class Installer extends AnAction {
private Properties props = new Properties();
private Settings settings = Settings.INSTANCE;
private Settings settings = Settings.getInstance();

private Logger logger;
private Thread t = new Thread(this::install);
Expand All @@ -50,9 +50,10 @@ private class Logger extends DialogWrapper {
init();
}

@NotNull
@Override
protected Action[] createActions() {
return new Action[] { getOKAction(), getCancelAction() };
return new Action[]{getOKAction(), getCancelAction()};
}

private void shiftProgress(int shift) {
Expand Down
6 changes: 3 additions & 3 deletions src/com/team334/frcplugin/wizard/Properties.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
import java.io.IOException;

public class Properties extends AnAction {
private Settings settings = Settings.INSTANCE;
private static Settings settings = Settings.getInstance();

static final File WPI_DIR = new File(Settings.WPI_PATH);

@Override
public void actionPerformed(AnActionEvent e) {
PropertiesControl props = new PropertiesControl();
PropertiesControl props = new PropertiesControl(settings);

props.getTeamNumberField().setText(settings.getTeamNumber());
props.getVersionField().setSelectedItem(settings.getVersion());
Expand All @@ -26,7 +26,7 @@ public void actionPerformed(AnActionEvent e) {
props.show();
}

public void serialize(String dir) throws IOException {
public static void serialize(String dir) throws IOException {
FileWriter fw = new FileWriter(new File(dir, "wpilib.properties"));

fw.write("# new properties entered here will be removed on plugin update\n");
Expand Down

0 comments on commit 3cbeac6

Please sign in to comment.