| 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 java.text.SimpleDateFormat; | 
| 18 |   | 
| 19 | import javax.swing.JFormattedTextField; | 
| 20 | import javax.swing.JLabel; | 
| 21 | import javax.swing.JPanel; | 
| 22 | import javax.swing.JTextField; | 
| 23 | import javax.swing.text.DateFormatter; | 
| 24 |   | 
| 25 | import net.sourceforge.hiveboard.Account; | 
| 26 | import net.sourceforge.hiveboard.Board; | 
| 27 | import net.sourceforge.hiveboard.Event; | 
| 28 | import net.sourceforge.hiveboard.docking.Views; | 
| 29 | import net.sourceforge.hiveboard.event.DeferredConsumer; | 
| 30 | import net.sourceforge.hiveboard.event.EventsPriorities; | 
| 31 | import net.sourceforge.hiveboard.model.AccountRepository; | 
| 32 | import net.sourceforge.hiveevents.Channel; | 
| 33 | import net.sourceforge.hiveevents.Consumer; | 
| 34 | import net.sourceforge.hiveevents.Filter; | 
| 35 | import net.sourceforge.hiveevents.PersistentConsumer; | 
| 36 |   | 
| 37 | import zappini.designgridlayout.DesignGridLayout; | 
| 38 | import zappini.designgridlayout.Row; | 
| 39 |   | 
| 40 | public class BoardDetailPanel extends JPanel | 
| 41 | { | 
| 42 |         public BoardDetailPanel(Channel<Board>                selectedBoard, | 
| 43 |                                                         Channel<Event>                eventChannel, | 
| 44 |                                                         AccountRepository        repository) | 
| 45 |         { | 
| 46 |                 super(); | 
| 47 |                 _repository = repository; | 
| 48 |                  | 
| 49 |                 initComponentNames(); | 
| 50 |   | 
| 51 |                 _txfName.setEditable(false); | 
| 52 |                 _txfDescription.setEditable(false); | 
| 53 |                 _txfSize.setEditable(false); | 
| 54 |                 _txfInitiator.setEditable(false); | 
| 55 |                 _txfCreation.setEditable(false); | 
| 56 |   | 
| 57 |                 DesignGridLayout layout = new DesignGridLayout(this);  | 
| 58 |                 setLayout(layout); | 
| 59 |                  | 
| 60 |                 // Add widgets to the panel | 
| 61 |                 layout.setTop(BORDER_MARGIN).setBottom(BORDER_MARGIN); | 
| 62 |                 layout.row().label(_lblName).add(_txfName); | 
| 63 |                 layout.row().label(_lblDescription).add(_txfDescription); | 
| 64 |                 layout.row().label(_lblSize).add(_txfSize).add(Row.EMPTY); | 
| 65 |                 layout.row().label(_lblInitiator).add(_txfInitiator); | 
| 66 |                 layout.row().label(_lblCreation).add(_txfCreation).add(Row.EMPTY); | 
| 67 |   | 
| 68 |                 // Listen to boards and account-related events | 
| 69 |                 initEventConsumers(selectedBoard, eventChannel); | 
| 70 |         } | 
| 71 |          | 
| 72 |         public void setDateFormat(String dateFormat) | 
| 73 |         { | 
| 74 |                 _dateFormatter.setFormat(new SimpleDateFormat(dateFormat)); | 
| 75 |         } | 
| 76 |   | 
| 77 |         public void setInitiatorFormat(String initiatorFormat) | 
| 78 |         { | 
| 79 |                 _initiatorFormat = initiatorFormat; | 
| 80 |         } | 
| 81 |   | 
| 82 |         public void setSizeFormat(String sizeFormat) | 
| 83 |         { | 
| 84 |                 _sizeFormat = sizeFormat; | 
| 85 |         } | 
| 86 |   | 
| 87 |         private void initComponentNames() | 
| 88 |         { | 
| 89 |                 _lblName.setName(Views.BOARD_DETAIL.id() + "-name-label"); | 
| 90 |                 _txfName.setName(Views.BOARD_DETAIL.id() + "-name"); | 
| 91 |                 _lblDescription.setName(Views.BOARD_DETAIL.id() + "-description-label"); | 
| 92 |                 _txfDescription.setName(Views.BOARD_DETAIL.id() + "-description"); | 
| 93 |                 _lblSize.setName(Views.BOARD_DETAIL.id() + "-size-label"); | 
| 94 |                 _txfSize.setName(Views.BOARD_DETAIL.id() + "-size"); | 
| 95 |                 _lblInitiator.setName(Views.BOARD_DETAIL.id() + "-initiator-label"); | 
| 96 |                 _txfInitiator.setName(Views.BOARD_DETAIL.id() + "-initiator"); | 
| 97 |                 _lblCreation.setName(Views.BOARD_DETAIL.id() + "-creation-label"); | 
| 98 |                 _txfCreation.setName(Views.BOARD_DETAIL.id() + "-creation"); | 
| 99 |         } | 
| 100 |          | 
| 101 |         private void initEventConsumers(Channel<Board>        selectedBoard, | 
| 102 |                                                                         Channel<Event>        eventChannel) | 
| 103 |         { | 
| 104 |                 int priority = EventsPriorities.BOARD_DETAIL_UPDATE; | 
| 105 |                 Filter<Event> filter = new Filter<Event>() | 
| 106 |                 { | 
| 107 |                         public boolean        passEvent(Event event) | 
| 108 |                         { | 
| 109 |                                 if (_board == null) | 
| 110 |                                 { | 
| 111 |                                         return false; | 
| 112 |                                 } | 
| 113 |                                          | 
| 114 |                                 switch (event.getType()) | 
| 115 |                                 { | 
| 116 |                                         case EVT_MODIFY_BOARD: | 
| 117 |                                         case EVT_DESTROY_BOARD: | 
| 118 |                                         return (event.getWhere() == _board.getId()); | 
| 119 |                                          | 
| 120 |                                         case EVT_MODIFY_ACCOUNT: | 
| 121 |                                         return (event.getWho() == _board.getInitiator()); | 
| 122 |                                          | 
| 123 |                                         default: | 
| 124 |                                         return false; | 
| 125 |                                 } | 
| 126 |                         } | 
| 127 |                 }; | 
| 128 |                 _consumer = new DeferredConsumer() | 
| 129 |                 { | 
| 130 |                         @Override protected void pushEvent(Event event) | 
| 131 |                         { | 
| 132 |                                 switch (event.getType()) | 
| 133 |                                 { | 
| 134 |                                         case EVT_MODIFY_BOARD: | 
| 135 |                                         // Fall through | 
| 136 |                                         case EVT_MODIFY_ACCOUNT: | 
| 137 |                                         refreshBoard(); | 
| 138 |                                         break; | 
| 139 |   | 
| 140 |                                         case EVT_DESTROY_BOARD: | 
| 141 |                                         setBoard(null); | 
| 142 |                                         break; | 
| 143 |   | 
| 144 |                                         default: | 
| 145 |                                         break; | 
| 146 |                                 } | 
| 147 |                         } | 
| 148 |                 }; | 
| 149 |                 eventChannel.registerPushConsumer(priority, filter, _consumer); | 
| 150 |   | 
| 151 |                 priority = EventsPriorities.SELECTED_BOARD_GUI_REFRESH; | 
| 152 |                 selectedBoard.registerPushConsumer(priority, new PersistentConsumer<Board>() | 
| 153 |                 { | 
| 154 |                         public void        push(Board board) | 
| 155 |                         { | 
| 156 |                                 setBoard(board); | 
| 157 |                         } | 
| 158 |                 }); | 
| 159 |         } | 
| 160 |          | 
| 161 |         public void        setBoard(Board board) | 
| 162 |         { | 
| 163 |                 _board = board; | 
| 164 |                 refreshBoard(); | 
| 165 |         } | 
| 166 |          | 
| 167 |         private void        refreshBoard() | 
| 168 |         { | 
| 169 |                 if (_board == null) | 
| 170 |                 { | 
| 171 |                         _txfName.setText(""); | 
| 172 |                         _txfDescription.setText(""); | 
| 173 |                         _txfSize.setText(""); | 
| 174 |                         _txfInitiator.setText(""); | 
| 175 |                         _txfCreation.setValue(null); | 
| 176 |                 } | 
| 177 |                 else | 
| 178 |                 { | 
| 179 |                         _txfName.setText(_board.getName()); | 
| 180 |                         _txfDescription.setText(_board.getDescription()); | 
| 181 |                         String size = String.format(_sizeFormat, | 
| 182 |                                                                                 _board.getWidth(), | 
| 183 |                                                                                 _board.getHeight()); | 
| 184 |                         _txfSize.setText(size); | 
| 185 |                         Account account = _repository.getAccount(_board.getInitiator()); | 
| 186 |                         String initiator = String.format(        _initiatorFormat, | 
| 187 |                                                                                                 account.getVisa(), | 
| 188 |                                                                                                 account.getName()); | 
| 189 |                         _txfInitiator.setText(initiator); | 
| 190 |                         _txfCreation.setValue(_board.getCreationDate()); | 
| 191 |                 } | 
| 192 |         } | 
| 193 |   | 
| 194 |         static private final int                        BORDER_MARGIN = 4; | 
| 195 |   | 
| 196 |         private final AccountRepository                _repository; | 
| 197 |         private Consumer<Event>                                _consumer; | 
| 198 |         private Board                                                _board; | 
| 199 |   | 
| 200 |         private final JLabel                                _lblName = new JLabel(); | 
| 201 |         private final JTextField                        _txfName = new JTextField(); | 
| 202 |         private final JLabel                                _lblDescription = new JLabel(); | 
| 203 |         private final JTextField                        _txfDescription = new JTextField(); | 
| 204 |         private final JLabel                                _lblSize = new JLabel(); | 
| 205 |         private final JTextField                        _txfSize = new JTextField(); | 
| 206 |         private final JLabel                                _lblInitiator = new JLabel(); | 
| 207 |         private final JTextField                        _txfInitiator = new JTextField(); | 
| 208 |         private final JLabel                                _lblCreation = new JLabel(); | 
| 209 |         private final DateFormatter                        _dateFormatter = new DateFormatter(); | 
| 210 |         private final JFormattedTextField        _txfCreation = new JFormattedTextField(_dateFormatter); | 
| 211 |          | 
| 212 |         private String                                                _initiatorFormat; | 
| 213 |         private String                                                _sizeFormat; | 
| 214 | } |