6dd754d24e6d689fafb32a09ee47831ad167b7cd
[debian/openrocket] / android / src / net / sf / openrocket / android / motor / MotorHierarchicalBrowser.java
1 package net.sf.openrocket.android.motor;\r
2 \r
3 import net.sf.openrocket.R;\r
4 import net.sf.openrocket.android.PreferencesActivity;\r
5 import net.sf.openrocket.android.db.DbAdapter;\r
6 import net.sf.openrocket.android.db.MotorDao;\r
7 import net.sf.openrocket.android.thrustcurve.TCQueryActivity;\r
8 import android.content.Context;\r
9 import android.content.Intent;\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.util.Log;\r
16 import android.view.ContextMenu;\r
17 import android.view.ContextMenu.ContextMenuInfo;\r
18 import android.view.Menu;\r
19 import android.view.MenuInflater;\r
20 import android.view.MenuItem;\r
21 import android.view.View;\r
22 import android.widget.CursorTreeAdapter;\r
23 import android.widget.ExpandableListView;\r
24 import android.widget.ResourceCursorTreeAdapter;\r
25 import android.widget.TextView;\r
26 \r
27 \r
28 public class MotorHierarchicalBrowser\r
29 extends PersistentExpandableListActivity\r
30 implements SharedPreferences.OnSharedPreferenceChangeListener\r
31 {\r
32         private static final String TAG = "MotorHierarchicalBrowser";\r
33 \r
34         private static final int ACTIVITY_DOWNLOAD=0;\r
35 \r
36         private static final int CONTEXTMENU_DELETE = Menu.FIRST+1;\r
37 \r
38         private String groupColumnPreferenceKey;\r
39         private String groupColumn = MotorDao.CASE_INFO;\r
40 \r
41         private static final String[] groupColumns = new String[] {\r
42                 MotorDao.CASE_INFO,\r
43                 MotorDao.DIAMETER,\r
44                 MotorDao.IMPULSE_CLASS,\r
45                 MotorDao.MANUFACTURER\r
46         };\r
47 \r
48         private CursorTreeAdapter mAdapter;\r
49 \r
50         private DbAdapter mDbHelper;\r
51 \r
52         public class MotorHierarchicalListAdapter extends ResourceCursorTreeAdapter\r
53         {\r
54 \r
55                 // Note that the constructor does not take a Cursor. This is done to avoid querying the \r
56                 // database on the main thread.\r
57                 public MotorHierarchicalListAdapter(Context context, Cursor cursor, int groupLayout,\r
58                                 int childLayout ) {\r
59 \r
60                         super(context, cursor, groupLayout, childLayout);\r
61                 }\r
62 \r
63                 @Override\r
64                 protected Cursor getChildrenCursor(Cursor arg0) {\r
65                         Log.d(TAG,"getChildrenCursor");\r
66                         String group = arg0.getString(arg0.getColumnIndex(groupColumn));\r
67                         Log.d(TAG,"  for: "+ groupColumn + " = " + group);\r
68                         Cursor c = mDbHelper.getMotorDao().fetchAllInGroups(groupColumn,group);\r
69                         Log.d(TAG,"  got cursor");\r
70                         startManagingCursor(c);\r
71                         return c;\r
72                 }\r
73 \r
74                 @Override\r
75                 public long getGroupId(int groupPosition) {\r
76                         return groupPosition;\r
77                 }\r
78 \r
79                 //new String[] { MotorDao.MANUFACTURER, MotorDao.DESIGNATION, MotorDao.CASE_INFO, MotorDao.TOTAL_IMPULSE }, // Number for child layouts\r
80                 //new int[] { R.id.motorChildManu, R.id.motorChildName, R.id.motorChildDelays, R.id.motorChildImpulse }\r
81 \r
82                 /* (non-Javadoc)\r
83                  * @see android.widget.CursorTreeAdapter#bindChildView(android.view.View, android.content.Context, android.database.Cursor, boolean)\r
84                  */\r
85                 @Override\r
86                 protected void bindChildView(View arg0, Context arg1, Cursor arg2,\r
87                                 boolean arg3) {\r
88                         \r
89                         TextView manu = (TextView) arg0.findViewById(R.id.motorChildManu);\r
90                         manu.setText( arg2.getString(arg2.getColumnIndex(MotorDao.MANUFACTURER)));\r
91                         \r
92                         TextView desig = (TextView) arg0.findViewById(R.id.motorChildName);\r
93                         desig.setText( arg2.getString(arg2.getColumnIndex(MotorDao.DESIGNATION)));\r
94                         \r
95                         TextView delays = (TextView) arg0.findViewById(R.id.motorChildDelays);\r
96                         delays.setText( MotorDao.extractPrettyDelayString( arg2 ));\r
97                         \r
98                         TextView totImpulse = (TextView) arg0.findViewById(R.id.motorChildImpulse);\r
99                         totImpulse.setText( arg2.getString(arg2.getColumnIndex(MotorDao.TOTAL_IMPULSE)));\r
100                 }\r
101 \r
102                 /* (non-Javadoc)\r
103                  * @see android.widget.CursorTreeAdapter#bindGroupView(android.view.View, android.content.Context, android.database.Cursor, boolean)\r
104                  */\r
105                 @Override\r
106                 protected void bindGroupView(View view, Context context, Cursor cursor,\r
107                                 boolean isExpanded) {\r
108                         TextView v = (TextView) view.findViewById(R.id.motorGroup);\r
109                         if ( MotorDao.DIAMETER.equals(groupColumn)) {\r
110                                 double d = cursor.getDouble( cursor.getColumnIndex(groupColumn));\r
111                                 v.setText( String.valueOf(Math.round(d * 1000.0)) );\r
112                         } else {\r
113                                 v.setText( cursor.getString( cursor.getColumnIndex(groupColumn)));\r
114                         }\r
115                 }\r
116                 \r
117                 \r
118 \r
119         }\r
120 \r
121         @Override\r
122         public void onSharedPreferenceChanged(SharedPreferences arg0, String arg1) {\r
123                 if ( groupColumnPreferenceKey.equals(arg1) ) {\r
124                         setGroupColumnFromPreferences(arg0);\r
125                         refreshData();\r
126                 }\r
127         }\r
128 \r
129         @Override\r
130         public void onCreate(Bundle savedInstanceState) {\r
131                 super.onCreate(savedInstanceState);\r
132 \r
133                 mDbHelper = new DbAdapter(this);\r
134                 mDbHelper.open();\r
135 \r
136                 Resources resources = this.getResources();\r
137                 groupColumnPreferenceKey = resources.getString(R.string.PreferenceMotorBrowserGroupingOption);\r
138                 SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);\r
139 \r
140                 setGroupColumnFromPreferences(pref);\r
141 \r
142                 pref.registerOnSharedPreferenceChangeListener(this);\r
143 \r
144                 refreshData();\r
145 \r
146                 registerForContextMenu(getExpandableListView());\r
147 \r
148         }\r
149 \r
150         @Override\r
151         public boolean onCreateOptionsMenu(Menu menu) {\r
152         MenuInflater inflater = getMenuInflater();\r
153         inflater.inflate(R.menu.motor_browser_option_menu, menu);\r
154                 return true;\r
155         }\r
156 \r
157         @Override\r
158         public boolean onMenuItemSelected(int featureId, MenuItem item) {\r
159                 Log.d(TAG,"onMenuItemSelected" + item.getItemId());\r
160                 switch(item.getItemId()) {\r
161                 case R.id.download_from_thrustcurve_menu_option:\r
162                         tcDownload();\r
163                         return true;\r
164                 case R.id.preference_menu_option:\r
165                         Intent intent = new Intent().setClass(this, PreferencesActivity.class);\r
166                         this.startActivity(intent);\r
167                         return true;\r
168                 }\r
169                 return super.onMenuItemSelected(featureId, item);\r
170         }\r
171 \r
172         @Override\r
173         public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {\r
174                 Log.d(TAG,"onCreateContextMenu " + menuInfo);\r
175                 Log.d(TAG, "v.getId() = " + v.getId());\r
176                 Log.d(TAG, "motorListView = " + R.id.motorListView);\r
177                 //      if (v.getId() == R.id.motorListView) {\r
178                 ExpandableListView.ExpandableListContextMenuInfo info = (ExpandableListView.ExpandableListContextMenuInfo) menuInfo;\r
179                 menu.setHeaderTitle("context menu");\r
180                 menu.add(Menu.NONE,CONTEXTMENU_DELETE,CONTEXTMENU_DELETE,"Delete");\r
181                 //      }\r
182                 super.onCreateContextMenu(menu, v, menuInfo);\r
183         }\r
184 \r
185         @Override\r
186         public boolean onContextItemSelected(MenuItem item) {\r
187                 ExpandableListView.ExpandableListContextMenuInfo info = (ExpandableListView.ExpandableListContextMenuInfo) item.getMenuInfo();\r
188                 long motorId = info.id;\r
189                 Log.d(TAG,"ContextMenu: " + motorId);\r
190                 switch(item.getItemId()) {\r
191                 case CONTEXTMENU_DELETE:\r
192                         mDbHelper.getMotorDao().deleteMotor(motorId);\r
193                         refreshData();\r
194                         return true;\r
195                 }\r
196                 return super.onContextItemSelected(item);\r
197         }\r
198 \r
199         @Override\r
200         protected void onActivityResult(int requestCode, int resultCode, Intent intent) {\r
201                 super.onActivityResult(requestCode, resultCode, intent);\r
202                 refreshData();\r
203         }\r
204 \r
205 \r
206         @Override\r
207         public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {\r
208                 super.onChildClick(parent, v, groupPosition, childPosition, id);\r
209                 //Intent i = new Intent(this, BurnPlotActivity.class);\r
210                 Intent i = new Intent(this,MotorDetails.class);\r
211                 i.putExtra("Motor", id);\r
212                 startActivity(i);\r
213                 return true;\r
214         }\r
215 \r
216         @Override\r
217         protected void onDestroy() {\r
218                 super.onDestroy();\r
219                 \r
220                 SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);\r
221                 pref.unregisterOnSharedPreferenceChangeListener(this);\r
222 \r
223                 // Null out the group cursor. This will cause the group cursor and all of the child cursors\r
224                 // to be closed.\r
225                 mAdapter.changeCursor(null);\r
226                 mAdapter = null;\r
227 \r
228                 mDbHelper.close();\r
229         }\r
230 \r
231         private void tcDownload() {\r
232                 Intent i = new Intent(this, TCQueryActivity.class);\r
233                 startActivityForResult(i, ACTIVITY_DOWNLOAD);\r
234         }\r
235 \r
236         private void setGroupColumnFromPreferences( SharedPreferences prefs ) {\r
237                 String indexStr = prefs.getString(groupColumnPreferenceKey, "1");\r
238                 int index;\r
239                 //Dirty hack, you can't use integer-array in ListPreferences\r
240                 try {\r
241                         index = Integer.parseInt(indexStr);\r
242                 } catch ( Exception e ) {\r
243                         index = 1;\r
244                 }\r
245                 if ( index >= groupColumns.length ) {\r
246                         index = 1;\r
247                 }\r
248                 groupColumn = groupColumns[index];\r
249 \r
250         }\r
251         private void refreshData() {\r
252                 if (mAdapter != null ) {\r
253                         mAdapter.changeCursor(null);\r
254                 }\r
255                 Cursor motorCursor = mDbHelper.getMotorDao().fetchGroups(groupColumn);\r
256                 startManagingCursor(motorCursor);\r
257                 // Set up our adapter\r
258                 mAdapter = new MotorHierarchicalListAdapter( \r
259                                 this,\r
260                                 motorCursor,\r
261                                 R.layout.motor_list_group,\r
262                                 R.layout.motor_list_child);\r
263                 setListAdapter(mAdapter);\r
264         }\r
265 }\r