package jas2.swingstudio;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import jas2.util.*;
import jas2.util.layout.*;
import jas2.util.xmlmenus.*;

public class JASToolbarHolder extends JPanel {
	public JASToolbarHolder(jas2.util.xmlmenus.XMLMenuBuilder xmb) {
		super();
		m_applicationToolBar = xmb.getToolBar("applicationToolBar");
		m_jobToolBar = (JToolBar)xmb.getToolBar("jobToolBar");
		m_codeToolBar = xmb.getToolBar("codeToolBar");
		m_webToolBar = xmb.getToolBar("webToolBar");
		m_rebinSlider = xmb.getToolBar("rebinSlider");
		addIfVisible(m_applicationToolBar);
		addIfVisible(m_jobToolBar);
		addIfVisible(m_codeToolBar);
		addIfVisible(m_webToolBar);
		addIfVisible(m_rebinSlider);
		theToolBarPopupMenu = JavaAnalysisStudio.getApp().getXMLMenuBuilder().getPopupMenu("toolbarPopupMenu");
		JavaAnalysisStudio.getApp().getCommandManager().add(m_command);
		setLayout(new ToolbarLayout(ToolbarLayout.LEFT,5,5));
		enableEvents(AWTEvent.MOUSE_EVENT_MASK);
	}
	public Component add(Component comp) {
		setToolbarCheckBoxMenuEnabled(comp, true, false);
		return super.add(comp);
	}	
	public Component add(Component comp, int index) {
		setToolbarCheckBoxMenuEnabled(comp, true, false);
		return super.add(comp, index);
	}	
	public void add(Component comp, Object constraints) {
		setToolbarCheckBoxMenuEnabled(comp, true, false);
		super.add(comp, constraints);
	}
	public void add(Component comp, Object constraints, int index) {
		setToolbarCheckBoxMenuEnabled(comp, true, false);
		super.add(comp, constraints, index);
	}
	public Component add(String name, Component comp) {
		setToolbarCheckBoxMenuEnabled(comp, true, false);
		return super.add(name, comp);
	}	
	public void remove(Component comp) {
		super.remove(comp);
		setToolbarCheckBoxMenuEnabled(comp, false, true);
	}
	protected void processMouseEvent(final MouseEvent e) {
		super.processMouseEvent(e);
		if (e.isPopupTrigger()) {
			theToolBarPopupMenu.show(e.getComponent(), e.getX(), e.getY());
		}
	}
	private void addIfVisible(JToolBar t) {
		if (t.getName().equals("applicationToolBar")) {
			if (getApplicationToolbarVisible()) {
				add(m_applicationToolBar);
			}
		} else if (t.getName().equals("jobToolBar")) {
			if (getJobToolbarVisible()) {
				add(m_jobToolBar);
			}
		} else if (t.getName().equals("codeToolBar")) {
			if (getCodeToolbarVisible()) {
				add(m_codeToolBar);
			}
		} else if (t.getName().equals("webToolBar")) {
			if (getWebToolbarVisible()) {
				add(m_webToolBar);
			}
		} else if (t.getName().equals("rebinSlider")) {
			if (getRebinSliderVisible()) {
				add(m_rebinSlider);
			}
		} else {
		}
	}
	private void changeMenu(int index, boolean enabled, boolean calledByRemove) {
		if (calledByRemove) {
			JMenu toolbarmenu = (JMenu)(JavaAnalysisStudio.getApp().getMenuBar().getMenu(VIEW_MENU).getPopupMenu().getComponent(TOOLBAR_MENU));
			((JASCheckboxMenuItem)toolbarmenu.getItem(index)).setCorrespondingToolBarFloating(true);
		} else {
			JMenu toolbarmenu = (JMenu)(JavaAnalysisStudio.getApp().getMenuBar().getMenu(VIEW_MENU).getPopupMenu().getComponent(TOOLBAR_MENU));
			((JASCheckboxMenuItem)toolbarmenu.getItem(index)).setCorrespondingToolBarFloating(false);
			//((JASCheckboxMenuItem)(JavaAnalysisStudio.getApp().getMenuBar().getMenu(VIEW_MENU).getPopupMenu().getComponent(index))).setCorrespondingToolBarFloating(false);
		}
		JavaAnalysisStudio.getApp().getMenuBar().getMenu(VIEW_MENU).getPopupMenu().getComponent(index).setEnabled(enabled);
		if (theToolBarPopupMenu != null) {
			//theToolBarPopupMenu == null until it is first displayed
			theToolBarPopupMenu.getComponent(index).setEnabled(enabled);
		}
	}
	private void setToolbarCheckBoxMenuEnabled(Component comp, boolean enabled, boolean calledByRemove) {
		//This method is a hack (see the comment in setAToolbarVisible) so it is coded as such.
		String name = comp.getName();
		if (!doNotTouchCheckBoxMenu) {
			//This flag is true if the method that calls remove() is invoked by the user
			//selecting a checkbox, in which case we don't want to disable the checkbox menu
			//item or it would be REALLY hard to make the toolbar visible again.
			//So if we get in here from remove then remove() was called by the ToolBar becoming
			//floating, so we want to disable the menu item and set the floating flag in
			//JASCheckboxMenuItem.  Note that we do NOT want to set this flag if we are calling this
			//method from add(), otherwise all the checkboxes would start out disabled.

			if (name.equals("applicationToolBar")) {
				changeMenu(0, enabled, calledByRemove);
			} else if (name.equals("jobToolBar")) {
				changeMenu(1, enabled, calledByRemove);
			} else if (name.equals("codeToolBar")) {
				changeMenu(2, enabled, calledByRemove);
			} else if (name.equals("webToolBar")) {
				changeMenu(3, enabled, calledByRemove);
			} else if (name.equals("rebinSlider")) {
				changeMenu(4, enabled, calledByRemove);
			} else {
				System.out.println("ERROR: unknown toolbar");
			}
		}
	}
	private void setAToolbarVisible(JToolBar theToolBar, String theProp, boolean visible) {
		if (visible) {
			add(theToolBar);
		} else {
			//When we move to JDK 1.2, we can call
			//JavaAnalysisStudio.getApp().getFrame().getOwnedWindows()
			//and iterate through the returned Window array to get a handle to the child toolbar
			//window, which we can then close in order to dock the toolbar.  This docked toolbar
			//can then be hidden by calling remove(theToolBar).  Until then, however, we will settle
			//for (in the remove() method) disabling the option to hide the toolbar in the
			//View menu while the toolbar is floating.  Since the remove method is also called 
			//to actually remove the toolbar (and hide it, not float it) we need to re-enable
			//the menu item after calling remove when we actually do want to remove it.
			doNotTouchCheckBoxMenu = true;//we can't pass this in as a parameter to remove since
										  //we need to override the base class remove()
			remove(theToolBar);
			doNotTouchCheckBoxMenu = false;
		}
		userProp.setBoolean(theProp,visible);
		revalidate();
		repaint();
	}
	private boolean doNotTouchCheckBoxMenu = false;
	public void setApplicationToolbarVisible(boolean visible) {
		setAToolbarVisible(m_applicationToolBar, applicationToolbarProp, visible);
	}
	public void setJobToolbarVisible(boolean visible) {
		setAToolbarVisible(m_jobToolBar, jobToolbarProp, visible);
	}
	public void setCodeToolbarVisible(boolean visible) {
		setAToolbarVisible(m_codeToolBar, codeToolbarProp, visible);
	}
	public void setWebToolbarVisible(boolean visible) {
		setAToolbarVisible(m_webToolBar, webToolbarProp, visible);
	}
	public void setRebinSliderVisible(boolean visible) {
		setAToolbarVisible(m_rebinSlider, rebinSliderProp, visible);
	}
	public boolean getApplicationToolbarVisible() {
		return userProp.getBoolean(applicationToolbarProp,true);
	}
	private boolean getJobToolbarVisible() {
		return userProp.getBoolean(jobToolbarProp,true);
	}
	private boolean getCodeToolbarVisible() {
		return userProp.getBoolean(codeToolbarProp,true);
	}
	private boolean getWebToolbarVisible() {
		return userProp.getBoolean(webToolbarProp,true);
	}
	private boolean getRebinSliderVisible() {
		return userProp.getBoolean(rebinSliderProp,true);
	}
	JComboBox getEventSourceComboBox() {
		return ((JobToolBar)m_jobToolBar).getEventSourceComboBox();
	}
	void setRebinModel(BoundedRangeModel xModel, BoundedRangeModel yModel) 
	{
		((RebinSlider)m_rebinSlider).setModel(xModel,yModel);
	}
	private JToolBar m_applicationToolBar;
	private JToolBar m_codeToolBar;
	private JToolBar m_jobToolBar;
	private JToolBar m_webToolBar;
	private JToolBar m_rebinSlider;
	private ToolbarCommand m_command = new ToolbarCommand();	
	private static final int VIEW_MENU = 4;//hack for disabling checkboxes
	private static final int TOOLBAR_MENU =0;
		
