MjoMenus.java :  » Graphic-Library » mjograph » mjo » core » Java Open Source

Java Open Source » Graphic Library » mjograph 
mjograph » mjo » core » MjoMenus.java
//
//  MjoMenus.java
//  MjoGraph


package mjo.core;


import mjo.components.*;
import mjo.scan.*;
import mjo.order.*;
import mjo.core.plot.*;
import mjo.core.data.*;
import mjo.undo.*;

import java.util.*;
import java.io.*;
import java.text.DecimalFormat;
import java.awt.geom.*;

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.datatransfer.*; //for clipboard

import java.awt.print.*;
import javax.print.*;
import javax.print.attribute.*;
import javax.print.attribute.standard.*;

import java.net.*;

public class MjoMenus{
    //
    final static public String scriptPath;

    static{//
      //
//   String path = System.getProperty("java.class.path");
//   path = path.substring(0, path.length() - 12); //12"mjograph.jar"
//   scriptPath = path + "scripts/";

      //
  URL mySource = MjoGraph.class.getProtectionDomain().getCodeSource().getLocation();
  //System.out.println(mySource.getPath());

  String path = mySource.getPath();
  //jar
  if(path.endsWith("bin/"))
      path = path.substring(0, path.length() - 4); // 4 = "bin/"
  else
      path = path.substring(0, path.length() - 12); // 12 = "mjograph.jar"

  scriptPath = path + "scripts/";

  
  //System.out.println(scriptPath);
    }

    //
    final private MjoGraph mg;

    //
    public MjoMenus(MjoGraph m){
  mg = m;
    }

    

    //
    private class QuitAction extends AbstractAction{
  public void actionPerformed(ActionEvent e){
      mg.dispose();      
  }
    }
    public Action getQuiteAction(){
  return new QuitAction();
    }

    //
    private class VersionInfoAction extends AbstractAction{
  public void actionPerformed(ActionEvent e){
      JOptionPane.showMessageDialog(null, mg.VERSION_INFO, "about", JOptionPane.INFORMATION_MESSAGE);
  }  
    }
    public Action getVersionInfoAction(){
  return new VersionInfoAction();
    }

    //website
    private class GoToWebSiteAction extends AbstractAction{
  //
  private String s;

  public GoToWebSiteAction(String s){
      this.s = s;
  }
  public void actionPerformed(ActionEvent e){
      String [] cmd = {"open", s};

      try{
    // URL 
    Runtime.getRuntime().exec(cmd);
      }catch(IOException ee){
    ee.printStackTrace();
      }
  }
    }
    public Action getGoToWebSiteAction(String s){
  return new GoToWebSiteAction(s);
    }
    
    //
    private class NewFrameAction extends AbstractAction{
  public void actionPerformed(ActionEvent e){
      MjoGraph.launchNewFrame();
  }  
    }
    public Action getNewFrameAction(){
  return new NewFrameAction();
    }

    //
    private class saveAction extends AbstractAction{
  public void actionPerformed(ActionEvent e){
      if(mg.graphArchive == null){
    mg.saveAsMenu.doClick();
    return;
      }

      try{
    FileOutputStream fos = new FileOutputStream(new File(mg.graphDirectory, mg.graphArchive));
    ObjectOutput oos = new ObjectOutputStream(fos);
    
    //
    oos.writeObject(mg.cPanel.getChart());
    //
    oos.writeBoolean(mg.isLogX());
    oos.writeBoolean(mg.isLogY());
    //
    oos.writeBoolean(mg.graphSizeSettingsPane.dim1.isSelected());

    // ver3.3.1
    oos.writeObject(mg.cPanel.getSize());
    
    //
    oos.flush();
    oos.close();
    fos.close();
    
    //
    mg.recent.add(new File(mg.graphDirectory, mg.graphArchive));
    for(Iterator i = mg.frames.iterator(); i.hasNext();){
        JMenuItem rMenu = mg.recent.createMenuItem();
        MjoGraph m = (MjoGraph)i.next();
        m.fileMenu.remove(4);
        m.fileMenu.add(rMenu, 4);
    }
    
    //
    mg.status.setText("Saved.");
    
    //
    mg.initModification();
      }catch(IOException ioe){
    ioe.printStackTrace();
    System.out.println(ioe);
      }
  }  
    }
    public Action getSaveAction(){
  return new saveAction();
    }


