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

net.vectorpublish.desktop.vp.NewFile 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.util.concurrent.Future;

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

import net.vectorpublish.desktop.vp.api.conf.Config;
import net.vectorpublish.desktop.vp.api.io.DrawArea;
import net.vectorpublish.desktop.vp.api.io.FileMenu;
import net.vectorpublish.desktop.vp.api.io.Files;
import net.vectorpublish.desktop.vp.api.layer.Layer;
import net.vectorpublish.desktop.vp.api.ui.Dialog;
import net.vectorpublish.desktop.vp.api.ui.KeyframeSlider;
import net.vectorpublish.desktop.vp.api.ui.ToolBar;
import net.vectorpublish.desktop.vp.api.ui.VPAbstractAction;
import net.vectorpublish.desktop.vp.i8n.I8n;
import net.vectorpublish.desktop.vp.io.NewDocumentHS;
import net.vectorpublish.desktop.vp.io.VPDocumentNode;
import net.vectorpublish.desktop.vp.io.VPDocumentROHSDB;
import net.vectorpublish.desktop.vp.pd.official.DrawPanel;
import net.vectorpublish.desktop.vp.ui.ImageKey;
import net.vectorpublish.desktop.vp.ui.Namespace;
import net.vectorpublish.desktop.vp.ui.i8n.I8nTextDefault;

@Named
public class NewFile extends VPAbstractAction {

	private static final Namespace NS = Namespace.getNamespace("net.vectorpublish.io", "file.new");

	@Inject
	private final Dialog dialog = null;

	@Inject
	private final Files files = null;

	@Inject
	private final FileMenu menu = null;

	@Inject
	private final Layer layer = null;

	@Inject
	private final ToolBar tb = null;

	@Inject
	private final History history = null;

	@Inject
	private final KeyframeSlider keyframer = null;

	@Inject
	private final Config cfg = null;

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

	public NewFile() {
		super(I8nTextDefault.NEW, I8nTextDefault.NEW_DESC, false);
	}

	@Override
	public void actionPerformed(ActionEvent evt) {
		new Thread(new Runnable() {
			public String findNewTitle() {
				String newTitle;
				int newDocumentNumber = 0;
				boolean titleFound;
				do {
					titleFound = false;
					newDocumentNumber++;
					newTitle = "Document " + newDocumentNumber;
					for (int tabIndex = 0; tabIndex < files.getTabCount(); tabIndex++) {
						if (files.getTitleAt(tabIndex).equals(newTitle)) {
							titleFound = true;
						}
					}
				} while (titleFound);
				return newTitle;
			}

			/*
			 * (non-Javadoc)
			 *
			 * @see java.lang.Runnable#run()
			 */
			@Override
			public void run() {
				try {

					final String w = cfg.read(NS, "width");
					final String h = cfg.read(NS, "height");
					int width = 300;
					int height = 400;
					if (w != null) {
						width = Integer.parseInt(w);
					}
					if (h != null) {
						height = Integer.parseInt(h);
					}
					final Future widthAsk = dialog.ask(NS, "What width?", width);
					final Future heightAsk = dialog.ask(NS, "What height?", height);

					Integer documentWidth = widthAsk.get();
					Integer documentHeight = heightAsk.get();
					if (documentHeight == null | documentWidth == null) {
						return;
					}
					final DrawArea drawArea = new DrawArea(documentWidth, documentHeight);
					final DrawPanel drawPanel = new DrawPanel(findNewTitle(), drawArea, keyframer);
					final VPDocumentNode doc = new VPDocumentNode(drawPanel);
					// So we have a document now and insert a HistoryStep for
					// the Document.

					doc.setLastExecutedHistoryStep(new NewDocumentHS(history, new VPDocumentROHSDB(drawArea.getDimensions())));
					files.addDocument(doc);
					layer.setRoot(doc);
					drawArea.setDocument(doc);
					cfg.write(NS, "width", "" + documentWidth);
					cfg.write(NS, "height", "" + documentHeight);
				} catch (final Exception e) {
					e.printStackTrace();
				}
			}
		}).start();
	}

	@PostConstruct
	private void setup() {
		setIcons(IOContributions.NS, ImageKey.get("new"));
		menu.add(this);
		tb.add(this);
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy