Package net.sf.cannagrower

Source Code of net.sf.cannagrower.CannaGrower

/*
*  CannaGrower [http://cannagrower.sourceforge.net]
*  Copyright (C) 2006 Alois Cochard <alois_cochard@users.sourceforge.net>
*
*  This program is free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  This program is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  You should have received a copy of the GNU General Public License along
*  with this program; if not, write to the Free Software Foundation, Inc.,
*  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

package net.sf.cannagrower;

import java.awt.BorderLayout;
import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFileChooser;
import javax.swing.JInternalFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import javax.swing.filechooser.FileFilter;

import net.sf.cannagrower.data.Culture;
import net.sf.cannagrower.data.Resources;
import net.sf.cannagrower.data.event.PlantsAdd;
import net.sf.cannagrower.gui.DialogStatus;
import net.sf.cannagrower.gui.FrameAbout;
import net.sf.cannagrower.gui.FrameCulture;
import net.sf.cannagrower.gui.FrameResources;
import net.sf.cannagrower.gui.FrameSettings;
import net.sf.cannagrower.gui.FrameTutorial;
import net.sf.cannagrower.gui.PanelVariety;
import net.sf.cannagrower.i18n.Messages;
import net.sf.orexio.common.InstanceManager;
import net.sf.orexio.common.ui.JFrame;
import net.sf.orexio.common.ui.SpotlightPanel;
import net.sf.orexio.lopf.repository.TrueZipRepository;

// (finish) TODO to_v0.2.0_Beta:  Create class to manage icon (as message in i18n...)
// TODO to_v0.2.0_Beta:  Add splashscreen in manifest and when loading main window
// TODO to_v0.2.0_Beta:  Link '.canna' file with system
// TODO to_v0.2.0_Beta: Upgrade all dependencies
// TODO to_v0.2.0_Beta:  Manage move of plants with INTERACTIVITY !
// TODO to_v0.2.0_Beta:  Integrate custum medium modification product
// TODO to_v0.2.0_Beta:  Improve stats and advice, with plugin/custum integration
// TODO to_v0.3.0_Beta:  Integrate internet access to variety/product repository
// TODO to_v0.5.0_Beta:  Improve GUI interaction using thread and EDT... and check Java FX possibilities...
// TODO to_v1.0.0_RC1:  Full integration of unit testing

/**
* This class is entry point to the application, this is the main frame
*
* @author alois_cochard@users.sf.net
*
*/
public class CannaGrower extends JFrame {

  public static CannaGrower cannagrower;
 
  public static final String   extension = ".canna";

  public static JFileChooser fileChooser;

  private static  InstanceManager  instanceManager=new InstanceManager(CannaGrower.class.getSimpleName());

  private static final long   serialVersionUID = 1L;
 
  private static File      tempFolder;

  public static final String   versionLabel = "Beta";

  public static final double   versionNumber = 0.1;

  public static final int   versionRevision = 2;

  public static final String   webMain = "http://cannagrower.sourceforge.net/";

  public static final String   webManual = "http://cannagrower.sourceforge.net/manual/"; // @jve:decl-index=0:
 
  public static CannaGrower getCannaGrower() {
    return cannagrower;
  }

  public static File getCannaGrowerFolder(){
      File        folder;
      String      folderPath=System.getProperty("user.home");
      boolean    success=false;

        if(!folderPath.endsWith(System.getProperty("file.separator"))){folderPath+=System.getProperty("file.separator");}
        folder=new File(folderPath+"."+CannaGrower.class.getSimpleName());
       
        try{
          folder.mkdirs();
          success=folder.exists();
        }catch(Exception e){}
       
        // Using home folder instead of a sub '.CannaGrower' folder.
        if(!success){folder=new File(folderPath);}
       
        // Testing access
        if(!folder.exists() || !folder.canWrite() || !folder.canRead()){
          Messages.showException(new Exception(Messages.getMessage(Messages.systemHomeNoAccess, folder.getAbsolutePath())));
          System.exit(1);
        }

    return folder;
  }

