com.getqube.store.BrowseCategoryController.java Source code

Java tutorial

Introduction

Here is the source code for com.getqube.store.BrowseCategoryController.java

Source

/**
 * Copyright 2014, Joshua Shane Martin <josmar52789@gmail.com>
 *
 * This file is part of Qube App Store.
 *
 * Qube App Store 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.
 *
 * Qube App Store 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 Qube App Store.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.getqube.store;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.getqube.store.models.App;
import com.getqube.store.models.Category;
import com.getqube.store.services.AppService;
import com.getqube.store.ui.CategoryButtonControl;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.control.TreeItem;
import javafx.scene.image.Image;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.TilePane;
import javafx.stage.Stage;
import net.engio.mbassy.listener.Listener;
import net.engio.mbassy.listener.References;
import org.controlsfx.control.action.Action;
import org.controlsfx.dialog.Dialog;
import org.controlsfx.dialog.DialogStyle;
import org.controlsfx.dialog.Dialogs;
import org.datafx.controller.context.ApplicationContext;

import javax.ws.rs.ProcessingException;
import java.io.IOException;
import java.net.ConnectException;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

@Listener(references = References.Strong)
public class BrowseCategoryController {
    private static Logger logger = Logger.getLogger(BrowseCategoryController.class.getPackage().getName());
    private HashMap<String, String> apps = new HashMap<String, String>();

    @FXML
    BorderPane paneLoading;

    @FXML
    BorderPane paneEverything;

    @FXML
    ProgressIndicator progressLoading;

    @FXML
    private TilePane paneApps;

    public void initialize() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Category c = ApplicationContext.getInstance().getRegisteredObject(Category.class);

                try {
                    App[] apps = AppService.allForCategory(String.valueOf(c.getId()));

                    // Show the app detail page now that the data has been loaded from the Web.
                    Platform.runLater(new Runnable() {
                        @Override
                        public void run() {

                            for (App app : apps) {
                                CategoryButtonControl button = new CategoryButtonControl();
                                button.setLink(app.getName());

                                button.setImage(new Image(app.getThumbnail()));

                                button.addEventFilter(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
                                    public void handle(final MouseEvent mouseEvent) {
                                        ApplicationContext.getInstance().register(app);

                                        TreeItem<String> appItem = new TreeItem<String>(app.getName());
                                        CategoriesController.BREADCRUMBS.getSelectedCrumb().getChildren()
                                                .add(appItem);
                                        CategoriesController.BREADCRUMBS.setSelectedCrumb(appItem);

                                        Navigator.loadVista(Navigator.APP_DETAIL);
                                    }
                                });

                                paneApps.getChildren().add(button);
                            }

                            paneLoading.setVisible(false);
                            paneEverything.setVisible(true);
                        }
                    });
                } catch (ProcessingException processingException) {
                    logger.log(Level.WARNING, processingException.getMessage(), processingException);
                    reportConnectionProblem();
                } catch (IOException ioException) {
                    logger.log(Level.WARNING, ioException.getMessage(), ioException);
                    reportConnectionProblem();
                }
            }
        }).start();
    }

    private void reportConnectionProblem() {
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                Action response = Dialogs.create().owner((Stage) paneLoading.getScene().getWindow())
                        .style(DialogStyle.NATIVE).title("Uh Oh").masthead("Possible Connection Problem")
                        .message(
                                "Could not retrieve apps from the Qube App Store possibly due to a connection problem. Please check your Internet connection and try again.")
                        .showError();

                if (response == Dialog.Actions.OK) {
                    Navigator.loadVista(Navigator.ALL_CATEGORIES);
                    CategoriesController.resetBreadcrumbs();
                }
            }
        });
    }
}