Skip to content

Commit 58f3b43

Browse files
committed
Improve reusability
1 parent ad84dee commit 58f3b43

File tree

6 files changed

+82
-61
lines changed

6 files changed

+82
-61
lines changed

src/fabs/component/ComponentCreator.java

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
import com.intellij.openapi.vfs.VfsUtil;
44
import com.intellij.openapi.vfs.VirtualFile;
5+
import com.samskivert.mustache.Template;
56
import fabs.util.AbstractCreator;
67
import fabs.util.FileUtils;
8+
import fabs.util.StringFormatter;
79
import fabs.util.TemplateRenderer;
810

911
import java.io.IOException;
@@ -31,36 +33,15 @@ public void create() throws IOException {
3133
VirtualFile componentDirectory = directory.createChildDirectory(directory, componentName);
3234

3335
FileUtils utils = new FileUtils();
34-
TemplateRenderer renderer = new TemplateRenderer();
3536

36-
templateModel.put("componentCamelcaseName", toCamelCase(componentName));
37+
templateModel.put("componentCamelcaseName", StringFormatter.toCamelCase(componentName));
3738

3839
for (int i = 0; i < filesToInclude.length; i++) {
3940
String file = filesToInclude[i];
40-
utils.writeFile(renderer.render(file, templateModel), componentDirectory.createChildData(componentDirectory, transformTemplateName(file, componentName)));
41+
utils.writeFile(
42+
TemplateRenderer.render(file, templateModel),
43+
componentDirectory.createChildData(componentDirectory, TemplateRenderer.transformTemplateName(file, componentName)
44+
));
4145
}
4246
}
43-
44-
private String toCamelCase(String input) {
45-
StringBuffer sb = new StringBuffer();
46-
for (String s : input.split("-")) {
47-
sb.append(Character.toUpperCase(s.charAt(0)));
48-
if (s.length() > 1) {
49-
sb.append(s.substring(1, s.length()).toLowerCase());
50-
}
51-
}
52-
return sb.toString();
53-
54-
}
55-
56-
private String transformTemplateName(String templateString, String componentName) {
57-
String[] parts = templateString.split("/");
58-
String fileName = parts[parts.length - 1];
59-
return fileName
60-
.replace(".mustache", "")
61-
.replace("component", componentName)
62-
;
63-
}
64-
65-
6647
}

src/fabs/component/ComponentCreatorAction.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,5 @@ public class ComponentCreatorAction extends AbstractCreatorAction {
1111
@Override
1212
protected AbstractCreator createCreator(VirtualFile directory, String componentName, Map<String, Object> templateModel, String[] files) {
1313
return new ComponentCreator(directory, componentName, templateModel, files);
14-
1514
}
1615
}

src/fabs/component/ComponentCreatorDialog.java

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package fabs.component;
22

3+
4+
import fabs.util.AbstractDialog;
5+
36
import javax.swing.*;
47
import java.awt.event.*;
58
import java.util.ArrayList;
69
import java.util.HashMap;
710
import java.util.Map;
811

9-
public class ComponentCreatorDialog extends JDialog {
12+
public class ComponentCreatorDialog extends AbstractDialog {
1013
private JPanel contentPane;
1114
private JButton buttonOK;
1215
private JButton buttonCancel;
@@ -22,24 +25,15 @@ public class ComponentCreatorDialog extends JDialog {
2225
private final String storyTemplateFile = "templates/component/component.story.tsx.mustache";
2326
private final String markdownTemplateFile = "templates/component/component.md.mustache";
2427

25-
private boolean hasCanceled = false;
2628

2729
public ComponentCreatorDialog() {
2830
setContentPane(contentPane);
2931
setModal(true);
3032
getRootPane().setDefaultButton(buttonOK);
3133

32-
buttonOK.addActionListener(new ActionListener() {
33-
public void actionPerformed(ActionEvent e) {
34-
onOK();
35-
}
36-
});
34+
buttonOK.addActionListener(e -> onOK());
3735

38-
buttonCancel.addActionListener(new ActionListener() {
39-
public void actionPerformed(ActionEvent e) {
40-
onCancel();
41-
}
42-
});
36+
buttonCancel.addActionListener(e -> onCancel());
4337

4438
// call onCancel() when cross is clicked
4539
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
@@ -50,12 +44,7 @@ public void windowClosing(WindowEvent e) {
5044
});
5145

5246
// call onCancel() on ESCAPE
53-
contentPane.registerKeyboardAction(new ActionListener() {
54-
public void actionPerformed(ActionEvent e) {
55-
onCancel();
56-
}
57-
}, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
58-
47+
contentPane.registerKeyboardAction(e -> onCancel(), KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
5948
}
6049

6150
public Map<String, Object> getTemplateVars() {
@@ -90,19 +79,4 @@ public String[] getFiles() {
9079

9180
return files.toArray(new String[files.size()]);
9281
}
93-
94-
public boolean isCanceled() {
95-
return hasCanceled;
96-
}
97-
98-
private void onOK() {
99-
hasCanceled = false;
100-
dispose();
101-
}
102-
103-
private void onCancel() {
104-
hasCanceled = true;
105-
dispose();
106-
}
107-
10882
}

src/fabs/util/AbstractDialog.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package fabs.util;
2+
3+
import javax.swing.*;
4+
import java.util.Map;
5+
6+
public abstract class AbstractDialog extends JDialog {
7+
8+
public abstract String[] getFiles();
9+
10+
public abstract Map<String, Object> getTemplateVars();
11+
12+
public abstract String getComponentName();
13+
14+
protected boolean hasCanceled = false;
15+
16+
public boolean isCanceled() {
17+
return hasCanceled;
18+
}
19+
20+
protected void onOK() {
21+
hasCanceled = false;
22+
dispose();
23+
}
24+
25+
protected void onCancel() {
26+
hasCanceled = true;
27+
dispose();
28+
}
29+
}

src/fabs/util/StringFormatter.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,21 @@ public static String toSnakeCase(String name) {
1111

1212
return buffer.toString();
1313
}
14+
15+
/**
16+
* Transform a string from dash case to camel case
17+
* @param input
18+
* @return
19+
*/
20+
public static String toCamelCase(String input) {
21+
StringBuffer sb = new StringBuffer();
22+
for (String s : input.split("-")) {
23+
sb.append(Character.toUpperCase(s.charAt(0)));
24+
if (s.length() > 1) {
25+
sb.append(s.substring(1, s.length()).toLowerCase());
26+
}
27+
}
28+
return sb.toString();
29+
30+
}
1431
}

src/fabs/util/TemplateRenderer.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,30 @@
66
import java.util.Map;
77

88
public class TemplateRenderer {
9-
public String render(String file, Map<String, Object> context) {
9+
/**
10+
* Render a template
11+
* @param file
12+
* @param context
13+
* @return
14+
*/
15+
public static String render(String file, Map<String, Object> context) {
1016
FileUtils utils = new FileUtils();
1117
Template tmpl = Mustache.compiler().compile(utils.getContent(file));
1218
return tmpl.execute(context);
1319
}
20+
21+
/**
22+
* Transform a filename
23+
* @param templateString
24+
* @param componentName
25+
* @return
26+
*/
27+
public static String transformTemplateName(String templateString, String componentName) {
28+
String[] parts = templateString.split("/");
29+
String fileName = parts[parts.length - 1];
30+
return fileName
31+
.replace(".mustache", "")
32+
.replace("component", componentName)
33+
;
34+
}
1435
}

0 commit comments

Comments
 (0)