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

net.vectorpublish.desktop.vp.OpenImpl 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.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.concurrent.ExecutionException;

import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.inject.Named;

import net.vectorpublish.desktop.vp.History.HistoryStep;
import net.vectorpublish.desktop.vp.api.io.FileMenu;
import net.vectorpublish.desktop.vp.api.io.Files;
import net.vectorpublish.desktop.vp.api.io.Open;
import net.vectorpublish.desktop.vp.api.layer.Layer;
import net.vectorpublish.desktop.vp.api.ui.Dialog;
import net.vectorpublish.desktop.vp.api.ui.ToolBar;
import net.vectorpublish.desktop.vp.api.ui.Warn;
import net.vectorpublish.desktop.vp.i8n.I8n;
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 OpenImpl extends Open {

	@Inject
	private final FileMenu fileMenu = null;

	@Inject
	private final Warn warn = null;

	@Inject
	private final Dialog dialog = null;

	@Inject
	private final VPFileReader reader = null;

	@Inject
	private final Files files = null;

	@Inject
	private final Layer layer = null;

	@Inject
	private final ToolBar toolbar = null;

	@Inject
	private final RedoAction redo = null;

	/**
	 * Injects a CDI-Bean.
	 */
	@Inject
	private final I8n i8n = null;

	@Override
	public void actionPerformed(ActionEvent e) {
		final File f = dialog.showOpenFile(IOConstraints.FILE_EXTENSION, IOConstraints.FILE_DESCRIPTION);
		if (f != null) {
			try {
				try {
					openFile(f);
				} catch (final ExecutionException ee) {
					final Throwable cause = ee.getCause();
					if (cause instanceof ClassNotFoundException) {
						warn.addWarning("Missing plug-in for " + cause.getMessage());
					} else {
						warn.addWarning("File could not be read:" + ee.getMessage());
					}
					return;
				}
			} catch (final Exception e1) {
				e1.printStackTrace();
			}
		}
	}

	@Override
	public void openFile(final File f) throws ExecutionException, InterruptedException, IOException, ClassNotFoundException, NoSuchMethodException,
			InstantiationException, IllegalAccessException, InvocationTargetException {
		final VPDocumentNode documentNode = reader.readFully(f, null).get();
		files.addDocument(documentNode);
		layer.setRoot(documentNode);
		HistoryStep step = documentNode.getLastExecutedHistoryStep();
		int count = 0;
		while (step.previous != null) {
			count++;
			step = step.previous;
		}
		documentNode.setLastExecutedHistoryStep(step);

		while (redo.isEnabled() && count-- > -2) {
			redo.actionPerformed(null);
		}
	}

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




© 2015 - 2025 Weber Informatics LLC | Privacy Policy