001/*
002 * Copyright 2010-2015 Institut Pasteur.
003 * 
004 * This file is part of Icy.
005 * 
006 * Icy 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 3 of the License, or
009 * (at your option) any later version.
010 * 
011 * Icy 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 Icy. If not, see <http://www.gnu.org/licenses/>.
018 */
019package icy.gui.plugin;
020
021import icy.gui.frame.error.ErrorReportFrame;
022import icy.plugin.PluginDescriptor;
023import icy.system.IcyExceptionHandler;
024import icy.util.StringUtil;
025
026import java.awt.BorderLayout;
027import java.awt.Color;
028import java.awt.Dimension;
029import java.awt.event.MouseAdapter;
030import java.awt.event.MouseEvent;
031
032import javax.swing.JButton;
033import javax.swing.JLabel;
034import javax.swing.JPanel;
035import javax.swing.JScrollPane;
036import javax.swing.JTextPane;
037import javax.swing.SwingConstants;
038import javax.swing.border.TitledBorder;
039import javax.swing.text.BadLocationException;
040import javax.swing.text.Document;
041import javax.swing.text.SimpleAttributeSet;
042import javax.swing.text.StyleConstants;
043
044/**
045 * @deprecated Use {@link ErrorReportFrame} instead
046 */
047@Deprecated
048public class PluginErrorReportPanel extends JPanel
049{
050    /**
051     * 
052     */
053    private static final long serialVersionUID = -2875740914347175762L;
054
055    // GUI
056    JTextPane errorMessageTextPane;
057    JLabel label;
058    JTextPane commentTextPane;
059    JButton reportButton;
060    JButton closeButton;
061    JPanel bottomPanel;
062    JScrollPane messageScrollPane;
063    JPanel commentPanel;
064    JPanel messagePanel;
065
066    final PluginDescriptor plugin;
067    final String devId;
068    final String title;
069    final String message;
070
071    public PluginErrorReportPanel(PluginDescriptor plugin, String devId, String title, String message)
072    {
073        super();
074
075        this.plugin = plugin;
076        this.devId = devId;
077        this.title = message;
078        this.message = message;
079
080        initialize();
081
082        String str;
083
084        if (plugin != null)
085            str = "<html><br>The plugin named <b>" + plugin.getName() + "</b> has encountered a problem";
086        else
087            str = "<html><br>The plugin from the developer <b>" + devId + "</b> has encountered a problem";
088
089        if (StringUtil.isEmpty(title))
090            str += ".<br><br>";
091        else
092            str += " :<br><i>" + title + "</i><br><br>";
093
094        str += "Reporting this problem is anonymous and will help improving this plugin.<br><br></html>";
095
096        label.setText(str);
097
098        try
099        {
100            errorMessageTextPane.getStyledDocument().insertString(errorMessageTextPane.getStyledDocument().getLength(),
101                    message, new SimpleAttributeSet());
102        }
103        catch (BadLocationException e)
104        {
105            System.err.println("PluginErrorReport(...) error :");
106            IcyExceptionHandler.showErrorMessage(e, true);
107        }
108        errorMessageTextPane.setCaretPosition(0);
109
110        final Document doc = commentTextPane.getDocument();
111
112        try
113        {
114            SimpleAttributeSet attributes = new SimpleAttributeSet();
115            StyleConstants.setItalic(attributes, true);
116            StyleConstants.setForeground(attributes, Color.GRAY);
117            doc.insertString(0, "Please type here your comment", attributes);
118        }
119        catch (BadLocationException e1)
120        {
121
122        }
123
124        commentTextPane.addMouseListener(new MouseAdapter()
125        {
126            // Displays a message at the beginning that
127            // disappears when first clicked
128            boolean firstClickDone = false;
129
130            @Override
131            public void mouseClicked(MouseEvent e)
132            {
133                if (!firstClickDone)
134                {
135                    commentTextPane.setText("");
136
137                    SimpleAttributeSet attributes = new SimpleAttributeSet();
138                    StyleConstants.setItalic(attributes, false);
139                    StyleConstants.setForeground(attributes, Color.BLACK);
140                    try
141                    {
142                        doc.insertString(0, " ", attributes);
143                    }
144                    catch (BadLocationException e1)
145                    {
146                    }
147
148                    firstClickDone = true;
149                }
150            }
151        });
152    }
153
154    /**
155     * @wbp.parser.constructor
156     */
157    PluginErrorReportPanel()
158    {
159        this(new PluginDescriptor(plugins.kernel.canvas.Canvas2DPlugin.class), null, null, "Error !!");
160    }
161
162    private void initialize()
163    {
164        if (plugin != null)
165            label = new JLabel("", plugin.getIcon(), SwingConstants.CENTER);
166        else
167            label = new JLabel("", SwingConstants.CENTER);
168
169        // center
170        errorMessageTextPane = new JTextPane();
171        errorMessageTextPane.setEditable(false);
172        errorMessageTextPane.setContentType("text/html");
173
174        messageScrollPane = new JScrollPane(errorMessageTextPane);
175
176        messagePanel = new JPanel();
177        messagePanel.setBorder(new TitledBorder(null, "Message", TitledBorder.LEADING, TitledBorder.TOP, null, null));
178        messagePanel.setLayout(new BorderLayout(0, 0));
179        messagePanel.add(messageScrollPane, BorderLayout.CENTER);
180
181        // comment pane
182        commentTextPane = new JTextPane();
183        commentTextPane.setEditable(true);
184        commentTextPane
185                .setToolTipText("Give here some informations about the context or how to reproduce the bug to help the developer in resolving the issue");
186
187        final JScrollPane scComment = new JScrollPane(commentTextPane);
188        scComment.setPreferredSize(new Dimension(23, 60));
189        scComment.setMinimumSize(new Dimension(23, 60));
190
191        commentPanel = new JPanel();
192        commentPanel.setBorder(new TitledBorder(null, "Comment", TitledBorder.LEADING, TitledBorder.TOP, null, null));
193        commentPanel.setLayout(new BorderLayout(0, 0));
194        commentPanel.add(scComment, BorderLayout.CENTER);
195
196        // buttons panel
197        reportButton = new JButton("Report");
198        closeButton = new JButton("Close");
199
200        JPanel buttonsPanel = new JPanel();
201        buttonsPanel.add(reportButton);
202        buttonsPanel.add(closeButton);
203
204        // bottom
205        bottomPanel = new JPanel();
206        bottomPanel.setLayout(new BorderLayout(0, 0));
207
208        bottomPanel.add(commentPanel, BorderLayout.CENTER);
209        bottomPanel.add(buttonsPanel, BorderLayout.SOUTH);
210
211        setLayout(new BorderLayout(0, 0));
212
213        add(label, BorderLayout.NORTH);
214        add(messagePanel, BorderLayout.CENTER);
215        add(bottomPanel, BorderLayout.SOUTH);
216    }
217}