/** @(#) JavaPaintS.hopjava 1.1 99/11/18
 * Copyright (c) 1999 Lawrence Rodrigues
 */
package app;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import java.beans.*;

import java.awt.print.*;
import com.vistech.shapes.*;
import com.vistech.graphics.*;
import com.vistech.util.*;

/** An application that lets users to draw a variety of shapes and perform operations
* such as move, fill, copy and paste on them.
* @version 1.0  18 Nov 1999
* @author Lawrence Rodrigues
**/
public class JavaPaintShop extends
    com.vistech.util.ApplicationFrame implements PropertyChangeListener{
    protected GraphicsCanvas2D  grCanvas;
    protected JTextArea messageBar;
    protected DrawShapes drawShapes;
    protected CommandSelectionPanel commandSelectionPanel;
    protected ShapeSelectionPanel shapeSelectionPanel;
    protected ParamsSelectionPanel paramPanel;

    protected JFrame paramFrame;

    public static void  main(String[] args){
       JavaPaintShop iv = new JavaPaintShop();
    }
    public JavaPaintShop() {
       addWindowListener(
          new WindowAdapter() {
             public void windowClosing(WindowEvent w){
                JavaPaintShop.this.dispose();
                System.exit(0);
            }
          });
      setTitle("JavaPaintShop ");
    }

    public void quit(){ this.dispose();}

    protected void createGUI(){
       grCanvas = new GraphicsCanvas2D();
       drawShapes = new DrawShapes(grCanvas);
       //To update message bar
       drawShapes.addPropertyChangeListener(this);

       grCanvas.addMouseListener(drawShapes);
       grCanvas.addMouseMotionListener(drawShapes);
       grCanvas.addKeyListener(drawShapes);

       commandSelectionPanel = new CommandSelectionPanel(drawShapes);
       grCanvas.addMouseListener(commandSelectionPanel);

       shapeSelectionPanel = new ShapeSelectionPanel(drawShapes);

       ViewerMenuBar viewerMenuBar = new ViewerMenuBar();
       setJMenuBar((JMenuBar)viewerMenuBar);
       Dimension ssize= Toolkit.getDefaultToolkit().getScreenSize();
       int vwid = (int)(ssize.width*3/4.0);
       int vht = (int)(ssize.height*3/4.0);
       this.setLocation((ssize.width/4),
                         (ssize.height/4));
       this.setSize((int)(ssize.width*3/4.0), (int)(ssize.height*3/4.0));
       messageBar = new JTextArea();
       messageBar.setBorder(BorderFactory.createTitledBorder("Messages"));
       messageBar.setBackground(Color.black);
       messageBar.setForeground(Color.green);
       this.updateMessageBar();

       GridBagLayout gb = new GridBagLayout() ;
       GridBagConstraints c = new GridBagConstraints();
       this.getContentPane().setLayout(gb);
       c.fill = GridBagConstraints.BOTH;
       c.weighty = 1.0; c.weightx = 1.0;
       c.gridheight =1;
       c.gridwidth = 1;
       c.gridwidth = GridBagConstraints.REMAINDER;

       gb.setConstraints(grCanvas, c);
       this.getContentPane().add(grCanvas);
       c.fill = GridBagConstraints.BOTH;
       c.weighty = 0.05; c.weightx = 1.0;
       gb.setConstraints(messageBar, c);
       this.getContentPane().add(messageBar);

       this.show();
       this.repaint();
   }

   public void propertyChange(PropertyChangeEvent p){
      updateMessageBar();
   }

   public void updateMessageBar(){
      if(grCanvas == null)return;
        messageBar.setBackground(Color.black);
        messageBar.setText("Current Shape Type : "+
        ShapeFactory.getTypeAsString(drawShapes.getShapeType())+ "\n"+
                           "Current Command : " + drawShapes.getCommandAsString());

        messageBar.repaint();
        repaint();
   }


   public class ViewerMenuBar extends com.vistech.util.ApplicationFrame.AppMenuBar {
       public ViewerMenuBar(){
          add(createGraphicsParamsMenu());
          add(shapeSelectionPanel.createShapeTypesMenu());
          createHelpMenu();
       }

       public JMenu createGraphicsParamsMenu(){
          JMenu selMenu = new JMenu("Params");
          JMenuItem ls = new JMenuItem(" Launch Params Panel");
          ls.addActionListener(
              new ActionListener() {
                public void actionPerformed(ActionEvent e){
                   if(paramFrame != null) paramFrame.dispose();
                   paramFrame = new JFrame();
                   paramPanel = new ParamsSelectionPanel(grCanvas);
                   paramFrame.getContentPane().add(paramPanel);
                   paramFrame.pack();
                   paramFrame.show();

                }
              }
           );
           selMenu.add(ls);
           return selMenu;
       }
  }
}

