/*
 * Perfclispe
 * 
 * 
 * Copyright (c) 2013 Jakub Knetl
 *
 * 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.perfclipse.ui.swt.jface;

import java.util.List;

import org.eclipse.gef.commands.Command;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.perfclipse.model.MessageModel;
import org.perfclipse.ui.gef.commands.EditMessageMultiplicityCommand;
import org.perfclipse.ui.gef.commands.EditMessageUriCommand;

/**
 * @author Jakub Knetl
 *
 */
public class MessagesTableViewer extends AbstractCommandTableViewer {
	
	static final int URI_COLUMN_WIDTH = 350;
	private TableViewerColumn multiplicity;
	private TableViewerColumn validatorRef;
	private TableViewerColumn uri;

	
	/**
	 * @param parent
	 * @param style
	 * @param commands
	 */
	public MessagesTableViewer(Composite parent, int style,
			List<Command> commands) {
		super(parent, style, commands);
	}

	/**
	 * @param parent
	 * @param commands
	 */
	public MessagesTableViewer(Composite parent, List<Command> commands) {
		super(parent, commands);
	}

	@Override
	protected void initColumns() {

		uri = new TableViewerColumn(this, SWT.NONE);
		uri.getColumn().setText("Message URI");
		uri.setLabelProvider(new ColumnLabelProvider(){

			@Override
			public String getText(Object element) {

				MessageModel message = (MessageModel) element;
				return message.getMessage().getUri();
			}
			
		});

		uri.setEditingSupport(new AbstractCommandEditingSupport(this, getCommands()) {
			
			@Override
			protected Object getValue(Object element) {
				MessageModel message = (MessageModel) element;

				return message.getMessage().getUri();
			}

			@Override
			protected Command getCommand(Object element, Object value) {
				MessageModel message = (MessageModel) element;
				return new EditMessageUriCommand(message, String.valueOf(value));
			}
		});
		
		multiplicity = new TableViewerColumn(this, SWT.NONE);
		multiplicity.getColumn().setText("Mulitplicity");
		multiplicity.getColumn().setAlignment(SWT.CENTER);
		multiplicity.setLabelProvider(new ColumnLabelProvider(){

			@Override
			public String getText(Object element) {
				MessageModel message = (MessageModel) element;
				return message.getMessage().getMultiplicity();
			}
			
		});
		
		multiplicity.setEditingSupport(new AbstractCommandEditingSupport(this, getCommands()) {
			
			@Override
			protected Object getValue(Object element) {
				MessageModel message = (MessageModel) element;
				return message.getMessage().getMultiplicity();
			}
			
			@Override
			protected Command getCommand(Object element, Object value) {
				MessageModel message = (MessageModel) element;
				try {
					String newValue = String.valueOf(value);
					return new EditMessageMultiplicityCommand(message, newValue);
				} catch (NumberFormatException e){
					//TODO: show user warning
					return null;
				}
			}
		});
		
		validatorRef = new TableViewerColumn(this, SWT.NONE);
		validatorRef.getColumn().setText("Validators");
		validatorRef.getColumn().setAlignment(SWT.CENTER);
		validatorRef.setLabelProvider(new ColumnLabelProvider(){

			@Override
			public String getText(Object element) {
				MessageModel message = (MessageModel) element;
				return String.valueOf(message.getMessage().getValidatorRef().size());
			}
			
		});
		
	}

	@Override
	protected void setColumnsSize() {
		uri.getColumn().setWidth(URI_COLUMN_WIDTH);
		validatorRef.getColumn().pack();
		multiplicity.getColumn().pack();
	}
}

@@ -894,16 +894,82 @@
 posite;%0A
+import org.perfcake.model.Scenario.Messages.Message.ValidatorRef;%0A
 import o
@@ -3659,17 +3659,26 @@
 etText(%22
-V
+Attached v
 alidator
@@ -3910,75 +3910,325 @@
 %09%09%09%09
-return String.valueOf(message.getMessage().getValidatorRef().size()
+StringBuilder builder = new StringBuilder();%0A%09%09%09%09for (int i = 0 ; i %3C message.getMessage().getValidatorRef().size(); i++)%7B%0A%09%09%09%09%09ValidatorRef ref = message.getMessage().getValidatorRef().get(i); %0A%09%09%09%09%09if (i != 0)%7B%0A%09%09%09%09%09%09builder.append(%22, %22);%0A%09%09%09%09%09%7D%0A%09%09%09%09%09builder.append(ref.getId());%0A%09%09%09%09%7D%0A%0A%09%09%09%09return builder.toString(
 );%0A%09

Fix messages table viewer validator column