001 /******************************************************************************
002 * Copyright (C) MActor Developers. All rights reserved. *
003 * ---------------------------------------------------------------------------*
004 * This file is part of MActor. *
005 * *
006 * MActor is free software; you can redistribute it and/or modify *
007 * it under the terms of the GNU General Public License as published by *
008 * the Free Software Foundation; either version 2 of the License, or *
009 * (at your option) any later version. *
010 * *
011 * MActor is distributed in the hope that it will be useful, *
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
014 * GNU General Public License for more details. *
015 * *
016 * You should have received a copy of the GNU General Public License *
017 * along with MActor; if not, write to the Free Software *
018 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
019 ******************************************************************************/
020 package org.mactor.ui.gui;
021
022 import java.awt.Component;
023 import java.awt.Cursor;
024 import java.awt.event.ActionEvent;
025
026 import javax.swing.AbstractAction;
027 import javax.swing.JOptionPane;
028 import javax.swing.SwingUtilities;
029
030 import org.mactor.framework.MactorException;
031
032 public class AsyncAction extends AbstractAction {
033 AsyncRunnable r;
034 Component source;
035 boolean confirm;
036 public AsyncAction(String name, boolean confirm, AsyncRunnable r) {
037 super(name);
038 this.r = r;
039 this.confirm = confirm;
040 }
041 public void actionPerformed(ActionEvent e) {
042 if (confirm) {
043 if (JOptionPane.YES_OPTION != JOptionPane.showConfirmDialog(null, "Are you sure you want to perfom '" + getValue(AbstractAction.NAME) + "' ?"))
044 return;
045 }
046 setEnabled(false);
047 if (e.getSource() instanceof Component && ((Component) e.getSource()).getParent() != null)
048 source = ((Component) e.getSource()).getParent();
049 if (source != null)
050 source.setCursor(new Cursor(Cursor.WAIT_CURSOR));
051 SwingUtilities.invokeLater(new Runnable() {
052 public void run() {
053 try {
054 try {
055 r.run();
056 } catch (MactorException me) {
057 GuiUtil.showGuiError(source, me);
058 }
059 } finally {
060 setEnabled(true);
061 if (source != null)
062 source.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
063 }
064 }
065 });
066 }
067 public static interface AsyncRunnable {
068 void run() throws MactorException;
069 }
070 }