Skip to content

Commit

Permalink
Fix bug where reference projects where added repeatedly, seriously la…
Browse files Browse the repository at this point in the history
…gging the user interface
  • Loading branch information
emd4600 committed Oct 18, 2024
1 parent 082ea21 commit 547c334
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 51 deletions.
9 changes: 3 additions & 6 deletions src/sporemodder/EditorManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import java.util.Properties;
import java.util.*;

import javafx.collections.ListChangeListener;
import javafx.scene.Node;
Expand Down Expand Up @@ -519,7 +516,7 @@ private void updateFileLabel(String relativePath) {
}


public void loadFixedTabs(List<String> tabPaths) {
public void loadFixedTabs(Collection<String> tabPaths) {
disableTabEvents = true;

for (String path : tabPaths) {
Expand Down Expand Up @@ -587,7 +584,7 @@ public void setUI(EditorPaneUI ui) {
}

if (!disableTabEvents) {
List<String> paths = ProjectManager.get().getActive().getFixedTabPaths();
Set<String> paths = ProjectManager.get().getActive().getFixedTabPaths();
paths.clear();
for (Tab tab : l.getList()) {

Expand Down
4 changes: 2 additions & 2 deletions src/sporemodder/ProjectManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -971,7 +971,7 @@ public void loadItemFolder(ProjectTreeItem parentItem) {
loadItemFolder(parentItem.getValue().getProject(), parentItem.getValue().getProject().getReferences(), parentItem);
}

public void loadItemFolder(Project project, List<Project> sources, ProjectTreeItem parentItem) {
public void loadItemFolder(Project project, Collection<Project> sources, ProjectTreeItem parentItem) {

/** The nodes that have already been loaded in this level. */
Map<String, ProjectTreeItem> loadedItems = createChildrenMap(null);
Expand All @@ -980,7 +980,7 @@ public void loadItemFolder(Project project, List<Project> sources, ProjectTreeIt
String relativePath = parentItem.getValue().getRelativePath();

// Iterate the sources in reverse orders, as the last ones (the least important) have to be loaded first
ListIterator<Project> iterable = sources.listIterator(sources.size());
ListIterator<Project> iterable = new LinkedList<>(sources).listIterator(sources.size());
while (iterable.hasPrevious())
{
Project source = iterable.previous();
Expand Down
40 changes: 12 additions & 28 deletions src/sporemodder/util/Project.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@

import java.io.*;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.*;
import java.util.stream.Collectors;

import sporemodder.MessageManager;
Expand Down Expand Up @@ -101,13 +97,13 @@ public enum PackageSignature {
/** External projects have a file in the Projects folder that links to the real path. */
private File externalLink;

private final List<Project> references = new ArrayList<>();
private final Set<Project> references = new LinkedHashSet<>();

/** The object that holds the path to the folder where the project DBPF is packed. */
private final GamePathConfiguration packPath;

/** A list of relative paths of all those files that are fixed tabs. */
private final List<String> fixedTabPaths = new ArrayList<>();
private final Set<String> fixedTabPaths = new LinkedHashSet<>();

/** The embedded 'editorPackages~' file that represents the package signature. */
private PackageSignature packageSignature = PackageSignature.NONE;
Expand Down Expand Up @@ -142,7 +138,7 @@ public Project(String name, File folder, File externalLink) {
onNameChanged(null);
}

public List<Project> getReferences() {
public Set<Project> getReferences() {
return references;
}

Expand Down Expand Up @@ -177,15 +173,15 @@ public void loadSettings() {
ProjectManager projectManager = ProjectManager.get();

String[] sourceNames = stringListSplit(PROPERTY_sources);
references.clear();
for (String str : sourceNames) {
Project p = projectManager.getProject(str);
if (p != null) references.add(p);
}

String[] tabPaths = stringListSplit(PROPERTY_fixedTabPaths);
for (String str : tabPaths) {
fixedTabPaths.add(str);
}
fixedTabPaths.clear();
fixedTabPaths.addAll(Arrays.asList(tabPaths));

packageName = settings.getProperty(PROPERTY_packageName);
if (packageName == null) packageName = getDefaultPackageName(name);
Expand Down Expand Up @@ -229,24 +225,12 @@ public void saveSettings() {
//if (!sources.isEmpty()) {
// Do this even if it's empty, as we need to update it if sources were removed
{
StringBuilder sb = new StringBuilder();

for (int i = 0; i < references.size(); i++) {
sb.append("\"" + references.get(i).name + "\"");
if (i != references.size()-1) sb.append("|");
}

settings.setProperty(PROPERTY_sources, sb.toString());
String referencesStr = references.stream().map(r -> '"' + r.name + '"').collect(Collectors.joining("|"));
settings.setProperty(PROPERTY_sources, referencesStr);
}
if (!fixedTabPaths.isEmpty()) {
StringBuilder sb = new StringBuilder();

for (int i = 0; i < fixedTabPaths.size(); i++) {
sb.append("\"" + fixedTabPaths.get(i) + "\"");
if (i != fixedTabPaths.size()-1) sb.append("|");
}

settings.setProperty(PROPERTY_fixedTabPaths, sb.toString());
String str = fixedTabPaths.stream().map(r -> '"' + r + '"').collect(Collectors.joining("|"));
settings.setProperty(PROPERTY_fixedTabPaths, str);
}

settings.put(PROPERTY_packageName, packageName);
Expand Down Expand Up @@ -304,7 +288,7 @@ public void setReadOnly(boolean isReadOnly) {
* Returns a list of relative paths of all those files that are fixed tabs.
* @return
*/
public List<String> getFixedTabPaths() {
public Set<String> getFixedTabPaths() {
return fixedTabPaths;
}

Expand Down
22 changes: 7 additions & 15 deletions src/sporemodder/view/dialogs/ProjectSettingsUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
package sporemodder.view.dialogs;

import java.io.File;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;

import javafx.fxml.FXML;
import javafx.scene.Node;
Expand Down Expand Up @@ -189,22 +191,12 @@ private void saveSettings(boolean saveSettingsOnExit) {
if (saveSettingsOnExit && nameChanged) {
ProjectManager.get().rename(project, tfName.getText());
}

List<Project> oldSources = project.getReferences();
List<Project> newSources = sourcesList.getItems();
boolean sourcesChanged = oldSources.size() != newSources.size();

if (!sourcesChanged) {
for (int i = 0; i < oldSources.size(); i++) {
if (oldSources.get(i) != newSources.get(i)) {
sourcesChanged = true;
break;
}
}
}


project.setPackageName(packageNameField.getText());


Set<Project> oldSources = project.getReferences();
Set<Project> newSources = new LinkedHashSet<>(sourcesList.getItems());
boolean sourcesChanged = !oldSources.equals(newSources);
if (sourcesChanged) {
project.getReferences().clear();
project.getReferences().addAll(sourcesList.getItems());
Expand Down

0 comments on commit 547c334

Please sign in to comment.