neman / neman / core / docs / core.textile

Core functions and macros

Main part of neman.core library is => (builder) macro used for constructiong Java objects and there compositions. Usage of builder is similar to builtin doto macro and new special form with abbility to inject new methods in existing Java classes, add new constructors, override existing methods and (TODO).

There is no real injection of new methods and constructors, they are effective only inside builder macro.

This is how you would construct a SWING menu for your app using builder macro (configured with neman.swing):

 (ns user
   (:import (javax.swing JMenuBase JMenu JMenuItem JSeparator))
   (:require net.ksojat.neman.swing)
   (:use net.ksojat.neman.core))

 (def m
   (=> (JMenuBar.)
     [(=> [JMenu "&File"]
        [(=> [JMenuItem "&New [ctrl n]"])
         (=> [JMenuItem "&Open [ctrl o]"])
         (=> [JMenuItem "&Save [ctrl s]"]
           :Enabled false)
         (=> [JMenuItem "&Exit [ctrl q]"])])
       (=> [JMenu "&Edit"]
         [(=> [JMenuItem "Cu&t [ctrl x]"])
          (=> [JMenuItem "&Copy [ctrl c]"])
          (=> [JMenuItem "&Paste [ctrl v]"])])]))
p. Equivalent code using @doto@ and @new@ could be written as:
  (ns user
    (:import (javax.swing JMenuBar JMenu JMenuItem JSeparator KeyStroke))

  (def m
    (doto (JMenuBar.)
        (doto (JMenu. "File")
          (.setMnemonic "f")
            (doto (JMenuItem. "New")
              (.setMnemonic "n")
              (.setAccelerator (KeyStroke/getKeyStroke "ctrl n"))))
            (doto (JMenuItem. "Open")
              (.setMnemonic "o")
              (.setAccelerator (KeyStroke/getKeyStroke "ctrl o"))))
            (doto (JMenuItem. "Save")
              (.setEnabled false)
              (.setMnemonic "s")
              (.setAccelerator (KeyStroke/getKeyStroke "ctrl s"))))))
        (doto (JMenu. "Edit")
          (.setMnemonic "e")
            (doto (JMenuItem. "Cut")
              (.setMnemonic "t")
              (.setAccelerator (KeyStroke/getKeyStroke "ctrl x"))))
            (doto (JMenuItem. "Copy")
              (.setMnemonic "c")
              (.setAccelerator (KeyStroke/getKeyStroke "ctrl c"))))
            (doto (JMenuItem. "Paste")
              (.setMnemonic "p")
              (.setAccelerator (KeyStroke/getKeyStroke "ctrl v"))))))))