  public static File getTempFile(String fileName){
    String filePath=getTempFolder().getAbsolutePath();
    if(!filePath.endsWith(System.getProperty("file.separator"))){filePath+=System.getProperty("file.separator");}
    return new File(filePath+fileName);
  }

 
 
  public static File getTempFolder(){
    File   tempSystemFolder;
    String   path;
   
    if(tempFolder==null){
      path=System.getProperty("java.io.tmpdir");
      if(!path.endsWith(System.getProperty("file.separator"))){path+=System.getProperty("file.separator");}
      tempSystemFolder=new File(path);
     
      path+=CannaGrower.class.getSimpleName()+"-"+CannaGrower.getOwnerlessWindows().hashCode()+System.getProperty("file.separator");
     
      tempFolder=new File(path);
     
      if(!tempFolder.exists()){
        if(!tempFolder.mkdir()){
          tempFolder=tempSystemFolder;
        }
      }
    }
    return tempFolder;
  }

  /**
   * This method return the full name of the version
   * @return The version name in a String
   */
  public static String getVersionFullName(){
    return String.valueOf(versionNumber)+"."+String.valueOf(versionRevision)+" "+versionLabel;
  }

  public static void launch(){
    if(cannagrower!=null){return;}

    // Opening CannaGrower
    cannagrower = new CannaGrower();
    cannagrower.setVisible(true);
   

    // Optionally opening Demo culture
    SwingUtilities.invokeLater(new Runnable(){
      public void run(){
        if(CannaGrowerProperties.getProperties().getShowDemo()){cannagrower.launchDemo();}
      }
    });
  }

  /**
   * This method launch application from OS
   *
   * @param args
   */
  public static void main(String[] args){
    // Checking instance
    if(instanceManager.isActive()){
      Messages.showException(new Exception(Messages.getMessage(Messages.systemAlreadyRunning)));
      System.exit(1);
    }
   
    // Add optional Laf.
    javax.swing.UIManager.installLookAndFeel(new org.jvnet.substance.skin.SubstanceCremeLookAndFeel().getName(),org.jvnet.substance.skin.SubstanceCremeLookAndFeel.class.getName());
    javax.swing.UIManager.installLookAndFeel(new org.jvnet.substance.skin.SubstanceBusinessLookAndFeel().getName(),org.jvnet.substance.skin.SubstanceBusinessLookAndFeel.class.getName());
    javax.swing.UIManager.installLookAndFeel(new org.jvnet.substance.skin.SubstanceModerateLookAndFeel().getName(),org.jvnet.substance.skin.SubstanceModerateLookAndFeel.class.getName());
    javax.swing.UIManager.installLookAndFeel(new org.jvnet.substance.skin.SubstanceSaharaLookAndFeel().getName(),org.jvnet.substance.skin.SubstanceSaharaLookAndFeel.class.getName());
    javax.swing.UIManager.installLookAndFeel(new org.jvnet.substance.skin.SubstanceOfficeBlue2007LookAndFeel().getName(),org.jvnet.substance.skin.SubstanceOfficeBlue2007LookAndFeel.class.getName());
    javax.swing.UIManager.installLookAndFeel(new org.jvnet.substance.skin.SubstanceOfficeSilver2007LookAndFeel().getName(),org.jvnet.substance.skin.SubstanceOfficeSilver2007LookAndFeel.class.getName());

    // Setting lopf truezip
    TrueZipRepository.setCustomExtensions(".jar",extension);

    SwingUtilities.invokeLater(new Runnable(){
      public void run() {
        if(settingsOpen()){
          // Open settings window
          FrameSettings settings=new FrameSettings();
          settings.setVisible(true);
        }else{
          // Directly open cannagrower
          launch();
        }
      }
    });
  }

