/*
 * Copyright (C) 2011 0xlab - http://0xlab.org/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Authored by Kan-Ru Chen <kanru@0xlab.org>
 */

package org.zeroxlab.aster;

import org.zeroxlab.aster.cmds.*;

import com.android.chimpchat.ChimpChat;
import com.android.chimpchat.core.IChimpImage;
import com.android.chimpchat.core.IChimpDevice;

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

public class AsterMainPanel extends JPanel {

    private ImageView mImageView;

    private ChimpChat mChimpChat;
    private IChimpDevice mDevice;
    private AsterCommand[] mCmds;

    public AsterMainPanel() {
	GridBagLayout gridbag = new GridBagLayout();
	GridBagConstraints c = new GridBagConstraints();

	setLayout(gridbag);
	c.fill = GridBagConstraints.BOTH;
	c.gridx = 0;
	c.gridy = 0;
	c.gridwidth = 3;
	c.gridheight = 3;
	c.weightx = 0.5;
	c.weighty = 0.5;
	mImageView = new ImageView();
	add(mImageView, c);

        mCmds = new AsterCommand[14];
        generateCmds(mCmds);
        JList myList = new JList(mCmds);
	myList.setCellRenderer(new AsterCommandCellRenderer());
	myList.setFixedCellHeight(50);
	myList.setFixedCellWidth(200);
	JScrollPane scrollPane = new JScrollPane();
	scrollPane.getViewport().setView(myList);
	scrollPane.setMinimumSize(new Dimension(250, 100));

        c.gridx = 4;
        c.gridy = 0;
        c.gridwidth = 1;
        c.gridheight = 3;
        c.weightx = 0;
        c.weighty = 0;
        add(scrollPane, c);


	Map<String, String> options = new TreeMap<String, String>();
	options.put("backend", "adb");
	mChimpChat = ChimpChat.getInstance(options);
	mDevice = mChimpChat.waitForConnection();
	updateScreen();
	timer.start();
    }

    private void generateCmds(AsterCommand cmds[]) {
        for (int i = 0; i < cmds.length; i++) {
	    cmds[i] = new Click(new Point(i, 2));
        }
    }

    private final javax.swing.Timer timer =
	new javax.swing.Timer(100, new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            updateScreen();
        }
    });

    private void updateScreen() {
        IChimpImage snapshot = mDevice.takeSnapshot();
        mImageView.setImage(snapshot.createBufferedImage());
	mImageView.repaint(mImageView.getBounds());
    }
}

@@ -1126,16 +1126,57 @@
 mCmds;%0A%0A
+    private UpdateScreen mUpdateScreen;%0A%0A
     publ
@@ -2231,31 +2231,118 @@
 ();%0A
-%09updateScreen();%0A%09timer
+%0A        mUpdateScreen = new UpdateScreen();%0A        Thread thread = new Thread(mUpdateScreen);%0A        thread
 .sta
@@ -2523,188 +2523,513 @@
     
-private final javax.swing.Timer timer =%0A%09new javax.swing.Timer(100, new ActionListener() %7B%0A        public void actionPerformed(ActionEvent e) %7B%0A            u
+class UpdateScreen implements Runnable %7B%0A%0A        private boolean mKeepWalking = true;%0A%0A        public void finish() %7B%0A            mKeepWalking = false;%0A        %7D%0A%0A        public void run() %7B%0A            while(mKeepWalking) %7B%0A                updateScreen();%0A                try %7B%0A                    Thread.sleep(100);%0A                %7D catch (InterruptedException e) %7B%0A                    System.err.println(%22U
 pdate
+ 
 Screen
-();%0A
+ thread is interrupted%22);%0A                    e.printStackTrace();%0A       
         
 %7D%0A  
@@ -3028,23 +3028,44 @@
     
+ 
 %7D%0A    
-%7D);%0A%0A
+        %7D%0A        %7D%0A%0A   
     
+ 
 priv
@@ -3086,24 +3086,28 @@
 eScreen() %7B%0A
+    
         IChi
@@ -3157,16 +3157,20 @@
         
+   
+ 
 mImageVi
@@ -3214,17 +3214,28 @@
 age());%0A
-%09
+            
 mImageVi
@@ -3266,20 +3266,30 @@
 tBounds());%0A
+        %7D%0A
     %7D%0A%7D%0A

Update Snapshot in another thread