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

net.vectorpublish.desktop.vp.ContextBorderUpdater 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.Comparator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

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

import net.vectorpublish.desktop.vp.History.HistoryStep;
import net.vectorpublish.desktop.vp.api.history.Redo;
import net.vectorpublish.desktop.vp.api.layer.LayerPopupContributor;
import net.vectorpublish.desktop.vp.api.layer.LayerSelectionListener;
import net.vectorpublish.desktop.vp.api.ui.Dialog;
import net.vectorpublish.desktop.vp.api.ui.VPAbstractAction;
import net.vectorpublish.desktop.vp.api.vpd.VectorPublishNode;
import net.vectorpublish.desktop.vp.i8n.I8nText;
import net.vectorpublish.desktop.vp.split.SplitNode;
import net.vectorpublish.desktop.vp.split.SplitText;
import net.vectorpublish.desktop.vp.split.border.BorderChangeCouncillor;
import net.vectorpublish.desktop.vp.split.border.BorderChangeHistoryStepData;
import net.vectorpublish.desktop.vp.split.border.ChangeBorderStep;
import net.vectorpublish.desktop.vp.utils.SetUtils;

@Named
public class ContextBorderUpdater extends VPAbstractAction implements LayerSelectionListener, LayerPopupContributor {
	private Set splitters = new LinkedHashSet<>();

	public ContextBorderUpdater() {
		super(SplitText.BORDER_SIZE, SplitText.BORDER_SIZE_TOOLTIP, true);
	}

	@Inject
	private final LayerPopup popup = null;

	@Inject
	private final Dialog dlg = null;

	@Inject
	private final History hst = null;
	@Inject
	private final Redo redo = null;

	@Inject
	private final BorderChangeCouncillor[] borderChangeCouncil = null;

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

			@Override
			public void run() {
				try {
					Future ask = dlg.ask(SplitText.NS, "What border-size?", 0);
					Future recursievlyQuestion = dlg.ask(SplitText.NS, "Childrens too?", true);
					Boolean recursievly = recursievlyQuestion.get();
					Integer newSize = ask.get();
					if (newSize == null | recursievly == null) {
						return;
					}
					Set> nodesToImmutableIndexs = SetUtils.nodesToImmutableIndexs(splitters);
					BorderChangeHistoryStepData data = new BorderChangeHistoryStepData(newSize, nodesToImmutableIndexs,
							recursievly);
					HistoryStep step = new ChangeBorderStep(hst,
							hst.getCurrentDocument().getLastExecutedHistoryStep(), data);
					redo.actionPerformed(e);
				} catch (InterruptedException | ExecutionException e) {
					e.printStackTrace();
				}
			}
		}).start();
	}

	@Override
	public void notify(Set selection) {
		String notSplitNode = null;
		splitters.clear();
		for (VectorPublishNode vectorPublishNode : selection) {
			if (vectorPublishNode instanceof SplitNode) {
				splitters.add((SplitNode) vectorPublishNode);
			} else {
				notSplitNode = vectorPublishNode.toString();
			}
		}
		if (notSplitNode != null) {
			this.setName(SplitText.SELECTION_MUST_CONTAINS_SPLITTER_ONLY);
		} else {
			boolean allowed = true;
			for (BorderChangeCouncillor councillor : borderChangeCouncil) {
				allowed &= !councillor.changeBorderVeto(splitters);
			}
			setEnabled(allowed);
			if (allowed) {
				Set messages = new LinkedHashSet<>(0);
				for (BorderChangeCouncillor councillor : borderChangeCouncil) {
					messages.add(councillor.getStatement(splitters));
				}
				messages.remove(null);
				if (!messages.isEmpty()) {
					setTooltip(messages.iterator().next());
				}
			} else {
				setTooltip(SplitText.SPLIT_TOOLTIP);
			}
		}
	}

	@Override
	public boolean hasPopupItem(Set selection) {
		return isEnabled();
	}

	@Override
	public JMenuItem createPopupItem(Set selection) {
		return new JMenuItem(this);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy