/**
   * Aware-P Epanet application entry point
   *
   * @param args
   * @throws UnsupportedLookAndFeelException
   */
  public static void main(String[] args) throws UnsupportedLookAndFeelException {
    if (Utilities.isMac()) {
      System.setProperty("apple.laf.useScreenMenuBar", "true");
      System.setProperty("com.apple.mrj.application.apple.menu.about.name", "Aware-P Epanet");

      Application app = Application.getApplication();
      app.setDockIconImage(
          Toolkit.getDefaultToolkit().getImage(EpanetUI.class.getResource("/uiresources/ae.png")));

      JMenuBar menuBar = new JMenuBar();
      JMenu fileMenu = new JMenu("File");
      menuBar.add(fileMenu);

      openAction = new JMenuItem("Open");
      saveAction = new JMenuItem("Save");
      runAction = new JMenuItem("Run");

      fileMenu.add(openAction);
      fileMenu.add(saveAction);
      fileMenu.add(runAction);
      app.setDefaultMenuBar(menuBar);
    }
    UIManager.setLookAndFeel(new Plastic3DLookAndFeel());
    new EpanetUI();
  }
    public boolean accept(File f) {
      if (f.isDirectory()) {
        return true;
      }

      if (Utilities.getFileExtension(f.getName()).equals("msx")) return true;
      return false;
    }
    public boolean accept(File f) {
      if (f.isDirectory()) {
        return true;
      }

      String extension = Utilities.getFileExtension(f.getName());

      if (extension.equals("msx") || extension.equals("inp") || extension.equals("xlsx"))
        return true;
      return false;
    }
  /** 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();
    }
  }
  /** 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);
    }
  }
  /** Aware-P Epanet frontend constructor. */
  public EpanetUI() {
    initLogger();

    frame = new JFrame();
    frame.setTitle(APP_TITTLE);
    frame.add(root);

    if (!Utilities.isMac()) {
      JMenuBar menuBar = new JMenuBar();

      JMenu fileMenu = new JMenu("File");
      menuBar.add(fileMenu);

      openAction = new JMenuItem("Open");
      saveAction = new JMenuItem("Save");
      runAction = new JMenuItem("Run");

      fileMenu.add(openAction);
      fileMenu.add(openAction);
      fileMenu.add(runAction);

      frame.setJMenuBar(menuBar);
    }

    openAction.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent actionEvent) {
            openEvent();
            network.repaint();
          }
        });

    saveAction.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent actionEvent) {
            saveEvent();
          }
        });

    runAction.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent actionEvent) {
            runSimulation();
          }
        });

    frame.pack();
    frame.setMinimumSize(new Dimension(848, 500));
    frame.setLocationRelativeTo(null);

    clearInterface();

    frame.setVisible(true);

    openINPButton.addActionListener(this);
    runSimulationButton.addActionListener(this);
    logoB.addActionListener(this);
    checkTanks.addActionListener(this);
    checkNodes.addActionListener(this);
    checkPipes.addActionListener(this);
    saveButton.addActionListener(this);
    // runMSXButton.addActionListener(this);
    // saveReport.addActionListener(this);

    frame.addWindowListener(
        new WindowListener() {
          public void windowOpened(WindowEvent e) {}

          public void windowClosing(WindowEvent e) {
            for (Handler handler : log.getHandlers()) {
              handler.flush();
            }
            System.exit(0);
          }

          public void windowClosed(WindowEvent e) {}

          public void windowIconified(WindowEvent e) {}

          public void windowDeiconified(WindowEvent e) {}

          public void windowActivated(WindowEvent e) {}

          public void windowDeactivated(WindowEvent e) {}
        });
  }