1 package net.sf.openrocket.android.rocket;
\r
4 import net.sf.openrocket.R;
\r
5 import net.sf.openrocket.android.ActivityHelpers;
\r
6 import net.sf.openrocket.android.CurrentRocketHolder;
\r
7 import net.sf.openrocket.android.simulation.SimulationChart;
\r
8 import net.sf.openrocket.android.simulation.SimulationViewActivity;
\r
9 import net.sf.openrocket.android.simulation.SimulationViewFragment;
\r
10 import net.sf.openrocket.android.simulation.Simulations;
\r
11 import net.sf.openrocket.android.util.AndroidLogWrapper;
\r
12 import net.sf.openrocket.document.OpenRocketDocument;
\r
13 import net.sf.openrocket.document.Simulation;
\r
14 import android.app.AlertDialog;
\r
15 import android.content.DialogInterface;
\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, OpenRocketSaverFragment.OnOpenRocketFileSaved
\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 final static String LOAD_AFTER_SAVE = "net.sf.openrocket.android.loadAfterSave";
\r
42 private boolean loadAfterSave = false;
\r
45 protected void onCreate(Bundle savedInstanceState) {
\r
46 super.onCreate(savedInstanceState);
\r
48 // If the application sleeps for a long time, the CurrentRocketHolder might get cleaned
\r
49 // up. When this happens, we cannot restore this state, so instead we just
\r
51 OpenRocketDocument rocDoc = CurrentRocketHolder.getCurrentRocket().getRocketDocument();
\r
52 if ( rocDoc == null ) {
\r
53 AndroidLogWrapper.d(OpenRocketViewer.class, "No document - go home");
\r
54 ActivityHelpers.goHome(this);
\r
58 if (savedInstanceState != null ) {
\r
59 loadAfterSave = savedInstanceState.getBoolean(LOAD_AFTER_SAVE);
\r
61 setTitle(rocDoc.getRocket().getName());
\r
62 getSupportActionBar().setHomeButtonEnabled(true);
\r
64 setContentView(R.layout.openrocketviewer);
\r
65 ViewPager viewPager = (ViewPager)findViewById(R.id.pager);
\r
66 viewPagerAdapter = new OpenRocketViewerPagerAdapter( this.getSupportFragmentManager() );
\r
67 viewPager.setAdapter( viewPagerAdapter );
\r
71 protected void onPause() {
\r
72 CurrentRocketHolder.getCurrentRocket().setHandler(null);
\r
77 protected void onResume() {
\r
78 RocketChangedEventHandler handler = new RocketChangedEventHandler();
\r
79 CurrentRocketHolder.getCurrentRocket().setHandler(handler);
\r
84 protected void onSaveInstanceState(Bundle outState) {
\r
85 super.onSaveInstanceState(outState);
\r
86 outState.putBoolean(LOAD_AFTER_SAVE, loadAfterSave);
\r
90 public boolean onCreateOptionsMenu(Menu menu) {
\r
91 MenuInflater inflater = getSupportMenuInflater();
\r
92 inflater.inflate(R.menu.rocket_viewer_option_menu, menu);
\r
93 MenuItem saveAction = menu.findItem(R.id.menu_save);
\r
94 if ( CurrentRocketHolder.getCurrentRocket().isModified() ) {
\r
95 saveAction.setVisible(true);
\r
96 saveAction.setShowAsAction( MenuItem.SHOW_AS_ACTION_ALWAYS );
\r
98 saveAction.setVisible(false);
\r
99 saveAction.setShowAsAction( MenuItem.SHOW_AS_ACTION_NEVER );
\r
105 public boolean onMenuItemSelected(int featureId, MenuItem item) {
\r
106 AndroidLogWrapper.d(OpenRocketViewer.class,"onMenuItemSelected" + item.getItemId());
\r
107 switch(item.getItemId()) {
\r
108 case R.id.menu_load:
\r
109 if ( CurrentRocketHolder.getCurrentRocket().isModified() ) {
\r
110 AlertDialog.Builder builder = new AlertDialog.Builder(this);
\r
111 builder.setMessage(R.string.loadWarnUnsaved);
\r
112 builder.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
\r
114 public void onClick(DialogInterface dialog, int which) {
\r
118 builder.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
\r
120 public void onClick(DialogInterface dialog, int which) {
\r
121 OpenRocketViewer.this.loadAfterSave = true;
\r
122 getSupportFragmentManager().beginTransaction()
\r
123 .add( OpenRocketSaverFragment.newInstance(), "saver")
\r
124 .commitAllowingStateLoss();
\r
127 builder.create().show();
\r
132 case R.id.menu_save:
\r
133 getSupportFragmentManager().beginTransaction()
\r
134 .add( OpenRocketSaverFragment.newInstance(), "saver")
\r
135 .commitAllowingStateLoss();
\r
137 case android.R.id.home:
\r
138 ActivityHelpers.goHome(this);
\r
140 case R.id.motor_list_menu_option:
\r
141 ActivityHelpers.browseMotors(this);
\r
143 case R.id.preference_menu_option:
\r
144 ActivityHelpers.startPreferences(this);
\r
146 case R.id.menu_about:
\r
147 ActivityHelpers.showAbout(this);
\r
150 return super.onMenuItemSelected(featureId, item);
\r
154 public void onSimulationSelected(int simulationId) {
\r
156 Simulation sim = CurrentRocketHolder.getCurrentRocket().getRocketDocument().getSimulation(simulationId);
\r
157 // Check if there is data for this simulation.
\r
158 if ( sim.getSimulatedData() == null || sim.getSimulatedData().getBranchCount() == 0 ) {
\r
159 AlertDialog.Builder builder = new AlertDialog.Builder(this);
\r
160 builder.setMessage("The selected simulation does not have saved data.");
\r
161 builder.setCancelable(true);
\r
166 View sidepane = findViewById(R.id.sidepane);
\r
167 if ( /* if multi pane */ sidepane != null ) {
\r
168 SimulationChart chart = new SimulationChart(simulationId);
\r
170 Fragment graph = SimulationViewFragment.newInstance(chart);
\r
172 FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
\r
173 ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
\r
174 // probably only want to update back stack for first time.
\r
175 ft.addToBackStack("simulationplot");
\r
176 ft.replace(R.id.sidepane, graph);
\r
182 Intent i = new Intent(this, SimulationViewActivity.class);
\r
183 i.putExtra("Simulation",simulationId);
\r
189 public void onOpenRocketFileSaved(Boolean result) {
\r
190 invalidateOptionsMenu();
\r
191 if ( loadAfterSave ) {
\r
192 loadAfterSave = false;
\r
197 private class RocketChangedEventHandler extends net.sf.openrocket.android.RocketChangedEventHandler {
\r
200 protected void doSimsChanged() {
\r
201 invalidateOptionsMenu();
\r
202 Simulations sims = (Simulations) viewPagerAdapter.getFragmentAtPos(SIMS_POS);
\r
203 if ( sims != null ) {
\r
204 sims.refreshSimulationList();
\r
209 protected void doMotorConfigsChanged() {
\r
210 invalidateOptionsMenu();
\r
211 Configurations configs = (Configurations) viewPagerAdapter.getFragmentAtPos(CONFIGS_POS);
\r
212 if ( configs != null ) {
\r
213 configs.refreshConfigsList();
\r
220 private class OpenRocketViewerPagerAdapter extends FragmentPagerAdapter {
\r
222 public OpenRocketViewerPagerAdapter( FragmentManager fm ) {
\r
226 public int getCount() {
\r
230 public Fragment getItem( int position ) {
\r
231 switch (position) {
\r
233 return new Overview();
\r
234 case COMPONENT_POS:
\r
235 return new Component();
\r
237 return new Simulations();
\r
239 return new Configurations();
\r
244 public CharSequence getPageTitle(int position) {
\r
245 switch (position) {
\r
248 case COMPONENT_POS:
\r
249 return "Components";
\r
251 return "Simulations";
\r
253 return "Configurations";
\r
258 public Fragment getFragmentAtPos( int pos ) {
\r
259 String tag = "android:switcher:"+R.id.pager+":"+pos;
\r
260 Fragment f = OpenRocketViewer.this.getSupportFragmentManager().findFragmentByTag(tag);
\r