    private class saveAsAction extends AbstractAction{
  public void actionPerformed(ActionEvent e){
      //
      FileDialog fd = new FileDialog(mg, "save as", FileDialog.SAVE);
      if(mg.graphArchive == null || mg.graphArchive.equals("")){
    //
    Calendar now = Calendar.getInstance();
    int y = now.get(Calendar.YEAR);
    int m = now.get(Calendar.MONTH) + 1;
    int d = now.get(Calendar.DATE);
    fd.setFile(y + "_" + m + "_" + d + "-" + mg.numFrame + ".mjo");
    //fd.setFile("untitled" + numFrame + ".mjo");
      }else
    fd.setFile(mg.graphArchive);
      fd.setVisible(true);
      
      //
      if(fd.getFile() == null)
    return;
      
      mg.graphDirectory = fd.getDirectory();
      mg.graphArchive = fd.getFile();
      mg.saveMenu.doClick();  

      try{//
    //
    String tt = mg.cPanel.getChart().getTitle().getText();
    mg.setTitle(tt);
      }catch(NullPointerException npe){
      }
        
  }
    }
    public Action getSaveAsAction(){
  return new saveAsAction();
    }

    //
    private class loadAction extends AbstractAction{
  public void actionPerformed(ActionEvent e){
      //
      FileDialog fd = new FileDialog(mg, "load");
      fd.setVisible(true);
      
      //
      if(fd.getFile() == null)
    return;
      
      //
      mg.recent.openFile(new File(fd.getDirectory(), fd.getFile()));
  }
    }
    public Action getLoadAction(){
  return new loadAction();
    }

    //add
    private class AddSeriesAction extends AbstractAction{
  public void actionPerformed(ActionEvent e){
      //FileChooseraquaFieDialog
      FileDialog fd = new FileDialog(mg);
      fd.setVisible(true);
      
      //
      if(fd.getFile() != null)
    mg.importHandler.importFile(mg.dataList, new File(fd.getDirectory(), fd.getFile()));
  }
    }
    public Action getAddSeriesAction(){
  return new AddSeriesAction();
    }

    //
    private class CreateFromClipboardAction extends AbstractAction{
  public void actionPerformed(ActionEvent e){
      Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
      Transferable object = clipboard.getContents(null);
      try{
    String str = (String)object.getTransferData(DataFlavor.stringFlavor);
    mg.importHandler.importFromString(mg.dataList, str);
      }catch(UnsupportedFlavorException flavorException){
    System.out.println("unsupported favor " + flavorException.toString());
      }catch (IOException ioe){
    ioe.printStackTrace();
      }
  }
    }
    public Action getCreateFromClipboardAction(){
  return new CreateFromClipboardAction();
    }

    //add
    private class AddFunctionAction extends AbstractAction{
  public void actionPerformed(ActionEvent e){
      FunctionInputDialog dialog = new FunctionInputDialog(mg);
      dialog.setLocationRelativeTo(mg);
      dialog.setVisible(true);
      String t = dialog.getFunctionText();
      if(!t.equals(""))
    mg.importHandler.importFunction(mg.dataList, t);
  }
    }
    public Action getAddFunctionAction(){
  return new AddFunctionAction();
    }

    //
    private class EditFunctionAction extends AbstractAction{
  public void actionPerformed(ActionEvent e){
      //
      MjoXYSeries mseries;

      //
      if(mg.selIndices.length != 1)
    return;

      //MjoFunctionSeries
      mseries = mg.mjodataset.getSeries(mg.selIndices[0]);
      if(!(mseries instanceof MjoFunctionSeries))
    return;

      //
      MjoFunctionSeries mfseries = (MjoFunctionSeries)mseries;      
      FunctionInputDialog dialog = new FunctionInputDialog(mg, mfseries.getFormalSourceFileName());
      
      dialog.setLocationRelativeTo(mg);
      dialog.setVisible(true);

      String t = dialog.getFunctionText();
      if(!t.equals("") && t.equals(mfseries.getFormalSourceFileName())){
    OnceDoEdit ue = new FunctionEditEdit(mg.mjodataset, mg.mjoxyplot, mfseries, t);
    mg.undoer.doAndRegister(ue);
      }
  }
    }
    public Action getEditFunctionAction(){
  return new EditFunctionAction();
    }

