-
Notifications
You must be signed in to change notification settings - Fork 325
/
ImageIndexEditor.java
347 lines (312 loc) · 13.1 KB
/
ImageIndexEditor.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package jmri.jmrit.catalog;
import java.awt.datatransfer.DataFlavor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.util.Enumeration;
import java.util.List;
import java.util.Set;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JSplitPane;
import javax.swing.JTree;
import javax.swing.SwingConstants;
import javax.swing.tree.*;
import jmri.CatalogTreeManager;
import jmri.InstanceInitializer;
import jmri.InstanceManager;
import jmri.implementation.AbstractInstanceInitializer;
import jmri.util.FileUtil;
import jmri.util.JmriJFrame;
import org.openide.util.lookup.ServiceProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* A JFrame for creating and editing an Image Index.
*
* @author Pete Cressman Copyright 2009
*/
public final class ImageIndexEditor extends JmriJFrame {
private CatalogPanel _catalog;
private CatalogPanel _index;
private JTree _indexTree;
public static final String IconDataFlavorMime = DataFlavor.javaJVMLocalObjectMimeType
+ ";class=jmri.jmrit.catalog.NamedIcon";
/**
* Ctor for an unnamed ImageIndexEditor.
*/
private ImageIndexEditor() {
super();
}
/**
* Ctor for a named ImageIndexEditor.
*
* @param name title to display on the editor frame
*/
private ImageIndexEditor(String name) {
super(name);
}
private void init() {
JMenuBar menuBar = new JMenuBar();
JMenu findIcon = new JMenu(Bundle.getMessage("MenuFile"));
menuBar.add(findIcon);
JMenuItem storeItem = new JMenuItem(Bundle.getMessage("MIStoreImageIndex"));
findIcon.add(storeItem);
storeItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
InstanceManager.getDefault(CatalogTreeManager.class).storeImageIndex();
}
});
findIcon.addSeparator();
JMenuItem openItem = new JMenuItem(Bundle.getMessage("openDirMenu"));
openItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
InstanceManager.getDefault(DirectorySearcher.class).openDirectory();
}
});
findIcon.add(openItem);
JMenuItem searchItem = new JMenuItem(Bundle.getMessage("searchFSMenu"));
searchItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
InstanceManager.getDefault(DirectorySearcher.class).searchFS();
}
});
findIcon.add(searchItem);
JMenu editMenu = new JMenu(Bundle.getMessage("MenuEdit"));
menuBar.add(editMenu);
addMenuItems(editMenu, null);
setJMenuBar(menuBar);
addHelpMenu("package.jmri.jmrit.catalog.ImageIndex", true);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
JPanel labelPanel = new JPanel();
labelPanel.add(new JLabel(Bundle.getMessage("dragIconsInstr"), SwingConstants.LEFT));
mainPanel.add(labelPanel);
JPanel catalogsPanel = new JPanel();
catalogsPanel.setLayout(new BoxLayout(catalogsPanel, BoxLayout.X_AXIS));
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
makeCatalogPanel(), makeIndexPanel());
splitPane.setContinuousLayout(true);
splitPane.setOneTouchExpandable(true);
catalogsPanel.add(splitPane);
mainPanel.add(catalogsPanel);
getContentPane().add(mainPanel);
addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(java.awt.event.WindowEvent e) {
InstanceManager.getDefault(DirectorySearcher.class).close();
}
});
setLocation(10, 200);
pack();
setVisible(true);
}
@SuppressWarnings("deprecation") // needs careful unwinding for Set operations, generics
private JPanel makeCatalogPanel() {
_catalog = new CatalogPanel("defaultCatalog", "selectNode", true); // make sure both these properties keys exist
// log.debug("init the new CatalogPanel for ImageIndexEditor.makeCatalogPanel()");
_catalog.init(false, true);
CatalogTreeManager manager = InstanceManager.getDefault(jmri.CatalogTreeManager.class);
List<String> sysNames = manager.getSystemNameList();
for (String systemName : sysNames) {
if (systemName.startsWith("IF")) {
_catalog.addTree(manager.getBySystemName(systemName));
// log.debug("added item to tree");
}
}
_catalog.createNewBranch("IFJAR", "Program Directory", "resources");
FileUtil.createDirectory(FileUtil.getUserFilesPath() + "resources");
_catalog.createNewBranch("IFPREF", "Preferences Directory", FileUtil.getUserFilesPath() + "resources");
return _catalog;
}
/*
* Provide node editing.
*
* @param evt mouse event providing x and y position in frame
*/
private void showTreePopUp(MouseEvent evt) {
int row = _indexTree.getRowForLocation(evt.getX(), evt.getY());
if (row <= 0) {
return;
}
TreePath path = _indexTree.getPathForLocation(evt.getX(), evt.getY());
String nodeName = path.getLastPathComponent().toString();
log.debug("showTreePopUp node= {}", nodeName);
JPopupMenu popup = new JPopupMenu();
popup.add(new JMenuItem(nodeName));
popup.add(new javax.swing.JPopupMenu.Separator());
addMenuItems(popup, (CatalogTreeNode) path.getLastPathComponent());
popup.show(evt.getComponent(), evt.getX(), evt.getY());
}
private void addMenuItems(javax.swing.JComponent editMenu, CatalogTreeNode node) {
JMenuItem addItem = new JMenuItem(Bundle.getMessage("addNode"));
addItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
addNode(node);
}
});
editMenu.add(addItem);
JMenuItem renameItem = new JMenuItem(Bundle.getMessage("renameNode"));
renameItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
renameNode(node);
}
});
editMenu.add(renameItem);
JMenuItem deleteItem = new JMenuItem(Bundle.getMessage("deleteNode"));
deleteItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
deleteNode(node);
}
});
editMenu.add(deleteItem);
}
@SuppressWarnings("deprecation") // needs careful unwinding for Set operations, generics
private JPanel makeIndexPanel() {
_index = new CatalogPanel("ImageIndex", "selectIndexNode", true); // make sure both these properties keys exist
// log.debug("init the new CatalogPanel for ImageIndexEditor.makeIndexPanel()");
// activate dragNdrop init(true,true) allows drop into same panel as the drag - i.e. duplicates icon in node
_index.init(true, false);
boolean found = false;
CatalogTreeManager manager = InstanceManager.getDefault(jmri.CatalogTreeManager.class);
List<String> sysNames = manager.getSystemNameList();
for (String systemName : sysNames) {
if (systemName.startsWith("IX")) {
_index.addTree(manager.getBySystemName(systemName));
found = true;
}
}
if (!found) {
_index.createNewBranch("IXII", Bundle.getMessage("ImageIndexRoot"), "ImageIndexRoot");
}
_indexTree = _index.getTree();
_indexTree.setSelectionRow(0); // begin with ImageIndexRoot selected
_indexTree.addMouseListener(new java.awt.event.MouseAdapter () {
@Override
public void mouseReleased(MouseEvent e) {
if (e.isPopupTrigger() || e.isMetaDown() || e.isAltDown()) {
showTreePopUp(e);
}
}
});
return _index;
}
void addNode(CatalogTreeNode selectedNode) {
if (selectedNode == null) {
selectedNode = _index.getSelectedNode();
}
if (selectedNode == null) {
JOptionPane.showMessageDialog(this, Bundle.getMessage("selectAddNode"),
Bundle.getMessage("MessageTitle"), JOptionPane.INFORMATION_MESSAGE);
} else {
String name = JOptionPane.showInputDialog(this, Bundle.getMessage("nameAddNode"),
Bundle.getMessage("QuestionTitle"), JOptionPane.QUESTION_MESSAGE);
if (name != null) {
if (!_index.insertNodeIntoModel(name, selectedNode)) {
JOptionPane.showMessageDialog(this, Bundle.getMessage("duplicateNodeName", name),
Bundle.getMessage("ErrorTitle"), JOptionPane.ERROR_MESSAGE);
}
}
}
invalidate();
}
private void renameNode(CatalogTreeNode selectedNode) {
if (selectedNode == null) {
selectedNode = _index.getSelectedNode();
}
if (selectedNode == null) {
JOptionPane.showMessageDialog(this, Bundle.getMessage("selectRenameNode"),
Bundle.getMessage("MessageTitle"), JOptionPane.INFORMATION_MESSAGE);
} else {
String name = JOptionPane.showInputDialog(this, Bundle.getMessage("newNameNode"),
selectedNode.getUserObject());
if (name != null) {
if (!_index.renameNode(selectedNode, name)) {
JOptionPane.showMessageDialog(this, Bundle.getMessage("duplicateNodeName", name),
Bundle.getMessage("ErrorTitle"), JOptionPane.ERROR_MESSAGE);
}
}
}
invalidate();
}
private void deleteNode(CatalogTreeNode selectedNode) {
if (selectedNode == null) {
selectedNode = _index.getSelectedNode();
}
if (selectedNode == null) {
JOptionPane.showMessageDialog(this, Bundle.getMessage("selectDeleteNode"),
Bundle.getMessage("MessageTitle"), JOptionPane.INFORMATION_MESSAGE);
return;
}
if (log.isDebugEnabled()) {
log.debug("delete node \"{}\" level= {}.", selectedNode, selectedNode.getLevel());
}
if (selectedNode.getLevel() <= 1) {
JOptionPane.showMessageDialog(this, Bundle.getMessage("deleteRootNode"),
Bundle.getMessage("MessageTitle"), JOptionPane.INFORMATION_MESSAGE);
return;
} else {
int numNodes = countSubNodes(selectedNode);
int numIcons = countIcons(selectedNode);
int response = JOptionPane.showConfirmDialog(this,
Bundle.getMessage("confirmDeleteNode", selectedNode.getUserObject(), numNodes, numIcons),
Bundle.getMessage("QuestionTitle"), JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);
if (response == JOptionPane.YES_OPTION) {
CatalogTreeNode parent = (CatalogTreeNode)selectedNode.getParent();
_index.removeNodeFromModel(selectedNode);
_index.setSelectedNode(parent);
}
}
}
int countSubNodes(CatalogTreeNode node) {
int cnt = 0;
Enumeration<TreeNode> e = node.children();
while (e.hasMoreElements()) {
CatalogTreeNode n = (CatalogTreeNode)e.nextElement();
cnt += countSubNodes(n) + 1;
}
return cnt;
}
private int countIcons(CatalogTreeNode node) {
int cnt = 0;
Enumeration<TreeNode> e = node.children();
while (e.hasMoreElements()) {
CatalogTreeNode n =(CatalogTreeNode)e.nextElement();
cnt += countIcons(n);
}
cnt += node.getNumLeaves();
return cnt;
}
@ServiceProvider(service = InstanceInitializer.class)
public static class Initializer extends AbstractInstanceInitializer {
@Override
public <T> Object getDefault(Class<T> type) {
if (type.equals(ImageIndexEditor.class)) {
ImageIndexEditor instance = new ImageIndexEditor(Bundle.getMessage("editIndexFrame"));
instance.init();
return instance;
}
return super.getDefault(type);
}
@Override
public Set<Class<?>> getInitalizes() {
Set<Class<?>> set = super.getInitalizes();
set.add(ImageIndexEditor.class);
return set;
}
}
private final static Logger log = LoggerFactory.getLogger(ImageIndexEditor.class);
}