  private static boolean settingsOpen(){
    java.util.Locale            locale;
    javax.swing.UIManager.LookAndFeelInfo  lafInfo;
    String                  laf;

    // Apply locale
    locale=CannaGrowerProperties.getProperties().getLocale();
    if(locale!=null){java.util.Locale.setDefault(locale);}
    // Apply laf
    lafInfo=CannaGrowerProperties.getProperties().getLaf();
    if(lafInfo==null){
      laf=javax.swing.UIManager.getSystemLookAndFeelClassName();
    }else{
      laf=lafInfo.getClassName();
    }
    try{javax.swing.UIManager.setLookAndFeel(laf);}
    catch(Exception e){}

    // Return show config flag
    return CannaGrowerProperties.getProperties().getShowSettings();
  }

  private FrameAbout about = new FrameAbout();

  FrameCulture cultureActive = null;

  FrameTutorial cultureDemo = null;

  private JButton jButtonToolBarHelp = null;

  private JButton jButtonToolBarNew = null;

  private JButton jButtonToolBarOpen = null;

  private JButton jButtonToolBarSave = null;

  private JPanel jContentPane = null;

  private JDesktopPane jDesktopPane = null;

  private JMenuBar jJMenuBar = null;

  private JToolBar jJToolBar = null;

  private JMenu jMenuFile = null;

  private JMenu jMenuHelp = null;

  private JMenuItem jMenuItemFileClose = null;

  private JMenuItem jMenuItemFileExit = null;

  private JMenuItem jMenuItemFileNew = null;

  private JMenuItem jMenuItemFileOpen = null;

  private JMenuItem jMenuItemFileSave = null;
 
  private JMenuItem jMenuItemFileSettings = null;
 
  private JMenuItem jMenuItemHelpAbout = null;

  private JMenuItem jMenuItemHelpDemo = null;

  private JMenuItem jMenuItemHelpOnlineManual = null;

  private JMenuItem jMenuItemResourcesVarieties = null;
 
  private JMenu jMenuResources = null;

  private JMenu jMenuWindow = null;


  private SpotlightPanel jPanelSpotlight;

  private DialogStatus status = new DialogStatus(this);
 
  private FrameResources varieties=new FrameResources(Resources.getResources().getVarieties(),new PanelVariety(),Resources.getVarietiesFactory());
  /**
   * This is the default constructor
   */
  public CannaGrower() {
    super(CannaGrowerProperties.getProperties());
    initialize();
  }

  /**
   * This method close all open culture.
   *
   * @return False if user abort operation
   */
  private boolean close() { 
    // Closing all cultures
    for(javax.swing.JInternalFrame frame:jDesktopPane.getAllFrames()){
      if(frame.getClass().equals(FrameCulture.class)){
        FrameCulture cultureViewer=(FrameCulture)frame;
        if(!cultureViewer.close())return false;
      }
    }
   
    // Saving ressources
    if(Resources.getResources().isModified()){
      switch(JOptionPane.showConfirmDialog(this, Messages.getMessage(Messages.ressourcesAskSave), getTitle(),
          JOptionPane.YES_NO_CANCEL_OPTION)){
        case JOptionPane.YES_OPTION:
          try{
            Resources.getResources().save();
            Resources.getResources().getRepository().write();
          }
          catch(Exception e){e.printStackTrace();}         
          break;
        case JOptionPane.NO_OPTION:
          break;
        case JOptionPane.CANCEL_OPTION:
          return false;
      }
    }
   
    // Cleaning tempory folder
    getTempFolder().delete();
   
    return true;
  }

  public FrameCulture getCultureActive() {
    return cultureActive;
  }

