/*
 * Created on Apr 20, 2003
 *
 * To change this generated comment go to 
 * Window>Preferences>Java>Code Generation>Code Template
 */
package jdraw.figures;

import java.awt.event.*;
import java.awt.*;
import java.util.*;
import javax.swing.*;

import jdraw.framework.*;

/**
 * The AbstractTool supports:
 * - Color
 * - filled or outline mode
 * - xy constraints, i.e. the Figure can only be created with 
 *   the same width and height.
 * @author Christoph
 */
public abstract class AbstractTool implements DrawTool {
	
	private Color color = Color.BLACK;
	private boolean filled = false;
	private boolean square = false;
	
	private final DrawEditor editor;
	private final DrawView view;
	
	private JMenu menu = new JMenu("Attributes");
	private JMenuItem colorItem = new JMenuItem("Color...");
	private JCheckBoxMenuItem filledItem = new JCheckBoxMenuItem("Filled", false);
	private JCheckBoxMenuItem squareItem = new JCheckBoxMenuItem("Square", false);
		
	
	public AbstractTool(DrawEditor editor) {
		this.editor = editor;
		this.view = editor.getView();
		
		colorItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Color c = JColorChooser.showDialog((JComponent)e.getSource(),
																					 "Choose Color...",
																					 color);
				if (c != null) color = c;
			}
		});
		
		filledItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				filled = filledItem.isSelected();
			}
		});

		squareItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				square = squareItem.isSelected();
			}
		});
		
	}
	
	protected void showMenu(boolean color, boolean fill,
													boolean square, Collection menuItems) {
		menu.removeAll();
		if (color) menu.add(colorItem);
		if (fill) menu.add(filledItem);
		if (square) menu.add(squareItem);
			
		if (menuItems != null && !menuItems.isEmpty()) {
			if (menu.getItemCount() > 0 ) menu.addSeparator();
			Iterator it = menuItems.iterator();
			while (it.hasNext()) {
				Object o = it.next();
				if (o instanceof JMenuItem) {
					menu.add((JMenuItem)o);
				}
			}
		}
		editor.addMenu(menu);
	}

	protected void showMenu(Collection menuItems) {
		showMenu(true, true, true, menuItems);
	}
	
	protected void hideMenu() {
		editor.removeMenu(menu);
	}
	
	protected DrawView getView() {
		return view;
	}
	
	protected DrawEditor getEditor() {
		return editor;
	}
	
	protected Color getCurrentColor() {
		return color;
	}
	
	protected boolean isFilled() {
		return filled;
	}
	
	protected boolean isSquareLocked() {
		return square;
	}
}
