PageRenderTime 22ms CodeModel.GetById 16ms app.highlight 4ms RepoModel.GetById 0ms app.codeStats 0ms

/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
Possible License(s): MPL-2.0-no-copyleft-exception
  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 */
 23package com.bluemarsh.jswat.ui.breakpoint;
 24
 25import com.bluemarsh.jswat.core.breakpoint.Breakpoint;
 26import com.bluemarsh.jswat.core.breakpoint.BreakpointGroup;
 27import com.bluemarsh.jswat.core.util.NameValuePair;
 28import java.util.Iterator;
 29import java.util.Stack;
 30import javax.swing.JComboBox;
 31
 32/**
 33 * A utility class containing methods used by one or more of the classes
 34 * contained in this package.
 35 *
 36 * @author Nathan Fiedler
 37 */
 38public class Breakpoints {
 39
 40    /**
 41     * Creates a new instance of Breakpoints.
 42     */
 43    private Breakpoints() {
 44        // None shall instantiate us.
 45    }
 46
 47    /**
 48     * Builds a combo box to display all the available breakpoint groups.
 49     * Each group is represented by its name, prefixed with the names of
 50     * all of its parents, separated by periods.
 51     *
 52     * @param  group  the breakpoint group acting as root of all groups.
 53     * @param  cbox   combo box to be populated with items.
 54     */
 55    public static void buildGroupList(BreakpointGroup group, JComboBox cbox) {
 56        buildGroupList(group, cbox, null);
 57    }
 58
 59    /**
 60     * Builds a combo box to display all the available breakpoint groups.
 61     * Each group is represented by its name, prefixed with the names of
 62     * all of its parents, separated by periods.
 63     *
 64     * @param  group    the breakpoint group acting as root of all groups.
 65     * @param  cbox     combo box to be populated with items.
 66     * @param  exclude  a breakpoint group to be excluded; this will exclude
 67     *                  the group and all of its child groups.
 68     */
 69    public static void buildGroupList(BreakpointGroup group, JComboBox cbox,
 70            BreakpointGroup exclude) {
 71        // Stack holds breakpoint group/composite name pairs.
 72        Stack<NameValuePair<BreakpointGroup>> stack =
 73                new Stack<NameValuePair<BreakpointGroup>>();
 74        stack.push(new NameValuePair<BreakpointGroup>(null, group));
 75        while (!stack.empty()) {
 76            // Get next composite name and breakpoint group.
 77            NameValuePair<BreakpointGroup> nvp = stack.pop();
 78            String name = nvp.getName();
 79            BreakpointGroup grp = nvp.getValue();
 80            if (name != null) {
 81                name = name + "." + grp.getName();
 82            } else {
 83                name = grp.getName();
 84            }
 85            // Add this group's name to the list.
 86            cbox.addItem(new NameValuePair<BreakpointGroup>(name, grp));
 87
 88            // Visit this group's subgroups.
 89            Iterator<BreakpointGroup> iter = grp.groups(false);
 90            while (iter.hasNext()) {
 91                BreakpointGroup subgroup = iter.next();
 92                // Ignore the excluded group, and all of its children.
 93                if (exclude == null || !subgroup.equals(exclude)) {
 94                    stack.push(new NameValuePair<BreakpointGroup>(name, subgroup));
 95                }
 96            }
 97        }
 98    }
 99
100    /**
101     * Using the given combo box, previously built using
102     * <code>buildGroupList()</code>, set one of the breakpoint groups
103     * in the combo box as selected according to the parent group of
104     * the given breakpoint.
105     *
106     * @param  box  combo box in which to select a group.
107     * @param  bp   breakpoint whose parent group should be selected.
108     */
109    public static void findAndSelectGroup(JComboBox box, Breakpoint bp) {
110        findAndSelectGroup(box, bp.getBreakpointGroup());
111    }
112
113    /**
114     * Using the given combo box, previously built using
115     * <code>buildGroupList()</code>, set one of the breakpoint groups
116     * in the combo box as selected according to the given group.
117     *
118     * @param  box    combo box in which to select a group.
119     * @param  group  group to be selected.
120     */
121    public static void findAndSelectGroup(JComboBox box, BreakpointGroup group) {
122        int count = box.getItemCount();
123        for (int ii = 0; ii < count; ii++) {
124            NameValuePair<?> pair = (NameValuePair<?>) box.getItemAt(ii);
125            if (pair.getValue() == group) {
126                box.setSelectedIndex(ii);
127                break;
128            }
129        }
130    }
131
132    /**
133     * Retrieves the selected BreakpointGroup from the given combo box.
134     * The combo box must have been built using the
135     * <code>buildGroupList()</code> method.
136     *
137     * @param  box  combo box that lists breakpoint groups.
138     * @return  selected breakpoint group, or null if none.
139     */
140    public static BreakpointGroup getSelectedGroup(JComboBox box) {
141        try {
142            NameValuePair<?> grpname = (NameValuePair<?>) box.getSelectedItem();
143            if (grpname != null) {
144                return (BreakpointGroup) grpname.getValue();
145            } else {
146                return null;
147            }
148        } catch (ClassCastException cce) {
149            return null;
150        }
151    }
152}