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

it.tidalwave.netbeans.dialog.DialogRunner Maven / Gradle / Ivy

There is a newer version: 2.13.61
Show newest version
/***********************************************************************************************************************
 *
 * OpenBlueSky - NetBeans Platform Enhancements
 * Copyright (C) 2006-2011 by Tidalwave s.a.s. (http://www.tidalwave.it)
 *
 ***********************************************************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations under the License.
 *
 ***********************************************************************************************************************
 *
 * WWW: http://openbluesky.kenai.com
 * SCM: https://kenai.com/hg/openbluesky~src
 *
 **********************************************************************************************************************/
package it.tidalwave.netbeans.dialog;

import it.tidalwave.netbeans.swing.OneShotMessagePanel;
import java.util.logging.Logger;
import java.util.prefs.Preferences;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import org.openide.util.Cancellable;
import org.openide.util.RequestProcessor;
import org.openide.DialogDescriptor;
import org.openide.DialogDisplayer;
import org.openide.ErrorManager;
import org.openide.NotifyDescriptor;
import org.openide.util.NbBundle;
import org.openide.util.NbPreferences;
import org.openide.windows.WindowManager;

/*******************************************************************************
 *
 * @author  Fabrizio Giudici
 * @version $Id$
 *
 ******************************************************************************/
