// created: 30.12.00
// modified:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class CommandToolbar extends JToolBar implements ActionListener
 {

 JButton parseButton; 
 JButton orderButton;
 JButton quitButton;

 PagePrefGUI theGUI;

 CommandToolbar(PagePrefGUI GUI)
  {
  theGUI = GUI; 

  parseButton = new JButton("parse");
  parseButton.setActionCommand("parse");
  parseButton.addActionListener(this);
  orderButton = new JButton("order");
  orderButton.setActionCommand("order");
  orderButton.addActionListener(this);
  quitButton = new JButton("quit");
  quitButton.setActionCommand("quit");
  quitButton.addActionListener(this);
   
  setFloatable(false);
  setBorder(BorderFactory.createTitledBorder("commands"));

  add(parseButton);
  add(orderButton);
  add(quitButton);
  } 

 /**************************/
 /* method actionPerformed */
 /**************************/

 public void actionPerformed(java.awt.event.ActionEvent e) 
  {
  if (e.getActionCommand().equals("parse"))
   {
   // disable relevant panels and components
   enableComponents(false);  

   // get the pages
   theGUI.pagePreferencer.setPages(theGUI.pagePanel.getPages());

   // parse the pages
   theGUI.pagePreferencer.parsePages();

   // display the page properties
   theGUI.pagePanel.displayPages();

   // enable relevant panels and components
   enableComponents(true);
   }
  else if (e.getActionCommand().equals("order")) 
   {
   // disable relevant panels and components
   enableComponents(false);  

   // get the pages
   theGUI.pagePreferencer.setPages(theGUI.pagePanel.getPages());

   // set the value functions
   ValueFunction [] funcs = theGUI.funcPanel.getFunctions();
   theGUI.pagePreferencer.setValueFunction("text",funcs[0]);
   theGUI.pagePreferencer.setValueFunction("images",funcs[1]);
   theGUI.pagePreferencer.setValueFunction("tables",funcs[2]);
   theGUI.pagePreferencer.setValueFunction("links",funcs[3]);
   theGUI.pagePreferencer.setValueFunction("downloads",funcs[4]);
   theGUI.pagePreferencer.setValueFunction("frames",funcs[5]);
   theGUI.pagePreferencer.setValueFunction("extension",funcs[6]);

   // order the pages.
   theGUI.pagePreferencer.orderPages(theGUI.wgtPanel.getWeights());

   // set the pages on display
   theGUI.pagePanel.setPages(theGUI.pagePreferencer.getPages());

   // enable buttons and panels
   enableComponents(true);
   }
  else if (e.getActionCommand().equals("quit"))  
   {
   System.exit(0);
   }
  }

 /***************************/
 /* method enableComponents */
 /***************************/

 void enableComponents(boolean bool)
  {
  theGUI.attPanel.setEnabled(true);
  if (bool)
   {
   if (theGUI.attPanel.textSwitch.theSwitch.isSelected())
    {
    theGUI.wgtPanel.textChanger.setEnabled(true);
    theGUI.funcPanel.textChanger.setEnabled(true);
    }
   if (theGUI.attPanel.imagesSwitch.theSwitch.isSelected())
    {
    theGUI.wgtPanel.imagesChanger.setEnabled(true);
    theGUI.funcPanel.imagesChanger.setEnabled(true);
    }
   if (theGUI.attPanel.tablesSwitch.theSwitch.isSelected())
    {
    theGUI.wgtPanel.tablesChanger.setEnabled(true);
    theGUI.funcPanel.tablesChanger.setEnabled(true);
    }
   if (theGUI.attPanel.linksSwitch.theSwitch.isSelected())
    {
    theGUI.wgtPanel.linksChanger.setEnabled(true);
    theGUI.funcPanel.linksChanger.setEnabled(true);
    }
   if (theGUI.attPanel.downloadsSwitch.theSwitch.isSelected())
    {
    theGUI.wgtPanel.downloadsChanger.setEnabled(true);
    theGUI.funcPanel.downloadsChanger.setEnabled(true);
    }
   if (theGUI.attPanel.framesSwitch.theSwitch.isSelected())
    {
    theGUI.wgtPanel.framesChanger.setEnabled(true);
    theGUI.funcPanel.framesChanger.setEnabled(true);
    }
   if (theGUI.attPanel.extensionSwitch.theSwitch.isSelected())
    {
    theGUI.wgtPanel.extensionChanger.setEnabled(true);
    theGUI.funcPanel.extensionChanger.setEnabled(true);
    }
   theGUI.pagePanel.setEnabled(true);       
   orderButton.setEnabled(true);
   parseButton.setEnabled(true);
   }
  else
   {
   parseButton.setEnabled(false);
   orderButton.setEnabled(false);
   theGUI.attPanel.setEnabled(false);
   theGUI.wgtPanel.setEnabled(false);
   theGUI.funcPanel.setEnabled(false);
   theGUI.pagePanel.setEnabled(false);      
   }
  }

 }