	private class ToolbarCommand extends JASCommandProcessor {
		public void onApplicationToolbar(boolean b) {
			setApplicationToolbarVisible(b);
			setChanged();
		}
		public void onJobToolbar(boolean b) {
			setJobToolbarVisible(b);
			setChanged();
		}
		public void onCodeToolbar(boolean b) {
			setCodeToolbarVisible(b);
			setChanged();
		}
		public void onWebToolbar(boolean b) {
			setWebToolbarVisible(b);
			setChanged();
		}
		public void onRebinSlider(boolean b) {
			setRebinSliderVisible(b);
			setChanged();
		}
		public void enableApplicationToolbar(JASCheckboxState state) {
			state.setCheckbox(getApplicationToolbarVisible());
			state.setEnabled(true);
		}
		public void enableJobToolbar(JASCheckboxState state) {
			state.setCheckbox(getJobToolbarVisible());
			state.setEnabled(true);
		}
		public void enableCodeToolbar(JASCheckboxState state) {
			state.setCheckbox(getCodeToolbarVisible());
			state.setEnabled(true);
		}
		public void enableWebToolbar(JASCheckboxState state) {
			state.setCheckbox(getWebToolbarVisible());
			state.setEnabled(true);
		}
		public void enableRebinSlider(JASCheckboxState state) {
			state.setCheckbox(getRebinSliderVisible());
			state.setEnabled(true);
		}
		public void onLargeIcons(boolean b) {
			largeIcons = b;
			setChanged();
		}
		public void onShowText(boolean b) {
			showText = b;
			setChanged();
		}
		public void enableLargeIcons(JASCheckboxState state) {
			state.setCheckbox(largeIcons);
			state.setEnabled(true);
		}
		public void enableShowText(JASCheckboxState state) {
			state.setCheckbox(showText);
			state.setEnabled(true);
		}
	}
	private boolean largeIcons = false;
	private boolean showText = false;
	private JPopupMenu theToolBarPopupMenu;
	private UserProperties userProp = Application.getApplication().getUserProperties();
	private final static String applicationToolbarProp = "ApplicationToolbarVisible";
	private final static String jobToolbarProp = "JobToolbarVisible";
	private final static String codeToolbarProp = "CodeToolbarVisible";
	private final static String webToolbarProp = "WebToolbarVisible";
	private final static String rebinSliderProp = "RebinSliderVisible";
}

 

Ads help maintain this website.