com.btmura.android.reddit.app.MarkdownTableFragment.java Source code

Java tutorial

Introduction

Here is the source code for com.btmura.android.reddit.app.MarkdownTableFragment.java

Source

/*
 * Copyright (C) 2013 Brian Muramatsu
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.btmura.android.reddit.app;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ScrollView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.btmura.android.reddit.R;
import com.btmura.android.reddit.text.MarkdownFormatter;
import com.btmura.android.reddit.util.MarkdownTableScanner;
import com.btmura.android.reddit.util.MarkdownTableScanner.OnTableScanListener;

public class MarkdownTableFragment extends Fragment {

    static final String TAG = "MarkdownTableFragment";

    private static final String EXTRA_TABLE_DATA = "tableData";

    private static final String STATE_SCROLL_X = "scrollX";
    private static final String STATE_SCROLL_Y = "scrollY";

    private ScrollView tableScrollView;

    private final MarkdownFormatter formatter = new MarkdownFormatter();

    public static MarkdownTableFragment newInstance(String tableData) {
        Bundle args = new Bundle(1);
        args.putString(EXTRA_TABLE_DATA, tableData);
        MarkdownTableFragment frag = new MarkdownTableFragment();
        frag.setArguments(args);
        return frag;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.markdown_table_frag, container, false);
        populateTable(view, inflater);
        setupScrollView(view, savedInstanceState);
        return view;
    }

    private void populateTable(final View view, final LayoutInflater inflater) {
        final TableLayout table = (TableLayout) view.findViewById(R.id.table);
        MarkdownTableScanner.scan(getTableDataExtra(), new OnTableScanListener<TableRow>() {
            @Override
            public TableRow onRowStart() {
                return new TableRow(getActivity());
            }

            @Override
            public void onCell(TableRow container, Cell cell) {
                int layout = cell.isHeader ? R.layout.markdown_table_cell_header : R.layout.markdown_table_cell;

                TextView tv = (TextView) inflater.inflate(layout, container, false);
                tv.setGravity(cell.gravity);
                tv.setMovementMethod(LinkMovementMethod.getInstance());
                tv.setText(formatter.formatAll(getActivity(), cell.contents));
                container.addView(tv);

                table.setColumnShrinkable(cell.column, true);
                table.setColumnStretchable(cell.column, true);
            }

            @Override
            public void onRowEnd(TableRow row) {
                table.addView(row);
            }
        });
    }

    private void setupScrollView(View view, Bundle savedInstanceState) {
        tableScrollView = (ScrollView) view.findViewById(R.id.table_scroller);
        if (savedInstanceState != null) {
            final int scrollX = savedInstanceState.getInt(STATE_SCROLL_X);
            final int scrollY = savedInstanceState.getInt(STATE_SCROLL_Y);
            tableScrollView.post(new Runnable() {
                @Override
                public void run() {
                    tableScrollView.setScrollX(scrollX);
                    tableScrollView.setScrollY(scrollY);
                }
            });
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(STATE_SCROLL_X, tableScrollView.getScrollX());
        outState.putInt(STATE_SCROLL_Y, tableScrollView.getScrollY());
    }

    private String getTableDataExtra() {
        return getArguments().getString(EXTRA_TABLE_DATA);
    }
}