openEditDialog comboRef detailsCombo detailsListRef inputBuffer  = do
    (selList,_,_) <- readIORef detailsListRef
    let len = length selList
    if len < 1 then
        runMsgDialog Nothing "There is nothing in the list to edit" Info
    else
        G.dialogNew >>= \dia -> do
            if len > 4 then G.widgetSetSizeRequest dia (-1) 250 else return ()
            deleteRef <- newIORef ([],True)
            table <- G.tableNew len 1 True
            -- add the table to a scrolled window
            selectedScrollWin <- vScrolledWinWithWgtList [table] VScroll
            -- pack stuff to the table let
            let selectedList = map display selList
            cbList <- mapM  G.checkButtonNewWithLabel  selectedList
            mapM_ (handleForCbuttons deleteRef) (zip selectedList cbList)
            let list = zip cbList [0..]
            mapM_ (\(bu, x) -> G.tableAttachDefaults table bu 0 1 x (x+1)) list
            -- add the scrolled window to the upper part of the dailog
            G.dialogGetUpper dia >>= \vbox -> do
                instructionLabel <- G.labelNew (Just " Select Conditions to remove  " )
                G.boxPackStart vbox instructionLabel G.PackNatural 3
                G.boxPackStart vbox selectedScrollWin G.PackGrow 2
            G.dialogGetActionArea dia >>= \hbox -> -- do
                mapM G.buttonNewWithLabel ["Select All", "Remove Selected"] >>=
                    \buttons -> do
                        G.on (buttons !! 0) G.buttonActivated $ do
                            mapM_ (\cb -> G.toggleButtonSetActive cb True) cbList
                        G.on (buttons !! 1) G.buttonActivated $ do
                            readIORef  deleteRef >>= \(toRemoveList,clear) -> do -- inputBuffer
                                if clear then do
                                    --mapM_ print toRemoveList
                                    if null toRemoveList then
                                        return () -- do nothing detailsListRef inputBuffer
                                    else do
                                        G.textBufferSetText inputBuffer ""
                                            --
                                        let selListNew  =  removeSel display toRemoveList selList
                                            --
                                        G.textBufferGetEndIter inputBuffer >>= \iter -> do
                                            let toBuffer = G.textBufferInsert inputBuffer iter
                                            mapM_ (\st -> toBuffer (display st ++"\n")) selListNew
                                        modifyIORef detailsListRef (\(_,xs,ys) -> (selListNew ,xs,ys))
                                --
                                else do
                                        G.textBufferSetText inputBuffer ""
                                        --clearComboList  comboRef detailsCombo
                                        modifyIORef detailsListRef (\(_,xs,ys) -> ([],xs,ys))
                            --
                            G.widgetDestroy dia
                        --
                        mapM_ (\b -> G.boxPackEnd hbox  b G.PackNatural 1) buttons
                --
            G.widgetShowAll dia
    where
        removeSel f [] ys     = ys
        removeSel f (x:xs) ys = removeSel f xs (filter ((x /=) . f ) ys)
        --
        handleForCbuttons delRef (string , cb) =
          G.on cb G.toggled $  do
            G.toggleButtonGetActive cb >>= \isSelected ->
                if isSelected then do
                    modifyIORef delRef (\(xs,_) -> (string : xs,True))
                else
                    modifyIORef delRef (\(xs,_) ->
                                    let ys = delete string xs in
                                        (ys, not $ null ys))

----------------------------------------------------------------------------------------------
-- set preview display
openEditDialog comboRef detailsCombo detailsListRef inputBuffer  = do
    (selList,_,_) <- readIORef detailsListRef
    let len = length selList
    if len < 1 then
        runMsgDialog Nothing "There is nothing in the list to edit" Info
    else
        G.dialogNew >>= \dia -> do
            if len > 4 then G.widgetSetSizeRequest dia (-1) 250 else return ()
            deleteRef <- newIORef ([],True)
            table <- G.tableNew len 1 True
            -- add the table to a scrolled window
            selectedScrollWin <- vScrolledWinWithWgtList [table] VScroll
            -- pack stuff to the table let
            let selectedList = map display selList
            cbList <- mapM  G.checkButtonNewWithLabel  selectedList
            mapM_ (handleForCbuttons deleteRef) (zip selectedList cbList)
            let list = zip cbList [0..]
            mapM_ (\(bu, x) -> G.tableAttachDefaults table bu 0 1 x (x+1)) list
            -- add the scrolled window to the upper part of the dailog
            G.dialogGetUpper dia >>= \vbox -> do
                instructionLabel <- G.labelNew (Just " Select Conditions to remove  " )
                G.boxPackStart vbox instructionLabel G.PackNatural 3
                G.boxPackStart vbox selectedScrollWin G.PackGrow 2
            G.dialogGetActionArea dia >>= \hbox -> -- do
                mapM G.buttonNewWithLabel ["Select All", "Remove Selected"] >>=
                    \buttons -> do
                        G.on (buttons !! 0) G.buttonActivated $ do
                            mapM_ (\cb -> G.toggleButtonSetActive cb True) cbList
                        G.on (buttons !! 1) G.buttonActivated $ do
                            readIORef  deleteRef >>= \(toRemoveList,clear) -> do -- inputBuffer
                                if clear then do
                                    --mapM_ print toRemoveList
                                    if null toRemoveList then
                                        return () -- do nothing detailsListRef inputBuffer
                                    else do
                                        G.textBufferSetText inputBuffer ""
                                            --
                                        let selListNew  =  removeSel display toRemoveList selList
                                            --
                                        G.textBufferGetEndIter inputBuffer >>= \iter -> do
                                            let toBuffer = G.textBufferInsert inputBuffer iter
                                            mapM_ (\st -> toBuffer (display st ++"\n")) selListNew
                                        modifyIORef detailsListRef (\(_,xs,ys) -> (selListNew ,xs,ys))
                                --
                                else do
                                        G.textBufferSetText inputBuffer ""
                                        --clearComboList  comboRef detailsCombo
                                        modifyIORef detailsListRef (\(_,xs,ys) -> ([],xs,ys))
                            --
                            G.widgetDestroy dia
                        --
                        mapM_ (\b -> G.boxPackEnd hbox  b G.PackNatural 1) buttons
                --
            G.widgetShowAll dia
    where
        removeSel f [] ys     = ys
        removeSel f (x:xs) ys = removeSel f xs (filter ((x /=) . f ) ys)
        --
        handleForCbuttons delRef (string , cb) =
          G.on cb G.toggled $  do
            G.toggleButtonGetActive cb >>= \isSelected ->
                if isSelected then do
                    modifyIORef delRef (\(xs,_) -> (string : xs,True))
                else
                    modifyIORef delRef (\(xs,_) ->
                                    let ys = delete string xs in
                                        (ys, not $ null ys))

----------------------------------------------------------------------------------------------
-- set preview display
openEditDialog comboRef detailsCombo detailsListRef inputBuffer  = do
    (selList,_,_) <- readIORef detailsListRef
    let len = length selList
    if len < 1 then
        runMsgDialog Nothing "There is nothing in the list to edit" Info
    else
        G.dialogNew >>= \dia -> do
            if len > 4 then G.widgetSetSizeRequest dia (-1) 250 else return ()
            deleteRef <- newIORef ([],True)
            table <- G.tableNew len 1 True
            -- add the table to a scrolled window
            selectedScrollWin <- vScrolledWinWithWgtList [table] VScroll
            -- pack stuff to the table let
            let selectedList = map display selList
            cbList <- mapM  G.checkButtonNewWithLabel  selectedList
            mapM_ (handleForCbuttons deleteRef) (zip selectedList cbList)
            let list = zip cbList [0..]
            mapM_ (\(bu, x) -> G.tableAttachDefaults table bu 0 1 x (x+1)) list
            -- add the scrolled window to the upper part of the dailog
            G.dialogGetUpper dia >>= \vbox -> do
                instructionLabel <- G.labelNew (Just " Select Conditions to remove  " )
                G.boxPackStart vbox instructionLabel G.PackNatural 3
                G.boxPackStart vbox selectedScrollWin G.PackGrow 2
            G.dialogGetActionArea dia >>= \hbox -> -- do
                mapM G.buttonNewWithLabel ["Select All", "Remove Selected"] >>=
                    \buttons -> do
                        G.on (buttons !! 0) G.buttonActivated $ do
                            mapM_ (\cb -> G.toggleButtonSetActive cb True) cbList
                        G.on (buttons !! 1) G.buttonActivated $ do
                            readIORef  deleteRef >>= \(toRemoveList,clear) -> do -- inputBuffer
                                if clear then do
                                    --mapM_ print toRemoveList
                                    if null toRemoveList then
                                        return () -- do nothing detailsListRef inputBuffer
                                    else do
                                        G.textBufferSetText inputBuffer ""
                                            --
                                        let selListNew  =  removeSel display toRemoveList selList
                                            --
                                        G.textBufferGetEndIter inputBuffer >>= \iter -> do
                                            let toBuffer = G.textBufferInsert inputBuffer iter
                                            mapM_ (\st -> toBuffer (display st ++"\n")) selListNew
                                        modifyIORef detailsListRef (\(_,xs,ys) -> (selListNew ,xs,ys))
                                --
                                else do
                                        G.textBufferSetText inputBuffer ""
                                        --clearComboList  comboRef detailsCombo
                                        modifyIORef detailsListRef (\(_,xs,ys) -> ([],xs,ys))
                            --
                            G.widgetDestroy dia
                        --
                        mapM_ (\b -> G.boxPackEnd hbox  b G.PackNatural 1) buttons
                --
            G.widgetShowAll dia
    where
        removeSel f [] ys     = ys
        removeSel f (x:xs) ys = removeSel f xs (filter ((x /=) . f ) ys)
        --
        handleForCbuttons delRef (string , cb) =
          G.on cb G.toggled $  do
            G.toggleButtonGetActive cb >>= \isSelected ->
                if isSelected then do
                    modifyIORef delRef (\(xs,_) -> (string : xs,True))
                else
                    modifyIORef delRef (\(xs,_) ->
                                    let ys = delete string xs in
                                        (ys, not $ null ys))

----------------------------------------------------------------------------------------------
-- set preview display