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

net.vectorpublish.desktop.vp.api.ui.VPAbstractAction 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.api.ui;

import java.util.Locale.LanguageRange;

import javax.inject.Inject;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.Icon;

import net.vectorpublish.desktop.vp.api.VPAction;
import net.vectorpublish.desktop.vp.i8n.I8n;
import net.vectorpublish.desktop.vp.i8n.I8nText;
import net.vectorpublish.desktop.vp.i8n.LanguageChangeListener;
import net.vectorpublish.desktop.vp.ui.ImageKey;
import net.vectorpublish.desktop.vp.ui.Namespace;
import net.vectorpublish.desktop.vp.ui.i8n.I8nImageFactory;

public abstract class VPAbstractAction extends AbstractAction implements VPAction, LanguageChangeListener {

	@Inject
	private final I8nImageFactory imageFactory = null;
	@Inject
	private final I8n i = null;
	private I8nText tooltip;
	private I8nText name;

	public VPAbstractAction(I8nText name, I8nText tooltip, boolean disabled) {
		super();
		this.name = name;
		this.tooltip = tooltip;
		if (disabled) {
			super.setEnabled(false);
		}
	}

	protected Icon getIcon() {
		return (Icon) getValue(javax.swing.Action.SMALL_ICON);
	}

	public void setIcons(Namespace ns, final ImageKey imageKey) {
		putValue(javax.swing.Action.SMALL_ICON, imageFactory.get(ns, imageKey, true));
		putValue(javax.swing.Action.LARGE_ICON_KEY, imageFactory.get(ns, imageKey, false));
	}

	@Deprecated
	public void setIcons(Namespace ns, String key) {
		final ImageKey imageKey = ImageKey.get(key);
		setIcons(ns, imageKey);
	}

	@Deprecated
	protected void setName(String text) {
		super.putValue(javax.swing.Action.NAME, text);
	}

	public final void changedTo(LanguageRange newLanguage) {
		putValue(Action.SHORT_DESCRIPTION, i.getTranslation(tooltip));
		putValue(Action.NAME, i.getTranslation(name));
	};

	protected void setName(I8nText name) {
		this.name = name;
	}

	protected void setTooltip(I8nText tooltip) {
		this.tooltip = tooltip;
	}

	public  A waitForEnable(long max) throws RuntimeException {
		A self = (A) this;
		long waited = 0;
		while (!this.enabled) {
			try {
				Thread.sleep(40);
				waited += 40;
				if (waited > max) {
					throw new RuntimeException("Not available!");
				}
			} catch (InterruptedException e) {
				throw new RuntimeException(e);
			}
		}
		return self;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy