Skip to content

Commit

Permalink
restructuring in direction of proper generic types
Browse files Browse the repository at this point in the history
  • Loading branch information
Dierk Koenig committed Jul 4, 2015
1 parent 56d9ea0 commit dd98527
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 69 deletions.
26 changes: 14 additions & 12 deletions client/src/main/frege/org/frege/Application.fr
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ import org.frege.AllNodes

import fregefx.javafx.application.Platform
import fregefx.javafx.fxml.FXMLLoader
import fregefx.javafx.stage.Stage
import fregefx.javafx.scene.Scene
import fregefx.javafx.scene.Node
import fregefx.javafx.scene.Parent
import fregefx.javafx.event.Event
import fregefx.javafx.scene.input.KeyEvent
import fregefx.javafx.scene.input.KeyCode
import fregefx.javafx.scene.text.Font
import fregefx.javafx.scene.control.Button
--import fregefx.javafx.scene.control.TextArea -- is in AllNodes
import fregefx.javafx.scene.control.TextArea
import fregefx.javafx.scene.control.TextInputControl
import fregefx.javafx.stage.WindowEvent
import fregefx.javafx.event.EventHandler
Expand All @@ -37,9 +39,9 @@ showUI mConsole stage = do
stage.setTitle "Frege - purely functional programming for the JVM"
stage.setScene scene
Stage.show stage
inputArea <- locateTextField scene "#editor"
outputArea <- locateTextField scene "#repl"
historyLV <- locateListView scene "#historyList"
inputArea <- locateTextArea scene "#editor"
outputArea <- locateTextArea scene "#repl"
historyLV <- locateListView scene "#historyList"

items <- historyLV.getItems
items.add ":help"
Expand All @@ -52,7 +54,7 @@ showUI mConsole stage = do

sm <- historyLV.getSelectionModel
sip <- sm.selectedItemProperty
cl <- ChangeListener.new (\old \new -> inputArea.setText new)
cl <- OnChange.new $ \old \new -> inputArea.setText new
sip.addListener cl

mQueue <- newEmptyMVar
Expand All @@ -79,14 +81,14 @@ startRepl console = do
repl console env.{state=newState} -- first step in endless loop
return ()

onClosing :: MVar String -> IO KeyEventHandler
onClosing mQueue = WindowEventHandler.new handle where
onClosing :: MVar String -> IO (EventHandler t)
onClosing mQueue = OnEvent.new handle where
handle windowEvent = do
shutdown -- closing the executor service - just to be clean
mQueue.put ":q"

onKeyReleased :: MVar String -> Stage -> TextArea -> TextArea -> (ObservableList String) -> SelectionModel -> IO KeyEventHandler
onKeyReleased mQueue stage inputArea outputArea items historySelection = KeyEventHandler.new handleKeyEvent where
onKeyReleased :: MVar String -> Stage -> TextArea -> TextArea -> (ObservableList String) -> (SelectionModel t) -> IO (EventHandler KeyEvent)
onKeyReleased mQueue stage inputArea outputArea items historySelection = OnEvent.new handleKeyEvent where
-- handleKeyEvent :: KeyEvent -> IO ()
handleKeyEvent keyEvent = do
-- println . show =<< keyEvent.toString
Expand Down Expand Up @@ -118,7 +120,7 @@ textInsert inputArea text = do
pos <- inputArea.getCaretPosition
inputArea.insertText pos text

doExecute :: MVar String ->TextArea -> (ObservableList String) -> SelectionModel -> IO ()
doExecute :: MVar String ->TextArea -> (ObservableList String) -> (SelectionModel t) -> IO ()
doExecute mQueue inputArea items historySelection = do
pos <- inputArea.getCaretPosition -- when we get here, we have just entered a CR
scriptStr <- inputArea.getText
Expand Down Expand Up @@ -147,15 +149,15 @@ loadFile stage inputArea = do
path <- mIoFile.getPath
inputArea.setText (":l " ++ path)

selectPrevious :: (ObservableList e) -> SelectionModel -> IO ()
selectPrevious :: (ObservableList e) -> (SelectionModel t) -> IO ()
selectPrevious items historySelection = do
oldIndex <- historySelection.getSelectedIndex
listSize <- items.size
temp = oldIndex - 1
newIndex = if temp < 0 then listSize - 1 else temp
historySelection.clearAndSelect newIndex

selectNext :: (ObservableList e) -> SelectionModel -> IO ()
selectNext :: (ObservableList e) -> (SelectionModel t) -> IO ()
selectNext items historySelection = do
oldIndex <- historySelection.getSelectedIndex
listSize <- items.size
Expand Down
14 changes: 8 additions & 6 deletions fregeFX/src/main/frege/org/frege/JavaFX.fr
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,29 @@ import frege.Prelude hiding(ListView)

import org.frege.AllNodes
import fregefx.javafx.scene.Node
import fregefx.javafx.scene.Scene
import fregefx.javafx.scene.control.TextArea


data Classifier = mutable native org.frege.Classifier where
native classify org.frege.Classifier.classify :: Node -> IO (IllegalArgumentException | AllNodes)
native classify org.frege.Classifier.classify :: Node -> IO (IllegalArgumentException | AllNodes t)

locateTextField :: Scene -> String -> IO TextArea
locateTextField scene selector = do
locateTextArea :: Scene -> String -> IO TextArea
locateTextArea scene selector = do
allNode <- locate scene selector
case allNode of
TextArea ta -> return ta
_ -> error ("Node " ++ selector ++ " is not a TextField")
_ -> error ("Node " ++ selector ++ " is not a TextArea")

locateListView :: Scene -> String -> IO ListView
locateListView :: Scene -> String -> IO (ListView t)
locateListView scene selector = do
allNode <- locate scene selector
case allNode of
ListView lv -> return lv
_ -> error ("Node " ++ selector ++ " is not a ListView")


locate :: Scene -> String -> IO AllNodes
locate :: Scene -> String -> IO (AllNodes t)
locate scene selector = do
maybeNode <- Scene.lookup scene selector
case maybeNode of
Expand Down
2 changes: 1 addition & 1 deletion fregeFX/src/main/java/org/frege/Classifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.ListView;

