From 0b9ea16c8560fea500173e702df9f2c82f0538b0 Mon Sep 17 00:00:00 2001 From: Georgy Litvinov Date: Tue, 11 Aug 2020 18:13:18 +0200 Subject: [PATCH] Added file path helper --- .../w2phtml/gui/ConfigurationWindow.java | 99 ++++++++++++++----- 1 file changed, 75 insertions(+), 24 deletions(-) diff --git a/src/main/java/pro/litvinovg/w2phtml/gui/ConfigurationWindow.java b/src/main/java/pro/litvinovg/w2phtml/gui/ConfigurationWindow.java index 1c17f12..8d0ff9b 100644 --- a/src/main/java/pro/litvinovg/w2phtml/gui/ConfigurationWindow.java +++ b/src/main/java/pro/litvinovg/w2phtml/gui/ConfigurationWindow.java @@ -236,6 +236,9 @@ public class ConfigurationWindow extends JFrame { JLabel lb_TargetFormat_description = new JLabel("Target format"); + JTextField tf_OutputFile = new JTextField(""); + configuration.put("outputFile", tf_OutputFile); + tf_OutputFile.setColumns(10); JButton btnChooseFile = new JButton("Choose"); btnChooseFile.addActionListener(new ActionListener() { @@ -244,12 +247,12 @@ public class ConfigurationWindow extends JFrame { String newFilePath = fileDialog.chooseFile(tf_inputFile.getText(),"odt"); if (newFilePath != null && !newFilePath.isEmpty()) { tf_inputFile.setText(newFilePath); + setOutputFilePath(newFilePath,tf_OutputFile,"epub"); + } } }); - JTextField tf_OutputFile = new JTextField(""); - configuration.put("outputFile", tf_OutputFile); - tf_OutputFile.setColumns(10); + JButton btn_ChooseOutputFile = new JButton("Choose"); btn_ChooseOutputFile.addActionListener(new ActionListener() { @@ -439,6 +442,11 @@ public class ConfigurationWindow extends JFrame { configuration.put("targetFormat", lb_TargetFormat); JLabel lb_TargetFormat_description = new JLabel("Target format"); + JLabel lb_OutFilePath = new JLabel("Output file path"); + JTextField tf_OutputFile = new JTextField(""); + tf_OutputFile.setColumns(10); + configuration.put("outputFile", tf_OutputFile); + JButton btn_ChooseInputFile = new JButton("Choose"); btn_ChooseInputFile.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { @@ -446,16 +454,12 @@ public class ConfigurationWindow extends JFrame { String newFilePath = fileDialog.chooseFile(tf_inputFile.getText(),"odt"); if (newFilePath != null && !newFilePath.isEmpty()) { tf_inputFile.setText(newFilePath); + setOutputFilePath(newFilePath,tf_OutputFile,"html"); } } - }); - - JLabel lb_OutFilePath = new JLabel("Output file path"); - - JTextField tf_OutputFile = new JTextField(""); - configuration.put("outputFile", tf_OutputFile); - tf_OutputFile.setColumns(10); + + }); JButton btn_ChooseOutputFile = new JButton("Choose"); btn_ChooseOutputFile.addActionListener(new ActionListener() { @@ -588,6 +592,47 @@ public class ConfigurationWindow extends JFrame { return panel_configHTML; } + protected void setOutputFilePath(String newFilePath, JTextField tf_OutputFile, String extension) { + + File inputFile = new File(newFilePath); + if (!inputFile.exists()) { + return; + } + File parent = inputFile.getParentFile(); + if (parent == null) { + return; + } + if (!parent.canWrite()){ + //TODO: Warning message + } + if (newFilePath.length() < 3) { + return; + } + String outputPath = newFilePath.substring(0, newFilePath.length()-3) + extension; + tf_OutputFile.setText(outputPath); + } + + protected void setMetadataFilePath(String newFilePath, JTextField tf_MetadataFile, String extension) { + + File inputFile = new File(newFilePath); + if (!inputFile.exists()) { + return; + } + File parent = inputFile.getParentFile(); + if (parent == null) { + return; + } + if (newFilePath.length() < 3) { + return; + } + String metadataPath = newFilePath.substring(0, newFilePath.length()-3) + extension; + File metaFile = new File(metadataPath); + if (metaFile.exists() && metaFile.canRead()){ + tf_MetadataFile.setText(metadataPath); + } + + } + private JPanel createConfigRDFPanel(HashMap configuration) { JPanel panel_configHTML = new JPanel(); @@ -648,6 +693,22 @@ public class ConfigurationWindow extends JFrame { JLabel lb_TargetFormat_description = new JLabel("Target format"); + + JLabel lb_OutFilePath = new JLabel("Output file path"); + + JTextField tf_OutputFile = new JTextField(""); + configuration.put("outputFile", tf_OutputFile); + tf_OutputFile.setColumns(10); + + String[] types = {"статья ЭФЭ","статья энциклопедии", "книга", "журнал"}; + JComboBox cbox_type = new JComboBox(types); + + JLabel lb_type = new JLabel("Type"); + + JLabel lb_metadataFilePath = new JLabel("Metadata path"); + + JTextField tf_metadataPath = new JTextField(""); + configuration.put("csv_metadata", tf_metadataPath); JButton btnChooseFile = new JButton("Choose"); btnChooseFile.addActionListener(new ActionListener() { @@ -656,15 +717,13 @@ public class ConfigurationWindow extends JFrame { String newFilePath = fileDialog.chooseFile(tf_inputFile.getText(),"odt"); if (newFilePath != null && !newFilePath.isEmpty()) { tf_inputFile.setText(newFilePath); + setOutputFilePath(newFilePath,tf_OutputFile,"rdf"); + setMetadataFilePath(newFilePath,tf_metadataPath,"csv"); } } }); - JLabel lb_OutFilePath = new JLabel("Output file path"); - - JTextField tf_OutputFile = new JTextField(""); - configuration.put("outputFile", tf_OutputFile); - tf_OutputFile.setColumns(10); + JButton btn_ChooseOutputFile = new JButton("Choose"); btn_ChooseOutputFile.addActionListener(new ActionListener() { @@ -676,15 +735,7 @@ public class ConfigurationWindow extends JFrame { } } }); - String[] types = {"статья ЭФЭ","статья энциклопедии", "книга", "журнал"}; - JComboBox cbox_type = new JComboBox(types); - - JLabel lb_type = new JLabel("Type"); - - JLabel lb_metadataFilePath = new JLabel("Metadata path"); - - JTextField tf_metadataPath = new JTextField(""); - configuration.put("csv_metadata", tf_metadataPath); + tf_metadataPath.setColumns(10);