-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMindMapApp.java
307 lines (266 loc) · 10.8 KB
/
MindMapApp.java
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.w3c.dom.*;
class MindMapNode {
String text;
int x, y;
int width, height;
Color color;
boolean isSelected=false;
List<MindMapNode> children = new ArrayList<>();
MindMapNode(String text, int x, int y, Color color) {
this.text = text;
this.x = x;
this.y = y;
this.color = color;
adjustSize();
}
void adjustSize() {
int baseWidth = 100;
int baseHeight = 50;
width = Math.max(baseWidth, text.length() * 7);
height = baseHeight;
}
void setText(String text) {
this.text = text;
adjustSize();
}
boolean containsPoint(int x, int y) {
return x > this.x && x < this.x + width && y > this.y && y < this.y + height;
}
void addChild(MindMapNode child) {
children.add(child);
}
void toggleSelection() {
isSelected = !isSelected;
}
void setColor(Color color) {
this.color = color;
}
}
class MindMapSurface extends JPanel {
private List<MindMapNode> nodes = new ArrayList<>();
private MindMapNode selectedNode = null;
MindMapSurface() {
setComponentPopupMenu(createPopupMenu());
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
for (MindMapNode node : nodes) {
if (node.containsPoint(e.getX(), e.getY())) {
String newText = JOptionPane.showInputDialog(null, "Edit Node Text:", node.text);
if (newText != null && !newText.trim().isEmpty()) {
node.setText(newText);
repaint();
}
break;
}
}
}
}
@Override
public void mousePressed(MouseEvent e) {
if (SwingUtilities.isRightMouseButton(e)) {
for (MindMapNode node : nodes) {
if (node.containsPoint(e.getX(), e.getY())) {
selectedNode = node;
return;
}
}
} else if (SwingUtilities.isLeftMouseButton(e)) {
for (MindMapNode node : nodes) {
if (node.containsPoint(e.getX(), e.getY())) {
selectedNode = node;
return;
}
}
}
}
@Override
public void mouseReleased(MouseEvent e) {
if (e.isPopupTrigger()) {
selectedNode = null;
}
}
});
addMouseMotionListener(new MouseAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
if (selectedNode != null) {
selectedNode.x = e.getX() - selectedNode.width / 2;
selectedNode.y = e.getY() - selectedNode.height / 2;
repaint();
}
}
});
}
private JPopupMenu createPopupMenu() {
JPopupMenu menu = new JPopupMenu();
JMenuItem deleteItem = new JMenuItem("Delete Node");
deleteItem.addActionListener(e -> {
if (selectedNode != null) {
nodes.removeIf(n -> n == selectedNode || isDescendant(selectedNode, n));
repaint();
}
});
JMenuItem addChildItem = new JMenuItem("Add Child Node");
addChildItem.addActionListener(e -> {
if (selectedNode != null) {
String childText = JOptionPane.showInputDialog("Child Node Text:");
if (childText != null && !childText.trim().isEmpty()) {
MindMapNode child = new MindMapNode(childText, selectedNode.x + selectedNode.width + 10, selectedNode.y + selectedNode.height + 10, Color.LIGHT_GRAY);
selectedNode.addChild(child);
nodes.add(child);
repaint();
}
}
});
JMenuItem changeColorItem = new JMenuItem("Change Color");
changeColorItem.addActionListener(e -> {
if (selectedNode != null) {
Color newColor = JColorChooser.showDialog(null, "Choose a color", selectedNode.color);
if (newColor != null) {
selectedNode.setColor(newColor);
repaint();
}
}
});
JMenuItem exportToPNGItem = new JMenuItem("Export to PNG");
exportToPNGItem.addActionListener(e -> exportToPNG());
JMenuItem exportToXMLItem = new JMenuItem("Export to XML");
exportToXMLItem.addActionListener(e -> exportToXML());
menu.add(addChildItem);
menu.add(exportToXMLItem);
menu.add(deleteItem);
menu.add(changeColorItem);
menu.add(exportToPNGItem);
return menu;
}
boolean isDescendant(MindMapNode parent, MindMapNode potentialDescendant) {
if (parent.children.contains(potentialDescendant)) {
return true;
}
for (MindMapNode child : parent.children) {
if (isDescendant(child, potentialDescendant)) {
return true;
}
}
return false;
}
void addNode(MindMapNode node) {
nodes.add(node);
repaint();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
RenderingHints rh = new RenderingHints(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHints(rh);
// Before drawing, let's ensure we're only drawing connections for existing nodes
for (MindMapNode node : nodes) {
drawNode(g2d, node);
}
for (MindMapNode node : nodes) {
for (MindMapNode child : node.children) {
if (nodes.contains(child)) { // Draw connection only if the child is in the active nodes list
drawConnection(g2d, node.x + node.width / 2, node.y + node.height / 2, child.x + child.width / 2, child.y + child.height / 2);
}
}
}
}
private void drawNode(Graphics2D g2d, MindMapNode node) {
Ellipse2D shape = new Ellipse2D.Double(node.x, node.y, node.width, node.height);
g2d.setColor(node.color);
g2d.fill(shape);
g2d.setColor(Color.BLACK);
g2d.draw(shape);
drawCenteredText(g2d, node.text, node.x, node.y, node.width, node.height);
}
private void drawConnection(Graphics2D g2d, int x1, int y1, int x2, int y2) {
g2d.drawLine(x1, y1, x2, y2);
}
private void drawCenteredText(Graphics2D g2d, String text, int x, int y, int width, int height) {
FontMetrics fm = g2d.getFontMetrics();
int textWidth = fm.stringWidth(text);
int textX = x + (width - textWidth) / 2;
int textY = y + ((height - fm.getHeight()) / 2) + fm.getAscent();
g2d.drawString(text, textX, textY);
}
private void exportToXML() {
try {
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.newDocument();
Element rootElement = doc.createElement("MindMap");
doc.appendChild(rootElement);
for (MindMapNode node : nodes) {
Element nodeElement = doc.createElement("Node");
nodeElement.setAttribute("Text", node.text);
nodeElement.setAttribute("X", String.valueOf(node.x));
nodeElement.setAttribute("Y", String.valueOf(node.y));
nodeElement.setAttribute("Color", Integer.toHexString(node.color.getRGB() & 0xffffff)); // Mask to remove alpha bits
rootElement.appendChild(nodeElement);
}
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("MindMap.xml"));
transformer.transform(source, result);
JOptionPane.showMessageDialog(this, "Exported as XML successfully!", "Export", JOptionPane.INFORMATION_MESSAGE);
} catch (ParserConfigurationException | TransformerException pce) {
pce.printStackTrace();
JOptionPane.showMessageDialog(this, "Failed to export as XML.", "Export Error", JOptionPane.ERROR_MESSAGE);
}
}
private void exportToPNG() {
BufferedImage image = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = image.createGraphics();
paint(g2);
g2.dispose();
try {
ImageIO.write(image, "png", new File("MindMap.png"));
JOptionPane.showMessageDialog(this, "Exported as PNG successfully!", "Export", JOptionPane.INFORMATION_MESSAGE);
} catch (IOException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, "Failed to export as PNG.", "Export Error", JOptionPane.ERROR_MESSAGE);
}
}
}
public class MindMapApp extends JFrame {
public MindMapApp() {
initUI();
}
private void initUI() {
MindMapSurface surface = new MindMapSurface();
add(surface);
MindMapNode root = new MindMapNode("Root", 300, 200, Color.CYAN);
surface.addNode(root);
setTitle("Advanced Mind Map");
setSize(800, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
MindMapApp ex = new MindMapApp();
ex.setVisible(true);
});
}
}