Created by marc on 3/24/15.
| 21 | import java.util.stream.Stream; |
| 22 | |
| 23 | /** |
| 24 | * Created by marc on 3/24/15. |
| 25 | */ |
| 26 | public class Templates extends Box implements IO.Loaded { |
| 27 | |
| 28 | static public final Dict.Prop<Box.BiFunctionOfBoxAnd<String, Box>> templateChild = new Dict.Prop<>("templateChild").toCanon() |
| 29 | .type() |
| 30 | .doc("`_.templateChild('template')` create a new box that's a child of this one, copied from 'template'"); |
| 31 | |
| 32 | static public final Dict.Prop<Box.TriFunctionOfBoxAnd<String, String, Box>> ensureChildTemplated = new Dict.Prop<>("ensureChildTemplated").toCanon() |
| 33 | .type() |
| 34 | .doc("`_.ensureChildTemplated('template', 'a')` create a new box that's a child of this one, copied from `template`, called `a`. If there's already something called `a`, just return that"); |
| 35 | |
| 36 | static public final Dict.Prop<Box.BiFunctionOfBoxAnd<String, Box>> saveAsTemplate = new Dict.Prop<>("saveAsTemplate").type() |
| 37 | .toCanon() |
| 38 | .doc("`_.saveAsTemplate('name')`. Save this box as a template called `name`"); |
| 39 | |
| 40 | private final Box root; |
| 41 | FileBrowser fileBrowser; |
| 42 | |
| 43 | public Templates(Box root) { |
| 44 | |
| 45 | this.root = root; |
| 46 | |
| 47 | properties.put(saveAsTemplate, (box, name) -> saveAsTemplate(Collections.singleton(box), name)); |
| 48 | |
| 49 | properties.put(templateChild, (of, name) -> { |
| 50 | |
| 51 | String path = fieldbox.FieldBox.fieldBox.io.findTemplateCalled(name); |
| 52 | |
| 53 | Set<Box> c = loadBox(path, of.properties.get(Box.frame) |
| 54 | .convert(0.9, 0.9)); |
| 55 | |
| 56 | c.forEach(cc -> IO.uniqifyIfNecessary(root, cc)); |
| 57 | |
| 58 | c.forEach(cc -> of.connect(cc)); |
| 59 | |
| 60 | return c.iterator().next(); |
| 61 | |
| 62 | }); |
| 63 | |
| 64 | QuoteCompletionHelpers.wrap(properties, templateChild, Box.BiFunctionOfBoxAnd.class, x -> { |
| 65 | |
| 66 | List<String> matches = fieldbox.FieldBox.fieldBox.io.findTemplateStartingWith(x); |
| 67 | |
| 68 | return matches.stream().map(y -> new Completion(-1, -1, y, "")).collect(Collectors.toList()); |
| 69 | |
| 70 | }); |
| 71 | |
| 72 | properties.put(ensureChildTemplated, (box, template, name) -> { |
| 73 | |
| 74 | Optional<Box> f = box.children() |
| 75 | .stream() |
| 76 | .filter(x -> x.properties.equals(Box.name, name)) |
| 77 | .findFirst(); |
| 78 | |
| 79 | return f.orElseGet(() -> { |
| 80 |