Skip to content

Commit 2cffb8a

Browse files
Bug fix (#1)
Fixed keyboard Short Cut. Provided room for viewing generated code for in pop up for already existing classes. File chooser is restricted to current project modules.
1 parent d38e032 commit 2cffb8a

File tree

8 files changed

+169
-49
lines changed

8 files changed

+169
-49
lines changed

Documents/imageCheckBox.png

8.05 KB
Loading

Readme.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ Import the downloaded jar from disk. <br>
1515

1616
## How to Use The Plugin
1717
- Select the xml code.
18-
- press Shift + ` shortcut and provide the details.
19-
- Code will be generated at the specified location.
18+
- press Ctrl + Shift + ` shortcut and provide the details.
19+
- Code will be generated at the specified location.<br>
2020

21+
![imageCheckBox](Documents/imageCheckBox.png "imageCheckBox")
22+
- Uncheck the checkBok to avoid writing and just view the generated code in pop up.
23+
- Go to action by pressing (⌘+Shift+A or Ctrl+Shift+A) and change the shortcut by yourself.
2124

22-
![GIF](Documents/MapStructGIF.gif "GIF")
2325

2426
## Support
2527

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ application {
1414
}
1515

1616
group = "com.techconative"
17-
version = "1.0-SNAPSHOT"
17+
version = "0.0.2"
1818

1919
repositories {
2020
mavenCentral()

src/main/java/com/techconative/actions/DozerTOMapperStructPlugin.java

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
import com.intellij.openapi.editor.Editor;
88
import com.intellij.openapi.fileChooser.FileChooser;
99
import com.intellij.openapi.fileChooser.FileChooserDescriptor;
10+
import com.intellij.openapi.roots.ProjectRootManager;
1011
import com.intellij.openapi.ui.Messages;
12+
import com.intellij.openapi.vfs.VirtualFile;
1113
import com.intellij.ui.JBColor;
1214
import com.intellij.util.ui.JBUI;
1315
import com.techconative.actions.service.GenerateMappings;
@@ -21,7 +23,11 @@
2123
import javax.swing.text.StyleConstants;
2224
import java.awt.*;
2325
import java.io.IOException;
26+
import java.nio.file.FileSystems;
27+
import java.util.Arrays;
28+
import java.util.List;
2429
import java.util.Objects;
30+
import java.util.stream.Collectors;
2531

2632

2733
public class DozerTOMapperStructPlugin extends AnAction {
@@ -31,17 +37,41 @@ public void actionPerformed(@NotNull AnActionEvent e) {
3137
Editor ediTorRequiredData = e.getRequiredData(CommonDataKeys.EDITOR);
3238
CaretModel caretModel = ediTorRequiredData.getCaretModel();
3339
String selectedText = caretModel.getCurrentCaret().getSelectedText();
34-
35-
FileChooserDescriptor fileChooserDescriptor =
36-
new FileChooserDescriptor(false, true, false,
37-
false, false, false);
38-
FileChooser.chooseFile(fileChooserDescriptor, e.getProject(), null, consumer -> {
39-
JTextPanes(consumer.toNioPath().normalize().toString(), selectedText);
40+
if (selectedText == null || selectedText.equals("") || selectedText.equals(" ")) {
41+
return;
42+
}
43+
if (GenerateMappings.CheckXml(selectedText)){
44+
String code = null;
45+
try {
46+
code = GenerateMappings.generateMappings(selectedText, "path",
47+
false, "className", "attributeName");
48+
} catch (IOException | BadLocationException ex) {
49+
return;
50+
}
51+
if (code != null) {
52+
try {
53+
getJTextPlane(code);
54+
} catch (BadLocationException ex) {
55+
return;
4056
}
41-
);
57+
}
58+
}else {
59+
60+
FileChooserDescriptor fileChooserDescriptor =
61+
new FileChooserDescriptor(false, true, false,
62+
false, false, false);
63+
List<VirtualFile> virtualFiles= Arrays.stream(ProjectRootManager.getInstance(e.getProject()).getContentSourceRoots()).filter(
64+
x->(x.toNioPath().normalize().toString().replace(FileSystems.getDefault().getSeparator(),".").contains("src.main.java"))
65+
).collect(Collectors.toList());
66+
fileChooserDescriptor.setRoots(virtualFiles);
67+
FileChooser.chooseFile(fileChooserDescriptor, e.getProject(), null, consumer -> {
68+
JTextPanes(consumer.toNioPath().normalize().toString(), selectedText);
69+
}
70+
);
71+
}
4272
}
4373

44-
JTextPane getJTextPlane(String code) throws BadLocationException {
74+
public static JTextPane getJTextPlane(String code) throws BadLocationException {
4575
JFrame frame = new JFrame("GENERATED CODE");
4676
Container cp = frame.getContentPane();
4777
JTextPane pane = new JTextPane();
@@ -117,8 +147,10 @@ void JTextPanes(String path, String selectedText) {
117147
if (isSelected) {
118148
GenerateMappings.generateMappings(selectedText, path, true, className, attributeName);
119149
} else {
120-
getJTextPlane(GenerateMappings.generateMappings(selectedText, path,
121-
false, className, attributeName));
150+
String code = GenerateMappings.generateMappings(selectedText, path,
151+
false, className, attributeName);
152+
if (code != null)
153+
getJTextPlane(code);
122154
}
123155
} catch (IOException | BadLocationException ex) {
124156
Messages.showMessageDialog(String.valueOf(ex), "ERROR", Messages.getErrorIcon());

src/main/java/com/techconative/actions/service/GenerateMappings.java

Lines changed: 70 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.intellij.openapi.ui.Messages;
44
import com.squareup.javapoet.*;
5+
import com.techconative.actions.DozerTOMapperStructPlugin;
56
import com.techconative.actions.utilities.Utilities;
67
import org.mapstruct.Mapper;
78
import org.mapstruct.Mapping;
@@ -14,50 +15,73 @@
1415
import org.xml.sax.SAXException;
1516

1617
import javax.lang.model.element.Modifier;
18+
import javax.swing.text.BadLocationException;
1719
import javax.xml.parsers.DocumentBuilder;
1820
import javax.xml.parsers.DocumentBuilderFactory;
1921
import javax.xml.parsers.ParserConfigurationException;
2022
import java.io.IOException;
2123
import java.io.StringReader;
24+
import java.nio.file.FileSystems;
2225
import java.nio.file.Paths;
2326
import java.util.*;
27+
import java.util.concurrent.atomic.AtomicBoolean;
2428
import java.util.stream.IntStream;
2529

2630
public class GenerateMappings {
2731

2832
static private TypeSpec.Builder person;
2933
static private boolean alreadyExecuted = false;
3034
private static Document finalDocument;
35+
private static int length;
3136

32-
public static String generateMappings(String selectedText, String path, boolean generate,
33-
String className, String mapperName) throws IOException {
37+
38+
public static Integer check(String selectedText) {
3439
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
35-
Map<String, String> map = new HashMap<>();
3640
DocumentBuilder dBuilder = null;
41+
length = 0;
3742
try {
3843
dBuilder = dbFactory.newDocumentBuilder();
3944
} catch (ParserConfigurationException ex) {
4045
Messages.showMessageDialog(String.valueOf(ex), "ERROR", Messages.getErrorIcon());
41-
return null;
46+
return 0;
4247
}
4348
finalDocument = null;
4449
try {
4550
finalDocument = dBuilder.parse(new InputSource(new StringReader(selectedText)));
4651
} catch (SAXException | IOException | NullPointerException ex) {
4752
Messages.showMessageDialog(String.valueOf(ex), "ERROR", Messages.getErrorIcon());
48-
return null;
53+
return 0;
4954
}
5055
finalDocument.getDocumentElement().normalize();
56+
return finalDocument.getElementsByTagName("mapping").getLength();
57+
}
5158

52-
int length = finalDocument.getElementsByTagName("mapping").getLength();
59+
public static boolean CheckXml(String selectedText) {
60+
length = check(selectedText);
61+
if (length == 1 && finalDocument.getElementsByTagName("mappings").getLength() == 0) {
62+
alreadyExecuted = true;
63+
return true;
64+
} else {
65+
alreadyExecuted = false;
66+
return false;
67+
}
68+
}
69+
70+
public static String generateMappings(String selectedText, String path, boolean generate,
71+
String className, String mapperName) throws IOException, BadLocationException {
72+
if (length == 0) {
73+
return null;
74+
}
75+
Map<String, String> map = new HashMap<>();
76+
person = null;
77+
AtomicBoolean partialMapping = new AtomicBoolean(false);
5378

5479
if (length != finalDocument.getElementsByTagName("class-a").getLength() &&
5580
length != finalDocument.getElementsByTagName("class-b").getLength()) {
5681
Messages.showMessageDialog("Wrong xml structure", "ERROR", Messages.getErrorIcon());
5782
return null;
5883
}
5984

60-
6185
IntStream.range(0, length).forEachOrdered(x -> {
6286
map.clear();
6387
NodeList nodeList = finalDocument.getElementsByTagName("mapping").item(x).getChildNodes();
@@ -99,13 +123,26 @@ public static String generateMappings(String selectedText, String path, boolean
99123
if (finalDocument.getElementsByTagName("mapping").item(x).getAttributes().getNamedItem("map-id") != null) {
100124
String mapId = finalDocument.getElementsByTagName("mapping").item(x).getAttributes()
101125
.getNamedItem("map-id").getTextContent();
102-
person.addAnnotation(AnnotationSpec.builder(Named.class)
103-
.addMember("value", "$S", Utilities.apply(mapId)).build());
126+
map.put("methodMapId", mapId);
127+
}
128+
if (finalDocument.getElementsByTagName("mappings").getLength() == 0 && length >= 1 && !generate) {
129+
try {
130+
DozerTOMapperStructPlugin.getJTextPlane(generateMethod(map, annotationSpecList, true)
131+
.replaceAll("@org.mapstruct.", "@"));
132+
partialMapping.set(true);
133+
} catch (BadLocationException e) {
134+
throw new RuntimeException(e);
135+
}
136+
} else {
137+
generateMethod(map, annotationSpecList, false);
104138
}
105-
generateMethod(map, annotationSpecList);
106139
});
107-
108-
return generateJavaClass(path, generate);
140+
if (partialMapping.get()) {
141+
alreadyExecuted = false;
142+
return null;
143+
} else {
144+
return generateJavaClass(path, generate);
145+
}
109146
}
110147

111148

@@ -119,7 +156,7 @@ private static String getClassName(String value) {
119156
return strings[strings.length - 1];
120157
}
121158

122-
static void generateMethod(Map<String, String> map, List<AnnotationSpec> annotationSpecList) {
159+
static String generateMethod(Map<String, String> map, List<AnnotationSpec> annotationSpecList, boolean partialMapping) {
123160

124161
ClassName classTypeB = ClassName.get(map.get("packageB"), map.get("ClassBName"));
125162
ClassName classTypeA = ClassName.get(map.get("packageA"), map.get("ClassAName"));
@@ -128,16 +165,24 @@ static void generateMethod(Map<String, String> map, List<AnnotationSpec> annotat
128165
.methodBuilder("to" + map.get("ClassBName"))
129166
.addParameter(classTypeA, Utilities.getObjectNameForClassName(map.get("ClassAName")))
130167
.returns(classTypeB).addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT);
131-
AnnotationSpec.Builder anno = AnnotationSpec.builder(Mappings.class);
132-
IntStream.range(0, annotationSpecList.size()).forEachOrdered(x ->
133-
anno.addMember("value", "$L", annotationSpecList.get(x))
134-
);
135-
method.addAnnotation(anno.build());
168+
if (!annotationSpecList.isEmpty() && annotationSpecList != null) {
169+
AnnotationSpec.Builder anno = AnnotationSpec.builder(Mappings.class);
170+
IntStream.range(0, annotationSpecList.size()).forEachOrdered(x ->
171+
anno.addMember("value", "$L", annotationSpecList.get(x))
172+
);
173+
method.addAnnotation(anno.build());
174+
}
136175
if (map.containsKey("methodMapId")) {
137176
method.addAnnotation(AnnotationSpec.builder(Named.class)
138177
.addMember("value", "$S", Utilities.findAndApply(map.get("methodMapId"))).build());
139178
}
140-
person.addMethod(method.build());
179+
if (partialMapping) {
180+
return method.build().toString().replaceAll(map.get("packageB") + ".", "")
181+
.replaceAll(map.get("packageA") + ".", "");
182+
} else {
183+
person.addMethod(method.build());
184+
return null;
185+
}
141186
}
142187

143188
static private void buildJavaClass(String path, Map<String, String> map, String className, String mapperName) {
@@ -177,10 +222,12 @@ static private String generateJavaClass(String path, boolean generate) throws IO
177222
return code;
178223
}
179224

180-
static private String[] getPath(String path) {
181-
String str = path.replace(path.charAt(2), '.');
182-
String[] strings = str.split("src.main.java.");
183-
strings[0] = str.replaceAll("." + strings[1], "").trim().replace('.', path.charAt(2));
225+
private static String[] getPath(String path) {
226+
String fileSeparator = FileSystems.getDefault().getSeparator();
227+
String str = path.replace(fileSeparator, ".");
228+
String[] strings = new String[2];
229+
strings[1] = str.split(".src.main.java.")[1];
230+
strings[0] = path.replace(strings[1].replace(".", fileSeparator), "");
184231
return strings;
185232
}
186233

src/main/java/com/techconative/actions/test/Test.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
package com.techconative.actions.test;
22

3-
4-
import com.techconative.actions.service.GenerateMappings;
5-
3+
import javax.swing.text.BadLocationException;
64
import java.io.IOException;
75

6+
87
public class Test {
9-
public static void main(String[] args) throws IOException {
8+
public static void main(String[] args) throws IOException, BadLocationException {
109
Test test=new Test();
1110
test.run();
1211
}
13-
void run() throws IOException {
12+
void run() throws IOException, BadLocationException {
1413
String xml="<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
1514
"<mappings>\n" +
1615
" <mapping type=\"one-way\" map-id=\"Custom_Map\">\n" +
@@ -48,10 +47,8 @@ void run() throws IOException {
4847
" </mapping>\n" +
4948
"\n" +
5049
"</mappings>";
51-
GenerateMappings.generateMappings(xml,"C:\\Users\\VISHNU\\Documents\\GitHub\\plugin\\src\\main\\java\\com\\techconative\\actions\\mappers",false,"className","mapper");
52-
GenerateMappings.generateMappings(xml,"C:\\Users\\VISHNU\\Documents\\GitHub\\plugin\\src\\main\\java\\com\\techconative\\actions\\mappers",true,"className","mapper");
53-
54-
50+
// GenerateMappings.generateMappings(xml, "<GIve complete absolute path>",false,"className","mapper");
51+
// GenerateMappings.generateMappings(xml,"<GIve complete absolute path>",true,"className","mapper");
5552
}
5653

5754
}

src/main/resources/META-INF/plugin.xml

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,45 @@
1010
<!-- A displayed Vendor name or Organization ID displayed on the Plugins Page. -->
1111
<vendor email="info@techconative.com" url="https://www.techconative.com">TechConative</vendor>
1212

13+
<change-notes>><![CDATA[
14+
<b>How to Use The Plugin</b><br>
15+
16+
- Select the xml code.<br>
17+
- press Ctrl + Shift + ` shortcut and provide the details.<br>
18+
- Code will be generated at the specified location.<br>
19+
- Can run in any platform independent of OS.
20+
- Can just view generated code without writing.
21+
- Can get code in pop up for already created class and append the missing part.
22+
- you can go to action by pressing (⌘+Shift+A or Ctrl+Shift+A) and type plugin name and change the shortcut yourself.
23+
24+
]]>
25+
</change-notes>
26+
1327
<!-- Description of the plugin displayed on the Plugin Page and IDE Plugin Manager.
1428
Simple HTML elements (text formatting, paragraphs, and lists) can be added inside of <![CDATA[ ]]> tag.
1529
Guidelines: https://plugins.jetbrains.com/docs/marketplace/plugin-overview-page.html#plugin-description -->
1630
<description><![CDATA[
17-
Here is my short description for My GenerateMappings plugin.<br>
18-
<em>Generate</em>
31+
This is a productivity tool to be used when you're modernizing you're moving the mappings from Dozer to Mapstruct in your applications.
32+
33+
With this plugin,
34+
<ol>
35+
<li>Select your <i>&lt;mapper&gt;</i>, <i>&lt;mappers&gt;</i> mapper definitions.</li>
36+
<li>Press shortcut to generate equivalent Mapstruct definitions in seconds.</li>
37+
</ol>
38+
39+
What this solves,
40+
<ul>
41+
<li>Straight forward mapping definitions with fields and ignore fields.</li>
42+
<li>Mapid.</li>
43+
</ul>
44+
45+
46+
We'll be covering more cases of mapping in upcoming releases.
47+
48+
49+
Feel free to contribute to the <a href="https://github.com/techconative/DoStruct-plugin/tree/bugFix">source code</href>
50+
51+
<em>With love from <a href="https://techconative.com/">TechConative<a> ❤️</em>
1952
]]></description>
2053

2154
<!-- Product and plugin compatibility requirements.
@@ -32,7 +65,7 @@
3265
<action id="com.techconative.actions.DozerTOMapperStructPlugin" class="com.techconative.actions.DozerTOMapperStructPlugin"
3366
text="DozerTOMapperStructPlugin" description="DozerTOMapperStructPlugin">
3467
<add-to-group group-id="CodeCompletionGroup" anchor="last"/>
35-
<keyboard-shortcut keymap="$default" first-keystroke="shift BACK_QUOTE"/>
68+
<keyboard-shortcut keymap="$default" first-keystroke="shift ctrl BACK_QUOTE"/>
3669
</action>
3770
</actions>
3871
</idea-plugin>
Lines changed: 9 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)