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

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

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

import net.vectorpublish.desktop.vp.api.io.DocumentWriter;
import net.vectorpublish.desktop.vp.api.io.FileMenu;
import net.vectorpublish.desktop.vp.api.io.SaveAs;
import net.vectorpublish.desktop.vp.api.ui.Dialog;
import net.vectorpublish.desktop.vp.api.ui.DocumentSelectionChangeListener;
import net.vectorpublish.desktop.vp.api.ui.FileChangedListener;
import net.vectorpublish.desktop.vp.api.ui.ToolBar;
import net.vectorpublish.desktop.vp.api.vpd.DocumentNode;
import net.vectorpublish.desktop.vp.i8n.I8n;
import net.vectorpublish.desktop.vp.io.IOConstraints;
import net.vectorpublish.desktop.vp.ui.ImageKey;
import net.vectorpublish.desktop.vp.ui.Namespace;

@Named
public class SaveFileAs extends SaveAs implements DocumentSelectionChangeListener {
	@Inject
	private final ToolBar toolbar = null;

	@Inject
	private final FileMenu menu = null;

	@Inject
	private final Dialog dialog = null;

	@Inject
	private final FileChangedListener[] listeners = null;

	@Inject
	private final DocumentWriter writer = null;

	private DocumentNode selected;

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

	@Override
	public void actionPerformed(ActionEvent e) {
		new Thread(new Runnable() {

			@Override
			public void run() {
				try {
					final File f = dialog.showSaveFile(IOConstraints.FILE_EXTENSION, IOConstraints.FILE_DESCRIPTION);
					if (f == null) { // user canceled save.
						return;
					}
					if (!f.exists() || dialog
							.confirm(Namespace.getNamespace("system.io", "file.overwrite"), "Overwrite File", "Realy overwrite", "Overwrite", "Cancel").get()) {
						writer.write(f, selected);
						selected.setFile(f);
						for (final FileChangedListener listener : listeners) {
							listener.fileChanged(selected, f);
						}
					}
				} catch (final IOException e1) {
					e1.printStackTrace();
				} catch (InterruptedException e1) {
					e1.printStackTrace();
				} catch (ExecutionException e1) {
					e1.printStackTrace();
				}

			}
		}).start();
	}

	@Override
	public void notifyDocumentSelected(DocumentNode selected) {
		this.selected = selected;
		setEnabled(selected != null);
	}

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




© 2015 - 2025 Weber Informatics LLC | Privacy Policy