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

net.vectorpublish.desktop.vp.DefaultDialogImpl 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.BorderLayout;
import java.awt.Container;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.util.concurrent.Future;

import javax.inject.Inject;
import javax.inject.Named;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.filechooser.FileFilter;

import org.springframework.scheduling.annotation.AsyncResult;

import net.vectorpublish.desktop.vp.api.conf.Config;
import net.vectorpublish.desktop.vp.api.ui.Dialog;
import net.vectorpublish.desktop.vp.api.ui.UserInterface;
import net.vectorpublish.desktop.vp.impl.AskDialog;
import net.vectorpublish.desktop.vp.impl.ConfirmResultHolder;
import net.vectorpublish.desktop.vp.log.Log;
import net.vectorpublish.desktop.vp.ui.Namespace;

@Named
public class DefaultDialogImpl implements Dialog {

	private static final String DEFAULT_CHECKBOX_TEXT = "";
	private static final Namespace FILE_OPEN_NAMESPACE = Namespace.getNamespace("net.vectorpublish.dialogs", "file.open");
	private static final Namespace FILE_SAVE_NAMESPACE = Namespace.getNamespace("net.vectorpublish.dialogs", "file.save");

	@Inject
	private final UserInterface ui = null;

	@Inject
	private final Config conf = null;

	@Inject
	private final Log log = null;

	private AskDialog ask = null;

	{
		try {
			SwingUtilities.invokeAndWait(new Runnable() {
				@Override
				public void run() {
					ask = new AskDialog();
				}
			});
		} catch (InvocationTargetException | InterruptedException e) {
			e.printStackTrace();
		}
	}

	@Override
	public  Future ask(Namespace contextNamespace, String question, A... proposal) {
		return ask(contextNamespace, DEFAULT_CHECKBOX_TEXT, question, proposal);
	}

	public  Future ask(Namespace contextNamespace, String dialogTitle, String question, A... proposal) {
		try {
			SwingUtilities.invokeAndWait(new Runnable() {
				@Override
				public void run() {
					ask.addQuestion(dialogTitle, question, proposal);
				}
			});
			SwingUtilities.invokeAndWait(new Runnable() {
				@Override
				public void run() {
					Rectangle bounds = conf.loadBounds(contextNamespace, "dialog");
					if (bounds == null) {
						bounds = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
					}
					ask.setBounds(bounds);
					if (!ask.isVisible()) {
						log.show("Ask dialog.");
					}
					ask.setVisible(true);
				}
			});
			while (ask.isVisible()) {
				Thread.sleep(200);
			}
			conf.storeBounds(contextNamespace, "dialog", ask.getBounds());
			return new AsyncResult<>((A) ask.removeResult(question));
		} catch (InvocationTargetException | InterruptedException e) {
			e.printStackTrace();
		}
		return new AsyncResult<>(proposal[0]);
	}

	@Override
	public Future confirm(Namespace namespace, String title, String question, String yesLabel, String noLabel) {
		final ConfirmResultHolder mon = new ConfirmResultHolder();
		try {
			SwingUtilities.invokeAndWait(new Runnable() {
				@Override
				public void run() {
					final JDialog dialog = new JDialog(ui, title, true);
					Rectangle bounds = conf.loadBounds(namespace, "confirmationBounds");
					if (bounds == null) {
						bounds = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
						final int halfWidth = bounds.width;
						if (halfWidth > 200) {
							bounds.x += (halfWidth - 200) / 2;
							bounds.width = 200;
						}
						final int halfHeight = bounds.height;
						if (halfHeight > 100) {
							bounds.y = (halfHeight - 200) / 2;
							bounds.height = 100;
						}
					}
					dialog.setBounds(bounds);
					final Container container = dialog.getContentPane();
					container.setLayout(new BorderLayout());
					final JPanel panel = new JPanel();
					panel.setLayout(new BorderLayout(5, 5));
					final JButton noButton = new JButton(new AbstractAction(noLabel) {

						@Override
						public void actionPerformed(ActionEvent e) {
							dialog.setVisible(false);
							mon.setResult(false);
							conf.storeBounds(namespace, "confirmationBounds", dialog.getBounds());
						}
					});
					panel.add(noButton, BorderLayout.WEST);
					final JButton yesButton = new JButton(new AbstractAction(yesLabel) {

						@Override
						public void actionPerformed(ActionEvent e) {
							dialog.setVisible(false);
							mon.setResult(true);
							conf.storeBounds(namespace, "confirmationBounds", dialog.getBounds());
						}
					});
					panel.add(yesButton, BorderLayout.EAST);
					container.add(panel, BorderLayout.SOUTH);
					container.add(new JLabel(question), BorderLayout.CENTER);
					dialog.pack();
					dialog.setVisible(true);
				}
			});
		} catch (final InvocationTargetException e) {
			e.printStackTrace();
		} catch (final InterruptedException e) {
			e.printStackTrace();
		}
		while (!mon.hasResult()) {
			try {
				Thread.sleep(200);
			} catch (final InterruptedException e) {
				e.printStackTrace();
			}
		}
		return new AsyncResult<>(mon.getResult());
	}

