Added checks and dialogs
This commit is contained in:
parent
7a1d3e4586
commit
c1f5347567
2 changed files with 80 additions and 38 deletions
|
@ -1,6 +1,7 @@
|
|||
package pro.litvinovg.w2phtml.gui;
|
||||
|
||||
import java.awt.Component;
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
|
@ -8,21 +9,27 @@ import java.util.List;
|
|||
import java.util.Set;
|
||||
|
||||
import javax.swing.JCheckBox;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JOptionPane;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JTextField;
|
||||
|
||||
import com.sun.star.uno.XComponentContext;
|
||||
|
||||
import pro.litvinovg.xml.Debug;
|
||||
|
||||
public class ConfigurationReader {
|
||||
|
||||
HashMap<String,String> options = new HashMap<String,String>();
|
||||
private XComponentContext context;
|
||||
private String format = null;
|
||||
private String inputFilePath = null;
|
||||
private String outputFilePath = null;
|
||||
private String metadataFilePath = null;
|
||||
private JFrame frame;
|
||||
|
||||
public ConfigurationReader(HashMap<String, Component> configuration, XComponentContext context) {
|
||||
public ConfigurationReader(HashMap<String, Component> configuration, XComponentContext context, JFrame frame) {
|
||||
this.context = context;
|
||||
this.frame = frame;
|
||||
readOptions(configuration);
|
||||
}
|
||||
|
||||
|
@ -39,35 +46,19 @@ public class ConfigurationReader {
|
|||
optionValue = ((JLabel) component).getText();
|
||||
}
|
||||
options.put(optionName, optionValue);
|
||||
//Debug.printLog(optionName + " : " + optionValue, context);
|
||||
|
||||
}
|
||||
|
||||
|
||||
format = options.get("targetFormat");
|
||||
options.remove("targetFormat");
|
||||
inputFilePath = options.get("inputFile");
|
||||
options.remove("inputFile");
|
||||
outputFilePath = options.get("outputFile");
|
||||
options.remove("outputFile");
|
||||
metadataFilePath = options.get("csv_metadata");
|
||||
}
|
||||
|
||||
public String[] getCommandLine() {
|
||||
List<String> args = new ArrayList<String>();
|
||||
String format = options.get("targetFormat");
|
||||
if (format == null) {
|
||||
Debug.printLog("target format is not set", context);
|
||||
return null;
|
||||
} else {
|
||||
options.remove("targetFormat");
|
||||
}
|
||||
String inputFile = options.get("inputFile");
|
||||
if (inputFile == null) {
|
||||
Debug.printLog("no input file", context);
|
||||
return null;
|
||||
} else {
|
||||
options.remove("inputFile");
|
||||
}
|
||||
String outputFile = options.get("outputFile");
|
||||
if (outputFile == null) {
|
||||
//Debug.printLog("no output file set", context);
|
||||
} else {
|
||||
options.remove("outputFile");
|
||||
}
|
||||
args.add("-"+format);
|
||||
for (String key: options.keySet()) {
|
||||
String value = options.get(key);
|
||||
|
@ -76,17 +67,66 @@ public class ConfigurationReader {
|
|||
args.add(value);
|
||||
}
|
||||
}
|
||||
args.add(inputFile);
|
||||
if (outputFile != null) {
|
||||
args.add(outputFile);
|
||||
args.add(inputFilePath);
|
||||
if (outputFilePath != null) {
|
||||
args.add(outputFilePath);
|
||||
}
|
||||
String sout = "";
|
||||
for (String val: args) {
|
||||
sout += val+" ";;
|
||||
}
|
||||
Debug.printLog(sout, context);
|
||||
//Debug.printLog(sout, context);
|
||||
String[] array = args.toArray(new String[0]);
|
||||
return array;
|
||||
}
|
||||
|
||||
public boolean isValid() {
|
||||
if (format == null) {
|
||||
return false;
|
||||
}
|
||||
if (inputFilePath == null) {
|
||||
JOptionPane.showMessageDialog(frame, "Input file path is null");
|
||||
return false;
|
||||
}
|
||||
File inputFile = new File(inputFilePath);
|
||||
if (!inputFile.exists()) {
|
||||
JOptionPane.showMessageDialog(frame, "Input file not exists");
|
||||
return false;
|
||||
}
|
||||
if (!inputFile.canRead()){
|
||||
JOptionPane.showMessageDialog(frame, "Input file can not be read");
|
||||
return false;
|
||||
}
|
||||
if (metadataFilePath != null) {
|
||||
File metadataFile = new File(metadataFilePath);
|
||||
if (!metadataFile.exists()) {
|
||||
JOptionPane.showMessageDialog(frame, "Metadata file not exists");
|
||||
return false;
|
||||
}
|
||||
if (!metadataFile.canRead()) {
|
||||
JOptionPane.showMessageDialog(frame, "Metadata file can not be read");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (outputFilePath == null) {
|
||||
File inputDir = inputFile.getParentFile();
|
||||
if (!inputDir.canWrite()) {
|
||||
JOptionPane.showMessageDialog(frame, "Output directory is not writable");
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
File outputFile = new File(outputFilePath);
|
||||
if (!outputFile.canWrite()) {
|
||||
if (outputFile.isDirectory()) {
|
||||
JOptionPane.showMessageDialog(frame, "Output directory is not writable");
|
||||
} else {
|
||||
JOptionPane.showMessageDialog(frame, "Output file is not writable");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -603,7 +603,7 @@ public class ConfigurationWindow extends JFrame {
|
|||
return;
|
||||
}
|
||||
if (!parent.canWrite()){
|
||||
//TODO: Warning message
|
||||
JOptionPane.showMessageDialog(singleFrame, "Attension. Output file can not be written.");
|
||||
}
|
||||
if (newFilePath.length() < 3) {
|
||||
return;
|
||||
|
@ -891,13 +891,15 @@ public class ConfigurationWindow extends JFrame {
|
|||
JOptionPane.showMessageDialog(singleFrame, "Not implemented, yet.");
|
||||
}
|
||||
});
|
||||
JButton btn_Convert = new JButton("Convert");
|
||||
btn_Convert.addActionListener(new ActionListener() {
|
||||
JButton btn_startConversion = new JButton("Convert");
|
||||
btn_startConversion.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent actionEvent) {
|
||||
try {
|
||||
ConfigurationReader reader = new ConfigurationReader(configuration, context);
|
||||
Application.main(reader.getCommandLine());
|
||||
JOptionPane.showMessageDialog(singleFrame, "Conversion completed.");
|
||||
ConfigurationReader reader = new ConfigurationReader(configuration, context, singleFrame);
|
||||
if (reader.isValid()) {
|
||||
Application.main(reader.getCommandLine());
|
||||
JOptionPane.showMessageDialog(singleFrame, "Conversion completed.");
|
||||
}
|
||||
} catch(Throwable e) {
|
||||
StringWriter errors = new StringWriter();
|
||||
e.printStackTrace(new PrintWriter(errors));
|
||||
|
@ -918,7 +920,7 @@ public class ConfigurationWindow extends JFrame {
|
|||
.addGap(18)
|
||||
.addComponent(btn_Save, GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE)
|
||||
.addGap(18)
|
||||
.addComponent(btn_Convert, GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE)
|
||||
.addComponent(btn_startConversion, GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE)
|
||||
.addContainerGap(427, Short.MAX_VALUE))
|
||||
);
|
||||
gl_panel_control.setVerticalGroup(
|
||||
|
@ -929,7 +931,7 @@ public class ConfigurationWindow extends JFrame {
|
|||
.addComponent(btn_SaveAs)
|
||||
.addComponent(btn_Save)
|
||||
.addComponent(btn_Exit)
|
||||
.addComponent(btn_Convert))
|
||||
.addComponent(btn_startConversion))
|
||||
.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
|
||||
);
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue