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-2015 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 ? initialObject.getPath() : initial;
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  if (selectionPath == null) {
111  return null;
112  }
113 
114  return ((NamedTreeNode<?>) selectionPath.getLastPathComponent()).getName();
115  //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;
116  }
117 
118 }
TreeCellRenderer for NamedTreeNodes in trees.
The data package contains classes for handling data that is organized in a tree.
This class manages NamedObjects, managing their tree as well as providing a method for showing a dial...
NamedObjectsUtils()
Private constructor to prevent instantiation.
Utility class for NamedObjects related functions.
Base package of all Gridarta classes.
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.
String getPath()
Get the path of this AbstractNamedObject.
Provider for faces of GameObjects and Archetypes.
The face is the appearance of an object.
static final ActionBuilder ACTION_BUILDER
The ActionBuilder.
TreeNode implementation for Named Objects (like arches, faces, animations, artifacts etc...