Gridarta Editor
NamedObjectsUtils.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2023 The Gridarta Developers.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 package net.sf.gridarta.gui.data;
21 
22 import java.awt.Component;
23 import java.awt.Window;
24 import java.awt.event.MouseAdapter;
25 import java.awt.event.MouseEvent;
26 import javax.swing.JOptionPane;
27 import javax.swing.JScrollPane;
28 import javax.swing.JTree;
29 import javax.swing.tree.TreeNode;
30 import javax.swing.tree.TreePath;
35 import net.sf.japi.swing.action.ActionBuilder;
36 import net.sf.japi.swing.action.ActionBuilderFactory;
37 import org.jetbrains.annotations.NotNull;
38 import org.jetbrains.annotations.Nullable;
39 
46 public class NamedObjectsUtils {
47 
51  @NotNull
52  private static final ActionBuilder ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder("net.sf.gridarta");
53 
57  private NamedObjectsUtils() {
58  }
59 
70  @Nullable
71  public static <E extends NamedObject> String showNodeChooserDialog(@NotNull final Component parentComponent, @NotNull final String initial, @NotNull final FaceObjectProviders faceObjectProviders, @NotNull final NamedObjects<E> namedObjects) {
72  final NamedObject initialObject = namedObjects.get(initial);
73  final String initialPath = initialObject == null ? initial : initialObject.getPath();
74  final JTree tree = new JTree(namedObjects.getTreeRoot());
75  tree.setCellRenderer(new NamedNodeTreeCellRenderer(faceObjectProviders));
76  final JScrollPane scrollPane = new JScrollPane(tree);
77  tree.setExpandsSelectedPaths(true);
78  final TreePath path = namedObjects.getTreeRoot().getPathFor(initialPath);
79  tree.makeVisible(path);
80  tree.setSelectionPath(path);
81  tree.scrollPathToVisible(path);
82  final JOptionPane pane = new JOptionPane(scrollPane, JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null, null, null);
83  final Window dialog = pane.createDialog(parentComponent, ACTION_BUILDER.format("chooseNamedObject.title", namedObjects.getName()));
84  pane.selectInitialValue();
85  //noinspection RefusedBequest
86  tree.addMouseListener(new MouseAdapter() {
87 
88  @Override
89  public void mousePressed(final MouseEvent e) {
90  if (e.getClickCount() == 2) {
91  final TreePath selPath = tree.getPathForLocation(e.getX(), e.getY());
92  if (selPath != null) {
93  final TreeNode node = (TreeNode) selPath.getLastPathComponent();
94  if (node.isLeaf()) {
95  pane.setValue(0);
96  }
97  }
98  }
99  }
100 
101  });
102  dialog.setVisible(true);
103  dialog.dispose();
104  // This only looks inconvertible. getValue() returns an Integer here.
105  if (pane.getValue() == null || !pane.getValue().equals(0)) {
106  return null;
107  }
108 
109  final TreePath selectionPath = tree.getSelectionPath();
110  return selectionPath == null ? null : ((NamedTreeNode<?>) selectionPath.getLastPathComponent()).getName();
111  //return showConfirmDialog(parentComponent, scrollPane, "Choose a face", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE) == OK_OPTION && tree.getSelectionPath() != null ? ((NamedTreeNode<?>) tree.getSelectionPath().getLastPathComponent()).getName() : null;
112  }
113 
114 }
net.sf.gridarta
Base package of all Gridarta classes.
net.sf
net.sf.gridarta.model.face.FaceObjectProviders
Provider for faces of GameObjects and Archetypes.
Definition: FaceObjectProviders.java:46
net.sf.gridarta.model.data.NamedObject
An.
Definition: NamedObject.java:32
net.sf.gridarta.gui.data.NamedObjectsUtils.ACTION_BUILDER
static final ActionBuilder ACTION_BUILDER
The ActionBuilder.
Definition: NamedObjectsUtils.java:52
net
net.sf.gridarta.model.data.NamedTreeNode
TreeNode implementation for Named Objects (like arches, faces, animations, artifacts etc....
Definition: NamedTreeNode.java:40
net.sf.gridarta.gui.data.NamedObjectsUtils
Utility class for NamedObjects related functions.
Definition: NamedObjectsUtils.java:46
net.sf.gridarta.model.data
Classes for handling data that is organized in a tree.
Definition: AbstractNamedObject.java:20
net.sf.gridarta.gui.data.NamedObjectsUtils.showNodeChooserDialog
static< E extends NamedObject > String showNodeChooserDialog(@NotNull final Component parentComponent, @NotNull final String initial, @NotNull final FaceObjectProviders faceObjectProviders, @NotNull final NamedObjects< E > namedObjects)
Show a dialog for choosing from the object tree.
Definition: NamedObjectsUtils.java:71
net.sf.gridarta.model
net.sf.gridarta.model.face
The face is the appearance of an object.
Definition: AbstractFaceObjects.java:20
net.sf.gridarta.model.data.NamedObject.getPath
String getPath()
Get the path of this AbstractNamedObject.
net.sf.gridarta.gui.data.NamedObjectsUtils.NamedObjectsUtils
NamedObjectsUtils()
Private constructor to prevent instantiation.
Definition: NamedObjectsUtils.java:57
net.sf.gridarta.model.data.NamedObjects
This class manages NamedObjects, managing their tree as well as providing a method for showing a dial...
Definition: NamedObjects.java:33
net.sf.gridarta.gui.data.NamedNodeTreeCellRenderer
TreeCellRenderer for NamedTreeNodes in trees.
Definition: NamedNodeTreeCellRenderer.java:35