-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModifyTagsButton.java
83 lines (78 loc) · 3.13 KB
/
ModifyTagsButton.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
package photo_renamer;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
/** A button used to view and edit a list of currently existing Tags. */
class ModifyTagsButton extends JButton implements ActionListener {
/** The serialVersionUID for this class. */
private static final long serialVersionUID = -9021117525598345699L;
/** the TagManager being modified. */
private final TagManager tagManager;
/**
* A button that can open a new window for modifying Tags.
*
* @param label the label of this button
* @param tagManager the TagManager managing the Tags
*/
ModifyTagsButton(String label, TagManager tagManager) {
super(label);
this.tagManager = tagManager;
this.addActionListener(this);
}
/**
* Handle ModifyTagsButton clicks.
*
* @param click the ActionEvent
*/
@Override
@SuppressWarnings("unchecked")
public void actionPerformed(ActionEvent click) {
// Create the window to display the tag modifier in.
JFrame modifierWindow = new JFrame("Modify Tags Database");
// Create a list model to populate the list of Tags.
DefaultListModel<Tag> tagModel = new DefaultListModel<>();
tagManager.tags.forEach(tagModel::addElement);
JList<String> tagList = new JList(tagModel);
JScrollPane tagsPane = new JScrollPane(tagList);
// Create a text field for user entry of new Tags.
JTextField newTagField = new JTextField("Enter new tag...", 20);
newTagField.addActionListener(enter -> {
Tag newTag = new Tag(newTagField.getText());
if (!tagModel.contains(newTag)) {
tagModel.addElement(newTag);
newTagField.setText("");
try {
tagManager.addTag(newTag);
} catch (IOException e1) {
JOptionPane.showMessageDialog(new JFrame(), "Failed to write new tag!");
e1.printStackTrace();
}
}
});
// Create a button to remove tags from the list of Tags.
JButton removeTagButton = new JButton("Remove Tag");
removeTagButton.addActionListener(e13 -> {
int tagIndex = tagList.getSelectedIndex();
if (tagIndex >= 0) {
Tag toDelete = tagModel.getElementAt(tagIndex);
tagModel.removeElement(toDelete);
try {
tagManager.removeTag(toDelete);
} catch (IOException e1) {
JOptionPane.showMessageDialog(new JFrame(), "Failed to remove tag!");
e1.printStackTrace();
}
}
});
// Put all components of the modifier window together.
JPanel bottomRow = new JPanel();
bottomRow.add(newTagField);
bottomRow.add(removeTagButton);
modifierWindow.add(tagsPane, BorderLayout.NORTH);
modifierWindow.add(bottomRow, BorderLayout.SOUTH);
modifierWindow.pack();
modifierWindow.setVisible(true);
}
}