Package com.jedics.swing

Source Code of com.jedics.swing.JediMenuBar

package com.jedics.swing;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.HashMap;

import javax.swing.ButtonGroup;
import javax.swing.JFileChooser;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JSeparator;

import com.jedics.core.AlgorithmManager;
import com.jedics.core.JediManager;
import com.jedics.core.JediState;
import com.jedics.graph.GraphAlgorithm;

@SuppressWarnings("serial")
public class JediMenuBar extends JMenuBar {
  private JediFrame frame;
  private JediManager manager;
 
  private JediState currentState;

  private JMenu file;
  private JMenu view;
  private JMenu run;
 
  private JMenuItem reset;
  private JMenuItem clear;

  public JediMenuBar(JediFrame frame, JediManager manager) {
    this.frame = frame;
    this.manager = manager;
   
    currentState = null;

    initGeneral();
  }

  private void initGeneral() {
    reset = new JMenuItem("Reset");
    reset.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        manager.getHandler(currentState).reset();
      }
    });
   
    clear = new JMenuItem("Clear");
    clear.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        manager.getHandler(currentState).clear();
      }
    });
   
    JMenuItem preferences = new JMenuItem("Preferences");
    preferences.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent arg0) {
        // TODO Add preferences support.
      }
    });

    JMenuItem exit = new JMenuItem("Exit");
    exit.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent arg0) {
        System.exit(0);
      }
    });

    file = new JMenu("File");
    file.add(new JSeparator());
    file.add(reset);
    file.add(clear);
    file.add(new JSeparator());
    file.add(preferences);
    file.add(new JSeparator());
    file.add(exit);


    JRadioButtonMenuItem graph = new JRadioButtonMenuItem("Graph");
    graph.setSelected(true);
    graph.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent arg0) {
        frame.switchState(JediState.GRAPH);
      }
    });
   
    JRadioButtonMenuItem tree = new JRadioButtonMenuItem("Binary Trees");
    tree.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent arg0) {
        frame.switchState(JediState.TREE);
      }
    });

    ButtonGroup type = new ButtonGroup();
    type.add(graph);
    type.add(tree);

    view = new JMenu("View");
    view.add(graph);
    view.add(tree);

    run = new JMenu("Algorithms");

    add(file);
    add(view);
    add(run);
  }

  public void switchState(JediState state) {
    if(currentState != null) {

    }

    if(state == JediState.GRAPH) {
      JMenuItem menuItem = new JMenuItem("Save Graph");
      menuItem.addActionListener(new ActionListener() {
        public synchronized void actionPerformed(ActionEvent evt) {
          JFileChooser fc = new JFileChooser();
          fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
          fc.setDialogType(JFileChooser.SAVE_DIALOG);
          int ret = fc.showOpenDialog(JediMenuBar.this);
          if(ret != JFileChooser.APPROVE_OPTION)
            return;

          manager.getHandler(JediState.GRAPH).save(fc.getSelectedFile());
        }
      });
      file.add(menuItem, 0);

      menuItem = new JMenuItem("Load Graph");
      menuItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
          JFileChooser fc = new JFileChooser();
          fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
          fc.setCurrentDirectory(new File("."));
          fc.setDialogType(JFileChooser.OPEN_DIALOG);
          int ret = fc.showOpenDialog(JediMenuBar.this);
          if(ret != JFileChooser.APPROVE_OPTION)
            return;

          if(!fc.getSelectedFile().exists()) {
            JOptionPane.showMessageDialog(JediMenuBar.this, "The file you are trying to load does not exist.", "Unable to Load", JOptionPane.ERROR_MESSAGE);
            return;
          }
          manager.getHandler(JediState.GRAPH).load(fc.getSelectedFile());
        }
      });
      file.add(menuItem, 0);

      run.removeAll();

      HashMap<Class<GraphAlgorithm>, String> algorithms = AlgorithmManager.getGraphAlgorithms();
      for(final Class<? extends GraphAlgorithm> algorithm : algorithms.keySet()) {
        menuItem = new JMenuItem(algorithms.get(algorithm));
        menuItem.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            try {
              manager.getAlgorithmManager().execute(algorithm.newInstance());
            } catch (InstantiationException ex) {
              JOptionPane.showMessageDialog(null, algorithm.getCanonicalName() + " could not be instantiated with the constructor of a GraphHandler.\nPlease make sure the class is public, and has public constructor with that signature.", "InstantiationException", JOptionPane.ERROR_MESSAGE);
            } catch (IllegalAccessException ex) {
              JOptionPane.showMessageDialog(null, algorithm.getCanonicalName() + " could not be instantiated. Please make sure that there is a default public constructor.", "IllegalAccessException", JOptionPane.ERROR_MESSAGE);
            }
          }
        });
        run.add(menuItem);
      }
    }

    currentState = state;
  }
}
TOP

Related Classes of com.jedics.swing.JediMenuBar

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.