  private JButton getJButtonToolBarHelp() {
    if (jButtonToolBarHelp == null) {
      jButtonToolBarHelp = new JButton();
      jButtonToolBarHelp.setIcon(new ImageIcon(getClass().getResource(
          "/net/sf/cannagrower/images/help_32.png")));
    }
    return jButtonToolBarHelp;
  }
  private JButton getJButtonToolBarNew() {
    if (jButtonToolBarNew == null) {
      jButtonToolBarNew = new JButton();
      jButtonToolBarNew.setIcon(new ImageIcon(getClass().getResource(
          "/net/sf/cannagrower/images/filenew_32.png")));
      jButtonToolBarNew
          .addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent e) {
              menuFileNew();
            }
          });
    }
    return jButtonToolBarNew;
  }

  private JButton getJButtonToolBarOpen() {
    if (jButtonToolBarOpen == null) {
      jButtonToolBarOpen = new JButton();
      jButtonToolBarOpen.setIcon(new ImageIcon(getClass().getResource(
          "/net/sf/cannagrower/images/fileopen_32.png")));
      jButtonToolBarOpen
          .addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent e) {
              menuFileOpen();
            }
          });
    }
    return jButtonToolBarOpen;
  }

  private JButton getJButtonToolBarSave() {
    if (jButtonToolBarSave == null) {
      jButtonToolBarSave = new JButton();
      jButtonToolBarSave.setIcon(new ImageIcon(getClass().getResource(
          "/net/sf/cannagrower/images/filesave_32.png")));
      jButtonToolBarSave
          .addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent e) {
              menuFileSave();
            }
          });
    }
    return jButtonToolBarSave;
  }

  private JPanel getJContentPane() {
    if (jContentPane == null) {
      jContentPane = new JPanel();
      jContentPane.setLayout(new BorderLayout());
      jContentPane.add(getJJToolBar(), BorderLayout.NORTH);
      jContentPane.add(getJDesktopPane(), BorderLayout.CENTER);
    }
    return jContentPane;
  }

  /**
   * This method initializes jDesktopPane
   *
   * @return javax.swing.JDesktopPane
   */
  private JDesktopPane getJDesktopPane() {
    if (jDesktopPane == null) {
      jDesktopPane = new JDesktopPane();
      jDesktopPane.add(varieties);
      varieties.setBounds(20, 20,600,400);
      jDesktopPane.add(about);
      about.setBounds(20,20,600,400);
    }
    return jDesktopPane;
  }

  private JFileChooser getFileChooser(){
    if(fileChooser==null){
      fileChooser= new JFileChooser();
    }
    return fileChooser;
  }
  private JMenuBar getJJMenuBar() {
    if (jJMenuBar == null) {
      jJMenuBar = new JMenuBar();
      jJMenuBar.add(getJMenuFile());
      jJMenuBar.add(getJMenuResources());
      jJMenuBar.add(getJMenuWindow());
      jJMenuBar.add(getJMenuHelp());
    }
    return jJMenuBar;
  }

  private JToolBar getJJToolBar() {
    if (jJToolBar == null) {
      jJToolBar = new JToolBar();
      jJToolBar.add(getJButtonToolBarNew());
      jJToolBar.add(getJButtonToolBarOpen());
      jJToolBar.add(getJButtonToolBarSave());
      jJToolBar.add(getJButtonToolBarHelp());
    }
    return jJToolBar;
  }

  private JMenu getJMenuFile() {
    if (jMenuFile == null) {
      jMenuFile = new JMenu();
      jMenuFile.setText(Messages.getMessage(Messages.menuFile));
      jMenuFile.setIcon(new ImageIcon(getClass().getResource(
          "/net/sf/cannagrower/images/filenew_16.png")));
      jMenuFile.add(getJMenuItemFileNew());
      jMenuFile.add(getJMenuItemFileOpen());
      jMenuFile.add(getJMenuItemFileSave());
      jMenuFile.add(getJMenuItemFileClose());
      jMenuFile.addSeparator();
      jMenuFile.add(getJMenuItemFileSettings());
      jMenuFile.addSeparator();
      jMenuFile.add(getJMenuItemFileExit());

    }
    return jMenuFile;
  }

  private JMenu getJMenuHelp() {
    if (jMenuHelp == null) {
      jMenuHelp = new JMenu();
      jMenuHelp.setText(Messages.getMessage(Messages.menuHelp));
      jMenuHelp.setIcon(new ImageIcon(getClass().getResource(
          "/net/sf/cannagrower/images/help_16.png")));
      jMenuHelp.add(getJMenuItemHelpDemo());
      jMenuHelp.add(getJMenuItemHelpOnlineManual());
      jMenuHelp.add(getJMenuItemHelpAbout());
    }
    return jMenuHelp;
  }

  private JMenuItem getJMenuItemFileClose() {
    if (jMenuItemFileClose == null) {
      jMenuItemFileClose = new JMenuItem();
      jMenuItemFileClose.setIcon(new ImageIcon(getClass().getResource(
          "/net/sf/cannagrower/images/fileclose_16.png")));
      jMenuItemFileClose.setText(Messages
          .getMessage(Messages.menuFileClose));
      jMenuItemFileClose
          .addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent e) {
              menuFileClose();
            }
          });
    }
    return jMenuItemFileClose;
  }

  private JMenuItem getJMenuItemFileExit() {
    if (jMenuItemFileExit == null) {
      jMenuItemFileExit = new JMenuItem();
      jMenuItemFileExit.setIcon(new ImageIcon(getClass().getResource(
          "/net/sf/cannagrower/images/finish_16.png")));
      jMenuItemFileExit.setText(Messages.getMessage(Messages.menuFileExit));
      jMenuItemFileExit
          .addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent e) {
              menuFileExit();
            }

          });
    }
    return jMenuItemFileExit;
  }

  private JMenuItem getJMenuItemFileNew() {
    if (jMenuItemFileNew == null) {
      jMenuItemFileNew = new JMenuItem();
      jMenuItemFileNew.setText(Messages.getMessage(Messages.menuFileNew));
      jMenuItemFileNew.setIcon(new ImageIcon(getClass().getResource(
          "/net/sf/cannagrower/images/filenew_16.png")));
      jMenuItemFileNew
          .addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent e) {
              menuFileNew();
            }
          });
    }
    return jMenuItemFileNew;
  }

  private JMenuItem getJMenuItemFileOpen() {
    if (jMenuItemFileOpen == null) {
      jMenuItemFileOpen = new JMenuItem();
      jMenuItemFileOpen.setIcon(new ImageIcon(getClass().getResource(
          "/net/sf/cannagrower/images/fileopen_16.png")));
      jMenuItemFileOpen.setText(Messages.getMessage(Messages.menuFileOpen));
      jMenuItemFileOpen
          .addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent e) {
              menuFileOpen();
            }
          });
    }
    return jMenuItemFileOpen;
  }

  private JMenuItem getJMenuItemFileSave() {
    if (jMenuItemFileSave == null) {
      jMenuItemFileSave = new JMenuItem();
      jMenuItemFileSave.setIcon(new ImageIcon(getClass().getResource(
          "/net/sf/cannagrower/images/filesave_16.png")));
      jMenuItemFileSave.setText(Messages.getMessage(Messages.menuFileSave));
      jMenuItemFileSave
          .addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent e) {
              menuFileSave();
            }
          });
    }
    return jMenuItemFileSave;
  }

  /**
   * This method initializes jMenuItemSettings
   *
   * @return javax.swing.JMenuItem
   */
  private JMenuItem getJMenuItemFileSettings() {
    if (jMenuItemFileSettings == null) {
      jMenuItemFileSettings = new JMenuItem();
      jMenuItemFileSettings.setIcon(new ImageIcon(getClass().getResource(
          "/net/sf/cannagrower/images/configure_16.png")));
      jMenuItemFileSettings.setText(Messages
          .getMessage(Messages.menuFileSettings));
      jMenuItemFileSettings
          .addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent e) {
              menuFileSettings();
            }
          });
    }
    return jMenuItemFileSettings;
  }

  private JMenuItem getJMenuItemHelpAbout() {
    if (jMenuItemHelpAbout == null) {
      jMenuItemHelpAbout = new JMenuItem();
      jMenuItemHelpAbout.setText(Messages
          .getMessage(Messages.menuHelpAbout));
      jMenuItemHelpAbout.setIcon(new ImageIcon(getClass().getResource(
          "/net/sf/cannagrower/images/cannagrower_16.png")));
      jMenuItemHelpAbout
          .addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent e) {
              menuHelpAbout();
            }
          });
    }
    return jMenuItemHelpAbout;
  }

  /**
   * This method initializes jMenuItemHelpDemo
   *
   * @return javax.swing.JMenuItem
   */
  private JMenuItem getJMenuItemHelpDemo() {
    if (jMenuItemHelpDemo == null) {
      jMenuItemHelpDemo = new JMenuItem();
      jMenuItemHelpDemo.setText(Messages
          .getMessage(Messages.menuHelpDemo));
      jMenuItemHelpDemo.setIcon(new ImageIcon(getClass().getResource(
          "/net/sf/cannagrower/images/data/box_16.png")));
      jMenuItemHelpDemo
          .addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent e) {
              menuHelpDemo();
            }
          });
    }
    return jMenuItemHelpDemo;
  }
 
  private JMenuItem getJMenuItemHelpOnlineManual() {
    if (jMenuItemHelpOnlineManual == null) {
      jMenuItemHelpOnlineManual = new JMenuItem();
      jMenuItemHelpOnlineManual.setIcon(new ImageIcon(getClass()
          .getResource("/net/sf/cannagrower/images/help_16.png")));
      jMenuItemHelpOnlineManual.setText(Messages
          .getMessage(Messages.menuHelpOnlineManual));
      jMenuItemHelpOnlineManual
          .addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent e) {
              menuHelpManual();
            }
          });
    }
    return jMenuItemHelpOnlineManual;
  }
 
  private JMenuItem getJMenuItemResourcesVarieties() {
    if (jMenuItemResourcesVarieties == null) {
      jMenuItemResourcesVarieties = new JMenuItem();
      jMenuItemResourcesVarieties.setText(Messages.getMessage(Messages.menuResourcesVarieties));
      jMenuItemResourcesVarieties.setIcon(new ImageIcon(getClass().getResource("/net/sf/cannagrower/images/data/plants_16.png")));
      jMenuItemResourcesVarieties
          .addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent e) {
              menuResourcesVarieties();
            }
          });
    }
    return jMenuItemResourcesVarieties;
  }

  private JMenu getJMenuResources() {
    if (jMenuResources == null) {
      jMenuResources = new JMenu();
      jMenuResources.setText(Messages.getMessage(Messages.menuResources));
      jMenuResources.setIcon(new ImageIcon(getClass().getResource("/net/sf/cannagrower/images/queue_16.png")));
      jMenuResources.add(getJMenuItemResourcesVarieties());
    }
    return jMenuResources;
  }
 
  /**
   * This method initializes jMenuWindow
   *
   * @return javax.swing.JMenu
   */
  private JMenu getJMenuWindow() {
    if (jMenuWindow == null) {
      jMenuWindow = new JMenu();
      jMenuWindow.setIcon(new ImageIcon(getClass().getResource("/net/sf/cannagrower/images/window_list_16.png")));
      jMenuWindow.setText(Messages.getMessage(Messages.menuWindow));
      jMenuWindow.addMenuListener(new javax.swing.event.MenuListener() {
        public void menuCanceled(javax.swing.event.MenuEvent e) {
        }
        public void menuDeselected(javax.swing.event.MenuEvent e) {
        }
        public void menuSelected(javax.swing.event.MenuEvent e) {
          menuWindow();
        }
      });
    }
    return jMenuWindow;
  }

  public SpotlightPanel getJPanelSpotlight() {
    return jPanelSpotlight;
  }

  /**
   * This method initializes the GUI
   *
   * @return void
   */
  private void initialize() {
    setIconImage(Toolkit.getDefaultToolkit().getImage(
        getClass().getResource(
            "/net/sf/cannagrower/images/cannagrower_32.png")));
    setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
    setPreferredSize(new Dimension(800, 600));
    setMinimumSize(new Dimension(800, 600));
    setSize(800,600);
    setJMenuBar(getJJMenuBar());
    setContentPane(getJContentPane());
    setTitle(CannaGrower.class.getSimpleName()+" "+getVersionFullName());
    addWindowListener(new java.awt.event.WindowAdapter() {
      public void windowClosing(java.awt.event.WindowEvent e) {
        menuFileExit();
      }
    });

    // Add spotlight system
    jPanelSpotlight = new SpotlightPanel();
    this.setGlassPane(jPanelSpotlight);

    // Configure filechooser
    getFileChooser().setAcceptAllFileFilterUsed(false);
    getFileChooser().setFileSelectionMode(JFileChooser.FILES_ONLY);
    getFileChooser().setFileFilter(new JFileChooserFilter());
   
    // Configure ressources
    varieties.setTitle(Messages.getMessage(Messages.ressourcesVarietyFrame));
    varieties.setFrameIcon(new ImageIcon(PlantsAdd.class.getResource("/net/sf/cannagrower/images/data/plants_16.png")));
  }

  private void launchDemo(){
    // Opening culture demo wizard
    if(cultureDemo==null){cultureDemo=new FrameTutorial();}
    cultureDemo.open();
    cultureDemo.setVisible(true);
  }

  /**
   * This method close the active culture
   *
   * @return void
   */
  private void menuFileClose() {
    if(jDesktopPane.getSelectedFrame()==null){return;}
    jDesktopPane.getSelectedFrame().doDefaultCloseAction();
  }

  /**
   * This method try to save active culture and then close the application
   *
   * @return void
   */
  private void menuFileExit() {
    if (!close()) return;
    System.exit(0);
  }

  /**
   * This method create a new culture and show it
   *
   * @return void
   */
  private void menuFileNew() {
    try{
      Culture culture = Culture.create(Culture.typeEmpty);
      open(culture,null);
    }catch(IOException e){
      Messages.showException(e);
    }catch(ClassNotFoundException e){
      Messages.showException(e);
    }

  }

  /**
   * This method try to close active culture and then ask user to open a new culture
   *
   * @return void
   */
  private void menuFileOpen() {
    // Ask for cultureFile
    if (getFileChooser().showOpenDialog(this) != JFileChooser.APPROVE_OPTION) {
      return;
    }

    // Loading
    open(null,getFileChooser().getSelectedFile());
  }

  /**
   * This method save the active culture
   *
   * @return void
   */
  private void menuFileSave() {
    if(jDesktopPane.getSelectedFrame()==null){return;}
    if(jDesktopPane.getSelectedFrame().getClass().equals(FrameCulture.class)){
      FrameCulture cultureViewer=(FrameCulture)jDesktopPane.getSelectedFrame();
      cultureViewer.save();
    }
  }

  private void menuFileSettings(){
    CannaGrowerProperties.getProperties().setShowSettings(true);
    JOptionPane.showMessageDialog(this, Messages.getMessage(Messages.messageSettings),getTitle(),JOptionPane.INFORMATION_MESSAGE);
  }

  private void menuHelpAbout() {
    uiShow(about);
  }

  private void menuHelpDemo(){
    launchDemo();
  }

  private void menuHelpManual(){
    try{
      Desktop.getDesktop().browse(new URI(webManual));
    }catch(IOException e){
      Messages.showException(e);
    }catch(URISyntaxException e){
      Messages.showException(e);
    }catch(UnsupportedOperationException e){
      Messages.showException(e);
    }

  }

  private void menuResourcesVarieties(){
    uiShow(varieties);
  }
  private void menuWindow(){
    getJMenuWindow().removeAll();
    for(JInternalFrame frame:getJDesktopPane().getAllFrames()){
      JMenuItem jMenuItem;
      if(frame.isVisible()){
        jMenuItem=new JMenuItem();
        if(frame.isSelected()){jMenuItem.setFont(jMenuItem.getFont().deriveFont(Font.BOLD));}
        jMenuItem.setText(frame.getTitle());
        jMenuItem.setIcon(frame.getFrameIcon());
        jMenuItem.addActionListener(new JMenuWindowActionListener(frame));

        getJMenuWindow().add(jMenuItem);
      }
    }
  }

  /**
   * This method open the culture viewer for active culture
   *
   * @return void
   */
  public void open(Culture culture,File cultureFile){
    String name;
    TaskOpen open=new TaskOpen(this,culture,cultureFile);

    if(cultureFile!=null){
      name=cultureFile.getName();
    }else{
      name=culture.getName();
    }

    status.setStatus(Messages.getMessage(Messages.statusTitle),Messages.getMessage(Messages.statusOpening,name));

    open.start();
  }

  FrameCulture uiOpen(FrameCulture cultureFrame) {
    status.setStatus();
    this.cultureActive = cultureFrame;
    jDesktopPane.add(cultureActive);
    cultureActive.setVisible(true);
    try{cultureActive.setMaximum(true);}
    catch(java.beans.PropertyVetoException e){}
    cultureActive.hardwareShowFirst();
    cultureActive.moveToFront();
    return cultureActive;
  }
 
  void uiShow(JInternalFrame frame){
    frame.setVisible(true);
    try{frame.setSelected(true);}
    catch(java.beans.PropertyVetoException e){}
  }
}

