Skip to content

Commit e3ff96e

Browse files
committed
Add simplified version of New Project dialog
1 parent c104605 commit e3ff96e

7 files changed

Lines changed: 190 additions & 61 deletions

File tree

src/sporemodder/ProjectManager.java

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,8 @@
2424
import java.nio.file.Files;
2525
import java.nio.file.StandardCopyOption;
2626
import java.nio.file.StandardOpenOption;
27-
import java.util.ArrayList;
28-
import java.util.Collection;
29-
import java.util.Collections;
30-
import java.util.HashMap;
31-
import java.util.LinkedHashMap;
32-
import java.util.List;
33-
import java.util.ListIterator;
34-
import java.util.Map;
35-
import java.util.Properties;
36-
import java.util.TreeMap;
27+
import java.util.*;
28+
import java.util.stream.Collectors;
3729

3830
import javax.xml.parsers.ParserConfigurationException;
3931
import javax.xml.transform.TransformerException;
@@ -2302,4 +2294,44 @@ public boolean showSaveDialog() {
23022294
return closeEditedFileDecision == CloseEditedFileDecision.SAVE;
23032295
}
23042296
}
2297+
2298+
private void createNewProjectCommon(ModBundle modBundle, String projectName, List<ProjectPreset> presets) throws ParserConfigurationException, TransformerException, IOException {
2299+
// Set default presets if not specified
2300+
if (presets == null) {
2301+
presets = this.presets.stream().filter(ProjectPreset::isRecommendable).collect(Collectors.toList());
2302+
}
2303+
2304+
// Create package project
2305+
Project project = new Project(projectName, modBundle);
2306+
modBundle.addProject(project);
2307+
2308+
// Add project references
2309+
project.getReferences().addAll(presets.stream().map(preset -> getProject(preset.getName())).collect(Collectors.toList()));
2310+
2311+
// Initialize package project folder
2312+
initializeProject(project);
2313+
2314+
// Save ModInfo
2315+
if (!modBundle.hasCustomModInfo()) {
2316+
modBundle.saveModInfo();
2317+
}
2318+
}
2319+
2320+
public void createNewMod(String modName, String uniqueTag, String description, String projectName, List<ProjectPreset> presets) throws IOException, ParserConfigurationException, TransformerException, InterruptedException {
2321+
ModBundle modBundle = new ModBundle(modName);
2322+
modBundle.setDescription(description);
2323+
modBundle.setUniqueTag(uniqueTag);
2324+
initializeModBundle(modBundle);
2325+
createNewProjectCommon(modBundle, projectName, presets);
2326+
2327+
// Initialize git repository, but don't try if git is not installed
2328+
if (GitHubManager.get().hasGitInstalled()) {
2329+
initializeModBundleGit(modBundle);
2330+
}
2331+
}
2332+
2333+
public void createNewProjectInMod(ModBundle modBundle, String projectName, List<ProjectPreset> presets) throws ParserConfigurationException, IOException, TransformerException {
2334+
assert modBundle != null;
2335+
createNewProjectCommon(modBundle, projectName, presets);
2336+
}
23052337
}

