/*
 * Copyright 2006 Maskat Project.
 * 
 * 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 org.maskat.ide.property.descriptor;

import org.eclipse.gef.commands.CommandStack;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.views.properties.PropertyDescriptor;
import org.maskat.framework.IBasicDef;
import org.maskat.ide.MaskatIDEUtil;
import org.maskat.ide.editors.MaskatEditor;
import org.maskat.ide.property.ChildAppendableProperty;
import org.maskat.ide.view.MaskatEditView;

public abstract class ChildAppendablePropertyDescriptor extends PropertyDescriptor {

	public ChildAppendablePropertyDescriptor(Object id, String displayName) {
		super(id, displayName);
	}

	public CellEditor createPropertyEditor(Composite parent) {
		SelectCellEditor editor = new SelectCellEditor(parent, this);
		if (getValidator() != null) {
			editor.setValidator(getValidator());
		}
		return editor;
	}

	private static class SelectCellEditor extends CellEditor {

		/** PropertySheetEntryが持っているvalues */
		private Button button;

		private Object value = null;

		private ChildAppendablePropertyDescriptor descriptor;

		protected SelectCellEditor(Composite parent,
				ChildAppendablePropertyDescriptor descriptor) {
			super(parent, SWT.NONE);
			this.descriptor = descriptor;
		}

		protected Control createControl(Composite parent) {

			button = new Button(parent, SWT.DOWN);
			button.setText("+");
			button.setFont(parent.getFont());
			button.addKeyListener(new KeyAdapter() {
				public void keyReleased(KeyEvent e) {
					if (e.character == '\u001b') { // Escape
						fireCancelEditor();
					}
				}
			});
			button.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent event) {
					MaskatEditView editView = MaskatIDEUtil.getMaskatEditView();
					if (editView != null) {
						IEditorPart ep = MaskatIDEUtil.getActiveEditor();
						if (ep instanceof MaskatEditor) {
							((MaskatEditor) ep).editMode = MaskatEditor.SOURCE_EDIT_MODE;
							try {
								startEdit((MaskatEditor) ep);
							} catch (PartInitException e) {
								ErrorDialog.openError(PlatformUI.getWorkbench()
										.getActiveWorkbenchWindow().getShell(),
										"子プロパティ追加処理", // タイトル
										"子プロパティ追加処理に失敗しました。", // エラーメッセージ
										e.getStatus());

							}
						}
					}
				}
			});

			setValueValid(true);

			return button;
		}

		protected Object doGetValue() {
			return value;
		}

		protected void doSetFocus() {
			button.setFocus();
		}

		protected void doSetValue(Object value) {
			this.value = value;
		}

		private void startEdit(MaskatEditor ep) throws PartInitException {

			if (value != null) {
				if (value instanceof ChildAppendableProperty) {
					ChildAppendableProperty hp = (ChildAppendableProperty) value;
					IBasicDef newChild = SelectCellEditor.this.descriptor
							.createChild();

					// use the commandstack to update the hp;
					CommandStack cs = (CommandStack) ep.getAdapter(CommandStack.class);
					AddChildCommand command = new AddChildCommand(hp, newChild);
					cs.execute(command);
				}
			}
		}

	}

	protected abstract IBasicDef createChild();

}
