| 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.awt.BorderLayout; | 
| 18 |   | 
| 19 | import javax.swing.ActionMap; | 
| 20 | import javax.swing.JPanel; | 
| 21 | import javax.swing.JScrollPane; | 
| 22 | import javax.swing.JTextField; | 
| 23 |   | 
| 24 | import org.jdesktop.application.Action; | 
| 25 | import org.jdesktop.application.Task; | 
| 26 |   | 
| 27 | import net.sourceforge.hiveboard.Board; | 
| 28 | import net.sourceforge.hiveboard.Event; | 
| 29 | import net.sourceforge.hiveboard.WhiteBoardUserService; | 
| 30 | import net.sourceforge.hiveboard.command.AbstractTask; | 
| 31 | import net.sourceforge.hiveboard.docking.Views; | 
| 32 | import net.sourceforge.hiveboard.event.EventsPriorities; | 
| 33 | import net.sourceforge.hiveevents.Channel; | 
| 34 | import net.sourceforge.hiveevents.PersistentConsumer; | 
| 35 | import net.sourceforge.hivegui.application.ApplicationContextHolder; | 
| 36 | import net.sourceforge.hivegui.application.HiveGuiApplicationMain; | 
| 37 | import net.sourceforge.hivegui.table.BeanTable; | 
| 38 | import net.sourceforge.hivegui.table.DataListModel; | 
| 39 | import net.sourceforge.hiveutils.service.ObjectBuilder; | 
| 40 |   | 
| 41 | public class CommentListPanel extends JPanel | 
| 42 | { | 
| 43 |         public CommentListPanel(        WhiteBoardUserService                service, | 
| 44 |                                                                 ObjectBuilder                                builder, | 
| 45 |                                                                 Channel<DrawingAreaHolder>        selectedBoardChannel, | 
| 46 |                                                                 Channel<Object>                                clearCommentsChannel, | 
| 47 |                                                                 BeanTable<Event>                        tblComments) | 
| 48 |         { | 
| 49 |                 setLayout(new BorderLayout()); | 
| 50 |                 _service = service; | 
| 51 |                 _builder = builder; | 
| 52 |                 _tblComments = tblComments; | 
| 53 |                 _tblComments.setName(Views.COMMENTS_LIST.id() + "-table"); | 
| 54 |                 _emptyModel = _tblComments.getDataListModel(); | 
| 55 |                 _model = _emptyModel; | 
| 56 |   | 
| 57 |                 // Listen to changes in the current Board | 
| 58 |                 int priority = EventsPriorities.SELECTED_IMAGE_GUI_REFRESH; | 
| 59 |                 selectedBoardChannel.registerPushConsumer( | 
| 60 |                                                 priority, new PersistentConsumer<DrawingAreaHolder>() | 
| 61 |                 { | 
| 62 |                         public void        push(DrawingAreaHolder holder) | 
| 63 |                         { | 
| 64 |                                 if (holder != null) | 
| 65 |                                 { | 
| 66 |                                         updateModel(holder.getDrawingArea().getBoardImageModel().getBoard()); | 
| 67 |                                 } | 
| 68 |                                 else | 
| 69 |                                 { | 
| 70 |                                         updateModel(null); | 
| 71 |                                 } | 
| 72 |                                 setSendEnabled(holder != null); | 
| 73 |                         } | 
| 74 |                 }); | 
| 75 |   | 
| 76 |                 priority = EventsPriorities.COMMENTS_LIST_CLEARED; | 
| 77 |                 clearCommentsChannel.registerPushConsumer(priority, new PersistentConsumer<Object>() | 
| 78 |                 { | 
| 79 |                         public void        push(Object e) | 
| 80 |                         { | 
| 81 |                                 _model.clear(); | 
| 82 |                         } | 
| 83 |                 }); | 
| 84 |   | 
| 85 |                 JScrollPane scroller = new JScrollPane(_tblComments); | 
| 86 |                 scroller.setName(Views.COMMENTS_LIST.id() + "-scroller"); | 
| 87 |             add(scroller, BorderLayout.CENTER); | 
| 88 |   | 
| 89 |                 _txfComment = new JTextField(); | 
| 90 |                 _txfComment.setName(Views.COMMENTS_LIST.id() + "-input"); | 
| 91 |             add(_txfComment, BorderLayout.NORTH); | 
| 92 |         } | 
| 93 |   | 
| 94 |         public void        setContextHolder(ApplicationContextHolder holder) | 
| 95 |         { | 
| 96 |                 _application = holder.getApplication(); | 
| 97 |                 // Initialize action | 
| 98 |                 ActionMap actionMap = holder.getContext().getActionMap(getClass(), this); | 
| 99 |                 _txfComment.setAction(actionMap.get("sendComment")); | 
| 100 |         } | 
| 101 |   | 
| 102 |         @Action(enabledProperty = "sendEnabled", block = Task.BlockingScope.WINDOW) | 
| 103 |         public Task        sendComment() | 
| 104 |         { | 
| 105 |                 if (_board == null) | 
| 106 |                 { | 
| 107 |                         return null; | 
| 108 |                 } | 
| 109 |                 final String text = _txfComment.getText(); | 
| 110 |                 // Clear previous content | 
| 111 |                 _txfComment.setText(""); | 
| 112 |                 if (text == null || text.trim().equals("")) | 
| 113 |                 { | 
| 114 |                         return null; | 
| 115 |                 } | 
| 116 |                 final int id = _board.getId(); | 
| 117 |                 return new AbstractTask<Void, Void>(_application) | 
| 118 |                 { | 
| 119 |                         @Override protected Void doInBackground() throws Exception | 
| 120 |                         { | 
| 121 |                                 _service.comment(id, text); | 
| 122 |                                 return null; | 
| 123 |                         } | 
| 124 |                 }; | 
| 125 |         } | 
| 126 |          | 
| 127 |         public DataListModel<Event>        getModel() | 
| 128 |         { | 
| 129 |                 return _model; | 
| 130 |         } | 
| 131 |          | 
| 132 |         private void        updateModel(Board board) | 
| 133 |         { | 
| 134 |                 if (_board == board) | 
| 135 |                 { | 
| 136 |                         return; | 
| 137 |                 } | 
| 138 |                 _board = board; | 
| 139 |                 if (board != null) | 
| 140 |                 { | 
| 141 |                         _model = _builder.create("CommentListModel", _board); | 
| 142 |                 } | 
| 143 |                 else | 
| 144 |                 { | 
| 145 |                         _model = _emptyModel; | 
| 146 |                 } | 
| 147 |                 _tblComments.setDataListModel(_model); | 
| 148 |         } | 
| 149 |   | 
| 150 |         // Utility property that can be used for automatic enabling of accept button | 
| 151 |         final public boolean        isSendEnabled() | 
| 152 |         { | 
| 153 |                 return _enabled; | 
| 154 |         } | 
| 155 |          | 
| 156 |         final public void                setSendEnabled(boolean enabled) | 
| 157 |         { | 
| 158 |                 boolean old = _enabled; | 
| 159 |                 _enabled = enabled; | 
| 160 |                 firePropertyChange("sendEnabled", old, enabled); | 
| 161 |         } | 
| 162 |          | 
| 163 |         final private WhiteBoardUserService        _service; | 
| 164 |         final private ObjectBuilder                        _builder; | 
| 165 |         final private BeanTable<Event>                _tblComments; | 
| 166 |         final private JTextField                        _txfComment; | 
| 167 |         final private DataListModel<Event>        _emptyModel; | 
| 168 |         private HiveGuiApplicationMain                _application; | 
| 169 |         private DataListModel<Event>                _model; | 
| 170 |         private Board                                                _board = null; | 
| 171 |         private boolean                                                _enabled = false; | 
| 172 | } |