Java tutorial
//--------------------------------------------------------------------------------------- // Copyright (c) 2001-2016 by PDFTron Systems Inc. All Rights Reserved. // Consult legal.txt regarding legal and license information. //--------------------------------------------------------------------------------------- package com.pdftron.pdf.controls; import android.app.Activity; import android.content.Context; import android.content.DialogInterface; import android.os.Bundle; import android.support.v4.app.DialogFragment; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.RelativeLayout; import android.widget.TextView; import android.widget.Toast; import com.pdftron.common.PDFNetException; import com.pdftron.pdf.Action; import com.pdftron.pdf.Bookmark; import com.pdftron.pdf.PDFViewCtrl; import com.pdftron.pdf.tools.R; import com.pdftron.pdf.utils.AnalyticsHandlerAdapter; import java.util.ArrayList; /** * The OutlineDialogFragment shows a document outline (bookmarks) that can be * used to navigate the document in the PDFViewCtrl. */ public class OutlineDialogFragment extends DialogFragment { public interface OutlineDialogFragmentListener { void onBookmarkClicked(Bookmark parent, Bookmark bookmark); void onOutlineDialogDismissed(Bookmark parent); } private OutlineDialogFragmentListener mListener; private PDFViewCtrl mPDFViewCtrl; private ArrayList<Bookmark> mBookmarks; private BookmarkListAdapter mBookmarkListAdapter; private ListView mListViewBookmarks; private RelativeLayout mNavigation; private TextView mNavigationText; /** * Our current bookmark. It reflects the current top bookmark being shown, * i.e., the list of bookmarks in the dialog are its children. A null * mCurrentBookmark or an indentation equals to zero means we are on the * root of the bookmark tree. */ public Bookmark mCurrentBookmark; public static OutlineDialogFragment newInstance(PDFViewCtrl pdfViewCtrl, Bookmark currentBookmark) { OutlineDialogFragment f = new OutlineDialogFragment(); f.setPDFViewCtrl(pdfViewCtrl); f.setCurrentBookmark(currentBookmark); return f; } public static OutlineDialogFragment newInstance(PDFViewCtrl pdfViewCtrl) { return newInstance(pdfViewCtrl, null); } public void setPDFViewCtrl(PDFViewCtrl pdfViewCtrl) { if (pdfViewCtrl == null) { throw new NullPointerException("pdfViewCtrl can't be null"); } mPDFViewCtrl = pdfViewCtrl; } public void setCurrentBookmark(Bookmark currentBookmark) { if (null != currentBookmark) { mCurrentBookmark = currentBookmark; } } @Override public void onAttach(Activity activity) { super.onAttach(activity); try { mListener = (OutlineDialogFragmentListener) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement OutlineDialogFragmentListener"); } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.controls_fragment_outline_dialog, null); mBookmarks = new ArrayList<Bookmark>(); if (mCurrentBookmark != null) { try { mBookmarks.addAll(getBookmarkList(mCurrentBookmark.getFirstChild())); } catch (PDFNetException e) { mBookmarks.clear(); mBookmarks.addAll(getBookmarkList(null)); mCurrentBookmark = null; } } else { mBookmarks.addAll(getBookmarkList(null)); } mBookmarkListAdapter = new BookmarkListAdapter(getActivity(), R.layout.controls_fragment_outline_listview_item, mBookmarks); mListViewBookmarks = (ListView) view.findViewById(R.id.control_outline_listview); mListViewBookmarks.setEmptyView(view.findViewById(R.id.control_outline_textview_empty)); mListViewBookmarks.setAdapter(mBookmarkListAdapter); mListViewBookmarks.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { try { com.pdftron.pdf.utils.AnalyticsHandlerAdapter.getInstance() .sendEvent(AnalyticsHandlerAdapter.CATEGORY_BOOKMARK, "Navigated by Outline List"); Action action = mBookmarks.get(position).getAction(); if (action != null && action.isValid() && action.getType() == Action.e_GoTo & action.getDest().isValid()) { if (mPDFViewCtrl != null) { mPDFViewCtrl.executeAction(action); } if (mListener != null) { mListener.onBookmarkClicked(mCurrentBookmark, mBookmarks.get(position)); } } } catch (PDFNetException e) { com.pdftron.pdf.utils.AnalyticsHandlerAdapter.getInstance().sendException(e); Toast.makeText(getActivity(), "This bookmark has an invalid action", Toast.LENGTH_SHORT).show(); } } }); // Navigation mNavigation = (RelativeLayout) view.findViewById(R.id.control_outline_layout_navigation); mNavigation.setVisibility(View.GONE); mNavigationText = (TextView) mNavigation.findViewById(R.id.control_outline_layout_navigation_title); try { if (mCurrentBookmark != null && mCurrentBookmark.getIndent() > 0) { mNavigationText.setText(mCurrentBookmark.getTitle()); if (mCurrentBookmark.getIndent() <= 0) { mNavigation.setVisibility(View.GONE); } else { mNavigation.setVisibility(View.VISIBLE); } } else { mNavigation.setVisibility(View.GONE); } } catch (PDFNetException e) { mNavigation.setVisibility(View.GONE); } mNavigation.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { navigateToParentBookmark(); } }); return view; } @Override public void onDismiss(DialogInterface dialog) { if (mListener != null) { mListener.onOutlineDialogDismissed(mCurrentBookmark); } super.onDismiss(dialog); } @Override public void onDestroy() { if (mListener != null) { mListener.onOutlineDialogDismissed(mCurrentBookmark); } super.onDestroy(); } private void navigateToParentBookmark() { ArrayList<Bookmark> temp = null; try { if (mCurrentBookmark != null && mCurrentBookmark.getIndent() > 0) { mCurrentBookmark = mCurrentBookmark.getParent(); temp = getBookmarkList(mCurrentBookmark.getFirstChild()); mNavigationText.setText(mCurrentBookmark.getTitle()); if (mCurrentBookmark.getIndent() <= 0) { mNavigation.setVisibility(View.GONE); } } else { temp = getBookmarkList(null); mCurrentBookmark = null; mNavigation.setVisibility(View.GONE); } } catch (PDFNetException e) { mCurrentBookmark = null; temp = null; } if (temp != null) { mBookmarks.clear(); mBookmarks.addAll(temp); mBookmarkListAdapter.notifyDataSetChanged(); } } private class BookmarkListAdapter extends ArrayAdapter<Bookmark> { private Context mContext; private int mLayoutResourceId; private ArrayList<Bookmark> mBookmarks; private ViewHolder mViewHolder; public BookmarkListAdapter(Context context, int resource, ArrayList<Bookmark> objects) { super(context, resource, objects); mContext = context; mLayoutResourceId = resource; mBookmarks = objects; } @Override public int getCount() { if (mBookmarks != null) { return mBookmarks.size(); } else { return 0; } } @Override public View getView(final int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = LayoutInflater.from(mContext).inflate(mLayoutResourceId, null); mViewHolder = new ViewHolder(); mViewHolder.bookmarkText = (TextView) convertView .findViewById(R.id.control_outline_listview_item_textview); mViewHolder.bookmarkArrow = (ImageView) convertView .findViewById(R.id.control_outline_listview_item_imageview); convertView.setTag(mViewHolder); } else { mViewHolder = (ViewHolder) convertView.getTag(); } mViewHolder.bookmarkArrow.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { OutlineDialogFragment.this.mCurrentBookmark = mBookmarks.get(position); ArrayList<Bookmark> temp = new ArrayList<Bookmark>(); try { temp = getBookmarkList(OutlineDialogFragment.this.mCurrentBookmark.getFirstChild()); } catch (PDFNetException e) { temp.clear(); } if (temp != null) { mBookmarks.clear(); mBookmarks.addAll(temp); notifyDataSetChanged(); OutlineDialogFragment.this.mNavigation.setVisibility(View.VISIBLE); try { OutlineDialogFragment.this.mNavigationText .setText(OutlineDialogFragment.this.mCurrentBookmark.getTitle()); } catch (PDFNetException e) { com.pdftron.pdf.utils.AnalyticsHandlerAdapter.getInstance().sendException(e); } } } }); Bookmark bookmark = mBookmarks.get(position); try { mViewHolder.bookmarkText.setText(bookmark.getTitle()); if (bookmark.hasChildren()) { mViewHolder.bookmarkArrow.setVisibility(View.VISIBLE); } else { mViewHolder.bookmarkArrow.setVisibility(View.GONE); } } catch (PDFNetException e) { com.pdftron.pdf.utils.AnalyticsHandlerAdapter.getInstance().sendException(e); } return convertView; } private class ViewHolder { public TextView bookmarkText; public ImageView bookmarkArrow; } } private ArrayList<Bookmark> getBookmarkList(Bookmark firstSibling) { ArrayList<Bookmark> bookmarkList = new ArrayList<Bookmark>(); if (mPDFViewCtrl != null) { try { Bookmark current; if (firstSibling == null) { current = mPDFViewCtrl.getDoc().getFirstBookmark(); } else { current = firstSibling; } int numBookmarks = 0; while (current.isValid()) { bookmarkList.add(current); current = current.getNext(); numBookmarks++; } if (firstSibling == null && numBookmarks == 1) { ArrayList<Bookmark> bookmarkListNextLevel = new ArrayList<Bookmark>(); bookmarkListNextLevel .addAll(getBookmarkList(mPDFViewCtrl.getDoc().getFirstBookmark().getFirstChild())); if (bookmarkListNextLevel.size() > 0) { return bookmarkListNextLevel; } } } catch (PDFNetException e) { bookmarkList.clear(); } } return bookmarkList; } private int countSiblings(Bookmark bookmark) { int numBookmarks = 0; try { Bookmark temp = bookmark.getNext(); while (temp.isValid()) { temp = temp.getNext(); numBookmarks++; } temp = bookmark.getPrev(); while (temp.isValid()) { temp = temp.getPrev(); numBookmarks++; } } catch (PDFNetException e) { numBookmarks = 0; } return numBookmarks; } }