module GuiX2 where
import Data.List
import SlicingX
cons :: [String] -> String
cons [] = []
cons (e:t) = e++[' ']++(cons t)
-- String -> [(String,[String],Int,Int)]
-- Source level
guiFilter l filterD = foldl (||) False [elem ("\""++f++"\"") l |f<-filterD]
--(elem "\"setEnabled\"" l)
--(elem "\"JButton\"" l) ||
functionDefinition ast = sliceConstr ast "HsPatBind" 1
variablesDeclaration ast = sliceParam ast "HsPVar" 1 2 3
qualifiers ast = sliceParam ast "HsList" 1 2 1
guiVariablesDeclaration ast filterD = let l = variablesDeclaration ast
res = [(a,b,c,d)|(a,b,c,d)<-l,guiFilter (slicen (b!!0) "HsIdent") filterD]
in res
statements ast = let l = sliceParam ast "HsGenerator" 1 1 3
l1 = [(a,b,c,d)|(a,b,c,d)<-l,[1|(e,f,g,h)<-l,c<g, d>h]==[]]
in l1
guiStatements ast filterD = let l = statements ast
res = [(a,b,c,d)|(a,b,c,d)<-l,guiFilter (slicen (concat b) "HsIdent") filterD]
in res
guiStatementsIf ast = [("HsIf1",[(b!!0)] ++ [(b!!1)],c+(length $ b!!0),c+(length $ b!!0)+(length $ b!!1))|(a,b,c,d)<-sliceParam ast "HsIf" 1 2 3] ++ [("HsIf2",[(b!!0)] ++ [(b!!2)],c+(length $ b!!0),c+(length $ b!!0)+(length $ b!!1))|(a,b,c,d)<-sliceParam ast "HsIf" 1 2 3]
guiStatementsWithoutListeners ast filterD = let l = statements ast
res = [(a,b,c,d)|(a,b,c,d)<-l,guiFilter (slicen (concat b) "HsIdent") filterD]
jbn1 = [(a,[b!!0,b!!1,take 25 (b!!2)],c,d) |(a,b,c,d)<-res,elem "\"button\"" (slicen (concat b) "HsIdent")]
jbn2 = [(a,b,c,d) |(a,b,c,d)<-res,not (elem "\"button\"" (slicen (concat b) "HsIdent"))]
in jbn1 ++ jbn2
buttonStatements ast = let sa = statements ast
jbn = [(a,b,c,d) |(a,b,c,d)<-sa,elem "\"button\"" (slicen (concat b) "HsIdent")]
in jbn
buttonStatementsWithoutListeners ast = let sa = statements ast
--jbn = [(a,b,c,d) |(a,b,c,d)<-sa,elem "\"button\"" (slicen (concat b) "HsIdent")]
jbn1 = [(a,[b!!0,b!!1,take 25 (b!!2)],c,d) |(a,b,c,d)<-sa,elem "\"button\"" (slicen (concat b) "HsIdent")]
in jbn1
externalAllFunction ast = sliceParam ast "HsApp" 1 1 2
externalFunction ast = let l1 = externalAllFunction ast
l2 = buttonStatements ast
l3 = [(a,b,c,d)|(a,b,c,d)<-l1,[1|(e,f,g,h)<-l2, c>g, h>d]==[]]
in l3
functions ast = sliceConstr ast "HsPatBind" 1
function ast name = let l = sliceConstr ast "HsPatBind" 1
res = [(a,b,c,d)|(a,b,c,d)<-l ,(slice1 (b!!0) "HsIdent")==["\""++name++"\""]]
in head res
idfunctionsDefinition ast = let l = functions ast
res = [(head (slice1 (concat b) "HsIdent"),a,b,c,d) |(a,b,c,d)<-l]
in res
allGui ast filterD = guiVariablesDeclaration ast filterD++ guiStatementsWithoutListeners ast filterD ++ externalFunction ast ++ guiStatementsIf ast
allGuiWithListener ast filterD = guiVariablesDeclaration ast filterD++ guiStatements ast filterD ++ externalAllFunction ast ++ qualifiers ast ++ guiStatementsIf ast
-- código executado pelo metodo name excluindo listener (considerando filtro)
initState ast name filterD = let (a,b,c,d) = function ast name
s = executedCode (a,b,c,d) ast [(c,d)] filterD
in s
-- todo o código executado excluindo listener
executedCode (a,b,ind1,ind2) ast d1e1 filterD
= let al = allGui ast filterD
dl = [(a,b,c,d,d1e1) | (a,b,c,d) <- al, ind1 < c, d < ind2]
allCode = dl ++ functionInvocation dl ast filterD
guiCode = [(a,b,c,d,ef)|(a,b,c,d,ef)<-allCode,a/="HsApp",a/="HsPatBind",a/="HsPVar"]
in guiCode
functionInvocation l ast filterD = let idEmth = [(slicen (concat b) "HsIdent",a,b,c,d,ef) | (a,b,c,d,ef) <- l, a == "HsApp"]
icpd = idfunctionsDefinition ast
emth = [(b,c,d,e,g1h1 ++ [(d1,e1)])|(a,b,c,d,e)<-icpd,(a1,b1,c1,d1,e1,g1h1)<-idEmth,elem a a1]
in emth ++ (concat [executedCode (b,c,d,e) ast d1e1 filterD |(b,c,d,e,d1e1)<-emth])
-- código executado por cada listener do metodo name (considerando filtro)
eventsFromInitState ast name filterD
= let (a,b,c,d) = function ast name
s = allExecutedCode (a,b,c,d) ast [(c,d)] filterD
bt = buttonStatements ast
l1 = [(a,b,c,d,d1e1)|(a,b,c,d,d1e1)<-s,a=="HsApp"]--slice ast "Emth" 1 2
l = [(a,b,c,d,d1e1)|(a,b,c,d,d1e1)<-l1,[1|(e,f,g,h)<-bt,c>g,d<h]/=[]]
p = [((a,b,c,d,d1e1),executedCodeEvents (a,b,c,d) ast d1e1 filterD)|(a,b,c,d,d1e1)<-l]
in p
-- todo o código executado incluindo listener
-- (a,b,c,d) ast -1 -1
allExecutedCode (a,b,ind1,ind2) ast d1e1 filterD
= let al = allGuiWithListener ast filterD
dl = [(a,b,c,d,d1e1) | (a,b,c,d) <- al, ind1 < c, d < ind2]
allCode = dl ++ allFunctionInvocation dl ast filterD
-- guiCode = [(a,b,c,d,e,f)|(a,b,c,d,e,f)<-allCode,a/="Emth" && a/="Dmth"]
in allCode
allFunctionInvocation l ast filterD = let idEmth = [(slicen (concat b) "HsIdent",a,b,c,d,ef) | (a,b,c,d,ef) <- l, a == "HsApp"]
icpd = idfunctionsDefinition ast
emth = [(b,c,d,e,g1h1 ++ [(d1,e1)])|(a,b,c,d,e)<-icpd,(a1,b1,c1,d1,e1,g1h1)<-idEmth,elem a a1]
in emth ++ (concat [allExecutedCode (b,c,d,e) ast d1e1 filterD |(b,c,d,e,d1e1)<-emth])
--Assume-se não existir AddActionListener aninhados
--código executado a partir de um evento
executedCodeEvents (a,b,ind1,ind2) ast d1e1 filterD
= let al = allGuiWithListener ast filterD
dl = [(a,b,c,d,d1e1) | (a,b,c,d) <- al, ind1 <= c, d <= ind2]
allCode = dl ++ functionInvocationEvents dl ast filterD
guiCode = [(a,b,c,d,ef)|(a,b,c,d,ef)<-allCode,a/="HsApp"]
in guiCode
functionInvocationEvents l ast filterD = let idEmth = [(slicen (concat b) "HsIdent",a,b,c,d,ef) | (a,b,c,d,ef) <- l, a == "HsApp"]
icpd = idfunctionsDefinition ast
emth = [(b,c,d,e,g1h1 ++ [(d1,e1)])|(a,b,c,d,e)<-icpd,(a1,b1,c1,d1,e1,g1h1)<-idEmth,elem a a1]
in emth ++ (concat [executedCodeEvents (b,c,d,e) ast d1e1 filterD |(b,c,d,e,d1e1)<-emth])
{-
-- Gui level
jButtonStatements ast = let sa = statements ast
jbn = [(a,b,c,d)
|(a,b,c,d)<-sa,elem "button" (slice1 (b!!2) "HsIdent")]
in jbn
--Assume-se não existir AddActionListener aninhados
--código executado a partir de um evento
executedCodeEvents (a,b,ind1,ind2) ast d1e1 filterD
= let al = allGuiWithListener ast filterD
dl = [(a,b,c,d,d1e1) | (a,b,c,d) <- al, ind1 < c, d < ind2]
allCode = dl ++ methodInvocationEvents dl ast filterD
guiCode = [(a,b,c,d,ef)|(a,b,c,d,ef)<-allCode,a/="Emth"&&a/="Dmth" && a/="Dconst"]
in guiCode
methodInvocationEvents l ast filterD = let idEmth = [(slice1 (b!!0) "Ident",a,b,c,d,ef) | (a,b,c,d,ef) <- l, a == "Emth"]
icpd = idClassProcedureDefinition ast
emth = [(b,c,d,e,g1h1 ++ [(d1,e1)])|(a,b,c,d,e)<-icpd,(a1,b1,c1,d1,e1,g1h1)<-idEmth,a==a1]
in emth ++ (concat [executedCodeEvents (b,c,d,e) ast d1e1 filterD |(b,c,d,e,d1e1)<-emth])
-- ???
initAdd ast name filterD = let m = method ast name
s = executedCodeEvents m ast [] filterD
in s
-}
|