Package periman

Source Code of periman.NoDocumentState

package periman;

import java.awt.event.WindowEvent;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;



public class NoDocumentState extends DocumentState {
 
  private MainWindow mainWindow;
  NoDocumentState(MainWindow mw)
  {
    mainWindow = mw;
    mainWindow.setTitle("Periman");
    mainWindow.enableCloseMenuItem(false);
    mainWindow.enableSaveMenuItem(false);
    mainWindow.enableSaveAsMenuItem(false);
   
    mainWindow.enableOpenMenuItem(true);
    mainWindow.enableNewMenuItem(true);
    mainWindow.setThingsEnabled(false);
  }


  @Override
  void close() {
    JOptionPane.showMessageDialog( mainWindow, "Should be impossible");

  }

  @Override
  void exit() {
    mainWindow.windowClosing(new WindowEvent(mainWindow, WindowEvent.WINDOW_CLOSING));
  }

  @Override
  /**
   * set working directory from user choice.
   * create a new Configuration
   * change state to "already opened document state"
   */
  void newDoc() {
    JFileChooser chooser = new JFileChooser();
    chooser.setDialogTitle("Choose a Working Directoty.");
    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    if (chooser.showOpenDialog(mainWindow) != JFileChooser.APPROVE_OPTION)
    {
      return;
    }
   
    mainWindow.setWorkingDir( chooser.getSelectedFile().getPath() );
    mainWindow.setConfigurationFileName("conf0.periman");
    mainWindow.setConf( new Configuration("conf0.periman"));
    mainWindow.setLastSavedConfToNew();
   
    mainWindow.changeDocState( new OpenDocumentState(mainWindow) );   
  }

  @Override
  /**
   * let user choose the configuration file;
   * update the working directory accordingly
   * change state to "already opened document state"
   */
  void open() {
    JFileChooser chooser = new JFileChooser();
    chooser.setDialogTitle("Choose a Configuration file to load.");
    chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    FileNameExtensionFilter filter = new FileNameExtensionFilter(
            "Periman Configuration files", "periman");
    chooser.setFileFilter(filter);

    if (chooser.showOpenDialog(mainWindow) != JFileChooser.APPROVE_OPTION)
    {
      return;
    }
     
    String filePath = chooser.getSelectedFile().getPath();
    Configuration retConf =
          Configuration.load(filePath);

     
     
    if(retConf != null)
    {
      mainWindow.setConf( retConf );
      mainWindow.setLastSavedConfToOpened();
     
      //! set the title of the window
      mainWindow.setConfigurationFileName( filePath );
     
      String s1 = chooser.getSelectedFile().getParent();
      mainWindow.setWorkingDir(s1);

   
      mainWindow.changeDocState( new OpenDocumentState(mainWindow) );

     
    }
   
  }

  @Override
  void save() {
    JOptionPane.showMessageDialog( mainWindow, "Should be impossible");

  }
 
  @Override
  void saveAs() {
    JOptionPane.showMessageDialog( mainWindow, "Should be impossible");

  }

}
TOP

Related Classes of periman.NoDocumentState

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.