X-Git-Url: https://git.gag.com/?a=blobdiff_plain;f=android%2Fsrc%2Fnet%2Fsf%2Fopenrocket%2Fandroid%2Frocket%2FOpenRocketViewer.java;h=58bb82759458577b5f3ea3069234c930d85358ba;hb=548d35633c5d863ecb9ecda43a1915e3a0559fda;hp=8584c763dae4bc95598384331ee08a000caa765c;hpb=1cb2403fcc6c8ffc57e38c367ce3ab0736871afb;p=debian%2Fopenrocket diff --git a/android/src/net/sf/openrocket/android/rocket/OpenRocketViewer.java b/android/src/net/sf/openrocket/android/rocket/OpenRocketViewer.java index 8584c763..58bb8275 100644 --- a/android/src/net/sf/openrocket/android/rocket/OpenRocketViewer.java +++ b/android/src/net/sf/openrocket/android/rocket/OpenRocketViewer.java @@ -1,94 +1,86 @@ package net.sf.openrocket.android.rocket; +import java.io.IOException; + import net.sf.openrocket.R; import net.sf.openrocket.android.ActivityHelpers; -import net.sf.openrocket.android.Application; +import net.sf.openrocket.android.CurrentRocketHolder; import net.sf.openrocket.android.simulation.SimulationChart; -import net.sf.openrocket.android.simulation.SimulationFragment; import net.sf.openrocket.android.simulation.SimulationViewActivity; +import net.sf.openrocket.android.simulation.SimulationViewFragment; import net.sf.openrocket.android.util.AndroidLogWrapper; -import net.sf.openrocket.android.util.TabsAdapter; import net.sf.openrocket.document.OpenRocketDocument; import net.sf.openrocket.document.Simulation; -import net.sf.openrocket.rocketcomponent.Configuration; +import android.app.AlertDialog; import android.content.Intent; -import android.content.SharedPreferences; import android.os.Bundle; -import android.preference.PreferenceManager; import android.support.v4.app.Fragment; -import android.support.v4.app.FragmentActivity; +import android.support.v4.app.FragmentManager; +import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.app.FragmentTransaction; import android.support.v4.view.ViewPager; -import android.view.Menu; -import android.view.MenuInflater; -import android.view.MenuItem; import android.view.View; -import android.widget.TabHost; -public class OpenRocketViewer extends FragmentActivity -implements SharedPreferences.OnSharedPreferenceChangeListener, -Simulations.OnSimulationSelectedListener -{ +import com.actionbarsherlock.view.Menu; +import com.actionbarsherlock.view.MenuInflater; +import com.actionbarsherlock.view.MenuItem; - OpenRocketDocument rocketDocument; - Configuration rocketConfiguration; +public class OpenRocketViewer extends OpenRocketLoaderActivity +implements Simulations.OnSimulationSelectedListener +{ - private Application app; + private final static int OVERVIEW_POS = 0; + private final static int COMPONENT_POS = 1; + private final static int SIMS_POS = 2; + private final static int CONFIGS_POS = 3; + private final static int TABSIZE = 4; - TabHost mTabHost; - ViewPager mViewPager; - TabsAdapter mTabsAdapter; + private OpenRocketViewerPagerAdapter viewPagerAdapter; + + private MenuItem saveAction; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - app = (Application) this.getApplication(); + // If the application sleeps for a long time, the CurrentRocketHolder might get cleaned + // up. When this happens, we cannot restore this state, so instead we just + // go home. + OpenRocketDocument rocDoc = CurrentRocketHolder.getCurrentRocket().getRocketDocument(); + if ( rocDoc == null ) { + AndroidLogWrapper.d(OpenRocketViewer.class, "No document - go home"); + ActivityHelpers.goHome(this); + finish(); + return; + } + setTitle(rocDoc.getRocket().getName()); + getSupportActionBar().setHomeButtonEnabled(true); setContentView(R.layout.openrocketviewer); - - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); - prefs.registerOnSharedPreferenceChangeListener(this); - - mTabHost = (TabHost)findViewById(android.R.id.tabhost); - mTabHost.setup(); - - mViewPager = (ViewPager)findViewById(R.id.pager); - - mTabsAdapter = new TabsAdapter(this, mTabHost, mViewPager); - - mTabsAdapter.addTab(mTabHost.newTabSpec("overview").setIndicator("Overview"), - Overview.class, null); - mTabsAdapter.addTab(mTabHost.newTabSpec("components").setIndicator("Components"), - Component.class, null); - mTabsAdapter.addTab(mTabHost.newTabSpec("simulations").setIndicator("Simulations"), - Simulations.class, null); - - if (savedInstanceState != null) { - mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab")); - } + ViewPager viewPager = (ViewPager)findViewById(R.id.pager); + viewPagerAdapter = new OpenRocketViewerPagerAdapter( this.getSupportFragmentManager() ); + viewPager.setAdapter( viewPagerAdapter ); } @Override - protected void onSaveInstanceState(Bundle outState) { - super.onSaveInstanceState(outState); - outState.putString("tab", mTabHost.getCurrentTabTag()); + protected void onPause() { + CurrentRocketHolder.getCurrentRocket().setHandler(null); + super.onPause(); } - /* (non-Javadoc) - * @see android.content.SharedPreferences.OnSharedPreferenceChangeListener#onSharedPreferenceChanged(android.content.SharedPreferences, java.lang.String) - */ @Override - public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { - // just in case the user changed the units, we redraw. - // TODO = updateContents(); redraw all children.. + protected void onResume() { + RocketChangedEventHandler handler = new RocketChangedEventHandler(); + CurrentRocketHolder.getCurrentRocket().setHandler(handler); + super.onResume(); } @Override public boolean onCreateOptionsMenu(Menu menu) { - MenuInflater inflater = getMenuInflater(); + MenuInflater inflater = getSupportMenuInflater(); inflater.inflate(R.menu.rocket_viewer_option_menu, menu); + saveAction = menu.findItem(R.id.menu_save); return true; } @@ -96,24 +88,54 @@ Simulations.OnSimulationSelectedListener public boolean onMenuItemSelected(int featureId, MenuItem item) { AndroidLogWrapper.d(OpenRocketViewer.class,"onMenuItemSelected" + item.getItemId()); switch(item.getItemId()) { + case R.id.menu_load: + // FIXME - Might want to prompt for save here. + pickOrkFiles(); + return true; + case R.id.menu_save: + // FIXME - Probably want to open a dialog here. + try { + CurrentRocketHolder.getCurrentRocket().saveOpenRocketDocument(); + saveAction.setVisible(false); + invalidateOptionsMenu(); + } catch ( IOException iex ) { + AndroidLogWrapper.d(OpenRocketViewer.class, iex.getMessage()); + } + return true; + case android.R.id.home: + ActivityHelpers.goHome(this); + return true; case R.id.motor_list_menu_option: ActivityHelpers.browseMotors(this); return true; case R.id.preference_menu_option: ActivityHelpers.startPreferences(this); return true; + case R.id.menu_about: + ActivityHelpers.showAbout(this); + return true; } return super.onMenuItemSelected(featureId, item); } @Override public void onSimulationSelected(int simulationId) { + + Simulation sim = CurrentRocketHolder.getCurrentRocket().getRocketDocument().getSimulation(simulationId); + // Check if there is data for this simulation. + if ( sim.getSimulatedData() == null || sim.getSimulatedData().getBranchCount() == 0 ) { + AlertDialog.Builder builder = new AlertDialog.Builder(this); + builder.setMessage("The selected simulation does not have saved data."); + builder.setCancelable(true); + builder.show(); + return; + } + View sidepane = findViewById(R.id.sidepane); if ( /* if multi pane */ sidepane != null ) { - Simulation sim = app.getRocketDocument().getSimulation(simulationId); SimulationChart chart = new SimulationChart(simulationId); - Fragment graph = SimulationFragment.newInstance(chart); + Fragment graph = SimulationViewFragment.newInstance(chart); FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); @@ -131,4 +153,78 @@ Simulations.OnSimulationSelectedListener } } + private class RocketChangedEventHandler extends net.sf.openrocket.android.RocketChangedEventHandler { + + @Override + protected void doSimsChanged() { + if (saveAction != null ) { + saveAction.setVisible(true); + invalidateOptionsMenu(); + } + Simulations sims = (Simulations) viewPagerAdapter.getFragmentAtPos(SIMS_POS); + if ( sims != null ) { + sims.refreshSimulationList(); + } + } + + @Override + protected void doMotorConfigsChanged() { + if (saveAction != null ) { + saveAction.setVisible(true); + invalidateOptionsMenu(); + } + Configurations configs = (Configurations) viewPagerAdapter.getFragmentAtPos(CONFIGS_POS); + if ( configs != null ) { + configs.refreshConfigsList(); + } + } + + }; + + + private class OpenRocketViewerPagerAdapter extends FragmentPagerAdapter { + + public OpenRocketViewerPagerAdapter( FragmentManager fm ) { + super(fm); + } + @Override + public int getCount() { + return TABSIZE; + } + @Override + public Fragment getItem( int position ) { + switch (position) { + case OVERVIEW_POS: + return new Overview(); + case COMPONENT_POS: + return new Component(); + case SIMS_POS: + return new Simulations(); + case CONFIGS_POS: + return new Configurations(); + } + return null; + } + @Override + public CharSequence getPageTitle(int position) { + switch (position) { + case OVERVIEW_POS: + return "Overview"; + case COMPONENT_POS: + return "Components"; + case SIMS_POS: + return "Simulations"; + case CONFIGS_POS: + return "Configurations"; + } + return null; + } + + public Fragment getFragmentAtPos( int pos ) { + String tag = "android:switcher:"+R.id.pager+":"+pos; + Fragment f = OpenRocketViewer.this.getSupportFragmentManager().findFragmentByTag(tag); + return f; + } + } + }