Package itunes

Source Code of itunes.ItunesScraper$ITunesThread

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);
  }
 

}
TOP

Related Classes of itunes.ItunesScraper$ITunesThread

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.