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.main; |
16 | |
17 | import java.awt.BorderLayout; |
18 | |
19 | import javax.swing.JToolBar; |
20 | |
21 | import org.flexdock.docking.Dockable; |
22 | import org.flexdock.docking.DockingManager; |
23 | import org.flexdock.docking.DockingPort; |
24 | import org.flexdock.view.View; |
25 | import org.jdesktop.application.ApplicationContext; |
26 | import org.jdesktop.application.ResourceMap; |
27 | |
28 | import net.sourceforge.hiveboard.Account; |
29 | import net.sourceforge.hiveboard.Board; |
30 | import net.sourceforge.hiveboard.Event; |
31 | import net.sourceforge.hiveboard.EventType; |
32 | import net.sourceforge.hiveboard.docking.ViewHelper; |
33 | import net.sourceforge.hiveboard.drawing.ToolHandler; |
34 | import net.sourceforge.hiveboard.event.ConstraintEventFilter; |
35 | import net.sourceforge.hiveboard.event.Constraints; |
36 | import net.sourceforge.hiveboard.event.EventsPriorities; |
37 | import net.sourceforge.hiveboard.event.PersistentDeferredConsumer; |
38 | import net.sourceforge.hiveboard.model.BoardImageModel; |
39 | import net.sourceforge.hiveboard.view.DrawingArea; |
40 | import net.sourceforge.hiveboard.view.DrawingAreaPanel; |
41 | import net.sourceforge.hiveevents.Channel; |
42 | import net.sourceforge.hiveevents.PersistentConsumer; |
43 | import net.sourceforge.hivegui.application.ApplicationContextHolder; |
44 | import net.sourceforge.hivegui.application.HiveGuiApplicationMain; |
45 | import net.sourceforge.hivegui.docking.DockingHelper; |
46 | import net.sourceforge.hivegui.docking.EmptyableViewport; |
47 | import net.sourceforge.hivegui.docking.ViewFactory; |
48 | |
49 | public class HiveBoardChannelHooksInitializerImpl implements HiveBoardChannelHooksInitializer |
50 | { |
51 | public HiveBoardChannelHooksInitializerImpl(ApplicationContextHolder appHolder, |
52 | ViewFactory viewFactory, |
53 | IdentityHolder holder, |
54 | ToolHandler toolHandler, |
55 | Channel<Object> fullScreenMode, |
56 | Channel<Object> windowMode, |
57 | Channel<Account> identityChange, |
58 | Channel<Board> selectBoardChannel, |
59 | Channel<BoardImageModel> openBoardChannel, |
60 | Channel<Event> eventsChannel) |
61 | { |
62 | _context = appHolder.getContext(); |
63 | _application = appHolder.getApplication(); |
64 | _viewFactory = viewFactory; |
65 | _holder = holder; |
66 | _toolbar = toolHandler.getToolBar(); |
67 | _fullScreenMode = fullScreenMode; |
68 | _windowMode = windowMode; |
69 | _identityChange = identityChange; |
70 | _selectedBoardChannel = selectBoardChannel; |
71 | _openBoardChannel = openBoardChannel; |
72 | _eventsChannel = eventsChannel; |
73 | } |
74 | |
75 | public void registerHooks() |
76 | { |
77 | // Register consumer for screen mode switching channels |
78 | int priority = EventsPriorities.WINDOWSHELL_FULL_SWITCH; |
79 | _fullScreenMode.registerPushConsumer(priority, new PersistentConsumer<Object>() |
80 | { |
81 | public void push(Object event) |
82 | { |
83 | switchFullScreen(true); |
84 | } |
85 | }); |
86 | priority = EventsPriorities.WINDOWSHELL_WINDOW_SWITCH; |
87 | _windowMode.registerPushConsumer(priority, new PersistentConsumer<Object>() |
88 | { |
89 | public void push(Object event) |
90 | { |
91 | switchFullScreen(false); |
92 | } |
93 | }); |
94 | |
95 | // Register consumer for changes in self identity (to update main frame title) |
96 | priority = EventsPriorities.IDENTITY_GUI_UPDATE; |
97 | _identityChange.registerPushConsumer(priority, new PersistentConsumer<Account>() |
98 | { |
99 | public void push(Account event) |
100 | { |
101 | updateIdentity(); |
102 | } |
103 | }); |
104 | |
105 | // Register consumer for change in board current selection |
106 | // (to activate the matching drawing area view) |
107 | priority = EventsPriorities.SELECTED_BOARD_GUI_REFRESH; |
108 | _selectedBoardChannel.registerPushConsumer(priority, new PersistentConsumer<Board>() |
109 | { |
110 | public void push(Board board) |
111 | { |
112 | changeBoardSelection(board); |
113 | } |
114 | }); |
115 | |
116 | // Register consumer for board open after user request (and server response) |
117 | priority = EventsPriorities.BOARDSLIST_OPEN_BOARD; |
118 | _openBoardChannel.registerPushConsumer( priority, |
119 | new PersistentConsumer<BoardImageModel>() |
120 | { |
121 | public void push(BoardImageModel model) |
122 | { |
123 | openBoard(model); |
124 | } |
125 | }); |
126 | |
127 | // Listen to LEAVE_BOARD & BOARD_DEL_PARTICIPANT events in order to remove |
128 | // board image panel |
129 | // Also listen to MODIFY_BOARD event (that may also remove self from participants) |
130 | String constraint1 = Constraints.eventTypeWho( |
131 | _holder.getId(), EventType.EVT_LEAVE_BOARD, EventType.EVT_BOARD_DEL_PARTICIPANT); |
132 | String constraint2 = Constraints.eventType(EventType.EVT_MODIFY_BOARD); |
133 | String constraint = constraint1 + " || " + constraint2; |
134 | ConstraintEventFilter filter = new ConstraintEventFilter(constraint); |
135 | priority = EventsPriorities.BOARDSLIST_REMOVAL_UPDATE; |
136 | _eventsChannel.registerPushConsumer(priority, filter, new PersistentDeferredConsumer() |
137 | { |
138 | @Override protected void pushEvent(Event event) |
139 | { |
140 | removeBoard(event.getWhere(), event.getBoard()); |
141 | } |
142 | }); |
143 | |
144 | updateIdentity(); |
145 | } |
146 | |
147 | protected void switchFullScreen(boolean fullScreen) |
148 | { |
149 | View active = (View) EmptyableViewport.getEmptyablePort().getDockable( |
150 | DockingManager.CENTER_REGION); |
151 | DrawingAreaPanel activeArea = (DrawingAreaPanel) active.getContentPane(); |
152 | if (fullScreen) |
153 | { |
154 | activeArea.detachArea(); |
155 | _application.getMainFrame().setVisible(false); |
156 | _application.getMainFrame().getContentPane().remove(_toolbar); |
157 | } |
158 | else |
159 | { |
160 | _application.getMainFrame().getContentPane().add(_toolbar, BorderLayout.NORTH); |
161 | _application.getMainFrame().setVisible(true); |
162 | activeArea.attachArea(); |
163 | } |
164 | } |
165 | |
166 | protected void updateIdentity() |
167 | { |
168 | ResourceMap map = _context.getResourceMap(); |
169 | String title = map.getString( "usertitle", |
170 | _holder.getIdentity().getVisa(), |
171 | _holder.getIdentity().getName()); |
172 | _application.getMainFrame().setTitle(title); |
173 | } |
174 | |
175 | protected void changeBoardSelection(Board board) |
176 | { |
177 | if (board != null) |
178 | { |
179 | View area = ViewHelper.findDrawingArea(board); |
180 | if (area != null) |
181 | { |
182 | //#### Problem: BoardsList table does not have focus yet... |
183 | DockingHelper.selectView( |
184 | EmptyableViewport.getEmptyablePort(), area.getPersistentId()); |
185 | } |
186 | } |
187 | } |
188 | |
189 | protected void openBoard(BoardImageModel model) |
190 | { |
191 | DrawingAreaPanel area = new DrawingAreaPanel(new DrawingArea(model)); |
192 | View view = _viewFactory.createView( |
193 | ViewHelper.getBoardAreaViewId(model.getBoard()), area); |
194 | EmptyableViewport port = EmptyableViewport.getEmptyablePort(); |
195 | DockingManager.dock((Dockable) view, (DockingPort) port, DockingManager.CENTER_REGION); |
196 | } |
197 | |
198 | protected void removeBoard(int idBoard, Board board) |
199 | { |
200 | View area = ViewHelper.findDrawingArea(idBoard); |
201 | if ( (area != null) |
202 | && (board == null || !board.getParticipants().contains(_holder.getId()))) |
203 | { |
204 | DockingManager.close(area); |
205 | } |
206 | } |
207 | |
208 | final protected ApplicationContext _context; |
209 | final protected HiveGuiApplicationMain _application; |
210 | final protected ViewFactory _viewFactory; |
211 | final protected IdentityHolder _holder; |
212 | final protected JToolBar _toolbar; |
213 | final protected Channel<Object> _fullScreenMode; |
214 | final protected Channel<Object> _windowMode; |
215 | final protected Channel<Account> _identityChange; |
216 | final protected Channel<Board> _selectedBoardChannel; |
217 | final protected Channel<BoardImageModel> _openBoardChannel; |
218 | final protected Channel<Event> _eventsChannel; |
219 | } |