|
| 1 | +package edofro.pseudofreeplaneapi |
| 2 | + |
| 3 | +//region: imports |
| 4 | + |
| 5 | +import org.freeplane.api.MindMap as ApiMindMap |
| 6 | +//import org.freeplane.api.Node as ProxyNode |
| 7 | +//import org.freeplane.plugin.script.proxy.Proxy.Node as ProxyNode |
| 8 | +import org.freeplane.plugin.script.proxy.NodeProxy as ProxyNode |
| 9 | +import org.freeplane.core.ui.components.UITools as ui |
| 10 | +import org.freeplane.features.map.MapModel; |
| 11 | +import org.freeplane.features.map.NodeModel; |
| 12 | +import org.freeplane.features.mode.Controller; |
| 13 | +import org.freeplane.features.styles.MapStyleModel; |
| 14 | +import org.freeplane.plugin.script.ScriptContext |
| 15 | +import org.freeplane.plugin.script.proxy.ScriptUtils |
| 16 | + |
| 17 | + |
| 18 | +class UserStyles { |
| 19 | + |
| 20 | +//region: properties |
| 21 | + |
| 22 | +//end: |
| 23 | + |
| 24 | +//region: copyUserStyles |
| 25 | + |
| 26 | + def static copyUserStyles(sourceMap, targetMap, boolean showMessage, groovy.lang.Closure closure){ |
| 27 | + def stylesToImport = getUserDefinedStylesParentNode(sourceMap).children.findAll(closure) |
| 28 | + def styleNamesToImport = stylesToImport*.text |
| 29 | + def texto = new StringBuilder("The following styles were imported \n from map '${sourceMap.name}.mm' \n into current map '${targetMap.name}.mm':\n\n") |
| 30 | + styleNamesToImport.each{styleName -> |
| 31 | + targetMap.copyStyleFrom(sourceMap, styleName) |
| 32 | + def sourceStyleNode = getUserStyleNode(sourceMap, styleName ) |
| 33 | + def targetStyleNode = getUserStyleNode(targetMap, styleName ) |
| 34 | + copyIcons(sourceStyleNode, targetStyleNode) |
| 35 | + copyAttributes(sourceStyleNode, targetStyleNode) |
| 36 | + texto << " - $styleName\n" |
| 37 | + } |
| 38 | + texto << "\n\n" |
| 39 | + if(showMessage) ui.informationMessage(texto.toString()) |
| 40 | + } |
| 41 | + |
| 42 | + def static copyUserStyles(sourceMap, targetMap, boolean showMessage = true){ |
| 43 | + copyUserStyles(sourceMap, targetMap, showMessage, {true}) |
| 44 | + } |
| 45 | + |
| 46 | + def static copyUserStyles(sourceMap, targetMap, groovy.lang.Closure closure){ |
| 47 | + copyUserStyles(sourceMap, targetMap, true, closure) |
| 48 | + } |
| 49 | + |
| 50 | + def static copyUserStyles(sourceMap, targetMap, String[] lista){ |
| 51 | + copyUserStyles(sourceMap, targetMap, true, lista) |
| 52 | + } |
| 53 | + |
| 54 | + def static copyUserStyles(sourceMap, targetMap, boolean showMessage, String[] lista){ |
| 55 | + def closure = {it.text in lista} |
| 56 | + copyUserStyles(sourceMap, targetMap, showMessage, closure) |
| 57 | + } |
| 58 | + |
| 59 | + def static copyUserStyles(sourceMap, targetMap, String texto){ |
| 60 | + copyUserStyles(sourceMap, targetMap, true, texto) |
| 61 | + } |
| 62 | + |
| 63 | + def static copyUserStyles(sourceMap, targetMap, boolean showMessage, String texto){ |
| 64 | + def closure = {it.text == texto} |
| 65 | + copyUserStyles(sourceMap, targetMap, showMessage, closure) |
| 66 | + } |
| 67 | + |
| 68 | +//end: |
| 69 | + |
| 70 | +//region: copy other things from node to node |
| 71 | + |
| 72 | + def static copyIcons(sourceNode, targetNode, boolean doClear = true){ |
| 73 | + if(doClear) targetNode.icons.clear() |
| 74 | + targetNode.icons.addAll(sourceNode.icons.icons) |
| 75 | + } |
| 76 | + |
| 77 | + def static copyAttributes(sourceNode, targetNode, boolean doClear = true){ |
| 78 | + if(doClear) targetNode.attributes.clear() |
| 79 | + sourceNode.attributes.each{a -> |
| 80 | + targetNode.attributes.add(a.key, a.value) |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | +//end: |
| 85 | + |
| 86 | +//region: getting an UserStyleNode as ProxyNode from active map |
| 87 | + |
| 88 | +// public |
| 89 | + def static getUserStyleNode( String userStyle ){ |
| 90 | + return getUserStyleNode( null, userStyle ) |
| 91 | + } |
| 92 | + |
| 93 | + def static getUserStyleNode(ApiMindMap mapaProxy, String userStyle ){ |
| 94 | + return getUserDefinedStylesParentNode(mapaProxy).children.find{it.text == userStyle} |
| 95 | + } |
| 96 | + |
| 97 | + |
| 98 | + def static getUserDefinedStylesParentNode(x = null){ |
| 99 | + return getUserDefinedStylesParentNode((ScriptContext) null) |
| 100 | + } |
| 101 | + |
| 102 | + def static getUserDefinedStylesParentNode(MapModel mapa){ |
| 103 | + return getUserDefinedStylesParentNode(mapa, null) |
| 104 | + } |
| 105 | + |
| 106 | + def static getUserDefinedStylesParentNode(ApiMindMap mapaProxy){ |
| 107 | + return getUserDefinedStylesParentNode(mapaProxy.delegate, null) |
| 108 | + } |
| 109 | + |
| 110 | + |
| 111 | + def static getUserDefinedStylesParentNode(ScriptContext scriptContext){ |
| 112 | + MapModel mapa = Controller.getCurrentController().getMap(); |
| 113 | + return getUserDefinedStylesParentNode(mapa, scriptContext) |
| 114 | + } |
| 115 | + |
| 116 | + def static getUserDefinedStylesParentNode(ApiMindMap mapaProxy, ScriptContext scriptContext){ |
| 117 | + return getUserDefinedStylesParentNode(mapaProxy.delegate, scriptContext) |
| 118 | + } |
| 119 | + |
| 120 | + def static getUserDefinedStylesParentNode(MapModel mapa, ScriptContext scriptContext){ |
| 121 | + if(!mapa) { |
| 122 | + return getUserDefinedStylesParentNode(scriptContext) |
| 123 | + } |
| 124 | + MapStyleModel styleModel = MapStyleModel.getExtension(mapa); |
| 125 | + MapModel styleMap = styleModel.getStyleMap(); |
| 126 | + NodeModel userStyleParentNode = styleModel.getStyleNodeGroup(styleMap, MapStyleModel.STYLES_USER_DEFINED); |
| 127 | + def userDefinedParentNode = new ProxyNode(userStyleParentNode, scriptContext) |
| 128 | + return userDefinedParentNode |
| 129 | + } |
| 130 | + |
| 131 | +//end: |
| 132 | + |
| 133 | +} |
| 134 | + |
0 commit comments