|
- package de.erich.railcontrol;
-
- import android.app.Activity;
-
- import android.app.ActionBar;
- import android.app.Fragment;
- import android.app.FragmentManager;
- import android.app.FragmentTransaction;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.view.ViewGroup;
- import android.support.v4.widget.DrawerLayout;
- import android.widget.Button;
- import android.widget.CheckBox;
- import android.widget.Switch;
- import android.widget.Toast;
-
- import java.io.IOException;
-
-
- public class MyActivity extends Activity
- implements NavigationDrawerFragment.NavigationDrawerCallbacks, View.OnClickListener {
-
-
- public Konfiguration fragm_config;
- public FragmentManager fragManager;
- public FragmentTransaction fragTransaction;
- public static zug E_10 = new zug(10, "E 12", 28);
- public zug E_12 = new zug(200, "Hans", 28);
- public static Lok1 currentFrag;
- public Lok1 [] loks = new Lok1[10];
- public static zug [] zuege = new zug[10];
- public zug BR_86 = new zug(86, "BR 86" , 28);
- static String defaultDataDir;
- static boolean power;
-
- /**
- * Fragment managing the behaviors, interactions and presentation of the navigation drawer.
- */
- private NavigationDrawerFragment mNavigationDrawerFragment;
-
- /**
- * Used to store the last screen title. For use in {@link #restoreActionBar()}.
- */
- private CharSequence mTitle;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- System.out.println("OnCreate");
- defaultDataDir = "/data/data/" + getPackageName();
-
- try {
- MainCall maincall = new MainCall(defaultDataDir);
- } catch (IOException e) {
- e.printStackTrace();
- }
-
- /*zuege[0] = E_10;
- zuege[1] = E_12;
- zuege[2] = BR_86; */
-
- int zuegeLength = 0;
- for (int i = 0; i < MyActivity.zuege.length; i++) {
- if(!(MyActivity.zuege[i] == null)) {
- zuegeLength++;
- }
- }
- NavigationDrawerFragment.titles = new String[zuegeLength + 3];
- System.out.println("Anzahl Züge: " + zuegeLength);
- NavigationDrawerFragment.titles[0] = getString(R.string.title_section1);
- NavigationDrawerFragment.titles[1] = getString(R.string.title_section2);
- NavigationDrawerFragment.titles[2] = "Neue Lok";
- for (int i = 3; i < zuege.length; i++) {
- if(!(MyActivity.zuege[i - 3] == null)) {
- NavigationDrawerFragment.titles[i] = zuege[i - 3].name;
- System.out.println("Created: " + i);
- }
- else {
- System.out.println("Something is null.");
- }
- }
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_my);
-
- mNavigationDrawerFragment = (NavigationDrawerFragment)
- getFragmentManager().findFragmentById(R.id.navigation_drawer);
- mTitle = getTitle();
-
- // Set up the drawer.
- mNavigationDrawerFragment.setUp(
- R.id.navigation_drawer,
- (DrawerLayout) findViewById(R.id.drawer_layout));
-
- fragm_config = (Konfiguration) Fragment.instantiate(this, Konfiguration.class.getName(), null);
- }
-
- @Override
- public void onNavigationDrawerItemSelected(int position) {
- // update the main content by replacing fragments
- FragmentManager fragmentManager = getFragmentManager();
- fragmentManager.beginTransaction()
- .replace(R.id.container, PlaceholderFragment.newInstance(position + 1))
- .commit();
- System.out.println(position);
- }
-
- public void onSectionAttached(int number) {
- System.out.println("onSectionAttached: "+number);
-
- switch (number) {
- case 1:
- mTitle = getString(R.string.title_section1);
- System.out.println("Section Attached");
- break;
- case 2:
- mTitle = getString(R.string.title_section2);
- fragManager = getFragmentManager();
- fragTransaction = fragManager.beginTransaction();
- fragTransaction.replace(R.id.container,fragm_config);
- fragTransaction.commit();
- break;
- case 3:
- mTitle = "Lok hinzufügen";
- AddLok addlok = new AddLok();
- fragManager = getFragmentManager();
- fragTransaction = fragManager.beginTransaction();
- fragTransaction.replace(R.id.container, addlok);
- fragTransaction.commit();
- System.out.println("Lok hinzufügen - Fragment");
- break;
-
- default:
- System.out.println("Section: " + number);
- if(loks[number-4] == null) {
- Lok1 lok1 = new Lok1();
- loks[number-4] = lok1;
- lok1.initFragment(zuege[number-4]);
- }
- Lok1 lok1 = loks[number-4];
- fragManager = getFragmentManager();
- fragTransaction = fragManager.beginTransaction();
- fragTransaction.replace(R.id.container, lok1);
- fragTransaction.commit();
- mTitle = lok1.fragsLok.name;
- //lok1.initFragment(E_10);
- currentFrag = lok1;
- break;
- }
- }
-
- public void restoreActionBar() {
- ActionBar actionBar = getActionBar();
- actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
- actionBar.setDisplayShowTitleEnabled(true);
- actionBar.setTitle(mTitle);
- }
-
-
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- System.out.println("OnCreateOptionsMenu");
-
- /* try {
- MySAXApp.initReader();
- } catch (Exception e) {
- e.printStackTrace();
- } */
- if (!mNavigationDrawerFragment.isDrawerOpen()) {
- // Only show items in the action bar relevant to this screen
- // if the drawer is not showing. Otherwise, let the drawer
- // decide what to show in the action bar.
- getMenuInflater().inflate(R.menu.my, menu);
- restoreActionBar();
- return true;
- }
-
- return super.onCreateOptionsMenu(menu);
- }
-
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- if (id == R.id.action_settings) {
- if(currentFrag == null) {
- System.out.println("Erst Lok auswählen");
- Toast.makeText(this, "Erst Lok auswählen", Toast.LENGTH_SHORT).show();
- }
- else {
- mTitle = "Lok ändern";
- EditLok editlok = new EditLok();
- fragManager = getFragmentManager();
- fragTransaction = fragManager.beginTransaction();
- fragTransaction.replace(R.id.container, editlok);
- fragTransaction.commit();
- }
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
-
- /*@Override
- public void onDestroy() {
- System.out.println("OnDestroy");
- try {
- MainCall.lConf.printConfiguration(defaultDataDir + "/configuration.txt");
- } catch (IOException e) {
- System.out.println("Error: " + e);
- System.exit(0);
- }
- }*/
-
- public void onCheckboxClicked(View view) {
- int status = 0;
- boolean checked = ((CheckBox) view).isChecked();
- if(checked) {
- status = 1;
- }
- else if(!checked) {
- status = 0;
- }
- if(view == Lok1.funktion_1) {
- currentFrag.fragsLok.setFunction(1, status);
- }
- else if(view == Lok1.funktion_2) {
- currentFrag.fragsLok.setFunction(2, status);
- }
- else if(view == Lok1.funktion_3) {
- currentFrag.fragsLok.setFunction(3, status);
- }
- else if(view == Lok1.funktion_4) {
- currentFrag.fragsLok.setFunction(4, status);
- }
-
- }
-
- @Override
- public void onClick(View view) {
- if(view == EditLok.btn_editLok_save) {
- int number = 100;
- String name = EditLok.et_editLok_name.getText().toString();
- int adresse;
- int maxspeed;
- try {
- adresse = Integer.parseInt(EditLok.et_editLok_adresse.getText().toString());
- maxspeed = Integer.parseInt(EditLok.et_editLok_maxspeed.getText().toString());
- } catch (NumberFormatException e) {
- adresse = 0;
- maxspeed = 0;
- }
- for (int i = 0;i < MyActivity.zuege.length; i++) {
- if (MyActivity.zuege[i].equals(MyActivity.currentFrag.fragsLok)) {
- number = i;
- i = MyActivity.zuege.length;
- System.out.println("Runned");
- }
- }
- if(!(name.equals("") || adresse == 0 || maxspeed == 0)) {
- System.out.println("Setzen der Werte, Number :"+number);
- MyActivity.currentFrag.fragsLok.name = name;
- MyActivity.currentFrag.fragsLok.adresse = adresse;
- MyActivity.currentFrag.fragsLok.maxspeed = maxspeed;
- if(!(number == 100)) {
- System.out.println("Nicht 100");
- MyActivity.zuege[number].name = name;
- MyActivity.zuege[number].adresse = adresse;
- MyActivity.zuege[number].maxspeed = maxspeed;
- System.out.println("Gespeichert");
- Toast.makeText(EditLok.btn_editLok_save.getContext(), "Lok gespeichert", Toast.LENGTH_SHORT).show();
- }
- else {
- System.out.println("Error occurred, EditLok");
- Toast.makeText(EditLok.btn_editLok_save.getContext(), "Es ist ein Fehler aufgetreten :(", Toast.LENGTH_SHORT).show();
-
- }
- }
- else {
- System.out.println("Fehlende Eingaben");
- Toast.makeText(EditLok.btn_editLok_save.getContext(), "Fehlende/Fehlerhafte Eingaben", Toast.LENGTH_SHORT).show();
- }
-
- }
- else if (view == EditLok.btn_editLok_delete) {
- int number = 100;
- for (int i = 0;i < MyActivity.zuege.length; i++) {
- if (MyActivity.zuege[i].equals(MyActivity.currentFrag.fragsLok)) {
- number = i;
- i = MyActivity.zuege.length;
- }
- }
- MyActivity.zuege[number] = null;
- /*for (int i = number; i < MyActivity.zuege.length; i++) {
- if(i == number) {
- System.out.println("Lok selber hihihi");
- }
- else {
- MyActivity.zuege[i - 1] = MyActivity.zuege[i];
- }
- }*/
- System.out.println(MyActivity.zuege[number]);
- Toast.makeText(EditLok.btn_editLok_save.getContext(), "Lok gelöscht", Toast.LENGTH_SHORT).show();
-
- }
- }
-
-
- /**
- * A placeholder fragment containing a simple view.
- */
- public static class PlaceholderFragment extends Fragment {
-
- public static Button btn_connect;
- public static Button btn_quit;
- public static Switch ip_selector;
- /**
- * The fragment argument representing the section number for this
- * fragment.
- */
- private static final String ARG_SECTION_NUMBER = "section_number";
-
- /**
- * Returns a new instance of this fragment for the given section
- * number.
- */
- public static PlaceholderFragment newInstance(int sectionNumber) {
- PlaceholderFragment fragment = new PlaceholderFragment();
- Bundle args = new Bundle();
- args.putInt(ARG_SECTION_NUMBER, sectionNumber);
- fragment.setArguments(args);
- return fragment;
- }
-
- public PlaceholderFragment() {
- }
-
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- View rootView = inflater.inflate(R.layout.fragment_my, container, false);
- this.btn_quit = (Button) rootView.findViewById(R.id.btn_quit);
- this.btn_connect = (Button) rootView.findViewById(R.id.btn_connect);
- System.out.println("Buttons gesetzt");
- System.out.println(this.btn_connect + ", " + this.btn_quit);
- btn_quit.setOnClickListener(new btn_listeners());
- btn_connect.setOnClickListener(new btn_listeners());
- ip_selector = (Switch) rootView.findViewById(R.id.select_ip);
-
- return rootView;
- }
-
- @Override
- public void onAttach(Activity activity) {
- super.onAttach(activity);
- ((MyActivity) activity).onSectionAttached(
- getArguments().getInt(ARG_SECTION_NUMBER));
- }
- }
-
-
-
- }
|