Project

General

Profile

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

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

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

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

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

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

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

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

    
96
        return container;
97
    }
98

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

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

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

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

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