/ui/src/com/bluemarsh/jswat/ui/breakpoint/Breakpoints.java

http://jswat.googlecode.com/ · Java · 152 lines · 63 code · 9 blank · 80 comment · 14 complexity · cb064f451dd6e1584243e3dccb279caa MD5 · raw file

  1. /*
  2. * The contents of this file are subject to the terms of the Common Development
  3. * and Distribution License (the License). You may not use this file except in
  4. * compliance with the License.
  5. *
  6. * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
  7. * or http://www.netbeans.org/cddl.txt.
  8. *
  9. * When distributing Covered Code, include this CDDL Header Notice in each file
  10. * and include the License file at http://www.netbeans.org/cddl.txt.
  11. * If applicable, add the following below the CDDL Header, with the fields
  12. * enclosed by brackets [] replaced by your own identifying information:
  13. * "Portions Copyrighted [year] [name of copyright owner]"
  14. *
  15. * The Original Software is JSwat. The Initial Developer of the Original
  16. * Software is Nathan L. Fiedler. Portions created by Nathan L. Fiedler
  17. * are Copyright (C) 2006-2010. All Rights Reserved.
  18. *
  19. * Contributor(s): Nathan L. Fiedler.
  20. *
  21. * $Id: Breakpoints.java 288 2010-11-21 02:59:13Z nathanfiedler $
  22. */
  23. package com.bluemarsh.jswat.ui.breakpoint;
  24. import com.bluemarsh.jswat.core.breakpoint.Breakpoint;
  25. import com.bluemarsh.jswat.core.breakpoint.BreakpointGroup;
  26. import com.bluemarsh.jswat.core.util.NameValuePair;
  27. import java.util.Iterator;
  28. import java.util.Stack;
  29. import javax.swing.JComboBox;
  30. /**
  31. * A utility class containing methods used by one or more of the classes
  32. * contained in this package.
  33. *
  34. * @author Nathan Fiedler
  35. */
  36. public class Breakpoints {
  37. /**
  38. * Creates a new instance of Breakpoints.
  39. */
  40. private Breakpoints() {
  41. // None shall instantiate us.
  42. }
  43. /**
  44. * Builds a combo box to display all the available breakpoint groups.
  45. * Each group is represented by its name, prefixed with the names of
  46. * all of its parents, separated by periods.
  47. *
  48. * @param group the breakpoint group acting as root of all groups.
  49. * @param cbox combo box to be populated with items.
  50. */
  51. public static void buildGroupList(BreakpointGroup group, JComboBox cbox) {
  52. buildGroupList(group, cbox, null);
  53. }
  54. /**
  55. * Builds a combo box to display all the available breakpoint groups.
  56. * Each group is represented by its name, prefixed with the names of
  57. * all of its parents, separated by periods.
  58. *
  59. * @param group the breakpoint group acting as root of all groups.
  60. * @param cbox combo box to be populated with items.
  61. * @param exclude a breakpoint group to be excluded; this will exclude
  62. * the group and all of its child groups.
  63. */
  64. public static void buildGroupList(BreakpointGroup group, JComboBox cbox,
  65. BreakpointGroup exclude) {
  66. // Stack holds breakpoint group/composite name pairs.
  67. Stack<NameValuePair<BreakpointGroup>> stack =
  68. new Stack<NameValuePair<BreakpointGroup>>();
  69. stack.push(new NameValuePair<BreakpointGroup>(null, group));
  70. while (!stack.empty()) {
  71. // Get next composite name and breakpoint group.
  72. NameValuePair<BreakpointGroup> nvp = stack.pop();
  73. String name = nvp.getName();
  74. BreakpointGroup grp = nvp.getValue();
  75. if (name != null) {
  76. name = name + "." + grp.getName();
  77. } else {
  78. name = grp.getName();
  79. }
  80. // Add this group's name to the list.
  81. cbox.addItem(new NameValuePair<BreakpointGroup>(name, grp));
  82. // Visit this group's subgroups.
  83. Iterator<BreakpointGroup> iter = grp.groups(false);
  84. while (iter.hasNext()) {
  85. BreakpointGroup subgroup = iter.next();
  86. // Ignore the excluded group, and all of its children.
  87. if (exclude == null || !subgroup.equals(exclude)) {
  88. stack.push(new NameValuePair<BreakpointGroup>(name, subgroup));
  89. }
  90. }
  91. }
  92. }
  93. /**
  94. * Using the given combo box, previously built using
  95. * <code>buildGroupList()</code>, set one of the breakpoint groups
  96. * in the combo box as selected according to the parent group of
  97. * the given breakpoint.
  98. *
  99. * @param box combo box in which to select a group.
  100. * @param bp breakpoint whose parent group should be selected.
  101. */
  102. public static void findAndSelectGroup(JComboBox box, Breakpoint bp) {
  103. findAndSelectGroup(box, bp.getBreakpointGroup());
  104. }
  105. /**
  106. * Using the given combo box, previously built using
  107. * <code>buildGroupList()</code>, set one of the breakpoint groups
  108. * in the combo box as selected according to the given group.
  109. *
  110. * @param box combo box in which to select a group.
  111. * @param group group to be selected.
  112. */
  113. public static void findAndSelectGroup(JComboBox box, BreakpointGroup group) {
  114. int count = box.getItemCount();
  115. for (int ii = 0; ii < count; ii++) {
  116. NameValuePair<?> pair = (NameValuePair<?>) box.getItemAt(ii);
  117. if (pair.getValue() == group) {
  118. box.setSelectedIndex(ii);
  119. break;
  120. }
  121. }
  122. }
  123. /**
  124. * Retrieves the selected BreakpointGroup from the given combo box.
  125. * The combo box must have been built using the
  126. * <code>buildGroupList()</code> method.
  127. *
  128. * @param box combo box that lists breakpoint groups.
  129. * @return selected breakpoint group, or null if none.
  130. */
  131. public static BreakpointGroup getSelectedGroup(JComboBox box) {
  132. try {
  133. NameValuePair<?> grpname = (NameValuePair<?>) box.getSelectedItem();
  134. if (grpname != null) {
  135. return (BreakpointGroup) grpname.getValue();
  136. } else {
  137. return null;
  138. }
  139. } catch (ClassCastException cce) {
  140. return null;
  141. }
  142. }
  143. }