/** Show the save dialog to save the network file. */ private void saveEvent() { JFileChooser fileSaver = new JFileChooser(inpFile.getParent()); fileSaver.setFileSelectionMode(JFileChooser.FILES_ONLY); fileSaver.setAcceptAllFileFilterUsed(false); fileSaver.addChoosableFileFilter(new XLSXFilter()); fileSaver.addChoosableFileFilter(new XMLFilter()); fileSaver.addChoosableFileFilter(new INPFilter()); fileSaver.showSaveDialog(frame); if (fileSaver.getSelectedFile() == null) return; Network.FileType netType = Network.FileType.INP_FILE; if (fileSaver.getFileFilter() instanceof XLSXFilter) { netType = Network.FileType.EXCEL_FILE; } else if (fileSaver.getFileFilter() instanceof XMLFilter) { netType = Network.FileType.XML_FILE; JOptionPane.showMessageDialog(frame, "Not supported yet !", "Error", JOptionPane.OK_OPTION); return; } OutputComposer compose = OutputComposer.create(netType); String extension = ""; if (Utilities.getFileExtension(fileSaver.getSelectedFile().getName()).equals("")) switch (netType) { case INP_FILE: extension = ".inp"; break; case EXCEL_FILE: extension = ".xlsx"; break; case XML_FILE: extension = ".xml"; break; } try { compose.composer( epanetNetwork, new File(fileSaver.getSelectedFile().getAbsolutePath() + extension)); } catch (ENException e1) { e1.printStackTrace(); } }
/** * Contructor from other exception and multiple arguments. * * @param e * @param arg */ public ENException(ENException e, Object... arg) { arguments = arg; codeID = e.getCodeID(); }
/** Show the open dialog and open the INP/XLSX and XML files. */ private void openEvent() { if (fileChooser == null) { // fileChooser = new FileDialog(frame); fileChooser = new JFileChooser(System.getProperty("user.dir")); fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); fileChooser.setAcceptAllFileFilterUsed(false); fileChooser.addChoosableFileFilter(new XLSXFilter()); fileChooser.addChoosableFileFilter(new XMLFilter()); fileChooser.addChoosableFileFilter(new MSXFilter()); fileChooser.addChoosableFileFilter(new INPFilter()); fileChooser.addChoosableFileFilter(new AllSuportedFilesFilter()); } if (fileChooser.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION) { File netFile = fileChooser.getSelectedFile(); String fileExtension = Utilities.getFileExtension(netFile.getName()); if (fileExtension.equals("xlsx") || fileExtension.equals("inp") || fileExtension.equals("xml")) { inpFile = netFile; msxFile = null; msxName.setText(""); Network.FileType netType = Network.FileType.INP_FILE; if (fileExtension.equals("xlsx")) netType = Network.FileType.EXCEL_FILE; else if (fileExtension.equals("xml")) { netType = Network.FileType.XML_FILE; JOptionPane.showMessageDialog( frame, "Not supported yet !", "Error", JOptionPane.OK_OPTION); return; } epanetNetwork = new Network(); InputParser inpParser = InputParser.create(netType, log); try { inpParser.parse(epanetNetwork, inpFile); } catch (ENException en_ex) { JOptionPane.showMessageDialog( frame, en_ex.toString() + "\nCheck epanet.log for detailed error description", "Error", JOptionPane.OK_OPTION); clearInterface(); inpFile = null; return; } catch (Exception egen) { JOptionPane.showMessageDialog( frame, "Unable to parse network configuration file", "Error", JOptionPane.OK_OPTION); log.log(ENLevels.ERROR, "openEvent", egen); clearInterface(); inpFile = null; return; } int resrvCount = 0; int tanksCount = 0; for (Tank tank : epanetNetwork.getTanks()) if (tank.getArea() == 0.0) resrvCount++; else tanksCount++; textReservoirs.setText(Integer.toString(resrvCount)); textTanks.setText(Integer.toString(tanksCount)); textPipes.setText(Integer.toString(epanetNetwork.getLinks().size())); textNodes.setText(Integer.toString(epanetNetwork.getNodes().size())); try { textDuration.setText( Utilities.getClockTime(epanetNetwork.getPropertiesMap().getDuration())); textUnits.setText(epanetNetwork.getPropertiesMap().getUnitsflag().name()); textHeadloss.setText(epanetNetwork.getPropertiesMap().getFormflag().name()); textQuality.setText(epanetNetwork.getPropertiesMap().getQualflag().name()); textDemand.setText(epanetNetwork.getPropertiesMap().getDmult().toString()); textHydraulic.setText( Utilities.getClockTime(epanetNetwork.getPropertiesMap().getHstep())); textPattern.setText(Utilities.getClockTime(epanetNetwork.getPropertiesMap().getPstep())); } catch (ENException ex) { } frame.setTitle(APP_TITTLE + inpFile.getName()); inpName.setText(inpFile.getName()); runSimulationButton.setEnabled(true); saveButton.setEnabled(true); reportOptions = null; } else if (fileExtension.equals("msx")) { if (inpFile == null) { JOptionPane.showMessageDialog( frame, "Load an INP or XLSX file with network configuration before opening the MSX file.", "Error", JOptionPane.OK_OPTION); return; } msxFile = netFile; msxName.setText(fileChooser.getSelectedFile().getName()); // fileChooser.getFile()); reportOptions = null; } saveAction.setEnabled(true); runAction.setEnabled(true); } }