-
Notifications
You must be signed in to change notification settings - Fork 4
/
CategoryNode.java
154 lines (132 loc) · 5.66 KB
/
CategoryNode.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
package net.flinkgutt.samples.nodes.view.beantree;
import java.awt.event.ActionEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.AbstractAction;
import javax.swing.Action;
import static javax.swing.Action.NAME;
import net.flinkgutt.samples.nodes.api.ICategory;
import net.flinkgutt.samples.nodes.api.ICategoryDAO;
import org.openide.DialogDisplayer;
import org.openide.NotifyDescriptor;
import org.openide.nodes.AbstractNode;
import org.openide.nodes.Children;
import org.openide.util.Lookup;
import org.openide.util.NbBundle;
import org.openide.util.WeakListeners;
import org.openide.util.lookup.Lookups;
/**
*
* @author Christian
*/
public class CategoryNode extends AbstractNode implements PropertyChangeListener {
private ICategoryDAO categoryDAO = Lookup.getDefault().lookup(ICategoryDAO.class);
private ICategory category;
public CategoryNode(Children children) {
super(children);
}
public CategoryNode(ICategory category) {
super(Children.create(new CategoryChildFactory(category), true), Lookups.singleton(category));
this.category = category;
this.category.addPropertyChangeListener(WeakListeners.propertyChange(this, this.category));
}
@Override
public boolean canRename() {
return true;
}
@Override
public void setName(String newDisplayName) {
String oldDisplayName = category.getName();
category.setName(newDisplayName);
fireNameChange(oldDisplayName, newDisplayName);
}
@Override
public String getDisplayName() {
if (this.category == null) {
return "";
}
return this.category.getName();
}
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getPropertyName().equals("name")) {
fireDisplayNameChange(evt.getOldValue().toString(), evt.getNewValue().toString());
}
}
@Override
public Action[] getActions(boolean popup) {
if (category != null) {
return new Action[]{
new CreateCategoryAction(),
new RenameCategoryAction(),
new RemoveCategoryAction()
};
} else {
return new Action[]{};
}
}
private class CreateCategoryAction extends AbstractAction {
public CreateCategoryAction() {
putValue(NAME, NbBundle.getMessage(CategoryNode.class, "Action.createCategory.label"));
}
@Override
public void actionPerformed(ActionEvent ae) {
NotifyDescriptor.InputLine input = new NotifyDescriptor.InputLine(
NbBundle.getMessage(CategoryNode.class, "Action.createCategory.name.label"),
NbBundle.getMessage(CategoryNode.class, "Action.createCategory.title"));
Object result = DialogDisplayer.getDefault().notify(input);
if (result != NotifyDescriptor.OK_OPTION) {
return;
}
String categoryName = input.getInputText();
// TODO Simplify the API some here
ICategory newCat = categoryDAO.createCategory(category, categoryName);
categoryDAO.addCategory(newCat);
category.addChild(newCat);
}
}
private class RenameCategoryAction extends AbstractAction {
public RenameCategoryAction() {
putValue(NAME, NbBundle.getMessage(CategoryNode.class, "Action.renameCategory.label"));
}
@Override
public void actionPerformed(ActionEvent ae) {
NotifyDescriptor.InputLine input = new NotifyDescriptor.InputLine(
NbBundle.getMessage(CategoryNode.class, "Action.renameCategory.name.label"),
NbBundle.getMessage(CategoryNode.class, "Action.renameCategory.title"));
// Setting the inputfield to the current name
input.setInputText(category.getName());
Object result = DialogDisplayer.getDefault().notify(input);
if (result != NotifyDescriptor.OK_OPTION) {
return;
}
String oldName = category.getName();
category.setName(input.getInputText());
categoryDAO.update(category);
fireNameChange(oldName, input.getInputText());
}
}
private class RemoveCategoryAction extends AbstractAction {
public RemoveCategoryAction() {
putValue(NAME, NbBundle.getMessage(CategoryNode.class, "Action.removeCategory.label"));
}
@Override
public void actionPerformed(ActionEvent ae) {
NotifyDescriptor.Confirmation confirm = new NotifyDescriptor.Confirmation(
NbBundle.getMessage(CategoryNode.class, "Action.removeCategory.areyousure"),
NbBundle.getMessage(CategoryNode.class, "Action.removeCategory.title"),
NotifyDescriptor.YES_NO_OPTION);
Object result = DialogDisplayer.getDefault().notify(confirm);
if (result != NotifyDescriptor.YES_OPTION) {
return;
}
if (categoryDAO.hasChildren(category)) {
NotifyDescriptor.Message msg = new NotifyDescriptor.Message(NbBundle.getMessage(CategoryNode.class, "Action.removeCategory.notEmpty"),NotifyDescriptor.INFORMATION_MESSAGE);
DialogDisplayer.getDefault().notifyLater(msg);
} else {
category.getParent().removeChild(category);
categoryDAO.deleteCategory(category);
}
}
}
}