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

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