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: * TestFrame.java
043: *
044: * Created on May 28, 2003, 2:50 AM
045: */
046:
047: package org.netbeans.swing.tabcontrol.demo;
048:
049: import org.netbeans.swing.tabcontrol.TabData;
050: import org.netbeans.swing.tabcontrol.TabDataModel;
051: import org.netbeans.swing.tabcontrol.DefaultTabDataModel;
052: import org.netbeans.swing.tabcontrol.TabbedContainer;
053:
054: import javax.swing.*;
055: import java.awt.*;
056: import java.awt.event.ActionEvent;
057: import java.awt.event.ActionListener;
058: import java.lang.reflect.Method;
059: import java.util.StringTokenizer;
060:
061: /**
062: * A frame to test the tab component
063: *
064: * @author Tim Boudreau
065: */
066: public class TestFrame extends javax.swing.JFrame {
067:
068: /**
069: * Creates new form TestFrame
070: */
071: public TestFrame() {
072: setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
073: initComponents();
074: /*
075: try {
076:
077: System.setProperty("os.version", new Float(3.5).toString());
078: Method m = Toolkit.class.getDeclaredMethod("setDesktopProperty",
079: new Class[] {String.class, Object.class});
080: m.setAccessible(true);
081: m.invoke(java.awt.Toolkit.getDefaultToolkit(),
082: new Object[] {"win.xpstyle.themeActive", Boolean.FALSE });
083:
084:
085: UIManager.setLookAndFeel(new PseudoWin());
086: // UIManager.setLookAndFeel(new javax.swing.plaf.metal.MetalLookAndFeel());
087: } catch (Exception e) {
088: e.printStackTrace();
089: }
090: */
091:
092: try {
093: // UIManager.setLookAndFeel(new javax.swing.plaf.metal.MetalLookAndFeel());
094: } catch (Exception e) {
095: }
096:
097: // UIManager.put ("EditorTabDisplayerUI", "org.netbeans.swing.tabcontrol.plaf.WinClassicEditorTabDisplayerUI");
098: // UIManager.put ("ViewTabDisplayerUI", "org.netbeans.swing.tabcontrol.plaf.WinClassicViewTabDisplayerUI");
099: // UIManager.put ("EditorTabDisplayerUI", "org.netbeans.swing.tabcontrol.plaf.WinXPEditorTabDisplayerUI");
100: // UIManager.put ("ViewTabDisplayerUI", "org.netbeans.swing.tabcontrol.plaf.WinXPViewTabDisplayerUI");
101:
102: JLabel jb1 = new JLabel("Label 1");
103: final JButton jb2 = new JButton("Button 2 - Update UI");
104: JButton jb3 = new JButton("Click me to remove this tab");
105: JLabel jb4 = new JLabel("Label 4");
106: JLabel jb5 = new JLabel("Label 5");
107: JButton jb6 = new JButton(
108: "Click me to programmatically set the selected index to 3 atomically change some tab text and icons");
109: JTree jtr = new JTree();
110:
111: jb1.setBackground(Color.ORANGE);
112: jb2.setBackground(Color.YELLOW);
113: jb3.setBackground(Color.CYAN);
114: jb4.setBackground(Color.GREEN);
115: jb2.setOpaque(true);
116: jb3.setOpaque(true);
117: jb4.setOpaque(true);
118: jb1.setOpaque(true);
119:
120: JTextArea JTA = new JTextArea(TEXT);
121: JTA.setWrapStyleWord(true);
122: JTA.setColumns(80);
123: JTA.setLineWrap(true);
124:
125: JButton jb7 = new JButton("Remove non-contiguous tabs");
126: JButton jb8 = new JButton("Discontig add");
127:
128: JButton jb9 = new JButton("Contig add");
129: JButton jb10 = new JButton("Contig remove");
130:
131: JButton jb11 = new JButton("Discontig add and remove");
132:
133: TabData tab0 = new TabData(jtr, myIcon, "0 JTree", "0");
134: TabData tab1 = new TabData(jb1, myIcon, "1 Tab 1", "1");
135: TabData tab2 = new TabData(jb2, myIcon2, "2 Different icon",
136: "2");
137: TabData tab3 = new TabData(jb3, myIcon, "3 Tab 3", "3");
138: TabData tab4 = new TabData(
139: jb4,
140: myIcon,
141: "<html>A <b>tab</b> with <font color='#3333DD'><i><u>html</u></i></font> <s>text</s> and stuff</html>",
142: null);
143: // TabData tab4 = new TabData(jb4, myIcon, "4 A bunch of stuff", "4");
144: TabData tab5 = new TabData(jb5, myIcon, "5 Tab 5", "5");
145: TabData tab6 = new TabData(jb6, myIcon2, "6 Click me!", "6");
146:
147: // TabData tab7 = new TabData(new JButton("foo"), myIcon, "A tab which has, shall we say, an extremely long name, one so long that it might cause some problems should the layout model not do all of its mathematics perfectly, I daresay", null);
148: TabData tab7 = new TabData(JTA, null, "7 some text", "7");
149: TabData tab8 = new TabData(new JLabel("gioo"), myIcon,
150: "8 something", "8");
151: TabData tab9 = new TabData(jb7, myIcon, "9 Discontig remove",
152: "9");
153: TabData tab10 = new TabData(new JLabel("gioo"), myIcon,
154: "10 wiggle", "10");
155: TabData tab11 = new TabData(jb8, myIcon, "11 Discontig add",
156: "11");
157: TabData tab12 = new TabData(jb9, myIcon, "12 contig add", "12");
158: TabData tab13 = new TabData(jb10, myIcon, "13 contig remove",
159: "13");
160: TabData tab14 = new TabData(jb11, myIcon, "14 MUNGE", "14");
161:
162: TabDataModel mdl = new DefaultTabDataModel(new TabData[] {
163: tab0, tab1, tab2, tab3, tab4, tab5, tab6, tab7, tab8,
164: tab9, tab10, tab11, tab12, tab13, tab14 });
165:
166: /*
167: TabDataModel mdl = new DefaultTabDataModel(
168: new TabData[] { tab0, tab1, tab2, tab3, tab4, tab5}
169: );
170: */
171:
172: final TabbedContainer tab = new TabbedContainer(mdl,
173: TabbedContainer.TYPE_VIEW);
174: tab.setActive(true);
175: tab.requestAttention(5);
176: tab.requestAttention(3);
177:
178: jb6.addActionListener(new ActionListener() {
179: public void actionPerformed(ActionEvent ae) {
180: tab.getSelectionModel().setSelectedIndex(3);
181: int[] indices = new int[] { 0, 3, 5 };
182: String[] newText = new String[] {
183: "I am still a JTree",
184: "<html>Changed <b><font color='#FFFF00'>3</font></b></html>",
185: "<html><s>changed</s></html> 5" };
186: Icon[] icons = new Icon[] { myIcon3, myIcon3, myIcon3 };
187: tab.getModel().setIconsAndText(indices, newText, icons);
188: }
189: });
190:
191: jb3.addActionListener(new ActionListener() {
192: public void actionPerformed(ActionEvent ae) {
193: tab.getModel().removeTab(3);
194: }
195: });
196:
197: jb2.addActionListener(new ActionListener() {
198: public void actionPerformed(ActionEvent ae) {
199: UIManager
200: .put("EditorTabDisplayerUI",
201: "org.netbeans.swing.tabcontrol.plaf.WinClassicEditorTabDisplayerUI");
202: UIManager
203: .put("ViewTabDisplayerUI",
204: "org.netbeans.swing.tabcontrol.plaf.WinClassicViewTabDisplayerUI");
205: tab.updateUI();
206: }
207: });
208:
209: jb7.addActionListener(new ActionListener() {
210: public void actionPerformed(ActionEvent ae) {
211: tab.getModel().removeTabs(new int[] { 1, 3, 7, 8 });
212: }
213: });
214:
215: jb8.addActionListener(new ActionListener() {
216: public void actionPerformed(ActionEvent ae) {
217: int[] idxs = new int[] { 1, 3, 6, 8 };
218: TabData[] td = new TabData[] {
219: new TabData(new JButton("inserted 1"), myIcon,
220: "I-1", "tip"),
221: new TabData(new JButton("inserted 3"), myIcon,
222: "I-3", "tip"),
223: new TabData(new JButton("inserted 6"), myIcon,
224: "I-6", "tip"),
225: new TabData(new JButton("inserted 8"), myIcon,
226: "I-8", "tip"),
227:
228: };
229: tab.getModel().addTabs(idxs, td);
230: }
231: });
232:
233: jb9.addActionListener(new ActionListener() {
234: public void actionPerformed(ActionEvent ae) {
235: int[] idxs = new int[] { 1, 3, 6, 8 };
236: TabData[] td = new TabData[] {
237: new TabData(new JButton("inserted c 1"),
238: myIcon, "Ic-1", "tip"),
239: new TabData(new JButton("inserted c 2"),
240: myIcon, "Ic-2", "tip"),
241: new TabData(new JButton("inserted c 3"),
242: myIcon, "Ic-3", "tip"),
243: new TabData(new JButton("inserted c 4"),
244: myIcon, "Ic-4", "tip"),
245:
246: };
247: tab.getModel().addTabs(1, td);
248: }
249: });
250:
251: jb10.addActionListener(new ActionListener() {
252: public void actionPerformed(ActionEvent ae) {
253: tab.getModel().removeTabs(1, 4);
254: }
255: });
256:
257: jb11.addActionListener(new ActionListener() {
258: public void actionPerformed(ActionEvent ae) {
259: TabData[] data = (TabData[]) tab.getModel().getTabs()
260: .toArray(new TabData[0]);
261:
262: TabData[] newData = new TabData[data.length - 3];
263: int ct = 0;
264:
265: //strip out some tabs
266: for (int i = 0; i < data.length; i++) {
267: newData[ct] = data[i];
268: if (i != 2 && i != 3 && i != 7) {
269: ct++;
270: }
271: }
272:
273: //replace one tab
274: newData[1] = new TabData(new JLabel("Hi there"),
275: myIcon, "New tab", "foo");
276: //swap some tabs
277: TabData td = newData[8];
278: newData[8] = newData[7];
279: newData[7] = td;
280:
281: tab.getModel().setTabs(newData);
282: }
283: });
284:
285: tab.setActive(true);
286:
287: // jb2.addMouseListener (new MouseAdapter() {
288: // public void mouseClicked(MouseEvent e) {
289: // Point point = e.getPoint();
290: // System.out.println("Constraint for location is: " + tab.getConstraintForLocation(point, false));
291: // System.out.println("Tab index is: " + tab.tabForCoordinate(point.x, point.y));
292: // System.out.println(tab.getIndicationForLocation(point, false, new Point(0, 0), false));
293: // }
294: // }); // PENDING
295: // tab.setActive(true);
296: getContentPane().add(tab);
297: setSize(700, 300);
298: setLocation(12, 12);
299: }
300:
301: public class PseudoWin extends
302: com.sun.java.swing.plaf.windows.WindowsLookAndFeel {
303:
304: public boolean isSupportedLookAndFeel() {
305: return true;
306: }
307: }
308:
309: TabDataModel mdl = null;
310: Icon myIcon = new Icon() {
311: public int getIconWidth() {
312: return 16;
313: }
314:
315: public int getIconHeight() {
316: return 14;
317: }
318:
319: public void paintIcon(Component c, Graphics g, int x, int y) {
320: g.setColor(Color.BLUE);
321: g.fillRect(x, y, getIconWidth() - 4, getIconHeight());
322:
323: g.setColor(Color.YELLOW);
324: g.drawLine(x + 2, y + 2, x + 3, y + 3);
325: g.drawLine(x + 2, y + 3, x + 3, y + 4);
326:
327: g.drawLine(x + 8, y + 2, x + 7, y + 3);
328: g.drawLine(x + 8, y + 3, x + 7, y + 4);
329:
330: g.drawLine(x + 4, y + 6, x + 6, y + 6);
331:
332: g.drawRoundRect(x + 3, y + 9, 5, 3, 8, 5);
333: }
334: };
335:
336: Icon myIcon2 = new Icon() {
337: public int getIconWidth() {
338: return 19;
339: }
340:
341: public int getIconHeight() {
342: return 14;
343: }
344:
345: public void paintIcon(Component c, Graphics g, int x, int y) {
346: g.setColor(Color.ORANGE);
347: g.fillRect(x, y, getIconWidth() - 4, getIconHeight());
348: g.setColor(Color.RED);
349: g.drawRect(x, y, getIconWidth() - 4, getIconHeight());
350:
351: g.setColor(Color.BLUE);
352: g.drawLine(x + 2, y + 2, x + 3, y + 3);
353: g.drawLine(x + 2, y + 3, x + 3, y + 4);
354:
355: g.drawLine(x + 7, y + 2, x + 6, y + 3);
356: g.drawLine(x + 7, y + 3, x + 6, y + 4);
357:
358: g.drawLine(x + 5, y + 6, x + 6, y + 6);
359:
360: g.drawRoundRect(x + 4, y + 9, 3, 3, 8, 5);
361: }
362: };
363:
364: Icon myIcon3 = new Icon() {
365: public int getIconWidth() {
366: return 19;
367: }
368:
369: public int getIconHeight() {
370: return 14;
371: }
372:
373: public void paintIcon(Component c, Graphics g, int x, int y) {
374: g.setColor(Color.CYAN);
375: g.fillRect(x, y, getIconWidth() - 4, getIconHeight());
376: g.setColor(Color.WHITE);
377: g.drawRect(x, y, getIconWidth() - 4, getIconHeight());
378:
379: g.setColor(Color.BLACK);
380: g.drawLine(x + 2, y + 2, x + 3, y + 3);
381: g.drawLine(x + 2, y + 3, x + 3, y + 4);
382:
383: g.drawLine(x + 7, y + 2, x + 6, y + 3);
384: g.drawLine(x + 7, y + 3, x + 6, y + 4);
385:
386: g.drawLine(x + 5, y + 6, x + 6, y + 6);
387:
388: g.drawRoundRect(x + 4, y + 9, 3, 3, 8, 5);
389: }
390: };
391:
392: /**
393: * This method is called from within the constructor to initialize the form.
394: * WARNING: Do NOT modify this code. The content of this method is always
395: * regenerated by the Form Editor.
396: */
397: private void initComponents() {//GEN-BEGIN:initComponents
398:
399: addWindowListener(new java.awt.event.WindowAdapter() {
400: public void windowClosing(java.awt.event.WindowEvent evt) {
401: exitForm(evt);
402: }
403: });
404:
405: pack();
406: }//GEN-END:initComponents
407:
408: /**
409: * Exit the Application
410: */
411: private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
412: System.exit(0);
413: }//GEN-LAST:event_exitForm
414:
415: /**
416: * @param args the command line arguments
417: */
418: public static void main(String args[]) throws Exception {
419: parseArgs(args);
420: // UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
421: // System.setProperty("apple.awt.brushMetalLook", "true");
422:
423: // RepaintManager nue = new MyRepaintManager(RepaintManager.currentManager(new javax.swing.JFrame()));
424: // RepaintManager.setCurrentManager(nue);
425: // System.setProperty ("nb.forceUI", "WindowsXPLFCustoms");
426: try {
427: // UIManager.setLookAndFeel(new javax.swing.plaf.metal.MetalLookAndFeel());
428: } catch (Exception e) {
429: }
430:
431: // System.setProperty ("nb.tabcontrol.fx.gratuitous","true");
432:
433: try {
434: Class c = Class.forName("org.netbeans.swing.plaf.Startup");
435: Method m = c.getDeclaredMethod("run", new Class[] {
436: Class.class, Integer.TYPE, java.net.URL.class });
437: System.err.println("Installing customs");
438: m.invoke(null, new Object[] { null, new Integer(0), null });
439: } catch (Exception e) {
440: System.err.println("Could not find plaf library");
441: }
442:
443: new TestFrame().show();
444: }
445:
446: private static void parseArgs(String[] s) {
447: try {
448: for (int i = 0; i < s.length; i++) {
449: System.err.println("Arg: " + s[i]);
450: if (s[i].indexOf('=') != -1) {
451: StringTokenizer st = new StringTokenizer(s[i], "=");
452: String key = st.nextToken();
453: String val = st.nextToken();
454: System.setProperty(key, val);
455: }
456: }
457: } catch (Exception e) {
458: e.printStackTrace();
459: }
460: }
461:
462: private static class MyRepaintManager extends RepaintManager {
463: private RepaintManager r;
464:
465: public MyRepaintManager(RepaintManager r) {
466: this .r = r;
467: }
468:
469: public synchronized void addInvalidComponent(
470: JComponent invalidComponent) {
471: System.err.println("AddInvalidComponent "
472: + invalidComponent);
473: super .addInvalidComponent(invalidComponent);
474: }
475:
476: public synchronized void addDirtyRegion(JComponent c, int x,
477: int y, int w, int h) {
478: System.err.println("addDirtyRegion " + x + "," + y + ","
479: + w + "," + h + " c=" + c);
480: super .addDirtyRegion(c, x, y, w, h);
481: // Thread.dumpStack();
482: }
483:
484: public void markCompletelyDirty(JComponent aComponent) {
485: System.err.println("MarkCompletelyDirty " + aComponent);
486: super .markCompletelyDirty(aComponent);
487: }
488:
489: public void markCompletelyClean(JComponent aComponent) {
490: super .markCompletelyClean(aComponent);
491: }
492: }
493:
494: // Variables declaration - do not modify//GEN-BEGIN:variables
495: // End of variables declaration//GEN-END:variables
496:
497: //Credit goes to Rick Ross of Javalobby for this - I needed something
498: //to cut and paste
499: private static final String TEXT = "Tools for Java developers are like "
500: + "restaurants in New York City. Even if you visited a different restaurant "
501: + "in NYC for every meal, every day, you would never get to all the eateries "
502: + "in the Big Apple. Likewise with Java tools, you could probably try a new "
503: + "Java development tool every single day and never get through all of them. "
504: + "There is simply an amazing adundance of Java tools available today, and "
505: + "a great many of them are absolutely free. Why do we still hear industry "
506: + "pundits complaining that Java lacks tools? I don't know what the heck they "
507: + "are talking about? This seems to be one of those criticisms that lives long "
508: + "past the time when it is no longer valid. A few years ago it may have been "
509: + "true that there weren't as many Java tools available as most developers "
510: + "would have liked, but today there are so many that no-one among us could "
511: + "possibly hope to keep up with the flow. Here's a sample list from A to Z, "
512: + "and none of these will cost you a penny.";
513:
514: }
|