001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Sergey Burlak
019: * @version $Revision$
020: */package javax.swing.plaf.metal;
021:
022: import java.awt.Component;
023: import java.awt.Graphics;
024: import javax.swing.Icon;
025: import javax.swing.JTree;
026: import javax.swing.SwingTestCase;
027: import javax.swing.UIManager;
028: import javax.swing.UnsupportedLookAndFeelException;
029: import javax.swing.tree.DefaultMutableTreeNode;
030:
031: public class MetalTreeUITest extends SwingTestCase {
032: private MetalTreeUI ui;
033:
034: private JTree tree;
035:
036: private DefaultMutableTreeNode root;
037:
038: private DefaultMutableTreeNode node1;
039:
040: private DefaultMutableTreeNode node2;
041:
042: private DefaultMutableTreeNode node3;
043:
044: private DefaultMutableTreeNode node11;
045:
046: private DefaultMutableTreeNode node21;
047:
048: private DefaultMutableTreeNode node22;
049:
050: @Override
051: protected void setUp() throws Exception {
052: super .setUp();
053: try {
054: UIManager.setLookAndFeel(new MetalLookAndFeel());
055: } catch (UnsupportedLookAndFeelException e) {
056: e.printStackTrace();
057: }
058: root = new DefaultMutableTreeNode("root");
059: node1 = new DefaultMutableTreeNode("node1");
060: node2 = new DefaultMutableTreeNode("node2");
061: node3 = new DefaultMutableTreeNode("node3");
062: node11 = new DefaultMutableTreeNode("node11");
063: node21 = new DefaultMutableTreeNode("node21");
064: node22 = new DefaultMutableTreeNode("node22");
065: root.add(node1);
066: node1.add(node11);
067: node11.add(new DefaultMutableTreeNode("node111"));
068: root.add(node1);
069: node2.add(node21);
070: node2.add(node22);
071: root.add(node2);
072: root.add(node3);
073: tree = new JTree(root);
074: ui = new MetalTreeUI();
075: tree.setUI(ui);
076: }
077:
078: @Override
079: protected void tearDown() throws Exception {
080: ui = null;
081: tree = null;
082: root = null;
083: node1 = null;
084: node2 = null;
085: node3 = null;
086: node11 = null;
087: node21 = null;
088: node22 = null;
089: super .tearDown();
090: }
091:
092: public void testPaint() {
093: Graphics g = createTestGraphics();
094: g.setClip(0, 0, 10, 10);
095: ui.paint(g, tree);
096: }
097:
098: public void testUninstallUI() {
099: ui.uninstallUI(tree);
100: assertNotNull(ui.getExpandedIcon());
101: assertNotNull(ui.getCollapsedIcon());
102: }
103:
104: public void testInstallUI() {
105: ui.uninstallUI(tree);
106: ui.installUI(tree);
107: assertNotNull(ui.getExpandedIcon());
108: assertNotNull(ui.getCollapsedIcon());
109: }
110:
111: public void testCreateUI() {
112: assertNotSame(MetalTreeUI.createUI(tree), MetalTreeUI
113: .createUI(tree));
114: }
115:
116: public void testGetHorizontalLegBuffer() {
117: assertEquals(4, ui.getHorizontalLegBuffer());
118: }
119:
120: public void testIsLocationInExpandControl() throws Exception {
121: tree.setShowsRootHandles(false);
122: tree.expandPath(tree.getPathForRow(1));
123: assertFalse(ui.isLocationInExpandControl(0, 0, -26, 25));
124: assertTrue(ui.isLocationInExpandControl(0, 0, -25, 25));
125: assertTrue(ui.isLocationInExpandControl(0, 0, -1, 25));
126: assertFalse(ui.isLocationInExpandControl(0, 0, 0, 25));
127: assertFalse(ui.isLocationInExpandControl(1, 0, -26, 25));
128: assertTrue(ui.isLocationInExpandControl(1, 0, -25, 25));
129: assertTrue(ui.isLocationInExpandControl(1, 0, -1, 25));
130: assertFalse(ui.isLocationInExpandControl(1, 0, 0, 25));
131: assertFalse(ui.isLocationInExpandControl(2, 0, -26, 25));
132: assertTrue(ui.isLocationInExpandControl(2, 0, -25, 25));
133: assertTrue(ui.isLocationInExpandControl(2, 0, -1, 25));
134: assertFalse(ui.isLocationInExpandControl(2, 0, 0, 25));
135: assertFalse(ui.isLocationInExpandControl(3, 0, -26, 25));
136: assertTrue(ui.isLocationInExpandControl(3, 0, -25, 25));
137: assertTrue(ui.isLocationInExpandControl(3, 0, -1, 25));
138: assertFalse(ui.isLocationInExpandControl(3, 0, 0, 25));
139: assertFalse(ui.isLocationInExpandControl(0, 1, -6, 25));
140: assertTrue(ui.isLocationInExpandControl(0, 1, -5, 25));
141: assertTrue(ui.isLocationInExpandControl(0, 1, 19, 25));
142: assertFalse(ui.isLocationInExpandControl(0, 1, 20, 25));
143: assertFalse(ui.isLocationInExpandControl(1, 1, -6, 20));
144: assertTrue(ui.isLocationInExpandControl(1, 1, -5, 20));
145: assertTrue(ui.isLocationInExpandControl(1, 1, 19, 20));
146: assertFalse(ui.isLocationInExpandControl(1, 1, 20, 20));
147: assertFalse(ui.isLocationInExpandControl(2, 1, -6, 20));
148: assertTrue(ui.isLocationInExpandControl(2, 1, -5, 20));
149: assertTrue(ui.isLocationInExpandControl(2, 1, 19, 20));
150: assertFalse(ui.isLocationInExpandControl(2, 1, 20, 20));
151: assertFalse(ui.isLocationInExpandControl(0, 2, 14, -20));
152: assertTrue(ui.isLocationInExpandControl(0, 2, 15, -20));
153: assertTrue(ui.isLocationInExpandControl(0, 2, 39, -20));
154: assertFalse(ui.isLocationInExpandControl(0, 2, 40, -20));
155: assertFalse(ui.isLocationInExpandControl(1, 2, 14, -20));
156: assertTrue(ui.isLocationInExpandControl(1, 2, 15, -20));
157: assertTrue(ui.isLocationInExpandControl(1, 2, 39, -20));
158: assertFalse(ui.isLocationInExpandControl(1, 2, 40, -20));
159: assertFalse(ui.isLocationInExpandControl(2, 2, 14, -20));
160: assertTrue(ui.isLocationInExpandControl(2, 2, 15, -20));
161: assertTrue(ui.isLocationInExpandControl(2, 2, 39, -20));
162: assertFalse(ui.isLocationInExpandControl(2, 2, 40, -20));
163: tree.setShowsRootHandles(true);
164: assertFalse(ui.isLocationInExpandControl(0, 0, -6, 25));
165: assertTrue(ui.isLocationInExpandControl(0, 0, -5, 25));
166: assertTrue(ui.isLocationInExpandControl(0, 0, 19, 25));
167: assertFalse(ui.isLocationInExpandControl(0, 0, 20, 25));
168: tree.setShowsRootHandles(false);
169: ui.setExpandedIcon(new Icon() {
170: public void paintIcon(Component c, Graphics g, int x, int y) {
171: }
172:
173: public int getIconWidth() {
174: return 0;
175: }
176:
177: public int getIconHeight() {
178: return 100;
179: }
180: });
181: assertFalse(ui.isLocationInExpandControl(0, 0, -17, 25));
182: assertTrue(ui.isLocationInExpandControl(0, 0, -16, 25));
183: assertTrue(ui.isLocationInExpandControl(0, 0, -10, 25));
184: assertFalse(ui.isLocationInExpandControl(0, 0, -9, 25));
185: }
186: }
|