Program: 139797
Project: 139797
Buggy source:
/** 
 * Add a new session to the database structure view. This will create a new tab for the session.
 * @param session
 */
private void addSession(final MetaDataSession session) throws SQLCannotConnectException {
  if (_allSessions.contains(session))   return;
  try {
    session.getMetaData();
    session.setAutoCommit(true);
  }
 catch (  SQLCannotConnectException e) {
    SQLExplorerPlugin.error(e);
    throw e;
  }
catch (  SQLException e) {
    SQLExplorerPlugin.error(e);
    MessageDialog.openError(getSite().getShell(),"String_Node_Str",e.getMessage());
  }
  DatabaseNode rootNode=session.getRoot();
  if (rootNode == null)   return;
  _allSessions.add(session);
  if (_filterAction != null) {
    _filterAction.setEnabled(true);
  }
  if (_tabFolder == null || _tabFolder.isDisposed()) {
    clearParent();
    _tabFolder=new CTabFolder(_parent,SWT.TOP | SWT.CLOSE);
    _tabFolder.addSelectionListener(new SelectionAdapter(){
      public void widgetSelected(      SelectionEvent e){
        DatabaseDetailView detailView=(DatabaseDetailView)getSite().getPage().findView(SqlexplorerViewConstants.SQLEXPLORER_DBDETAIL);
        synchronizeDetailView(detailView);
      }
    }
);
    Display display=getSite().getShell().getDisplay();
    _tabFolder.setSelectionBackground(new Color[]{display.getSystemColor(SWT.COLOR_WHITE),new Color(null,211,225,250),new Color(null,175,201,246),IConstants.TAB_BORDER_COLOR},new int[]{25,50,75},true);
    _tabFolder.addCTabFolder2Listener(new CTabFolder2Adapter(){
      public void close(      CTabFolderEvent event){
        CTabItem tabItem=(CTabItem)event.item;
        TabData tabData=(TabData)tabItem.getData();
        _allSessions.remove(tabData.session);
        event.doit=true;
      }
    }
);
    _parent.layout();
    _parent.redraw();
  }
  final CTabItem tabItem=new CTabItem(_tabFolder,SWT.NULL);
  TabData tabData=new TabData();
  tabItem.setData(tabData);
  tabData.session=session;
  String labelText=session.getUser().getDescription();
  tabItem.setText(labelText);
  Composite composite=new Composite(_tabFolder,SWT.NULL);
  composite.setLayout(new FillLayout());
  tabItem.setControl(composite);
  final TreeViewer treeViewer=new TreeViewer(composite,SWT.V_SCROLL | SWT.H_SCROLL | SWT.MULTI| SWT.BORDER);
  tabData.treeViewer=treeViewer;
  Transfer[] transfers=new Transfer[]{TableNodeTransfer.getInstance()};
  treeViewer.addDragSupport(DND.DROP_COPY,transfers,new DragSourceListener(){
    public void dragFinished(    DragSourceEvent event){
      System.out.println("String_Node_Str");
      TableNodeTransfer.getInstance().setSelection(null);
    }
    public void dragSetData(    DragSourceEvent event){
      Object sel=((IStructuredSelection)treeViewer.getSelection()).getFirstElement();
      event.data=sel;
    }
    public void dragStart(    DragSourceEvent event){
      event.doit=!treeViewer.getSelection().isEmpty();
      if (event.doit) {
        Object sel=((IStructuredSelection)treeViewer.getSelection()).getFirstElement();
        if (!(sel instanceof TableNode)) {
          event.doit=false;
        }
 else {
          TableNode tn=(TableNode)sel;
          TableNodeTransfer.getInstance().setSelection(tn);
          if (!tn.isTable())           event.doit=false;
        }
      }
    }
  }
);
  treeViewer.setUseHashlookup(true);
  treeViewer.setContentProvider(new DBTreeContentProvider());
  treeViewer.setLabelProvider(new DBTreeLabelProvider());
  treeViewer.setInput(rootNode);
  treeViewer.addSelectionChangedListener(new ISelectionChangedListener(){
    public void selectionChanged(    SelectionChangedEvent ev){
      DatabaseDetailView detailView=(DatabaseDetailView)getSite().getPage().findView(SqlexplorerViewConstants.SQLEXPLORER_DBDETAIL);
      synchronizeDetailView(detailView);
    }
  }
);
  treeViewer.addDoubleClickListener(new IDoubleClickListener(){
    public void doubleClick(    DoubleClickEvent event){
      try {
        DatabaseDetailView detailView=(DatabaseDetailView)getSite().getPage().findView(SqlexplorerViewConstants.SQLEXPLORER_DBDETAIL);
        if (detailView == null) {
          getSite().getPage().showView(SqlexplorerViewConstants.SQLEXPLORER_DBDETAIL);
        }
        getSite().getPage().bringToTop(detailView);
        synchronizeDetailView(detailView);
      }
 catch (      Exception e) {
      }
    }
  }
);
  treeViewer.addTreeListener(new ITreeViewerListener(){
    public void treeCollapsed(    TreeExpansionEvent event){
      INode node=(INode)event.getElement();
      node.setExpanded(false);
      TreeViewer viewer=(TreeViewer)event.getSource();
      viewer.update(node,null);
    }
    public void treeExpanded(    TreeExpansionEvent event){
      INode node=(INode)event.getElement();
      node.setExpanded(true);
      TreeViewer viewer=(TreeViewer)event.getSource();
      viewer.update(node,null);
    }
  }
);
  _tabFolder.setSelection(_tabFolder.getItemCount() - 1);
  DatabaseDetailView detailView=(DatabaseDetailView)getSite().getPage().findView(SqlexplorerViewConstants.SQLEXPLORER_DBDETAIL);
  if (detailView != null) {
    synchronizeDetailView(detailView);
    getSite().getPage().bringToTop(detailView);
  }
  composite.layout();
  _tabFolder.layout();
  _tabFolder.redraw();
  getSite().getPage().bringToTop(this);
  final DBTreeActionGroup actionGroup=new DBTreeActionGroup(treeViewer);
  MenuManager menuManager=new MenuManager("String_Node_Str");
  menuManager.setRemoveAllWhenShown(true);
  Menu contextMenu=menuManager.createContextMenu(treeViewer.getTree());
  treeViewer.getTree().setMenu(contextMenu);
  menuManager.addMenuListener(new IMenuListener(){
    public void menuAboutToShow(    IMenuManager manager){
      actionGroup.fillContextMenu(manager);
    }
  }
);
  if (sessionSelectionMap.containsKey(tabData.session)) {
    tabData.treeViewer.setSelection(sessionSelectionMap.get(tabData.session));
    sessionSelectionMap.remove(tabData.session);
  }
}

