/*
 ============================================================================
 Author	    : Dmitry Moskalchuk
 Version	: 1.5
 Copyright  : Copyright (C) 2008 Rhomobile. All rights reserved.

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ============================================================================
 */
package com.rhomobile.rhodes;

import java.util.Vector;

import com.rhomobile.rhodes.mainview.MainView;
import com.rhomobile.rhodes.mainview.SimpleMainView;
import com.rhomobile.rhodes.mainview.TabbedMainView;

public class NativeBar {
	
	private static final String TAG = "NativeBar";
	
	public static final int TOOLBAR_TYPE = 0;
	public static final int TABBAR_TYPE = 1;
	public static final int NOBAR_TYPE = 2;
	
	private static class CreateTask implements Runnable {
		
		private int type;
		private Vector<Object> params;
		
		public CreateTask(int t, Vector<Object> p) {
			type = t;
			params = p;
		}

		public void run() {
			Rhodes r = RhodesInstance.getInstance();
			MainView v = null;
			
			switch (type) {
			case NOBAR_TYPE:
				v = new SimpleMainView();
				break;
			case TOOLBAR_TYPE:
				v = new SimpleMainView(params);
				break;
			case TABBAR_TYPE:
				v = new TabbedMainView(params);
				break;
			default:
				Logger.E(TAG, "Unknown bar type passed: " + type);
			}
			
			if (v != null)
				r.setMainView(v);
		}
		
	};
	
	private static class SwitchTabTask implements Runnable {
		
		private int index;
		
		public SwitchTabTask(int i) {
			index = i;
		}
		
		public void run() {
			RhodesInstance.getInstance().getMainView().switchTab(index);
		}
	};
	
	private static void reportFail(String name, Exception e) {
		Logger.E(TAG, "Call of \"" + name + "\" failed: " + e.getMessage());
	}

	public static void create(int type, Vector<Object> params) {
		try {
			Rhodes.performOnUiThread(new CreateTask(type, params), false);
		}
		catch (Exception e) {
			reportFail("create", e);
		}
	}
	
	public static void remove() {
		try {
			Rhodes.performOnUiThread(new CreateTask(NOBAR_TYPE, null), false);
		}
		catch (Exception e) {
			reportFail("remove", e);
		}
	}
	
	public static void switchTab(int index) {
		try {
			Rhodes.performOnUiThread(new SwitchTabTask(index), false);
		}
		catch (Exception e) {
			reportFail("switchTab", e);
		}
	}
	
}

@@ -1515,32 +1515,42 @@
 ic void run() %7B%0A
+%09%09%09try %7B%0A%09
 %09%09%09Rhodes r = Rh
@@ -1580,16 +1580,17 @@
 e();%0A%09%09%09
+%09
 MainView
@@ -1603,20 +1603,22 @@
 ull;%0A%09%09%09
+%09
 %0A%09%09%09
+%09
 switch (
@@ -1625,16 +1625,17 @@
 type) %7B%0A
+%09
 %09%09%09case 
@@ -1638,32 +1638,33 @@
 ase NOBAR_TYPE:%0A
+%09
 %09%09%09%09v = new Simp
@@ -1677,31 +1677,33 @@
 View();%0A%09%09%09%09
+%09
 break;%0A
+%09
 %09%09%09case TOOL
@@ -1704,32 +1704,33 @@
 e TOOLBAR_TYPE:%0A
+%09
 %09%09%09%09v = new Simp
@@ -1753,23 +1753,25 @@
 s);%0A%09%09%09%09
+%09
 break;%0A
+%09
 %09%09%09case 
@@ -1779,24 +1779,25 @@
 ABBAR_TYPE:%0A
+%09
 %09%09%09%09v = new 
@@ -1824,23 +1824,25 @@
 s);%0A%09%09%09%09
+%09
 break;%0A
+%09
 %09%09%09defau
@@ -1845,16 +1845,17 @@
 efault:%0A
+%09
 %09%09%09%09Logg
@@ -1908,14 +1908,17 @@
 %0A%09%09%09
+%09
 %7D%0A%09%09%09
-%0A
+%09%0A%09
 %09%09%09i
@@ -1935,16 +1935,17 @@
 ll)%0A%09%09%09%09
+%09
 r.setMai
@@ -1954,16 +1954,84 @@
 iew(v);%0A
+%09%09%09%7D%0A%09%09%09catch (Exception e) %7B%0A%09%09%09%09reportFail(%22CreateTask%22, e);%0A%09%09%09%7D%0A
 %09%09%7D%0A%09%09%0A%09

Fix issue #3374570 - Android: Native bar test - toolbar fails