1 | package genius.gui.boaparties;
|
---|
2 |
|
---|
3 | import java.awt.BorderLayout;
|
---|
4 | import java.awt.event.ActionEvent;
|
---|
5 | import java.awt.event.MouseAdapter;
|
---|
6 | import java.awt.event.MouseEvent;
|
---|
7 |
|
---|
8 | import javax.swing.AbstractAction;
|
---|
9 | import javax.swing.JFrame;
|
---|
10 | import javax.swing.JMenuItem;
|
---|
11 | import javax.swing.JOptionPane;
|
---|
12 | import javax.swing.JPanel;
|
---|
13 | import javax.swing.JPopupMenu;
|
---|
14 | import javax.swing.JScrollPane;
|
---|
15 | import javax.swing.JTable;
|
---|
16 |
|
---|
17 | import genius.core.exceptions.InstantiateException;
|
---|
18 | import genius.core.repository.BoaPartyRepository;
|
---|
19 | import genius.core.repository.RepositoryFactory;
|
---|
20 | import genius.core.repository.boa.BoaPartyRepItem;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * A panel that shows available BOA parties and allows you to add/remove
|
---|
24 | *
|
---|
25 | */
|
---|
26 | @SuppressWarnings("serial")
|
---|
27 | public class BoaPartiesPanel extends JPanel {
|
---|
28 |
|
---|
29 | public BoaPartiesPanel() {
|
---|
30 | setLayout(new BorderLayout());
|
---|
31 | JTable table = new JTable(new BoaPartiesModel());
|
---|
32 | JScrollPane scrollpane = new JScrollPane(table);
|
---|
33 | add(scrollpane, BorderLayout.CENTER);
|
---|
34 |
|
---|
35 | PopClickListener popuplistener = new PopClickListener(table);
|
---|
36 | scrollpane.addMouseListener(popuplistener);
|
---|
37 | table.addMouseListener(popuplistener);
|
---|
38 | }
|
---|
39 |
|
---|
40 | public static void main(String[] args) {
|
---|
41 | JFrame frame = new JFrame();
|
---|
42 | frame.setLayout(new BorderLayout());
|
---|
43 | frame.getContentPane().add(new BoaPartiesPanel(), BorderLayout.CENTER);
|
---|
44 | frame.pack();
|
---|
45 | frame.setVisible(true);
|
---|
46 | }
|
---|
47 | }
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * Right mouse click menu.
|
---|
51 | *
|
---|
52 | */
|
---|
53 | class PopClickListener extends MouseAdapter {
|
---|
54 | private JTable table;
|
---|
55 |
|
---|
56 | public PopClickListener(JTable table) {
|
---|
57 | this.table = table;
|
---|
58 | }
|
---|
59 |
|
---|
60 | public void mousePressed(MouseEvent e) {
|
---|
61 | if (e.isPopupTrigger())
|
---|
62 | doPop(e);
|
---|
63 | }
|
---|
64 |
|
---|
65 | public void mouseReleased(MouseEvent e) {
|
---|
66 | if (e.isPopupTrigger())
|
---|
67 | doPop(e);
|
---|
68 | }
|
---|
69 |
|
---|
70 | private void doPop(MouseEvent e) {
|
---|
71 | PopUp menu = new PopUp(table);
|
---|
72 | menu.show(e.getComponent(), e.getX(), e.getY());
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | @SuppressWarnings("serial")
|
---|
77 | class PopUp extends JPopupMenu {
|
---|
78 |
|
---|
79 | public PopUp(JTable table) {
|
---|
80 | add(new JMenuItem(new AddAction(table)));
|
---|
81 | add(new JMenuItem(new EditAction(table)));
|
---|
82 | add(new JMenuItem(new RemoveAction(table)));
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * Action to add an item to the boaparty repo
|
---|
88 | *
|
---|
89 | */
|
---|
90 | @SuppressWarnings("serial")
|
---|
91 | class AddAction extends AbstractAction {
|
---|
92 |
|
---|
93 | private BoaPartyRepository partymodel = RepositoryFactory.getBoaPartyRepository();
|
---|
94 |
|
---|
95 | public AddAction(JTable table) {
|
---|
96 | super("add item");
|
---|
97 | putValue(SHORT_DESCRIPTION, "add a new boa item");
|
---|
98 | }
|
---|
99 |
|
---|
100 | @Override
|
---|
101 | public void actionPerformed(ActionEvent e) {
|
---|
102 | try {
|
---|
103 | BoaPartyModel model = new BoaPartyModel(new BoaPartyRepItem("newParty"));
|
---|
104 | final BoaPartyPanel panel = new BoaPartyPanel(model);
|
---|
105 | int result = JOptionPane.showConfirmDialog(null, panel, "Add new party", JOptionPane.OK_CANCEL_OPTION);
|
---|
106 | if (result == JOptionPane.OK_OPTION) {
|
---|
107 | System.out.println("ADDING " + model.getValues());
|
---|
108 | partymodel.addAll(model.getValues());
|
---|
109 | }
|
---|
110 | } catch (InstantiateException e2) {
|
---|
111 | e2.printStackTrace();
|
---|
112 | }
|
---|
113 | }
|
---|
114 | }
|
---|
115 |
|
---|
116 | /**
|
---|
117 | * Action to remove an item to the boaparty repo
|
---|
118 | *
|
---|
119 | */
|
---|
120 | @SuppressWarnings("serial")
|
---|
121 | class RemoveAction extends AbstractAction {
|
---|
122 |
|
---|
123 | private BoaPartyRepository partymodel = RepositoryFactory.getBoaPartyRepository();
|
---|
124 | private JTable table;
|
---|
125 |
|
---|
126 | public RemoveAction(JTable table) {
|
---|
127 | super("remove item");
|
---|
128 | this.table = table;
|
---|
129 | putValue(SHORT_DESCRIPTION, "remove a boa item");
|
---|
130 | }
|
---|
131 |
|
---|
132 | @Override
|
---|
133 | public void actionPerformed(ActionEvent e) {
|
---|
134 | int selectedrow = table.getSelectedRow();
|
---|
135 | if (selectedrow == -1)
|
---|
136 | return;
|
---|
137 | BoaPartyRepItem selectedparty = partymodel.getList().getList().get(selectedrow);
|
---|
138 | partymodel.remove(selectedparty);
|
---|
139 | }
|
---|
140 |
|
---|
141 | }
|
---|
142 |
|
---|
143 | /**
|
---|
144 | * Action to edit an item to the boaparty repo
|
---|
145 | *
|
---|
146 | */
|
---|
147 | @SuppressWarnings("serial")
|
---|
148 | class EditAction extends AbstractAction {
|
---|
149 |
|
---|
150 | private BoaPartyRepository partymodel = RepositoryFactory.getBoaPartyRepository();
|
---|
151 | private JTable table;
|
---|
152 |
|
---|
153 | public EditAction(JTable table) {
|
---|
154 | super("edit item");
|
---|
155 | this.table = table;
|
---|
156 | putValue(SHORT_DESCRIPTION, "edit a boa item");
|
---|
157 | }
|
---|
158 |
|
---|
159 | @Override
|
---|
160 | public void actionPerformed(ActionEvent e) {
|
---|
161 |
|
---|
162 | try {
|
---|
163 | int selectedrow = table.getSelectedRow();
|
---|
164 | if (selectedrow == -1)
|
---|
165 | return;
|
---|
166 | BoaPartyRepItem selectedparty = partymodel.getList().getList().get(selectedrow);
|
---|
167 | BoaPartyModel model = new BoaPartyModel(selectedparty);
|
---|
168 | final BoaPartyPanel panel = new BoaPartyPanel(model);
|
---|
169 |
|
---|
170 | int result = JOptionPane.showConfirmDialog(null, panel, "Edit party", JOptionPane.OK_CANCEL_OPTION);
|
---|
171 | if (result == JOptionPane.OK_OPTION) {
|
---|
172 | partymodel.remove(selectedparty);
|
---|
173 | partymodel.addAll(model.getValues());
|
---|
174 | }
|
---|
175 | } catch (InstantiateException e1) {
|
---|
176 | e1.printStackTrace();
|
---|
177 | }
|
---|
178 |
|
---|
179 | }
|
---|
180 |
|
---|
181 | }
|
---|