#5297 Add remoting progress monitoring
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / dialog / ReportTextDialog.java
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 System.out.println("File Name : " + fileName);
74 if(fileName != null) {
75 File file = new File(fileName);
76 if (file.exists()) {
77 MessageBox mb = new MessageBox(fileDialog.getParent(), SWT.ICON_WARNING
78 | SWT.YES | SWT.NO);
79 mb.setMessage(fileName + " already exists. Do you want to replace it?");
80 boolean override = mb.open() == SWT.YES;
81 if(!override) {
82 return;
83 }
84 }
85 try {
86 FileWriter fileWriter = new FileWriter(fileName, false);
87 fileWriter.write(getReportText());
88 fileWriter.close();
89 } catch (IOException ioe) {
90 throw new IllegalStateException(ioe);
91 }
92 }
93 }
94 });
95 btnSave.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, false, 1, 1));
96 btnSave.setText("save");
97
98 return container;
99 }
100
101 /**
102 * Create contents of the button bar.
103 * @param parent
104 */
105 @Override
106 protected void createButtonsForButtonBar(Composite parent) {
107 createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
108 }
109
110 @Override
111 protected void configureShell(Shell shell) {
112 super.configureShell(shell);
113 shell.setText(title);
114 }
115 /**
116 * Return the initial size of the dialog.
117 */
118 @Override
119 protected Point getInitialSize() {
120 return new Point(669, 501);
121 }
122
123 public void setReportText(String text) {
124 this.reportText = text;
125 }
126
127 public String getReportText() {
128 return reportText;
129 }
130
131 public void setTitle(String title) {
132 this.title = title;
133 }
134 }