Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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.components;
  23. import com.dmdirc.config.ConfigManager;
  24. import com.dmdirc.config.IdentityManager;
  25. import com.dmdirc.interfaces.ConfigChangeListener;
  26. import com.dmdirc.addons.ui_swing.UIUtilities;
  27. import java.awt.Component;
  28. import javax.swing.JSplitPane;
  29. /**
  30. * JSplit pane that snaps around its components preferred size.
  31. */
  32. public class SplitPane extends JSplitPane implements ConfigChangeListener {
  33. /**
  34. * A version number for this class. It should be changed whenever the class
  35. * structure is changed (or anything else that would prevent serialized
  36. * objects being unserialized with the new class).
  37. */
  38. private static final long serialVersionUID = 2;
  39. /** Snap distance. */
  40. private final int snapDistance;
  41. /** Component to snap to? */
  42. private boolean leftComponentSnap = true;
  43. /** use one touch expandable? */
  44. private boolean useOneTouchExpandable;
  45. /** Global config manager. */
  46. private ConfigManager config;
  47. /** Orientation type . */
  48. public enum Orientation {
  49. /** Horizontal orientation. */
  50. HORIZONTAL,
  51. /** Vertical orientation. */
  52. VERTICAL
  53. }
  54. /**
  55. * Instantiates a new snapping split pane. Defaults to using a horizontal
  56. * split, two null components and snapping to the left component.
  57. */
  58. public SplitPane() {
  59. this(Orientation.HORIZONTAL);
  60. }
  61. /**
  62. * Instantiates a new snapping split pane. Defaults to using a horizontal
  63. * split, two null components and snapping to the left component.
  64. *
  65. * @param snapDistance Distance to around the preferred size
  66. */
  67. public SplitPane(final int snapDistance) {
  68. this(Orientation.HORIZONTAL, snapDistance);
  69. }
  70. /**
  71. * Instantiates a new snapping split pane. Defaults to using a horizontal
  72. * split and two null components.
  73. *
  74. * @param leftComponentSnap Snap to the preferred size of the left component?
  75. */
  76. public SplitPane(final boolean leftComponentSnap) {
  77. this(Orientation.HORIZONTAL, leftComponentSnap, 10);
  78. }
  79. /**
  80. * Instantiates a new snapping split pane. Defaults to using a horizontal
  81. * split and two null components.
  82. *
  83. * @param leftComponentSnap Snap to the preferred size of the left component?
  84. * @param snapDistance Distance to around the preferred size
  85. */
  86. public SplitPane(final boolean leftComponentSnap,
  87. final int snapDistance) {
  88. this(Orientation.HORIZONTAL, leftComponentSnap, snapDistance);
  89. }
  90. /**
  91. * Instantiates a new snapping split pane. Defaults to using two null
  92. * components and snapping to the left component.
  93. *
  94. * @param orientation Split pane orientation
  95. * <code>JSplitPane.HORIZONTAL_SPLIT</code> or
  96. * <code>JSplitPane.VERTICAL_SPLIT</code>
  97. */
  98. public SplitPane(final Orientation orientation) {
  99. this(orientation, null, null, 10);
  100. }
  101. /**
  102. * Instantiates a new snapping split pane. Defaults to using two null
  103. * components and snapping to the left component.
  104. *
  105. * @param orientation Split pane orientation
  106. * <code>JSplitPane.HORIZONTAL_SPLIT</code> or
  107. * <code>JSplitPane.VERTICAL_SPLIT</code>
  108. * @param snapDistance Distance to around the preferred size
  109. */
  110. public SplitPane(final Orientation orientation,
  111. final int snapDistance) {
  112. this(orientation, null, null, snapDistance);
  113. }
  114. /**
  115. * Instantiates a new snapping split pane. Defaults to using two null
  116. * components.
  117. *
  118. * @param orientation Split pane orientation
  119. * <code>JSplitPane.HORIZONTAL_SPLIT</code> or
  120. * <code>JSplitPane.VERTICAL_SPLIT</code>
  121. * @param leftComponentSnap Snap to the preferred size of the left component?
  122. */
  123. public SplitPane(final Orientation orientation,
  124. final boolean leftComponentSnap) {
  125. this(orientation, null, null, leftComponentSnap, 10);
  126. }
  127. /**
  128. * Instantiates a new snapping split pane. Defaults to using two null
  129. * components.
  130. *
  131. * @param orientation Split pane orientation
  132. * <code>JSplitPane.HORIZONTAL_SPLIT</code> or
  133. * <code>JSplitPane.VERTICAL_SPLIT</code>
  134. * @param leftComponentSnap Snap to the preferred size of the left component?
  135. * @param snapDistance Distance to around the preferred size
  136. */
  137. public SplitPane(final Orientation orientation,
  138. final boolean leftComponentSnap, final int snapDistance) {
  139. this(orientation, null, null, leftComponentSnap, snapDistance);
  140. }
  141. /**
  142. * Instantiates a new snapping split pane. Defaults to using two null
  143. * components and snapping to the left component.
  144. *
  145. * @param orientation Split pane orientation
  146. * <code>JSplitPane.HORIZONTAL_SPLIT</code> or
  147. * <code>JSplitPane.VERTICAL_SPLIT</code>
  148. * @param leftComponent left component
  149. * @param rightComponent right component
  150. */
  151. public SplitPane(final Orientation orientation,
  152. final Component leftComponent, final Component rightComponent) {
  153. this(orientation, leftComponent, rightComponent, true, 10);
  154. }
  155. /**
  156. * Instantiates a new snapping split pane. Defaults to using two null
  157. * components and snapping to the left component.
  158. *
  159. * @param orientation Split pane orientation
  160. * <code>JSplitPane.HORIZONTAL_SPLIT</code> or
  161. * <code>JSplitPane.VERTICAL_SPLIT</code>
  162. * @param leftComponent left component
  163. * @param rightComponent right component
  164. * @param snapDistance Distance to around the preferred size
  165. */
  166. public SplitPane(final Orientation orientation,
  167. final Component leftComponent, final Component rightComponent,
  168. final int snapDistance) {
  169. this(orientation, leftComponent, rightComponent, true, snapDistance);
  170. }
  171. /**
  172. * Instantiates a new snapping split pane. Defaults to using two null
  173. * components.
  174. *
  175. * @param orientation Split pane orientation
  176. * <code>JSplitPane.HORIZONTAL_SPLIT</code> or
  177. * <code>JSplitPane.VERTICAL_SPLIT</code>
  178. * @param leftComponent left component
  179. * @param rightComponent right component
  180. * @param leftComponentSnap Snap to the preferred size of the left component?
  181. */
  182. public SplitPane(final Orientation orientation,
  183. final Component leftComponent, final Component rightComponent,
  184. final boolean leftComponentSnap) {
  185. this(orientation, leftComponent, rightComponent, leftComponentSnap, 10);
  186. }
  187. /**
  188. * Instantiates a new snapping split pane. Defaults to using two null
  189. * components.
  190. *
  191. * @param orientation Split pane orientation
  192. * <code>JSplitPane.HORIZONTAL_SPLIT</code> or
  193. * <code>JSplitPane.VERTICAL_SPLIT</code>
  194. * @param leftComponent left component
  195. * @param rightComponent right component
  196. * @param leftComponentSnap Snap to the preferred size of the left component?
  197. * @param snapDistance Distance to around the preferred size
  198. */
  199. public SplitPane(final Orientation orientation,
  200. final Component leftComponent, final Component rightComponent,
  201. final boolean leftComponentSnap, final int snapDistance) {
  202. super((orientation.equals(Orientation.HORIZONTAL)) ? HORIZONTAL_SPLIT : VERTICAL_SPLIT,
  203. true, leftComponent, rightComponent);
  204. this.leftComponentSnap = leftComponentSnap;
  205. this.snapDistance = snapDistance;
  206. config = IdentityManager.getGlobalConfig();
  207. useOneTouchExpandable = config.getOptionBool("ui", "useOneTouchExpandable");
  208. setOneTouchExpandable(useOneTouchExpandable);
  209. setContinuousLayout(true);
  210. getActionMap().setParent(null);
  211. getActionMap().clear();
  212. config.addChangeListener("ui", "useOneTouchExpandable", this);
  213. }
  214. /** {@inheritDoc} */
  215. @Override
  216. public void configChanged(final String domain, final String key) {
  217. useOneTouchExpandable = config.getOptionBool("ui", "useOneTouchExpandable");
  218. UIUtilities.invokeLater(new Runnable() {
  219. /** {@inheritDoc} */
  220. @Override
  221. public void run() {
  222. setOneTouchExpandable(useOneTouchExpandable);
  223. }
  224. });
  225. }
  226. }