Package org.mizartools.example.ui

Source Code of org.mizartools.example.ui.TestExecutablePanel$Job

/*
   Copyright (c) 2011 Mizar Tools Contributors (mizartools.org)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
/*  Contributors :
*  2011-03-11 Marco Riccardi - initial implementation
*/
package org.mizartools.example.ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.io.File;
import java.util.Calendar;
import java.util.LinkedList;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import org.mizartools.system.executable.ExecutableFileEnum;
import org.mizartools.system.executable.ExecutableFileManager;
import org.mizartools.utility.ProcessManager;
import org.mizartools.utility.TemporaryDirectory;
import org.mizartools.utility.TemporaryDirectoryException;
import org.mizartools.utility.TemporaryDirectoryFactory;

@SuppressWarnings("serial")
public class TestExecutablePanel extends JPanel {

  private JTextArea textArea;
  private JScrollPane scroller;
  private JProgressBar progressBar;
  private JButton button;

  public TestExecutablePanel(){
    super();
      setLayout(new BorderLayout());
    setPreferredSize(new Dimension(500, 150));
    button = new JButton();
    AbstractAction action =  new AbstractAction() {
         public void actionPerformed(ActionEvent e) {
           textArea.setText(Calendar.getInstance().getTime().toString()+"\n\n");
         Job job = new Job();
         Thread thread = new Thread(job);
         thread.start();
         }
     };
     button.setAction(action);
     button.setMaximumSize(getMaximumSize());
     button.setText("Start");
    add(button, BorderLayout.NORTH);
    textArea = new JTextArea();
    textArea.setEditable(false);
        scroller = new JScrollPane();
    scroller.getViewport().add(textArea);
    add(scroller, BorderLayout.CENTER);
    progressBar = new JProgressBar();
    add(progressBar, BorderLayout.SOUTH);
  }
 
  private class Job implements Runnable {

    @Override
    public void run() {
      button.setEnabled(false);
      TemporaryDirectory temporaryDirectory = null;
      long beforeTime = Calendar.getInstance().getTimeInMillis() - 24*60*60*1000;
      TemporaryDirectoryFactory.clearTemporaryDirectories(beforeTime);
      try {
        temporaryDirectory = TemporaryDirectoryFactory.newTemporaryDirectory();
      } catch (TemporaryDirectoryException e) {
        textArea.append(e.getMessage());
      }
      if (temporaryDirectory == null){
          textArea.append("\n");
          textArea.repaint();
          textArea.setCaretPosition(textArea.getText().length()-1);
          return;
      }
      File workDirectory = temporaryDirectory.getFile();
      textArea.append("Temporary Directory : " + workDirectory.getPath());
      textArea.append("\n");
      textArea.repaint();
      textArea.setCaretPosition(textArea.getText().length()-1);
        int i = 0;
        progressBar.setValue(0);
        progressBar.setMaximum(ExecutableFileEnum.values().length);
      boolean error = false;
        progressBar.setIndeterminate(false);
        for (ExecutableFileEnum executableFileEnum : ExecutableFileEnum.values()){
          if (!error) {
            textArea.append(executableFileEnum.toString());
            ExecutableFileManager executableFileManager = ExecutableFileManager.getInstance();
            LinkedList<String> command = new LinkedList<String>();
            command.add(executableFileManager.getCommand(executableFileEnum));
            ProcessManager processManager = new ProcessManager(workDirectory, command);
            processManager.setRedirectOutput(false);
            processManager.setCaptureLastOutput(false);
            processManager.setCaptureOutput(true);
            processManager.setPriority(Thread.MIN_PRIORITY);
            processManager.start();
            processManager.waitProcess();
            if (processManager.getException() == null) {
              if (!processManager.getOutput().isEmpty()) {
                if (processManager.getOutput().get(0).contains(executableFileEnum.getDescription())) {
                  textArea.append(" [OK]");
                  textArea.append(" ["+processManager.getOutput().get(0)+"]");
                  textArea.append(" \n");
                } else {
                  error = true;
                  textArea.append("\n\n [Error] \n");
                  textArea.append(" ["+processManager.getOutput().get(0)+"]");
                  textArea.append(" ["+executableFileEnum.getDescription()+"]");
                  textArea.append(" \n");
                }
              } else {
                if (!executableFileEnum.getDescription().equals("?")) {
                  error = true;
                  textArea.append("\n\n [Error] \nThe output message is not present.\n");
                  textArea.append(" \n");
                } else {
                  textArea.append(" [OK]");
                  textArea.append(" \n");
                }
              }
            } else {
              error = true;
              textArea.append("\n\n [Error] \n"+processManager.getException().getMessage()+"\n");
              textArea.append(" \n");
            }
           
  //          textArea.append("\n");
  //          for (String s : processManager.getOutput()) {
  //              textArea.append(s);
  //              textArea.append("\n");
  //          }
  //          textArea.append("\n");

            textArea.repaint();
            textArea.setCaretPosition(textArea.getText().length()-1);
            progressBar.setValue(i);
            progressBar.repaint();
            i++;
          }
        }
        textArea.setCaretPosition(textArea.getText().length()-1);
        progressBar.setValue(i);
        progressBar.repaint();
        progressBar.setIndeterminate(false);
        progressBar.setValue(0);
      button.setEnabled(true);
    }
   
  }
 
}
TOP

Related Classes of org.mizartools.example.ui.TestExecutablePanel$Job

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.