/*
 * JaamSim Discrete Event Simulation
 * Copyright (C) 2005-2013 Ausenco Engineering Canada Inc.
 * Copyright (C) 2016-2020 JaamSim Software Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.jaamsim.ui;

import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.util.ArrayList;

import javax.swing.JColorChooser;
import javax.swing.JDialog;

import com.jaamsim.input.ColourInput;
import com.jaamsim.math.Color4d;
import com.jaamsim.ui.EditBox.EditTable;

/**
 * Handles colour inputs.
 *
 */
public class ColorEditor extends CellEditor {

	private static JColorChooser colorChooser;

	public static final String DIALOG_NAME = "Colour Chooser";
	public static final String OPTION_COLOUR_CHOOSER = String.format("*** %s ***", DIALOG_NAME);

	public ColorEditor(EditTable table) {
		super(table, true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if("button".equals(e.getActionCommand())) {

			// Present colour
			Color4d col = (Color4d) input.getValue();
			ArrayList<Color4d> coloursInUse = new ArrayList<>(1);
			if (col != null)
				coloursInUse.add(col);
			ColourMenu menu = new ColourMenu(col, coloursInUse, false) {

				@Override
				public void setColour(String colStr) {
					setValue(colStr);
					stopCellEditing();
					propTable.requestFocusInWindow();
				}

			};
			Component button = (Component)e.getSource();
			Component panel = button.getParent();
			menu.show(panel, 0, panel.getHeight());
			return;
		}
	}

	public static JColorChooser getColorChooser() {
		if (colorChooser == null)
			colorChooser = new JColorChooser();
		return colorChooser;
	}

	public void launchDialog() {
		JColorChooser chooser = getColorChooser();
		JDialog dialog = JColorChooser.createDialog(null,
				DIALOG_NAME,
				true,  //modal
				chooser,
				this,  //OK button listener
				null); //no CANCEL button listener
		dialog.setIconImage(GUIFrame.getWindowIcon());
		dialog.setAlwaysOnTop(true);

		Color4d col = ((ColourInput)input).getValue();
		chooser.setColor(new Color((float)col.r, (float)col.g, (float)col.b, (float)col.a));
		dialog.setVisible(true);

		// Apply editing
		stopCellEditing();

		// Focus the cell
		propTable.requestFocusInWindow();
	}

}

@@ -732,31 +732,8 @@
 i;%0A%0A
-import java.awt.Color;%0A
 impo
@@ -857,74 +857,8 @@
 er;%0A
-import javax.swing.JDialog;%0A%0Aimport com.jaamsim.input.ColourInput;
 %0Aimp
@@ -2071,599 +2071,6 @@
 %09%7D%0A%0A
-%09public void launchDialog() %7B%0A%09%09JColorChooser chooser = getColorChooser();%0A%09%09JDialog dialog = JColorChooser.createDialog(null,%0A%09%09%09%09DIALOG_NAME,%0A%09%09%09%09true,  //modal%0A%09%09%09%09chooser,%0A%09%09%09%09this,  //OK button listener%0A%09%09%09%09null); //no CANCEL button listener%0A%09%09dialog.setIconImage(GUIFrame.getWindowIcon());%0A%09%09dialog.setAlwaysOnTop(true);%0A%0A%09%09Color4d col = ((ColourInput)input).getValue();%0A%09%09chooser.setColor(new Color((float)col.r, (float)col.g, (float)col.b, (float)col.a));%0A%09%09dialog.setVisible(true);%0A%0A%09%09// Apply editing%0A%09%09stopCellEditing();%0A%0A%09%09// Focus the cell%0A%09%09propTable.requestFocusInWindow();%0A%09%7D%0A%0A
 %7D%0A

delete unused method 'launchDialog' in ColorEditor