
net.vectorpublish.desktop.vp.WarnDialogImpl 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.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.LayoutManager;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedHashSet;
import java.util.Locale.LanguageRange;
import java.util.Set;
import javax.inject.Inject;
import javax.inject.Named;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import net.vectorpublish.desktop.vp.api.conf.Config;
import net.vectorpublish.desktop.vp.api.ui.Warn;
import net.vectorpublish.desktop.vp.i8n.I8n;
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;
@Named
public class WarnDialogImpl extends JDialog implements LayoutManager, ActionListener, Warn, LanguageChangeListener {
private static final int HEADER_HEIGHT = 30;
private static final Namespace NS_DIALOG_WARNING = Namespace.getNamespace("system.ui", "dialog.warning");
private static final int BUTTON_HEIGHT = 50;
private final JLabel label = new JLabel("Warning!");
private final Set warnings = new LinkedHashSet<>();
@Inject
private final Config conf = null;
public final static Namespace NS = Namespace.getNamespace("net.vectorpublish", "dialogs");
@Inject
private final I8nImageFactory imageFactory = null;
/**
* Injects a CDI-Bean.
*/
@Inject
private final I8n i8n = null;
/**
* The default-constructor for the {@link WarnDialogImpl dialog}.
*
* Defines the basic layout of the dialog.
*/
public WarnDialogImpl() {
super();
setTitle("Warning!");
setModal(true);
final Container c = getContentPane();
c.setLayout(this);
c.add(label);
}
@Override
public void actionPerformed(ActionEvent e) {
final JButton jb = (JButton) e.getSource();
getContentPane().remove(jb);
warnings.remove(jb);
updateStatus();
}
@Override
public void addLayoutComponent(String name, Component comp) {
}
@Override
public void addWarning(String message) {
final JButton warningButton = new JButton(message, imageFactory.get(NS, ImageKey.get("warning"), false));
warningButton.addActionListener(this);
getContentPane().add(warningButton);
warnings.add(warningButton);
updateStatus();
}
@Override
public void changedTo(LanguageRange lr) {
label.setText(i8n.getTranslation(I8nDialog.WARN_LABEL));
setTitle(i8n.getTranslation(I8nDialog.WARN_TITLE));
}
@Override
public void layoutContainer(Container parent) {
label.setBounds(0, 0, getContentPane().getWidth(), HEADER_HEIGHT);
int index = -1;
for (final JButton btn : warnings) {
index++;
btn.setBounds(0, HEADER_HEIGHT + index * BUTTON_HEIGHT, getContentPane().getWidth(), BUTTON_HEIGHT);
}
}
@Override
public Dimension minimumLayoutSize(Container parent) {
return getSize();
}
@Override
public Dimension preferredLayoutSize(Container parent) {
return getSize();
}
@Override
public void removeLayoutComponent(Component comp) {
}
private void updateStatus() {
if (warnings.isEmpty()) {
if (isVisible()) {
conf.storeBounds(NS_DIALOG_WARNING, "bounds", getBounds());
setVisible(false);
}
doLayout();
} else {
if (!isVisible()) {
Rectangle loadBounds = conf.loadBounds(NS_DIALOG_WARNING, "bounds");
if (loadBounds == null) {
loadBounds = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
}
setBounds(loadBounds);
setVisible(true);
}
doLayout();
}
}
}