Program: 66937
Project: 66937
Buggy source:
private void createGroupField(){
  Composite groupField=new Composite(this,SWT.NONE);
  groupField.setLayoutData(new GridData(GridData.FILL_BOTH));
  groupField.setLayout(new GridLayout());
  Label groupLabel=new Label(groupField,SWT.NONE);
  groupLabel.setText(Messages.getString("String_Node_Str"));
  groupViewer=new TreeViewer(groupField,SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL| SWT.BORDER);
  groupViewer.getTree().setLayoutData(new GridData(GridData.FILL_BOTH));
  ((GridData)groupViewer.getTree().getLayoutData()).heightHint=250;
  ((GridData)groupViewer.getTree().getLayoutData()).widthHint=200;
  groupViewer.setLabelProvider(getCubeLabelProvider());
  groupViewer.setContentProvider(new CubeContentProvider());
  groupViewer.addSelectionChangedListener(new ISelectionChangedListener(){
    public void selectionChanged(    SelectionChangedEvent event){
      updateButtons();
    }
  }
);
  groupViewer.getTree().addKeyListener(new KeyAdapter(){
    public void keyPressed(    KeyEvent e){
      if (e.keyCode == SWT.DEL) {
        try {
          if (delBtn.isEnabled())           handleDelEvent();
        }
 catch (        Exception e1) {
          ExceptionUtil.handle(e1);
        }
      }
    }
  }
);
  groupViewer.getTree().addSelectionListener(new SelectionListener(){
    public void widgetSelected(    SelectionEvent e){
    }
    public void widgetDefaultSelected(    SelectionEvent e){
      if (editBtn.isEnabled())       handleEditEvent();
    }
  }
);
  groupViewer.getTree().addListener(SWT.PaintItem,new org.eclipse.swt.widgets.Listener(){
    public void handleEvent(    Event e){
      TreeItem item=(TreeItem)e.item;
      Color gray=Display.getCurrent().getSystemColor(SWT.COLOR_DARK_GRAY);
      if (item != null && item.getData() != null) {
        if (checkSharedDimension(item.getData()) && item.getData() instanceof LevelHandle)         item.setForeground(gray);
 else         item.setForeground(item.getParent().getForeground());
      }
 else       item.setForeground(item.getParent().getForeground());
    }
  }
);
  final DragSource fieldsSource=new DragSource(groupViewer.getTree(),operations);
  fieldsSource.setTransfer(types);
  fieldsSource.addDragListener(new CustomDragListener(groupViewer));
  DropTarget target=new DropTarget(groupViewer.getTree(),operations);
  target.setTransfer(types);
  target.addDropListener(new DropTargetAdapter(){
    public void dragOver(    DropTargetEvent event){
      event.feedback=DND.FEEDBACK_EXPAND | DND.FEEDBACK_SCROLL;
      if (OlapUtil.isFromLibrary(input)) {
        event.detail=DND.DROP_NONE;
        return;
      }
      if (event.item != null) {
        TreeItem item=(TreeItem)event.item;
        Object element=item.getData();
        event.detail=DND.DROP_MOVE;
        Object obj=dragSourceItems[0].getData();
        ResultSetColumnHandle dataField=null;
        DataSetHandle dataset=null;
        if (obj == null || obj instanceof DataSetHandle || (obj instanceof VirtualField && ((VirtualField)obj).getType().equals(VirtualField.TYPE_OTHER_DATASETS)) || (obj instanceof VirtualField && ((VirtualField)obj).getType().equals(VirtualField.TYPE_SHARED_DIMENSIONS))) {
          event.detail=DND.DROP_NONE;
          return;
        }
        if (checkSharedDimension(element)) {
          event.detail=DND.DROP_NONE;
          return;
        }
        if (obj instanceof ResultSetColumnHandle) {
          dataField=(ResultSetColumnHandle)obj;
          dataset=(DataSetHandle)dataField.getElementHandle();
          if (element instanceof LevelHandle) {
            DataSetHandle temp=OlapUtil.getHierarchyDataset(((TabularHierarchyHandle)((LevelHandle)element).getContainer()));
            if (temp != null && dataset != null && dataset != temp) {
              event.detail=DND.DROP_NONE;
              return;
            }
            DesignElementHandle hierarchy=((TabularLevelHandle)element).getContainer();
            DimensionHandle dimension=(DimensionHandle)hierarchy.getContainer();
            if (dimension.isTimeType()) {
              event.detail=DND.DROP_NONE;
              return;
            }
          }
 else           if (element instanceof DimensionHandle || (element instanceof VirtualField && ((VirtualField)element).getType().equals(VirtualField.TYPE_LEVEL))) {
            DimensionHandle dimension=null;
            if (element instanceof DimensionHandle)             dimension=(DimensionHandle)element;
 else             dimension=(DimensionHandle)((VirtualField)element).getModel();
            if (dimension.isTimeType()) {
              if (dimension.getDefaultHierarchy().getLevelCount() != 0 || !isDateType(dataField.getDataType())) {
                event.detail=DND.DROP_NONE;
                return;
              }
            }
            DataSetHandle temp=OlapUtil.getHierarchyDataset((TabularHierarchyHandle)dimension.getDefaultHierarchy());
            if (temp != null && dataset != null && dataset != temp) {
              event.detail=DND.DROP_NONE;
              return;
            }
          }
 else           if (element instanceof MeasureGroupHandle || (element instanceof VirtualField && ((VirtualField)element).getType().equals(VirtualField.TYPE_MEASURE)) || element instanceof MeasureHandle|| (element instanceof VirtualField && ((VirtualField)element).getType().equals(VirtualField.TYPE_MEASURE_GROUP))|| (element instanceof PropertyHandle && ((PropertyHandle)element).getPropertyDefn().getName().equals(ICubeModel.MEASURE_GROUPS_PROP))) {
            DataSetHandle primary=input.getDataSet();
            if (primary == null || primary != dataset) {
              event.detail=DND.DROP_NONE;
              return;
            }
          }
        }
        if (obj instanceof DimensionHandle) {
          DimensionHandle dimension=(DimensionHandle)obj;
          if (dimension.getContainer() instanceof TabularCubeHandle) {
            event.detail=DND.DROP_NONE;
            return;
          }
          if (!((element instanceof PropertyHandle && ((PropertyHandle)element).getPropertyDefn().getName().equals(ICubeModel.DIMENSIONS_PROP)) || (element instanceof VirtualField && ((VirtualField)element).getType().equals(VirtualField.TYPE_DIMENSION)))) {
            event.detail=DND.DROP_NONE;
            return;
          }
        }
        if (obj instanceof LevelHandle) {
          if (!(element instanceof LevelHandle) || element == obj || ((LevelHandle)obj).getContainer() != ((LevelHandle)element).getContainer()) {
            event.detail=DND.DROP_NONE;
            return;
          }
        }
        Point pt=Display.getCurrent().map(null,groupViewer.getTree(),event.x,event.y);
        Rectangle bounds=item.getBounds();
        if (pt.y < bounds.y + bounds.height / 3) {
          event.feedback|=DND.FEEDBACK_INSERT_BEFORE;
        }
 else         if (pt.y > bounds.y + 2 * bounds.height / 3) {
          event.feedback|=DND.FEEDBACK_INSERT_AFTER;
        }
 else {
          event.feedback|=DND.FEEDBACK_SELECT;
        }
      }
 else {
        event.detail=DND.DROP_NONE;
      }
    }
    public void drop(    DropTargetEvent event){
      if (event.data == null) {
        event.detail=DND.DROP_NONE;
        return;
      }
      Object obj=dragSourceItems[0].getData();
      ResultSetColumnHandle dataField=null;
      DataSetHandle dataset=null;
      if (obj == null || obj instanceof DataSetHandle) {
        event.detail=DND.DROP_NONE;
        return;
      }
      TreeItem item=(TreeItem)event.item;
      Object element=item.getData();
      if (obj instanceof DimensionHandle) {
        if ((element instanceof VirtualField && ((VirtualField)element).getType().equals(VirtualField.TYPE_DIMENSION)) || (element instanceof PropertyHandle && ((PropertyHandle)element).getPropertyDefn().getName().equals(ICubeModel.DIMENSIONS_PROP))) {
          CommandStack stack=SessionHandleAdapter.getInstance().getCommandStack();
          stack.startTrans("String_Node_Str");
          try {
            TabularDimensionHandle dimension=DesignElementFactory.getInstance().newTabularDimension(null);
            input.add(CubeHandle.DIMENSIONS_PROP,dimension);
            dimension.setSharedDimension((DimensionHandle)obj);
            stack.commit();
          }
 catch (          SemanticException e) {
            stack.rollback();
            refresh();
            ExceptionUtil.handle(e);
          }
        }
      }
      if (obj instanceof ResultSetColumnHandle) {
        dataField=(ResultSetColumnHandle)obj;
        dataset=(DataSetHandle)dataField.getElementHandle();
        if (event.item == null || dataField == null) {
          event.detail=DND.DROP_NONE;
          return;
        }
 else {
          Point pt=Display.getCurrent().map(null,groupViewer.getTree(),event.x,event.y);
          Rectangle bounds=item.getBounds();
          Boolean isValidName=UIUtil.validateDimensionName(OlapUtil.getDataFieldDisplayName(dataField));
          if (pt.y < bounds.y + bounds.height / 3) {
            if (element instanceof MeasureHandle) {
              CommandStack stack=SessionHandleAdapter.getInstance().getCommandStack();
              stack.startTrans("String_Node_Str");
              try {
                TabularMeasureHandle measure=DesignElementFactory.getInstance().newTabularMeasure(OlapUtil.getDataFieldDisplayName(dataField));
                Expression expression=new Expression(ExpressionUtility.getExpression(dataField,ExpressionUtility.getExpressionConverter(UIUtil.getDefaultScriptType())),UIUtil.getDefaultScriptType());
                measure.setExpressionProperty(MeasureHandle.MEASURE_EXPRESSION_PROP,expression);
                ColumnHintHandle column=OlapUtil.getColumnHintHandle(dataField);
                if (column != null) {
                  measure.setAlignment(column.getHorizontalAlign());
                }
                initMeasure(dataField,measure);
                ((MeasureHandle)element).getContainer().add(IMeasureGroupModel.MEASURES_PROP,measure);
                if (!isValidName) {
                  MeasureDialog dialog=new MeasureDialog(false);
                  dialog.setInput(measure);
                  if (dialog.open() == Window.CANCEL) {
                    SessionHandleAdapter.getInstance().getCommandStack().rollback();
                  }
                }
 else                 stack.commit();
              }
 catch (              SemanticException e) {
                stack.rollback();
                refresh();
                ExceptionUtil.handle(e);
              }
              return;
            }
 else             if (element instanceof LevelHandle) {
              DesignElementHandle hierarchy=((TabularLevelHandle)element).getContainer();
              DimensionHandle dimension=(DimensionHandle)hierarchy.getContainer();
              if (dimension.isTimeType()) {
                event.detail=DND.DROP_NONE;
                return;
              }
              int index=((LevelHandle)element).getIndex();
              CommandStack stack=SessionHandleAdapter.getInstance().getCommandStack();
              stack.startTrans("String_Node_Str");
              try {
                TabularLevelHandle level=DesignElementFactory.getInstance().newTabularLevel(dimension,OlapUtil.getDataFieldDisplayName(dataField));
                level.setColumnName(dataField.getColumnName());
                level.setDataType(dataField.getDataType());
                ((LevelHandle)element).getContainer().add(IHierarchyModel.LEVELS_PROP,level,index);
                ColumnHintHandle column=OlapUtil.getColumnHintHandle(dataField);
                if (column != null) {
                  level.setAlignment(column.getHorizontalAlign());
                }
                if (!isValidName) {
                  LevelPropertyDialog dialog=new LevelPropertyDialog(true);
                  dialog.setInput(level);
                  if (dialog.open() == Window.CANCEL) {
                    SessionHandleAdapter.getInstance().getCommandStack().rollback();
                  }
                }
 else                 stack.commit();
              }
 catch (              SemanticException e) {
                stack.rollback();
                refresh();
                ExceptionUtil.handle(e);
              }
              return;
            }
          }
{
            if (element instanceof MeasureHandle) {
              if (!checkColumnDataType(dataField))               return;
              CommandStack stack=SessionHandleAdapter.getInstance().getCommandStack();
              stack.startTrans("String_Node_Str");
              try {
                TabularMeasureHandle measure=DesignElementFactory.getInstance().newTabularMeasure(OlapUtil.getDataFieldDisplayName(dataField));
                Expression expression=new Expression(ExpressionUtility.getExpression(dataField,ExpressionUtility.getExpressionConverter(UIUtil.getDefaultScriptType())),UIUtil.getDefaultScriptType());
                measure.setExpressionProperty(MeasureHandle.MEASURE_EXPRESSION_PROP,expression);
                ColumnHintHandle column=OlapUtil.getColumnHintHandle(dataField);
                if (column != null) {
                  measure.setAlignment(column.getHorizontalAlign());
                }
                initMeasure(dataField,measure);
                ((MeasureHandle)element).getContainer().add(IMeasureGroupModel.MEASURES_PROP,measure);
                if (!isValidName) {
                  MeasureDialog dialog=new MeasureDialog(false);
                  dialog.setInput(measure);
                  if (dialog.open() == Window.CANCEL) {
                    SessionHandleAdapter.getInstance().getCommandStack().rollback();
                  }
                }
 else                 stack.commit();
              }
 catch (              SemanticException e) {
                stack.rollback();
                refresh();
                ExceptionUtil.handle(e);
                return;
              }
            }
 else             if (element instanceof MeasureGroupHandle || (element instanceof VirtualField && ((VirtualField)element).getType().equals(VirtualField.TYPE_MEASURE)) || (element instanceof VirtualField && ((VirtualField)element).getType().equals(VirtualField.TYPE_MEASURE_GROUP))|| (element instanceof PropertyHandle && ((PropertyHandle)element).getPropertyDefn().getName().equals(ICubeModel.MEASURE_GROUPS_PROP))) {
              if (!checkColumnDataType(dataField))               return;
              MeasureGroupHandle measureGroup=null;
              CommandStack stack=SessionHandleAdapter.getInstance().getCommandStack();
              stack.startTrans("String_Node_Str");
              try {
                if ((element instanceof VirtualField && ((VirtualField)element).getType().equals(VirtualField.TYPE_MEASURE_GROUP)) || (element instanceof PropertyHandle && ((PropertyHandle)element).getPropertyDefn().getName().equals(ICubeModel.MEASURE_GROUPS_PROP))) {
                  measureGroup=DesignElementFactory.getInstance().newTabularMeasureGroup(null);
                  input.add(CubeHandle.MEASURE_GROUPS_PROP,measureGroup);
                }
 else {
                  if (element instanceof MeasureGroupHandle)                   measureGroup=(MeasureGroupHandle)element;
 else                   measureGroup=(MeasureGroupHandle)((VirtualField)element).getModel();
                }
                TabularMeasureHandle measure=DesignElementFactory.getInstance().newTabularMeasure(OlapUtil.getDataFieldDisplayName(dataField));
                Expression expression=new Expression(ExpressionUtility.getExpression(dataField,ExpressionUtility.getExpressionConverter(UIUtil.getDefaultScriptType())),UIUtil.getDefaultScriptType());
                measure.setExpressionProperty(MeasureHandle.MEASURE_EXPRESSION_PROP,expression);
                ColumnHintHandle column=OlapUtil.getColumnHintHandle(dataField);
                if (column != null) {
                  measure.setAlignment(column.getHorizontalAlign());
                }
                initMeasure(dataField,measure);
                measureGroup.add(IMeasureGroupModel.MEASURES_PROP,measure);
                if (!isValidName) {
                  MeasureDialog dialog=new MeasureDialog(false);
                  dialog.setInput(measure);
                  if (dialog.open() == Window.CANCEL) {
                    SessionHandleAdapter.getInstance().getCommandStack().rollback();
                  }
                }
 else                 stack.commit();
              }
 catch (              SemanticException e) {
                stack.rollback();
                refresh();
                ExceptionUtil.handle(e);
                return;
              }
            }
 else             if (element instanceof LevelHandle) {
              TabularHierarchyHandle hierarchy=(TabularHierarchyHandle)((LevelHandle)element).getContainer();
              DimensionHandle dimension=(DimensionHandle)hierarchy.getContainer();
              if (dimension.isTimeType()) {
                event.detail=DND.DROP_NONE;
                return;
              }
              CommandStack stack=SessionHandleAdapter.getInstance().getCommandStack();
              stack.startTrans("String_Node_Str");
              try {
                int index=((LevelHandle)element).getIndex();
                TabularLevelHandle level=DesignElementFactory.getInstance().newTabularLevel(dimension,OlapUtil.getDataFieldDisplayName(dataField));
                level.setColumnName(dataField.getColumnName());
                level.setDataType(dataField.getDataType());
                ColumnHintHandle column=OlapUtil.getColumnHintHandle(dataField);
                if (column != null) {
                  level.setAlignment(column.getHorizontalAlign());
                }
                ((LevelHandle)element).getContainer().add(IHierarchyModel.LEVELS_PROP,level,index + 1);
                if (!isValidName) {
                  LevelPropertyDialog dialog=new LevelPropertyDialog(true);
                  dialog.setInput(level);
                  if (dialog.open() == Window.CANCEL) {
                    SessionHandleAdapter.getInstance().getCommandStack().rollback();
                  }
                }
 else                 stack.commit();
              }
 catch (              SemanticException e) {
                stack.rollback();
                refresh();
                ExceptionUtil.handle(e);
                return;
              }
            }
 else             if (element instanceof DimensionHandle || (element instanceof VirtualField && ((VirtualField)element).getType().equals(VirtualField.TYPE_LEVEL)) || (element instanceof VirtualField && ((VirtualField)element).getType().equals(VirtualField.TYPE_DIMENSION))|| (element instanceof PropertyHandle && ((PropertyHandle)element).getPropertyDefn().getName().equals(ICubeModel.DIMENSIONS_PROP))) {
              DimensionHandle dimension=null;
              CommandStack stack=SessionHandleAdapter.getInstance().getCommandStack();
              stack.startTrans("String_Node_Str");
              try {
                if ((element instanceof VirtualField && ((VirtualField)element).getType().equals(VirtualField.TYPE_DIMENSION)) || (element instanceof PropertyHandle && ((PropertyHandle)element).getPropertyDefn().getName().equals(ICubeModel.DIMENSIONS_PROP))) {
                  dimension=DesignElementFactory.getInstance().newTabularDimension(null);
                  input.add(CubeHandle.DIMENSIONS_PROP,dimension);
                  if (!isDateType(dataField.getDataType())) {
                    GroupRenameDialog inputDialog=createRenameDialog(dimension,Messages.getString("String_Node_Str"),Messages.getString("String_Node_Str"));
                    if (inputDialog.open() != Window.OK) {
                      stack.rollback();
                      refresh();
                      return;
                    }
                  }
                }
 else {
                  if (element instanceof DimensionHandle)                   dimension=(DimensionHandle)element;
 else                   dimension=(DimensionHandle)((VirtualField)element).getModel();
                }
                if (dimension.isTimeType() && dimension.getDefaultHierarchy().getLevelCount() > 0) {
                  event.detail=DND.DROP_NONE;
                  stack.rollback();
                  return;
                }
                TabularHierarchyHandle hierarchy=(TabularHierarchyHandle)dimension.getDefaultHierarchy();
                if (hierarchy.getDataSet() == null && hierarchy.getLevelCount() == 0 && (dataset != null && dataset != input.getDataSet())) {
                  hierarchy.setDataSet(dataset);
                }
                if (isDateType(dataField.getDataType()) && hierarchy.getLevelCount() == 0) {
                  GroupDialog dialog=new GroupDialog(true);
                  dialog.setInput(hierarchy,dataField);
                  if (dialog.open() != Window.OK) {
                    stack.rollback();
                  }
 else {
                    if (!isValidName) {
                      TabularLevelHandle level=(TabularLevelHandle)hierarchy.getLevel(dataField.getColumnName());
                      LevelPropertyDialog dialog2=new LevelPropertyDialog(false);
                      dialog2.setInput(level);
                      if (dialog2.open() == Window.CANCEL) {
                        SessionHandleAdapter.getInstance().getCommandStack().rollback();
                      }
                    }
 else                     stack.commit();
                  }
                }
 else {
                  TabularLevelHandle level=DesignElementFactory.getInstance().newTabularLevel(dimension,OlapUtil.getDataFieldDisplayName(dataField));
                  level.setColumnName(dataField.getColumnName());
                  level.setDataType(dataField.getDataType());
                  ColumnHintHandle column=OlapUtil.getColumnHintHandle(dataField);
                  if (column != null) {
                    level.setAlignment(column.getHorizontalAlign());
                  }
                  hierarchy.add(IHierarchyModel.LEVELS_PROP,level);
                  if (!isValidName) {
                    LevelPropertyDialog dialog=new LevelPropertyDialog(true);
                    dialog.setInput(level);
                    if (dialog.open() == Window.CANCEL) {
                      SessionHandleAdapter.getInstance().getCommandStack().rollback();
                    }
                  }
 else                   stack.commit();
                }
              }
 catch (              SemanticException e) {
                stack.rollback();
                refresh();
                ExceptionUtil.handle(e);
                return;
              }
            }
          }
        }
      }
      if (obj instanceof LevelHandle) {
        int oldIndex=((LevelHandle)obj).getIndex();
        if (event.item == null) {
          event.detail=DND.DROP_NONE;
          return;
        }
 else {
          Point pt=Display.getCurrent().map(null,groupViewer.getTree(),event.x,event.y);
          Rectangle bounds=item.getBounds();
          if (element instanceof LevelHandle) {
            int newIndex=((LevelHandle)element).getIndex();
            if (newIndex < oldIndex) {
              if (pt.y < bounds.y + bounds.height / 3) {
                newIndex=((LevelHandle)element).getIndex();
              }
 else               newIndex=((LevelHandle)element).getIndex() + 1;
            }
 else             if (newIndex > oldIndex) {
              if (pt.y < bounds.y + bounds.height / 3) {
                newIndex=((LevelHandle)element).getIndex() - 1;
              }
 else               newIndex=((LevelHandle)element).getIndex();
            }
            CommandStack stack=SessionHandleAdapter.getInstance().getCommandStack();
            stack.startTrans("String_Node_Str");
            try {
              ((LevelHandle)obj).moveTo(newIndex);
              stack.commit();
            }
 catch (            SemanticException e) {
              stack.rollback();
              refresh();
              ExceptionUtil.handle(e);
              return;
            }
            groupViewer.expandToLevel((obj),AbstractTreeViewer.ALL_LEVELS);
            groupViewer.setSelection(new StructuredSelection((obj)),true);
          }
        }
      }
      refresh();
    }
  }
);
}

