| 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.docking; |
| 16 | |
| 17 | import javax.swing.Action; |
| 18 | |
| 19 | import org.flexdock.view.View; |
| 20 | import org.jdesktop.application.ResourceMap; |
| 21 | |
| 22 | import net.sourceforge.hiveboard.Board; |
| 23 | import net.sourceforge.hiveboard.Event; |
| 24 | import net.sourceforge.hiveboard.EventType; |
| 25 | import net.sourceforge.hiveboard.event.EventsPriorities; |
| 26 | import net.sourceforge.hiveboard.event.PersistentDeferredConsumer; |
| 27 | import net.sourceforge.hiveboard.view.DrawingAreaPanel; |
| 28 | import net.sourceforge.hiveevents.Channel; |
| 29 | import net.sourceforge.hiveevents.Consumer; |
| 30 | import net.sourceforge.hiveevents.Filter; |
| 31 | import net.sourceforge.hivegui.docking.DefaultViewFactory; |
| 32 | |
| 33 | public class HiveBoardViewFactory extends DefaultViewFactory |
| 34 | { |
| 35 | public HiveBoardViewFactory(Action leaveBoardAction, Channel<Event> eventChannel) |
| 36 | { |
| 37 | _action = leaveBoardAction; |
| 38 | // Add consumer to board modifications |
| 39 | int priority = EventsPriorities.BOARD_DETAIL_UPDATE; |
| 40 | Filter<Event> filter = new Filter<Event>() |
| 41 | { |
| 42 | @SuppressWarnings("unchecked") |
| 43 | public boolean passEvent(Event event) |
| 44 | { |
| 45 | // check the event type first |
| 46 | if (event.getType() != EventType.EVT_MODIFY_BOARD) |
| 47 | { |
| 48 | return false; |
| 49 | } |
| 50 | // check if this board has an open drawing area currently |
| 51 | return ViewHelper.findDrawingArea(event.getWhere()) != null; |
| 52 | } |
| 53 | }; |
| 54 | Consumer<Event> consumer = new PersistentDeferredConsumer() |
| 55 | { |
| 56 | @Override protected void pushEvent(Event event) |
| 57 | { |
| 58 | View view = ViewHelper.findDrawingArea(event.getWhere()); |
| 59 | if (view != null) |
| 60 | { |
| 61 | setViewTitles(view); |
| 62 | } |
| 63 | } |
| 64 | }; |
| 65 | eventChannel.registerPushConsumer(priority, filter, consumer); |
| 66 | } |
| 67 | |
| 68 | @Override protected void initView(View view, String id) |
| 69 | { |
| 70 | // Special processing for board area views |
| 71 | if (ViewHelper.isOpenBoardAreaView(id)) |
| 72 | { |
| 73 | ResourceMap map = _context.getResourceMap(View.class); |
| 74 | |
| 75 | // Trick the resource injection to inject any other properties |
| 76 | view.setName(Views.BOARD_AREA.name()); |
| 77 | map.injectComponents(view); |
| 78 | view.setName(id); |
| 79 | |
| 80 | // Inject special properties |
| 81 | setViewTitles(view); |
| 82 | view.addAction(new BoardAreaCloseAction(_action)); |
| 83 | } |
| 84 | else |
| 85 | { |
| 86 | super.initView(view, id); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | protected void setViewTitles(View view) |
| 91 | { |
| 92 | ResourceMap map = _context.getResourceMap(View.class); |
| 93 | DrawingAreaPanel content = (DrawingAreaPanel) view.getContentPane(); |
| 94 | Board board = content.getDrawingArea().getBoardImageModel().getBoard(); |
| 95 | String key = Views.BOARD_AREA.name() + ".tabText"; |
| 96 | view.setTabText(map.getString(key, board.getName())); |
| 97 | key = Views.BOARD_AREA.name() + ".title"; |
| 98 | view.setTitle(map.getString(key, board.getName())); |
| 99 | } |
| 100 | |
| 101 | protected final Action _action; |
| 102 | } |