Fixed version (ground truth):
/** 
 * Add a new session to the database structure view. This will create a new tab for the session.
 * @param session
 */
private void addSession(final MetaDataSession session) throws SQLCannotConnectException {
  if (_allSessions.contains(session))   return;
  try {
    session.getMetaData();
    session.setAutoCommit(true);
  }
 catch (  SQLCannotConnectException e) {
    SQLExplorerPlugin.error(e);
    throw e;
  }
catch (  SQLException e) {
    SQLExplorerPlugin.error(e);
    MessageDialog.openError(getSite().getShell(),"String_Node_Str",e.getMessage());
  }
  DatabaseNode rootNode=session.getRoot();
  if (rootNode == null)   return;
  _allSessions.add(session);
  if (_filterAction != null) {
    _filterAction.setEnabled(true);
  }
  if (_tabFolder == null || _tabFolder.isDisposed()) {
    clearParent();
    _tabFolder=new CTabFolder(_parent,SWT.TOP | SWT.CLOSE);
    _tabFolder.addSelectionListener(new SelectionAdapter(){
      public void widgetSelected(      SelectionEvent e){
        DatabaseDetailView detailView=(DatabaseDetailView)getSite().getPage().findView(SqlexplorerViewConstants.SQLEXPLORER_DBDETAIL);
        synchronizeDetailView(detailView);
      }
    }
);
    Display display=getSite().getShell().getDisplay();
    _tabFolder.setSelectionBackground(new Color[]{display.getSystemColor(SWT.COLOR_WHITE),new Color(null,211,225,250),new Color(null,175,201,246),IConstants.TAB_BORDER_COLOR},new int[]{25,50,75},true);
    _tabFolder.addCTabFolder2Listener(new CTabFolder2Adapter(){
      public void close(      CTabFolderEvent event){
        CTabItem tabItem=(CTabItem)event.item;
        TabData tabData=(TabData)tabItem.getData();
        _allSessions.remove(tabData.session);
        event.doit=true;
      }
    }
);
    _parent.layout();
    _parent.redraw();
  }
  final CTabItem tabItem=new CTabItem(_tabFolder,SWT.NULL);
  TabData tabData=new TabData();
  tabItem.setData(tabData);
  tabData.session=session;
  String labelText=session.getUser().getDescription();
  tabItem.setText(labelText);
  Composite composite=new Composite(_tabFolder,SWT.NULL);
  composite.setLayout(new FillLayout());
  tabItem.setControl(composite);
  final TreeViewer treeViewer=new TreeViewer(composite,SWT.V_SCROLL | SWT.H_SCROLL | SWT.MULTI| SWT.BORDER);
  tabData.treeViewer=treeViewer;
  Transfer[] transfers=new Transfer[]{TableNodeTransfer.getInstance()};
  treeViewer.addDragSupport(DND.DROP_COPY,transfers,new DragSourceListener(){
    public void dragFinished(    DragSourceEvent event){
      System.out.println("String_Node_Str");
      TableNodeTransfer.getInstance().setSelection(null);
    }
    public void dragSetData(    DragSourceEvent event){
      Object sel=((IStructuredSelection)treeViewer.getSelection()).getFirstElement();
      event.data=sel;
    }
    public void dragStart(    DragSourceEvent event){
      event.doit=!treeViewer.getSelection().isEmpty();
      if (event.doit) {
        Object sel=((IStructuredSelection)treeViewer.getSelection()).getFirstElement();
        if (!(sel instanceof TableNode)) {
          event.doit=false;
        }
 else {
          TableNode tn=(TableNode)sel;
          TableNodeTransfer.getInstance().setSelection(tn);
          if (!tn.isTable())           event.doit=false;
        }
      }
    }
  }
);
  treeViewer.setUseHashlookup(true);
  treeViewer.setContentProvider(new DBTreeContentProvider());
  treeViewer.setLabelProvider(new DBTreeLabelProvider());
  treeViewer.setInput(rootNode);
  treeViewer.addSelectionChangedListener(new ISelectionChangedListener(){
    public void selectionChanged(    SelectionChangedEvent ev){
      DatabaseDetailView detailView=(DatabaseDetailView)getSite().getPage().findView(SqlexplorerViewConstants.SQLEXPLORER_DBDETAIL);
      synchronizeDetailView(detailView);
    }
  }
);
  treeViewer.addDoubleClickListener(new IDoubleClickListener(){
    public void doubleClick(    DoubleClickEvent event){
      try {
        DatabaseDetailView detailView=(DatabaseDetailView)getSite().getPage().findView(SqlexplorerViewConstants.SQLEXPLORER_DBDETAIL);
        if (detailView == null) {
          getSite().getPage().showView(SqlexplorerViewConstants.SQLEXPLORER_DBDETAIL);
        }
        getSite().getPage().bringToTop(detailView);
        synchronizeDetailView(detailView);
      }
 catch (      Exception e) {
      }
    }
  }
);
  treeViewer.addTreeListener(new ITreeViewerListener(){
    public void treeCollapsed(    TreeExpansionEvent event){
      INode node=(INode)event.getElement();
      node.setExpanded(false);
      TreeViewer viewer=(TreeViewer)event.getSource();
      viewer.update(node,null);
    }
    public void treeExpanded(    TreeExpansionEvent event){
      INode node=(INode)event.getElement();
      node.setExpanded(true);
      TreeViewer viewer=(TreeViewer)event.getSource();
      viewer.update(node,null);
    }
  }
);
  _tabFolder.setSelection(_tabFolder.getItemCount() - 1);
  DatabaseDetailView detailView=(DatabaseDetailView)getSite().getPage().findView(SqlexplorerViewConstants.SQLEXPLORER_DBDETAIL);
  if (detailView != null) {
    synchronizeDetailView(detailView);
    getSite().getPage().bringToTop(detailView);
  }
  composite.layout();
  _tabFolder.layout();
  _tabFolder.redraw();
  getSite().getPage().bringToTop(this);
  final DBTreeActionGroup actionGroup=new DBTreeActionGroup(treeViewer);
  MenuManager menuManager=new MenuManager("String_Node_Str");
  menuManager.setRemoveAllWhenShown(true);
  Menu contextMenu=menuManager.createContextMenu(treeViewer.getTree());
  treeViewer.getTree().setMenu(contextMenu);
  menuManager.addMenuListener(new IMenuListener(){
    public void menuAboutToShow(    IMenuManager manager){
      actionGroup.fillContextMenu(manager);
    }
  }
);
  if (sessionSelectionMap.containsKey(tabData.session)) {
    tabData.treeViewer.setSelection(sessionSelectionMap.get(tabData.session));
    sessionSelectionMap.remove(tabData.session);
    _allSessions.remove(tabData.session);
  }
}