/**
* This class filter CannGrower file when using file chooser.
* @author alois_cochard@users.sf.net
*
*/
class JFileChooserFilter extends FileFilter {
  public boolean accept(File file) {
    if (file.isDirectory()) {
      return true;
    }
    return file.getName().endsWith(CannaGrower.extension);
  }

  public String getDescription() {
    return Messages.getMessage(Messages.cultureFileFilter);
  }
}

class JMenuWindowActionListener implements java.awt.event.ActionListener{
  JInternalFrame jInternalFrame;

  JMenuWindowActionListener(JInternalFrame jInternalFrame){
    this.jInternalFrame=jInternalFrame;
  }

  public void actionPerformed(java.awt.event.ActionEvent e) {
    try{
      jInternalFrame.setSelected(true);
      if(jInternalFrame.isIcon()){jInternalFrame.setIcon(false);}
    }
    catch(java.beans.PropertyVetoException ex){}
  }
}

class TaskOpen extends Thread{
  CannaGrower      cannagrower;
  Culture       culture;
  File         cultureFile;
  FrameCulture    cultureFrame;

  TaskOpen(CannaGrower cannagrower,Culture culture,File cultureFile){
    this.cannagrower=cannagrower;
    this.culture=culture;
    this.cultureFile=cultureFile;
  }

  public void run(){
    if (culture == null) {
      if (cultureFile == null) {
        return;
      }
      try {
        culture = Culture.read(cultureFile);
      } catch (IOException e) {
        Messages.showException(e);
      }catch(ClassNotFoundException e){
        Messages.showException(e);
      }
    }

    cultureFrame = new FrameCulture(CannaGrower.cannagrower, culture,cultureFile);

    SwingUtilities.invokeLater(new Runnable(){
      public void run() {
        cannagrower.uiOpen(cultureFrame);
      }
    });
  }
}
TOP

Related Classes of net.sf.cannagrower.CannaGrower

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.