public class DialogRunner 
  {
    private static final String CLASS = DialogRunner.class.getName();   
    private static final Logger logger = Logger.getLogger(CLASS);
    
    private static Dialog currentModalDialog;

    /***************************************************************************
     *
     *
     **************************************************************************/
    public static interface Task
      {
        public void run()
          throws Exception;
      }
    
    /***************************************************************************
     *
     *
     **************************************************************************/
    private DialogRunner() 
      {
      }
    
    /***************************************************************************
     *
     *
     **************************************************************************/
    public static Dialog getCurrentModalDialog()
      {
        return currentModalDialog;  
      }
    
    /***************************************************************************
     *
     * @return  true if 'Ok' was pressed, false if 'Cancel'
     *
     **************************************************************************/
    public static boolean runOkCancelModalDialog (final Object dialogPane, final String title)
      {
        final boolean[] code = new boolean[1];
        
        final DialogDescriptor dialogDescriptor = new DialogDescriptor(dialogPane, title, true, 
          new ActionListener() 
            {
              public void actionPerformed (final ActionEvent event) 
                {
                  try
                    {
                      code[0] = "OK".equals(event.getActionCommand());

                      if (code[0])
                        {
                          if (dialogPane instanceof OkDialogListener)
                            {
                              ((OkDialogListener)dialogPane).ok();  
                            }
                        }
                      else
                        {
                          if (dialogPane instanceof CancelDialogListener)
                            {
                              ((CancelDialogListener)dialogPane).cancel();  
                            }
                        }
                    }
                  catch (Exception e)
                    {
                      logger.throwing(CLASS, "", e);
                      notifyError(e);
                    }
                }
            });
          
        currentModalDialog = DialogDisplayer.getDefault().createDialog(dialogDescriptor);
        currentModalDialog.setResizable(false);
        currentModalDialog.setVisible(true);
        currentModalDialog = null;
        return code[0];
      }
    
    /***************************************************************************
     *
     *
     **************************************************************************/
    public static void runOkModalDialog (final Object dialogPane, final String title)
      {
        final DialogDescriptor dialogDescriptor = new DialogDescriptor(dialogPane, title, true, null);
        dialogDescriptor.setOptions(new Object[]{"Ok"});
        currentModalDialog = DialogDisplayer.getDefault().createDialog(dialogDescriptor);
        currentModalDialog.setResizable(false);
        currentModalDialog.setVisible(true);
        currentModalDialog = null;
      }
    
    /***************************************************************************
     *
     * @return  
     *
     **************************************************************************/
    public static void runTaskDialog (final Object dialogPane, final String title, final Task runnable)
      {
        internalRunTaskDialog(dialogPane, title, runnable);
      }
    
    /***************************************************************************
     *
     * @deprecated  Use the version with Task instead
     * @return  
     *
     **************************************************************************/
    public static void runTaskDialog (final Object dialogPane, final String title, final Runnable runnable)
      {
        internalRunTaskDialog(dialogPane, title, runnable);
      }
    
    /***************************************************************************
     *
     * @return  
     *
     **************************************************************************/
    private static void internalRunTaskDialog (final Object dialogPane, String title, final Object runnable)
      {
        final JButton cancelButton = new JButton("Cancel");
        cancelButton.setEnabled(runnable instanceof Cancellable);
        final Object[] options = new Object[]{ cancelButton };
        
        final DialogDescriptor dialogDescriptor = new DialogDescriptor(dialogPane, title, true, options, null, DialogDescriptor.DEFAULT_ALIGN, null,
          new ActionListener() 
            {
              public void actionPerformed (final ActionEvent event) 
                {
                  try
                    {
                      if (runnable instanceof Cancellable)
                        {
                          ((Cancellable)runnable).cancel();
                        }

                      if (dialogPane instanceof CancelDialogListener)
                        {
                          ((CancelDialogListener)dialogPane).cancel();  
                        }
                    }
                  catch (Exception e)
                    {
                      logger.throwing(CLASS, "", e);
                      notifyError(e);
                    }
                }
            });
          
        currentModalDialog = DialogDisplayer.getDefault().createDialog(dialogDescriptor);
        final Dialog theDialog = currentModalDialog;
        
        RequestProcessor.getDefault().post(new Runnable() 
          {
            public void run() 
              {
                try 
                  {
                    if (runnable instanceof Runnable)
                      {
                        ((Runnable)runnable).run();
                      }
                    
                    else if (runnable instanceof Task)
                      {
                        ((Task)runnable).run();
                      }
                  }  
                catch (Throwable t) 
                  {
                    logger.warning("Task completed by error");
                    notifyError(t);
                  }
                
                logger.info("Task completed, closing the progress dialog");
                theDialog.setResizable(false);
                theDialog.setVisible(false);
                theDialog.dispose();
              }
          });
          
        currentModalDialog.setVisible(true);
        currentModalDialog = null;
      }

    /***************************************************************************
     *
     * Use this instead of calling ErrorManager.notify() as this method would
     * first close a modal dialog, if opened. Otherwise the exception dialog 
     * could not appear.
     *
     **************************************************************************/
    public static void notifyError (final Throwable t)
      {
        logger.throwing(CLASS, "notifyError()", t);
        
        if ((currentModalDialog != null) && currentModalDialog.isVisible())
          {
            currentModalDialog.dispose();
            currentModalDialog = null;
          }
        
        ErrorManager.getDefault().notify(t);
      }
    
    /***************************************************************************
     *
     * Shows a message that by default should be shown only once (the user can
     * change the selection on a checkbox to change this).
     * 
     * @param  message  the message to show
     * @param  title    the title to show
     * @param  clazz    the class invoking this dialog
     * @param  id       the identifier of the message 
     *
     **************************************************************************/
    public static void showOneShotMessage (final String message, 
                                           final String title, 
                                           final Class clazz, 
                                           final String id)
      {
        final Preferences preferences = NbPreferences.forModule(clazz);
        final String propertyName = id + ".show";
        
        if (preferences.getBoolean(propertyName, true))
          {
            final JDialog dialog = new JDialog(WindowManager.getDefault().getMainWindow(), title, true);
            final OneShotMessagePanel messagePanel = new OneShotMessagePanel(message, dialog);
            dialog.setContentPane(messagePanel);
            dialog.pack();
            final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            final Point point = new Point((screenSize.width - dialog.getWidth()) / 2,
                                          (screenSize.height - dialog.getHeight()) / 2);
            dialog.setLocation(point);
            dialog.setResizable(false);
            dialog.setVisible(true);          
            preferences.putBoolean(propertyName, messagePanel.isShowNextTimeSelected());
          }
      }
    
    /***************************************************************************
     *
     * 
     **************************************************************************/
    public static void showWarning (final String message, final String title)
      {
        showWarning(message);
      }
    
    /***************************************************************************
     *
     * 
     **************************************************************************/
    public static void showWarning (final String message)
      {
        DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(message, NotifyDescriptor.WARNING_MESSAGE));        
      }
    
    /***************************************************************************
     *
     * 
     **************************************************************************/
    public static void showWarning (final String message1, final String message2, final String title)
      {
        final String message = "" + message1 + "
" + message2 + ""; showWarning(message, title); } /*************************************************************************** * * **************************************************************************/ public static void showWarning (final Class clazz, final String resourceName) { final String message1 = NbBundle.getMessage(clazz, resourceName + ".message1"); final String message2 = NbBundle.getMessage(clazz, resourceName + ".message2"); final String title = NbBundle.getMessage(clazz, resourceName + ".title"); showWarning(message1, message2, title); } /*************************************************************************** * * **************************************************************************/ public static void showError (final String message, final String title) { showError(message); } /*************************************************************************** * * **************************************************************************/ public static void showError (final String message) { DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(message, NotifyDescriptor.ERROR_MESSAGE)); } /*************************************************************************** * * **************************************************************************/ public static void showError (final String message1, final String message2, final String title) { final String message = "" + message1 + "
" + message2 + ""; showError(message, title); } /*************************************************************************** * * **************************************************************************/ public static void showError (final Class clazz, final String resourceName) { final String message1 = NbBundle.getMessage(clazz, resourceName + ".message1"); final String message2 = NbBundle.getMessage(clazz, resourceName + ".message2"); final String title = NbBundle.getMessage(clazz, resourceName + ".title"); showError(message1, message2, title); } /*************************************************************************** * * **************************************************************************/ public static void showMessage (final String message, final String title) { showMessage(message); } /*************************************************************************** * * Show an informational message. * * @param message the message to show * **************************************************************************/ public static void showMessage (final String message) { DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(message, NotifyDescriptor.INFORMATION_MESSAGE)); } /*************************************************************************** * * Show a confirmation message with a couple of "OK/Cancel" buttons. * * @param message the message to show * @param title the title to show * @return if "Ok" has been pressed * **************************************************************************/ public static boolean showOkCancelConfirmation (final String message, final String title) { return showConfirmation(title, message, NotifyDescriptor.OK_CANCEL_OPTION, NotifyDescriptor.OK_OPTION); } /*************************************************************************** * * Show a confirmation message with a couple of "Ok/Cancel" buttons. * * @param message1 the message to show in bold * @param message2 the message to show in small * @param title the title to show * @return if "Ok" has been pressed * **************************************************************************/ public static boolean showOkCancelConfirmation (final String message1, final String message2, final String title) { final String message = "" + message1 + "
" + message2 + ""; return showConfirmation(title, message, NotifyDescriptor.OK_CANCEL_OPTION, NotifyDescriptor.OK_OPTION); } /*************************************************************************** * * Show a confirmation message with a couple of "Ok/Cancel" buttons. * **************************************************************************/ public static boolean showOkCancelConfirmation (final Class clazz, final String resourceName) { final String message1 = NbBundle.getMessage(clazz, resourceName + ".message1"); final String message2 = NbBundle.getMessage(clazz, resourceName + ".message2"); final String title = NbBundle.getMessage(clazz, resourceName + ".title"); return showOkCancelConfirmation(message1, message2, title); } /*************************************************************************** * * Show a confirmation message with a couple of "Yes/No" buttons. * * @param message the message to show * @param title the title to show * @return if "Ok" has been pressed * **************************************************************************/ public static boolean showYesNoConfirmation (final String message, final String title) { return showConfirmation(title, message, NotifyDescriptor.YES_NO_OPTION, NotifyDescriptor.YES_OPTION); } /*************************************************************************** * * Show a confirmation message with a couple of "Yes/No" buttons. * * @param message1 the message to show in bold * @param message2 the message to show in small * @param title the title to show * @return if "Ok" has been pressed * **************************************************************************/ public static boolean showYesNoConfirmation (final String message1, final String message2, final String title) { final String message = "" + message1 + "
" + message2 + ""; return showConfirmation(title, message, NotifyDescriptor.YES_NO_OPTION, NotifyDescriptor.YES_OPTION); } /*************************************************************************** * * Show a confirmation message with a couple of "Yes/No" buttons. * **************************************************************************/ public static boolean showYesNoConfirmation (final Class clazz, final String resourceName) { final String message1 = NbBundle.getMessage(clazz, resourceName + ".message1"); final String message2 = NbBundle.getMessage(clazz, resourceName + ".message2"); final String title = NbBundle.getMessage(clazz, resourceName + ".title"); return showYesNoConfirmation(message1, message2, title); } /*************************************************************************** * **************************************************************************/ private static boolean showConfirmation (final String title, final String message, final int mode, final Object positiveResponse) { final NotifyDescriptor d = new NotifyDescriptor.Confirmation(message, title, mode); return DialogDisplayer.getDefault().notify(d) == positiveResponse; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy