Package gui

Source Code of gui.DocumentWindow

package gui;

import java.awt.*;
import java.awt.event.*;
import java.io.File;
import javax.swing.*;
import main.TikzFile;
import tikzparser.SyntaxTikzPicture;


/**
* GUI-frontend of the MieZ program.
*/
public class DocumentWindow extends JFrame {
 
  ////////////////////////////////
  // STATICS
  ////////////////////////////////
 
  private static final String PICCHOICE_TITLE
    = "Please choose one of the following pictures";

  private static final String PICCHOICE_MESSAGE
    = "The selected file contains multiple TikZ pictures.\n"
    + "Please select the picture you would like to edit:";
 
  /**
   * Window's standard size
   */
  private static final Dimension preferredSize = new Dimension(800,600);
 
 
  /**
   * FileChooser for the 'open'-dialog
   */
  private JFileChooser fileChooser;
 
 
  /**
   * Window's tool bar
   */
  private MiezToolBar toolBar;
 
 
  /**
   * Window's editor panel (everything below tool bar)
   */
  private EditorPanel ep;
 

  /**
   * Window's title 
   */
  private String title;
 
 
 
  /**
   * Constructs a new MieZ main window.
   *
   * @param title
   *   Window's title.
   */
  public DocumentWindow(String title, final DocumentWindowListener guiListener) {
    super(title);
    this.title = title;
   
   
    // Register a WindowListener
    setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent we) {
        guiListener.onCloseWindow();
      }
    });
   
   
    fileChooser = new JFileChooser();
   
    // Set the menu bar
    MiezMenu menu = new MiezMenu(guiListener);
    setJMenuBar(menu);
   
    // Set the tool bar
    toolBar = new MiezToolBar(guiListener);
    toolBar.buttonSave.setEnabled(false);
    getContentPane().add(toolBar, BorderLayout.PAGE_START);
   
    // Set the editor panel
    ep = new EditorPanel();
    getContentPane().add(ep, BorderLayout.CENTER);
   
    // Register Listeners
    ep.dp.addMouseMotionListener(toolBar.statusPanel);
    ep.dp.addMouseMotionListener(ep.columnView);
    ep.dp.addMouseMotionListener(ep.rowView);
   
    // Set proper size
    setSize(preferredSize);
  }
 
 
  /**
   * Shows the file chooser dialog.
   *
   * @return
   *   Chosen file.
   */
  public File showOpenFileDialog() {
    File f;
    fileChooser.showOpenDialog(this);
    f = fileChooser.getSelectedFile();
    fileChooser.setSelectedFile(null);
    return f;
  }
 
 
  /**
   * Show the TikZ picture chooser dialog, i.e. MieZ found
   * several TikZ pictures in one file.
   *
   * @param file
   *   Source file
   *
   * @return
   *   Chosen TikZ picture
   */
  public SyntaxTikzPicture choosePicture(TikzFile file) {
    SyntaxTikzPicture[] pics = file.getPictures();
    SyntaxTikzPicture pic = pics[0];
   
    if(file.getPictures().length > 1) {
      /*
       * Generate ComboBox values
       */
      String[] values = new String[pics.length];
      for(int i=0; i<values.length; i++) {
        values[i] = "Picture #" +(i+1)+ " (" + pics[i].toString().length() + " characters)";
      }
     
     
      /*
       * Show InputDialog
       */
      Object result = JOptionPane.showInputDialog(
        this,
        PICCHOICE_MESSAGE,
        PICCHOICE_TITLE,
        JOptionPane.QUESTION_MESSAGE,
        null,
        values,
        values[0]
      );
     
     
      /*
       * Detect selected picture
       */
      for(int i=0; i<values.length; i++) {
        if(values[i] == result) {
          pic = pics[i];
        }
      }
    }
   
    return pic;
  }
 
 
  /**
   * Displays a picture.
   *
   * @param pic
   *   Picture to be displayed.
   */
  public void setPicture(SyntaxTikzPicture pic) {
    ep.dp.createFigures(pic.getPicture());
  }
 
 
  /**
   *
   */
  public void showStateChanged(boolean stateChanged) {
    toolBar.setSavable(stateChanged);
   
    if(stateChanged) {
      setTitle("*"+title);
    } else {
      setTitle(title);
    }
  }
 
 
  /**
   * Returns the window's EditorPanel instance.
   * @return
   */
  public EditorPanel getEditorPanel() {
    return ep;
  }

}
TOP

Related Classes of gui.DocumentWindow

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.