1 19 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 ; 35 import java.awt.event.ActionListener ; 36 import java.lang.reflect.Method ; 37 import java.util.StringTokenizer ; 38 39 44 public class TestFrame extends javax.swing.JFrame { 45 46 47 50 public TestFrame() { 51 setDefaultCloseOperation (javax.swing.WindowConstants.EXIT_ON_CLOSE); 52 initComponents(); 53 70 71 try { 72 } catch (Exception e) { 74 } 75 76 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 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(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 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 () { 159 public void actionPerformed(ActionEvent ae) { 160 tab.getSelectionModel().setSelectedIndex(3); 161 int[] indices = new int[]{0, 3, 5}; 162 String [] newText = new String []{ 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 () { 174 public void actionPerformed(ActionEvent ae) { 175 tab.getModel().removeTab(3); 176 } 177 }); 178 179 jb2.addActionListener(new ActionListener () { 180 public void actionPerformed(ActionEvent 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 () { 188 public void actionPerformed(ActionEvent ae) { 189 tab.getModel().removeTabs(new int[] {1, 3, 7, 8}); 190 } 191 }); 192 193 jb8.addActionListener(new ActionListener () { 194 public void actionPerformed(ActionEvent 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 () { 208 public void actionPerformed(ActionEvent 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 () { 222 public void actionPerformed(ActionEvent ae) { 223 tab.getModel().removeTabs(1, 4); 224 } 225 }); 226 227 jb11.addActionListener(new ActionListener () { 228 public void actionPerformed(ActionEvent 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 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 newData[1] = new TabData (new JLabel ("Hi there"), myIcon, "New tab", "foo"); 244 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 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 368 private void initComponents() { 370 addWindowListener(new java.awt.event.WindowAdapter () { 371 public void windowClosing(java.awt.event.WindowEvent evt) { 372 exitForm(evt); 373 } 374 }); 375 376 pack(); 377 } 379 382 private void exitForm(java.awt.event.WindowEvent evt) { System.exit(0); 384 } 386 389 public static void main(String args[]) throws Exception { 390 parseArgs (args); 391 394 try { 398 } catch (Exception e) { 400 } 401 402 404 try { 405 Class c = Class.forName("org.netbeans.swing.plaf.Startup"); 406 Method m = c.getDeclaredMethod("run", new Class [] {Class .class, Integer.TYPE, java.net.URL .class}); 407 System.err.println("Installing customs"); 408 m.invoke(null, new Object [] {null, new Integer (0), null}); 409 } catch (Exception e) { 410 System.err.println("Could not find plaf library"); 411 } 412 413 new TestFrame().show(); 414 } 415 416 private static void parseArgs (String [] 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 st = new StringTokenizer (s[i], "="); 422 String key = st.nextToken(); 423 String val = st.nextToken(); 424 System.setProperty (key, val); 425 } 426 } 427 } catch (Exception 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 } 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 469 private static final String 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 |