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

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

The newest version!
/*
 * 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.List;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

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.conf.Config;
import net.vectorpublish.desktop.vp.api.history.ReadOnlyHistoryStepDataBean;
import net.vectorpublish.desktop.vp.api.history.Redo;
import net.vectorpublish.desktop.vp.api.layer.LayerSelectionListener;
import net.vectorpublish.desktop.vp.api.ui.Dialog;
import net.vectorpublish.desktop.vp.api.ui.ToolBar;
import net.vectorpublish.desktop.vp.api.ui.VPAbstractAction;
import net.vectorpublish.desktop.vp.api.vpd.VectorPublishNode;
import net.vectorpublish.desktop.vp.split.SplitText;
import net.vectorpublish.desktop.vp.split.SplitStep;
import net.vectorpublish.desktop.vp.split.bean.SplitData;
import net.vectorpublish.desktop.vp.ui.ImageKey;
import net.vectorpublish.desktop.vp.utils.SetUtils;

@Named
public class SplitButton extends VPAbstractAction implements LayerSelectionListener {

	@Inject
	private final ToolBar toolbar = null;

	@Inject
	private final VetoConsulSplit[] consil = null;

	@Inject
	private final Dialog dialog = null;

	@Inject
	private final History hist = null;

	@Inject
	private final Redo redo = null;

	private Set selection;

	@Inject
	private final Config conf = null;

	public SplitButton() {
		super(SplitText.SPLIT, SplitText.SPLIT_TOOLTIP, true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		new Thread() {
			public void run() {
				try {
					VectorPublishNode parent = selection.iterator().next();
					List path = SetUtils.nodeToImmutableIndex(parent);
					Future confirm = dialog.confirm(SplitText.NS, "How to split?",
							"How do you like to split? Choose vertical for left and right sides or horizontal for top and bottom sides.",
							"Left / Right", "Top / Bottom");
					boolean vertical = confirm.get();
					String defaultBorder = conf.read(SplitText.NS, "border");
					if (defaultBorder == null) {
						defaultBorder = "0";
					}
					Future border = dialog.ask(SplitText.NS, "Border", Integer.parseInt(defaultBorder));
					String defaultPercent = conf.read(SplitText.NS, "percent");
					if (defaultPercent == null) {
						defaultPercent = "50.0";
					}
					Future percent = dialog.ask(SplitText.NS, "Percent from " + (vertical ? "Left" : "Top"),
							Double.parseDouble(defaultPercent));
					SplitData data = new SplitData(vertical, border.get(), (float) (double) percent.get(), path);
					HistoryStep step = new SplitStep(hist,
							hist.getCurrentDocument().getLastExecutedHistoryStep(), data);
					redo.actionPerformed(e);
					conf.write(SplitText.NS, "percent", "" + percent.get());
					conf.write(SplitText.NS, "border", border.get() + "");
				} catch (InterruptedException | ExecutionException e) {
					e.printStackTrace();
				}
			};
		}.start();
	}

	@PostConstruct
	public void setup() {
		toolbar.add(this);
		setIcons(SplitText.NS, ImageKey.get("split"));
	}

	@Override
	public void notify(Set selection) {
		for (VectorPublishNode node : selection) {
			for (VetoConsulSplit concilor : consil) {
				if (concilor.hasVeto(node)) {
					setEnabled(false);
					return;
				}
			}
		}

		setEnabled(selection.size() > 0);
		this.selection = selection;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy