michael@0: /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.home; michael@0: michael@0: import org.mozilla.gecko.db.BrowserContract.HomeItems; michael@0: import org.mozilla.gecko.home.HomeConfig.ViewConfig; michael@0: import org.mozilla.gecko.home.HomePager.OnUrlOpenListener; michael@0: import org.mozilla.gecko.home.PanelLayout.FilterManager; michael@0: import org.mozilla.gecko.home.PanelLayout.OnItemOpenListener; michael@0: michael@0: import android.database.Cursor; michael@0: michael@0: import java.util.EnumSet; michael@0: michael@0: class PanelViewItemHandler { michael@0: private final ViewConfig mViewConfig; michael@0: private OnItemOpenListener mItemOpenListener; michael@0: private FilterManager mFilterManager; michael@0: michael@0: public PanelViewItemHandler(ViewConfig viewConfig) { michael@0: mViewConfig = viewConfig; michael@0: } michael@0: michael@0: public void setOnItemOpenListener(OnItemOpenListener listener) { michael@0: mItemOpenListener = listener; michael@0: } michael@0: michael@0: public void setFilterManager(FilterManager manager) { michael@0: mFilterManager = manager; michael@0: } michael@0: michael@0: /** michael@0: * If item at this position is a back item, perform the go back action via the michael@0: * {@code FilterManager}. Otherwise, prepare the url to be opened by the michael@0: * {@code OnUrlOpenListener}. michael@0: */ michael@0: public void openItemAtPosition(Cursor cursor, int position) { michael@0: if (mFilterManager != null && mFilterManager.canGoBack()) { michael@0: if (position == 0) { michael@0: mFilterManager.goBack(); michael@0: return; michael@0: } michael@0: michael@0: position--; michael@0: } michael@0: michael@0: if (cursor == null || !cursor.moveToPosition(position)) { michael@0: throw new IllegalStateException("Couldn't move cursor to position " + position); michael@0: } michael@0: michael@0: int urlIndex = cursor.getColumnIndexOrThrow(HomeItems.URL); michael@0: final String url = cursor.getString(urlIndex); michael@0: michael@0: int titleIndex = cursor.getColumnIndexOrThrow(HomeItems.TITLE); michael@0: final String title = cursor.getString(titleIndex); michael@0: michael@0: if (mItemOpenListener != null) { michael@0: mItemOpenListener.onItemOpen(url, title); michael@0: } michael@0: } michael@0: }