/**
 *  Copyright (c) 2015-2017 Angelo ZERR.
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  which accompanies this distribution, and is available at
 *  http://www.eclipse.org/legal/epl-v10.html
 *
 *  Contributors:
 *  Angelo Zerr <angelo.zerr@gmail.com> - initial API and implementation
 */
package org.eclipse.tm4e.ui.internal.widgets;

import java.util.Iterator;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.window.Window;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.tm4e.registry.IGrammarDefinition;
import org.eclipse.tm4e.ui.internal.TMUIMessages;
import org.eclipse.tm4e.ui.internal.wizards.CreateThemeAssociationWizard;
import org.eclipse.tm4e.ui.themes.IThemeAssociation;
import org.eclipse.tm4e.ui.themes.IThemeManager;

/**
 * Widget which displays theme associations list on the left and "New", "Remove"
 * buttons on the right.
 *
 */
public class ThemeAssociationsWidget extends TableAndButtonsWidget {

	private IThemeManager themeManager;

	private Button newButton;
	private Button removeButton;

	private IGrammarDefinition definition;

	public ThemeAssociationsWidget(IThemeManager themeManager, Composite parent, int style) {
		super(parent, style, TMUIMessages.ThemeAssociationsWidget_description);
		this.themeManager = themeManager;
		super.setContentProvider(ArrayContentProvider.getInstance());
		super.setLabelProvider(new ThemeAssociationLabelProvider());
	}

	@Override
	protected void createButtons(Composite parent) {
		newButton = new Button(parent, SWT.PUSH);
		newButton.setText(TMUIMessages.Button_new);
		newButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		newButton.addListener(SWT.Selection, (e) -> {
			// Open the wizard to create association between theme and grammar.
			CreateThemeAssociationWizard wizard = new CreateThemeAssociationWizard(false);
			wizard.setInitialDefinition(definition);
			wizard.setThemeManager(themeManager);
			WizardDialog dialog = new WizardDialog(getShell(), wizard);
			if (dialog.open() == Window.OK) {
				refresh();
			}
		});
		newButton.setEnabled(false);

		removeButton = new Button(parent, SWT.PUSH);
		removeButton.setText(TMUIMessages.Button_remove);
		removeButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		removeButton.addListener(SWT.Selection, (e) -> {

			if (MessageDialog.openConfirm(getShell(), TMUIMessages.ThemeAssociationsWidget_remove_dialog_title,
					TMUIMessages.ThemeAssociationsWidget_remove_dialog_message)) {
				IStructuredSelection selection = super.getSelection();
				Iterator<IThemeAssociation> it = selection.iterator();
				while (it.hasNext()) {
					IThemeAssociation association = it.next();
					themeManager.unregisterThemeAssociation(association);
				}
				refresh();
			}

		});
		removeButton.setEnabled(false);
	}

	public Button getNewButton() {
		return newButton;
	}

	public Button getRemoveButton() {
		return removeButton;
	}

	public IThemeAssociation[] setGrammarDefinition(IGrammarDefinition definition) {
		this.definition = definition;
		return refresh();
	}

	private IThemeAssociation[] refresh() {
		IThemeAssociation[] themeAssociations = themeManager.getThemeAssociationsForScope(definition.getScopeName());
		super.setInput(themeAssociations);
		return themeAssociations;
	}

}

@@ -632,24 +632,79 @@
 Selection;%0D%0A
+import org.eclipse.jface.viewers.StructuredSelection;%0D%0A
 import org.e
@@ -2511,32 +2511,117 @@
 .OK) %7B%0D%0A%09%09%09%09
-refresh(
+IThemeAssociation association = wizard.getCreatedThemeAssociation();%0D%0A%09%09%09%09refresh(association
 );%0D%0A%09%09%09%7D%0D%0A%09%09
@@ -3330,24 +3330,28 @@
 %09%09%09%09refresh(
+null
 );%0D%0A%09%09%09%7D%0D%0A%0D%0A
@@ -3657,16 +3657,20 @@
 refresh(
+null
 );%0D%0A%09%7D%0D%0A
@@ -3708,16 +3708,45 @@
 refresh(
+IThemeAssociation association
 ) %7B%0D%0A%09%09I
@@ -3861,42 +3861,329 @@
 %0D%0A%09%09
-super.setInput(themeAssociations);
+// Refresh the list of associations%0D%0A%09%09super.setInput(themeAssociations);%0D%0A%09%09// Select the first of given association%0D%0A%09%09if (association == null && themeAssociations.length %3E 0) %7B%0D%0A%09%09%09association = themeAssociations%5B0%5D;%0D%0A%09%09%7D%0D%0A%09%09if (association != null) %7B%0D%0A%09%09%09super.setSelection(new StructuredSelection(association));%0D%0A%09%09%7D
 %0D%0A%09%09

Refresh association when new/remove association is done. See https://github.com/eclipse/tm4e/issues/117 Signed-off-by: angelozerr <angelo.zerr@gmail.com>