<commit_before>package org.csstudio.platform.ui.swt.stringtable;

import java.util.Arrays;
import java.util.List;

import org.csstudio.platform.ui.CSSPlatformUiPlugin;
import org.csstudio.platform.ui.internal.localization.Messages;
import org.csstudio.platform.ui.swt.AutoSizeColumn;
import org.csstudio.platform.ui.swt.AutoSizeControlListener;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Table;

/** Editor for table (list) of String or String[] entries,
 *  allows up/down ordering, add and delete
 *  @author <NAME>, <NAME>
 */
public class StringTableEditor extends Composite
{
	private static final String DELETE = "delete"; //$NON-NLS-1$
	private static final String DOWN = "down"; //$NON-NLS-1$
	private static final String UP = "up"; //$NON-NLS-1$
	private static final String EDIT = "edit"; //$NON-NLS-1$
	private final TableViewer tableViewer;
	private final static ImageRegistry images = new ImageRegistry();
	private Button editButton;
	private Button upButton;
	private Button downButton;
	private Button deleteButton;
	
	static {
		// Buttons: edit/up/down/delete		
		images.put(EDIT, CSSPlatformUiPlugin.getImageDescriptor("icons/edit.gif")); //$NON-NLS-1$
		images.put(UP, CSSPlatformUiPlugin.getImageDescriptor("icons/up.gif")); //$NON-NLS-1$
		images.put(DOWN, CSSPlatformUiPlugin.getImageDescriptor("icons/down.gif")); //$NON-NLS-1$
		images.put(DELETE, CSSPlatformUiPlugin.getImageDescriptor("icons/delete.gif")); //$NON-NLS-1$
	}
	
