Android Open Source - dexedd Pokemon Text Util






From Project

Back to project page dexedd.

License

The source code is released under:

MIT License

If you think the Android project dexedd listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.nav.dexedd.util;
//w ww .  j av a2s .c  o m
import android.content.Context;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.view.View;
import android.widget.Toast;

import com.nav.dexedd.text.CleanClickableSpan;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * General Pokmon related Text methods.
 *
 * @author Eduardo Naveda
 * @since 0.0.1
 */
public class PokemonTextUtil {

    /**
     * Processable pattern.
     */
    private static final String PROCESSABLE_DEX_TEXT_PATTERN = "\\[(.*?)\\]\\{(.+?):(.+?)\\}";

    /**
     * Processable tag 'type'.
     */
    private static final String PROCESSABLE_TAG_TYPE = "type";

    /* private static final String PROCESSABLE_MECHANIC = "mechanic";
    private static final String PROCESSABLE_MOVE = "move";
    private static final String PROCESSABLE_ITEM = "item"; */ // Unused for now

    /**
     * Formats dex numbers in a visually familiar manner.
     *
     * @param number The dex number to be formatted
     * @return The formatted dex number
     */
    public static String getFormattedDexNumber(Integer number) {
        if (number < 10) {
            return "#00" + number;
        } else {
            if (number < 100) {
                return "#0" + number;
            } else {
                return "#" + number;
            }
        }
    }

    /**
     * Clean formatted text from Veekun's db.
     *
     * @param text Text to be cleaned
     * @return Cleaned text
     */
    public static String cleanDexText(String text) {
        String cleanText = "";
        for (int i = 0; i < text.length(); i++) {
            String character = "" + text.charAt(i);
            if (text.charAt(i) == '\n') {
                if ((i+1) < text.length() && text.charAt(i + 1) == '\n') {
                    character = "\n\n";
                    i++;
                } else {
                    character = " ";
                }
            }
            cleanText = cleanText + character;
        }
        return cleanText;
    }

    /**
     * Processes Pokmon text such as ability entries which use special processable patterns for formatting
     * (such as type, mechanics and moves), ie: [badly poisoned]{mechanic:badly-poisoned}.
     *
     * @param context           Application context
     * @param unprocessedString String to be processed
     * @return Builder with special formatting ready to be set to a TextView
     */
    public static SpannableStringBuilder processPokemonText(final Context context, String unprocessedString) {
        int renderGroup;
        Pattern pattern = Pattern.compile(PROCESSABLE_DEX_TEXT_PATTERN);
        Matcher matcher = pattern.matcher(unprocessedString);
        SpannableStringBuilder builder = new SpannableStringBuilder();
        int head = 0;
        while (matcher.find()) {
            renderGroup = 1;
            builder.append(unprocessedString.substring(head, matcher.start()));
            if (matcher.group(1).equals("")) {
                renderGroup = 3;
            }
            String processableExtract = matcher.group(renderGroup);

            // Set type formatting for the found matches
            if (matcher.group(2).equals(PROCESSABLE_TAG_TYPE)) {
                final String properProcessableExtract =
                        processableExtract.substring(0, 1).toUpperCase() + processableExtract.substring(1);
                builder.append(properProcessableExtract);
                final StyleSpan bold = new StyleSpan(android.graphics.Typeface.BOLD); // Bold
                final ForegroundColorSpan color = new ForegroundColorSpan(context.getResources() // Type color
                        .getColor(TypeUtil.getTypeColorRes(
                                TypeUtil.TypeValue.getTypeValueByName(
                                        processableExtract))));
                final CleanClickableSpan link = new CleanClickableSpan() {
                    @Override
                    public void onClick(View widget) {
                        Toast.makeText(widget.getContext(), properProcessableExtract, Toast.LENGTH_SHORT).show(); //Todo: to type entry
                    }
                };
                int start = builder.length() - matcher.group(renderGroup).length();
                int end = builder.length();
                builder.setSpan(bold, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                builder.setSpan(color, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                builder.setSpan(link, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            } else {
                builder.append(processableExtract);
            }
            head = matcher.end();
        }
        builder.append(unprocessedString.substring(head, unprocessedString.length()));
        return builder;
    }

}




Java Source Code List

com.nav.dexedd.activity.DexEntryActivity.java
com.nav.dexedd.activity.Dexedd.java
com.nav.dexedd.activity.MainActivity.java
com.nav.dexedd.activity.SplashActivity.java
com.nav.dexedd.adapter.DexAdapter.java
com.nav.dexedd.adapter.TreeAdapter.java
com.nav.dexedd.fragment.DatabaseInitFragment.java
com.nav.dexedd.fragment.NavigationDrawerFragment.java
com.nav.dexedd.model.Ability.java
com.nav.dexedd.model.EggGroup.java
com.nav.dexedd.model.EvolutionCondition.java
com.nav.dexedd.model.Item.java
com.nav.dexedd.model.Location.java
com.nav.dexedd.model.Move.java
com.nav.dexedd.model.Pokemon.java
com.nav.dexedd.model.Region.java
com.nav.dexedd.model.StatSpread.java
com.nav.dexedd.model.Stat.java
com.nav.dexedd.model.Type.java
com.nav.dexedd.persistence.DexDatabase.java
com.nav.dexedd.persistence.access.Access.java
com.nav.dexedd.persistence.access.DexEntry.java
com.nav.dexedd.persistence.access.Dex.java
com.nav.dexedd.structure.Tree.java
com.nav.dexedd.text.CleanClickableSpan.java
com.nav.dexedd.ui.BetterGridView.java
com.nav.dexedd.ui.BetterScrollView.java
com.nav.dexedd.ui.LabelView.java
com.nav.dexedd.ui.SquareImageView.java
com.nav.dexedd.ui.TreeLayout.java
com.nav.dexedd.ui.TypeTagView.java
com.nav.dexedd.util.AssetUtil.java
com.nav.dexedd.util.ConversionUtil.java
com.nav.dexedd.util.PokemonTextUtil.java
com.nav.dexedd.util.TypeUtil.java