Skip to content

Commit

Permalink
Refactoring of LayoutAction, implementation of getting most recent ve…
Browse files Browse the repository at this point in the history
…rsion of file from editor.
  • Loading branch information
tmorcinek committed Sep 27, 2014
1 parent 305ea89 commit 2cd6cae
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.morcinek.android.codegenerator.plugin.actions;

import com.morcinek.android.codegenerator.CodeGenerator;
import com.morcinek.android.codegenerator.codegeneration.providers.ResourceProvidersFactory;
import com.morcinek.android.codegenerator.codegeneration.providers.factories.ActivityResourceProvidersFactory;
import com.morcinek.android.codegenerator.plugin.codegenerator.CodeGeneratorFactory;

/**
* Copyright 2014 Tomasz Morcinek. All rights reserved.
Expand All @@ -15,7 +14,12 @@ protected String getResourceName() {
}

@Override
protected CodeGenerator getCodeGenerator() {
return CodeGeneratorFactory.createCodeGenerator("Activity_template", new ActivityResourceProvidersFactory());
protected String getTemplateName() {
return "Activity_template";
}

@Override
protected ResourceProvidersFactory getResourceProvidersFactory() {
return new ActivityResourceProvidersFactory();
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.morcinek.android.codegenerator.plugin.actions;

import com.morcinek.android.codegenerator.CodeGenerator;
import com.morcinek.android.codegenerator.codegeneration.providers.ResourceProvidersFactory;
import com.morcinek.android.codegenerator.codegeneration.providers.factories.AdapterResourceProvidersFactory;
import com.morcinek.android.codegenerator.plugin.codegenerator.CodeGeneratorFactory;

/**
* Copyright 2014 Tomasz Morcinek. All rights reserved.
Expand All @@ -15,7 +14,12 @@ protected String getResourceName() {
}

@Override
protected CodeGenerator getCodeGenerator() {
return CodeGeneratorFactory.createCodeGenerator("Adapter_template", new AdapterResourceProvidersFactory());
protected String getTemplateName() {
return "Adapter_template";
}

@Override
protected ResourceProvidersFactory getResourceProvidersFactory() {
return new AdapterResourceProvidersFactory();
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.morcinek.android.codegenerator.plugin.actions;

import com.morcinek.android.codegenerator.CodeGenerator;
import com.morcinek.android.codegenerator.codegeneration.providers.ResourceProvidersFactory;
import com.morcinek.android.codegenerator.codegeneration.providers.factories.FragmentResourceProvidersFactory;
import com.morcinek.android.codegenerator.plugin.codegenerator.CodeGeneratorFactory;

/**
* Copyright 2014 Tomasz Morcinek. All rights reserved.
Expand All @@ -15,7 +14,12 @@ protected String getResourceName() {
}

@Override
protected CodeGenerator getCodeGenerator() {
return CodeGeneratorFactory.createCodeGenerator("Fragment_template", new FragmentResourceProvidersFactory());
protected String getTemplateName() {
return "Fragment_template";
}

@Override
protected ResourceProvidersFactory getResourceProvidersFactory() {
return new FragmentResourceProvidersFactory();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.vfs.VirtualFile;
import com.morcinek.android.codegenerator.CodeGenerator;
import com.morcinek.android.codegenerator.codegeneration.providers.ResourceProvidersFactory;
import com.morcinek.android.codegenerator.plugin.actions.visibility.ActionVisibilityHelper;
import com.morcinek.android.codegenerator.plugin.codegenerator.CodeGeneratorController;
import com.morcinek.android.codegenerator.plugin.error.ErrorHandler;
import com.morcinek.android.codegenerator.plugin.persistence.Settings;
import com.morcinek.android.codegenerator.plugin.ui.CodeDialogBuilder;
Expand All @@ -17,10 +18,7 @@
import com.morcinek.android.codegenerator.plugin.utils.PackageHelper;
import com.morcinek.android.codegenerator.plugin.utils.PathHelper;
import com.morcinek.android.codegenerator.plugin.utils.ProjectHelper;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathExpressionException;
import java.io.IOException;

/**
Expand All @@ -45,7 +43,8 @@ public void actionPerformed(AnActionEvent event) {
final Project project = event.getData(PlatformDataKeys.PROJECT);
final VirtualFile selectedFile = event.getData(PlatformDataKeys.VIRTUAL_FILE);
try {
String generatedCode = getGeneratedCode(selectedFile);
CodeGeneratorController codeGeneratorController = new CodeGeneratorController(getTemplateName(), getResourceProvidersFactory());
String generatedCode = codeGeneratorController.generateCode(project, selectedFile, event.getData(PlatformDataKeys.EDITOR));
final CodeDialogBuilder codeDialogBuilder = new CodeDialogBuilder(project,
String.format(StringResources.TITLE_FORMAT_TEXT, selectedFile.getName()), generatedCode);
codeDialogBuilder.addTextSection(StringResources.SOURCE_PATH_LABEL, settings.getSourcePath());
Expand Down Expand Up @@ -79,7 +78,9 @@ public void run() {

protected abstract String getResourceName();

protected abstract CodeGenerator getCodeGenerator();
protected abstract String getTemplateName();

protected abstract ResourceProvidersFactory getResourceProvidersFactory();

private String getFolderPath(CodeDialogBuilder codeDialogBuilder) {
String sourcePath = codeDialogBuilder.getValueForLabel(StringResources.SOURCE_PATH_LABEL);
Expand All @@ -93,10 +94,6 @@ private String getFinalCode(CodeDialogBuilder codeDialogBuilder) {
return pathHelper.getMergedCodeWithPackage(packageName, modifiedCode);
}

private String getGeneratedCode(VirtualFile file) throws ParserConfigurationException, SAXException, XPathExpressionException, IOException {
return getCodeGenerator().produceCode(file.getInputStream(), file.getName());
}

@Override
public void update(AnActionEvent event) {
event.getPresentation().setVisible(actionVisibilityHelper.isVisible(event.getDataContext()));
Expand Down

0 comments on commit 2cd6cae

Please sign in to comment.