1 23 24 package org.objectweb.fractal.gui.dialog.view; 25 26 import java.awt.Color ; 27 import java.awt.Container ; 28 import java.awt.Dimension ; 29 import java.awt.Font ; 30 import java.awt.FontMetrics ; 31 import java.awt.BorderLayout ; 32 import java.awt.FlowLayout ; 33 import java.awt.GridLayout ; 34 import java.awt.GridBagConstraints ; 35 import java.awt.GridBagLayout ; 36 import java.awt.AWTEvent ; 37 import java.awt.event.ActionEvent ; 38 import java.awt.event.ActionListener ; 39 import java.awt.event.WindowEvent ; 40 import java.awt.Insets ; 41 import java.awt.Component ; 42 43 import java.util.Enumeration ; 44 import java.util.Iterator ; 45 import java.util.List ; 46 import java.util.TreeMap ; 47 import java.util.TreeSet ; 48 import java.util.ArrayList ; 49 import java.util.jar.JarFile ; 50 import java.util.zip.ZipEntry ; 51 52 import java.lang.reflect.Method ; 53 54 import java.net.URL ; 55 56 import java.io.File ; 57 import java.io.FileInputStream ; 58 import java.io.FileOutputStream ; 59 import java.io.PrintStream ; 60 import javax.swing.BorderFactory ; 61 import javax.swing.ListCellRenderer ; 62 import javax.swing.DefaultListModel ; 63 import javax.swing.ImageIcon ; 64 import javax.swing.JDialog ; 65 import javax.swing.JLabel ; 66 import javax.swing.JList ; 67 import javax.swing.JMenu ; 68 import javax.swing.JPanel ; 69 import javax.swing.JTabbedPane ; 70 import javax.swing.JTextArea ; 71 import javax.swing.JButton ; 72 import javax.swing.JScrollPane ; 73 import javax.swing.text.Document ; 74 import javax.swing.event.DocumentListener ; 75 import javax.swing.event.DocumentEvent ; 76 import javax.swing.event.ListSelectionListener ; 77 import javax.swing.event.ListSelectionEvent ; 78 79 public class ClassSelector extends JDialog implements 80 ActionListener , 81 DocumentListener 82 { 83 static String LS = new String (System.getProperty ("line.separator")); 84 static String FS = new String (System.getProperty ("file.separator")); 85 static String UD = new String (System.getProperty ("user.dir")); 86 static String CP = new String (System.getProperty ("java.class.path")); 87 static String PS = new String (System.getProperty ("path.separator")); 88 89 static String EXIT = "Exit"; 90 static String UP = "Up"; 91 static String SELECT = "Select"; 92 static String SEARCH = "Implem."; 93 94 static int SEL_INT = 1; 95 static int SEL_CLA = 2; 96 int WIDTH = 600; 97 int HEIGHT = 600; 98 99 int filter = SEL_INT + SEL_CLA; 100 101 List lp = new ArrayList (); 102 List lpgen = new ArrayList (); 103 104 URL urlc, urli, urlp; 105 106 ImageIcon imgClasse; 107 ImageIcon imgInterface; 108 ImageIcon imgPackage; 109 JMenu menu, menuo; 110 File f; 111 FileInputStream fis; 112 FileOutputStream fos = null; 113 PrintStream of = null; 114 Container cp; 115 Color buttonColor = new Color (204, 255, 204); 117 Color buttonColor1 = new Color (180, 224, 180); 118 Color baseColor = new Color (200, 200, 164); 119 Color classColor = new Color (255, 255, 204); 120 Font buttonFont = new Font ("Arial", Font.PLAIN, 9); 121 Font normalFont = new Font ("Arial", Font.PLAIN, 11); 122 Font menuFont = new Font ("Arial", Font.BOLD, 12); 123 Font listFont = new Font ("MonoSpaced", Font.PLAIN, 11); 124 int nc = 0; 125 int MAX = 3; 126 JTabbedPane jtp; 127 TreeMap mapImport = new TreeMap (); 128 129 String [] path = new String [MAX]; 130 CapLabel [] clb = new CapLabel [MAX]; 131 DefaultListModel lmAll; 132 CapList cplAll; 133 DefaultListModel lmClass; 134 CapList cplClass; 135 CapTextArea transit; 136 CapButton b_up = new CapButton (UP, buttonColor1); 137 CapButton b_search = new CapButton (SEARCH, buttonColor1); 138 CapButton b_select = new CapButton (SELECT, buttonColor); 139 CapButton b_exit = new CapButton (EXIT, buttonColor); 140 String libIntrf = "(Interface)"; 141 String libClass = "(Class)"; 142 String libPackg = "(Package)"; 143 String [] noms = { 144 "Packages, Classes and Interfaces", 145 "Looking for Implementation ", 146 }; 147 String prefix = new String (""); 148 String curInterf = new String (""); 149 static String retour = ""; 150 int z = 1; 151 152 public ClassSelector () { 153 this.setTitle("Please, make your selection"); 154 this.setModal(true); 155 156 enableEvents (AWTEvent.WINDOW_EVENT_MASK); 158 160 urlc = getClass().getResource("/org/objectweb/fractal/gui/resources/classe.gif"); 161 imgClasse = new ImageIcon (urlc); 162 urli = getClass().getResource("/org/objectweb/fractal/gui/resources/interface.gif"); 163 imgInterface = new ImageIcon (urli); 164 urlp = getClass().getResource("/org/objectweb/fractal/gui/resources/package.gif"); 165 imgPackage = new ImageIcon (urlp); 166 167 cp = getContentPane(); 168 cp.setForeground (Color.black); 169 170 for (int i = 0; i < 2; i++) { 171 clb[i] = new CapLabel (noms[i]); 172 } 173 lmAll = new DefaultListModel (); 174 lmClass = new DefaultListModel (); 175 cplAll = new CapList (lmAll, "All"); 176 cplClass = new CapList (lmClass, "Class"); 177 cplAll.setCellRenderer(new MyRenderer(1024)); 178 cplClass.setCellRenderer(new MyRenderer(1024)); 179 cplClass.setBackground(classColor); 180 181 transit = new CapTextArea (true); 182 transit.getDocument().addDocumentListener(this); 183 GridBagLayout gbl = new GridBagLayout (); 184 GridBagConstraints gbc = new GridBagConstraints (); 185 cp.setLayout (gbl); 186 187 gbc.insets = new Insets (5, 2, 2, 5); 188 189 191 gbc.gridy = 0; 192 gbc.weightx = 0; 193 gbc.weighty = 0; 194 gbc.fill = GridBagConstraints.BOTH; 195 gbc.anchor = GridBagConstraints.NORTHWEST; 196 gbc.gridx = 0; 197 b_up.addActionListener(this); 198 JPanel jp = new JPanel (); 199 jp.setLayout(new BorderLayout ()); 200 jp.add(clb[0], BorderLayout.WEST); 201 jp.add(b_up, BorderLayout.EAST); 202 gbl.setConstraints(jp, gbc); 203 cp.add (jp); 204 205 207 gbc.gridx = 0; 208 gbc.gridy = 1; 209 gbc.weightx = 1; 210 gbc.weighty = 0.8; 211 JPanel jp1 = new JPanel (); 212 jp1.setLayout(new FlowLayout (FlowLayout.LEFT, 0, 0)); 213 jp1.add(cplAll); 214 jp1.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0)); 215 JScrollPane jsp = new JScrollPane (jp1); 216 jsp.setPreferredSize(new Dimension (WIDTH, 60)); 217 jsp.getVerticalScrollBar().setUnitIncrement(6); 219 jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 220 gbl.setConstraints(jsp, gbc); 221 cp.add (jsp); 222 223 225 gbc.gridx = 0; 226 gbc.gridy = 2; 227 gbc.weightx = 0; 228 gbc.weighty = 0; 229 b_select.addActionListener(this); 230 b_search.addActionListener(this); 231 b_exit.addActionListener(this); 232 b_select.setUsable(false); 233 JPanel jpb = new JPanel (new GridLayout (1, 3)); 234 jpb.add(b_exit); 235 jpb.add(b_select); 236 jpb.add(b_search); 237 238 JPanel jpi = new JPanel (); 239 jpi.setLayout(new BorderLayout ()); 240 jpi.add(clb[1], BorderLayout.WEST); 241 jpi.add(jpb, BorderLayout.EAST); 242 gbl.setConstraints(jpi, gbc); 243 cp.add (jpi); 244 245 247 gbc.gridx = 0; 248 gbc.gridy = 3; 249 gbc.weightx = 0; 250 gbc.weighty = 1; 251 252 jtp = new JTabbedPane (); 253 254 JPanel jp1c = new JPanel (); 255 jp1c.setLayout(new FlowLayout (FlowLayout.LEFT, 0, 0)); 256 jp1c.add(cplClass); 257 jp1c.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0)); 258 JScrollPane jspc = new JScrollPane (jp1c); 259 jspc.setPreferredSize(new Dimension (WIDTH, 60)); 260 jspc.getVerticalScrollBar().setUnitIncrement(6); 262 jspc.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 263 jtp.addTab ("Implementation(s)", imgClasse, jspc); 264 265 JScrollPane jsinf = new JScrollPane (transit); 266 jtp.addTab ("interface's method(s)", jsinf); 267 b_up.setUsable(false); 268 b_search.setUsable(false); 269 270 gbl.setConstraints(jtp, gbc); 271 cp.add (jtp); 272 273 275 initList(); 276 remakeList (); 277 278 pack (); 280 setSize (WIDTH, HEIGHT); 281 show (); 282 } 283 284 public static String initSelector () { 285 new ClassSelector (); 286 return (retour); 288 } 289 290 private void refreshLooking () { 291 clb[1].setText(noms[1]); 292 b_select.setUsable(false); 293 lmClass.clear(); 294 transit.setText(""); 295 } 296 297 301 public void insertUpdate(DocumentEvent e) { 302 modifyListCommand (e); 303 } 304 305 public void removeUpdate(DocumentEvent e) { 306 modifyListCommand (e); 307 } 308 309 public void changedUpdate(DocumentEvent e) { 310 modifyListCommand (e); 311 } 312 313 private void modifyListCommand (DocumentEvent e) { 314 try { 315 Document d = e.getDocument(); 316 int i = 0; 317 String st = d.getText(0, d.getLength()); 318 transit.setText(st); 319 } catch (Exception ex) { } 320 } 321 322 326 327 protected void processWindowEvent (WindowEvent e) 328 { 329 if (e.getID() == WindowEvent.WINDOW_CLOSING) { 330 super.processWindowEvent (e); 331 } 333 } 334 335 339 public void actionPerformed (ActionEvent e) { 340 if (e.getActionCommand().equals(EXIT)) { 341 if (of != null) of.close(); 342 retour = ""; 343 dispose (); 344 } 345 else if (e.getActionCommand().equals(SELECT)) { 346 if (of != null) of.close(); 347 dispose (); 348 } 349 else if (e.getActionCommand().equals(UP)) { 350 int i = prefix.lastIndexOf('.'); 351 if (i < 1) { 352 prefix = ""; 353 b_up.setUsable(false); 354 } 355 else prefix = prefix.substring(0, i); 356 remakeList (); 357 refreshLooking (); 358 } 359 else if (e.getActionCommand().equals(SEARCH)) { 360 int i = curInterf.lastIndexOf('.'); 361 if (i < 1) { 362 clb[1].setText(noms[1]+"of "+curInterf); 363 } else { 364 clb[1].setText(noms[1]+"of [...] "+curInterf.substring(i)); 365 } 366 makeClassList (); 367 jtp.setSelectedIndex(0); 368 b_search.setUsable(false); 369 } 370 } 371 372 376 public void initList () { 377 int i = 999999; 378 String cp = new String (CP+PS.charAt(0)); 379 lpgen.clear(); 380 381 while (i > 0) { 382 i = cp.indexOf(PS.charAt(0)); if (i < 1) break; 383 lp.add(cp.substring(0, i)); 384 cp = cp.substring(i+1); 385 } 386 387 for (i = 0; i < lp.size(); i++) { 388 String morcif = (String )lp.get(i); 389 390 if (morcif.endsWith(".jar")) { 392 try { 393 JarFile jf = new JarFile (morcif); 394 for (Enumeration zipe = jf.entries() ; zipe.hasMoreElements() ;) { 395 ZipEntry zip = (ZipEntry )zipe.nextElement(); 396 if (zip.getName().endsWith(".class")) { 397 lpgen.add(zip.getName().replace('/', '.')); 398 } 399 } 400 } 401 catch (Exception ex) { ex.printStackTrace(); continue; } 402 } 403 404 else { 406 try { 407 scanDir (morcif, morcif); 408 } catch (Exception ex) { ex.printStackTrace(); continue; } 409 } 410 } 411 } 412 413 415 public void scanDir (String namefile, String base) throws Exception { 416 if (namefile == null) return; 417 File f = new File (namefile); 418 419 if (f.isDirectory()) { 420 File []files = f.listFiles(); 421 if (files == null) return; 422 for (int i = 0; i < files.length; i++) { 423 scanDir (namefile+FS+files[i].getName(), base); 424 } 425 } else { 426 if (!(namefile.endsWith(".class"))) return; 427 String classname = namefile.substring(base.length()+1); 428 lpgen.add (classname.replace(FS.charAt(0), '.')); 429 } 430 } 431 432 436 public void remakeList () { 437 TreeMap lcomb = new TreeMap (); 438 ClassLoader cl = this.getClass().getClassLoader(); 439 b_search.setUsable(false); 440 441 443 int len = prefix.length(); 444 445 for (int i = 0; i < lpgen.size(); i++) { 446 String item = (String )lpgen.get(i); 447 if ((len > 0) && (!item.startsWith(prefix+"."))) continue; 448 if (item.length() <= len) continue; 449 String litem; 450 if (len == 0) litem = item; 451 else litem = item.substring(len+1); 452 char c = litem.charAt(0); 453 int d; 454 if (c > 'Z') d = (int)c-32; 455 else d = c; 456 try { 457 String subName = (char)d+litem.substring(0, litem.indexOf('.')); 458 String nature = (String )lcomb.get(subName); 459 if (nature != null) continue; 460 lcomb.put(subName, item); 461 } catch (Exception ignore) { continue; } 462 } 463 lmAll.clear(); 464 465 TreeSet ts = new TreeSet (lcomb.keySet()); 466 for (Iterator it = ts.iterator(); it.hasNext();) { 467 String sta = (String )it.next(); 468 String st = sta.substring(1); 469 if ((st.charAt(0) < 'A') || (st.charAt(0) > 'Z')) { 470 lmAll.addElement (new JLabel (putLabel(st, libPackg), 471 imgPackage, JLabel.LEFT)); 472 } else { 473 if (prefix.length() > 1) st = prefix+"."+st; 474 try { 475 Class cla = cl.loadClass (st); 476 if (cla.isInterface()) { 477 if ((filter & SEL_INT) == 0) continue; 478 lmAll.addElement (new JLabel (putLabel(st, libIntrf), 479 imgInterface, JLabel.LEFT)); 480 } else { 481 if ((filter & SEL_CLA) == 0) continue; 482 lmAll.addElement (new JLabel (putLabel(st, libClass), 483 imgClasse, JLabel.LEFT)); 484 } 485 } 486 catch (Error er) { continue; } 487 catch (Exception ex) { continue; } 488 } 489 } 490 int z = lmAll.getSize(); 491 while (z < 20) { 492 lmAll.addElement(new JLabel ("", JLabel.LEFT)); 493 z++; 494 } 495 } 496 497 private String putLabel (String st, String label) { 498 String white = 499 " "; 500 FontMetrics fm = cplAll.fm; 501 int space = fm.charWidth(' '); 502 int d = 0; 503 for (int z = 0; z < st.length(); z++) d = d + fm.charWidth(st.charAt(z)); 504 int n = (WIDTH - 140 - d)/space; 505 if (n < 1) n = 1; 506 return (st+white.substring(0, n)+label); 507 } 508 509 513 public void makeClassList () { 514 TreeMap lcomb = new TreeMap (); 515 ClassLoader cl = this.getClass().getClassLoader(); 516 517 519 for (int i = 0; i < lpgen.size(); i++) { 520 String item = (String )lpgen.get(i); 521 522 int j = item.indexOf(".class"); 523 if (j < 1) continue; 524 525 try { 526 Class cla = cl.loadClass (item.substring(0, j)); 527 if (cla.isInterface()) continue; 528 529 Class [] lcl = cla.getInterfaces(); 530 for (int z = 0; z < lcl.length; z++) { 531 if (lcl[z].getName().equals(curInterf)) { 532 lcomb.put (item, "zz"); 533 break; 534 } 535 } 536 } 537 catch (Error er) { continue; } 538 catch (Exception ex) { continue; } 539 } 540 lmClass.clear(); 541 542 544 TreeSet ts = new TreeSet (lcomb.keySet()); 545 for (Iterator it = ts.iterator(); it.hasNext();) { 546 String sta = (String )it.next(); 547 String st = sta.substring(0); 548 lmClass.addElement (new JLabel (st, JLabel.LEFT)); 549 } 550 int z = lmClass.getSize(); 551 while (z < 20) { 552 lmClass.addElement(new JLabel ("", JLabel.LEFT)); 553 z++; 554 } 555 } 556 557 561 public void display () { 562 ClassLoader cl = this.getClass().getClassLoader(); 563 transit.setText(""); 564 mapImport.clear(); 565 566 try { 567 Class cla = cl.loadClass (curInterf); 568 int in = cla.getName().lastIndexOf('.'); 569 if (in < 1) { 570 transit.append(" interface "+cla.getName()+" "); 571 } 572 else { 573 transit.append(" package "+cla.getName().substring(0, in)+";"+LS+LS); 574 transit.append(" interface "+cla.getName().substring(in+1)+" "); 575 } 576 577 Class [] interf = cla.getInterfaces(); 578 if (interf.length == 0) transit.append("{"+LS); 579 else transit.append("extends "+LS); 580 for (int i = 0; i < interf.length; i++) { 581 transit.append(" "+reduc(interf[i].getName())); 582 if (i == interf.length-1) transit.append (" {"); 583 transit.append (LS); 584 } 585 transit.append (LS); 586 587 Method [] mets = cla.getDeclaredMethods(); 588 for (int i = 0; i < mets.length; i++) { 589 transit.append(" "+reduc(mets[i].getReturnType().getName())+" " 590 +reduc(mets[i].getName())); 591 592 Class [] param = mets[i].getParameterTypes(); 593 if (param.length == 0) { 594 transit.append (" ();"+LS+LS); 595 continue; 596 } 597 transit.append(" ("); 598 char virgule = ','; 599 for (int j = 0; j < param.length; j++) { 600 if (j == param.length-1) virgule = ')'; 601 if (j != 0) transit.append(" "); 602 transit.append (reduc(param[j].getName())+" p"+j+virgule+LS); 603 } 604 transit.append(LS); 605 } 606 transit.append(" }"+LS); 607 608 if (mapImport.size() == 0) return; 609 610 String total = transit.getText(); 611 transit.setText(""); 612 TreeSet ts = new TreeSet (mapImport.keySet()); 613 int d = 0; 614 for (Iterator it = ts.iterator(); it.hasNext();) { 615 transit.append (" import "+(String )it.next()+";"+LS); 616 } 617 transit.append(LS); 618 transit.append(total); 619 } 620 catch (Error er) { } 621 catch (Exception ex) { } 622 } 623 624 private String reduc (String st) { 625 int i = st.lastIndexOf('.'); 626 if (i < 1) return st; 627 if (st.substring(0, i).equals("java.lang")) return st; 628 mapImport.put(st, "zz"); 629 return st.substring(i+1); 630 } 631 632 636 class CapLabel extends JLabel { 637 CapLabel (String nom) { 638 super (" "+nom); 639 this.setFont(new Font ("Arial", Font.PLAIN, 12)); 640 this.setForeground(Color.black); 641 } 642 } 643 644 648 class CapTextArea extends JTextArea { 649 CapTextArea (boolean editable) { 650 super ("", 80, 80); 651 this.setBackground (Color.white); 652 this.setFont(listFont); 653 this.setBorder(BorderFactory.createLoweredBevelBorder()); 654 this.setEditable(editable); 655 } 656 } 657 658 662 class CapButton extends JButton { 663 CapButton (String lib, Color color) { 664 super (lib); 665 this.setBackground (color); 667 this.setForeground (Color.black); 668 this.setFont(buttonFont); 669 FontMetrics fm = this.getFontMetrics(buttonFont); 670 int d = 0; 671 for (int z = 0; z< lib.length(); z++) d = d + fm.charWidth(lib.charAt(z)); 672 this.setPreferredSize (new Dimension (36+d, 10)); 673 this.setEnabled(true); 674 } 675 676 public void setUsable (boolean b) { 677 this.setEnabled (b); 678 this.setOpaque (b); 679 } 680 } 681 682 686 class CapList extends JList implements ListSelectionListener { 687 DefaultListModel listmodel; 688 String ident = null; 689 FontMetrics fm; 690 691 CapList (DefaultListModel listModel, String id) { 692 super (listModel); 693 this.setBackground (Color.white); 694 this.setFont(listFont); 695 fm = this.getFontMetrics(this.getFont()); 696 setBorder(BorderFactory.createLoweredBevelBorder()); 697 listmodel = listModel; 698 this.addListSelectionListener(this); 699 ident = new String (id); 700 } 701 702 704 public void valueChanged (ListSelectionEvent e) { 705 if (e.getValueIsAdjusting() == false) { 706 707 if (this.getSelectedIndex() > -1) { 708 JLabel jlab = (JLabel )listmodel.getElementAt(this.getSelectedIndex()); 709 String texte = jlab.getText(); 710 711 String util = null; 712 int i = texte.indexOf(' '); 713 if (i > 0) util = texte.substring(0, i); 714 else util = texte; 715 716 if (texte.endsWith(libPackg)) { 718 if (prefix.length() < 1) { 719 prefix = prefix.concat(util); 720 } else { 721 prefix = prefix.concat("."+util); 722 } 723 remakeList (); 724 b_up.setUsable(true); 725 refreshLooking (); 726 retour = ""; 727 } 728 729 else if (texte.endsWith(libIntrf)) { 731 b_search.setUsable(true); 732 curInterf = util; 733 retour = ""; 734 jtp.setSelectedIndex(1); 735 refreshLooking (); 736 display (); 737 } 738 739 else { 741 if (ident.equals("Class")) retour = new String (texte); 742 else retour = new String (util); 743 b_select.setUsable(true); 744 } 745 } 746 } 747 } 748 } 749 750 754 class MyRenderer extends JLabel implements ListCellRenderer { 755 int w; 756 757 public MyRenderer (int width) { 758 setOpaque(true); 759 setHorizontalAlignment(LEFT); 760 setVerticalAlignment(CENTER); 761 w = width; 762 } 763 764 public Component getListCellRendererComponent ( 765 JList list, 766 Object value, 767 int index, 768 boolean isSelected, boolean cellHasFocus) { 769 770 if (isSelected) { 771 this.setBackground(list.getSelectionBackground()); 772 this.setForeground(list.getSelectionForeground()); 773 } else { 774 this.setBackground(list.getBackground()); 775 this.setForeground(list.getForeground()); 776 } 777 778 if (value instanceof JLabel ) { 779 JLabel jlab = (JLabel )value; 780 ImageIcon icon = (ImageIcon )jlab.getIcon(); 781 setText((String )jlab.getText()); 782 setIcon (icon); 783 this.setFont (listFont); 784 setPreferredSize(new Dimension (w, 17)); 785 } 786 return this; 787 } 788 } 789 } 791 | Popular Tags |