src/sporemodder/util/ModBundle.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ public enum ExperimentalStatus {
8484
public static final String UNIQUETAG_ALLOWED_REGEX = "[" + UNIQUETAG_REGEX + "]";
8585
public static final String UNIQUETAG_FORBIDDEN_REGEX = "[^" + UNIQUETAG_REGEX + "]";
8686

87+
public static final String NAME_ILLEGAL_CHARACTERS = "/\\[]{}";
88+
8789
private final String name;
8890
private final List<Project> projects = new ArrayList<>();
8991
/** The base folder with all the data and source code of the mod. */

src/sporemodder/view/IntroUI.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,7 @@
3232
import sporemodder.ProjectManager;
3333
import sporemodder.UIManager;
3434
import sporemodder.util.Project;
35-
import sporemodder.view.dialogs.CreateProjectUI;
36-
import sporemodder.view.dialogs.OpenProjectUI;
37-
import sporemodder.view.dialogs.UnpackPackageUI;
38-
import sporemodder.view.dialogs.UnpackPresetsUI;
35+
import sporemodder.view.dialogs.*;
3936

4037
public class IntroUI implements Controller {
4138

@@ -78,7 +75,7 @@ private void setHyperlinkURL(Hyperlink hyperlink, String url) {
7875
@FXML private void initialize() {
7976

8077
btnNew.setOnAction(event -> {
81-
CreateProjectUI.show();
78+
CreateProjectSimpleUI.show();
8279
});
8380
btnNew.setTooltip(new Tooltip("Creates a new empty project to start modding."));
8481

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<?import javafx.scene.control.Label?>
4+
<?import javafx.scene.control.TextField?>
5+
<?import javafx.scene.layout.HBox?>
6+
<?import javafx.scene.layout.VBox?>
7+
8+
9+
<VBox fx:id="mainNode" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefWidth="450.0" spacing="10.0" xmlns="http://javafx.com/javafx/20.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sporemodder.view.dialogs.CreateProjectSimpleUI">
10+
<children>
11+
<HBox alignment="CENTER_LEFT" spacing="5.0">
12+
<children>
13+
<Label text="Mod Name:" />
14+
<TextField fx:id="modNameTextField" HBox.hgrow="ALWAYS" />
15+
</children>
16+
</HBox>
17+
<Label fx:id="warningLabel" text="Label" visible="false" />
18+
</children>
19+
</VBox>
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package sporemodder.view.dialogs;
2+
3+
import javafx.fxml.FXML;
4+
import javafx.scene.Node;
5+
import javafx.scene.control.*;
6+
import sporemodder.ProjectManager;
7+
import sporemodder.UIManager;
8+
import sporemodder.util.ModBundle;
9+
import sporemodder.view.Controller;
10+
11+
import javax.xml.parsers.ParserConfigurationException;
12+
import javax.xml.transform.TransformerException;
13+
import java.io.IOException;
14+
15+
public class CreateProjectSimpleUI implements Controller {
16+
17+
private Dialog<ButtonType> dialog;
18+
@FXML
19+
private Node mainNode;
20+
@FXML
21+
private TextField modNameTextField;
22+
@FXML
23+
private Label warningLabel;
24+
25+
@FXML
26+
private void initialize() {
27+
warningLabel.setGraphic(UIManager.get().getAlertIcon(Alert.AlertType.WARNING, 16, 16));
28+
29+
// Set a project text
30+
modNameTextField.setText("Project " + (ProjectManager.get().getProjects().size() + 1));
31+
32+
modNameTextField.requestFocus();
33+
modNameTextField.selectAll();
34+
modNameTextField.requestFocus();
35+
36+
// Ban illegal characters
37+
modNameTextField.setTextFormatter(new TextFormatter<>(change -> {
38+
String text = change.getControlNewText();
39+
if (ModBundle.NAME_ILLEGAL_CHARACTERS.chars().anyMatch(c -> text.indexOf(c) != -1)) {
40+
return null;
41+
} else {
42+
return change;
43+
}
44+
}));
45+
}
46+
47+
private void validateModFields() {
48+
boolean showWarning = true;
49+
// Show warning if name collides
50+
String modName = modNameTextField.getText();
51+
if (ProjectManager.get().hasModBundle(modName) ||
52+
ProjectManager.get().hasProject(modName)) {
53+
warningLabel.setText("A project with this name already exists");
54+
} else if (modName.isBlank()) {
55+
warningLabel.setText("Mod name cannot be blank");
56+
} else {
57+
showWarning = false;
58+
}
59+
60+
warningLabel.setVisible(showWarning);
61+
dialog.getDialogPane().lookupButton(ButtonType.OK).setDisable(showWarning);
62+
}
63+
64+
public void createMod() throws IOException, InterruptedException, ParserConfigurationException, TransformerException {
65+
ProjectManager.get().createNewMod(
66+
modNameTextField.getText(),
67+
ModBundle.generateUniqueTagFromName(modNameTextField.getText()),
68+
"",
69+
modNameTextField.getText(),
70+
null
71+
);
72+
}
73+
74+
public static void show() {
75+
CreateProjectSimpleUI node = UIManager.get().loadUI("dialogs/CreateProjectSimpleUI");
76+
node.dialog = new Dialog<>();
77+
node.dialog.getDialogPane().setContent(node.getMainNode());
78+
79+
node.dialog.setTitle("Create new mod project");
80+
node.dialog.getDialogPane().getButtonTypes().setAll(ButtonType.CANCEL, ButtonType.OK, ButtonType.NEXT);
81+
82+
((Button)node.dialog.getDialogPane().lookupButton(ButtonType.NEXT)).setText("Advanced Options");
83+
84+
node.validateModFields();
85+
86+
UIManager.get().showDialog(node.dialog).ifPresent(result -> {
87+
if (result == ButtonType.NEXT) {
88+
CreateProjectUI.show();
89+
} else if (result == ButtonType.OK && UIManager.get().tryAction(node::createMod,
90+
"Cannot initialize project. Try manually deleting the project folder in SporeModder FX\\Projects\\"))
91+
{
92+
ProjectManager.get().setActive(ProjectManager.get().getProject(node.modNameTextField.getText()));
93+
}
94+
});
95+
}
96+
97+
@Override
98+
public Node getMainNode() {
99+
return mainNode;
100+
}
101+
}

src/sporemodder/view/dialogs/CreateProjectUI.java

Lines changed: 22 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,13 @@
5050

5151
public class CreateProjectUI implements Controller {
5252

53-
private static final String ILLEGAL_CHARACTERS = "/\\[]{}";
54-
5553
private static final String WARNING_UNIQUE_TAG_EMPTY = "Unique tag cannot be empty";
5654
private static final String WARNING_MOD_NAME_EMPTY = "Mod name cannot be empty";
5755
private static final String WARNING_PROJECT_NAME_EMPTY = "Package project name cannot be empty";
5856
private static final String WARNING_MOD_NAME_REPEATED = "A mod with this name already exists";
5957
private static final String WARNING_PROJECT_NAME_REPEATED = "A package project with this name already exists";
60-
private static final String WARNING_MOD_NAME_INVALID = "Mod name cannot contain any character from " + ILLEGAL_CHARACTERS;
61-
private static final String WARNING_PROJECT_NAME_INVALID = "Package project name cannot contain any character from " + ILLEGAL_CHARACTERS;
58+
private static final String WARNING_MOD_NAME_INVALID = "Mod name cannot contain any character from " + ModBundle.NAME_ILLEGAL_CHARACTERS;
59+
private static final String WARNING_PROJECT_NAME_INVALID = "Package project name cannot contain any character from " + ModBundle.NAME_ILLEGAL_CHARACTERS;
6260

6361
private Dialog<ButtonType> dialog;
6462

@@ -103,42 +101,26 @@ public Pane getMainNode() {
103101
}
104102

105103
public void createMod() throws IOException, InterruptedException, ParserConfigurationException, TransformerException {
106-
ProjectManager projectManager = ProjectManager.get();
107-
108-
// Create base mod
109-
ModBundle modBundle;
110-
if (newModButton.isSelected()) {
111-
modBundle = new ModBundle(modNameField.getText());
112-
modBundle.setDescription(descriptionTextField.getText());
113-
modBundle.setUniqueTag(uniqueTagTextField.getText());
114-
projectManager.initializeModBundle(modBundle);
115-
} else {
116-
modBundle = projectManager.getModBundle(existingModChoiceBox.getValue());
117-
assert modBundle != null;
118-
}
119-
120-
// Create package project
121-
Project project = new Project(projectNameField.getText(), modBundle);
122-
modBundle.addProject(project);
123-
124-
// Add project references
125-
project.getReferences().addAll(presetBoxes.stream()
104+
List<ProjectPreset> presets = presetBoxes.stream()
126105
.filter(CheckBox::isSelected)
127-
.map(box -> projectManager.getProject(((ProjectPreset)box.getUserData()).getName()))
106+
.map(box -> ((ProjectPreset)box.getUserData()))
128107
.filter(Objects::nonNull)
129-
.collect(Collectors.toList()));
130-
131-
// Initialize package project folder
132-
projectManager.initializeProject(project);
133-
134-
// Save ModInfo
135-
if (!modBundle.hasCustomModInfo()) {
136-
modBundle.saveModInfo();
137-
}
108+
.collect(Collectors.toList());
138109

139-
// Initialize git repository, but don't try if git is not installed
140-
if (newModButton.isSelected() && GitHubManager.get().hasGitInstalled()) {
141-
projectManager.initializeModBundleGit(modBundle);
110+
if (newModButton.isSelected()) {
111+
ProjectManager.get().createNewMod(
112+
modNameField.getText(),
113+
uniqueTagTextField.getText(),
114+
descriptionTextField.getText(),
115+
projectNameField.getText(),
116+
presets
117+
);
118+
} else {
119+
ProjectManager.get().createNewProjectInMod(
120+
ProjectManager.get().getModBundle(existingModChoiceBox.getValue()),
121+
projectNameField.getText(),
122+
presets
123+
);
142124
}
143125
}
144126

@@ -162,7 +144,7 @@ public static void show() {
162144
}
163145

164146
private static boolean hasIllegalChar(String text) {
165-
return ILLEGAL_CHARACTERS.chars().anyMatch(c -> text.indexOf(c) != -1);
147+
return ModBundle.NAME_ILLEGAL_CHARACTERS.chars().anyMatch(c -> text.indexOf(c) != -1);
166148
}
167149

168150
private boolean isValid() {
@@ -224,15 +206,15 @@ private void initialize() {
224206
// Ban illegal characters
225207
modNameField.setTextFormatter(new TextFormatter<>(change -> {
226208
String text = change.getControlNewText();
227-
if (ILLEGAL_CHARACTERS.chars().anyMatch(c -> text.indexOf(c) != -1)) {
209+
if (ModBundle.NAME_ILLEGAL_CHARACTERS.chars().anyMatch(c -> text.indexOf(c) != -1)) {
228210
return null;
229211
} else {
230212
return change;
231213
}
232214
}));
233215
projectNameField.setTextFormatter(new TextFormatter<>(change -> {
234216
String text = change.getControlNewText();
235-
if (ILLEGAL_CHARACTERS.chars().anyMatch(c -> text.indexOf(c) != -1)) {
217+
if (ModBundle.NAME_ILLEGAL_CHARACTERS.chars().anyMatch(c -> text.indexOf(c) != -1)) {
236218
return null;
237219
} else {
238220
return change;

src/sporemodder/view/ribbons/ProgramMenu.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,7 @@
4747
import sporemodder.UIManager;
4848
import sporemodder.util.Project;
4949
import sporemodder.view.UIUpdateListener;
50-
import sporemodder.view.dialogs.CreateProjectUI;
51-
import sporemodder.view.dialogs.OpenProjectUI;
52-
import sporemodder.view.dialogs.ProgramSettingsUI;
53-
import sporemodder.view.dialogs.UnpackPackageUI;
54-
import sporemodder.view.dialogs.UnpackPresetsUI;
50+
import sporemodder.view.dialogs.*;
5551

5652
/**
5753
* The main program menu, which is the first tab of the ribbon. This program menu shows a dropdown panel with a vertical division:
@@ -208,7 +204,7 @@ public void initialize(Ribbon ribbon) {
208204

209205
newProjectButton = createButton("New project", null);
210206
newProjectButton.setOnAction((event) -> {
211-
CreateProjectUI.show();
207+
CreateProjectSimpleUI.show();
212208
});
213209
newProjectButton.setTooltip(new Tooltip("Creates a new empty project to start modding."));
214210

0 commit comments

Comments
 (0)