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.

StepLayout.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /*
  2. * Copyright (c) 2006-2011 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.wizard;
  23. import java.awt.Component;
  24. import java.awt.Container;
  25. import java.awt.Dimension;
  26. import java.awt.Insets;
  27. import java.awt.LayoutManager2;
  28. import java.io.Serializable;
  29. import java.util.ArrayList;
  30. import java.util.List;
  31. /**
  32. * Adjusted Card layout.
  33. */
  34. public class StepLayout implements LayoutManager2, Serializable {
  35. /**
  36. * A version number for this class. It should be changed whenever the class
  37. * structure is changed (or anything else that would prevent serialized
  38. * objects being unserialized with the new class).
  39. */
  40. private static final long serialVersionUID = 2;
  41. /** Parent container. */
  42. private Container parent;
  43. /** Cards vector. */
  44. private final List<Step> steps;
  45. /** Current step. */
  46. private int currentStep;
  47. /** Vertical gap. */
  48. private int vGap;
  49. /** Horiontal gap. */
  50. private int hGap;
  51. /**
  52. * Instantiates a new step layout.
  53. */
  54. public StepLayout() {
  55. this(0, 0);
  56. }
  57. /**
  58. * Instantiates a new step layout.
  59. *
  60. * @param parent Parent component
  61. */
  62. public StepLayout(final Container parent) {
  63. this(0, 0, parent);
  64. }
  65. /**
  66. * Instantiates a new step layout with the specified gaps.
  67. *
  68. * @param hGap Horizontal gap
  69. * @param vGap Vertical gap
  70. */
  71. public StepLayout(final int hGap, final int vGap) {
  72. steps = new ArrayList<Step>();
  73. currentStep = -1;
  74. this.hGap = hGap;
  75. this.vGap = vGap;
  76. }
  77. /**
  78. * Instantiates a new step layout with the specified gaps.
  79. *
  80. * @param hGap Horizontal gap
  81. * @param vGap Vertical gap
  82. * @param parent Parent component
  83. */
  84. public StepLayout(final int hGap, final int vGap, final Container parent) {
  85. steps = new ArrayList<Step>();
  86. currentStep = -1;
  87. this.hGap = hGap;
  88. this.vGap = vGap;
  89. this.parent = parent;
  90. }
  91. /**
  92. * Returns the number of steps in the layout.
  93. *
  94. * @return number of steps >= 0
  95. */
  96. public int size() {
  97. return steps.size();
  98. }
  99. /**
  100. * Checks if the layout is empty
  101. *
  102. * @return true iif the layout has no steps
  103. */
  104. public boolean isEmpty() {
  105. return steps.isEmpty();
  106. }
  107. /**
  108. * Returns the specified step from the layout.
  109. *
  110. * @param index Step to retrieve
  111. *
  112. * @return Step
  113. */
  114. public Step getStep(final int index) {
  115. return steps.get(index);
  116. }
  117. /**
  118. * Returns the step list.
  119. *
  120. * @return List of steps
  121. */
  122. public List getSteps() {
  123. return steps;
  124. }
  125. /**
  126. * Show the first step.
  127. *
  128. * @param parent Parent container
  129. */
  130. public void first(final Container parent) {
  131. show(0, parent);
  132. }
  133. /**
  134. * Show the last step.
  135. *
  136. * @param parent Parent container
  137. */
  138. public void last(final Container parent) {
  139. show(parent.getComponentCount() - 1, parent);
  140. }
  141. /**
  142. * Show the next step.
  143. *
  144. * @param parent Parent container
  145. */
  146. public void next(final Container parent) {
  147. show(currentStep + 1, parent);
  148. }
  149. /**
  150. * Show the previous step.
  151. *
  152. * @param parent Parent container
  153. */
  154. public void previous(final Container parent) {
  155. show(currentStep - 1, parent);
  156. }
  157. /**
  158. * Show the specified step.
  159. *
  160. * @param step Step to show
  161. * @param parent Parent container
  162. */
  163. public void show(final Step step, final Container parent) {
  164. show(steps.indexOf(step), parent);
  165. }
  166. /**
  167. * Show the step at the specified index.
  168. *
  169. * @param step Step to show
  170. * @param parent Parent container
  171. */
  172. public void show(final int step, final Container parent) {
  173. int stepNumber = step;
  174. if (stepNumber == -1) {
  175. if (stepNumber >= steps.size()) {
  176. stepNumber = steps.size() - 1;
  177. } else {
  178. stepNumber = 0;
  179. }
  180. }
  181. synchronized (parent.getTreeLock()) {
  182. int componentCount = parent.getComponentCount();
  183. for (int i = 0; i < componentCount; i++) {
  184. Component comp = parent.getComponent(i);
  185. if (comp.isVisible()) {
  186. comp.setVisible(false);
  187. break;
  188. }
  189. }
  190. if (componentCount > 0) {
  191. currentStep = stepNumber;
  192. parent.getComponent(currentStep).setVisible(true);
  193. parent.validate();
  194. }
  195. }
  196. }
  197. /** {@inheritDoc} */
  198. @Override
  199. public void addLayoutComponent(final Component comp,
  200. final Object constraints) {
  201. if (!(comp instanceof Step)) {
  202. throw new IllegalArgumentException("Component must be an instance of Step");
  203. }
  204. addLayoutComponent((Step) comp);
  205. }
  206. /**
  207. * {@inheritDoc}
  208. *
  209. * @deprecated Use addLayoutComponent(Component, Object) or
  210. * addLayoutComponent(Component)
  211. *
  212. * @see addLayoutComponent(Component)
  213. * @see addLayoutComponent(Component, Object)
  214. */
  215. @Override
  216. @Deprecated
  217. public void addLayoutComponent(final String name, final Component comp) {
  218. if (!(comp instanceof Step)) {
  219. throw new IllegalArgumentException("Component must be an instance of Step");
  220. }
  221. addLayoutComponent((Step) comp);
  222. }
  223. /**
  224. * Adds a component to the layout.
  225. *
  226. * @param step Component to add
  227. */
  228. public void addLayoutComponent(final Step step) {
  229. synchronized (step.getTreeLock()) {
  230. if (!steps.isEmpty()) {
  231. step.setVisible(false);
  232. }
  233. steps.add(step);
  234. }
  235. }
  236. /** {@inheritDoc} */
  237. @Override
  238. public void removeLayoutComponent(final Component comp) {
  239. synchronized (comp.getTreeLock()) {
  240. if (comp.isVisible()) {
  241. comp.setVisible(false);
  242. }
  243. next(comp.getParent());
  244. steps.remove(comp);
  245. }
  246. }
  247. /**
  248. * {@inheritDoc}
  249. *
  250. * @return Returns the preferred size of the container
  251. */
  252. @Override
  253. public Dimension preferredLayoutSize(final Container parent) {
  254. synchronized (parent.getTreeLock()) {
  255. Insets insets = parent.getInsets();
  256. int componentCount = parent.getComponentCount();
  257. int width = 0;
  258. int height = 0;
  259. for (int i = 0; i < componentCount; i++) {
  260. Component comp = parent.getComponent(i);
  261. Dimension preferredDimension = comp.getPreferredSize();
  262. if (preferredDimension.width > width) {
  263. width = preferredDimension.width;
  264. }
  265. if (preferredDimension.height > height) {
  266. height = preferredDimension.height;
  267. }
  268. }
  269. return new Dimension(insets.left + insets.right + width + hGap * 2,
  270. insets.top + insets.bottom + height + vGap * 2);
  271. }
  272. }
  273. /**
  274. * {@inheritDoc}
  275. *
  276. * @return Returns the minimum size of the container
  277. */
  278. @Override
  279. public Dimension minimumLayoutSize(final Container parent) {
  280. synchronized (parent.getTreeLock()) {
  281. Insets insets = parent.getInsets();
  282. int componentCount = parent.getComponentCount();
  283. int width = 0;
  284. int height = 0;
  285. for (int i = 0; i < componentCount; i++) {
  286. Component comp = parent.getComponent(i);
  287. Dimension minimumDimension = comp.getMinimumSize();
  288. if (minimumDimension.width > width) {
  289. width = minimumDimension.width;
  290. }
  291. if (minimumDimension.height > height) {
  292. height = minimumDimension.height;
  293. }
  294. }
  295. return new Dimension(insets.left + insets.right + width + hGap * 2,
  296. insets.top + insets.bottom + height + vGap * 2);
  297. }
  298. }
  299. /**
  300. * {@inheritDoc}
  301. *
  302. * @param parent Container to get the size for
  303. *
  304. * @return Returns the maximum size of the container
  305. */
  306. @Override
  307. public Dimension maximumLayoutSize(final Container parent) {
  308. return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
  309. }
  310. /**
  311. * {@inheritDoc}
  312. *
  313. * @param target Container to get the alignment from
  314. *
  315. * @return Alignment
  316. */
  317. @Override
  318. public float getLayoutAlignmentX(final Container target) {
  319. return 0.5f;
  320. }
  321. /**
  322. * {@inheritDoc}
  323. *
  324. * @param target Container to get the alignment from
  325. *
  326. * @return Alignment
  327. */
  328. @Override
  329. public float getLayoutAlignmentY(final Container target) {
  330. return 0.5f;
  331. }
  332. /**
  333. * {@inheritDoc}
  334. *
  335. * @param target Container to invalidate
  336. */
  337. @Override
  338. public void invalidateLayout(final Container target) {
  339. //Ignore
  340. }
  341. /** {@inheritDoc} */
  342. @Override
  343. public void layoutContainer(final Container parent) {
  344. synchronized (parent.getTreeLock()) {
  345. Insets insets = parent.getInsets();
  346. int componentCount = parent.getComponentCount();
  347. Component comp = null;
  348. boolean currentFound = false;
  349. for (int i = 0; i < componentCount; i++) {
  350. comp = parent.getComponent(i);
  351. comp.setBounds(hGap + insets.left, vGap + insets.top,
  352. parent.getWidth() - (hGap * 2 + insets.left +
  353. insets.right), parent.getHeight() - (vGap * 2 +
  354. insets.top + insets.bottom));
  355. if (comp.isVisible()) {
  356. currentFound = true;
  357. }
  358. }
  359. if (!currentFound && componentCount > 0) {
  360. parent.getComponent(0).setVisible(true);
  361. }
  362. }
  363. }
  364. }