	@Override
	public File showOpenFile(String fileExtension, String fileFormatDescription) {
		final JFileChooser fc = new JFileChooser();
		fc.setFileFilter(new FileFilter() {

			@Override
			public boolean accept(File f) {
				if (!f.isFile()) {
					return true;
				}
				return f.getName().endsWith(fileExtension);
			}

			@Override
			public String getDescription() {
				return fileFormatDescription + " (" + fileExtension + ")";
			}
		});
		Rectangle bounds = conf.loadBounds(FILE_OPEN_NAMESPACE, fileExtension);
		if (bounds == null) {
			bounds = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
		}
		fc.setBounds(bounds);
		final String absoluteFilename = conf.read(FILE_OPEN_NAMESPACE, fileExtension);
		fc.addPropertyChangeListener(new PropertyChangeListener() {

			@Override
			public void propertyChange(PropertyChangeEvent evt) {
				String propertyName = evt.getPropertyName();
				if (JFileChooser.DIRECTORY_CHANGED_PROPERTY.equals(propertyName)) {
					fc.setDialogTitle(fc.getCurrentDirectory().getPath());
				}
			}
		});
		if (absoluteFilename != null) {
			final File file = new File(absoluteFilename);
			if (file.exists() && file.canRead()) {
				fc.setSelectedFile(file);
			} else if (file.getParentFile().exists()) {
				fc.setSelectedFile(file.getParentFile());
			}
		}
		final int open = fc.showOpenDialog(ui);
		if (open == JFileChooser.APPROVE_OPTION) {
			conf.storeBounds(FILE_OPEN_NAMESPACE, fileExtension, fc.getBounds());
			final File selectedFile = fc.getSelectedFile();
			conf.write(FILE_OPEN_NAMESPACE, fileExtension, selectedFile.getAbsolutePath());
			return selectedFile;
		}
		return null;
	}

	@Override
	public File showSaveFile(String fileExtension, String fileFormatDescription) {
		final JFileChooser fc = new JFileChooser();
		fc.setFileFilter(new FileFilter() {

			@Override
			public boolean accept(File f) {
				if (!f.isFile()) {
					return true;
				}
				return f.getName().endsWith(fileExtension);
			}

			@Override
			public String getDescription() {
				return fileFormatDescription + " (" + fileExtension + ")";
			}
		});

		final String absoluteFilename = conf.read(FILE_SAVE_NAMESPACE, fileExtension);
		if (absoluteFilename != null) {
			final File file = new File(absoluteFilename);
			if (file.exists() && file.canRead()) {
				fc.setSelectedFile(file);
			} else if (file.getParentFile().exists()) {
				fc.setSelectedFile(file.getParentFile());
			}
		}

		Rectangle bounds = conf.loadBounds(FILE_SAVE_NAMESPACE, fileExtension);
		if (bounds == null) {
			bounds = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
		}
		fc.setBounds(bounds);
		final int save = fc.showSaveDialog(ui);
		if (save == JFileChooser.APPROVE_OPTION) {
			conf.storeBounds(FILE_SAVE_NAMESPACE, fileExtension, fc.getBounds());
			String newAbsoluteFilename = fc.getSelectedFile().getAbsolutePath();
			if (!newAbsoluteFilename.endsWith("." + fileExtension)) {
				newAbsoluteFilename += "." + fileExtension;
			}
			final File file = new File(newAbsoluteFilename);
			conf.write(FILE_SAVE_NAMESPACE, fileExtension, file.getAbsolutePath());
			return file;
		}
		return null;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy