Skip to content

Commit

Permalink
Add buttons to move items up and/or down to change order.
Browse files Browse the repository at this point in the history
Signed-off-by: Carl Thronson <cthronson@actuate.com>
  • Loading branch information
Carl Thronson committed Feb 2, 2015
1 parent e4de4be commit cf903c2
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4326,6 +4326,7 @@ IDEResourcePageHelper.WorkSpace.Button=&Workspace...

###########################################################
#DataSetColumnBindingsFormHandleProvider
DataSetColumnBindingsFormHandleProvider.Column.Position=Position
DataSetColumnBindingsFormHandleProvider.Column.Name=Name
DataSetColumnBindingsFormHandleProvider.Column.DisplayNameID=Display Name ID
DataSetColumnBindingsFormHandleProvider.Column.DisplayName=Display Name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.eclipse.birt.core.data.ExpressionUtil;
import org.eclipse.birt.core.exception.BirtException;
Expand Down Expand Up @@ -67,6 +69,7 @@ public class DataSetColumnBindingsFormHandleProvider extends
private static final String NONE = Messages.getString( "DataSetColumnBindingsFormHandleProvider.NONE" );//$NON-NLS-1$

private String[] columnNames = new String[]{
Messages.getString( "DataSetColumnBindingsFormHandleProvider.Column.Position" ),//$NON-NLS-1$
Messages.getString( "DataSetColumnBindingsFormHandleProvider.Column.Name" ),//$NON-NLS-1$
Messages.getString( "DataSetColumnBindingsFormHandleProvider.Column.DisplayNameID" ),//$NON-NLS-1$
Messages.getString( "DataSetColumnBindingsFormHandleProvider.Column.DisplayName" ),//$NON-NLS-1$
Expand All @@ -77,7 +80,7 @@ public class DataSetColumnBindingsFormHandleProvider extends
private CellEditor[] editors;

private static int[] columnWidth = new int[]{
140, 140, 140, 80, 200
80, 140, 140, 140, 80, 200
};

// object to add data binding.
Expand All @@ -89,6 +92,8 @@ public class DataSetColumnBindingsFormHandleProvider extends
.getAllowedChoices( )
.getChoices( );

Map<String,Integer> columnBindingNameToPositionMap = new HashMap<String,Integer>();

public boolean isEnable( )
{
if ( DEUtil.getInputSize( input ) != 1 )
Expand Down Expand Up @@ -139,7 +144,10 @@ public ReportElementHandle getBindingObject( )
public void setBindingObject( DesignElementHandle bindingObject )
{
if ( bindingObject instanceof ReportElementHandle )
this.bindingObject = (ReportElementHandle) bindingObject;
{
this.bindingObject = ( ReportElementHandle )bindingObject;
this.columnBindingNameToPositionMap.clear();
}
}

public String[] getColumnNames( )
Expand Down Expand Up @@ -259,21 +267,62 @@ public boolean doEditItem( int pos )
return false;
}

private Integer getColumnPosition( ComputedColumnHandle handle )
{
String name = handle.getName( );
if ( name == null )
return 0;

Map<String, Integer> map = getColumnBindingMap( );
if (map.containsKey( name ))
{
return map.get( name );
}
else
{
return map.size() + 1;
}
}

private Map<String, Integer> getColumnBindingMap( )
{
if ( this.columnBindingNameToPositionMap.isEmpty() )
{
DesignElementHandle eHandle = this.getBindingObject();
if ( eHandle instanceof ReportItemHandle )
{
int position = 1;
ReportItemHandle handle = ( ReportItemHandle ) this.getBindingObject();
for ( Iterator iter = handle.getColumnBindings( )
.iterator( ); iter.hasNext( ); )
{
ComputedColumnHandle col = (ComputedColumnHandle)iter.next();
String name = col.getName();
this.columnBindingNameToPositionMap.put( col.getName(), position );
position++;
}
}
}
return this.columnBindingNameToPositionMap;
}

public String getColumnText( Object element, int columnIndex )
{
switch ( columnIndex )
{
case 0 :
return ( (ComputedColumnHandle) element ).getName( );
return getColumnPosition( (ComputedColumnHandle) element ).toString();
case 1 :
return ( (ComputedColumnHandle) element ).getDisplayNameID( );
return ( (ComputedColumnHandle) element ).getName( );
case 2 :
return ( (ComputedColumnHandle) element ).getDisplayName( );
return ( (ComputedColumnHandle) element ).getDisplayNameID( );
case 3 :
return getDataTypeDisplayName( ( (ComputedColumnHandle) element ).getDataType( ) );
return ( (ComputedColumnHandle) element ).getDisplayName( );
case 4 :
return DataUtil.getAggregationExpression( (ComputedColumnHandle) element );
return getDataTypeDisplayName( ( (ComputedColumnHandle) element ).getDataType( ) );
case 5 :
return DataUtil.getAggregationExpression( (ComputedColumnHandle) element );
case 6 :
try
{
String function = ( (ComputedColumnHandle) element ).getAggregateFunction( );
Expand All @@ -293,7 +342,7 @@ public String getColumnText( Object element, int columnIndex )
ExceptionHandler.handle( e );
}
return null;
case 6 :
case 7 :
String ExpValue = ( (ComputedColumnHandle) element ).getFilterExpression( );
if ( ExpValue != null && ExpValue.length( ) > 0 )
{
Expand All @@ -303,7 +352,7 @@ public String getColumnText( Object element, int columnIndex )
{
return null;
}
case 7 :
case 8 :
String value = DEUtil.getAggregateOn( (ComputedColumnHandle) element );
String text;
if ( value == null )
Expand Down Expand Up @@ -387,15 +436,26 @@ public int getOriginalIndex( int pos )

private class BindingComparator implements Comparator
{

public int compare( Object o1, Object o2 )
{
ComputedColumnHandle binding1 = (ComputedColumnHandle) o1;
ComputedColumnHandle binding2 = (ComputedColumnHandle) o2;
String columnText1 = getColumnText( binding1, sortingColumnIndex );
String columnText2 = getColumnText( binding2, sortingColumnIndex );
int result = ( columnText1 == null ? "" : columnText1 ).compareTo( ( columnText2 == null ? "" //$NON-NLS-1$ //$NON-NLS-2$

int result = 0;

if ( sortingColumnIndex == 0 ) // Sort by position
{
Integer col1 = getColumnPosition( binding1 );
Integer col2 = getColumnPosition( binding2 );
result = col1.compareTo( col2 );
}
else
{
String columnText1 = getColumnText( binding1, sortingColumnIndex );
String columnText2 = getColumnText( binding2, sortingColumnIndex );
result = ( columnText1 == null ? "" : columnText1 ).compareTo( ( columnText2 == null ? "" //$NON-NLS-1$ //$NON-NLS-2$
: columnText2 ) );
}
if ( sortDirection == SWT.UP )
return result;
else
Expand Down Expand Up @@ -704,6 +764,7 @@ public void setShowAggregation( boolean showAggregation )
if ( showAggregation )
{
columnNames = new String[]{
Messages.getString( "DataSetColumnBindingsFormHandleProvider.Column.Position" ), //$NON-NLS-1$
Messages.getString( "DataSetColumnBindingsFormHandleProvider.Column.Name" ), //$NON-NLS-1$
Messages.getString( "DataSetColumnBindingsFormHandleProvider.Column.DisplayNameID" ), //$NON-NLS-1$
Messages.getString( "DataSetColumnBindingsFormHandleProvider.Column.DisplayName" ), //$NON-NLS-1$
Expand All @@ -714,7 +775,7 @@ public void setShowAggregation( boolean showAggregation )
Messages.getString( "DataSetColumnBindingsFormHandleProvider.Column.AggregateOn" )//$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
};
columnWidth = new int[]{
110, 110, 110, 80, 110, 110, 110, 90
80, 110, 110, 110, 80, 110, 110, 110, 90
};
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
* Data set binding page.
*/

public class DataSetColumnBindingsFormPage extends SortingFormPage
public class DataSetColumnBindingsFormPage extends FormPage
{

private Button btnAddAggr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,18 @@
import org.eclipse.birt.report.model.api.metadata.IChoiceSet;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Table;

/**
*
*/

public class DataSetColumnBindingsFormHandleProvider implements
ISortingFormHandleProvider
IFormHandleProvider
{

protected static final Logger logger = Logger.getLogger( DataSetColumnBindingsFormHandleProvider.class.getName( ) );
Expand Down Expand Up @@ -266,30 +269,12 @@ public boolean doEditItem( int pos )

public int getOriginalIndex( int pos )
{
List children = new ArrayList( );
for ( Iterator iter = ( (ReportItemHandle) bindingObject ).getColumnBindings( )
.iterator( ); iter.hasNext( ); )
{
children.add( iter.next( ) );
}

Object[] arrays = children.toArray( );
Arrays.sort( arrays, new BindingComparator( ) );
return children.indexOf( Arrays.asList( arrays ).get( pos ) );
return pos;
}

public int getShowIndex( int pos )
{
List children = new ArrayList( );
for ( Iterator iter = ( (ReportItemHandle) bindingObject ).getColumnBindings( )
.iterator( ); iter.hasNext( ); )
{
children.add( iter.next( ) );
}

Object[] arrays = children.toArray( );
Arrays.sort( arrays, new BindingComparator( ) );
return Arrays.asList( arrays ).indexOf( children.get( pos ) );
return pos;
}

public String getColumnText( Object element, int columnIndex )
Expand Down Expand Up @@ -394,30 +379,11 @@ public Object[] getElements( Object inputElement )
}
}
Object[] arrays = children.toArray( );
Arrays.sort( arrays, new BindingComparator( ) );
return arrays;
}
return new Object[]{};
}

private class BindingComparator implements Comparator
{

public int compare( Object o1, Object o2 )
{
ComputedColumnHandle binding1 = (ComputedColumnHandle) o1;
ComputedColumnHandle binding2 = (ComputedColumnHandle) o2;
String columnText1 = getColumnText( binding1, sortingColumnIndex );
String columnText2 = getColumnText( binding2, sortingColumnIndex );
int result = ( columnText1 == null ? "" : columnText1 ).compareTo( ( columnText2 == null ? "" //$NON-NLS-1$ //$NON-NLS-2$
: columnText2 ) );
if ( sortDirection == SWT.UP )
return result;
else
return 0 - result;
}
}

public Object getValue( Object element, String property )
{
int index = Arrays.asList( columnNames ).indexOf( property );
Expand Down Expand Up @@ -747,4 +713,73 @@ public void setSortDirection( int dir )
sortDirection = dir;
}

@Override
public CellEditor[] getEditors(Table table) {
return null;
}

@Override
public boolean doMoveItem(int oldPos, int newPos) throws Exception
{
if ( Math.abs( oldPos - newPos) > 1 )
return false;

ReportElementHandle elementHandle = this.getBindingObject();
if (elementHandle instanceof ReportItemHandle )
{
ReportItemHandle itemHandle = ( ReportItemHandle )elementHandle;
List<ComputedColumn> list = itemHandle.getColumnBindings().getItems();

ComputedColumn itemToMove = list.get( oldPos );
ComputedColumn displacedItem = list.get(newPos );

// Move the item that is moving
list.set( newPos, itemToMove );
// Restore the item that was displaced
list.set( oldPos, displacedItem );

// Wipe out all the bound columns
int size = list.size();
for (int index = 0; index < size; index++)
{
( (ReportItemHandle) bindingObject ).getColumnBindings( )
.getAt( 0 )
.drop( );
// itemHandle.getColumnBindings( ).getAt( 0 ).drop( );
}

// Add back all the items in the new list order
Iterator<ComputedColumn> it = list.iterator();
while ( it.hasNext() )
{
ComputedColumn next = it.next();
itemHandle.addColumnBinding( next, true );
}

if ( viewer != null )
{
viewer.refresh( true );
viewer.getTable( ).setSelection( newPos );
}
return true;
}
return false;
}

@Override
public Image getImage(Object element, int columnIndex) {
return null;
}

@Override
public boolean canModify(Object element, String property) {
return false;
}

@Override
public boolean modify(Object data, String property, Object value)
throws Exception {
return false;
}

}

0 comments on commit cf903c2

Please sign in to comment.