All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.vectorpublish.desktop.vp.DocumentImport Maven / Gradle / Ivy

/*
 * Copyright (c) 2016, Peter Rader. All rights reserved.
 *  ___ ___               __                 ______         __     __  __         __
 * |   |   |.-----..----.|  |_ .-----..----.|   __ \.--.--.|  |--.|  ||__|.-----.|  |--.
 * |   |   ||  -__||  __||   _||  _  ||   _||    __/|  |  ||  _  ||  ||  ||__ --||     |
 *  \_____/ |_____||____||____||_____||__|  |___|   |_____||_____||__||__||_____||__|__|
 *
 * http://www.gnu.org/licenses/gpl-3.0.html
 */
package net.vectorpublish.desktop.vp;

import java.awt.event.ActionEvent;
import java.io.File;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.concurrent.Future;

import javax.inject.Inject;
import javax.inject.Named;

import net.vectorpublish.desktop.vp.History.HistoryStep;
import net.vectorpublish.desktop.vp.api.history.Redo;
import net.vectorpublish.desktop.vp.api.io.I8nTextIO;
import net.vectorpublish.desktop.vp.api.layer.Layer;
import net.vectorpublish.desktop.vp.api.ui.Dialog;
import net.vectorpublish.desktop.vp.api.ui.DocumentSelectionChangeListener;
import net.vectorpublish.desktop.vp.api.ui.ToolBar;
import net.vectorpublish.desktop.vp.api.ui.VPAbstractAction;
import net.vectorpublish.desktop.vp.api.ui.Warn;
import net.vectorpublish.desktop.vp.api.vpd.DocumentNode;
import net.vectorpublish.desktop.vp.io.IOConstraints;
import net.vectorpublish.desktop.vp.io.VPDocumentNode;
import net.vectorpublish.desktop.vp.io.VPFileReader;
import net.vectorpublish.desktop.vp.ui.ImageKey;

@Named
public class DocumentImport extends VPAbstractAction implements DocumentSelectionChangeListener {

	private class AskHistoryStep {
		HistoryStep step;
		Future ask;
	}

	private static final String NS = "net.vectorpublish.desktop.vp.DocumentImport";

	/**
	 * The toolbar.
	 */
	@Inject
	private final ToolBar toolbar = null;

	/**
	 * For general questions to the user.
	 */
	@Inject
	private final Dialog diag = null;

	/**
	 * The warn-dialog for warn-messasges.
	 */
	@Inject
	private final Warn warn = null;

	/**
	 * The History.
	 */
	@Inject
	private final History hist = null;

	/**
	 * Injects a CDI-Bean.
	 */
	@Inject
	private final Redo redo = null;

	/**
	 * The Layer we use.
	 */
	@Inject
	private final Layer layer = null;

	@Inject
	private final VPFileReader reader = null;

	private DocumentNode selectedDocument;

	public DocumentImport() {
		super(I8nTextIO.IMPORT, I8nTextIO.IMPORT_DESC, true);
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		new Thread(new Runnable() {
			@Override
			public void run() {
				final File f = diag.showOpenFile(IOConstraints.FILE_EXTENSION, IOConstraints.FILE_DESCRIPTION);

				if (f != null) {
					try {
						final VPDocumentNode imported = reader.readFully(f, (VPDocumentNode) selectedDocument).get();
						final HistoryStep lastExecutedHistoryStep = selectedDocument.getLastExecutedHistoryStep();

						HistoryStep step = imported.getLastExecutedHistoryStep();

						while (step.previous != null) {
							step = step.previous;
						}
						final HistoryStep first = step;
						final LinkedHashSet askExecute = new LinkedHashSet<>();
						// now we are on first step, now ask for all:
						int i = 0;

						while (step != null) {
							i++;
							final AskHistoryStep ahs = new AskHistoryStep();
							ahs.ask = diag.ask(IOContributions.NS, i+"# " + step, true);
							ahs.step = step;
							step = step.next;
							askExecute.add(ahs);
						}
						boolean addedAtLeastOne = false;
						Iterator iterator = askExecute.iterator();
						while (iterator.hasNext()) {
							DocumentImport.AskHistoryStep askHistoryStep = (DocumentImport.AskHistoryStep) iterator.next();
							Boolean b = askHistoryStep.ask.get();
							if (b == null) {
								return;
							}
							if (!b) {
								HistoryStep remove = askHistoryStep.step;
								HistoryStep previous = remove.previous;
								HistoryStep next = remove.next;
								next.previous = previous;
								previous.next = next;
								iterator.remove();
							} else {
								addedAtLeastOne = true;
							}
						}
						Iterator it = askExecute.iterator();
						selectedDocument.setLastExecutedHistoryStep(it.next().step);
						if (addedAtLeastOne) {
							do {
								redo.actionPerformed(null);
							} while (redo.isEnabled());
						}
					} catch (final Exception e1) {
						warn.addWarning("Could not import everything, problem on importing '" + hist.getCurrentDocument().getLastExecutedHistoryStep().next + "'!");
						e1.printStackTrace();
					}
				}

			}
		}).start();
	}

	@Override
	public void notifyDocumentSelected(DocumentNode selectedDocument) {
		this.selectedDocument = selectedDocument;
		setEnabled(true);
	}

	@javax.annotation.PostConstruct
	private void setup() {
		setIcons(IOContributions.NS, ImageKey.get("import"));
		toolbar.add(this);
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy