123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- /*
- * Copyright (c) 2006-2017 DMDirc Developers
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
- * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
- * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
- * permit persons to whom the Software is furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
- * Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
- * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
- * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
- * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- */
-
- package com.dmdirc.addons.ui_swing.components.addonbrowser;
-
- import com.dmdirc.addons.ui_swing.UIUtilities;
- import com.dmdirc.addons.ui_swing.components.LoggingSwingWorker;
- import com.dmdirc.addons.ui_swing.components.text.TextLabel;
- import com.dmdirc.config.provider.AggregateConfigProvider;
- import com.dmdirc.updater.manager.UpdateManager;
- import com.dmdirc.util.URLBuilder;
- import com.dmdirc.util.io.ConfigFile;
- import com.dmdirc.util.io.DownloadListener;
- import com.dmdirc.util.io.Downloader;
- import com.dmdirc.util.io.InvalidConfigFileException;
-
- import java.io.IOException;
- import java.nio.file.Path;
- import java.util.Collection;
- import java.util.Collections;
- import java.util.concurrent.ExecutionException;
- import java.util.stream.Collectors;
-
- import javax.swing.Box;
- import javax.swing.JPanel;
- import javax.swing.JProgressBar;
- import javax.swing.JScrollPane;
- import javax.swing.text.StyleConstants;
-
- import net.miginfocom.swing.MigLayout;
-
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
-
- import static com.dmdirc.util.LogUtils.USER_ERROR;
-
- /**
- * Loads the addon data feed into the addon browser.
- */
- public class DataLoaderWorker
- extends LoggingSwingWorker<Collection<AddonInfo>, Object>
- implements DownloadListener {
-
- private static final Logger LOG = LoggerFactory.getLogger(DataLoaderWorker.class);
- /** List to load data into. */
- private final AddonTable table;
- /** Browser window to pass to addon info objects. */
- private final BrowserWindow browserWindow;
- /** Table's parent scrollpane. */
- private final JScrollPane scrollPane;
- /** Downloader progress bar. */
- private final JProgressBar jpb = new JProgressBar(0, 100);
- /** Refresh addons feed? */
- private final boolean download;
- /** Directory to store temporary files in. */
- private final Path tempDirectory;
- /** URL Builder to use when loading addon resources. */
- private final URLBuilder urlBuilder;
- /** Factory to use to produce install workers. */
- private final InstallWorkerFactory workerFactory;
- /** The manager to use to retrieve update information. */
- private final UpdateManager updateManager;
- /** Configuration to read settings from. */
- private final AggregateConfigProvider globalConfig;
- /** Downloader to download files. */
- private final Downloader downloader;
-
- /**
- * Creates a new data loader worker.
- *
- * @param downloader Used to download files
- * @param globalConfig Configuration to read settings from.
- * @param urlBuilder URL Builder to use when loading addon resources.
- * @param workerFactory Factory to use to produce install workers.
- * @param updateManager Manager to use to retrieve update information.
- * @param tempDirectory The directory to store temporary items in, such as the addons feed.
- * @param table Table to load data into
- * @param download Download new addons feed?
- * @param browserWindow Browser window to pass to table objects
- * @param scrollPane Table's parent scrollpane
- */
- public DataLoaderWorker(
- final Downloader downloader,
- final AggregateConfigProvider globalConfig,
- final URLBuilder urlBuilder,
- final InstallWorkerFactory workerFactory,
- final UpdateManager updateManager,
- final Path tempDirectory,
- final AddonTable table,
- final boolean download,
- final BrowserWindow browserWindow,
- final JScrollPane scrollPane) {
- this.downloader = downloader;
- this.globalConfig = globalConfig;
- this.urlBuilder = urlBuilder;
- this.workerFactory = workerFactory;
- this.download = download;
- this.table = table;
- this.updateManager = updateManager;
- this.tempDirectory = tempDirectory;
- this.browserWindow = browserWindow;
- this.scrollPane = scrollPane;
- }
-
- @Override
- protected Collection<AddonInfo> doInBackground() {
- final JPanel loadingPanel = new JPanel(
- new MigLayout("fill, alignx 50%, aligny 50%"));
- scrollPane.setViewportView(loadingPanel);
- if (download) {
- final TextLabel label = new TextLabel(
- "Downloading addon info, please wait...");
- label.setAlignment(StyleConstants.ALIGN_CENTER);
- loadingPanel.add(Box.createVerticalGlue(), "growy, pushy, wrap");
- loadingPanel.add(label, "growx, wrap");
- loadingPanel.add(jpb, "growx, wrap");
- loadingPanel.add(Box.createVerticalGlue(), "growy, pushy");
- try {
- downloader.downloadPage("https://addons.dmdirc.com/feed",
- tempDirectory.resolve("addons.feed"), this);
- } catch (final IOException ex) {
- loadingPanel.removeAll();
- loadingPanel.add(new TextLabel("Unable to download feeds: " + ex.getMessage()));
- return Collections.emptyList();
- }
- }
-
- loadingPanel.removeAll();
- loadingPanel.add(new TextLabel("Loading addon info, please wait."));
- final ConfigFile data = new ConfigFile(tempDirectory.resolve("addons.feed"));
- try {
- data.read();
- } catch (final IOException | InvalidConfigFileException ex) {
- return Collections.emptyList();
- }
-
- return data.getKeyDomains().values().stream()
- .map(entry -> new AddonInfo(globalConfig, updateManager, urlBuilder, entry))
- .collect(Collectors.toList());
- }
-
- @Override
- protected void done() {
- if (isCancelled()) {
- return;
- }
-
- Collection<AddonInfo> data;
- try {
- data = get();
- } catch (final InterruptedException ex) {
- data = Collections.emptyList();
- } catch (final ExecutionException ex) {
- LOG.warn(USER_ERROR, ex.getMessage(), ex);
- data = Collections.emptyList();
- }
- final int selectedRow;
- if (table.getRowCount() > 0 && table.getSelectedRow() > 0) {
- selectedRow = table.getSelectedRow();
- } else {
- selectedRow = 0;
- }
- table.getModel().setRowCount(0);
- for (final AddonInfo info : data) {
- table.getModel().addRow(new Object[]{
- new AddonInfoLabel(info, browserWindow, workerFactory),});
- }
- table.getSelectionModel().setSelectionInterval(selectedRow, selectedRow);
- scrollPane.setViewportView(table);
- }
-
- @Override
- public void downloadProgress(final float percent) {
- UIUtilities.invokeLater(() -> jpb.setValue((int) percent));
- }
-
- @Override
- public void setIndeterminate(final boolean indeterminate) {
- UIUtilities.invokeLater(() -> jpb.setIndeterminate(indeterminate));
- }
-
- }
|