From fbe0c33b7d96e2972b9df162521cf840209c33be Mon Sep 17 00:00:00 2001 From: mmyers Date: Fri, 31 May 2024 17:59:02 -0500 Subject: [PATCH] Add mode selection to multi-editor window --- .../src/editor/EditorDialog.java | 9 +++ EU3_Scenario_Editor/src/editor/EditorUI.java | 9 ++- .../src/editor/MultiFileEditorDialog.java | 76 +++++++++++++++++-- 3 files changed, 88 insertions(+), 6 deletions(-) diff --git a/EU3_Scenario_Editor/src/editor/EditorDialog.java b/EU3_Scenario_Editor/src/editor/EditorDialog.java index 7e5e119..c96d45d 100644 --- a/EU3_Scenario_Editor/src/editor/EditorDialog.java +++ b/EU3_Scenario_Editor/src/editor/EditorDialog.java @@ -506,6 +506,15 @@ private void setBackupFilesCheckBoxMenuItemActionPerformed(java.awt.event.Action }//GEN-LAST:event_setBackupFilesCheckBoxMenuItemActionPerformed + /** + * Sets a component to be above the text area and below the menu bar. + * Useful for the MultiFileEditorDialog to include settings. + * @param topBar + */ + protected void setTopBar(java.awt.Component topBar) { + this.add(topBar, BorderLayout.NORTH); + } + private void updateUndoStates() { undoMenuItem.setEnabled(undo.canUndo()); if (undo.canUndo()) diff --git a/EU3_Scenario_Editor/src/editor/EditorUI.java b/EU3_Scenario_Editor/src/editor/EditorUI.java index 984abf6..28ebe0a 100644 --- a/EU3_Scenario_Editor/src/editor/EditorUI.java +++ b/EU3_Scenario_Editor/src/editor/EditorUI.java @@ -19,6 +19,7 @@ import eug.specific.ck2.CK2SaveGame; import eug.specific.ck2.CK2Scenario; import eug.specific.clausewitz.ClausewitzHistory; +import eug.specific.clausewitz.ClausewitzHistoryMergeTool; import eug.specific.clausewitz.ClausewitzSaveGame; import eug.specific.clausewitz.ClausewitzScenario; import eug.specific.eu3.EU3SaveGame; @@ -71,6 +72,8 @@ public final class EditorUI extends javax.swing.JFrame { private Map map; private ProvinceData provinceData; + private ClausewitzHistoryMergeTool provinceHistoryMergeTool; // only initialized if the user wants to do a multi-file edit + /** * Creates new form EditorUI. */ @@ -718,8 +721,12 @@ public void handleSaveEvent(String tagOrTitle) { dlg.setVisible(true); } } else { + if (provinceHistoryMergeTool == null) { + provinceHistoryMergeTool = new ClausewitzHistoryMergeTool(); + provinceHistoryMergeTool.initAutoMergeList(mapPanel.getDataSource()); + } // multi file dialog is modal -- too confusing if we let there be more than one at a time - MultiFileEditorDialog dlg = new MultiFileEditorDialog(this, currentProvinces, resolver, provinceData); + MultiFileEditorDialog dlg = new MultiFileEditorDialog(this, currentProvinces, resolver, provinceData, provinceHistoryMergeTool); dlg.setModal(true); dlg.setVisible(true); mapPanel.getModel().clearHistoryCache(); diff --git a/EU3_Scenario_Editor/src/editor/MultiFileEditorDialog.java b/EU3_Scenario_Editor/src/editor/MultiFileEditorDialog.java index a305d86..f721989 100644 --- a/EU3_Scenario_Editor/src/editor/MultiFileEditorDialog.java +++ b/EU3_Scenario_Editor/src/editor/MultiFileEditorDialog.java @@ -7,10 +7,16 @@ import eug.specific.ck2.CK2DataSource; import eug.specific.clausewitz.ClausewitzDataSource; import eug.specific.clausewitz.ClausewitzHistory; +import eug.specific.clausewitz.ClausewitzHistoryMergeTool; +import java.awt.FlowLayout; import java.awt.Frame; import java.util.List; import java.util.stream.Collectors; +import javax.swing.ButtonGroup; +import javax.swing.JLabel; import javax.swing.JOptionPane; +import javax.swing.JPanel; +import javax.swing.JRadioButton; /** * Dialog to edit multiple provinces or countries at the same time. To allow @@ -91,29 +97,89 @@ public class MultiFileEditorDialog extends EditorDialog { private final List provinces; private final List countryTags; private final List countryNames; + + private final ClausewitzHistoryMergeTool mergeTool; + + JRadioButton addModeButton; + JRadioButton overwriteModeButton; + JRadioButton autoModeButton; + JRadioButton deleteModeButton; - public MultiFileEditorDialog(Frame parent, List provinces, FilenameResolver resolver, ProvinceData data) { + public MultiFileEditorDialog(Frame parent, List provinces, FilenameResolver resolver, ProvinceData data, ClausewitzHistoryMergeTool mergeTool) { super(parent, String.join(", ", provinces.stream().map(p -> p.getName()).collect(Collectors.toList())), resolver, data); + this.mergeTool = mergeTool; + // don't need to do much else here, since we don't open files till we finish this dialog this.provinces = provinces; this.countryTags = null; this.countryNames = null; setOriginalContents("# Anything typed here will be added to all selected province history files"); + + createTopBar(); } - public MultiFileEditorDialog(Frame parent, List countryTags, List countryNames, String contents, FilenameResolver resolver, ProvinceData data) { + // this constructor is private for now until better implemented and tested + private MultiFileEditorDialog(Frame parent, List countryTags, List countryNames, String contents, FilenameResolver resolver, ProvinceData data, ClausewitzHistoryMergeTool mergeTool) { super(parent, String.join(", ", countryTags), contents, resolver, data); + this.mergeTool = mergeTool; + // don't need to do much else here, since we don't open files till we finish this dialog this.provinces = null; this.countryTags = countryTags; this.countryNames = countryNames; setOriginalContents("# Anything typed here will be added to all selected country history files"); + + createTopBar(); } + private void createTopBar() { + JPanel settingsPanel = new JPanel(new FlowLayout()); + settingsPanel.add(new JLabel("Choose mode:")); + + ButtonGroup settingsGroup = new ButtonGroup(); + + addModeButton = new JRadioButton("Always add"); + overwriteModeButton = new JRadioButton("Overwrite if existing"); + autoModeButton = new JRadioButton("Automatically choose add/overwrite"); + deleteModeButton = new JRadioButton("

