package itunes;
import com.jacob.com.*;
import itunes.ItunesEvents;
import javax.swing.*;
import java.awt.event.*;
import java.io.IOException;
import java.awt.BorderLayout;
import com.jacob.activeX.ActiveXComponent;
public class ItunesScraper extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private ITunesThread iTunes;
public ItunesScraper() {
JButton btnExit = new JButton("Exit");
btnExit.addActionListener(new ExitListener());
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(btnExit, BorderLayout.CENTER);
this.addWindowListener(new ExitListener());
this.pack();
iTunes = new ITunesThread();
iTunes.start();
}
public void doExit() {
iTunes.quit = true;
while (!iTunes.finished) {
synchronized(this) {
try {
System.out.println("Waiting for the ITunesThread to finish");
wait(100);
} catch (InterruptedException e) {}
}
}
System.exit(0);
}
private class ExitListener implements ActionListener, WindowListener {
public void actionPerformed(ActionEvent evt) {
doExit();
}
public void windowClosing(WindowEvent e) {
doExit();
}
public void windowActivated(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowOpened(WindowEvent e) {}
}
private class ITunesThread extends Thread {
public boolean quit = false;
public boolean finished = false;
public void run () {
ComThread.InitMTA(true);
ActiveXComponent iTunesCom = new ActiveXComponent("iTunes.Application");
Dispatch iTunesController = (Dispatch)iTunesCom.getObject();
DispatchEvents events = new DispatchEvents(iTunesController, new ItunesEvents());
while (!quit) {
try {
sleep(100);
} catch (InterruptedException e) {}
}
ComThread.Release();
finished = true;
System.out.println("ITunesThread has finished");
}
}
public static void main(String[] args) {
ItunesScraper app = new ItunesScraper();
app.setVisible(true);
}
}