Fixed version (ground truth):
private void createGroupField(){
  Composite groupField=new Composite(this,SWT.NONE);
  groupField.setLayoutData(new GridData(GridData.FILL_BOTH));
  groupField.setLayout(new GridLayout());
  Label groupLabel=new Label(groupField,SWT.NONE);
  groupLabel.setText(Messages.getString("String_Node_Str"));
  groupViewer=new TreeViewer(groupField,SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL| SWT.BORDER);
  groupViewer.getTree().setLayoutData(new GridData(GridData.FILL_BOTH));
  ((GridData)groupViewer.getTree().getLayoutData()).heightHint=250;
  ((GridData)groupViewer.getTree().getLayoutData()).widthHint=200;
  groupViewer.setLabelProvider(getCubeLabelProvider());
  groupViewer.setContentProvider(new CubeContentProvider());
  groupViewer.addSelectionChangedListener(new ISelectionChangedListener(){
    public void selectionChanged(    SelectionChangedEvent event){
      updateButtons();
    }
  }
);
  groupViewer.getTree().addKeyListener(new KeyAdapter(){
    public void keyPressed(    KeyEvent e){
      if (e.keyCode == SWT.DEL) {
        try {
          if (delBtn.isEnabled())           handleDelEvent();
        }
 catch (        Exception e1) {
          ExceptionUtil.handle(e1);
        }
      }
    }
  }
);
  groupViewer.getTree().addSelectionListener(new SelectionListener(){
    public void widgetSelected(    SelectionEvent e){
    }
    public void widgetDefaultSelected(    SelectionEvent e){
      if (editBtn.isEnabled())       handleEditEvent();
    }
  }
);
  groupViewer.getTree().addListener(SWT.PaintItem,new org.eclipse.swt.widgets.Listener(){
    public void handleEvent(    Event e){
      TreeItem item=(TreeItem)e.item;
      Color gray=Display.getCurrent().getSystemColor(SWT.COLOR_DARK_GRAY);
      if (item != null) {
        if (item.getData() != null) {
          if (checkSharedDimension(item.getData()) && item.getData() instanceof LevelHandle)           item.setForeground(gray);
 else           item.setForeground(item.getParent().getForeground());
        }
 else         item.setForeground(item.getParent().getForeground());
      }
    }
  }
);
  final DragSource fieldsSource=new DragSource(groupViewer.getTree(),operations);
  fieldsSource.setTransfer(types);
  fieldsSource.addDragListener(new CustomDragListener(groupViewer));
  DropTarget target=new DropTarget(groupViewer.getTree(),operations);
  target.setTransfer(types);
  target.addDropListener(new DropTargetAdapter(){
    public void dragOver(    DropTargetEvent event){
      event.feedback=DND.FEEDBACK_EXPAND | DND.FEEDBACK_SCROLL;
      if (OlapUtil.isFromLibrary(input)) {
        event.detail=DND.DROP_NONE;
        return;
      }
      if (event.item != null) {
        TreeItem item=(TreeItem)event.item;
        Object element=item.getData();
        event.detail=DND.DROP_MOVE;
        Object obj=dragSourceItems[0].getData();
        ResultSetColumnHandle dataField=null;
        DataSetHandle dataset=null;
        if (obj == null || obj instanceof DataSetHandle || (obj instanceof VirtualField && ((VirtualField)obj).getType().equals(VirtualField.TYPE_OTHER_DATASETS)) || (obj instanceof VirtualField && ((VirtualField)obj).getType().equals(VirtualField.TYPE_SHARED_DIMENSIONS))) {
          event.detail=DND.DROP_NONE;
          return;
        }
        if (checkSharedDimension(element)) {
          event.detail=DND.DROP_NONE;
          return;
        }
        if (obj instanceof ResultSetColumnHandle) {
          dataField=(ResultSetColumnHandle)obj;
          dataset=(DataSetHandle)dataField.getElementHandle();
          if (element instanceof LevelHandle) {
            DataSetHandle temp=OlapUtil.getHierarchyDataset(((TabularHierarchyHandle)((LevelHandle)element).getContainer()));
            if (temp != null && dataset != null && dataset != temp) {
              event.detail=DND.DROP_NONE;
              return;
            }
            DesignElementHandle hierarchy=((TabularLevelHandle)element).getContainer();
            DimensionHandle dimension=(DimensionHandle)hierarchy.getContainer();
            if (dimension.isTimeType()) {
              event.detail=DND.DROP_NONE;
              return;
            }
          }
 else           if (element instanceof DimensionHandle || (element instanceof VirtualField && ((VirtualField)element).getType().equals(VirtualField.TYPE_LEVEL))) {
            DimensionHandle dimension=null;
            if (element instanceof DimensionHandle)             dimension=(DimensionHandle)element;
 else             dimension=(DimensionHandle)((VirtualField)element).getModel();
            if (dimension.isTimeType()) {
              if (dimension.getDefaultHierarchy().getLevelCount() != 0 || !isDateType(dataField.getDataType())) {
                event.detail=DND.DROP_NONE;
                return;
              }
            }
            DataSetHandle temp=OlapUtil.getHierarchyDataset((TabularHierarchyHandle)dimension.getDefaultHierarchy());
            if (temp != null && dataset != null && dataset != temp) {
              event.detail=DND.DROP_NONE;
              return;
            }
          }
 else           if (element instanceof MeasureGroupHandle || (element instanceof VirtualField && ((VirtualField)element).getType().equals(VirtualField.TYPE_MEASURE)) || element instanceof MeasureHandle|| (element instanceof VirtualField && ((VirtualField)element).getType().equals(VirtualField.TYPE_MEASURE_GROUP))|| (element instanceof PropertyHandle && ((PropertyHandle)element).getPropertyDefn().getName().equals(ICubeModel.MEASURE_GROUPS_PROP))) {
            DataSetHandle primary=input.getDataSet();
            if (primary == null || primary != dataset) {
              event.detail=DND.DROP_NONE;
              return;
            }
          }
        }
        if (obj instanceof DimensionHandle) {
          DimensionHandle dimension=(DimensionHandle)obj;
          if (dimension.getContainer() instanceof TabularCubeHandle) {
            event.detail=DND.DROP_NONE;
            return;
          }
          if (!((element instanceof PropertyHandle && ((PropertyHandle)element).getPropertyDefn().getName().equals(ICubeModel.DIMENSIONS_PROP)) || (element instanceof VirtualField && ((VirtualField)element).getType().equals(VirtualField.TYPE_DIMENSION)))) {
            event.detail=DND.DROP_NONE;
            return;
          }
        }
        if (obj instanceof LevelHandle) {
          if (!(element instanceof LevelHandle) || element == obj || ((LevelHandle)obj).getContainer() != ((LevelHandle)element).getContainer()) {
            event.detail=DND.DROP_NONE;
            return;
          }
        }
        Point pt=Display.getCurrent().map(null,groupViewer.getTree(),event.x,event.y);
        Rectangle bounds=item.getBounds();
        if (pt.y < bounds.y + bounds.height / 3) {
          event.feedback|=DND.FEEDBACK_INSERT_BEFORE;
        }
 else         if (pt.y > bounds.y + 2 * bounds.height / 3) {
          event.feedback|=DND.FEEDBACK_INSERT_AFTER;
        }
 else {
          event.feedback|=DND.FEEDBACK_SELECT;
        }
      }
 else {
        event.detail=DND.DROP_NONE;
      }
    }
    public void drop(    DropTargetEvent event){
      if (event.data == null) {
        event.detail=DND.DROP_NONE;
        return;
      }
      Object obj=dragSourceItems[0].getData();
      ResultSetColumnHandle dataField=null;
      DataSetHandle dataset=null;
      if (obj == null || obj instanceof DataSetHandle) {
        event.detail=DND.DROP_NONE;
        return;
      }
      TreeItem item=(TreeItem)event.item;
      Object element=item.getData();
      if (obj instanceof DimensionHandle) {
        if ((element instanceof VirtualField && ((VirtualField)element).getType().equals(VirtualField.TYPE_DIMENSION)) || (element instanceof PropertyHandle && ((PropertyHandle)element).getPropertyDefn().getName().equals(ICubeModel.DIMENSIONS_PROP))) {
          CommandStack stack=SessionHandleAdapter.getInstance().getCommandStack();
          stack.startTrans("String_Node_Str");
          try {
            TabularDimensionHandle dimension=DesignElementFactory.getInstance().newTabularDimension(null);
            input.add(CubeHandle.DIMENSIONS_PROP,dimension);
            dimension.setSharedDimension((DimensionHandle)obj);
            stack.commit();
          }
 catch (          SemanticException e) {
            stack.rollback();
            refresh();
            ExceptionUtil.handle(e);
          }
        }
      }
      if (obj instanceof ResultSetColumnHandle) {
        dataField=(ResultSetColumnHandle)obj;
        dataset=(DataSetHandle)dataField.getElementHandle();
        if (event.item == null || dataField == null) {
          event.detail=DND.DROP_NONE;
          return;
        }
 else {
          Point pt=Display.getCurrent().map(null,groupViewer.getTree(),event.x,event.y);
          Rectangle bounds=item.getBounds();
          Boolean isValidName=UIUtil.validateDimensionName(OlapUtil.getDataFieldDisplayName(dataField));
          if (pt.y < bounds.y + bounds.height / 3) {
            if (element instanceof MeasureHandle) {
              CommandStack stack=SessionHandleAdapter.getInstance().getCommandStack();
              stack.startTrans("String_Node_Str");
              try {
                TabularMeasureHandle measure=DesignElementFactory.getInstance().newTabularMeasure(OlapUtil.getDataFieldDisplayName(dataField));
                Expression expression=new Expression(ExpressionUtility.getExpression(dataField,ExpressionUtility.getExpressionConverter(UIUtil.getDefaultScriptType())),UIUtil.getDefaultScriptType());
                measure.setExpressionProperty(MeasureHandle.MEASURE_EXPRESSION_PROP,expression);
                ColumnHintHandle column=OlapUtil.getColumnHintHandle(dataField);
                if (column != null) {
                  measure.setAlignment(column.getHorizontalAlign());
                }
                initMeasure(dataField,measure);
                ((MeasureHandle)element).getContainer().add(IMeasureGroupModel.MEASURES_PROP,measure);
                if (!isValidName) {
                  MeasureDialog dialog=new MeasureDialog(false);
                  dialog.setInput(measure);
                  if (dialog.open() == Window.CANCEL) {
                    SessionHandleAdapter.getInstance().getCommandStack().rollback();
                  }
                }
 else                 stack.commit();
              }
 catch (              SemanticException e) {
                stack.rollback();
                refresh();
                ExceptionUtil.handle(e);
              }
              return;
            }
 else             if (element instanceof LevelHandle) {
              DesignElementHandle hierarchy=((TabularLevelHandle)element).getContainer();
              DimensionHandle dimension=(DimensionHandle)hierarchy.getContainer();
              if (dimension.isTimeType()) {
                event.detail=DND.DROP_NONE;
                return;
              }
              int index=((LevelHandle)element).getIndex();
              CommandStack stack=SessionHandleAdapter.getInstance().getCommandStack();
              stack.startTrans("String_Node_Str");
              try {
                TabularLevelHandle level=DesignElementFactory.getInstance().newTabularLevel(dimension,OlapUtil.getDataFieldDisplayName(dataField));
                level.setColumnName(dataField.getColumnName());
                level.setDataType(dataField.getDataType());
                ((LevelHandle)element).getContainer().add(IHierarchyModel.LEVELS_PROP,level,index);
                ColumnHintHandle column=OlapUtil.getColumnHintHandle(dataField);
                if (column != null) {
                  level.setAlignment(column.getHorizontalAlign());
                }
                if (!isValidName) {
                  LevelPropertyDialog dialog=new LevelPropertyDialog(true);
                  dialog.setInput(level);
                  if (dialog.open() == Window.CANCEL) {
                    SessionHandleAdapter.getInstance().getCommandStack().rollback();
                  }
                }
 else                 stack.commit();
              }
 catch (              SemanticException e) {
                stack.rollback();
                refresh();
                ExceptionUtil.handle(e);
              }
              return;
            }
          }
{
            if (element instanceof MeasureHandle) {
              if (!checkColumnDataType(dataField))               return;
              CommandStack stack=SessionHandleAdapter.getInstance().getCommandStack();
              stack.startTrans("String_Node_Str");
              try {
                TabularMeasureHandle measure=DesignElementFactory.getInstance().newTabularMeasure(OlapUtil.getDataFieldDisplayName(dataField));
                Expression expression=new Expression(ExpressionUtility.getExpression(dataField,ExpressionUtility.getExpressionConverter(UIUtil.getDefaultScriptType())),UIUtil.getDefaultScriptType());
                measure.setExpressionProperty(MeasureHandle.MEASURE_EXPRESSION_PROP,expression);
                ColumnHintHandle column=OlapUtil.getColumnHintHandle(dataField);
                if (column != null) {
                  measure.setAlignment(column.getHorizontalAlign());
                }
                initMeasure(dataField,measure);
                ((MeasureHandle)element).getContainer().add(IMeasureGroupModel.MEASURES_PROP,measure);
                if (!isValidName) {
                  MeasureDialog dialog=new MeasureDialog(false);
                  dialog.setInput(measure);
                  if (dialog.open() == Window.CANCEL) {
                    SessionHandleAdapter.getInstance().getCommandStack().rollback();
                  }
                }
 else                 stack.commit();
              }
 catch (              SemanticException e) {
                stack.rollback();
                refresh();
                ExceptionUtil.handle(e);
                return;
              }
            }
 else             if (element instanceof MeasureGroupHandle || (element instanceof VirtualField && ((VirtualField)element).getType().equals(VirtualField.TYPE_MEASURE)) || (element instanceof VirtualField && ((VirtualField)element).getType().equals(VirtualField.TYPE_MEASURE_GROUP))|| (element instanceof PropertyHandle && ((PropertyHandle)element).getPropertyDefn().getName().equals(ICubeModel.MEASURE_GROUPS_PROP))) {
              if (!checkColumnDataType(dataField))               return;
              MeasureGroupHandle measureGroup=null;
              CommandStack stack=SessionHandleAdapter.getInstance().getCommandStack();
              stack.startTrans("String_Node_Str");
              try {
                if ((element instanceof VirtualField && ((VirtualField)element).getType().equals(VirtualField.TYPE_MEASURE_GROUP)) || (element instanceof PropertyHandle && ((PropertyHandle)element).getPropertyDefn().getName().equals(ICubeModel.MEASURE_GROUPS_PROP))) {
                  measureGroup=DesignElementFactory.getInstance().newTabularMeasureGroup(null);
                  input.add(CubeHandle.MEASURE_GROUPS_PROP,measureGroup);
                }
 else {
                  if (element instanceof MeasureGroupHandle)                   measureGroup=(MeasureGroupHandle)element;
 else                   measureGroup=(MeasureGroupHandle)((VirtualField)element).getModel();
                }
                TabularMeasureHandle measure=DesignElementFactory.getInstance().newTabularMeasure(OlapUtil.getDataFieldDisplayName(dataField));
                Expression expression=new Expression(ExpressionUtility.getExpression(dataField,ExpressionUtility.getExpressionConverter(UIUtil.getDefaultScriptType())),UIUtil.getDefaultScriptType());
                measure.setExpressionProperty(MeasureHandle.MEASURE_EXPRESSION_PROP,expression);
                ColumnHintHandle column=OlapUtil.getColumnHintHandle(dataField);
                if (column != null) {
                  measure.setAlignment(column.getHorizontalAlign());
                }
                initMeasure(dataField,measure);
                measureGroup.add(IMeasureGroupModel.MEASURES_PROP,measure);
                if (!isValidName) {
                  MeasureDialog dialog=new MeasureDialog(false);
                  dialog.setInput(measure);
                  if (dialog.open() == Window.CANCEL) {
                    SessionHandleAdapter.getInstance().getCommandStack().rollback();
                  }
                }
 else                 stack.commit();
              }
 catch (              SemanticException e) {
                stack.rollback();
                refresh();
                ExceptionUtil.handle(e);
                return;
              }
            }
 else             if (element instanceof LevelHandle) {
              TabularHierarchyHandle hierarchy=(TabularHierarchyHandle)((LevelHandle)element).getContainer();
              DimensionHandle dimension=(DimensionHandle)hierarchy.getContainer();
              if (dimension.isTimeType()) {
                event.detail=DND.DROP_NONE;
                return;
              }
              CommandStack stack=SessionHandleAdapter.getInstance().getCommandStack();
              stack.startTrans("String_Node_Str");
              try {
                int index=((LevelHandle)element).getIndex();
                TabularLevelHandle level=DesignElementFactory.getInstance().newTabularLevel(dimension,OlapUtil.getDataFieldDisplayName(dataField));
                level.setColumnName(dataField.getColumnName());
                level.setDataType(dataField.getDataType());
                ColumnHintHandle column=OlapUtil.getColumnHintHandle(dataField);
                if (column != null) {
                  level.setAlignment(column.getHorizontalAlign());
                }
                ((LevelHandle)element).getContainer().add(IHierarchyModel.LEVELS_PROP,level,index + 1);
                if (!isValidName) {
                  LevelPropertyDialog dialog=new LevelPropertyDialog(true);
                  dialog.setInput(level);
                  if (dialog.open() == Window.CANCEL) {
                    SessionHandleAdapter.getInstance().getCommandStack().rollback();
                  }
                }
 else                 stack.commit();
              }
 catch (              SemanticException e) {
                stack.rollback();
                refresh();
                ExceptionUtil.handle(e);
                return;
              }
            }
 else             if (element instanceof DimensionHandle || (element instanceof VirtualField && ((VirtualField)element).getType().equals(VirtualField.TYPE_LEVEL)) || (element instanceof VirtualField && ((VirtualField)element).getType().equals(VirtualField.TYPE_DIMENSION))|| (element instanceof PropertyHandle && ((PropertyHandle)element).getPropertyDefn().getName().equals(ICubeModel.DIMENSIONS_PROP))) {
              DimensionHandle dimension=null;
              CommandStack stack=SessionHandleAdapter.getInstance().getCommandStack();
              stack.startTrans("String_Node_Str");
              try {
                if ((element instanceof VirtualField && ((VirtualField)element).getType().equals(VirtualField.TYPE_DIMENSION)) || (element instanceof PropertyHandle && ((PropertyHandle)element).getPropertyDefn().getName().equals(ICubeModel.DIMENSIONS_PROP))) {
                  dimension=DesignElementFactory.getInstance().newTabularDimension(null);
                  input.add(CubeHandle.DIMENSIONS_PROP,dimension);
                  if (!isDateType(dataField.getDataType())) {
                    GroupRenameDialog inputDialog=createRenameDialog(dimension,Messages.getString("String_Node_Str"),Messages.getString("String_Node_Str"));
                    if (inputDialog.open() != Window.OK) {
                      stack.rollback();
                      refresh();
                      return;
                    }
                  }
                }
 else {
                  if (element instanceof DimensionHandle)                   dimension=(DimensionHandle)element;
 else                   dimension=(DimensionHandle)((VirtualField)element).getModel();
                }
                if (dimension.isTimeType() && dimension.getDefaultHierarchy().getLevelCount() > 0) {
                  event.detail=DND.DROP_NONE;
                  stack.rollback();
                  return;
                }
                TabularHierarchyHandle hierarchy=(TabularHierarchyHandle)dimension.getDefaultHierarchy();
                if (hierarchy.getDataSet() == null && hierarchy.getLevelCount() == 0 && (dataset != null && dataset != input.getDataSet())) {
                  hierarchy.setDataSet(dataset);
                }
                if (isDateType(dataField.getDataType()) && hierarchy.getLevelCount() == 0) {
                  GroupDialog dialog=new GroupDialog(true);
                  dialog.setInput(hierarchy,dataField);
                  if (dialog.open() != Window.OK) {
                    stack.rollback();
                  }
 else {
                    if (!isValidName) {
                      TabularLevelHandle level=(TabularLevelHandle)hierarchy.getLevel(dataField.getColumnName());
                      LevelPropertyDialog dialog2=new LevelPropertyDialog(false);
                      dialog2.setInput(level);
                      if (dialog2.open() == Window.CANCEL) {
                        SessionHandleAdapter.getInstance().getCommandStack().rollback();
                      }
                    }
 else                     stack.commit();
                  }
                }
 else {
                  TabularLevelHandle level=DesignElementFactory.getInstance().newTabularLevel(dimension,OlapUtil.getDataFieldDisplayName(dataField));
                  level.setColumnName(dataField.getColumnName());
                  level.setDataType(dataField.getDataType());
                  ColumnHintHandle column=OlapUtil.getColumnHintHandle(dataField);
                  if (column != null) {
                    level.setAlignment(column.getHorizontalAlign());
                  }
                  hierarchy.add(IHierarchyModel.LEVELS_PROP,level);
                  if (!isValidName) {
                    LevelPropertyDialog dialog=new LevelPropertyDialog(true);
                    dialog.setInput(level);
                    if (dialog.open() == Window.CANCEL) {
                      SessionHandleAdapter.getInstance().getCommandStack().rollback();
                    }
                  }
 else                   stack.commit();
                }
              }
 catch (              SemanticException e) {
                stack.rollback();
                refresh();
                ExceptionUtil.handle(e);
                return;
              }
            }
          }
        }
      }
      if (obj instanceof LevelHandle) {
        int oldIndex=((LevelHandle)obj).getIndex();
        if (event.item == null) {
          event.detail=DND.DROP_NONE;
          return;
        }
 else {
          Point pt=Display.getCurrent().map(null,groupViewer.getTree(),event.x,event.y);
          Rectangle bounds=item.getBounds();
          if (element instanceof LevelHandle) {
            int newIndex=((LevelHandle)element).getIndex();
            if (newIndex < oldIndex) {
              if (pt.y < bounds.y + bounds.height / 3) {
                newIndex=((LevelHandle)element).getIndex();
              }
 else               newIndex=((LevelHandle)element).getIndex() + 1;
            }
 else             if (newIndex > oldIndex) {
              if (pt.y < bounds.y + bounds.height / 3) {
                newIndex=((LevelHandle)element).getIndex() - 1;
              }
 else               newIndex=((LevelHandle)element).getIndex();
            }
            CommandStack stack=SessionHandleAdapter.getInstance().getCommandStack();
            stack.startTrans("String_Node_Str");
            try {
              ((LevelHandle)obj).moveTo(newIndex);
              stack.commit();
            }
 catch (            SemanticException e) {
              stack.rollback();
              refresh();
              ExceptionUtil.handle(e);
              return;
            }
            groupViewer.expandToLevel((obj),AbstractTreeViewer.ALL_LEVELS);
            groupViewer.setSelection(new StructuredSelection((obj)),true);
          }
        }
      }
      refresh();
    }
  }
);
}