| 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.view; |
| 16 | |
| 17 | import javax.swing.ButtonGroup; |
| 18 | import javax.swing.JCheckBox; |
| 19 | import javax.swing.JLabel; |
| 20 | import javax.swing.JPanel; |
| 21 | import javax.swing.JRadioButton; |
| 22 | import javax.swing.JTextField; |
| 23 | |
| 24 | import net.sourceforge.hiveboard.Account; |
| 25 | import net.sourceforge.hiveboard.AccountRole; |
| 26 | import net.sourceforge.hiveboard.Event; |
| 27 | import net.sourceforge.hiveboard.docking.Views; |
| 28 | import net.sourceforge.hiveboard.event.DeferredConsumer; |
| 29 | import net.sourceforge.hiveboard.event.EventsPriorities; |
| 30 | import net.sourceforge.hiveevents.Channel; |
| 31 | import net.sourceforge.hiveevents.Consumer; |
| 32 | import net.sourceforge.hiveevents.Filter; |
| 33 | import net.sourceforge.hiveevents.PersistentConsumer; |
| 34 | |
| 35 | import zappini.designgridlayout.DesignGridLayout; |
| 36 | import zappini.designgridlayout.Row; |
| 37 | |
| 38 | public class AccountDetailPanel extends JPanel |
| 39 | { |
| 40 | public AccountDetailPanel( Channel<Account> selectedAccount, |
| 41 | Channel<Event> eventChannel) |
| 42 | { |
| 43 | super(); |
| 44 | |
| 45 | initComponentNames(); |
| 46 | |
| 47 | _txfVisa.setEditable(false); |
| 48 | _txfName.setEditable(false); |
| 49 | _txfEmail.setEditable(false); |
| 50 | _cbxConnected.setEnabled(false); |
| 51 | _rbnRoleBasic.setEnabled(false); |
| 52 | _rbnRoleInitiator.setEnabled(false); |
| 53 | _cbxAdmin.setEnabled(false); |
| 54 | |
| 55 | ButtonGroup group = new ButtonGroup(); |
| 56 | group.add(_rbnRoleBasic); |
| 57 | group.add(_rbnRoleInitiator); |
| 58 | |
| 59 | setAccount(null); |
| 60 | |
| 61 | DesignGridLayout layout = new DesignGridLayout(this); |
| 62 | setLayout(layout); |
| 63 | |
| 64 | layout.setTop(BORDER_MARGIN).setBottom(BORDER_MARGIN); |
| 65 | layout.row().label(_lblVisa).add(_txfVisa).add(Row.EMPTY); |
| 66 | layout.row().label(_lblName).add(_txfName); |
| 67 | layout.row().label(_lblEmail).add(_txfEmail); |
| 68 | layout.row().label(_lblRole).add(_rbnRoleBasic, _rbnRoleInitiator); |
| 69 | layout.row().add(_cbxAdmin); |
| 70 | layout.row().add(_cbxConnected); |
| 71 | |
| 72 | // Listen to accounts-related events |
| 73 | initEventConsumers(selectedAccount, eventChannel); |
| 74 | } |
| 75 | |
| 76 | private void initComponentNames() |
| 77 | { |
| 78 | _lblVisa.setName(Views.ACCOUNT_DETAIL.id() + "-visa-label"); |
| 79 | _txfVisa.setName(Views.ACCOUNT_DETAIL.id() + "-visa"); |
| 80 | _lblName.setName(Views.ACCOUNT_DETAIL.id() + "-name-label"); |
| 81 | _txfName.setName(Views.ACCOUNT_DETAIL.id() + "-name"); |
| 82 | _lblEmail.setName(Views.ACCOUNT_DETAIL.id() + "-email-label"); |
| 83 | _txfEmail.setName(Views.ACCOUNT_DETAIL.id() + "-email"); |
| 84 | _cbxAdmin.setName(Views.ACCOUNT_DETAIL.id() + "-isadmin"); |
| 85 | _cbxConnected.setName(Views.ACCOUNT_DETAIL.id() + "-connected"); |
| 86 | _lblRole.setName(Views.ACCOUNT_DETAIL.id() + "-role-label"); |
| 87 | _rbnRoleBasic.setName(Views.ACCOUNT_DETAIL.id() + "-role-basic"); |
| 88 | _rbnRoleInitiator.setName(Views.ACCOUNT_DETAIL.id() + "-role-initiator"); |
| 89 | } |
| 90 | |
| 91 | private void initEventConsumers(Channel<Account> selectedAccount, |
| 92 | Channel<Event> eventChannel) |
| 93 | { |
| 94 | int priority = EventsPriorities.ACCOUNT_DETAIL_UPDATE; |
| 95 | Filter<Event> filter = new Filter<Event>() |
| 96 | { |
| 97 | public boolean passEvent(Event event) |
| 98 | { |
| 99 | if (_account == null || event.getWho() != _account.getId()) |
| 100 | { |
| 101 | return false; |
| 102 | } |
| 103 | |
| 104 | switch (event.getType()) |
| 105 | { |
| 106 | case EVT_MODIFY_ACCOUNT: |
| 107 | case EVT_LOGIN: |
| 108 | case EVT_LOGOUT: |
| 109 | case EVT_DESTROY_ACCOUNT: |
| 110 | return true; |
| 111 | |
| 112 | default: |
| 113 | return false; |
| 114 | } |
| 115 | } |
| 116 | }; |
| 117 | _consumer = new DeferredConsumer() |
| 118 | { |
| 119 | @Override protected void pushEvent(Event event) |
| 120 | { |
| 121 | switch (event.getType()) |
| 122 | { |
| 123 | case EVT_MODIFY_ACCOUNT: |
| 124 | case EVT_LOGIN: |
| 125 | case EVT_LOGOUT: |
| 126 | refreshAccount(); |
| 127 | break; |
| 128 | |
| 129 | case EVT_DESTROY_ACCOUNT: |
| 130 | setAccount(null); |
| 131 | break; |
| 132 | |
| 133 | default: |
| 134 | break; |
| 135 | } |
| 136 | } |
| 137 | }; |
| 138 | eventChannel.registerPushConsumer(priority, filter, _consumer); |
| 139 | |
| 140 | priority = EventsPriorities.SELECTED_ACCOUNT_GUI_REFRESH; |
| 141 | selectedAccount.registerPushConsumer(priority, new PersistentConsumer<Account>() |
| 142 | { |
| 143 | public void push(Account account) |
| 144 | { |
| 145 | setAccount(account); |
| 146 | } |
| 147 | }); |
| 148 | } |
| 149 | |
| 150 | public void setAccount(Account account) |
| 151 | { |
| 152 | _account = account; |
| 153 | refreshAccount(); |
| 154 | } |
| 155 | |
| 156 | private void refreshAccount() |
| 157 | { |
| 158 | if (_account == null) |
| 159 | { |
| 160 | _txfVisa.setText(""); |
| 161 | _txfName.setText(""); |
| 162 | _txfEmail.setText(""); |
| 163 | _cbxConnected.setSelected(false); |
| 164 | _rbnRoleBasic.setSelected(true); |
| 165 | _cbxAdmin.setSelected(false); |
| 166 | } |
| 167 | else |
| 168 | { |
| 169 | _txfVisa.setText(_account.getVisa()); |
| 170 | _txfName.setText(_account.getName()); |
| 171 | _txfEmail.setText(_account.getEmail()); |
| 172 | _cbxConnected.setSelected(_account.isConnected()); |
| 173 | if (_account.getRole() == AccountRole.BASIC) |
| 174 | { |
| 175 | _rbnRoleBasic.setSelected(true); |
| 176 | } |
| 177 | else |
| 178 | { |
| 179 | _rbnRoleInitiator.setSelected(true); |
| 180 | } |
| 181 | _cbxAdmin.setSelected(_account.isAdmin()); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | static private final int BORDER_MARGIN = 4; |
| 186 | |
| 187 | private Consumer<Event> _consumer; |
| 188 | private Account _account; |
| 189 | private final JLabel _lblVisa = new JLabel(); |
| 190 | private final JTextField _txfVisa = new JTextField(); |
| 191 | private final JLabel _lblName = new JLabel(); |
| 192 | private final JTextField _txfName = new JTextField(); |
| 193 | private final JLabel _lblEmail = new JLabel(); |
| 194 | private final JTextField _txfEmail = new JTextField(); |
| 195 | private final JCheckBox _cbxConnected = new JCheckBox(); |
| 196 | private final JLabel _lblRole = new JLabel(); |
| 197 | private final JRadioButton _rbnRoleBasic = new JRadioButton(); |
| 198 | private final JRadioButton _rbnRoleInitiator = new JRadioButton(); |
| 199 | private final JCheckBox _cbxAdmin = new JCheckBox(); |
| 200 | } |