001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.swing.tabcontrol;
043:
044: import java.awt.Color;
045: import java.awt.Component;
046: import java.awt.FlowLayout;
047: import java.awt.Graphics;
048: import java.awt.Point;
049: import java.awt.event.MouseAdapter;
050: import java.awt.event.MouseEvent;
051: import java.util.Arrays;
052: import javax.swing.Icon;
053: import javax.swing.JButton;
054: import javax.swing.JComponent;
055: import javax.swing.JFrame;
056: import javax.swing.SwingUtilities;
057: import javax.swing.UIManager;
058: import junit.framework.TestCase;
059: import org.netbeans.swing.popupswitcher.SwitcherTableItem;
060:
061: /**
062: * Convenient IDE tester. Just run and push the button. Move the whole frame to
063: * visually check the row per columns computation.
064: *
065: * @author mkrauskopf
066: */
067: public class ButtonPopupSwitcherTestHid extends TestCase {
068:
069: private JFrame frame;
070:
071: private SwitcherTableItem[] items = new SwitcherTableItem[100];
072:
073: public ButtonPopupSwitcherTestHid(String testName) {
074: super (testName);
075: try {
076: UIManager.setLookAndFeel(UIManager
077: .getSystemLookAndFeelClassName());
078: } catch (Exception ex) {
079: System.err.println("Cannot set L&F: " + ex);
080: }
081: }
082:
083: protected void setUp() {
084: frame = createFrame();
085: frame.setVisible(true);
086: items[0] = new SwitcherTableItem(new DummyActivatable(
087: "Something.txt"), "Something.txt", new DummyIcon(
088: Color.BLUE));
089: items[1] = new SwitcherTableItem(new DummyActivatable(
090: "Sometime.txt"), "Sometime.txt", new DummyIcon());
091: SwitcherTableItem selectedItem = new SwitcherTableItem(
092: new DummyActivatable("Somewhere.txt"), "Somewhere.txt",
093: "Somewhere.txt", new DummyIcon(Color.YELLOW), true);
094: items[2] = selectedItem;
095: items[3] = new SwitcherTableItem(new DummyActivatable(
096: "AbCd.txt"), "AbCd.txt", new DummyIcon(Color.BLUE));
097: items[4] = new SwitcherTableItem(
098: new DummyActivatable("Sometime.txt"),
099: "Very Very Very Long"
100: + " name with a lot of words in its name bla bla bla bla bla bla"
101: + " which sould be shortened and should ends with three dots [...]."
102: + " Hmmmmm", new DummyIcon());
103: items[5] = new SwitcherTableItem(new DummyActivatable(
104: "Somewhere.txt"), "Somewhere.txt", new DummyIcon(
105: Color.YELLOW));
106: Arrays.fill(items, 6, 70, new SwitcherTableItem(
107: new DummyActivatable("s2.txt"), "s2.txt",
108: new DummyIcon()));
109: items[70] = new SwitcherTableItem(new DummyActivatable(
110: "Somewhere.txt"), "null icon", null);
111: Arrays.fill(items, 71, 90, new SwitcherTableItem(
112: new DummyActivatable("s5.txt"), "s5.txt",
113: new DummyIcon()));
114: items[90] = new SwitcherTableItem(new DummyActivatable(
115: "Somewhere.txt"), null, new DummyIcon(Color.BLACK));
116: Arrays.fill(items, 91, 100, new SwitcherTableItem(
117: new DummyActivatable("q1.txt"), "q1.txt",
118: new DummyIcon(Color.GREEN)));
119: Arrays.sort(items);
120: // wait until a developer close the frame
121: sleepForever();
122: }
123:
124: public void testFake() {
125: // needed to "run" this class
126: }
127:
128: private JFrame createFrame() {
129: JFrame frame = new JFrame(getClass().getName());
130: frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
131: frame.getContentPane().setLayout(new FlowLayout());
132: JButton pBut = new JButton("Popup");
133: pBut.addMouseListener(new MouseAdapter() {
134: public void mousePressed(MouseEvent e) {
135: pButAction(e);
136: }
137: });
138: frame.getContentPane().add(pBut);
139: frame.pack();
140: frame.setLocationRelativeTo(null);
141: return frame;
142: }
143:
144: private void pButAction(MouseEvent e) {
145: // create popup with our SwitcherTable
146: JComponent c = (JComponent) e.getSource();
147: Point p = new Point(c.getWidth(), c.getHeight());
148: SwingUtilities.convertPointToScreen(p, c);
149: if (!ButtonPopupSwitcher.isShown()) {
150: ButtonPopupSwitcher.selectItem(c, items, p.x, p.y);
151: }
152: }
153:
154: private static class DummyIcon implements Icon {
155: Color color;
156:
157: private DummyIcon(Color color) {
158: this .color = color;
159: }
160:
161: private DummyIcon() {
162: this .color = Color.RED;
163: }
164:
165: public void paintIcon(Component c, Graphics g, int x, int y) {
166: int left = ((JComponent) c).getInsets().left;
167: int top = ((JComponent) c).getInsets().top;
168: g.setColor(color);
169: g.fillRect(left + 2, top + 2, 12, 12);
170: g.setColor(Color.BLACK);
171: g.fillRect(left + 4, top + 4, 8, 8);
172: }
173:
174: public int getIconWidth() {
175: return 16;
176: }
177:
178: public int getIconHeight() {
179: return 16;
180: }
181: }
182:
183: /**
184: * Activatable tester class.
185: */
186: private static class DummyActivatable implements
187: SwitcherTableItem.Activatable {
188: String dummyName;
189:
190: private DummyActivatable(String name) {
191: this .dummyName = name;
192: }
193:
194: public void activate() {
195: System.out.println("MK> Activating \"" + dummyName
196: + "\"....");
197: }
198: }
199:
200: private void sleep() {
201: sleep(12000);
202: }
203:
204: private void sleep(long millis) {
205: try {
206: Thread.sleep(millis);
207: } catch (InterruptedException e) {
208: e.printStackTrace();
209: }
210: }
211:
212: private void sleepForever() {
213: boolean dumb = true;
214: while (dumb) {
215: sleep(60000);
216: }
217: }
218: }
|