1 package net.sf.openrocket.android.rocket;
\r
4 import java.io.IOException;
\r
6 import net.sf.openrocket.R;
\r
7 import net.sf.openrocket.android.ActivityHelpers;
\r
8 import net.sf.openrocket.android.CurrentRocketHolder;
\r
9 import net.sf.openrocket.android.simulation.SimulationChart;
\r
10 import net.sf.openrocket.android.simulation.SimulationViewActivity;
\r
11 import net.sf.openrocket.android.simulation.SimulationViewFragment;
\r
12 import net.sf.openrocket.android.util.AndroidLogWrapper;
\r
13 import net.sf.openrocket.document.OpenRocketDocument;
\r
14 import net.sf.openrocket.document.Simulation;
\r
15 import android.app.AlertDialog;
\r
16 import android.content.Intent;
\r
17 import android.os.Bundle;
\r
18 import android.support.v4.app.Fragment;
\r
19 import android.support.v4.app.FragmentManager;
\r
20 import android.support.v4.app.FragmentPagerAdapter;
\r
21 import android.support.v4.app.FragmentTransaction;
\r
22 import android.support.v4.view.ViewPager;
\r
23 import android.view.View;
\r
25 import com.actionbarsherlock.view.Menu;
\r
26 import com.actionbarsherlock.view.MenuInflater;
\r
27 import com.actionbarsherlock.view.MenuItem;
\r
29 public class OpenRocketViewer extends OpenRocketLoaderActivity
\r
30 implements Simulations.OnSimulationSelectedListener
\r
33 private final static int OVERVIEW_POS = 0;
\r
34 private final static int COMPONENT_POS = 1;
\r
35 private final static int SIMS_POS = 2;
\r
36 private final static int CONFIGS_POS = 3;
\r
37 private final static int TABSIZE = 4;
\r
39 private OpenRocketViewerPagerAdapter viewPagerAdapter;
\r
41 private MenuItem saveAction;
\r
44 protected void onCreate(Bundle savedInstanceState) {
\r
45 super.onCreate(savedInstanceState);
\r
47 // If the application sleeps for a long time, the CurrentRocketHolder might get cleaned
\r
48 // up. When this happens, we cannot restore this state, so instead we just
\r
50 OpenRocketDocument rocDoc = CurrentRocketHolder.getCurrentRocket().getRocketDocument();
\r
51 if ( rocDoc == null ) {
\r
52 AndroidLogWrapper.d(OpenRocketViewer.class, "No document - go home");
\r
53 ActivityHelpers.goHome(this);
\r
57 setTitle(rocDoc.getRocket().getName());
\r
58 getSupportActionBar().setHomeButtonEnabled(true);
\r
60 setContentView(R.layout.openrocketviewer);
\r
61 ViewPager viewPager = (ViewPager)findViewById(R.id.pager);
\r
62 viewPagerAdapter = new OpenRocketViewerPagerAdapter( this.getSupportFragmentManager() );
\r
63 viewPager.setAdapter( viewPagerAdapter );
\r
67 protected void onPause() {
\r
68 CurrentRocketHolder.getCurrentRocket().setHandler(null);
\r
73 protected void onResume() {
\r
74 RocketChangedEventHandler handler = new RocketChangedEventHandler();
\r
75 CurrentRocketHolder.getCurrentRocket().setHandler(handler);
\r
80 public boolean onCreateOptionsMenu(Menu menu) {
\r
81 MenuInflater inflater = getSupportMenuInflater();
\r
82 inflater.inflate(R.menu.rocket_viewer_option_menu, menu);
\r
83 saveAction = menu.findItem(R.id.menu_save);
\r
88 public boolean onMenuItemSelected(int featureId, MenuItem item) {
\r
89 AndroidLogWrapper.d(OpenRocketViewer.class,"onMenuItemSelected" + item.getItemId());
\r
90 switch(item.getItemId()) {
\r
91 case R.id.menu_load:
\r
92 // FIXME - Might want to prompt for save here.
\r
95 case R.id.menu_save:
\r
96 // FIXME - Probably want to open a dialog here.
\r
98 CurrentRocketHolder.getCurrentRocket().saveOpenRocketDocument();
\r
99 saveAction.setVisible(false);
\r
100 invalidateOptionsMenu();
\r
101 } catch ( IOException iex ) {
\r
102 AndroidLogWrapper.d(OpenRocketViewer.class, iex.getMessage());
\r
105 case android.R.id.home:
\r
106 ActivityHelpers.goHome(this);
\r
108 case R.id.motor_list_menu_option:
\r
109 ActivityHelpers.browseMotors(this);
\r
111 case R.id.preference_menu_option:
\r
112 ActivityHelpers.startPreferences(this);
\r
114 case R.id.menu_about:
\r
115 ActivityHelpers.showAbout(this);
\r
118 return super.onMenuItemSelected(featureId, item);
\r
122 public void onSimulationSelected(int simulationId) {
\r
124 Simulation sim = CurrentRocketHolder.getCurrentRocket().getRocketDocument().getSimulation(simulationId);
\r
125 // Check if there is data for this simulation.
\r
126 if ( sim.getSimulatedData() == null || sim.getSimulatedData().getBranchCount() == 0 ) {
\r
127 AlertDialog.Builder builder = new AlertDialog.Builder(this);
\r
128 builder.setMessage("The selected simulation does not have saved data.");
\r
129 builder.setCancelable(true);
\r
134 View sidepane = findViewById(R.id.sidepane);
\r
135 if ( /* if multi pane */ sidepane != null ) {
\r
136 SimulationChart chart = new SimulationChart(simulationId);
\r
138 Fragment graph = SimulationViewFragment.newInstance(chart);
\r
140 FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
\r
141 ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
\r
142 // probably only want to update back stack for first time.
\r
143 ft.addToBackStack("simulationplot");
\r
144 ft.replace(R.id.sidepane, graph);
\r
150 Intent i = new Intent(this, SimulationViewActivity.class);
\r
151 i.putExtra("Simulation",simulationId);
\r
156 private class RocketChangedEventHandler extends net.sf.openrocket.android.RocketChangedEventHandler {
\r
159 protected void doSimsChanged() {
\r
160 if (saveAction != null ) {
\r
161 saveAction.setVisible(true);
\r
162 invalidateOptionsMenu();
\r
164 Simulations sims = (Simulations) viewPagerAdapter.getFragmentAtPos(SIMS_POS);
\r
165 if ( sims != null ) {
\r
166 sims.refreshSimulationList();
\r
171 protected void doMotorConfigsChanged() {
\r
172 if (saveAction != null ) {
\r
173 saveAction.setVisible(true);
\r
174 invalidateOptionsMenu();
\r
176 Configurations configs = (Configurations) viewPagerAdapter.getFragmentAtPos(CONFIGS_POS);
\r
177 if ( configs != null ) {
\r
178 configs.refreshConfigsList();
\r
185 private class OpenRocketViewerPagerAdapter extends FragmentPagerAdapter {
\r
187 public OpenRocketViewerPagerAdapter( FragmentManager fm ) {
\r
191 public int getCount() {
\r
195 public Fragment getItem( int position ) {
\r
196 switch (position) {
\r
198 return new Overview();
\r
199 case COMPONENT_POS:
\r
200 return new Component();
\r
202 return new Simulations();
\r
204 return new Configurations();
\r
209 public CharSequence getPageTitle(int position) {
\r
210 switch (position) {
\r
213 case COMPONENT_POS:
\r
214 return "Components";
\r
216 return "Simulations";
\r
218 return "Configurations";
\r
223 public Fragment getFragmentAtPos( int pos ) {
\r
224 String tag = "android:switcher:"+R.id.pager+":"+pos;
\r
225 Fragment f = OpenRocketViewer.this.getSupportFragmentManager().findFragmentByTag(tag);
\r