Udated with Work changes: Default value handling, adding new column

This commit is contained in:
2026-03-08 19:47:25 +01:00
parent d905d4195c
commit e35d5ff013
6 changed files with 72 additions and 348 deletions

View File

@@ -1,340 +0,0 @@
package application;
import java.awt.BorderLayout;
import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.List;
import javax.naming.ConfigurationException;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.filechooser.FileNameExtensionFilter;
import de.lcag.common.ExcelWorkbook;
import de.lcag.common.LcagProperties;
import de.lcag.common.MappingTable;
import de.lcag.common.WorkbookComparisonResult;
import routines.LcagFileTools;
import routines.LcagStringTools;
public class FileActionsV3 extends JFrame {
private static final int FRAME_HEIGHT = 150;
private static String MASTER_FILE_LABEL = "Main Excel File";
private static String CONFIG_FILE_LABEL = "Configuration File";
private JLabel masterFileLabel, clientFileLabel, configFileLabel;
private JButton selectMasterFile, selectClientFile, selectConfigFile;
private Path masterFile, clientFile, configFile;
private JMenuBar menuBar;
private JMenu actionsMenu;
private JMenuItem mergeAction;
private JPopupMenu contextMenuMasterFile, contextMenuClientFile;
private File configFileDirectoryPath = null;
private LcagProperties config = null;
private File masterDirecory = null;
public FileActionsV3() {
setTitle("Compare 2 Excel Files");
setSize(800, FRAME_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel mainPanel = new JPanel(new BorderLayout());
JPanel leftButtonPanel = new JPanel(new GridLayout(3, 1, 5, 5));
JPanel rightTextPanel = new JPanel(new GridLayout(3, 1, 5, 5));
// setLayout(new GridLayout(4, 2));
leftButtonPanel.setPreferredSize(new Dimension(200, FRAME_HEIGHT)); // Fixed width
// Add an empty border to create a gap on the right side of leftPanel
leftButtonPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 10)); // right gap: 10px
rightTextPanel.setPreferredSize(new Dimension(500, FRAME_HEIGHT));
mainPanel.add(leftButtonPanel, BorderLayout.WEST);
mainPanel.add(rightTextPanel, BorderLayout.EAST);
configFileLabel = new JLabel(String.format(" %s: Not selected", CONFIG_FILE_LABEL));
selectConfigFile = new JButton("Select " + CONFIG_FILE_LABEL);
leftButtonPanel.add(selectConfigFile);
rightTextPanel.add(configFileLabel);
// File Chooser for config file
selectConfigFile.setEnabled(true);
selectConfigFile.addActionListener(e -> {
if (!configFileDirectoryPath.isDirectory()) {
System.err.println(String.format("ERROR: Config directory %s does not exist",
configFileDirectoryPath.getAbsolutePath()));
System.exit(1);
}
JFileChooser fileChooser = new JFileChooser(configFileDirectoryPath);
FileNameExtensionFilter filter = new FileNameExtensionFilter("Config Files", "properties");
fileChooser.setFileFilter(filter);
if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
String clientDirString = null;
configFile = selectFile(fileChooser);
config = new LcagProperties(configFile.toString());
configFileLabel.setText(String.format(" %s: %s", CONFIG_FILE_LABEL, configFile));
masterFile = config.getOptionalFilePathProperty("main_file");
clientFile = config.getOptionalFilePathProperty("compare_file");
if (clientFile.isAbsolute()) {
clientDirString = clientFile.getParent().toString();
} else {
// client file is relative expect it is same directory as master
Path dirPath = masterFile.getParent();
clientDirString = "same directory as 1st file";
clientFile = dirPath.resolve(clientFile);
}
// After user selected a config file enable buttons to set files to compare (can
// overwrite files listed in config file):
selectMasterFile.setEnabled(true);
if (clientFile != null) {
String fileName = clientFile.getFileName().toString();
clientFileLabel
.setText(String.format("<html>Selected: %s<br>in: %s</html>", fileName, clientDirString));
selectClientFile.setEnabled(true);
}
if (masterFile != null) {
String fileName = masterFile.getFileName().toString();
masterDirecory = new File(masterFile.getParent().toString());
masterFileLabel.setText(String.format("<html>Selected: %s<br>in: %s</html>", fileName,
masterFile.getParent().toString()));
selectClientFile.setEnabled(true);
}
}
});
// Kontextmen<65>s erstellen
contextMenuMasterFile = new JPopupMenu();
contextMenuClientFile = new JPopupMenu();
JMenuItem renameFile1 = new JMenuItem("Rename File");
JMenuItem renameFile2 = new JMenuItem("Rename File");
contextMenuMasterFile.add(renameFile1);
contextMenuClientFile.add(renameFile2);
masterFileLabel = new JLabel(String.format(" %s: %s", MASTER_FILE_LABEL,
masterFile == null ? "Not selected" : masterFile.getFileName()));
selectMasterFile = new JButton("Select " + MASTER_FILE_LABEL);
leftButtonPanel.add(selectMasterFile);
rightTextPanel.add(masterFileLabel);
clientFileLabel = new JLabel(String.format(" %s: %s", "Excel to compare",
clientFile == null ? "Not selected" : clientFile.getFileName()));
selectClientFile = new JButton("Select Excel to Compare");
// MouseListener f<>r Kontextmen<65> auf den Labels
masterFileLabel.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (SwingUtilities.isRightMouseButton(e) && masterFile != null) {
contextMenuMasterFile.show(masterFileLabel, e.getX(), e.getY());
}
}
});
clientFileLabel.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (SwingUtilities.isRightMouseButton(e) && clientFile != null) {
contextMenuClientFile.show(clientFileLabel, e.getX(), e.getY());
}
}
});
leftButtonPanel.add(selectClientFile);
rightTextPanel.add(clientFileLabel);
renameFile1.addActionListener(e -> renameFile(masterFile, masterFileLabel, "Datei 1"));
renameFile2.addActionListener(e -> renameFile(clientFile, clientFileLabel, "Datei 2"));
// File Chooser f<>r Datei 1 (nur Excel-Dateien)
selectMasterFile.setEnabled(false);
selectMasterFile.addActionListener(e -> {
JFileChooser fileChooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("Excel-Files", "xls", "xlsx");
fileChooser.setFileFilter(filter);
if (masterFile != null) {
masterDirecory = new File(masterFile.getParent().toString());
fileChooser.setCurrentDirectory(masterDirecory);
}
if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
masterFile = selectFile(fileChooser);
masterDirecory = new File(masterFile.getParent().toString());
masterFileLabel.setText(String.format(" %s: %s", MASTER_FILE_LABEL, masterFile.getFileName()));
selectClientFile.setEnabled(true);
}
});
// File Chooser f<>r Datei 2 (im selben Verzeichnis wie Datei 1, nur
// Excel-Dateien)
selectClientFile.setEnabled(false);
selectClientFile.addActionListener(e -> {
if (masterDirecory != null) {
JFileChooser fileChooser = new JFileChooser(masterDirecory);
FileNameExtensionFilter filter = new FileNameExtensionFilter("Excel-Dateien", "xls", "xlsx");
fileChooser.setFileFilter(filter);
if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
clientFile = selectFile(fileChooser);
clientFileLabel.setText("Datei 2: " + clientFile.getFileName());
}
}
});
// Men<65> erstellen
menuBar = new JMenuBar();
actionsMenu = new JMenu("Actions");
mergeAction = new JMenuItem("Merge 2nd into Main file");
mergeAction.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_M, ActionEvent.CTRL_MASK));
mergeAction.addActionListener(e -> {
System.out.println("Ctrl-C pressed on menu");
});
actionsMenu.add(mergeAction);
menuBar.add(actionsMenu);
setJMenuBar(menuBar);
// Aktion: Erste Datei in Excel <20>ffnen
mergeAction.addActionListener(e -> {
mergeFiles(config, masterFile, clientFile);
});
add(mainPanel);
pack();
setVisible(true);
}
private Path selectFile(JFileChooser fileChooser) {
File result = fileChooser.getSelectedFile();
return Path.of(result.getAbsolutePath());
}
private void mergeFiles(LcagProperties pConfig, Path pMasterFilePath, Path pClientFilePath) {
if (pConfig != null) {
String keyFieldString = pConfig.getProperty("list_of_keys");
List<String> keyFields = LcagStringTools.asList(keyFieldString, ",\\s*");
if (pMasterFilePath != null && pClientFilePath != null)
try {
String masterFileName = pMasterFilePath.getFileName().toString();
Path resultFilePath = LcagFileTools.appendStringToFilePath(pMasterFilePath, "-merged");
String fieldMapping = pConfig.getProperty("column.mapping.main2compare");
Files.copy(pMasterFilePath, resultFilePath, StandardCopyOption.REPLACE_EXISTING);
ExcelWorkbook excel1 = new ExcelWorkbook(resultFilePath.toString());
ExcelWorkbook excel2 = new ExcelWorkbook(pClientFilePath.toString());
excel1.setKeyColumns(keyFields);
excel1.read();
excel2.setKeyColumns(keyFields);
excel2.read();
excel1.setOption(keyFieldString, FRAME_HEIGHT);
if (fieldMapping != null) {
MappingTable mappingTable = new MappingTable(fieldMapping, ";", "->");
excel2.setOption(mappingTable);
}
WorkbookComparisonResult compareResult = excel1.compareTo(excel2);
ExcelWorkbook integratedTable = (ExcelWorkbook) compareResult.integrateDifferencesIntoMaster();
integratedTable.save();
Desktop.getDesktop().open(resultFilePath.toFile());
} catch (IOException ex) {
JOptionPane.showMessageDialog(this, "Cannot open comparison result file.", "Error",
JOptionPane.ERROR_MESSAGE);
} catch (ConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
JOptionPane.showMessageDialog(this, "Pls select at least a config file!", "Error",
JOptionPane.ERROR_MESSAGE);
}
}
private Path getPath(LcagProperties config, String pProperty, File pMasterFile) {
Path result = null;
String filePathString = (pMasterFile == null) ? config.getProperty(pProperty) : pMasterFile.getAbsolutePath();
if (!LcagStringTools.isEmpty(filePathString)) {
result = Path.of(filePathString);
}
return result;
}
private Path renameFile(Path pFile, JLabel label, String prefix) {
Path newPath = null;
if (pFile == null)
return newPath;
File file = new File(pFile.toString());
String newName = JOptionPane.showInputDialog(this, "Enter new name:", file.getName());
if (!LcagStringTools.isEmpty(newName)) {
newPath = pFile.getParent().resolve(newName);
File newFile = new File(newPath.toString());
if (file.renameTo(newFile)) {
file = newFile;
label.setText(prefix + ": " + newFile.getName());
if (prefix.equals("Datei 1"))
this.masterFile = newPath;
else
this.clientFile = newPath;
} else {
JOptionPane.showMessageDialog(this, "Umbenennung fehlgeschlagen!", "Fehler", JOptionPane.ERROR_MESSAGE);
}
}
return newPath;
}
public static void main(String[] args) {
String cfgFileDirectory = args.length < 1 ? System.getenv("_MY_DATA") + "/Tools" : args[1];
SwingUtilities.invokeLater(() -> {
FileActionsV3 gui = new FileActionsV3();
gui.configFileDirectoryPath = new File(cfgFileDirectory);
gui.setVisible(true);
});
}
}

