@Override public boolean onContextItemSelected(MenuItem item) { SpreadsheetCell cell; TableDisplayActivity activity = (TableDisplayActivity) getActivity(); switch (item.getItemId()) { case MENU_ITEM_ID_HISTORY_IN: cell = spreadsheetTable.getSpreadsheetCell(activity, this.mLastDataCellMenued); openCollectionView(cell); return true; // case MENU_ITEM_ID_EDIT_CELL: // cell = spreadsheetTable.getSpreadsheetCell(activity, this.mLastDataCellMenued); // openCellEditDialog(cell); // return true; case MENU_ITEM_ID_DELETE_ROW: cell = spreadsheetTable.getSpreadsheetCell(activity, this.mLastDataCellMenued); AlertDialog confirmDeleteAlert; // Prompt an alert box final String rowId = cell.row.getDataByKey(DataTableColumns.ID); AlertDialog.Builder alert = new AlertDialog.Builder(activity); alert .setTitle(getString(R.string.confirm_delete_row)) .setMessage(getString(R.string.are_you_sure_delete_row, rowId)); // OK Action => delete the row alert.setPositiveButton( getString(R.string.ok), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { TableDisplayActivity activity = (TableDisplayActivity) getActivity(); try { deleteRow(rowId); init(); } catch (ActionNotAuthorizedException e) { WebLogger.getLogger(activity.getAppName()).printStackTrace(e); WebLogger.getLogger(activity.getAppName()) .e(TAG, "Not authorized for action while " + "accessing database"); Toast.makeText( activity, "Not authorized for action while accessing database", Toast.LENGTH_LONG) .show(); } catch (ServicesAvailabilityException e) { WebLogger.getLogger(activity.getAppName()).printStackTrace(e); WebLogger.getLogger(activity.getAppName()) .e(TAG, "Error while accessing database"); Toast.makeText(activity, "Error while accessing database", Toast.LENGTH_LONG) .show(); } } }); // Cancel Action alert.setNegativeButton( getString(R.string.cancel), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { // Canceled. } }); // show the dialog confirmDeleteAlert = alert.create(); confirmDeleteAlert.show(); return true; case MENU_ITEM_ID_EDIT_ROW: cell = spreadsheetTable.getSpreadsheetCell(activity, this.mLastDataCellMenued); // It is possible that a custom form has been defined for this table. // We will get the strings we need, and then set the parameter object. try { ActivityUtil.editRow( activity, activity.getAppName(), activity.getTableId(), activity.getColumnDefinitions(), cell.row); } catch (ServicesAvailabilityException e) { WebLogger.getLogger(activity.getAppName()).printStackTrace(e); WebLogger.getLogger(activity.getAppName()).e(TAG, "Error while accessing database"); Toast.makeText(activity, "Error while accessing database", Toast.LENGTH_LONG).show(); return true; } // launch ODK Collect return true; case MENU_ITEM_ID_OPEN_JOIN_TABLE: cell = spreadsheetTable.getSpreadsheetCell(getActivity(), this.mLastDataCellMenued); ColumnDefinition cd = spreadsheetTable.getColumnByElementKey(cell.elementKey); // Get the JoinColumn. ArrayList<JoinColumn> joinColumns; DbHandle db = null; try { db = Tables.getInstance().getDatabase().openDatabase(getAppName()); joinColumns = ColumnUtil.get() .getJoins( Tables.getInstance(), getAppName(), db, getTableId(), cd.getElementKey()); } catch (ServicesAvailabilityException e) { WebLogger.getLogger(activity.getAppName()).printStackTrace(e); WebLogger.getLogger(activity.getAppName()).e(TAG, "Error while accessing database"); Toast.makeText(activity, "Error while accessing database", Toast.LENGTH_LONG).show(); return true; } finally { if (db != null) { try { Tables.getInstance().getDatabase().closeDatabase(getAppName(), db); } catch (ServicesAvailabilityException e) { WebLogger.getLogger(activity.getAppName()).printStackTrace(e); WebLogger.getLogger(activity.getAppName()).e(TAG, "Error closing database"); Toast.makeText(activity, "Error closing database", Toast.LENGTH_LONG).show(); } } } AlertDialog.Builder badJoinDialog; // TODO should check for valid table properties and // column properties here. or rather valid ids and keys. if (joinColumns == null || joinColumns.size() == 0) { badJoinDialog = new AlertDialog.Builder(this.getActivity()); badJoinDialog.setTitle("Bad Join"); badJoinDialog.setMessage("A join column has not been " + "set in Column Properties."); badJoinDialog.create().show(); WebLogger.getLogger(getAppName()) .e( TAG, "cp.getJoins was null but open join table " + "was requested for cp: " + cd.getElementKey()); } else if (joinColumns.size() != 1) { badJoinDialog = new AlertDialog.Builder(this.getActivity()); badJoinDialog.setTitle("Bad Join"); badJoinDialog.setMessage( "Multiple join associations have been " + "set in Column Properties."); badJoinDialog.create().show(); WebLogger.getLogger(getAppName()) .e( TAG, "cp.getJoins has multiple joins " + "(missing code is needed to handle this) for cp: " + cd.getElementKey()); } else { JoinColumn joinColumn = joinColumns.get(0); if (joinColumn.getTableId().equals(JoinColumn.DEFAULT_NOT_SET_VALUE) || joinColumn.getElementKey().equals(JoinColumn.DEFAULT_NOT_SET_VALUE)) { badJoinDialog = new AlertDialog.Builder(this.getActivity()); badJoinDialog.setTitle("Bad Join"); badJoinDialog.setMessage("Both a table and column " + "must be set."); badJoinDialog.create().show(); WebLogger.getLogger(getAppName()) .e( TAG, "Bad elementKey or tableId in open join " + "table. tableId: " + joinColumn.getTableId() + " elementKey: " + joinColumn.getElementKey()); } else { String tableId = joinColumn.getTableId(); String elementKey = joinColumn.getElementKey(); String joinedColTableDisplayName; db = null; try { db = Tables.getInstance().getDatabase().openDatabase(getAppName()); joinedColTableDisplayName = ColumnUtil.get() .getLocalizedDisplayName( Tables.getInstance(), getAppName(), db, tableId, elementKey); } catch (ServicesAvailabilityException e) { WebLogger.getLogger(activity.getAppName()).printStackTrace(e); WebLogger.getLogger(activity.getAppName()).e(TAG, "Error while accessing database"); Toast.makeText(activity, "Error while accessing database", Toast.LENGTH_LONG).show(); } finally { if (db != null) { try { Tables.getInstance().getDatabase().closeDatabase(getAppName(), db); } catch (ServicesAvailabilityException e) { WebLogger.getLogger(activity.getAppName()).printStackTrace(e); WebLogger.getLogger(activity.getAppName()).e(TAG, "Error closing database"); Toast.makeText(activity, "Error closing database", Toast.LENGTH_LONG).show(); } } } // I would prefer this kind of query to be set in another // object, but alas, it looks like atm it is hardcoded. Intent intent = new Intent(this.getActivity(), TableDisplayActivity.class); Bundle extras = new Bundle(); IntentUtil.addAppNameToBundle(extras, getAppName()); // TODO: pass the correct view type. IntentUtil.addFragmentViewTypeToBundle(extras, ViewFragmentType.SPREADSHEET); intent.putExtras(extras); getActivity().startActivityForResult(intent, Constants.RequestCodes.LAUNCH_VIEW); // Controller.launchTableActivity(context, joinedTable, // joinedTable.getDefaultViewType()); } } return true; case MENU_ITEM_ID_SET_COLUMN_AS_GROUP_BY: addGroupByColumn( spreadsheetTable.getColumnByElementKey(this.mLastHeaderCellMenued.elementKey)); init(); return true; case MENU_ITEM_ID_UNSET_COLUMN_AS_GROUP_BY: removeGroupByColumn( spreadsheetTable.getColumnByElementKey(this.mLastHeaderCellMenued.elementKey)); init(); return true; case MENU_ITEM_ID_SET_COLUMN_AS_SORT: setColumnAsSort( spreadsheetTable.getColumnByElementKey(this.mLastHeaderCellMenued.elementKey)); init(); return true; case MENU_ITEM_ID_UNSET_COLUMN_AS_SORT: setColumnAsSort(null); init(); return true; case MENU_ITEM_ID_SET_AS_INDEXED_COL: setColumnAsIndexedCol( spreadsheetTable.getColumnByElementKey(this.mLastHeaderCellMenued.elementKey)); init(); return true; case MENU_ITEM_ID_UNSET_AS_INDEXED_COL: setColumnAsIndexedCol(null); init(); return true; case MENU_ITEM_ID_EDIT_COLUMN_COLOR_RULES: String elementKey = this.mLastHeaderCellMenued.elementKey; ActivityUtil.launchTablePreferenceActivityToEditColumnColorRules( this.getActivity(), getAppName(), getTableId(), elementKey); default: WebLogger.getLogger(getAppName()) .e(TAG, "unrecognized menu item selected: " + item.getItemId()); return super.onContextItemSelected(item); } }
/** * Return true if group bys are currently being displayed. * * @return */ private boolean hasGroupBys() { SQLQueryStruct queryStruct = IntentUtil.getSQLQueryStructFromBundle(this.getActivity().getIntent().getExtras()); return queryStruct.groupBy != null; }
private void openCollectionView(SpreadsheetCell cell) { Bundle intentExtras = this.getActivity().getIntent().getExtras(); String sqlWhereClause = intentExtras.getString(IntentKeys.SQL_WHERE); String[] sqlSelectionArgs = null; if (sqlWhereClause != null && sqlWhereClause.length() != 0) { sqlSelectionArgs = intentExtras.getStringArray(IntentKeys.SQL_SELECTION_ARGS); } String[] sqlGroupBy = intentExtras.getStringArray(IntentKeys.SQL_GROUP_BY_ARGS); String sqlHaving = null; if (sqlGroupBy != null && sqlGroupBy.length != 0) { sqlHaving = intentExtras.getString(IntentKeys.SQL_HAVING); } String sqlOrderByElementKey = intentExtras.getString(IntentKeys.SQL_ORDER_BY_ELEMENT_KEY); String sqlOrderByDirection = null; if (sqlOrderByElementKey != null && sqlOrderByElementKey.length() != 0) { sqlOrderByDirection = intentExtras.getString(IntentKeys.SQL_ORDER_BY_DIRECTION); if (sqlOrderByDirection == null || sqlOrderByDirection.length() == 0) { sqlOrderByDirection = "ASC"; } } if (sqlGroupBy != null && sqlGroupBy.length != 0) { StringBuilder s = new StringBuilder(); if (sqlWhereClause != null && sqlWhereClause.length() != 0) { s.append("(").append(sqlWhereClause).append(") AND "); } List<String> newSelectionArgs = new ArrayList<String>(); if (sqlSelectionArgs != null) { newSelectionArgs.addAll(Arrays.asList(sqlSelectionArgs)); } boolean first = true; for (String groupByColumn : sqlGroupBy) { if (!first) { s.append(", "); } first = false; String value = cell.row.getDataByKey(groupByColumn); if (value == null) { s.append(groupByColumn).append(" IS NULL"); } else { s.append(groupByColumn).append("=?"); newSelectionArgs.add(value); } } sqlWhereClause = s.toString(); sqlSelectionArgs = newSelectionArgs.toArray(new String[newSelectionArgs.size()]); } Intent intent = new Intent(this.getActivity(), TableDisplayActivity.class); Bundle extras = new Bundle(); IntentUtil.addSQLKeysToBundle( extras, sqlWhereClause, sqlSelectionArgs, sqlGroupBy, sqlHaving, sqlOrderByElementKey, sqlOrderByDirection); IntentUtil.addFragmentViewTypeToBundle(extras, ViewFragmentType.SPREADSHEET); IntentUtil.addAppNameToBundle(extras, this.getAppName()); intent.putExtras(extras); this.startActivityForResult(intent, Constants.RequestCodes.LAUNCH_VIEW); }