| 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.GridBagConstraints; |
| 18 | import java.awt.GridBagLayout; |
| 19 | import java.awt.Insets; |
| 20 | |
| 21 | import javax.swing.DefaultComboBoxModel; |
| 22 | import javax.swing.JComboBox; |
| 23 | import javax.swing.JLabel; |
| 24 | import javax.swing.JScrollPane; |
| 25 | |
| 26 | import org.jdesktop.application.Action; |
| 27 | import org.jdesktop.application.Task; |
| 28 | |
| 29 | import net.sourceforge.hiveboard.Board; |
| 30 | import net.sourceforge.hiveboard.BoardTokenSetter; |
| 31 | import net.sourceforge.hiveboard.WhiteBoardUserService; |
| 32 | import net.sourceforge.hiveboard.command.AbstractTask; |
| 33 | import net.sourceforge.hiveboard.main.IdentityHolder; |
| 34 | import net.sourceforge.hiveboard.util.Participant; |
| 35 | import net.sourceforge.hivegui.table.BeanTable; |
| 36 | import net.sourceforge.hivegui.table.DataListModel; |
| 37 | import net.sourceforge.hiveutils.service.ObjectBuilder; |
| 38 | |
| 39 | public class GiveTokenPanel extends AbstractBoardPanel |
| 40 | { |
| 41 | static private final String NAME = "give-token"; |
| 42 | |
| 43 | public GiveTokenPanel( ObjectBuilder builder, |
| 44 | BeanTable<Participant> tblPresents, |
| 45 | BoardTokenSetterRenderer renderer, |
| 46 | WhiteBoardUserService service, |
| 47 | IdentityHolder identity, |
| 48 | Board board) |
| 49 | { |
| 50 | super(NAME); |
| 51 | setLayout(new GridBagLayout()); |
| 52 | _board = board; |
| 53 | _service = service; |
| 54 | _tblPresents = tblPresents; |
| 55 | |
| 56 | _tblPresents.setName(NAME + "-list"); |
| 57 | _lblTokenSetter.setName(NAME + "-token-setter-label"); |
| 58 | _cboTokenSetter.setName(NAME + "-token-setter"); |
| 59 | |
| 60 | DataListModel<Participant> model = builder.create("PresentListModel", board); |
| 61 | _tblPresents.setDataListModel(model); |
| 62 | |
| 63 | _cboTokenSetter.setModel( |
| 64 | new DefaultComboBoxModel(BoardTokenSetter.class.getEnumConstants())); |
| 65 | _cboTokenSetter.setRenderer(renderer); |
| 66 | |
| 67 | init(); |
| 68 | |
| 69 | _cboTokenSetter.setEnabled(_board.getInitiator() == identity.getId()); |
| 70 | _cboTokenSetter.setSelectedItem(_board.getTokenSetter()); |
| 71 | _tblPresents.clearSelection(); |
| 72 | } |
| 73 | |
| 74 | private void init() |
| 75 | { |
| 76 | _lblTokenSetter.setLabelFor(_cboTokenSetter); |
| 77 | |
| 78 | // Add widgets to the panel |
| 79 | GridBagConstraints constraints = new GridBagConstraints(); |
| 80 | constraints.gridwidth = 1; |
| 81 | constraints.gridheight = 1; |
| 82 | constraints.weightx = 0.0; |
| 83 | constraints.weighty = 0.0; |
| 84 | constraints.anchor = GridBagConstraints.WEST; |
| 85 | constraints.fill = GridBagConstraints.HORIZONTAL; |
| 86 | constraints.insets = new Insets(2, 2, 2, 2); |
| 87 | |
| 88 | constraints.gridx = 0; |
| 89 | constraints.gridy = 0; |
| 90 | add(_lblTokenSetter, constraints); |
| 91 | |
| 92 | constraints.gridx = 1; |
| 93 | add(_cboTokenSetter, constraints); |
| 94 | |
| 95 | constraints.gridx = 0; |
| 96 | constraints.gridy = 1; |
| 97 | constraints.gridwidth = 2; |
| 98 | constraints.fill = GridBagConstraints.BOTH; |
| 99 | add(new JScrollPane(_tblPresents), constraints); |
| 100 | } |
| 101 | |
| 102 | @Action(block = Task.BlockingScope.WINDOW) |
| 103 | public Task accept() |
| 104 | { |
| 105 | return new AbstractTask<Void, Void>(_application, _parent) |
| 106 | { |
| 107 | @Override protected Void doInBackground() throws Exception |
| 108 | { |
| 109 | // Check if board token setter has changed |
| 110 | if (_board.getTokenSetter() != _cboTokenSetter.getSelectedItem()) |
| 111 | { |
| 112 | _board.setTokenSetter( |
| 113 | (BoardTokenSetter) _cboTokenSetter.getSelectedItem()); |
| 114 | _service.modifyBoard(_board); |
| 115 | } |
| 116 | |
| 117 | // Check if writer has been set to someone else |
| 118 | int selection = _tblPresents.getSelectedRow(); |
| 119 | if (selection != -1) |
| 120 | { |
| 121 | int newWriter = _tblPresents.getDataListModel() |
| 122 | .getRow(selection).getAccount().getId(); |
| 123 | if (newWriter != _board.getWriter()) |
| 124 | { |
| 125 | _service.giveToken(_board.getId(), newWriter); |
| 126 | } |
| 127 | } |
| 128 | return null; |
| 129 | } |
| 130 | }; |
| 131 | } |
| 132 | |
| 133 | final private WhiteBoardUserService _service; |
| 134 | final private JLabel _lblTokenSetter = new JLabel(); |
| 135 | final private JComboBox _cboTokenSetter = new JComboBox(); |
| 136 | final private BeanTable<Participant> _tblPresents; |
| 137 | } |