Auto Generate constructor

This commit is contained in:
nora 2020-11-23 15:42:49 +01:00
parent e3643765ec
commit b202f6b9ad
5 changed files with 51 additions and 5 deletions

View file

@ -36,7 +36,7 @@ public class UMLClass {
for (String line : linesBeheaded) {
if (line != null) {
if (line.matches(Regex.METHOD_FIND_REGEX.pattern())) { //MATCHES METHOD
methods.add(new UMLMethod(line, name));
methods.add(new UMLMethod(line, name, manager));
} else if (line.matches(Regex.FIELD_FIND_REGEX.pattern())) { //MATCHES FIELD
fields.add(new UMLField(line));
}

View file

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="UMLClassView">
<grid id="27dc6" binding="panel1" default-binding="true" layout-manager="GridLayoutManager" row-count="5" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="27dc6" binding="panel1" default-binding="true" layout-manager="GridLayoutManager" row-count="6" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="3" left="3" bottom="3" right="3"/>
<constraints>
<xy x="20" y="20" width="500" height="400"/>
@ -123,7 +123,7 @@
</component>
<vspacer id="5f3c8">
<constraints>
<grid row="4" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
<grid row="5" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<component id="fe6ca" class="javax.swing.JCheckBox" binding="generateGetSetButton">
@ -134,6 +134,14 @@
<text value="Generate Getter/Setter"/>
</properties>
</component>
<component id="9da58" class="javax.swing.JCheckBox" binding="autoFillConstructor">
<constraints>
<grid row="4" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Auto-Fill Contructor"/>
</properties>
</component>
</children>
</grid>
</form>

View file

@ -19,6 +19,7 @@ public class UMLClassView {
private JTextField packagePathField;
private JCheckBox watermarkBox;
private JCheckBox generateGetSetButton;
private JCheckBox autoFillConstructor;
private UMLManager manager;
@ -78,6 +79,13 @@ public class UMLClassView {
refreshTextArea();
}
});
autoFillConstructor.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
manager.setAutoGenerateConstructor(autoFillConstructor.isSelected());
refreshTextArea();
}
});
}
private void refreshTextArea(){
@ -101,4 +109,8 @@ public class UMLClassView {
public boolean isGetSetAutoSelected() {
return generateGetSetButton.isSelected();
}
public boolean isAutoConstructorSelected() {
return autoFillConstructor.isSelected();
}
}

View file

@ -11,11 +11,13 @@ public class UMLManager {
private UMLClassView view;
private boolean showWatermark;
private boolean getSetAuto;
private boolean autoFillConstructor;
public UMLManager(UMLClassView view) {
this.view = view;
this.showWatermark = view.isWatermarkSelected();
this.getSetAuto = view.isGetSetAutoSelected();
this.autoFillConstructor = view.isAutoConstructorSelected();
view.setManager(this);
}
@ -66,4 +68,12 @@ public class UMLManager {
public boolean isGetSetAuto() {
return getSetAuto;
}
public boolean isAutoFillConstructor(){
return autoFillConstructor;
}
public void setAutoGenerateConstructor(boolean selected) {
autoFillConstructor = selected;
}
}

View file

@ -12,6 +12,10 @@ public class UMLMethod {
private final ArrayList<String> argsNames = new ArrayList<>();
private final ArrayList<String> argsTypes = new ArrayList<>();
private boolean isConstructor;
private UMLManager manager;
private String methodBody = "";
/**
@ -20,7 +24,9 @@ public class UMLMethod {
* @param line The line in the UML diagram
* @param className The name of the class
*/
public UMLMethod(String line, String className) {
public UMLMethod(String line, String className, UMLManager manager) {
this.manager = manager;
//First, format it nicely
@ -36,8 +42,11 @@ public class UMLMethod {
this.name = parts[2];
if(className.equals(name)){
isConstructor = true;
}
if (parts[1].equals("") && !className.equals(name)) {
if (parts[1].equals("") && !isConstructor) {
this.returnType = "void ";
} else {
this.returnType = parts[1] + " ";
@ -82,6 +91,13 @@ public class UMLMethod {
}
returnString.append(") {\n ");
if(isConstructor && manager.isAutoFillConstructor()){
for (String argsName : argsNames) {
addBodyLine("this." + argsName + " = " + argsName + ";");
}
}
returnString.append(methodBody);
returnString.append("\n }\n");