Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

ButtonBar.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.addons.ui_swing.framemanager.buttonbar;
  23. import com.dmdirc.FrameContainer;
  24. import com.dmdirc.FrameContainerComparator;
  25. import com.dmdirc.addons.ui_swing.UIUtilities;
  26. import com.dmdirc.config.IdentityManager;
  27. import com.dmdirc.interfaces.FrameInfoListener;
  28. import com.dmdirc.interfaces.NotificationListener;
  29. import com.dmdirc.interfaces.SelectionListener;
  30. import com.dmdirc.ui.IconManager;
  31. import com.dmdirc.ui.interfaces.FrameManager;
  32. import com.dmdirc.ui.interfaces.FramemanagerPosition;
  33. import com.dmdirc.ui.interfaces.Window;
  34. import com.dmdirc.util.MapList;
  35. import java.awt.Color;
  36. import java.awt.Dimension;
  37. import java.awt.Insets;
  38. import java.awt.event.ActionEvent;
  39. import java.awt.event.ActionListener;
  40. import java.awt.event.ComponentEvent;
  41. import java.awt.event.ComponentListener;
  42. import java.io.Serializable;
  43. import java.util.Collections;
  44. import java.util.HashMap;
  45. import java.util.List;
  46. import java.util.Map;
  47. import javax.swing.JComponent;
  48. import javax.swing.JPanel;
  49. import javax.swing.JToggleButton;
  50. import javax.swing.SwingConstants;
  51. import net.miginfocom.swing.MigLayout;
  52. /**
  53. * The button bar manager is a grid of buttons that presents a manager similar
  54. * to that used by mIRC.
  55. *
  56. * @author chris
  57. */
  58. public final class ButtonBar implements FrameManager, ActionListener,
  59. ComponentListener, Serializable, NotificationListener,
  60. SelectionListener, FrameInfoListener {
  61. /**
  62. * A version number for this class. It should be changed whenever the class
  63. * structure is changed (or anything else that would prevent serialized
  64. * objects being unserialized with the new class).
  65. */
  66. private static final long serialVersionUID = 3;
  67. /** A map of parent containers to their respective windows. */
  68. private final MapList<FrameContainer, FrameContainer> windows;
  69. /** A map of containers to the buttons we're using for them. */
  70. private final Map<FrameContainer, JToggleButton> buttons;
  71. /** The position of this frame manager. */
  72. private final FramemanagerPosition position;
  73. /** The parent for the manager. */
  74. private JComponent parent;
  75. /** The panel used for our buttons. */
  76. private final JPanel panel;
  77. /** The currently selected window. */
  78. private transient FrameContainer selected;
  79. /** Selected window. */
  80. private Window activeWindow;
  81. /** The number of buttons per row or column. */
  82. private int cells = 1;
  83. /** The number of buttons to render per {cell,row}. */
  84. private int maxButtons = Integer.MAX_VALUE;
  85. /** The width of buttons. */
  86. private int buttonWidth;
  87. /** Creates a new instance of DummyFrameManager. */
  88. public ButtonBar() {
  89. windows = new MapList<FrameContainer, FrameContainer>();
  90. buttons = new HashMap<FrameContainer, JToggleButton>();
  91. position = FramemanagerPosition.getPosition(
  92. IdentityManager.getGlobalConfig().getOption("ui", "framemanagerPosition"));
  93. if (position.isHorizontal()) {
  94. panel = new JPanel(new MigLayout("ins 0, fill, flowx"));
  95. } else {
  96. panel = new JPanel(new MigLayout("ins 0, fill, flowy"));
  97. }
  98. }
  99. /** {@inheritDoc} */
  100. @Override
  101. public void setParent(final JComponent parent) {
  102. this.parent = parent;
  103. parent.setLayout(new MigLayout());
  104. parent.add(panel);
  105. buttonWidth = position.isHorizontal() ? 150 : (parent.getWidth() - UIUtilities.SMALL_BORDER * 3) / cells;
  106. if (position.isHorizontal()) {
  107. maxButtons = parent.getWidth() / (buttonWidth + UIUtilities.SMALL_BORDER * 2);
  108. }
  109. parent.addComponentListener(this);
  110. }
  111. /**
  112. * Removes all buttons from the bar and readds them.
  113. */
  114. private void relayout() {
  115. panel.removeAll();
  116. for (Map.Entry<FrameContainer, List<FrameContainer>> entry : windows.entrySet()) {
  117. buttons.get(entry.getKey()).setPreferredSize(new Dimension(buttonWidth, 25));
  118. buttons.get(entry.getKey()).setMinimumSize(new Dimension(buttonWidth, 25));
  119. panel.add(buttons.get(entry.getKey()));
  120. Collections.sort(entry.getValue(), new FrameContainerComparator());
  121. for (FrameContainer child : entry.getValue()) {
  122. buttons.get(child).setPreferredSize(new Dimension(buttonWidth, 25));
  123. buttons.get(child).setMinimumSize(new Dimension(buttonWidth, 25));
  124. panel.add(buttons.get(child));
  125. }
  126. }
  127. panel.validate();
  128. }
  129. /**
  130. * Adds a button to the button array with the details from the specified
  131. * container.
  132. *
  133. * @param source The Container to get title/icon info from
  134. */
  135. private void addButton(final FrameContainer source) {
  136. final JToggleButton button = new JToggleButton(source.toString(),
  137. IconManager.getIconManager().getIcon(source.getIcon()));
  138. button.addActionListener(this);
  139. button.setHorizontalAlignment(SwingConstants.LEFT);
  140. button.setMargin(new Insets(0, 0, 0, 0));
  141. buttons.put(source, button);
  142. }
  143. /** {@inheritDoc} */
  144. @Override
  145. public boolean canPositionVertically() {
  146. return true;
  147. }
  148. /** {@inheritDoc} */
  149. @Override
  150. public boolean canPositionHorizontally() {
  151. return true;
  152. }
  153. /** {@inheritDoc} */
  154. @Override
  155. public void addWindow(final FrameContainer window) {
  156. windows.add(window);
  157. addButton(window);
  158. relayout();
  159. window.addNotificationListener(this);
  160. window.addSelectionListener(this);
  161. window.addFrameInfoListener(this);
  162. }
  163. /** {@inheritDoc} */
  164. @Override
  165. public void delWindow(final FrameContainer window) {
  166. windows.remove(window);
  167. relayout();
  168. window.removeNotificationListener(this);
  169. window.removeFrameInfoListener(this);
  170. window.removeSelectionListener(this);
  171. }
  172. /** {@inheritDoc} */
  173. @Override
  174. public void addWindow(final FrameContainer parent, final FrameContainer window) {
  175. windows.add(parent, window);
  176. addButton(window);
  177. relayout();
  178. window.addNotificationListener(this);
  179. window.addSelectionListener(this);
  180. window.addFrameInfoListener(this);
  181. }
  182. /** {@inheritDoc} */
  183. @Override
  184. public void delWindow(final FrameContainer parent, final FrameContainer window) {
  185. windows.remove(parent, window);
  186. relayout();
  187. window.removeNotificationListener(this);
  188. window.removeFrameInfoListener(this);
  189. window.removeSelectionListener(this);
  190. }
  191. /**
  192. * Called when the user clicks on one of the buttons.
  193. *
  194. * @param e The action event associated with this action
  195. */
  196. @Override
  197. public void actionPerformed(final ActionEvent e) {
  198. for (Map.Entry<FrameContainer, JToggleButton> entry : buttons.entrySet()) {
  199. if (entry.getValue().equals(e.getSource())) {
  200. if (entry.getKey().getFrame().equals(activeWindow)) {
  201. entry.getValue().setSelected(true);
  202. }
  203. entry.getKey().activateFrame();
  204. }
  205. }
  206. }
  207. /**
  208. * Called when the parent component is resized.
  209. *
  210. * @param e A ComponentEvent corresponding to this event.
  211. */
  212. @Override
  213. public void componentResized(final ComponentEvent e) {
  214. buttonWidth = position.isHorizontal() ? 150 : (parent.getWidth() - UIUtilities.SMALL_BORDER * 3) / cells;
  215. if (position.isHorizontal()) {
  216. maxButtons = parent.getWidth() / (buttonWidth + UIUtilities.SMALL_BORDER * 2);
  217. }
  218. relayout();
  219. }
  220. /**
  221. * Called when the parent component is moved.
  222. *
  223. * @param e A ComponentEvent corresponding to this event.
  224. */
  225. @Override
  226. public void componentMoved(final ComponentEvent e) {
  227. // Do nothing
  228. }
  229. /**
  230. * Called when the parent component is made visible.
  231. *
  232. * @param e A ComponentEvent corresponding to this event.
  233. */
  234. @Override
  235. public void componentShown(final ComponentEvent e) {
  236. // Do nothing
  237. }
  238. /**
  239. * Called when the parent component is made invisible.
  240. *
  241. * @param e A ComponentEvent corresponding to this event.
  242. */
  243. @Override
  244. public void componentHidden(final ComponentEvent e) {
  245. // Do nothing
  246. }
  247. /** {@inheritDoc} */
  248. @Override
  249. public void notificationSet(final Window window, final Color colour) {
  250. if (buttons.containsKey(window.getContainer())) {
  251. buttons.get(window.getContainer()).setForeground(colour);
  252. }
  253. }
  254. /** {@inheritDoc} */
  255. @Override
  256. public void notificationCleared(final Window window) {
  257. notificationSet(window, window.getContainer().getNotification());
  258. }
  259. /** {@inheritDoc} */
  260. @Override
  261. public void selectionChanged(final Window window) {
  262. activeWindow = window;
  263. if (selected != null && buttons.containsKey(selected)) {
  264. buttons.get(selected).setSelected(false);
  265. }
  266. selected = window.getContainer();
  267. if (buttons.containsKey(window.getContainer())) {
  268. buttons.get(window.getContainer()).setSelected(true);
  269. }
  270. }
  271. /** {@inheritDoc} */
  272. @Override
  273. public void iconChanged(final Window window, final String icon) {
  274. buttons.get(window.getContainer()).setIcon(IconManager.getIconManager().getIcon(icon));
  275. }
  276. /** {@inheritDoc} */
  277. @Override
  278. public void nameChanged(final Window window, final String name) {
  279. buttons.get(window.getContainer()).setText(name);
  280. }
  281. }