| 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.Component; |
| 18 | import java.util.SortedMap; |
| 19 | import java.util.TreeMap; |
| 20 | |
| 21 | import javax.swing.Action; |
| 22 | import javax.swing.JMenu; |
| 23 | import javax.swing.JMenuItem; |
| 24 | import javax.swing.JPopupMenu; |
| 25 | |
| 26 | import org.jdesktop.application.Resource; |
| 27 | |
| 28 | import net.sourceforge.hiveboard.Account; |
| 29 | import net.sourceforge.hiveboard.WhiteBoardUserService; |
| 30 | import net.sourceforge.hiveboard.command.GeneralSetWriter; |
| 31 | import net.sourceforge.hiveboard.model.AccountRepository; |
| 32 | import net.sourceforge.hiveboard.util.GuiContext; |
| 33 | import net.sourceforge.hiveboard.util.GuiContextListener; |
| 34 | import net.sourceforge.hiveboard.util.TokenSetterHelper; |
| 35 | import net.sourceforge.hivegui.application.ApplicationContextHolder; |
| 36 | import net.sourceforge.hivegui.application.HiveGuiApplicationMain; |
| 37 | |
| 38 | /** |
| 39 | * Dynamically handles the content of the popup menu that shows in full screen |
| 40 | * mode. It is completely event-driven and dynamically updates the list of |
| 41 | * present participants to the currently showing whiteboard, instantiates |
| 42 | * commands for each of them, updates labels when needed (ie when visa or name |
| 43 | * changes)... |
| 44 | * |
| 45 | * @author Jean-Francois Poilpret |
| 46 | */ |
| 47 | public class FullScreenPopupHandler implements GuiContextListener |
| 48 | { |
| 49 | public FullScreenPopupHandler( JPopupMenu popup, |
| 50 | WhiteBoardUserService service, |
| 51 | AccountRepository accounts, |
| 52 | ApplicationContextHolder holder) |
| 53 | { |
| 54 | _application = holder.getApplication(); |
| 55 | _popup = popup; |
| 56 | _service = service; |
| 57 | _accounts = accounts; |
| 58 | |
| 59 | // Find the "set-any-writer" menu for later update |
| 60 | _setWriterMenu = findSetWriterMenu(); |
| 61 | |
| 62 | holder.getContext().getResourceMap(FullScreenPopupHandler.class).injectFields(this); |
| 63 | } |
| 64 | |
| 65 | public void setGuiContext(GuiContext context) |
| 66 | { |
| 67 | context.addContextListener(this); |
| 68 | } |
| 69 | |
| 70 | public JPopupMenu getPopup() |
| 71 | { |
| 72 | return _popup; |
| 73 | } |
| 74 | |
| 75 | private JMenu findSetWriterMenu() |
| 76 | { |
| 77 | // Find the "set-any-writer" menu for later update |
| 78 | for (int i = 0; i < _popup.getComponentCount(); i++) |
| 79 | { |
| 80 | Component comp = _popup.getComponent(i); |
| 81 | if (comp instanceof JMenu) |
| 82 | { |
| 83 | JMenu menu = (JMenu) comp; |
| 84 | if ("set-any-writer".equals(menu.getActionCommand())) |
| 85 | { |
| 86 | return menu; |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | return null; |
| 91 | } |
| 92 | |
| 93 | public void contextChanged(GuiContext context) |
| 94 | { |
| 95 | if (context.getDrawBoard() == null) |
| 96 | { |
| 97 | // Nothing to do, we cannot be in fullscreen mode currently! |
| 98 | return; |
| 99 | } |
| 100 | // Check that we can give the token to participants |
| 101 | boolean enabled = TokenSetterHelper.canGiveToken( context.getBoard(), |
| 102 | context.getIdentity().getId()); |
| 103 | _setWriterMenu.removeAll(); |
| 104 | _setWriterMenu.setEnabled(enabled); |
| 105 | if (!enabled) |
| 106 | { |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | // Reset content of "set-any-writer" menu item |
| 111 | int where = context.getBoard().getId(); |
| 112 | SortedMap<String, JMenuItem> presents = new TreeMap<String, JMenuItem>(); |
| 113 | for (int who: context.getBoard().getPresents()) |
| 114 | { |
| 115 | Account account = _accounts.getAccount(who); |
| 116 | JMenuItem item = new JMenuItem(); |
| 117 | Action command = GeneralSetWriter.createAction( |
| 118 | _application, _service, account, where); |
| 119 | command.putValue(Action.NAME, getMenuLabel(account)); |
| 120 | item.setAction(command); |
| 121 | presents.put((String) command.getValue(Action.NAME), item); |
| 122 | } |
| 123 | // Add each item in the correct sort order |
| 124 | for (JMenuItem item: presents.values()) |
| 125 | { |
| 126 | _setWriterMenu.add(item); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | protected String getMenuLabel(Account who) |
| 131 | { |
| 132 | return String.format( _formatGiveToken, |
| 133 | who.getVisa(), |
| 134 | who.getName()); |
| 135 | } |
| 136 | |
| 137 | private final HiveGuiApplicationMain _application; |
| 138 | private final JPopupMenu _popup; |
| 139 | private final WhiteBoardUserService _service; |
| 140 | private final AccountRepository _accounts; |
| 141 | private final JMenu _setWriterMenu; |
| 142 | |
| 143 | @Resource(key = "Popup.formatGiveToken") |
| 144 | private String _formatGiveToken; |
| 145 | } |