	/** Creates an editable table.  The size of headers array implies the number of columns. 
	 * @param parent The composite which the table resides in
	 * @param headers Contains the header for each column
	 * @param editable Whether it is editable for each column. The size must be same as headers.  
	 * @param items The items to be displayed and manipulated in the table. 
	 * Each element in the list, which is an array of string, represents the data in a row.  
	 * In turn, each element in the string array represents the data in a cell. 
	 * So it is required that every string array in the list must has the same size as headers.  
	 */
	public StringTableEditor(final Composite parent, final String[] headers, 
			final boolean[] editable, final List<String[]> items, 
			final RowEditDialog rowEditDialog,
			final int[] columnsMinWidth) 
	{
		super(parent, 0);
		final GridLayout layout = new GridLayout();
		layout.numColumns = headers.length;
		setLayout(layout);
		
		//Edit-able Table
		tableViewer = new TableViewer(this, SWT.BORDER | SWT.FULL_SELECTION |
				SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
		final Table table = tableViewer.getTable();
		GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 4);
		gd.heightHint = 100;
		table.setLayoutData(gd);
		table.setLinesVisible(true);
		table.setHeaderVisible(true);
		
		//Create edit-able columns
		for(int i = 0; i < layout.numColumns; i++) {
			final TableViewerColumn col = 
				AutoSizeColumn.make(tableViewer, headers[i], columnsMinWidth[i], 100, false);
			col.setLabelProvider(new StringMultiColumnsLabelProvider(tableViewer, editable[i]));
			//col.setLabelProvider(new StringColumnLabelProvider(tableViwer));
			if(editable[i]) {
				col.setEditingSupport(new StringMultiColumnsEditor(tableViewer,
						layout.numColumns, i));	
			}
		}
		tableViewer.setContentProvider(new StringTableContentProvider<String[]>());
		tableViewer.setInput(items);
		new AutoSizeControlListener(table);		
		editButton = createEditButton(layout.numColumns, rowEditDialog);
		upButton = createUpButton();
		downButton = createDownButton();
		deleteButton = createDeleteButton();		<commit_msg>Only worked for tables with 2 columns because the layout (table, buttons) used the same column count as the table. Now the layout is fixed to the (correct) 2 columns.<commit_after>package org.csstudio.platform.ui.swt.stringtable;

import java.util.Arrays;
import java.util.List;

import org.csstudio.platform.ui.CSSPlatformUiPlugin;
import org.csstudio.platform.ui.internal.localization.Messages;
import org.csstudio.platform.ui.swt.AutoSizeColumn;
import org.csstudio.platform.ui.swt.AutoSizeControlListener;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Table;

/** Editor for table (list) of String or String[] entries,
 *  allows up/down ordering, add and delete
 *  @author <NAME>, <NAME>
 */
public class StringTableEditor extends Composite
{
	private static final String DELETE = "delete"; //$NON-NLS-1$
	private static final String DOWN = "down"; //$NON-NLS-1$
	private static final String UP = "up"; //$NON-NLS-1$
	private static final String EDIT = "edit"; //$NON-NLS-1$
	private final TableViewer tableViewer;
	private final static ImageRegistry images = new ImageRegistry();
	private Button editButton;
	private Button upButton;
	private Button downButton;
	private Button deleteButton;
	
	static {
		// Buttons: edit/up/down/delete		
		images.put(EDIT, CSSPlatformUiPlugin.getImageDescriptor("icons/edit.gif")); //$NON-NLS-1$
		images.put(UP, CSSPlatformUiPlugin.getImageDescriptor("icons/up.gif")); //$NON-NLS-1$
		images.put(DOWN, CSSPlatformUiPlugin.getImageDescriptor("icons/down.gif")); //$NON-NLS-1$
		images.put(DELETE, CSSPlatformUiPlugin.getImageDescriptor("icons/delete.gif")); //$NON-NLS-1$
	}
	
	/** Creates an editable table.  The size of headers array implies the number of columns. 
	 * @param parent The composite which the table resides in
	 * @param headers Contains the header for each column
	 * @param editable Whether it is editable for each column. The size must be same as headers.  
	 * @param items The items to be displayed and manipulated in the table. 
	 * Each element in the list, which is an array of string, represents the data in a row.  
	 * In turn, each element in the string array represents the data in a cell. 
	 * So it is required that every string array in the list must has the same size as headers.  
	 */
	public StringTableEditor(final Composite parent, final String[] headers, 
			final boolean[] editable, final List<String[]> items, 
			final RowEditDialog rowEditDialog,
			final int[] columnsMinWidth) 
	{
		super(parent, 0);

		final int table_columns = headers.length;
		if (editable.length != table_columns ||
		    columnsMinWidth.length != table_columns)
		    throw new Error("Inconsistent table column count"); //$NON-NLS-1$

		// StringTableEditor itself is a Composite that contains 2 columns:
		// Left column: Table
		// Right column: Edit/up/down/delete buttons
		setLayout(new GridLayout(2, false));
		
		//Edit-able Table
		tableViewer = new TableViewer(this, SWT.BORDER | SWT.FULL_SELECTION |
				SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
		final Table table = tableViewer.getTable();
		GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 4);
		gd.heightHint = 100;
		table.setLayoutData(gd);
		table.setLinesVisible(true);
		table.setHeaderVisible(true);
		
		//Create edit-able columns
		for(int i = 0; i < table_columns; i++) {
			final TableViewerColumn col = 
				AutoSizeColumn.make(tableViewer, headers[i], columnsMinWidth[i], 100, false);
			col.setLabelProvider(new StringMultiColumnsLabelProvider(tableViewer, editable[i]));
			//col.setLabelProvider(new StringColumnLabelProvider(tableViwer));
			if(editable[i]) {
				col.setEditingSupport(new StringMultiColumnsEditor(tableViewer,
				        table_columns, i));	
			}
		}
		tableViewer.setContentProvider(new StringTableContentProvider<String[]>());
		tableViewer.setInput(items);
		new AutoSizeControlListener(table);		
		editButton = createEditButton(table_columns, rowEditDialog);
		upButton = createUpButton();
		downButton = createDownButton();
		deleteButton = createDeleteButton();		
{'fraction_non_alphanumeric': 0.10824453168816602, 'fraction_numerical': 0.004598990465507572, 'mean_word_length': 4.68985322271857, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6351711', 'n_tokens_mistral': 2918, 'n_tokens_neox': 2708, 'n_words': 734}