    //del
    private class DeleteSeriesAction extends AbstractAction{
  public void actionPerformed(ActionEvent e){
      // -- 
      //int ans = JOptionPane.showConfirmDialog(null, "Do you really delete the selected series(es)?\n This command can not undo.", "delete series", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
      
      //if(ans == JOptionPane.YES_OPTION){
      mg.importHandler.deleteSeries(mg.dataList, mg.selIndices);
      mg.doneUnundoableEdit();
      mg.status.setText("The selected series(es) was deleted. This cannot be un-done.");
    //}
  }
    }
    public Action getDeleteSeriesAction(){
  return new DeleteSeriesAction();
    }

    //del
    private class DeleteAnnotationObjectAction extends AbstractAction{
  public void actionPerformed(ActionEvent e){
      //
      if(mg.cPanel.getSelectedEntity() != null){
    mg.cPanel.deleteSelectedEntity();
    return;
      }
  }
    }
    public Action getDeleteAnnotationObjectAction(){
  return new DeleteAnnotationObjectAction();
    }
    

    //object
    private class DelAllAnnotationAction extends AbstractAction{
  public void actionPerformed(ActionEvent e){
      //
      int ans = JOptionPane.showConfirmDialog(null, "Do you really delete all the objects?\nThis command can not undo.", "delete", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
      if(ans == JOptionPane.YES_OPTION){
    mg.cPanel.deleteAllAnnotations();
    mg.doneUnundoableEdit();
      }
  }
    }
    public Action getDelAllAnnotationAction(){
  return new DelAllAnnotationAction();
    }


    //3.3.0
    //
    private class AppearanceAction extends AbstractAction{
  public void actionPerformed(ActionEvent e){
   //    //
//       if(mg.aDialog == null)
//     mg.aDialog = new MjoAppearanceDialog(mg, mg.cPanel.getChart());
      
//       //
//       mg.aDialog.setVisible(true);
//       //mg.aDialog.setFocusableWindowState(false);
  }
    }
    public Action getAppearanceAction(){
  return new AppearanceAction();
    }

    //
    private class PrintAction extends AbstractAction{
  public void actionPerformed( ActionEvent e ){
      try{
    PrinterJob pj = PrinterJob.getPrinterJob();
    PageFormat pf = pj.defaultPage();
    if(mg.cPanel.getSize().equals(GraphSizeSettingsPane.PREPERRED_DIM1))
        pf.setOrientation(PageFormat.LANDSCAPE);
    else
        pf.setOrientation(PageFormat.PORTRAIT);
    
    pj.setPrintable(mg.cPanel, pf);
    //pj.pageDialog(aset);
    if(pj.printDialog())
        pj.print();
      }catch(PrinterException pe){
    pe.printStackTrace();
    System.err.println(pe.getMessage());
      }
  }
    }
    public Action getPrintAction(){
  return new PrintAction();
    }


