You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FrameContainer.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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;
  23. import com.dmdirc.config.ConfigManager;
  24. import com.dmdirc.interfaces.ConfigChangeListener;
  25. import com.dmdirc.interfaces.FrameInfoListener;
  26. import com.dmdirc.interfaces.NotificationListener;
  27. import com.dmdirc.interfaces.SelectionListener;
  28. import com.dmdirc.ui.interfaces.Window;
  29. import com.dmdirc.util.ListenerList;
  30. import java.awt.Color;
  31. /**
  32. * The frame container implements basic methods that should be present in
  33. * all objects that handle a frame.
  34. *
  35. * @author chris
  36. */
  37. public abstract class FrameContainer {
  38. /** Logger to use. */
  39. private static final java.util.logging.Logger LOGGER = java.util.logging
  40. .Logger.getLogger(FrameContainer.class.getName());
  41. /** The colour of our frame's notifications. */
  42. protected Color notification = Color.BLACK;
  43. /** A list of listeners for this containers's events. */
  44. protected final ListenerList listeners = new ListenerList();
  45. /** The name of the icon being used for this container's frame. */
  46. private String icon;
  47. /** The name of this container. */
  48. private String name;
  49. /** The config manager for this container. */
  50. private final ConfigManager config;
  51. /** The IconChanger for this container. */
  52. private final IconChanger changer = new IconChanger();
  53. /**
  54. * Instantiate new frame container.
  55. *
  56. * @param icon The icon to use for this container
  57. * @param name The name of this container
  58. * @param config The config manager for this container
  59. * @since 0.6.3m2
  60. */
  61. public FrameContainer(final String icon, final String name, final ConfigManager config) {
  62. this.config = config;
  63. this.name = name;
  64. setIcon(icon);
  65. }
  66. /**
  67. * Returns the internal frame associated with this object.
  68. *
  69. * @return The internal frame associated with this object
  70. */
  71. public abstract Window getFrame();
  72. /** {@inheritDoc} */
  73. @Override
  74. public String toString() {
  75. return name;
  76. }
  77. /**
  78. * Retrieves the name of this container.
  79. *
  80. * @return This container's name
  81. * @since 0.6.3m2
  82. */
  83. public String getName() {
  84. return name;
  85. }
  86. /**
  87. * Changes the name of this container, and notifies any
  88. * {@link FrameInfoListener}s of the change.
  89. *
  90. * @param name The new name for this frame.
  91. */
  92. protected void setName(final String name) {
  93. this.name = name;
  94. synchronized (listeners) {
  95. for (FrameInfoListener listener : listeners.get(FrameInfoListener.class)) {
  96. listener.nameChanged(getFrame(), name);
  97. }
  98. }
  99. }
  100. /**
  101. * Closes this container (and it's associated frame).
  102. */
  103. public void close() {
  104. if (getFrame() == null) {
  105. throw new IllegalStateException("No frame associated with this container!");
  106. } else {
  107. getFrame().close();
  108. }
  109. }
  110. /**
  111. * Returns the server instance associated with this container.
  112. *
  113. * @return the associated server connection
  114. */
  115. public abstract Server getServer();
  116. /**
  117. * Sets the icon to be used by this frame container.
  118. *
  119. * @param icon The new icon to be used
  120. */
  121. public final void setIcon(final String icon) {
  122. this.icon = icon;
  123. iconUpdated();
  124. config.removeListener(changer);
  125. config.addChangeListener("icon", icon, changer);
  126. }
  127. /**
  128. * Called when this container's icon is updated.
  129. */
  130. private void iconUpdated() {
  131. synchronized (listeners) {
  132. for (FrameInfoListener listener : listeners.get(FrameInfoListener.class)) {
  133. listener.iconChanged(getFrame(), icon);
  134. }
  135. }
  136. }
  137. /**
  138. * Retrieves the name of the icon used by this container's window.
  139. *
  140. * @return This container's icon
  141. */
  142. public final String getIcon() {
  143. return icon;
  144. }
  145. /**
  146. * Returns the config manager for this container.
  147. *
  148. * @return the associated config manager
  149. */
  150. public final ConfigManager getConfigManager() {
  151. return config;
  152. }
  153. /**
  154. * Requests that this object's frame be activated.
  155. */
  156. @Precondition("getFrame() does not return null")
  157. public void activateFrame() {
  158. final Window window = getFrame();
  159. if (window == null) {
  160. throw new IllegalStateException("Cannot activate frame '"
  161. + getName() + "' while window is null");
  162. } else {
  163. window.activateFrame();
  164. }
  165. }
  166. /**
  167. * Clears any outstanding notifications this frame has set.
  168. */
  169. protected void clearNotification() {
  170. LOGGER.finer(toString() + ": clearNotification(): frame = "
  171. + (getFrame() == null ? null : getFrame().getClass().getName()));
  172. // TODO: This should default ot something colour independent
  173. notification = Color.BLACK;
  174. synchronized (listeners) {
  175. for (NotificationListener listener : listeners.get(NotificationListener.class)) {
  176. listener.notificationCleared(getFrame());
  177. }
  178. }
  179. }
  180. /**
  181. * Sends a notification to the frame manager if this fame isn't active.
  182. *
  183. * @param colour The colour to use for the notification
  184. */
  185. public void sendNotification(final Color colour) {
  186. final Window activeFrame = Main.getUI().getActiveWindow();
  187. if (activeFrame != null && !activeFrame.equals(getFrame())
  188. && !colour.equals(notification)) {
  189. notification = colour;
  190. synchronized (listeners) {
  191. for (NotificationListener listener : listeners.get(NotificationListener.class)) {
  192. listener.notificationSet(getFrame(), colour);
  193. }
  194. }
  195. }
  196. }
  197. /**
  198. * Retrieves the current notification colour of this channel.
  199. *
  200. * @return This channel's notification colour
  201. */
  202. public Color getNotification() {
  203. return notification;
  204. }
  205. /**
  206. * Determines if the specified frame is owned by this object.
  207. *
  208. * @param target Window to check ownership of
  209. * @return True iff frame is owned by this container, false otherwise
  210. */
  211. public boolean ownsFrame(final Window target) {
  212. return getFrame().equals(target);
  213. }
  214. /**
  215. * Invoked when our window has been opened.
  216. */
  217. public void windowOpened() {
  218. if (config == null || getFrame() == null) {
  219. return;
  220. }
  221. }
  222. /**
  223. * Invoked when our window is closing.
  224. */
  225. public abstract void windowClosing();
  226. /**
  227. * Invoked when our window has been closed.
  228. */
  229. public void windowClosed() {
  230. // Ignore.
  231. }
  232. /**
  233. * Invoked when our window is activated.
  234. */
  235. public void windowActivated() {
  236. LOGGER.finer(toString() + ": windowActivated(): frame = "
  237. + (getFrame() == null ? null : getFrame().getClass().getName()));
  238. if (getFrame() == null) {
  239. return;
  240. }
  241. synchronized (listeners) {
  242. for (SelectionListener listener : listeners.get(SelectionListener.class)) {
  243. listener.selectionChanged(getFrame());
  244. }
  245. }
  246. clearNotification();
  247. if (getServer() != null) {
  248. getServer().setActiveFrame(this);
  249. }
  250. }
  251. /**
  252. * Invoked when our window is deactivated.
  253. */
  254. public void windowDeactivated() {
  255. LOGGER.finer(toString() + ": windowDeactivated(): frame = "
  256. + (getFrame() == null ? null : getFrame().getClass().getName()));
  257. }
  258. /**
  259. * Adds a line to this container's window. If the window is null for some
  260. * reason, the line is silently discarded.
  261. *
  262. * @param type The message type to use
  263. * @param args The message's arguments
  264. */
  265. protected void addLine(final String type, final Object ... args) {
  266. if (getFrame() != null) {
  267. getFrame().addLine(type, args);
  268. }
  269. }
  270. /**
  271. * Adds a line to this container's window. If the window is null for some
  272. * reason, the line is silently discarded.
  273. *
  274. * @param type The message type to use
  275. * @param args The message's arguments
  276. */
  277. protected void addLine(final StringBuffer type, final Object ... args) {
  278. if (getFrame() != null) {
  279. getFrame().addLine(type, args);
  280. }
  281. }
  282. /**
  283. * Adds a notification listener for this frame container.
  284. *
  285. * @param listener The listener to be added
  286. */
  287. public void addNotificationListener(final NotificationListener listener) {
  288. synchronized (listeners) {
  289. listeners.add(NotificationListener.class, listener);
  290. }
  291. }
  292. /**
  293. * Removes a notification listener from this frame container.
  294. *
  295. * @param listener The listener to be removed
  296. */
  297. public void removeNotificationListener(final NotificationListener listener) {
  298. synchronized (listeners) {
  299. listeners.remove(NotificationListener.class, listener);
  300. }
  301. }
  302. /**
  303. * Adds a selection listener for this frame container.
  304. *
  305. * @param listener The listener to be added
  306. */
  307. public void addSelectionListener(final SelectionListener listener) {
  308. synchronized (listeners) {
  309. listeners.add(SelectionListener.class, listener);
  310. }
  311. }
  312. /**
  313. * Removes a selection listener from this frame container.
  314. *
  315. * @param listener The listener to be removed
  316. */
  317. public void removeSelectionListener(final SelectionListener listener) {
  318. synchronized (listeners) {
  319. listeners.remove(SelectionListener.class, listener);
  320. }
  321. }
  322. /**
  323. * Adds a frame info listener for this frame container.
  324. *
  325. * @param listener The listener to be added
  326. */
  327. public void addFrameInfoListener(final FrameInfoListener listener) {
  328. synchronized (listeners) {
  329. listeners.add(FrameInfoListener.class, listener);
  330. }
  331. }
  332. /**
  333. * Removes a frame info listener from this frame container.
  334. *
  335. * @param listener The listener to be removed
  336. */
  337. public void removeFrameInfoListener(final FrameInfoListener listener) {
  338. synchronized (listeners) {
  339. listeners.remove(FrameInfoListener.class, listener);
  340. }
  341. }
  342. /**
  343. * Updates the icon of this frame if its config setting is changed.
  344. */
  345. private class IconChanger implements ConfigChangeListener {
  346. /** {@inheritDoc} */
  347. @Override
  348. public void configChanged(final String domain, final String key) {
  349. iconUpdated();
  350. }
  351. }
  352. }