Package DisplayProject

Source Code of DisplayProject.ErrorDialog$WindowFocus

/*
Copyright (c) 2003-2008 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.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.PrintWriter;
import java.io.StringWriter;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.border.BevelBorder;
import javax.swing.text.JTextComponent;
import javax.swing.text.TextAction;

/**
* Create an error dialog similar to the one that Forte provides, for when an un-handled exception reaches the top level of the application
*/
@SuppressWarnings("serial")
public class ErrorDialog extends JDialog {
    /** Minimum Width */
    static final int WIDTH = 300;
    /** Minimum Height */
    static final int HEIGHT = 130;

    /* Text for Details button */
    static final String EXP_TEXT = Messages.getString("ErrorDialog.0"); //$NON-NLS-1$
    static final String UNEXP_TEXT = Messages.getString("ErrorDialog.1"); //$NON-NLS-1$

    /* Text Areas */
    protected JTextArea extErrorText = new JTextArea(" "); //$NON-NLS-1$
    protected JTextArea errorText = new JTextArea(" "); //$NON-NLS-1$

    /* Right click menu for text area */
    protected JPopupMenu copyMenu = new JPopupMenu();
    protected JMenuItem copyText = new JMenuItem(Messages.getString("ErrorDialog.4")); //$NON-NLS-1$

    /* Buttons */
    protected JButton okBtn = new JButton(Messages.getString("ErrorDialog.5")); //$NON-NLS-1$
    protected JButton detailsBtn = new JButton(UNEXP_TEXT);

    /* Panels */
    protected JPanel basePanel = new JPanel();
    protected JPanel textPanel = new JPanel(new BorderLayout(2, 2));
    protected JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
    protected JPanel topPanel = new JPanel();
    protected JPanel extTextPanel = new JPanel(new GridLayout());
    protected JScrollPane extTextScroll = new JScrollPane(extErrorText);
    protected BoxLayout box = new BoxLayout(basePanel, BoxLayout.Y_AXIS);

    /** Used to indicate is the bottom/details portion is visible */
    protected boolean expanded = false;

    /** Owner/Application and dialog title */
    public ErrorDialog(Frame owner, String title) {
        super(owner, title);
        init();
    }
    public ErrorDialog(Frame owner, String title, Throwable t) {
        super(owner, title);
        init();
        setErrorText(t);
    }

    /** Initialise all components */
    private void init() {
        topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.Y_AXIS));
        errorText.setBackground(textPanel.getBackground());
        errorText.setForeground(java.awt.Color.black);
        errorText.setEditable(false);
        errorText.setEnabled(true);
        errorText.setFocusable(false);

        extErrorText.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
        extErrorText.setEditable(false);

        JLabel icon = new JLabel(UIManager.getIcon("OptionPane.errorIcon")); //$NON-NLS-1$
        icon.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
        errorText.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));

        copyMenu.add(copyText);
        copyText.addActionListener(new CopyButtonAction());

        textPanel.add(icon, BorderLayout.WEST);
        textPanel.add(errorText, BorderLayout.CENTER);
        topPanel.add(textPanel);
        extErrorText.setRows(5);
        extTextPanel.add(extTextScroll);
        extErrorText.addMouseListener(new ExtErrorCopier());

        detailsBtn.setEnabled(false);
        buttonPanel.add(okBtn);
        buttonPanel.add(detailsBtn);
        topPanel.add(buttonPanel);
        okBtn.addActionListener(new OKListener());
        detailsBtn.addActionListener(new DetailsListener());

        basePanel.setLayout(new BorderLayout());
//        basePanel.add(new JPanel());
//        basePanel.add(textPanel);
        basePanel.add(topPanel, BorderLayout.NORTH);
