-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjustifyTopicAlignment.groovy
More file actions
94 lines (81 loc) · 3.28 KB
/
Copy pathjustifyTopicAlignment.groovy
File metadata and controls
94 lines (81 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright (C) 2026 euu2021 (Github)
// SPDX-License-Identifier: GPL-2.0-or-later
// Discussion thread: https://github.com/freeplane/freeplane/discussions/2270
// Version: 1.0
// @ExecutionModes({ON_SINGLE_NODE})
import org.freeplane.view.swing.map.NodeView
import org.freeplane.features.mode.Controller
import org.freeplane.features.map.INodeSelectionListener
import org.freeplane.features.map.NodeModel
import groovy.transform.Field
@Field NodeModel currentlySelectedNode = Controller.currentController.MapViewManager.mapView.mapSelection.selectionRoot
INodeSelectionListener mySelectionListener = new INodeSelectionListener() {
@Override
public void onDeselect(NodeModel node) {
}
@Override
public void onSelect(NodeModel node) {
if (node == currentlySelectedNode) {
return
}
currentlySelectedNode = node
//
// Configuration
//
def width_grid = 20 // Grid: increments of 20 pixels
def width_offset = 10 // Offset to be added
def zoomfactor = c.getZoom()
//
// Group nodes by level (only visible nodes)
//
def nodesByLevel = [:].withDefault { [] }
c.findAll().each { n ->
// Ignore nodes within folded branches (except the node itself)
def insideFoldedBranch = n.pathToRoot.dropRight(1).any { it.folded }
// def insideFoldedBranch = false
if (!insideFoldedBranch) {
def level = n.getNodeLevel(false)
nodesByLevel[level] << n
}
}
//
// Step 1: Reset minNodeWidth to -1 on all nodes,
// allowing them to return to their natural size
//
nodesByLevel.each { level, nodesInLevel ->
nodesInLevel.each { n ->
n.style.minNodeWidth = -1
}
}
// Force layout update
Controller.getCurrentController().getMap().rootNode.viewers[0].updateAll()
// Wait 200 milliseconds for the interface to update
Thread.sleep(200)
//
// Step 2: For each level, calculate the maximum natural width and apply the new minNodeWidth
//
nodesByLevel.each { level, nodesInLevel ->
def maxwidth = 0
// Iterate over the nodes at this level to determine the largest natural width
nodesInLevel.each { n ->
def nodeView = n.delegate.viewers.find { it instanceof NodeView }
// Use getPreferredSize() to obtain the natural width of the component
def width = nodeView?.mainView?.getPreferredSize()?.width ?: 0
// def width = nodeView?.mainView?.width ?: 0
if (width > maxwidth) {
maxwidth = width
}
}
// Adjust the value to the grid
def widthcompare = width_grid
while (maxwidth.div(zoomfactor) + width_offset > widthcompare) {
widthcompare += width_grid
}
// Apply the new minNodeWidth to all nodes at this level
nodesInLevel.each { n ->
n.style.minNodeWidth = widthcompare
}
}
}
}
Controller.currentController.modeController.mapController.addNodeSelectionListener(mySelectionListener)