Package DisplayProject

Source Code of DisplayProject.DialogOptionPane

/*
Copyright (c) 2003-2009 ITerative Consulting Pty Ltd. All Rights Reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:

o Redistributions of source code must retain the above copyright notice, this list of conditions and
the following disclaimer.
 
o Redistributions in binary form must reproduce the above copyright notice, this list of conditions
and the following disclaimer in the documentation and/or other materials provided with the distribution.
   
o This jcTOOL Helper Class software, whether in binary or source form may not be used within,
or to derive, any other product without the specific prior written permission of the copyright holder

 
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


*/
package DisplayProject;

import java.awt.Component;
import java.awt.Container;
import java.awt.HeadlessException;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.Locale;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;

@SuppressWarnings("serial")
public class DialogOptionPane extends JOptionPane {
    public static final String WARNING;
    public static final String QUESTION;
    public static final String ERROR;
    public static final String INFORMATION;
    private static final int  MAX_CHARS_PER_LINE = 50;
//    private static final char  NEWLINE = '\n';
//    private static final String  SPACE  = " ";
    private static final Integer OK_SELECTED    = new Integer(OK_OPTION);
    private static final Integer CANCEL_SELECTED = new Integer(CANCEL_OPTION);
    private static final Integer NO_SELECTED    = new Integer(NO_OPTION);
    // Array of button text indexed by option
    private static final String[] BUTTON_TEXT = new String[] {null, "No", "Cancel"};
    static {
      if (Locale.getDefault().getLanguage().equals(Locale.FRENCH.getLanguage())){
        WARNING = "Avertissement";
        QUESTION = "Question";
        ERROR = "Erreur";
        INFORMATION = "Information";
      } else if (Locale.getDefault().getLanguage().equals(Locale.ITALIAN.getLanguage())){
        WARNING = "Attenzione";
        QUESTION = "Domanda";
        ERROR = "Errore";
        INFORMATION = "Informazioni";
      } else if (Locale.getDefault().getLanguage().equals(Locale.GERMAN.getLanguage())){
        WARNING = "Warnung";
        QUESTION = "Anfrage";
        ERROR = "Fehler";
        INFORMATION = "Informationen";
//      } else if (Locale.getDefault().getLanguage().equals(Locale.JAPANESE.getLanguage())){
//        WARNING = "";
//        QUESTION = "";
//        ERROR = "";
//        INFORMATION = "";
      } else {
        WARNING = "Warning";
        QUESTION = "Question";
        ERROR = "Error";
        INFORMATION = "Information";
      }
     
    }

    public static int showOptionDialog(Component parentComponent, String message, int messageType, int maxCharsPerLine) {
        String title;
        switch (messageType) {
            case JOptionPane.WARNING_MESSAGE:
                title = WARNING;
                break;
            case JOptionPane.QUESTION_MESSAGE:
                title = QUESTION;
                break;
            case JOptionPane.ERROR_MESSAGE:
                title = ERROR;
                break;
            default:
                title = INFORMATION;
                break;
        }
        return showOptionDialog(parentComponent, message, title, JOptionPane.DEFAULT_OPTION, messageType, JOptionPane.OK_OPTION, maxCharsPerLine);
    }

    public static int showOptionDialog(Component parentComponent, String message, String title, int optionType, int messageType, int initialOption) throws HeadlessException {
        return showOptionDialog(parentComponent, message, title, optionType, messageType, initialOption, MAX_CHARS_PER_LINE);
    }

    public static int showOptionDialog(Component parentComponent, String message, String title, int optionType, int messageType, int initialOption, int maxCharsPerLine) throws HeadlessException {
        //PM:14/05/2008: fix text wrap
            JOptionPane pane   = new JOptionPane(message/*reformatMsg(message, maxCharsPerLine)*/, messageType, optionType);
            final JDialog dialog = pane.createDialog(parentComponent, title);

            // Only need to override default if not first button
            if (initialOption>0) {
                String defaultButtonText = BUTTON_TEXT[initialOption];
                final JButton defaultButton = findButtonByName(pane, defaultButtonText);
               
                if (defaultButton != null) {
                 
                  // CraigM: 17/07/2008 - Set the focus only when the window is shown.  Ref: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5018574
                  dialog.addComponentListener(new ComponentAdapter() {
                    @Override
                    public void componentShown(ComponentEvent e) {
                            dialog.getRootPane().setDefaultButton(defaultButton);
                            defaultButton.requestFocus();
                    }
                  });
                }
            }

            // TF Add this window into the window manager as a managed, application modal window.

            // MSS: But only manage while it's visible, and run on the AWT thread, so that deregistration
            // happens before the underlying window is activated.
            UIutils.invokeOnGuiThread(new Runnable(){
                public void run() {
                    WindowManager.registerWindow(dialog, true);
                    dialog.setVisible(true);
                    WindowManager.deregisterWindow(dialog);
                }
            });

//          DC Added test for instanceof integer -
//          sometimes was getting ClassCastException when getValue returned UNINITIALIZED_VALUE 
            Integer selectedValue = null;
            if (pane.getValue() instanceof Integer) {
                selectedValue = (Integer)pane.getValue();
            }
            // If user hit dialog close button, treat like a negative response
            if (selectedValue==null || selectedValue.intValue()==CLOSED_OPTION) {
                switch (optionType) {
                    case YES_NO_OPTION:
                        selectedValue = NO_SELECTED;
                        break;
                    case DEFAULT_OPTION:
                        selectedValue = OK_SELECTED;  // No negative to pick
                        break;
                    default:
                        selectedValue = CANCEL_SELECTED;
                        break;
                }
            }
            return selectedValue.intValue();
    }