Delete if existing

"); + + addModeButton.setToolTipText("If selected, all text in the editor will be added to the selected files, even if there are already matching statements in the files (useful for add_core or discovered_by)"); + overwriteModeButton.setToolTipText("If selected, all text in the editor will be added to the selected files and overwrite any matching statements in the files (useful for owner or controller)"); + autoModeButton.setToolTipText("If selected, the editor will attempt to guess which parts of the text should be added and which should overwrite existing statements"); + deleteModeButton.setToolTipText("If selected, all text in the editor (excluding comments) will be DELETED from the selected files. Use with caution!"); + + settingsGroup.add(addModeButton); + settingsGroup.add(overwriteModeButton); + settingsGroup.add(autoModeButton); + settingsGroup.add(deleteModeButton); + + settingsPanel.add(addModeButton); + settingsPanel.add(overwriteModeButton); + settingsPanel.add(autoModeButton); + settingsPanel.add(deleteModeButton); + + addModeButton.setSelected(true); + + super.setTopBar(settingsPanel); + pack(); + } + + private ClausewitzHistoryMergeTool.MergeMode getMergeMode() { + if (addModeButton.isSelected()) + return ClausewitzHistoryMergeTool.MergeMode.ADD; + else if (overwriteModeButton.isSelected()) + return ClausewitzHistoryMergeTool.MergeMode.OVERWRITE; + else if (autoModeButton.isSelected()) + return ClausewitzHistoryMergeTool.MergeMode.AUTO; + else if (deleteModeButton.isSelected()) + return ClausewitzHistoryMergeTool.MergeMode.DELETE; + + return ClausewitzHistoryMergeTool.MergeMode.AUTO; + } @Override protected void close() { @@ -151,7 +217,7 @@ private void saveProvinces() { for (ProvinceData.Province p : provinces) { GenericObject oldProvince = dataSource.getProvince(p.getId()); - ClausewitzHistory.mergeHistObjects(oldProvince, created); + mergeTool.mergeHistObjects(oldProvince, created, getMergeMode()); dataSource.saveProvince(p.getId(), p.getName(), oldProvince.toString()); dataSource.reloadProvince(p.getId()); @@ -164,7 +230,7 @@ private void saveTitles() { for (String titleStr : countryTags) { GenericObject oldTitle = ((CK2DataSource)dataSource).getTitle(titleStr); - ClausewitzHistory.mergeHistObjects(oldTitle, created); + mergeTool.mergeHistObjects(oldTitle, created, getMergeMode()); ((CK2DataSource)dataSource).saveTitle(titleStr, oldTitle.toString()); ((CK2DataSource)dataSource).reloadTitle(titleStr); @@ -177,7 +243,7 @@ private void saveCountries() { for (String tagStr : countryTags) { GenericObject oldCountry = dataSource.getCountry(tagStr); - ClausewitzHistory.mergeHistObjects(oldCountry, created); + mergeTool.mergeHistObjects(oldCountry, created, getMergeMode()); dataSource.saveCountry(tagStr, tagStr, oldCountry.toString()); // TODO: need to get the country name dataSource.reloadCountry(tagStr);