0N/A/*
2362N/A * Copyright (c) 2002, 2007, Oracle and/or its affiliates. All rights reserved.
0N/A *
0N/A * Redistribution and use in source and binary forms, with or without
0N/A * modification, are permitted provided that the following conditions
0N/A * are met:
0N/A *
0N/A * - Redistributions of source code must retain the above copyright
0N/A * notice, this list of conditions and the following disclaimer.
0N/A *
0N/A * - Redistributions in binary form must reproduce the above copyright
0N/A * notice, this list of conditions and the following disclaimer in the
0N/A * documentation and/or other materials provided with the distribution.
0N/A *
2362N/A * - Neither the name of Oracle nor the names of its
0N/A * contributors may be used to endorse or promote products derived
0N/A * from this software without specific prior written permission.
0N/A *
0N/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
0N/A * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
0N/A * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
0N/A * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
0N/A * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
0N/A * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
0N/A * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
0N/A * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
0N/A * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
0N/A * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
0N/A * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0N/A */
0N/A
4378N/A/*
4378N/A * This source code is provided to illustrate the usage of a given feature
4378N/A * or technique and has been deliberately simplified. Additional steps
4378N/A * required for a production-quality application, such as security checks,
4378N/A * input validation and proper error handling, might not be present in
4378N/A * this sample code.
4378N/A */
4378N/A
4378N/A
0N/Apackage j2dbench;
0N/A
0N/Aimport java.io.PrintWriter;
0N/Aimport javax.swing.BoxLayout;
0N/Aimport javax.swing.JComponent;
0N/Aimport javax.swing.JPanel;
0N/Aimport javax.swing.JTabbedPane;
0N/Aimport javax.swing.border.TitledBorder;
0N/Aimport java.util.NoSuchElementException;
0N/A
0N/Aimport j2dbench.ui.CompactLayout;
0N/Aimport j2dbench.ui.EnableButton;
0N/A
0N/Apublic class Group extends Node {
0N/A public static Group root = new Group();
0N/A
0N/A private Node children;
0N/A private boolean tabbed;
0N/A private boolean hidden;
0N/A private boolean horizontal;
0N/A private Boolean bordered;
0N/A private int tabPlacement;
0N/A
0N/A private Group() {
0N/A setTabbed(JTabbedPane.LEFT);
0N/A }
0N/A
0N/A public Group(String nodeName, String description) {
0N/A this(root, nodeName, description);
0N/A }
0N/A
0N/A public Group(Group parent, String nodeName, String description) {
0N/A super(parent, nodeName, description);
0N/A }
0N/A
0N/A public void addChild(Node child) {
0N/A Node prev = null;
0N/A for (Node node = children; node != null; node = node.getNext()) {
0N/A if (node.getNodeName().equalsIgnoreCase(child.getNodeName())) {
0N/A throw new RuntimeException("duplicate child added");
0N/A }
0N/A prev = node;
0N/A }
0N/A if (prev == null) {
0N/A children = child;
0N/A } else {
0N/A prev.setNext(child);
0N/A }
0N/A }
0N/A
0N/A public Node.Iterator getChildIterator() {
0N/A return new ChildIterator();
0N/A }
0N/A
0N/A public Node.Iterator getRecursiveChildIterator() {
0N/A return new RecursiveChildIterator();
0N/A }
0N/A
0N/A public Node getFirstChild() {
0N/A return children;
0N/A }
0N/A
0N/A public boolean isBordered() {
0N/A if (bordered == null) {
0N/A return (getParent() == null || !getParent().isTabbed());
0N/A }
0N/A return bordered.booleanValue();
0N/A }
0N/A
0N/A public boolean isTabbed() {
0N/A return tabbed;
0N/A }
0N/A
0N/A public boolean isHidden() {
0N/A return hidden;
0N/A }
0N/A
0N/A public boolean isHorizontal() {
0N/A return horizontal;
0N/A }
0N/A
0N/A public void setBordered(boolean b) {
0N/A bordered = b ? Boolean.TRUE : Boolean.FALSE;
0N/A }
0N/A
0N/A public void setTabbed() {
0N/A setTabbed(JTabbedPane.TOP);
0N/A }
0N/A
0N/A public void setTabbed(int tabPlacement) {
0N/A this.tabbed = true;
0N/A this.tabPlacement = tabPlacement;
0N/A }
0N/A
0N/A public void setHidden() {
0N/A hidden = true;
0N/A }
0N/A
0N/A public void setHorizontal() {
0N/A horizontal = true;
0N/A }
0N/A
0N/A public void traverse(Visitor v) {
0N/A super.traverse(v);
0N/A for (Node node = children; node != null; node = node.getNext()) {
0N/A node.traverse(v);
0N/A }
0N/A }
0N/A
0N/A public void restoreDefault() {
0N/A }
0N/A
0N/A public String setOption(String key, String value) {
0N/A int index = key.indexOf('.');
0N/A String subkey;
0N/A if (index < 0) {
0N/A subkey = "";
0N/A } else {
0N/A subkey = key.substring(index+1);
0N/A key = key.substring(0, index);
0N/A }
0N/A for (Node node = children; node != null; node = node.getNext()) {
0N/A if (node.getNodeName().equalsIgnoreCase(key)) {
0N/A return node.setOption(subkey, value);
0N/A }
0N/A }
0N/A return "Key failed to match an existing option";
0N/A }
0N/A
0N/A public void write(PrintWriter pw) {
0N/A }
0N/A
0N/A public JComponent getJComponent() {
0N/A if (isHidden()) {
0N/A return null;
0N/A } else if (isTabbed()) {
0N/A JTabbedPane jtp = new JTabbedPane(tabPlacement);
0N/A for (Node node = children; node != null; node = node.getNext()) {
0N/A JComponent comp = node.getJComponent();
0N/A if (comp != null) {
0N/A jtp.addTab(node.getDescription(), comp);
0N/A }
0N/A }
0N/A return jtp;
0N/A } else {
0N/A JPanel p = new JPanel();
0N/A p.setLayout(new BoxLayout(p,
0N/A horizontal
0N/A ? BoxLayout.X_AXIS
0N/A : BoxLayout.Y_AXIS));
0N/A p.setLayout(new CompactLayout(horizontal));
0N/A if (getDescription() != null && isBordered()) {
0N/A p.setBorder(new TitledBorder(getDescription()));
0N/A addEnableButtons(p);
0N/A }
0N/A for (Node node = children; node != null; node = node.getNext()) {
0N/A JComponent comp = node.getJComponent();
0N/A if (comp != null) {
0N/A p.add(comp);
0N/A }
0N/A }
0N/A return p;
0N/A }
0N/A }
0N/A
0N/A public void addEnableButtons(JPanel p) {
0N/A p.add(new EnableButton(this, EnableButton.DEFAULT));
0N/A p.add(new EnableButton(this, EnableButton.CLEAR));
0N/A p.add(new EnableButton(this, EnableButton.INVERT));
0N/A p.add(new EnableButton(this, EnableButton.SET));
0N/A }
0N/A
0N/A public static void restoreAllDefaults() {
0N/A root.traverse(new Visitor() {
0N/A public void visit(Node node) {
0N/A node.restoreDefault();
0N/A }
0N/A });
0N/A }
0N/A
0N/A public static void writeAll(final PrintWriter pw) {
0N/A root.traverse(new Visitor() {
0N/A public void visit(Node node) {
0N/A node.write(pw);
0N/A }
0N/A });
0N/A pw.flush();
0N/A }
0N/A
0N/A public static String setOption(String s) {
0N/A int index = s.indexOf('=');
0N/A if (index < 0) {
0N/A return "No value specified";
0N/A }
0N/A String key = s.substring(0, index);
0N/A String value = s.substring(index+1);
0N/A return root.setOption(key, value);
0N/A }
0N/A
0N/A public String toString() {
0N/A return "Group("+getTreeName()+")";
0N/A }
0N/A
0N/A public class ChildIterator implements Node.Iterator {
0N/A protected Node cur = getFirstChild();
0N/A
0N/A public boolean hasNext() {
0N/A return (cur != null);
0N/A }
0N/A
0N/A public Node next() {
0N/A Node ret = cur;
0N/A if (ret == null) {
0N/A throw new NoSuchElementException();
0N/A }
0N/A cur = cur.getNext();
0N/A return ret;
0N/A }
0N/A }
0N/A
0N/A public class RecursiveChildIterator extends ChildIterator {
0N/A Node.Iterator subiterator;
0N/A
0N/A public boolean hasNext() {
0N/A while (true) {
0N/A if (subiterator != null && subiterator.hasNext()) {
0N/A return true;
0N/A }
0N/A if (cur instanceof Group) {
0N/A subiterator = ((Group) cur).getRecursiveChildIterator();
0N/A cur = cur.getNext();
0N/A } else {
0N/A subiterator = null;
0N/A return super.hasNext();
0N/A }
0N/A }
0N/A }
0N/A
0N/A public Node next() {
0N/A if (subiterator != null) {
0N/A return subiterator.next();
0N/A } else {
0N/A return super.next();
0N/A }
0N/A }
0N/A }
0N/A
0N/A public static class EnableSet extends Group implements Modifier {
0N/A public EnableSet() {
0N/A super();
0N/A }
0N/A
0N/A public EnableSet(Group parent, String nodeName, String description) {
0N/A super(parent, nodeName, description);
0N/A }
0N/A
0N/A public Modifier.Iterator getIterator(TestEnvironment env) {
0N/A return new EnableIterator();
0N/A }
0N/A
0N/A public void modifyTest(TestEnvironment env, Object val) {
0N/A ((Option.Enable) val).modifyTest(env);
0N/A env.setModifier(this, val);
0N/A }
0N/A
0N/A public void restoreTest(TestEnvironment env, Object val) {
0N/A ((Option.Enable) val).restoreTest(env);
0N/A env.removeModifier(this);
0N/A }
0N/A
0N/A public String getAbbreviatedModifierDescription(Object val) {
0N/A Option.Enable oe = (Option.Enable) val;
0N/A return oe.getAbbreviatedModifierDescription(Boolean.TRUE);
0N/A }
0N/A
0N/A public String getModifierValueName(Object val) {
0N/A Option.Enable oe = (Option.Enable) val;
0N/A return oe.getModifierValueName(Boolean.TRUE);
0N/A }
0N/A
0N/A public class EnableIterator implements Modifier.Iterator {
0N/A Node.Iterator childiterator = getRecursiveChildIterator();
0N/A Option.Enable curval;
0N/A
0N/A public boolean hasNext() {
0N/A if (curval != null) {
0N/A return true;
0N/A }
0N/A while (childiterator.hasNext()) {
0N/A Node node = childiterator.next();
0N/A if (node instanceof Option.Enable) {
0N/A curval = (Option.Enable) node;
0N/A if (curval.isEnabled()) {
0N/A return true;
0N/A }
0N/A curval = null;
0N/A }
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A public Object next() {
0N/A if (curval == null) {
0N/A if (!hasNext()) {
0N/A throw new NoSuchElementException();
0N/A }
0N/A }
0N/A Object ret = curval;
0N/A curval = null;
0N/A return ret;
0N/A }
0N/A }
0N/A }
0N/A}