    private static JButton findButtonByName(Container c, final String buttonText) {
        JButton jb = null;
        for (int i=0; i<c.getComponentCount() && jb==null; i++) {
            Component comp = c.getComponent(i);
            if (comp instanceof JButton && buttonText.equals(((JButton)comp).getText())) {
                jb = (JButton)comp;
            }
            else if (comp instanceof Container) {
                jb = findButtonByName((Container)comp, buttonText);
            }
        }
        return jb;
    }
    //PM:14/05/2008: find correct button using a constant
//    private static JButton findButton(Container c, int buttonSet, int buttonValue) {
//      String buttonText = "";
//      switch (buttonSet){
//      case Constants.BS_OKCANCEL:
//        switch (buttonValue){
//        case Constants.BV_OK:
//          buttonText = Constants.OK_CANCEL_OPTIONS[0];
//          break;
//        case Constants.BV_CANCEL:
//          buttonText = Constants.OK_CANCEL_OPTIONS[1];
//          break;
//        }
//        break;
//      case Constants.BS_YESNO:
//        switch (buttonValue){
//        case Constants.BV_YES:
//          buttonText = Constants.YES_NO_OPTIONS[0];
//          break;
//        case Constants.BV_NO:
//          buttonText = Constants.YES_NO_OPTIONS[1];
//          break;
//        }
//        break;
//      case Constants.BS_YESNOCANCEL:
//        switch (buttonValue){
//        case Constants.BV_YES:
//          buttonText = Constants.YES_NO_CANCEL_OPTIONS[0];
//          break;
//        case Constants.BV_NO:
//          buttonText = Constants.YES_NO_CANCEL_OPTIONS[1];
//          break;
//        case Constants.BV_CANCEL:
//          buttonText = Constants.YES_NO_CANCEL_OPTIONS[2];
//          break;
//        }
//        break;
//      }
//      JButton jb = null;
//        for (int i=0; i<c.getComponentCount() && jb==null; i++) {
//            Component comp = c.getComponent(i);
//            if (comp instanceof JButton && buttonText.equals(((JButton)comp).getText())) {
//                jb = (JButton)comp;
//            }
//            else if (comp instanceof Container) {
//                jb = findButtonByName((Container)comp, buttonText);
//            }
//        }
//        return jb;
//    }
//    // Reformat the message to wrap each line of text at MAX_CHARS_PER_LINE where a line is
//    // delimited by the newline character.
//    private static String reformatMsg(String pMsg, int pMaxCharsPerLine) {
//        StringBuilder sb = new StringBuilder();
//        String[] splitMsg = pMsg.split(Character.toString(NEWLINE));
//        for (int i = 0; i < splitMsg.length; i++) {
//            String s = splitMsg[i];
//            sb.append(reformat(s, pMaxCharsPerLine));
//            sb.append(NEWLINE);
//        }
//        sb.deleteCharAt(sb.length()-1);
//        return sb.toString();
//    }
//    // If the line length exceeds MAX_CHARS_PER_LINE, replace spaces with newline characters
//    private static String reformat(String pMsgPart, int pMaxCharsPerLine) {
//        StringBuilder sb = new StringBuilder(pMsgPart);
//        int searchPos = pMaxCharsPerLine;
//        while (searchPos<sb.length()) {
//            int lastSpaceIndex = sb.lastIndexOf(SPACE, searchPos);
//            if (lastSpaceIndex==-1) break;
//            sb.setCharAt(lastSpaceIndex, NEWLINE);
//            searchPos = lastSpaceIndex+pMaxCharsPerLine;
//        }
//        return sb.toString();
//    }
}
TOP

Related Classes of DisplayProject.DialogOptionPane

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.