View File

@@ -50,6 +50,7 @@ public abstract class AbstractTable implements Table {
private PositionOrderedMap<String, TableColumn> rowMap = new PositionOrderedMap<String, TableColumn>();
private Set<String> keyColumns = new ListOrderedSet<>();
private Set<String> nonKeyColumnNames = null;
private List<String> wantedColumns = new ArrayList<>();
private List<String> excludedColumns = new ArrayList<>();
protected Map<String, Object> options = new HashMap<>();
@@ -61,7 +62,7 @@ public abstract class AbstractTable implements Table {
public class ListColumn implements TableColumn {
List<Object> data = new ArrayList<>();
private Object defaultValue = null;
private static Object defaultValue = null;
private String style = "";
public ListColumn() {
@@ -80,6 +81,11 @@ public abstract class AbstractTable implements Table {
data.add(v);
}
@Override
public Object getDefaultValue() {
return defaultValue;
}
public Object getValue(int i) {
Object result = null;
int n = data.size();
@@ -551,7 +557,7 @@ public abstract class AbstractTable implements Table {
}
if (!colNames.containsKey(colUniqueName)) {
int noItems = pColumn.length();
int noNewColumnRecords = pColumn.length();
if (col.length > 1) {
pColumn.setStyle(col[1]);
@@ -571,9 +577,18 @@ public abstract class AbstractTable implements Table {
rowMap.toString();
}
if (this.noRows < noItems) {
this.noRows = noItems;
//FIXME: Next block may need corrections: Not all cells become initialized.
if (this.noRows < noNewColumnRecords) {
// New column has more entries than the table has rows => increase number of rows in table rows
this.noRows = noNewColumnRecords;
} else {
// Table has likely more rows then the new column (e.g. when adding an empty column) => init the new column with default value so it has the same number of value
Object v = pColumn.getDefaultValue();
for (int i = noNewColumnRecords; i < this.noRows; i++) {
pColumn.addValue(v);
}
}
}
return colPosResult;
@@ -918,6 +933,7 @@ public abstract class AbstractTable implements Table {
protected void setKeyColumns(List<String> pKeyColumns) {
this.keyColumn = new KeyColumn();
this.nonKeyColumnNames = null;
for (String s : pKeyColumns) {
this.keyColumns.add(s);
@@ -994,6 +1010,19 @@ public abstract class AbstractTable implements Table {
}
private Set<String> getNonKeyColumnNames() {
if (this.nonKeyColumnNames == null) {
nonKeyColumnNames = new ListOrderedSet<String>();
nonKeyColumnNames.addAll(this.getColumnNames());
nonKeyColumnNames.removeAll(this.keyColumns);
nonKeyColumnNames.removeAll(this.keyColumns);
}
return this.nonKeyColumnNames;
}
@Override
public String setUniqueKeyValue(int pRow) {
String resultKeyValue = calculateKeyFromColumns(pRow, this.keyColumns);
@@ -1008,7 +1037,9 @@ public abstract class AbstractTable implements Table {
Integer existingKeyValue = this.getRowByKey(resultKeyValue);
if (existingKeyValue != null) {
resultKeyValue = calculateKeyFromColumns(pRow, this.getColumnNames());
Set<String> nonKeyColumnNames = this.getNonKeyColumnNames();
resultKeyValue = resultKeyValue + "_" + calculateKeyFromColumns(pRow, nonKeyColumnNames);
}
this.keyColumn.setValue(pRow, resultKeyValue);

View File

@@ -134,6 +134,11 @@ public class ExcelWorkbook implements Table, Storage {
return ExcelTable.this.shiftColumns(columnPosition);
}
@Override
public Object getDefaultValue() {
return null;
}
@Override
public Object getValue(int pRowNumber) {
Object result = null;

View File

@@ -29,6 +29,8 @@ public interface Table {
public <V> V getValue(int i);
public <V> V getDefaultValue();
public void setValue(int rowNo, Object v);
public int length();

View File

@@ -9,6 +9,8 @@ import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import de.lcag.common.Table.TableColumn;
class CsvTableTest {
@BeforeAll
@@ -51,4 +53,28 @@ class CsvTableTest {
assertEquals(37780, n);
}
/**
* Important Testcase: Function used in EBX
*
* @throws IOException
*/
@Test
void testAddColumn() throws IOException {
CsvTable csv = null;
int n;
csv = new CsvTable("CsvTable/City_change_history_ok.csv");
csv.read();
TableColumn newCol = csv.addColumn("myNewColumn");
n = csv.lenght();
assertEquals(37780, n);
newCol.setValue(n - 1, 28);
Object val = csv.getCellValue(n - 1, "myNewColumn");
assertEquals(28, val);
}
}

View File

@@ -20,7 +20,7 @@ class DbTableTest {
void testAppendString() {
DbTable table = new DbTable(dbProp);
table.append("select * from vlcag_sys_util_stationefreightcapabilities");
table.append("select * from util_stationefreightcapabilities");
String csv = table.asCsvData();