Package ru.jnano.swing.bsaf

Source Code of ru.jnano.swing.bsaf.MainApp$AppExitListener

package ru.jnano.swing.bsaf;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.EventObject;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.CopyOnWriteArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.UIManager;

import org.jdesktop.application.Application;
import org.jdesktop.application.Application.ExitListener;

import ru.jnano.swing.AppLock;

import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.factories.FormFactory;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JTextPane;
import java.awt.Font;
import java.io.IOException;
import java.io.InputStream;

public class MainApp extends JFrame{
 
  private JTextPane textPane;
 
  private List<Class<? extends SingleTaskFrameApplication>> clazzs;
  private String[] args;
 
  private String[] descriptions;
  private String[] titles;
 
  private List<Application> apps;
 
 
  public MainApp(String title, List<Class<? extends SingleTaskFrameApplication>> _clazzs, String[] _args) {
    super();
    this.apps = new CopyOnWriteArrayList<Application>();
    this.clazzs=_clazzs;
    this.args=_args;
    setTitle(title);
    setResizable(false);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   
    init();
       
    getContentPane().setLayout(new FormLayout(new ColumnSpec[] {
        FormFactory.RELATED_GAP_COLSPEC,
        FormFactory.DEFAULT_COLSPEC,
        FormFactory.RELATED_GAP_COLSPEC,
        ColumnSpec.decode("max(150dlu;default):grow"),
        FormFactory.RELATED_GAP_COLSPEC,
        FormFactory.DEFAULT_COLSPEC,
        FormFactory.RELATED_GAP_COLSPEC,
        FormFactory.DEFAULT_COLSPEC,},
      new RowSpec[] {
        FormFactory.RELATED_GAP_ROWSPEC,
        FormFactory.DEFAULT_ROWSPEC,
        FormFactory.RELATED_GAP_ROWSPEC,
        FormFactory.DEFAULT_ROWSPEC,
        FormFactory.RELATED_GAP_ROWSPEC,
        RowSpec.decode("fill:max(87dlu;default)"),
        FormFactory.RELATED_GAP_ROWSPEC,
        FormFactory.DEFAULT_ROWSPEC,}));
   
    JLabel label = new JLabel("\u0412\u044B\u0431\u0435\u0440\u0438\u0442\u0435 \u043F\u0440\u0438\u043B\u043E\u0436\u0435\u043D\u0438\u0435:");
    label.setFont(new Font("Times New Roman", Font.ITALIC, 14));
    getContentPane().add(label, "2, 2");
   
    final JComboBox cbApps = new JComboBox(titles);
    cbApps.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        updateDescription(cbApps.getSelectedIndex());
      }
    });
    getContentPane().add(cbApps, "4, 2");
   
    JButton btLaunch = new JButton("\u0417\u0430\u043F\u0443\u0441\u0442\u0438\u0442\u044C");
    btLaunch.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent event) {
       
        Class<? extends Application> c = clazzs.get(cbApps.getSelectedIndex());
       
        for(Application app:apps){
          if (app.getClass().equals(c)) return;
        }
       
        Application.launch(c, args);
       
        setVisible(false);
               
        Thread thread = new Thread(new Runnable() {
         
          private long maxTime=60000;
          private long stepTime=100;
         
          @Override
          public void run() {
            try {
              long time=0;
              while(time<maxTime){
                Thread.sleep(stepTime);
                try{
                  ranApplication(Application.getInstance());
                  return;
                }catch(Exception e){
                  time+=stepTime;
                }
              }
              throw new IllegalStateException("Application launch timeout");
            } catch (Exception e) {
              e.printStackTrace();
            }
          }
        });
       
        thread.start();
       
       
      }
    });
    getContentPane().add(btLaunch, "6, 2");
   
    JLabel label_1 = new JLabel("\u041E\u043F\u0438\u0441\u0430\u043D\u0438\u0435:");
    label_1.setFont(new Font("Times New Roman", Font.BOLD, 14));
    getContentPane().add(label_1, "2, 4");
   
    textPane = new JTextPane();
    getContentPane().add(textPane, "2, 6, 5, 1");
   
    pack();
   
    setLocationRelativeTo(null);
   
    updateDescription(0);
   
       
  }
 
  private void ranApplication(Application app){
    SingleTaskFrameApplication appFrame = (SingleTaskFrameApplication) app;
    apps.add(appFrame);
    app.addExitListener(new AppExitListener(appFrame));
   
  }
 
  private class AppExitListener implements ExitListener{
   
    private SingleTaskFrameApplication app;
   
    public AppExitListener(SingleTaskFrameApplication app) {
      super();
      this.app = app;
    }

    @Override
    public boolean canExit(EventObject arg0) {
      apps.remove(app);
      app.externelShutdown();
      app.getMainFrame().setVisible(false);
      setVisible(true);
      return false;
    }

    @Override
    public void willExit(EventObject arg0) {
      // TODO Auto-generated method stub
     
    }
   
  }
 
  private void updateDescription(int index){
    if ("html".equals(descriptions[index].trim())){
      textPane.setContentType("text/html");
      try {
        textPane.setPage(clazzs.get(index).getResource("resources/"+clazzs.get(index).getSimpleName()+".html"));
      } catch (IOException e) {
        e.printStackTrace();
        textPane.setContentType("text");
        textPane.setText("html error");
      }
    }else{
      textPane.setContentType("text");
      textPane.setText(descriptions[index]);
    }
  }
 
  private void init(){
    titles = new String[clazzs.size()];
   
    descriptions = new String[clazzs.size()];
   
    for(int i=0; i<clazzs.size(); i++){
      Class<? extends Application> c = clazzs.get(i);
      Properties configProp = new Properties();
      InputStream in = c.getResourceAsStream("resources/"+c.getSimpleName()+".properties");
      try {
              configProp.load(in);
              titles[i] = configProp.getProperty("Application.title");
              descriptions[i] = configProp.getProperty("Application.description");
          } catch (IOException e) {
              e.printStackTrace();
          }
    }
  }
 
  public static void launch(final String title, final List<Class<? extends SingleTaskFrameApplication>> clazzs, final String[] args){
    EventQueue.invokeLater(new Runnable() {
      public void run() {
        try {
          UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
          if (AppLock.singletonApp("app.lock")){
            JFrame app=new MainApp(title,clazzs,args);
            app.setVisible(true);
          }
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    });
  }

 

}
TOP

Related Classes of ru.jnano.swing.bsaf.MainApp$AppExitListener

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.