KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > swing > tabcontrol > demo > TestFrame


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 NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 /*
20  * TestFrame.java
21  *
22  * Created on May 28, 2003, 2:50 AM
23  */

24
25 package org.netbeans.swing.tabcontrol.demo;
26
27 import org.netbeans.swing.tabcontrol.TabData;
28 import org.netbeans.swing.tabcontrol.TabDataModel;
29 import org.netbeans.swing.tabcontrol.DefaultTabDataModel;
30 import org.netbeans.swing.tabcontrol.TabbedContainer;
31
32 import javax.swing.*;
33 import java.awt.*;
34 import java.awt.event.ActionEvent JavaDoc;
35 import java.awt.event.ActionListener JavaDoc;
36 import java.lang.reflect.Method JavaDoc;
37 import java.util.StringTokenizer JavaDoc;
38
39 /**
40  * A frame to test the tab component
41  *
42  * @author Tim Boudreau
43  */

44 public class TestFrame extends javax.swing.JFrame JavaDoc {
45
46
47     /**
48      * Creates new form TestFrame
49      */

50     public TestFrame() {
51         setDefaultCloseOperation (javax.swing.WindowConstants.EXIT_ON_CLOSE);
52         initComponents();
53         /*
54         try {
55             
56             System.setProperty("os.version", new Float(3.5).toString());
57             Method m = Toolkit.class.getDeclaredMethod("setDesktopProperty",
58             new Class[] {String.class, Object.class});
59             m.setAccessible(true);
60             m.invoke(java.awt.Toolkit.getDefaultToolkit(),
61             new Object[] {"win.xpstyle.themeActive", Boolean.FALSE });
62  
63             
64             UIManager.setLookAndFeel(new PseudoWin());
65 // UIManager.setLookAndFeel(new javax.swing.plaf.metal.MetalLookAndFeel());
66         } catch (Exception e) {
67             e.printStackTrace();
68         }
69          */

70         
71         try {
72 // UIManager.setLookAndFeel(new javax.swing.plaf.metal.MetalLookAndFeel());
73
} catch (Exception JavaDoc e) {
74         }
75
76 // UIManager.put ("EditorTabDisplayerUI", "org.netbeans.swing.tabcontrol.plaf.WinClassicEditorTabDisplayerUI");
77
// UIManager.put ("ViewTabDisplayerUI", "org.netbeans.swing.tabcontrol.plaf.WinClassicViewTabDisplayerUI");
78
// UIManager.put ("EditorTabDisplayerUI", "org.netbeans.swing.tabcontrol.plaf.WinXPEditorTabDisplayerUI");
79
// UIManager.put ("ViewTabDisplayerUI", "org.netbeans.swing.tabcontrol.plaf.WinXPViewTabDisplayerUI");
80

81         
82         JLabel jb1 = new JLabel("Label 1");
83         final JButton jb2 = new JButton("Button 2 - Update UI");
84         JButton jb3 = new JButton("Click me to remove this tab");
85         JLabel jb4 = new JLabel("Label 4");
86         JLabel jb5 = new JLabel("Label 5");
87         JButton jb6 = new JButton(
88                 "Click me to programmatically set the selected index to 3 atomically change some tab text and icons");
89         JTree jtr = new JTree();
90
91         jb1.setBackground(Color.ORANGE);
92         jb2.setBackground(Color.YELLOW);
93         jb3.setBackground(Color.CYAN);
94         jb4.setBackground(Color.GREEN);
95         jb2.setOpaque(true);
96         jb3.setOpaque(true);
97         jb4.setOpaque(true);
98         jb1.setOpaque(true);
99
100         JTextArea JTA = new JTextArea (TEXT);
101         JTA.setWrapStyleWord(true);
102         JTA.setColumns(80);
103         JTA.setLineWrap(true);
104         
105         JButton jb7 = new JButton ("Remove non-contiguous tabs");
106         JButton jb8 = new JButton ("Discontig add");
107         
108         JButton jb9 = new JButton ("Contig add");
109         JButton jb10 = new JButton ("Contig remove");
110         
111         JButton jb11 = new JButton ("Discontig add and remove");
112         
113
114         TabData tab0 = new TabData(jtr, myIcon, "0 JTree", "0");
115         TabData tab1 = new TabData(jb1, myIcon, "1 Tab 1", "1");
116         TabData tab2 = new TabData(jb2, myIcon2, "2 Different icon", "2");
117         TabData tab3 = new TabData(jb3, myIcon, "3 Tab 3", "3");
118         TabData tab4 = new TabData(jb4, myIcon,
119                                    "<html>A <b>tab</b> with <font color='#3333DD'><i><u>html</u></i></font> <s>text</s> and stuff</html>",
120                                    null);
121 // TabData tab4 = new TabData(jb4, myIcon, "4 A bunch of stuff", "4");
122
TabData tab5 = new TabData(jb5, myIcon, "5 Tab 5", "5");
123         TabData tab6 = new TabData(jb6, myIcon2, "6 Click me!", "6");
124
125 // 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);
126
TabData tab7 = new TabData(JTA, null, "7 some text",
127                                    "7");
128         TabData tab8 = new TabData(new JLabel("gioo"), myIcon,
129                                    "8 something", "8");
130         TabData tab9 = new TabData(jb7, myIcon, "9 Discontig remove",
131                                    "9");
132         TabData tab10 = new TabData(new JLabel("gioo"), myIcon, "10 wiggle",
133                                     "10");
134         TabData tab11 = new TabData(jb8, myIcon, "11 Discontig add",
135                                     "11");
136         TabData tab12 = new TabData(jb9, myIcon,
137                                     "12 contig add", "12");
138         TabData tab13 = new TabData(jb10, myIcon,
139                                     "13 contig remove", "13");
140         TabData tab14 = new TabData(jb11, myIcon, "14 MUNGE",
141                                     "14");
142
143         TabDataModel mdl = new DefaultTabDataModel(new TabData[]{
144             tab0, tab1, tab2, tab3, tab4, tab5, tab6, tab7, tab8, tab9, tab10,
145             tab11, tab12, tab13, tab14});
146  
147         /*
148         TabDataModel mdl = new DefaultTabDataModel(
149             new TabData[] { tab0, tab1, tab2, tab3, tab4, tab5}
150         );
151          */

152         
153         final TabbedContainer tab = new TabbedContainer(mdl, TabbedContainer.TYPE_VIEW);
154         tab.setActive(true);
155         tab.requestAttention(5);
156         tab.requestAttention(3);
157         
158         jb6.addActionListener(new ActionListener JavaDoc() {
159             public void actionPerformed(ActionEvent JavaDoc ae) {
160                 tab.getSelectionModel().setSelectedIndex(3);
161                 int[] indices = new int[]{0, 3, 5};
162                 String JavaDoc[] newText = new String JavaDoc[]{
163                     "I am still a JTree",
164                     "<html>Changed <b><font color='#FFFF00'>3</font></b></html>",
165                     "<html><s>changed</s></html> 5"};
166                 Icon[] icons = new Icon[]{myIcon3, myIcon3, myIcon3};
167                 tab.getModel().setIconsAndText(indices, newText, icons);
168             }
169         });
170         
171         
172
173         jb3.addActionListener(new ActionListener JavaDoc() {
174             public void actionPerformed(ActionEvent JavaDoc ae) {
175                 tab.getModel().removeTab(3);
176             }
177         });
178         
179         jb2.addActionListener(new ActionListener JavaDoc() {
180             public void actionPerformed(ActionEvent JavaDoc ae) {
181                 UIManager.put ("EditorTabDisplayerUI", "org.netbeans.swing.tabcontrol.plaf.WinClassicEditorTabDisplayerUI");
182                 UIManager.put ("ViewTabDisplayerUI", "org.netbeans.swing.tabcontrol.plaf.WinClassicViewTabDisplayerUI");
183                 tab.updateUI();
184             }
185         });
186         
187         jb7.addActionListener(new ActionListener JavaDoc() {
188             public void actionPerformed(ActionEvent JavaDoc ae) {
189                 tab.getModel().removeTabs(new int[] {1, 3, 7, 8});
190             }
191         });
192         
193         jb8.addActionListener(new ActionListener JavaDoc() {
194             public void actionPerformed(ActionEvent JavaDoc ae) {
195                 int[] idxs = new int [] { 1, 3, 6, 8};
196                 TabData[] td = new TabData[] {
197                     new TabData(new JButton("inserted 1"), myIcon, "I-1", "tip"),
198                     new TabData(new JButton("inserted 3"), myIcon, "I-3", "tip"),
199                     new TabData(new JButton("inserted 6"), myIcon, "I-6", "tip"),
200                     new TabData(new JButton("inserted 8"), myIcon, "I-8", "tip"),
201                     
202                 };
203                 tab.getModel().addTabs(idxs, td);
204             }
205         });
206         
207         jb9.addActionListener(new ActionListener JavaDoc() {
208             public void actionPerformed(ActionEvent JavaDoc ae) {
209                 int[] idxs = new int [] { 1, 3, 6, 8};
210                 TabData[] td = new TabData[] {
211                     new TabData(new JButton("inserted c 1"), myIcon, "Ic-1", "tip"),
212                     new TabData(new JButton("inserted c 2"), myIcon, "Ic-2", "tip"),
213                     new TabData(new JButton("inserted c 3"), myIcon, "Ic-3", "tip"),
214                     new TabData(new JButton("inserted c 4"), myIcon, "Ic-4", "tip"),
215                     
216                 };
217                 tab.getModel().addTabs(1, td);
218             }
219         });
220         
221         jb10.addActionListener(new ActionListener JavaDoc() {
222             public void actionPerformed(ActionEvent JavaDoc ae) {
223                 tab.getModel().removeTabs(1, 4);
224             }
225         });
226         
227         jb11.addActionListener(new ActionListener JavaDoc() {
228             public void actionPerformed(ActionEvent JavaDoc ae) {
229                 TabData[] data = (TabData[]) tab.getModel().getTabs().toArray(new TabData[0]);
230                 
231                 TabData[] newData = new TabData [ data.length - 3];
232                 int ct = 0;
233                 
234                 //strip out some tabs
235
for (int i=0; i < data.length; i++) {
236                     newData[ct] = data[i];
237                     if (i != 2 && i != 3 && i != 7) {
238                         ct++;
239                     }
240                 }
241                 
242                 //replace one tab
243
newData[1] = new TabData (new JLabel ("Hi there"), myIcon, "New tab", "foo");
244                 //swap some tabs
245
TabData td = newData[8];
246                 newData[8] = newData[7];
247                 newData[7] = td;
248                 
249                 tab.getModel().setTabs(newData);
250             }
251         });
252
253         tab.setActive(true);
254         
255 // jb2.addMouseListener (new MouseAdapter() {
256
// public void mouseClicked(MouseEvent e) {
257
// Point point = e.getPoint();
258
// System.out.println("Constraint for location is: " + tab.getConstraintForLocation(point, false));
259
// System.out.println("Tab index is: " + tab.tabForCoordinate(point.x, point.y));
260
// System.out.println(tab.getIndicationForLocation(point, false, new Point(0, 0), false));
261
// }
262
// }); // PENDING
263
// tab.setActive(true);
264
getContentPane().add(tab);
265         setSize(700, 300);
266         setLocation(12, 12);
267     }
268
269
270     public class PseudoWin
271             extends com.sun.java.swing.plaf.windows.WindowsLookAndFeel {
272
273         public boolean isSupportedLookAndFeel() {
274             return true;
275         }
276     }
277
278     TabDataModel mdl = null;
279     Icon myIcon = new Icon() {
280         public int getIconWidth() {
281             return 16;
282         }
283
284         public int getIconHeight() {
285             return 14;
286         }
287
288         public void paintIcon(Component c, Graphics g, int x, int y) {
289             g.setColor (Color.BLUE);
290             g.fillRect (x, y, getIconWidth()-4, getIconHeight());
291             
292             g.setColor (Color.YELLOW);
293             g.drawLine(x+2, y+2, x+3, y+3);
294             g.drawLine(x+2, y+3, x+3, y+4);
295
296             g.drawLine (x+8, y+2, x+7, y+3);
297             g.drawLine (x+8, y+3, x+7, y+4);
298             
299             g.drawLine (x+4, y+6, x+6, y+6);
300             
301             g.drawRoundRect(x+3, y+9, 5, 3, 8, 5);
302         }
303     };
304
305     Icon myIcon2 = new Icon() {
306         public int getIconWidth() {
307             return 19;
308         }
309
310         public int getIconHeight() {
311             return 14;
312         }
313
314         public void paintIcon(Component c, Graphics g, int x, int y) {
315             g.setColor(Color.ORANGE);
316             g.fillRect(x, y, getIconWidth() - 4, getIconHeight());
317             g.setColor(Color.RED);
318             g.drawRect(x, y, getIconWidth() - 4, getIconHeight());
319
320
321             g.setColor(Color.BLUE);
322             g.drawLine(x + 2, y + 2, x + 3, y + 3);
323             g.drawLine(x + 2, y + 3, x + 3, y + 4);
324
325             g.drawLine(x + 7, y + 2, x + 6, y + 3);
326             g.drawLine(x + 7, y + 3, x + 6, y + 4);
327
328             g.drawLine(x + 5, y + 6, x + 6, y + 6);
329
330             g.drawRoundRect(x + 4, y + 9, 3, 3, 8, 5);
331         }
332     };
333
334     Icon myIcon3 = new Icon() {
335         public int getIconWidth() {
336             return 19;
337         }
338
339         public int getIconHeight() {
340             return 14;
341         }
342
343         public void paintIcon(Component c, Graphics g, int x, int y) {
344             g.setColor(Color.CYAN);
345             g.fillRect(x, y, getIconWidth() - 4, getIconHeight());
346             g.setColor(Color.WHITE);
347             g.drawRect(x, y, getIconWidth() - 4, getIconHeight());
348
349
350             g.setColor(Color.BLACK);
351             g.drawLine(x + 2, y + 2, x + 3, y + 3);
352             g.drawLine(x + 2, y + 3, x + 3, y + 4);
353
354             g.drawLine(x + 7, y + 2, x + 6, y + 3);
355             g.drawLine(x + 7, y + 3, x + 6, y + 4);
356
357             g.drawLine(x + 5, y + 6, x + 6, y + 6);
358
359             g.drawRoundRect(x + 4, y + 9, 3, 3, 8, 5);
360         }
361     };
362
363     /**
364      * This method is called from within the constructor to initialize the form.
365      * WARNING: Do NOT modify this code. The content of this method is always
366      * regenerated by the Form Editor.
367      */

368     private void initComponents() {//GEN-BEGIN:initComponents
369

370         addWindowListener(new java.awt.event.WindowAdapter JavaDoc() {
371             public void windowClosing(java.awt.event.WindowEvent JavaDoc evt) {
372                 exitForm(evt);
373             }
374         });
375
376         pack();
377     }//GEN-END:initComponents
378

379     /**
380      * Exit the Application
381      */

382     private void exitForm(java.awt.event.WindowEvent JavaDoc evt) {//GEN-FIRST:event_exitForm
383
System.exit(0);
384     }//GEN-LAST:event_exitForm
385

386     /**
387      * @param args the command line arguments
388      */

389     public static void main(String JavaDoc args[]) throws Exception JavaDoc {
390         parseArgs (args);
391 // UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
392
// System.setProperty("apple.awt.brushMetalLook", "true");
393

394         // RepaintManager nue = new MyRepaintManager(RepaintManager.currentManager(new javax.swing.JFrame()));
395
// RepaintManager.setCurrentManager(nue);
396
// System.setProperty ("nb.forceUI", "WindowsXPLFCustoms");
397
try {
398 // UIManager.setLookAndFeel(new javax.swing.plaf.metal.MetalLookAndFeel());
399
} catch (Exception JavaDoc e) {
400         }
401        
402      // System.setProperty ("nb.tabcontrol.fx.gratuitous","true");
403

404         try {
405             Class JavaDoc c = Class.forName("org.netbeans.swing.plaf.Startup");
406             Method JavaDoc m = c.getDeclaredMethod("run", new Class JavaDoc[] {Class JavaDoc.class, Integer.TYPE, java.net.URL JavaDoc.class});
407             System.err.println("Installing customs");
408             m.invoke(null, new Object JavaDoc[] {null, new Integer JavaDoc(0), null});
409         } catch (Exception JavaDoc e) {
410             System.err.println("Could not find plaf library");
411         }
412         
413         new TestFrame().show();
414     }
415
416     private static void parseArgs (String JavaDoc[] s) {
417         try {
418         for (int i=0; i < s.length; i++) {
419             System.err.println ("Arg: " + s[i]);
420             if (s[i].indexOf ('=') != -1) {
421                 StringTokenizer JavaDoc st = new StringTokenizer JavaDoc (s[i], "=");
422                 String JavaDoc key = st.nextToken();
423                 String JavaDoc val = st.nextToken();
424                 System.setProperty (key, val);
425             }
426         }
427         } catch (Exception JavaDoc e) {
428             e.printStackTrace();
429         }
430     }
431
432
433     private static class MyRepaintManager extends RepaintManager {
434         private RepaintManager r;
435
436         public MyRepaintManager(RepaintManager r) {
437             this.r = r;
438         }
439
440         public synchronized void addInvalidComponent(
441                 JComponent invalidComponent) {
442             System.err.println("AddInvalidComponent " + invalidComponent);
443             super.addInvalidComponent(invalidComponent);
444         }
445
446         public synchronized void addDirtyRegion(JComponent c, int x, int y,
447                                                 int w, int h) {
448             System.err.println("addDirtyRegion " + x + "," + y + "," + w + ","
449                                + h
450                                + " c=" + c);
451             super.addDirtyRegion(c, x, y, w, h);
452 // Thread.dumpStack();
453
}
454
455         public void markCompletelyDirty(JComponent aComponent) {
456             System.err.println("MarkCompletelyDirty " + aComponent);
457             super.markCompletelyDirty(aComponent);
458         }
459
460         public void markCompletelyClean(JComponent aComponent) {
461             super.markCompletelyClean(aComponent);
462         }
463     }
464     
465     
466     // Variables declaration - do not modify//GEN-BEGIN:variables
467
// End of variables declaration//GEN-END:variables
468

469     //Credit goes to Rick Ross of Javalobby for this - I needed something
470
//to cut and paste
471
private static final String JavaDoc TEXT = "Tools for Java developers are like " +
472     "restaurants in New York City. Even if you visited a different restaurant " +
473     "in NYC for every meal, every day, you would never get to all the eateries " +
474     "in the Big Apple. Likewise with Java tools, you could probably try a new " +
475     "Java development tool every single day and never get through all of them. " +
476     "There is simply an amazing adundance of Java tools available today, and " +
477     "a great many of them are absolutely free. Why do we still hear industry " +
478     "pundits complaining that Java lacks tools? I don't know what the heck they " +
479     "are talking about? This seems to be one of those criticisms that lives long " +
480     "past the time when it is no longer valid. A few years ago it may have been " +
481     "true that there weren't as many Java tools available as most developers " +
482     "would have liked, but today there are so many that no-one among us could " +
483     "possibly hope to keep up with the flow. Here's a sample list from A to Z, " +
484     "and none of these will cost you a penny.";
485     
486 }
487
Popular Tags