1 | // Copyright 2004-2007 Jean-Francois Poilpret |
2 | // |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | // you may not use this file except in compliance with the License. |
5 | // You may obtain a copy of the License at |
6 | // |
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | // |
9 | // Unless required by applicable law or agreed to in writing, software |
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | // See the License for the specific language governing permissions and |
13 | // limitations under the License. |
14 | |
15 | package net.sourceforge.hiveboard.dialog; |
16 | |
17 | import java.awt.BorderLayout; |
18 | import java.util.ArrayList; |
19 | import java.util.List; |
20 | |
21 | import javax.swing.JScrollPane; |
22 | import javax.swing.event.ListSelectionEvent; |
23 | import javax.swing.event.ListSelectionListener; |
24 | |
25 | import org.jdesktop.application.Action; |
26 | import org.jdesktop.application.Task; |
27 | |
28 | import net.sourceforge.hiveboard.Account; |
29 | import net.sourceforge.hiveboard.Board; |
30 | import net.sourceforge.hiveboard.WhiteBoardUserService; |
31 | import net.sourceforge.hiveboard.command.AbstractTask; |
32 | import net.sourceforge.hivegui.table.BeanTable; |
33 | import net.sourceforge.hivegui.table.DataListModel; |
34 | |
35 | public class AddParticipantPanel extends AbstractBoardPanel |
36 | { |
37 | static private final String NAME = "add-participant"; |
38 | |
39 | public AddParticipantPanel( BeanTable<Account> tblAccounts, |
40 | WhiteBoardUserService service, |
41 | Board board) |
42 | { |
43 | super(NAME); |
44 | setLayout(new BorderLayout()); |
45 | _board = board; |
46 | _service = service; |
47 | _tblAccounts = tblAccounts; |
48 | _accounts = _tblAccounts.getDataListModel(); |
49 | |
50 | _tblAccounts.setName(NAME + "-list"); |
51 | |
52 | add(new JScrollPane(_tblAccounts), BorderLayout.CENTER); |
53 | |
54 | _tblAccounts.clearSelection(); |
55 | _tblAccounts.getSelectionModel().addListSelectionListener(new ListSelectionListener() |
56 | { |
57 | public void valueChanged(ListSelectionEvent e) |
58 | { |
59 | setAcceptEnabled(_tblAccounts.getSelectedRowCount() != 0); |
60 | } |
61 | }); |
62 | } |
63 | |
64 | @Override public void reset() |
65 | { |
66 | setAcceptEnabled(false); |
67 | } |
68 | |
69 | @Action(enabledProperty = "acceptEnabled", block = Task.BlockingScope.WINDOW) |
70 | public Task accept() |
71 | { |
72 | int[] selected = _tblAccounts.getSelectedRows(); |
73 | final List<Account> accounts = new ArrayList<Account>(); |
74 | for (int i = 0; i < selected.length; i++) |
75 | { |
76 | Account account = _accounts.getRow(selected[i]); |
77 | if (!_board.getParticipants().contains(account.getId())) |
78 | { |
79 | accounts.add(account); |
80 | } |
81 | } |
82 | return new AbstractTask<Void, Void>(_application, _parent) |
83 | { |
84 | @Override protected Void doInBackground() throws Exception |
85 | { |
86 | for (Account account: accounts) |
87 | { |
88 | _service.addParticipant(_board.getId(), account.getId()); |
89 | } |
90 | return null; |
91 | } |
92 | }; |
93 | } |
94 | |
95 | final private WhiteBoardUserService _service; |
96 | final private BeanTable<Account> _tblAccounts; |
97 | final private DataListModel<Account> _accounts; |
98 | } |