Encapsulation optional (WIP)

This commit is contained in:
nora 2020-11-23 16:48:55 +01:00
parent b202f6b9ad
commit f8e81462a5
7 changed files with 121 additions and 36 deletions

View file

@ -57,4 +57,18 @@ public abstract class Regex {
* </ul> * </ul>
*/ */
public static final Pattern ARG_SPLIT_REGEX = Pattern.compile(" *(\\w+) *: *(\\w+)"); public static final Pattern ARG_SPLIT_REGEX = Pattern.compile(" *(\\w+) *: *(\\w+)");
public static final Pattern FIELD_FIND_REGEX_NO_ENCAPSULATION = Pattern.compile(" *(?<capsule>[+\\-~#])* *(?<name>\\w+) *: *(?<type>\\w+)");
public static final Pattern METHOD_FIND_REGEX_NO_ENCAPSULATION = Pattern.compile(" *(?<capsule>[+\\-~#])* *(?<name>\\w+) *\\( *(?<args>(?: *\\w+ *: *\\w+ *,? *)*) *\\) *(?:: *(?<return>\\w+))?");
public static String getMethodPattern(boolean encapsulation){
return encapsulation ? METHOD_FIND_REGEX_NO_ENCAPSULATION.pattern() : METHOD_FIND_REGEX.pattern();
}
public static String getFieldPattern(boolean encapsulation){
return encapsulation ? FIELD_FIND_REGEX_NO_ENCAPSULATION.pattern() : FIELD_FIND_REGEX.pattern();
}
} }

View file

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

View file

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="UMLClassView"> <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="6" 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="7" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="3" left="3" bottom="3" right="3"/> <margin top="3" left="3" bottom="3" right="3"/>
<constraints> <constraints>
<xy x="20" y="20" width="500" height="400"/> <xy x="20" y="20" width="500" height="400"/>
@ -10,7 +10,7 @@
<children> <children>
<tabbedpane id="97eb"> <tabbedpane id="97eb">
<constraints> <constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"> <grid row="0" column="0" row-span="1" col-span="3" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
<preferred-size width="200" height="200"/> <preferred-size width="200" height="200"/>
</grid> </grid>
</constraints> </constraints>
@ -105,7 +105,7 @@
</tabbedpane> </tabbedpane>
<component id="fa538" class="javax.swing.JLabel"> <component id="fa538" class="javax.swing.JLabel">
<constraints> <constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/> <grid row="1" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints> </constraints>
<properties> <properties>
<text value="Settings:"/> <text value="Settings:"/>
@ -113,7 +113,7 @@
</component> </component>
<component id="8f883" class="javax.swing.JCheckBox" binding="watermarkBox"> <component id="8f883" class="javax.swing.JCheckBox" binding="watermarkBox">
<constraints> <constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/> <grid row="2" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints> </constraints>
<properties> <properties>
<selected value="false"/> <selected value="false"/>
@ -123,25 +123,43 @@
</component> </component>
<vspacer id="5f3c8"> <vspacer id="5f3c8">
<constraints> <constraints>
<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"/> <grid row="6" column="2" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints> </constraints>
</vspacer> </vspacer>
<component id="fe6ca" class="javax.swing.JCheckBox" binding="generateGetSetButton"> <component id="fe6ca" class="javax.swing.JCheckBox" binding="generateGetSetBox">
<constraints> <constraints>
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/> <grid row="3" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints> </constraints>
<properties> <properties>
<text value="Generate Getter/Setter"/> <text value="Generate Getter/Setter"/>
</properties> </properties>
</component> </component>
<component id="9da58" class="javax.swing.JCheckBox" binding="autoFillConstructor"> <component id="9da58" class="javax.swing.JCheckBox" binding="autoFillConstructorBox">
<constraints> <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"/> <grid row="4" column="0" row-span="1" col-span="3" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints> </constraints>
<properties> <properties>
<text value="Auto-Fill Contructor"/> <text value="Auto-Fill Contructor"/>
</properties> </properties>
</component> </component>
<component id="af39e" class="javax.swing.JCheckBox" binding="ignoreEncapsulationBox">
<constraints>
<grid row="5" 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="Ignore Encapsulation"/>
</properties>
</component>
<component id="9a1ec" class="javax.swing.JTextField" binding="defaultEncapsulationField">
<constraints>
<grid row="5" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties>
<enabled value="false"/>
</properties>
</component>
</children> </children>
</grid> </grid>
</form> </form>

View file