    //
    private class RevealAction extends AbstractAction{
  public void actionPerformed(ActionEvent e){
      java.util.List cmdSeq = new ArrayList();
      //
      cmdSeq.add("/bin/sh");
      
      String reveal = scriptPath + "reveal.sh";
      cmdSeq.add(reveal);
        
      //.reveal.sh
      MjoXYSeries mseries;
      for(int i = 0; i < mg.selIndices.length; i++){
    mseries = mg.mjodataset.getSeries(mg.selIndices[i]);
    cmdSeq.add(mseries.getFormalSourceFileName());
      }
      
      //to array
      String[] cmd = (String[])cmdSeq.toArray(new String[]{});
      
      try{
    Runtime.getRuntime().exec(cmd);
      }catch(IOException ioe){//
    mg.status.setText("failed to open file(s). (This command is macOSX only)");
      }
      
      
  }
    }
    public Action getRevealAction(){
  return new RevealAction();
    }

    
    //
    private class EditDataAction extends AbstractAction{
  public void actionPerformed(ActionEvent e){
      //
      if((mg.selIndices.length == 1) && (mg.mjodataset.getSeries(mg.selIndices[0]) instanceof MjoFunctionSeries)){
    editFunction();
    return;
      }

      java.util.List cmdSeq = new ArrayList();
      //
      cmdSeq.add("open");
      cmdSeq.add("-t");
      
      //
      MjoXYSeries mseries;
      for(int i = 0; i < mg.selIndices.length; i++){
    mseries = mg.mjodataset.getSeries(mg.selIndices[i]);
    if(mseries instanceof MjoFunctionSeries)//MjoFunctionSeries
        return;
    cmdSeq.add(mseries.getFormalSourceFileName());
      }
      
      //toArray
      // http://ameblo.jp/java/entry-10000993821.html
      String[] cmd = (String[])cmdSeq.toArray(new String[]{});
      
      try{
    Runtime.getRuntime().exec(cmd);
      }catch(IOException ioe){//
    mg.status.setText("failed to open file(s). (This command is macOSX only)");
      }
      
      
  }

  private void editFunction(){
      MjoFunctionSeries mfseries = (MjoFunctionSeries)mg.mjodataset.getSeries(mg.selIndices[0]);
      FunctionInputDialog dialog = new FunctionInputDialog(mg, mfseries.getFunctionName());
      dialog.setLocationRelativeTo(mg);
      dialog.setVisible(true);

      String t = dialog.getFunctionText();
      if(!t.equals("") && !t.equals(mfseries.getFunctionName())){
    OnceDoEdit ue = new FunctionEditEdit(mg.mjodataset, mg.mjoxyplot, mfseries, t);
    mg.undoer.doAndRegister(ue);
      }
  }
    }
    public Action getEditDataAction(){
  return new EditDataAction();
    }
    
    //reload
    private class ReloadAction extends AbstractAction{
  public void actionPerformed(ActionEvent e){
      mg.seriesSettingsPane.reloadButton.doClick();
  }
    }
    public Action getReloadAction(){
  return new ReloadAction();
    }


    //export
    private class ExportAction extends AbstractAction{
  public void actionPerformed(ActionEvent e){
      //.export
      if(mg.export == null){
    mg.export = new ImageExportDialog(mg);
      }

      //
      mg.cPanel.discardSelectedEntity();
    
      //.mjo
      if(mg.graphArchive != null && !mg.graphArchive.equals("")){
    String s;
    if(mg.graphArchive.endsWith(".mjo"))
        s = mg.graphArchive.substring(0, mg.graphArchive.length()-4);
    else
        s = mg.graphArchive;
    
    //
    String sep = System.getProperty("file.separator");
    if(mg.graphDirectory.endsWith(sep))
        mg.export.setPathWithAutoExtention(mg.graphDirectory + s);
    else
        mg.export.setPathWithAutoExtention(mg.graphDirectory + sep + s);
      }else{
    if(mg.export.getDrawn() != mg.cPanel){//
        mg.export.setDefaultPath();
    }
      }
      mg.export.setDrawn(mg.cPanel);
      mg.export.setLocationRelativeTo(mg);
      mg.export.setVisible(true);

      //FreeHEPexport
      //         if(export == null){
      //       //~/Desktop
      //       String s = System.getProperty("user.home");
      //       System.setProperty("user.home", s + System.getProperty("file.separator") + "Desktop");
      //       export = new ExportDialog();
      //       System.setProperty("user.home", s);
      
      //       //Portrait
      //       Properties pro = new Properties();
      //       pro.setProperty(PSGraphics2D.ORIENTATION, PageConstants.PORTRAIT);
      //       export.setUserProperties(pro);
      
      //         }
      //         //eps
      //         FloatLegendChart chart = (FloatLegendChart)cPanel.getChart();
      //         boolean b = chart.getUseImage();
      //         chart.setUseImage(false);
      //         export.showExportDialog(null, "Export Graph as ...", cPanel, "graph" );
      //         chart.setUseImage(b);
  }
  
    }
    public Action getExportAction(){
  return new ExportAction();
    }
    
    
}

java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.