1 package net.sf.openrocket.android.motor;
\r
3 import net.sf.openrocket.R;
\r
4 import net.sf.openrocket.android.db.DbAdapter;
\r
5 import net.sf.openrocket.android.db.MotorDao;
\r
6 import net.sf.openrocket.android.util.AndroidLogWrapper;
\r
7 import net.sf.openrocket.android.util.PersistentExpandableListFragment;
\r
8 import android.app.Activity;
\r
9 import android.content.Context;
\r
10 import android.content.SharedPreferences;
\r
11 import android.content.res.Resources;
\r
12 import android.database.Cursor;
\r
13 import android.os.Bundle;
\r
14 import android.preference.PreferenceManager;
\r
15 import android.support.v4.app.DialogFragment;
\r
16 import android.support.v4.app.Fragment;
\r
17 import android.support.v4.app.FragmentTransaction;
\r
18 import android.view.ContextMenu;
\r
19 import android.view.ContextMenu.ContextMenuInfo;
\r
20 import android.view.Menu;
\r
21 import android.view.MenuItem;
\r
22 import android.view.View;
\r
23 import android.widget.ExpandableListView;
\r
24 import android.widget.ResourceCursorTreeAdapter;
\r
25 import android.widget.TextView;
\r
29 * TODO - make this work with PersistentExpandableListFragment.
\r
32 public class MotorListFragment extends PersistentExpandableListFragment
\r
33 implements SharedPreferences.OnSharedPreferenceChangeListener
\r
35 public interface OnMotorSelectedListener {
\r
36 public void onMotorSelected( long motorId );
\r
39 public static MotorListFragment newInstance( ) {
\r
40 MotorListFragment frag = new MotorListFragment();
\r
44 private static final int CONTEXTMENU_DELETE = Menu.FIRST+1;
\r
45 private static final int CONTEXTMENU_EDIT = Menu.FIRST+2;
\r
47 private String groupColumnPreferenceKey;
\r
48 private String groupColumn = MotorDao.CASE_INFO;
\r
50 private static final String[] groupColumns = new String[] {
\r
53 MotorDao.IMPULSE_CLASS,
\r
54 MotorDao.MANUFACTURER
\r
57 private DbAdapter mDbHelper;
\r
59 private OnMotorSelectedListener motorSelectedListener;
\r
61 public void setMotorSelectedListener(
\r
62 OnMotorSelectedListener motorSelectedListener) {
\r
63 this.motorSelectedListener = motorSelectedListener;
\r
66 public class MotorHierarchicalListAdapter extends ResourceCursorTreeAdapter
\r
69 // Note that the constructor does not take a Cursor. This is done to avoid querying the
\r
70 // database on the main thread.
\r
71 public MotorHierarchicalListAdapter(Context context, Cursor cursor, int groupLayout,
\r
74 super(context, cursor, groupLayout, childLayout);
\r
78 protected Cursor getChildrenCursor(Cursor arg0) {
\r
79 AndroidLogWrapper.d(MotorListFragment.class,"getChildrenCursor");
\r
80 String group = arg0.getString(arg0.getColumnIndex(groupColumn));
\r
81 Cursor c = mDbHelper.getMotorDao().fetchAllInGroups(groupColumn,group);
\r
86 public long getGroupId(int groupPosition) {
\r
87 return groupPosition;
\r
91 * @see android.widget.CursorTreeAdapter#bindChildView(android.view.View, android.content.Context, android.database.Cursor, boolean)
\r
94 protected void bindChildView(View arg0, Context arg1, Cursor arg2,
\r
97 TextView manu = (TextView) arg0.findViewById(R.id.motorChildManu);
\r
98 manu.setText( arg2.getString(arg2.getColumnIndex(MotorDao.MANUFACTURER)));
\r
100 TextView desig = (TextView) arg0.findViewById(R.id.motorChildName);
\r
101 desig.setText( arg2.getString(arg2.getColumnIndex(MotorDao.DESIGNATION)));
\r
103 TextView delays = (TextView) arg0.findViewById(R.id.motorChildDelays);
\r
104 delays.setText( arg2.getString(arg2.getColumnIndex(MotorDao.DELAYS)));
\r
106 TextView totImpulse = (TextView) arg0.findViewById(R.id.motorChildImpulse);
\r
107 totImpulse.setText( arg2.getString(arg2.getColumnIndex(MotorDao.TOTAL_IMPULSE)));
\r
111 * @see android.widget.CursorTreeAdapter#bindGroupView(android.view.View, android.content.Context, android.database.Cursor, boolean)
\r
114 protected void bindGroupView(View view, Context context, Cursor cursor,
\r
115 boolean isExpanded) {
\r
116 TextView v = (TextView) view.findViewById(R.id.motorGroup);
\r
117 if ( MotorDao.DIAMETER.equals(groupColumn)) {
\r
118 double d = cursor.getDouble( cursor.getColumnIndex(groupColumn));
\r
119 v.setText( String.valueOf(Math.round(d * 1000.0)) );
\r
121 v.setText( cursor.getString( cursor.getColumnIndex(groupColumn)));
\r
128 public void onSharedPreferenceChanged(SharedPreferences arg0, String arg1) {
\r
129 if ( groupColumnPreferenceKey.equals(arg1) ) {
\r
130 setGroupColumnFromPreferences(arg0);
\r
136 public void onViewCreated(View view, Bundle savedInstanceState) {
\r
137 super.onViewCreated(view, savedInstanceState);
\r
138 // TODO - need some error text but unfortunately doing this, makes the layout funky
\r
140 //setEmptyText("No motors in database - download them from Thrustcurve using the Option Menu");
\r
144 registerForContextMenu(getExpandableListView());
\r
149 public void onAttach(Activity activity) {
\r
150 super.onAttach(activity);
\r
151 mDbHelper = new DbAdapter(getActivity());
\r
154 Resources resources = this.getResources();
\r
155 groupColumnPreferenceKey = resources.getString(R.string.PreferenceMotorBrowserGroupingOption);
\r
156 SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(getActivity());
\r
158 setGroupColumnFromPreferences(pref);
\r
160 pref.registerOnSharedPreferenceChangeListener(this);
\r
162 if ( activity instanceof OnMotorSelectedListener ) {
\r
163 motorSelectedListener = (OnMotorSelectedListener) activity;
\r
169 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
\r
170 menu.setHeaderTitle("Motor Operations");
\r
171 menu.add(Menu.NONE,CONTEXTMENU_DELETE,CONTEXTMENU_DELETE,"Delete");
\r
172 menu.add(Menu.NONE,CONTEXTMENU_EDIT,CONTEXTMENU_EDIT,"Edit");
\r
173 super.onCreateContextMenu(menu, v, menuInfo);
\r
177 public boolean onContextItemSelected(MenuItem item) {
\r
178 ExpandableListView.ExpandableListContextMenuInfo info = (ExpandableListView.ExpandableListContextMenuInfo) item.getMenuInfo();
\r
179 long motorId = info.id;
\r
180 AndroidLogWrapper.d(MotorListFragment.class,"ContextMenu: " + motorId);
\r
181 switch(item.getItemId()) {
\r
182 case CONTEXTMENU_DELETE:
\r
183 mDbHelper.getMotorDao().deleteMotor(motorId);
\r
186 case CONTEXTMENU_EDIT:
\r
187 // DialogFragment.show() will take care of adding the fragment
\r
188 // in a transaction. We also want to remove any currently showing
\r
189 // dialog, so make our own transaction and take care of that here.
\r
190 FragmentTransaction ft = getFragmentManager().beginTransaction();
\r
191 Fragment prev = getFragmentManager().findFragmentByTag("dialog");
\r
192 if (prev != null) {
\r
195 ft.addToBackStack(null);
\r
197 // Create and show the dialog.
\r
198 DialogFragment newFragment = MotorDetailsFragment.newInstance(motorId);
\r
199 newFragment.show(ft, "dialog");
\r
202 return super.onContextItemSelected(item);
\r
206 public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
\r
207 super.onChildClick(parent, v, groupPosition, childPosition, id);
\r
208 if( motorSelectedListener != null ) {
\r
209 motorSelectedListener.onMotorSelected(id);
\r
215 public void onDetach() {
\r
217 SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(getActivity());
\r
218 pref.unregisterOnSharedPreferenceChangeListener(this);
\r
223 private void setGroupColumnFromPreferences( SharedPreferences prefs ) {
\r
224 String indexStr = prefs.getString(groupColumnPreferenceKey, "1");
\r
226 //Dirty hack, you can't use integer-array in ListPreferences
\r
228 index = Integer.parseInt(indexStr);
\r
229 } catch ( Exception e ) {
\r
232 if ( index >= groupColumns.length ) {
\r
235 groupColumn = groupColumns[index];
\r
239 public void refreshData() {
\r
240 Cursor motorCursor = mDbHelper.getMotorDao().fetchGroups(groupColumn);
\r
241 MotorHierarchicalListAdapter mAdapter = new MotorHierarchicalListAdapter(
\r
244 R.layout.motor_list_group,
\r
245 R.layout.motor_list_child);
\r
246 setListAdapter(mAdapter);
\r
247 onContentChanged();
\r