@ -18,8 +18,10 @@ public class UMLClassView {
private JButton convertFileButton; private JButton convertFileButton;
private JTextField packagePathField; private JTextField packagePathField;
private JCheckBox watermarkBox; private JCheckBox watermarkBox;
private JCheckBox generateGetSetButton; private JCheckBox generateGetSetBox;
private JCheckBox autoFillConstructor; private JCheckBox autoFillConstructorBox;
private JCheckBox ignoreEncapsulationBox;
private JTextField defaultEncapsulationField;
private UMLManager manager; private UMLManager manager;
@ -72,20 +74,45 @@ public class UMLClassView {
refreshTextArea(); refreshTextArea();
}); });
generateGetSetButton.addActionListener(new ActionListener() { generateGetSetBox.addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
manager.setGetSetAuto(generateGetSetButton.isSelected()); manager.setGetSetAuto(generateGetSetBox.isSelected());
refreshTextArea(); refreshTextArea();
} }
}); });
autoFillConstructor.addActionListener(new ActionListener() { autoFillConstructorBox.addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
manager.setAutoGenerateConstructor(autoFillConstructor.isSelected()); manager.setAutoGenerateConstructor(autoFillConstructorBox.isSelected());
refreshTextArea(); refreshTextArea();
} }
}); });
ignoreEncapsulationBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
manager.setIgnoreEcapsulation(ignoreEncapsulationBox.isSelected());
defaultEncapsulationField.setEnabled(ignoreEncapsulationBox.isSelected());
refreshTextArea();
}
});
defaultEncapsulationField.addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
UMLManager.setDefaultEncapsulation(defaultEncapsulationField.getText() + " ");
}
@Override
public void keyPressed(KeyEvent e) {
UMLManager.setDefaultEncapsulation(defaultEncapsulationField.getText() + " ");
}
@Override
public void keyReleased(KeyEvent e) {
UMLManager.setDefaultEncapsulation(defaultEncapsulationField.getText() + " ");
}
});
} }
private void refreshTextArea(){ private void refreshTextArea(){
@ -107,10 +134,10 @@ public class UMLClassView {
} }
public boolean isGetSetAutoSelected() { public boolean isGetSetAutoSelected() {
return generateGetSetButton.isSelected(); return generateGetSetBox.isSelected();
} }
public boolean isAutoConstructorSelected() { public boolean isAutoConstructorSelected() {
return autoFillConstructor.isSelected(); return autoFillConstructorBox.isSelected();
} }
} }

View file

@ -7,6 +7,8 @@ public class UMLField {
private final String name; private final String name;
private final String encapsulation; private final String encapsulation;
private UMLManager manager;
private boolean valid; private boolean valid;
/** /**
@ -14,11 +16,14 @@ public class UMLField {
* *
* @param line Format: "- name: String" * @param line Format: "- name: String"
*/ */
public UMLField(String line) { public UMLField(String line, UMLManager manager) {
this.manager = manager;
String formatted = line.replaceAll(Regex.FIELD_FIND_REGEX.pattern(), "$1;$3;$2"); String formatted = line.replaceAll(Regex.getFieldPattern(manager.isIgnoreEcapsulation()), "$1;$3;$2");
String[] formattedSplit = formatted.split(";"); String[] formattedSplit = formatted.split(";");
if(!manager.isIgnoreEcapsulation()) {
System.out.println("not ignore");
this.encapsulation = switch (formattedSplit[0]) { this.encapsulation = switch (formattedSplit[0]) {
case "+" -> "public "; case "+" -> "public ";
case "-" -> "private "; case "-" -> "private ";
@ -26,6 +31,9 @@ public class UMLField {
case "~" -> ""; case "~" -> "";
default -> "[undefined] "; default -> "[undefined] ";
}; };
} else {
this.encapsulation = UMLManager.DEFAULT_ENCAPSULATION;
}
this.name = formattedSplit[2]; this.name = formattedSplit[2];
this.dataType = formattedSplit[1]; this.dataType = formattedSplit[1];

View file

@ -8,10 +8,12 @@ import java.util.ArrayList;
*/ */
public class UMLManager { public class UMLManager {
public static String DEFAULT_ENCAPSULATION = "";
private UMLClassView view; private UMLClassView view;
private boolean showWatermark; private boolean showWatermark;
private boolean getSetAuto; private boolean getSetAuto;
private boolean autoFillConstructor; private boolean autoFillConstructor;
private boolean ignoreEcapsulation;
public UMLManager(UMLClassView view) { public UMLManager(UMLClassView view) {
this.view = view; this.view = view;
@ -76,4 +78,16 @@ public class UMLManager {
public void setAutoGenerateConstructor(boolean selected) { public void setAutoGenerateConstructor(boolean selected) {
autoFillConstructor = selected; autoFillConstructor = selected;
} }
public boolean isIgnoreEcapsulation() {
return ignoreEcapsulation;
}
public void setIgnoreEcapsulation(boolean ignoreEcapsulation) {
this.ignoreEcapsulation = ignoreEcapsulation;
}
public static void setDefaultEncapsulation(String defaultEncapsulation) {
DEFAULT_ENCAPSULATION = defaultEncapsulation;
}
} }

View file

@ -32,6 +32,7 @@ public class UMLMethod {
String formatted = line.replaceAll(Regex.METHOD_FIND_REGEX.pattern(), "$1;$4;$2;$3"); String formatted = line.replaceAll(Regex.METHOD_FIND_REGEX.pattern(), "$1;$4;$2;$3");
String[] parts = formatted.split(";"); String[] parts = formatted.split(";");
if(!manager.isIgnoreEcapsulation()) {
this.encapsulation = switch (parts[0]) { this.encapsulation = switch (parts[0]) {
case "+" -> "public "; case "+" -> "public ";
case "-" -> "private "; case "-" -> "private ";
@ -39,6 +40,9 @@ public class UMLMethod {
case "~" -> ""; case "~" -> "";
default -> "[undefined] "; default -> "[undefined] ";
}; };
} else {
this.encapsulation = UMLManager.DEFAULT_ENCAPSULATION;
}
this.name = parts[2]; this.name = parts[2];