/*
 * Copyright 2006 Mask@ 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.ide.MaskatIDEUtil;
import org.maskat.ide.editors.MaskatEditor;
import org.maskat.ide.property.HeaderProperty;
import org.maskat.ide.view.MaskatEditView;

public class HeaderPropertyDescriptor extends PropertyDescriptor {

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

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

	private static class SelectCellEditor extends CellEditor {

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

		private Object value = null;

		protected SelectCellEditor(Composite parent) {
			super(parent, SWT.NONE);
		}

		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(),
										"Header削除処理", // タイトル
										"Header削除処理に失敗しました。", // エラーメッセージ
										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 HeaderProperty) {
					HeaderProperty hp = (HeaderProperty) value;

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

	}

}