//        basePanel.add(new JPanel());
//        basePanel.add(buttonPanel);

        okBtn.setMnemonic('o');
        detailsBtn.setMnemonic('d');


        getContentPane().add(basePanel);
        getContentPane().transferFocus();

        addWindowListener(new WindowFocus());

        setResizable(false);
        setModal(true);
        setAlwaysOnTop(true);
    }

    /**
     * Call this method to set the text messages
     * @param t          Throwable that needs to be printed
     */
    public void setErrorText(Throwable t) {
        // TF:12/2/08:Null check
        if (t != null) {
            String msg = t.getMessage();
            if ((msg == null) || (msg.length()==0)) //$NON-NLS-1$
                msg = t.getClass().getName();
            this.setErrorText(msg, this.getExcText(t));
        }
    }
    /**
     * Call this method to set the text messages
     * @param text          The main message
     * @param extraText     The extended text, null and "" disable the details button
     */
    public void setErrorText(String text, String extraText) {
        StringBuilder tx = new StringBuilder(text);
        int offset = 0;
        for (int i = 0; i < tx.length(); i++){
            if ((offset > 60) && ((tx.charAt(i) == ' ') || (tx.charAt(i) == '.') || (tx.charAt(i) == ','))){
                offset = 0;
                tx.insert(i+1, '\n');
            } else {
                offset++;
            }
        }

        errorText.setText(tx.toString());
        if (extraText != null && !"".equals(extraText)) { //$NON-NLS-1$
            extErrorText.setText(extraText);
            detailsBtn.setEnabled(true);
        } else {
            detailsBtn.setEnabled(false);
        }
    }

    /** hard-coded minimum size */
    public Dimension getMinimumSize() {
        Dimension d = new Dimension(WIDTH, HEIGHT);
        return d;
    }

    /** Preferred size of the dialog down to a minimum size */
    public Dimension getPreferredSize() {
        Dimension sd = super.getPreferredSize();
        int w = sd.width;
        int h = sd.height;
        w = Math.max(w, WIDTH);
        h = Math.max(h, HEIGHT);
        Dimension d = new Dimension (w, h);
        return d;
    }

    /* used when resizing the dialog to include the extra details */
    private int lastPreferredSize = -1;

    /** change the state of the dialog to show details or not */
    private void setExpanded(boolean expand) {
        if (expand) {
            detailsBtn.setText(EXP_TEXT);
            basePanel.add(extTextPanel);
            extTextScroll.getHorizontalScrollBar().setValue(0);
            extTextScroll.getVerticalScrollBar().setValue(0);
            lastPreferredSize = extTextPanel.getPreferredSize().height;
            setSize(getWidth(), getHeight() + lastPreferredSize);
            setResizable(true);
        } else {
            setResizable(false);
            Component comps[] = extTextPanel.getComponents();
            boolean found = false;
            for( int i = 0; !found && i < comps.length; i++) {
                found = extTextScroll.equals(comps[i]);
            }
            if (found) {
                detailsBtn.setText(UNEXP_TEXT);
                basePanel.remove(extTextPanel);
                pack();
//                if (lastPreferredSize != -1) {
//                    setSize(getWidth(), getHeight() - lastPreferredSize);
//                }
                lastPreferredSize = -1;
            }
        }
        invalidate();
        validate();
    }

    public void setVisible(boolean visible) {
        expanded = false;
        setExpanded(expanded);
        this.pack();
        this.setLocationRelativeTo(this.getParent());

        super.setVisible(visible);
    }

    class ExtErrorCopier extends MouseAdapter {
        public void mouseReleased(MouseEvent e) {
            if(e.getClickCount() == 1 && e.getModifiers() == MouseEvent.BUTTON3_MASK) {
                copyMenu.show(extErrorText, e.getX(), e.getY());
            }
        }
    }

    /** OK button listener */
    class OKListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            setVisible(false);
        }
    }

    /** Details button listener */
    class DetailsListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            expanded = !expanded;
            setExpanded(expanded);
        }
    }

    /** Copy button listener */
    class CopyButtonAction extends TextAction implements ActionListener {
        public CopyButtonAction() {
            super(Messages.getString("ErrorDialog.9")); //$NON-NLS-1$
        }

        public void actionPerformed( ActionEvent evt ) {
            JTextComponent text = (JTextComponent)extErrorText;
            text.selectAll();
            text.copy();
            text.setCaretPosition(0);
        }
    }

    /** Window listener to set OK with the focus */
    class WindowFocus extends WindowAdapter {
        public void windowOpened(WindowEvent e) {
            okBtn.requestFocus();
        }
    }
    protected String getExcText(Throwable exc){
        if (exc == null) {
            return null;
        }
        StringWriter sw = new StringWriter();
        exc.printStackTrace(new PrintWriter(sw));
        StringBuilder txt = new StringBuilder(64);
        txt.append("Exception: ");
        txt.append(exc.getClass().getName());
        txt.append("\n");
        txt.append(new java.util.Date().toString());
        txt.append("\n");
        txt.append(sw.toString());
        return txt.toString();
    }

}
TOP

Related Classes of DisplayProject.ErrorDialog$WindowFocus

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.