public class Classifier {

static public org.frege.AllNodes.TAllNodes classify(Node n) throws IllegalArgumentException {
Expand Down
11 changes: 3 additions & 8 deletions preFregeFX/src/main/frege/fregefx/javafx/event/EventHandler.fr
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,7 @@ import fregefx.javafx.event.Event
data EventHandler t = mutable native javafx.event.EventHandler where
native handle :: EventHandler Event -> Event -> IO ()

-- specialized event handlers that are not available as such in Java
-- Generic implementation of the Event Handler Interface in Java to use from Frege

import fregefx.javafx.scene.input.KeyEvent
data KeyEventHandler = mutable native org.frege.FregeEventHandler where
native new :: (KeyEvent -> IO () ) -> IO KeyEventHandler

import fregefx.javafx.stage.WindowEvent
data WindowEventHandler = mutable native org.frege.FregeEventHandler where
native new :: (WindowEvent -> IO () ) -> IO WindowEventHandler
data OnEvent t = mutable native org.frege.FregeEventHandler where
native new :: (t -> IO () ) -> IO (OnEvent t)
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module fregefx.javafx.scene.control.TextArea where

import fregefx.javafx.event.EventHandler
import fregefx.javafx.scene.text.Font
import fregefx.javafx.scene.input.KeyEvent

data TextArea = mutable native javafx.scene.control.TextArea where

Expand Down Expand Up @@ -37,8 +38,8 @@ data TextArea = mutable native javafx.scene.control.TextArea where
-- native getCaretPosition :: TextArea -> IO Int
-- native insertText :: TextArea -> Int -> String -> IO ()
-- native appendText :: TextArea -> String -> IO()
native setOnKeyTyped :: TextArea -> KeyEventHandler -> IO () -- overriding Node
native setOnKeyReleased :: TextArea -> KeyEventHandler -> IO () -- overriding Node
native setOnKeyTyped :: TextArea -> (EventHandler KeyEvent) -> IO () -- overriding Node
native setOnKeyReleased :: TextArea -> (EventHandler KeyEvent) -> IO () -- overriding Node
native requestFocus :: TextArea -> IO ()
-- native getFont :: TextArea -> IO Font
-- native setFont :: TextArea -> Font -> IO ()
3 changes: 3 additions & 0 deletions preFregeFX/src/main/frege/fregefx/javafx/stage/Stage.fr
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module fregefx.javafx.stage.Stage where

import fregefx.javafx.scene.Scene
import fregefx.javafx.event.EventHandler
import fregefx.javafx.stage.WindowEvent

data Stage = mutable native javafx.stage.Stage where
native new :: () -> IO Stage
Expand Down Expand Up @@ -46,6 +48,7 @@ data Stage = mutable native javafx.stage.Stage where
native setMaximized :: Stage -> Bool -> IO ()
native setMinHeight :: Stage -> Double -> IO ()
native setMinWidth :: Stage -> Double -> IO ()
native setOnHidden :: Stage -> (EventHandler WindowEvent) -> IO ()
native setResizable :: Stage -> Bool -> IO ()
native setScene :: Stage -> Scene -> IO ()
native setTitle :: Stage -> String -> IO ()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module fregefx.javafx.stage.WindowEvent where

import fregefx.javafx.event.Event
import fregefx.javafx.event.EventHandler

data WindowEvent = mutable native javafx.stage.WindowEvent where

Expand Down
86 changes: 47 additions & 39 deletions preFregeFX/src/main/frege/org/frege/AllNodes.fr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module org.frege.AllNodes where
import frege.Prelude hiding(ListView)

import fregefx.javafx.application.Platform
--import fregefx.javafx.fxml.FXMLLoader
import fregefx.javafx.scene.Node
import fregefx.javafx.scene.Parent
import fregefx.javafx.scene.input.KeyEvent
Expand All @@ -14,55 +13,64 @@ import fregefx.javafx.stage.WindowEvent
import fregefx.javafx.event.EventHandler
import fregefx.javafx.collections.ObservableList
import fregefx.javafx.stage.Stage
--import fregefx.javafx.scene.Scene

import fregefx.javafx.scene.Scene

data FregeFX = mutable native org.frege.FregeFX where
native launch org.frege.FregeFX.launch :: ( Stage -> IO () ) -> IO ()
native fxml org.frege.FregeFX.fxml :: String -> String -> IO Parent


data ListView = mutable native javafx.scene.control.ListView where
native getItems :: ListView -> IO (ObservableList String)
native getSelectionModel :: ListView -> IO SelectionModel


data SelectionModel = mutable native javafx.scene.control.SelectionModel where
native selectedItemProperty :: SelectionModel -> IO ReadOnlyObjectProperty
native getSelectedIndex :: SelectionModel -> IO Int -- -1 if there is no selection
native clearAndSelect :: SelectionModel -> Int -> IO ()
native clearSelection :: SelectionModel -> IO ()

data ReadOnlyObjectProperty = mutable native javafx.beans.property.ReadOnlyObjectProperty where
native addListener :: ReadOnlyObjectProperty -> ChangeListener -> IO ()

data ChangeListener = mutable native org.frege.FregeChangeListener where
native new :: (String -> String -> IO()) -> IO ChangeListener -- make later polymorphic

data Scene = mutable native javafx.scene.Scene where
native new :: Parent -> Double -> Double -> IO Scene
native lookup :: Scene -> String -> IO (Maybe Node)
native setOnKeyTyped :: Scene -> KeyEventHandler -> IO ()
native setOnKeyReleased :: Scene -> KeyEventHandler -> IO ()

data Stage = mutable native javafx.stage.Stage where
native new :: () -> IO Stage
native show :: Stage -> IO ()
native setScene :: Stage -> Scene -> IO ()
native setTitle :: Stage -> String -> IO ()
native setOnHidden :: Stage -> WindowEventHandler -> IO ()
data ListView t = mutable native javafx.scene.control.ListView where
native getItems :: ListView t -> IO (ObservableList t)
native getSelectionModel :: ListView t -> IO (SelectionModel t)

data SelectionModel t = mutable native javafx.scene.control.SelectionModel where
native clearAndSelect :: SelectionModel t -> Int -> IO ()
native clearSelection :: SelectionModel t -> Int -> IO ()
| SelectionModel t -> IO ()
native getSelectedIndex :: SelectionModel t -> IO Int
native getSelectedItem :: SelectionModel t -> IO t
native isEmpty :: SelectionModel t -> IO Bool
native isSelected :: SelectionModel t -> Int -> IO Bool
native select :: SelectionModel t -> Int -> IO ()
| SelectionModel t -> t -> IO ()
native selectFirst :: SelectionModel t -> IO ()
native selectLast :: SelectionModel t -> IO ()
native selectNext :: SelectionModel t -> IO ()
native selectPrevious :: SelectionModel t -> IO ()
native selectedIndexProperty :: SelectionModel t -> IO ReadOnlyIntegerProperty
native selectedItemProperty :: SelectionModel t -> IO (ReadOnlyObjectProperty t)

data ReadOnlyObjectProperty t = mutable native javafx.beans.property.ReadOnlyObjectProperty where
native toString :: ReadOnlyObjectProperty t -> IO String

data ReadOnlyIntegerProperty = mutable native javafx.beans.property.ReadOnlyIntegerProperty where
native asObject :: ReadOnlyIntegerProperty -> IO (ReadOnlyObjectProperty Integer)
native readOnlyIntegerProperty "javafx.beans.property.ReadOnlyIntegerProperty.readOnlyIntegerProperty" :: ReadOnlyProperty t -> IO ReadOnlyIntegerProperty
native toString :: ReadOnlyIntegerProperty -> IO String

data ReadOnlyProperty t = mutable native javafx.beans.property.ReadOnlyProperty where
native getBean :: ReadOnlyProperty t -> IO Object
native getName :: ReadOnlyProperty t -> IO String

data OnChange t = mutable native org.frege.FregeChangeListener where
native new :: (t -> t -> IO()) -> IO (OnChange t)

data ObservableValue t = mutable native javafx.beans.value.ObservableValue where
native addListener :: ObservableValue t -> ChangeListener t -> IO ()
native getValue :: ObservableValue t -> IO t
native removeListener :: ObservableValue t -> ChangeListener t -> IO ()

data ChangeListener t = mutable native javafx.beans.value.ChangeListener where
native changed :: ChangeListener t -> ObservableValue t -> t -> t -> IO ()

data FileChooser = mutable native javafx.stage.FileChooser where
native new :: () -> IO FileChooser
native setTitle :: FileChooser -> String -> IO ()
native showOpenDialog :: FileChooser -> Stage -> IO (Maybe (MutableIO File))


data TextArea = mutable native javafx.scene.control.TextArea

data AllNodes = TextArea TextArea
| Button Button
| ListView ListView
data AllNodes t = TextArea TextArea
| Button Button
| ListView t



5 changes: 4 additions & 1 deletion preFregeFX/src/main/java/org/frege/FregeChangeListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ public FregeChangeListener(Lambda lambda) {
this.lambda = lambda;
}

/**
* @param observable It is by design that we do not make use of the of this object. It would introduce more coupling.
*/
@Override
public void changed(ObservableValue observable, Object oldValue, Object newValue) {
try {
Applicable inter = lambda.apply((String)oldValue).apply((String) newValue); // we only work with Strings for the moment
Applicable inter = lambda.apply(oldValue).apply(newValue);
Delayed.forced(inter.apply(null).result().forced()); // the second argument is the IO context
} catch(RuntimeException re) {
re.printStackTrace();
Expand Down

0 comments on commit dd98527

Please sign in to comment.