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 org.jdesktop.application.Resource; |
18 | |
19 | import net.sourceforge.hiveboard.Account; |
20 | import net.sourceforge.hiveboard.Board; |
21 | import net.sourceforge.hiveboard.Event; |
22 | import net.sourceforge.hiveboard.EventType; |
23 | import net.sourceforge.hiveboard.event.EventsPriorities; |
24 | import net.sourceforge.hiveboard.event.PersistentDeferredConsumer; |
25 | import net.sourceforge.hiveboard.model.AccountRepository; |
26 | import net.sourceforge.hiveevents.Channel; |
27 | import net.sourceforge.hiveevents.Filter; |
28 | import net.sourceforge.hivegui.application.ApplicationContextHolder; |
29 | |
30 | public class FullScreenMatteHandler |
31 | { |
32 | public FullScreenMatteHandler( ApplicationContextHolder holder, |
33 | Channel<Event> events, |
34 | AccountRepository accounts) |
35 | { |
36 | _accounts = accounts; |
37 | holder.getContext().getResourceMap(FullScreenMatteHandler.class).injectFields(this); |
38 | registerConsumers(events); |
39 | } |
40 | |
41 | public void setCommentAcceptor(MatteCommentAcceptor acceptor) |
42 | { |
43 | _acceptor = acceptor; |
44 | } |
45 | |
46 | private void registerConsumers(Channel<Event> events) |
47 | { |
48 | // Register comments events here |
49 | int priority = EventsPriorities.FULLSCREEN_NOTIFICATIONS; |
50 | Filter<Event> filter = new Filter<Event>() |
51 | { |
52 | public boolean passEvent(Event event) |
53 | { |
54 | return (event.getType() == EventType.EVT_SEND_COMMENT) |
55 | && (_board != null) |
56 | && (event.getWhere() == _board.getId()); |
57 | } |
58 | }; |
59 | events.registerPushConsumer(priority, filter, new PersistentDeferredConsumer() |
60 | { |
61 | @Override protected void pushEvent(Event event) |
62 | { |
63 | Account who = _accounts.getAccount(event.getWho()); |
64 | String message = String.format( _formatSendComment, |
65 | who.getVisa(), |
66 | who.getName(), |
67 | event.getComment()); |
68 | setMatteComment(message); |
69 | } |
70 | }); |
71 | |
72 | // Register some notifications here |
73 | filter = new Filter<Event>() |
74 | { |
75 | public boolean passEvent(Event event) |
76 | { |
77 | return (event.getType() == EventType.EVT_GIVE_TOKEN) |
78 | && (_board != null) |
79 | && (event.getWhere() == _board.getId()); |
80 | } |
81 | }; |
82 | events.registerPushConsumer(priority, filter, new PersistentDeferredConsumer() |
83 | { |
84 | @Override protected void pushEvent(Event event) |
85 | { |
86 | Account who = _accounts.getAccount(event.getWho()); |
87 | String message = String.format( _formatGiveToken, |
88 | who.getVisa(), |
89 | who.getName(), |
90 | _board.getName()); |
91 | setMatteComment(message); |
92 | } |
93 | }); |
94 | |
95 | filter = new Filter<Event>() |
96 | { |
97 | public boolean passEvent(Event event) |
98 | { |
99 | return (event.getType() == EventType.EVT_REQUEST_TOKEN) |
100 | && (_board != null) |
101 | && (event.getWhere() == _board.getId()); |
102 | } |
103 | }; |
104 | events.registerPushConsumer(priority, filter, new PersistentDeferredConsumer() |
105 | { |
106 | @Override protected void pushEvent(Event event) |
107 | { |
108 | Account who = _accounts.getAccount(event.getWho()); |
109 | String message = String.format( _formatRequestToken, |
110 | who.getVisa(), |
111 | who.getName(), |
112 | _board.getName()); |
113 | setMatteComment(message); |
114 | } |
115 | }); |
116 | } |
117 | |
118 | private void setMatteComment(String comment) |
119 | { |
120 | if (_acceptor != null) |
121 | { |
122 | _acceptor.setMatteComment(comment); |
123 | } |
124 | } |
125 | |
126 | public void setBoard(Board board) |
127 | { |
128 | _board = board; |
129 | } |
130 | private final AccountRepository _accounts; |
131 | private MatteCommentAcceptor _acceptor; |
132 | private Board _board = null; |
133 | |
134 | @Resource(key = "Matte.formatSendComment") |
135 | private String _formatSendComment; |
136 | @Resource(key = "Matte.formatGiveToken") |
137 | private String _formatGiveToken; |
138 | @Resource(key = "Matte.formatRequestToken") |
139 | private String _formatRequestToken; |
140 | } |