Java tutorial
/*************************************************************************************** * Copyright (c) 2012 Norbert Nagold <norbert.nagold@gmail.com> * * * * This program is free software; you can redistribute it and/or modify it under * * the terms of the GNU General Public License as published by the Free Software * * Foundation; either version 3 of the License, or (at your option) any later * * version. * * * * This program is distributed in the hope that it will be useful, but WITHOUT ANY * * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A * * PARTICULAR PURPOSE. See the GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License along with * * this program. If not, see <http://www.gnu.org/licenses/>. * ****************************************************************************************/ package com.ichi2.anki; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.content.DialogInterface; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentTransaction; import android.text.Editable; import android.text.TextWatcher; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup.LayoutParams; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.ToggleButton; import com.ichi2.anki.CardEditor.JSONNameComparator; import com.ichi2.anki2.R; import com.ichi2.libanki.Collection; import com.ichi2.themes.StyledDialog; import com.ichi2.themes.Themes; public class CramDeckFragment extends Fragment { private EditText mCramDeckName; private LinearLayout mDecks; private TextView mDeckLabel; private EditText mSteps; // private EditText mOrder; private EditText mLimit; private EditText mInterval; private Button mCreateButton; private Button mCancelButton; private Collection mCol; private JSONObject mDeck; private boolean mFragmented; private StyledDialog mDecksDialog; public static CramDeckFragment newInstance(int index) { CramDeckFragment f = new CramDeckFragment(); Bundle args = new Bundle(); args.putInt("index", index); f.setArguments(args); return f; } public int getShownIndex() { return getArguments().getInt("index", 0); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if (container == null) { // Currently in a layout without a container, so no // reason to create our view. return null; } mFragmented = getActivity().getClass() != CramDeckActivity.class; View main = inflater.inflate(R.layout.cram_deck, null); mCramDeckName = (EditText) main.findViewById(R.id.cram_deck_name); mCramDeckName.addTextChangedListener(new TextWatcher() { @Override public void afterTextChanged(Editable arg0) { if (mCramDeckName.getText().length() == 0) { mCreateButton.setEnabled(false); } else { mCreateButton.setEnabled(true); } } @Override public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { } @Override public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { } }); mDecks = (LinearLayout) main.findViewById(R.id.cram_deck_decks_button); mDeckLabel = (TextView) main.findViewById(R.id.cram_deck_decks_text); mDecks.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { showDecksDialog(); } }); mSteps = (EditText) main.findViewById(R.id.cram_deck_steps); // mOrder mLimit = (EditText) main.findViewById(R.id.cram_deck_limit); mInterval = (EditText) main.findViewById(R.id.cram_deck_interval); mCreateButton = (Button) main.findViewById(R.id.cram_deck_create); mCreateButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { try { mDeck.put("steps", DeckOptions.getDelays(mSteps.getText().toString())); // mDeck.put("search", ) // mDeck.put("order", value); mDeck.put("limit", Integer.parseInt(mLimit.getText().toString())); mDeck.put("fmult", Integer.parseInt(mLimit.getText().toString()) / 100.0); } catch (JSONException e) { throw new RuntimeException(e); } closeCramDeckAdder(); } }); mCancelButton = (Button) main.findViewById(R.id.cram_deck_cancel); mCancelButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { closeCramDeckAdder(); } }); mCol = Collection.currentCollection(); if (mCol == null) { return null; } mDeck = mCol.getDecks().current(); try { if (mDeck.getInt("dyn") == 0) { String currentDeckName = mDeck.getString("name"); ArrayList<String> names = mCol.getDecks().allNames(); int n = 1; String cramDeckName = "Cram 1"; while (names.contains(cramDeckName)) { n++; cramDeckName = "Cram " + n; } mCol.getDecks().newDyn(cramDeckName); mDeck = mCol.getDecks().current(); mDeck.put("search", "\'deck:" + currentDeckName + "\'"); } mCramDeckName.setText(mDeck.getString("name")); mDeckLabel.setText(mDeck.getString("search")); mSteps.setText(DeckOptions.getDelays(mDeck.getJSONArray("delays"))); // TODO: set mOrder mLimit.setText(Integer.toString(mDeck.getInt("limit"))); mInterval.setText(Integer.toString((int) (mDeck.getDouble("fmult") * 100))); } catch (JSONException e) { throw new RuntimeException(e); } if (!mFragmented) { main.setBackgroundResource(R.drawable.white_wallpaper); } Themes.showThemedToast(this.getActivity(), "not yet implemented", false); return main; } private void closeCramDeckAdder() { if (mFragmented) { FragmentTransaction ft = getFragmentManager().beginTransaction(); ft.remove(CramDeckFragment.this); ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); ft.commit(); } else { ((CramDeckActivity) getActivity()).closeCramDeckAdder(); } } private void showDecksDialog() { if (mDecksDialog == null) { ArrayList<CharSequence> dialogDeckItems = new ArrayList<CharSequence>(); // Use this array to know which ID is associated with each // Item(name) final ArrayList<Long> dialogDeckIds = new ArrayList<Long>(); ArrayList<JSONObject> decks = mCol.getDecks().all(); String currentName; try { currentName = mDeck.getString("name"); for (int i = 0; i < decks.size(); i++) { JSONObject d = decks.get(i); if (d.getString("name").equals(currentName)) { decks.remove(i); } } } catch (JSONException e1) { throw new RuntimeException(e1); } Collections.sort(decks, new JSONNameComparator()); StyledDialog.Builder builder = new StyledDialog.Builder(getActivity()); builder.setTitle(R.string.deck); for (JSONObject d : decks) { try { dialogDeckItems.add(DeckPicker.readableDeckName(d.getString("name").split("::"))); dialogDeckIds.add(d.getLong("id")); } catch (JSONException e) { throw new RuntimeException(e); } } // Convert to Array String[] items = new String[dialogDeckItems.size()]; dialogDeckItems.toArray(items); builder.setItems(items, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int item) { try { mDeckLabel.setText( "\'deck:" + mCol.getDecks().get(dialogDeckIds.get(item)).getString("name") + "\'"); } catch (JSONException e) { throw new RuntimeException(e); } } }); mDecksDialog = builder.create(); } mDecksDialog.show(); } public class JSONNameComparator implements Comparator<JSONObject> { @Override public int compare(JSONObject lhs, JSONObject rhs) { String[] o1; String[] o2; try { o1 = lhs.getString("name").split("::"); o2 = rhs.getString("name").split("::"); } catch (JSONException e) { throw new RuntimeException(e); } for (int i = 0; i < Math.min(o1.length, o2.length); i++) { int result = o1[i].compareToIgnoreCase(o2[i]); if (result != 0) { return result; } } if (o1.length < o2.length) { return -1; } else if (o1.length > o2.length) { return 1; } else { return 0; } } } }