/*
 * Write by cocobear
 * cocobear.cn@gmail.com
 *  * 
 */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class OpenCD
{
        public static void main(String[] args) {
                ButtonFrame frame = new ButtonFrame();
                        }

}

class ButtonFrame extends JFrame
{
        public ButtonFrame() {
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                setResizable(false);
                setLocationRelativeTo(null);
                setLayout(new BorderLayout());
                setTitle("OpenCD -- by cocobear.infoquot;);
                setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);

                ButtonPanel panel = new ButtonPanel();

                add(panel);
                setVisible(true);
        }
        public static final int DEFAULT_WIDTH = 300;
        public static final int DEFAULT_HEIGHT = 200;

}

class ButtonPanel extends JPanel
{
        public ButtonPanel() {
                int Open = 1;
                int Close = 0;
                int Auto = 2;

                setLayout(new GridBagLayout());
                GridBagConstraints gb = new GridBagConstraints();
                setBackground(Color.lightGray);
                JButton OpenButton = new JButton("OPEN");
                JButton CloseButton = new JButton("CLOSE");
                JButton AutoButton = new JButton("AUTO");


                gb.weightx = 3;
                add(OpenButton,gb);
                add(CloseButton,gb);
                add(AutoButton,gb);

                setVisible(true);
                ButtonAction CloseAction = new ButtonAction(Close);
                ButtonAction OpenAction = new ButtonAction(Open);
                ButtonAction AutoAction = new ButtonAction(Auto);

                CloseButton.addActionListener(CloseAction);
                OpenButton.addActionListener(OpenAction);
                AutoButton.addActionListener(AutoAction);

        }

        private class ButtonAction implements ActionListener {
                int ret;
                public ButtonAction(int i) {
                        ret = i;
                }

                public void actionPerformed(ActionEvent event) {
                        Runtime rn = Runtime.getRuntime();
                        if (ret == 1) {
                                try {
                                        Process ps = rn.exec("eject");
                                        int exitVal = ps.waitFor();
                                        if (exitVal == 1) {
                                                System.out.println("What? eject failed!");
                                        }
                                }catch(Exception e) {
                                        System.out.println("eject not found.\nTry add it into your PATH.");
                                }
                        }
                        if (ret == 0) {
                                try {
                                        Process ps = rn.exec("eject -t");
                                        int exitVal = ps.waitFor();
                                        if (exitVal == 1) {

                                                System.out.println("Oh,sorry,YOUR DEVICE is not supported!");
                                        }
                                }catch(Exception e) {
                                        System.out.println("eject not found or your device is not support by eject");
                                }
                        }
                        if (ret == 2) {
                                try {
                                        Process ps = rn.exec("eject -T");
                                        int exitVal = ps.waitFor();
                                        if (exitVal == 1) {

                                                System.out.println("Oh,sorry,YOUR DEVICE is not supported!");
                                        }
                                }catch(Exception e) {
                                        System.out.println("eject not found.");
                                }
                        }
                }

        }
}