Project

General

Profile

Download (4.28 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
 * Copyright (C) 2015 EDIT
4
 * European Distributed Institute of Taxonomy
5
 * http://www.e-taxonomy.eu
6
 *
7
 * The contents of this file are subject to the Mozilla Public License Version 1.1
8
 * See LICENSE.TXT at the top of this package for the full license terms.
9
 */
10
package eu.etaxonomy.taxeditor.ui.dialog;
11

    
12
import java.io.File;
13
import java.io.FileWriter;
14
import java.io.IOException;
15

    
16
import org.eclipse.jface.dialogs.Dialog;
17
import org.eclipse.jface.dialogs.IDialogConstants;
18
import org.eclipse.swt.SWT;
19
import org.eclipse.swt.custom.StyledText;
20
import org.eclipse.swt.events.SelectionAdapter;
21
import org.eclipse.swt.events.SelectionEvent;
22
import org.eclipse.swt.graphics.Point;
23
import org.eclipse.swt.layout.GridData;
24
import org.eclipse.swt.layout.GridLayout;
25
import org.eclipse.swt.widgets.Button;
26
import org.eclipse.swt.widgets.Composite;
27
import org.eclipse.swt.widgets.Control;
28
import org.eclipse.swt.widgets.FileDialog;
29
import org.eclipse.swt.widgets.MessageBox;
30
import org.eclipse.swt.widgets.Shell;
31

    
32
/**
33
 * @author cmathew
34
 * @date 22 Oct 2015
35
 *
36
 */
37
public class ReportTextDialog extends Dialog {
38

    
39
    private String reportText = "";
40
    private String title = "";
41
    /**
42
     * Create the dialog.
43
     * @param parentShell
44
     */
45
    public ReportTextDialog(Shell parentShell) {
46
        super(parentShell);
47
    }
48

    
49
    /**
50
     * Create contents of the dialog.
51
     * @param parent
52
     */
53
    @Override
54
    protected Control createDialogArea(final Composite parent) {
55
        Composite container = (Composite) super.createDialogArea(parent);
56
        GridLayout gridLayout = (GridLayout) container.getLayout();
57
        gridLayout.numColumns = 2;
58

    
59
        StyledText styledText = new StyledText(container, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL | SWT.BORDER);
60
        styledText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
61
        styledText.setText(reportText);
62

    
63
        Button btnSave = new Button(container, SWT.NONE);
64
        btnSave.addSelectionListener(new SelectionAdapter() {
65
            @Override
66
            public void widgetSelected(SelectionEvent e) {
67
                FileDialog fileDialog = new FileDialog(parent.getShell(), SWT.SAVE);
68
                // Set filter on .txt files
69
                fileDialog.setFilterExtensions(new String[] { "*.txt" });
70
                // Put in a readable name for the filter
71
                fileDialog.setFilterNames(new String[] { "Textfiles(*.txt)" });
72
                String fileName = fileDialog.open();
73
                if(fileName != null) {
74
                    File file = new File(fileName);
75
                    if (file.exists()) {
76
                        MessageBox mb = new MessageBox(fileDialog.getParent(), SWT.ICON_WARNING
77
                                | SWT.YES | SWT.NO);
78
                        mb.setMessage(fileName + " already exists. Do you want to replace it?");
79
                        boolean override = mb.open() == SWT.YES;
80
                        if(!override) {
81
                            return;
82
                        }
83
                    }
84
                    try {
85
                        FileWriter fileWriter = new FileWriter(fileName, false);
86
                        fileWriter.write(getReportText());
87
                        fileWriter.close();
88
                    } catch (IOException ioe) {
89
                        throw new IllegalStateException(ioe);
90
                    }
91
                }
92
            }
93
        });
94
        btnSave.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, false, 1, 1));
95
        btnSave.setText("save");
96

    
97
        return container;
98
    }
99

    
100
    /**
101
     * Create contents of the button bar.
102
     * @param parent
103
     */
104
    @Override
105
    protected void createButtonsForButtonBar(Composite parent) {
106
        createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
107
    }
108

    
109
    @Override
110
    protected void configureShell(Shell shell) {
111
        super.configureShell(shell);
112
        shell.setText(title);
113
     }
114
    /**
115
     * Return the initial size of the dialog.
116
     */
117
    @Override
118
    protected Point getInitialSize() {
119
        return new Point(669, 501);
120
    }
121

    
122
    public void setReportText(String text) {
123
        this.reportText = text;
124
    }
125

    
126
    public String getReportText() {
127
        return reportText;
128
    }
129

    
130
    public void setTitle(String title) {
131
        this.title = title;
132
    }
133
}
(5-5/6)