/** * Returns empty string if verification was successful, otherwise it returns a message describing * what went wrong. */ private String verifyInputFields() { String result = ""; String inputFileName = ""; String outputFileName = ""; if (fileSelectionPanel != null) { inputFileName = fileSelectionPanel.getInputFileName(); outputFileName = fileSelectionPanel.getOutputFileName(); } File inF = new File(inputFileName); if (!inF.exists()) result += "\n *Specified input file does not exists"; else { int extIndex = inputFileName.lastIndexOf("."); boolean isTextFile = true; if (extIndex != -1) { String ext = inputFileName.substring(extIndex + 1, inputFileName.length()); System.out.println("ext = " + ext); if (!ext.equalsIgnoreCase("txt")) isTextFile = false; } else isTextFile = false; if (isTextFile == false) result += "\n *Specified input file must be a txt file"; } int extIndex = outputFileName.lastIndexOf("."); boolean isPDFFile = true; if (extIndex != -1) { String ext = outputFileName.substring(extIndex + 1, outputFileName.length()); System.out.println("ext = " + ext); if (!ext.equalsIgnoreCase("pdf")) isPDFFile = false; } else isPDFFile = false; if (isPDFFile == false) result += "\n *Specified output file must be a pdf file"; else { File outF = new File(outputFileName); if (outF.exists()) { if (!overwriteOutputFileChB.isSelected()) result += "\n *Specified output file exists"; } else // by creation of a file we are checking if the output file name is proper try { outF.createNewFile(); } catch (IOException ex) { Logger.getLogger(CommonPanel.class.getName()).log(Level.SEVERE, null, ex); result += "\n *Specified output file name has not permissible characters"; } } if (fontSizeSpecRB.isSelected()) try { Float.parseFloat(fontSizeTF.getText()); } catch (NumberFormatException e) { result += "\n *Specified font size is not a float number, e.g. 10.5, 12, 22.1"; } return result; }
public void actionPerformed(ActionEvent e) { String verifyResult = verifyInputFields(); if (verifyResult.equals("")) { // do conversion of txt file to pdf String inputFileName = fileSelectionPanel.getInputFileName(); String outputFileName = fileSelectionPanel.getOutputFileName(); String encoding = (String) encodingCB.getSelectedItem(); String outputFontName = (String) fontCB.getSelectedItem(); boolean isLandscapeMode = landscapeRB.isSelected(); float fontSize = -1.0f; if (fitTextToWidthRB.isSelected()) try { Rectangle pageSize = PageSize.A4; if (isLandscapeMode) pageSize = PageSize.A4.rotate(); Document document = new Document(pageSize); String fileContent = ReadWriteTextFileWithEncoding.read(inputFileName, encoding); fontSize = calculateFontSizeFittingAllStringIntoPageWidth( document, fileContent, outputFontName, encoding); } catch (IOException ex) { Logger.getLogger(CommonPanel.class.getName()).log(Level.SEVERE, null, ex); } catch (DocumentException ex) { Logger.getLogger(CommonPanel.class.getName()).log(Level.SEVERE, null, ex); } else fontSize = Float.parseFloat(fontSizeTF.getText()); try { convertTextToPDFFile( inputFileName, encoding, outputFontName, fontSize, isLandscapeMode, outputFileName); } catch (DocumentException ex) { Logger.getLogger(CommonPanel.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(CommonPanel.class.getName()).log(Level.SEVERE, null, ex); } } else JOptionPane.showMessageDialog( ownerFrame, verifyResult, "Cannot convert, becauses:", JOptionPane.INFORMATION_MESSAGE); }