Révision 3559

TXM/trunk/org.txm.core/src/java/org/txm/core/results/TXMResult.java (revision 3559)
8 8
import java.text.DateFormat;
9 9
import java.text.SimpleDateFormat;
10 10
import java.util.ArrayList;
11
import java.util.Arrays;
12 11
import java.util.Calendar;
13 12
import java.util.Date;
14 13
import java.util.HashMap;
......
2066 2065
	 * @param all
2067 2066
	 * @return
2068 2067
	 */
2069
	protected TXMResult clone(TXMResult newParent, boolean all) {
2068
	public TXMResult clone(TXMResult newParent, boolean all) {
2070 2069
		TXMResult clone = null;
2071 2070
		try {
2072 2071
			String newNodePath = this.getProject().getParametersNodeRootPath() + createUUID() + "_" + this.getClass().getSimpleName();
TXM/trunk/org.txm.searchengine.cqp.core/src/org/txm/searchengine/cqp/corpus/CQPCorpus.java (revision 3559)
487 487
	 * @return
488 488
	 */
489 489
	@Override
490
	protected TXMResult clone(TXMResult newParent, boolean all) {
490
	public TXMResult clone(TXMResult newParent, boolean all) {
491 491
		CQPCorpus c = (CQPCorpus) super.clone(newParent, all);
492 492
		c.pID = "S" + getNextSubcorpusCounter();
493 493
		return c;
TXM/trunk/org.txm.rcp/src/main/java/org/txm/rcp/handlers/export/ExportResultParameters.java (revision 3559)
1
// Copyright © 2010-2020 ENS de Lyon., University of Franche-Comté
2
// Copyright © 2007-2010 ENS de Lyon, CNRS, INRP, University of
3
// Lyon 2, University of Franche-Comté, University of Nice
4
// Sophia Antipolis, University of Paris 3.
5
//
6
// The TXM platform is free software: you can redistribute it
7
// and/or modify it under the terms of the GNU General Public
8
// License as published by the Free Software Foundation,
9
// either version 2 of the License, or (at your option) any
10
// later version.
11
//
12
// The TXM platform is distributed in the hope that it will be
13
// useful, but WITHOUT ANY WARRANTY; without even the implied
14
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15
// PURPOSE. See the GNU General Public License for more
16
// details.
17
//
18
// You should have received a copy of the GNU General
19
// Public License along with the TXM platform. If not, see
20
// http://www.gnu.org/licenses.
21
//
22
//
23
//
24
// $LastChangedDate:$
25
// $LastChangedRevision:$
26
// $LastChangedBy:$
27
//
28
package org.txm.rcp.handlers.export;
29

  
30
import java.io.File;
31
import java.util.ArrayList;
32
import java.util.Calendar;
33
import java.util.HashSet;
34

  
35
import org.eclipse.core.commands.AbstractHandler;
36
import org.eclipse.core.commands.ExecutionEvent;
37
import org.eclipse.core.commands.ExecutionException;
38
import org.eclipse.core.runtime.IProgressMonitor;
39
import org.eclipse.core.runtime.IStatus;
40
import org.eclipse.core.runtime.Status;
41
import org.eclipse.core.runtime.SubMonitor;
42
import org.eclipse.jface.viewers.IStructuredSelection;
43
import org.eclipse.osgi.util.NLS;
44
import org.eclipse.swt.SWT;
45
import org.eclipse.swt.widgets.FileDialog;
46
import org.eclipse.swt.widgets.Shell;
47
import org.eclipse.ui.handlers.HandlerUtil;
48
import org.txm.Toolbox;
49
import org.txm.core.preferences.TXMPreferences;
50
import org.txm.core.results.TXMResult;
51
import org.txm.rcp.JobsTimer;
52
import org.txm.rcp.StatusLine;
53
import org.txm.rcp.messages.TXMUIMessages;
54
import org.txm.rcp.swt.dialog.LastOpened;
55
import org.txm.rcp.utils.JobHandler;
56
import org.txm.utils.DeleteDir;
57
import org.txm.utils.zip.Zip;
58

  
59
// TODO: Auto-generated Javadoc
60
/**
61
 * export a result of a result by calling the function toTxt(File f) then show
62
 * the result in the text editor @ author mdecorde.
63
 */
64
public class ExportResultParameters extends AbstractHandler {
65
	
66
	private static final String ID = ExportResultParameters.class.getName();
67
	
68
	/**
69
	 * Export a TXM result parameters in the preferences format
70
	 *
71
	 * @param event the event
72
	 * @return the object
73
	 * @throws ExecutionException the execution exception
74
	 */
75
	@Override
76
	public Object execute(ExecutionEvent event) throws ExecutionException {
77
		IStructuredSelection selection = (IStructuredSelection) HandlerUtil.getCurrentSelection(event);
78
		
79
		Shell shell = HandlerUtil.getActiveWorkbenchWindowChecked(event).getShell();
80
		
81
		// String txmhome = Toolbox.getTxmHomePath();
82
		FileDialog dialog = new FileDialog(shell, SWT.SAVE);
83
		
84
		String extensions[] = { "*.txmresults" }; //$NON-NLS-1$
85
		dialog.setFilterExtensions(extensions);
86
		dialog.setFileName("results_"+Toolbox.dateformat.format(Calendar.getInstance().getTime()) +".txmresults"); //$NON-NLS-1$
87
		if (LastOpened.getFile(ID) != null) {
88
			dialog.setFilterPath(LastOpened.getFolder(ID));
89
		}
90
		
91
		if (dialog.open() != null) {
92
			StatusLine.setMessage(TXMUIMessages.exportingResults);
93
			String filepath = dialog.getFilterPath() + "/" + dialog.getFileName(); //$NON-NLS-1$
94
			if (!(filepath.endsWith(extensions[0].substring(1)))) {
95
				filepath += extensions[0].substring(1);
96
			}
97
			
98
			final File zipfile = new File(filepath+".tmp");
99
			final File zipfiledir = new File(filepath); // temporary directory to setup the archive
100
			LastOpened.set(ID, zipfiledir.getParent(), zipfiledir.getName()); 
101
			zipfiledir.delete();
102
			zipfiledir.mkdirs();
103
			
104
			JobHandler jobhandler = new JobHandler(TXMUIMessages.exportingResults) {
105
				
106
				@Override
107
				protected IStatus run(IProgressMonitor monitor) {
108
					
109
					// convert the monitor into sub-monitor
110
					SubMonitor subMonitor = SubMonitor.convert(monitor, TXMUIMessages.exporting, 100);
111
					this.runInit(subMonitor);
112
					
113
					try {
114
						
115
						HashSet<TXMResult> toExport = new HashSet<TXMResult>();
116
						for (Object o : selection.toList()) {
117
							if (!(o instanceof TXMResult)) continue;
118
							TXMResult r = (TXMResult)o;
119
							toExport.add(r);
120
							TXMResult parent = ((TXMResult)o).getParent();
121
							
122
							// add invisible parents to rebuild the result
123
							while(parent != null && !parent.isVisible()) { // insivisble parent must be added to the exported results
124
								
125
								toExport.add(parent);
126
								parent = parent.getParent();
127
							}
128
							
129
							// add children 
130
							for (TXMResult c : r.getDeepChildren()) {
131
								toExport.add(c);
132
							}
133
						}
134
						
135
						ArrayList<File> files = new ArrayList<>();
136
						for (TXMResult r : toExport) {
137
							
138
							if (r.isAltered()) {
139
								System.out.println(TXMUIMessages.ExportResultParameters_WarningOnlyTheParametersAreExportedManualChangesAreNotTransfered);
140
							}
141
							else {
142
								r.compute(subMonitor); // refresh result
143
							}
144
							
145
							File outfile = new File(zipfiledir, (TXMPreferences.getNode(r.getParametersNodePath()).name()+".parameters"));
146
							if (r.toParametersFile(outfile)) {
147
								files.add(outfile);
148
							}
149
						}
150
						
151
						System.out.println(NLS.bind(TXMUIMessages.ExportResultParameters_ParametersExportedToTheP0File, zipfiledir));
152
						Zip.compress(files.toArray(new File[files.size()]), zipfile, Zip.DEFAULT_LEVEL_COMPRESSION, null, null);
153
						DeleteDir.deleteDirectory(zipfiledir);
154
						zipfile.renameTo(zipfiledir);
155
					}
156
					catch (ThreadDeath td) {
157
						
158
						return Status.CANCEL_STATUS;
159
					}
160
					catch (Exception e) {
161
						System.out.println(NLS.bind(TXMUIMessages.failedToExportResultP0ColonP1, selection.toList(), e));
162
						org.txm.utils.logger.Log.printStackTrace(e);
163
						return Status.CANCEL_STATUS;
164
					}
165
					finally {
166
						subMonitor.done();
167
						JobsTimer.stopAndPrint();
168
					}
169
					return Status.OK_STATUS;
170
				}
171
			};
172
			
173
			jobhandler.startJob();
174
		}
175
		return null;
176
	}
177
}
TXM/trunk/org.txm.rcp/src/main/java/org/txm/rcp/handlers/export/ImportResult.java (revision 3559)
1
// Copyright © 2010-2020 ENS de Lyon., University of Franche-Comté
2
// Copyright © 2007-2010 ENS de Lyon, CNRS, INRP, University of
3
// Lyon 2, University of Franche-Comté, University of Nice
4
// Sophia Antipolis, University of Paris 3.
5
//
6
// The TXM platform is free software: you can redistribute it
7
// and/or modify it under the terms of the GNU General Public
8
// License as published by the Free Software Foundation,
9
// either version 2 of the License, or (at your option) any
10
// later version.
11
//
12
// The TXM platform is distributed in the hope that it will be
13
// useful, but WITHOUT ANY WARRANTY; without even the implied
14
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15
// PURPOSE. See the GNU General Public License for more
16
// details.
17
//
18
// You should have received a copy of the GNU General
19
// Public License along with the TXM platform. If not, see
20
// http://www.gnu.org/licenses.
21
//
22
//
23
//
24
// $LastChangedDate:$
25
// $LastChangedRevision:$
26
// $LastChangedBy:$
27
//
28
package org.txm.rcp.handlers.export;
29

  
30
import java.io.File;
31
import java.io.IOException;
32

  
33
import org.eclipse.core.commands.AbstractHandler;
34
import org.eclipse.core.commands.ExecutionEvent;
35
import org.eclipse.core.commands.ExecutionException;
36
import org.eclipse.core.runtime.IProgressMonitor;
37
import org.eclipse.core.runtime.IStatus;
38
import org.eclipse.core.runtime.Status;
39
import org.eclipse.jface.viewers.IStructuredSelection;
40
import org.eclipse.osgi.util.NLS;
41
import org.eclipse.swt.SWT;
42
import org.eclipse.swt.widgets.FileDialog;
43
import org.eclipse.swt.widgets.Shell;
44
import org.eclipse.ui.handlers.HandlerUtil;
45
import org.txm.core.preferences.TBXPreferences;
46
import org.txm.core.preferences.TXMPreferences;
47
import org.txm.core.results.TXMResult;
48
import org.txm.rcp.JobsTimer;
49
import org.txm.rcp.StatusLine;
50
import org.txm.rcp.handlers.files.EditFile;
51
import org.txm.rcp.messages.TXMUIMessages;
52
import org.txm.rcp.swt.dialog.LastOpened;
53
import org.txm.rcp.utils.JobHandler;
54
import org.txm.rcp.views.corpora.CorporaView;
55

  
56
// TODO: Auto-generated Javadoc
57
/**
58
 * export a result of a result by calling the function toTxt(File f) then show
59
 * the result in the text editor @ author mdecorde.
60
 */
61
public class ImportResult extends AbstractHandler {
62
	
63
	private static final String ID = ImportResult.class.getName();
64
	
65
	/**
66
	 * Export a TXM result parameters in the preferences format
67
	 *
68
	 * @param event the event
69
	 * @return the object
70
	 * @throws ExecutionException the execution exception
71
	 */
72
	@Override
73
	public Object execute(ExecutionEvent event) throws ExecutionException {
74
		IStructuredSelection selection = (IStructuredSelection) HandlerUtil.getCurrentSelection(event);
75
		final Object s = selection.getFirstElement();
76
		if (!(s instanceof TXMResult)) {
77
			return null;
78
		}
79
		Shell shell = HandlerUtil.getActiveWorkbenchWindowChecked(event).getShell();
80
		
81
		// String txmhome = Toolbox.getTxmHomePath();
82
		FileDialog dialog = new FileDialog(shell, SWT.OPEN);
83
		
84
		String extensions[] = { "*.parameters" }; //$NON-NLS-1$
85
		dialog.setFilterExtensions(extensions);
86
		
87
		if (LastOpened.getFile(ID) != null) {
88
			dialog.setFilterPath(LastOpened.getFolder(ID));
89
		}
90
		
91
		if (dialog.open() != null) {
92
			StatusLine.setMessage(TXMUIMessages.exportingResults);
93
			String filepath = dialog.getFilterPath()
94
					+ "/" + dialog.getFileName(); //$NON-NLS-1$
95
			if (!(filepath.endsWith(extensions[0].substring(1))))
96
				filepath += extensions[0].substring(1);
97
			
98
			final File outfile = new File(filepath);
99
			LastOpened.set(ID, outfile.getParent(), outfile.getName());
100
			try {
101
				outfile.createNewFile();
102
			}
103
			catch (IOException e1) {
104
				System.err.println(NLS.bind(TXMUIMessages.exportColonCantCreateFileP0ColonP1, outfile, e1));
105
			}
106
			if (!outfile.canWrite()) {
107
				System.out.println(NLS.bind(TXMUIMessages.impossibleToReadP0, outfile));
108
				return null;
109
			}
110
			if (!outfile.isFile()) {
111
				System.out.println("Error: " + outfile + " is not a file"); //$NON-NLS-1$ //$NON-NLS-2$
112
				return null;
113
			}
114
			
115
			JobHandler jobhandler = new JobHandler(TXMUIMessages.exportingResults) {
116
				
117
				@Override
118
				protected IStatus run(IProgressMonitor monitor) {
119
					try {
120
						this.runInit(monitor);
121
						monitor.beginTask(TXMUIMessages.exporting, 100);
122
						
123
						TXMResult parent = (TXMResult) s;
124
						TXMResult r = parent.importResult(outfile);
125
						CorporaView.refreshObject(r);
126
						if (r != null) {
127
							System.out.println(NLS.bind(TXMUIMessages.P0ResultsImportedFromTheP1File, r, outfile));
128
						}
129
					}
130
					catch (ThreadDeath td) {
131
						return Status.CANCEL_STATUS;
132
					}
133
					catch (Exception e) {
134
						System.out.println(NLS.bind(TXMUIMessages.failedToExportResultP0ColonP1, s, e));
135
						org.txm.utils.logger.Log.printStackTrace(e);
136
						return Status.CANCEL_STATUS;
137
					}
138
					finally {
139
						monitor.done();
140
						JobsTimer.stopAndPrint();
141
					}
142
					return Status.OK_STATUS;
143
				}
144
			};
145
			
146
			jobhandler.startJob();
147
		}
148
		return null;
149
	}
150
}
TXM/trunk/org.txm.rcp/src/main/java/org/txm/rcp/handlers/export/ImportResultParameters.java (revision 3559)
1
// Copyright © 2010-2020 ENS de Lyon., University of Franche-Comté
2
// Copyright © 2007-2010 ENS de Lyon, CNRS, INRP, University of
3
// Lyon 2, University of Franche-Comté, University of Nice
4
// Sophia Antipolis, University of Paris 3.
5
//
6
// The TXM platform is free software: you can redistribute it
7
// and/or modify it under the terms of the GNU General Public
8
// License as published by the Free Software Foundation,
9
// either version 2 of the License, or (at your option) any
10
// later version.
11
//
12
// The TXM platform is distributed in the hope that it will be
13
// useful, but WITHOUT ANY WARRANTY; without even the implied
14
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15
// PURPOSE. See the GNU General Public License for more
16
// details.
17
//
18
// You should have received a copy of the GNU General
19
// Public License along with the TXM platform. If not, see
20
// http://www.gnu.org/licenses.
21
//
22
//
23
//
24
// $LastChangedDate:$
25
// $LastChangedRevision:$
26
// $LastChangedBy:$
27
//
28
package org.txm.rcp.handlers.export;
29

  
30
import java.io.BufferedReader;
31
import java.io.File;
32
import java.io.IOException;
33
import java.lang.reflect.Constructor;
34
import java.nio.file.FileSystem;
35
import java.nio.file.FileSystems;
36
import java.nio.file.Files;
37
import java.util.Arrays;
38
import java.util.Collections;
39
import java.util.HashMap;
40
import java.util.LinkedHashMap;
41
import java.util.Properties;
42

  
43
import org.eclipse.core.commands.AbstractHandler;
44
import org.eclipse.core.commands.ExecutionEvent;
45
import org.eclipse.core.commands.ExecutionException;
46
import org.eclipse.core.runtime.IProgressMonitor;
47
import org.eclipse.core.runtime.IStatus;
48
import org.eclipse.core.runtime.Platform;
49
import org.eclipse.core.runtime.Status;
50
import org.eclipse.jface.viewers.IStructuredSelection;
51
import org.eclipse.osgi.util.NLS;
52
import org.eclipse.swt.SWT;
53
import org.eclipse.swt.widgets.FileDialog;
54
import org.eclipse.swt.widgets.Shell;
55
import org.eclipse.ui.handlers.HandlerUtil;
56
import org.osgi.framework.Bundle;
57
import org.txm.Toolbox;
58
import org.txm.core.preferences.TXMPreferences;
59
import org.txm.core.results.TXMResult;
60
import org.txm.rcp.JobsTimer;
61
import org.txm.rcp.StatusLine;
62
import org.txm.rcp.messages.TXMUIMessages;
63
import org.txm.rcp.swt.dialog.LastOpened;
64
import org.txm.rcp.utils.JobHandler;
65
import org.txm.rcp.utils.SWTEditorsUtils;
66
import org.txm.rcp.views.corpora.CorporaView;
67
import org.txm.utils.DeleteDir;
68
import org.txm.utils.io.IOUtils;
69
import org.txm.utils.logger.Log;
70
import org.txm.utils.zip.Zip;
71

  
72
// TODO: Auto-generated Javadoc
73
/**
74
 * export a result of a result by calling the function toTxt(File f) then show
75
 * the result in the text editor @ author mdecorde.
76
 */
77
public class ImportResultParameters extends AbstractHandler {
78
	
79
	private static final String ID = ImportResultParameters.class.getName();
80
	
81
	/**
82
	 * Export a TXM result parameters in the preferences format
83
	 *
84
	 * @param event the event
85
	 * @return the object
86
	 * @throws ExecutionException the execution exception
87
	 */
88
	@Override
89
	public Object execute(ExecutionEvent event) throws ExecutionException {
90
		IStructuredSelection selection = (IStructuredSelection) HandlerUtil.getCurrentSelection(event);
91
		final Object s = selection.getFirstElement();
92
		if (!(s instanceof TXMResult)) {
93
			return null;
94
		}
95
		Shell shell = HandlerUtil.getActiveWorkbenchWindowChecked(event).getShell();
96
		
97
		// String txmhome = Toolbox.getTxmHomePath();
98
		FileDialog dialog = new FileDialog(shell, SWT.OPEN);
99
		
100
		String extensions[] = { "*.txmresults" }; //$NON-NLS-1$
101
		dialog.setFilterExtensions(extensions);
102
		
103
		if (LastOpened.getFile(ID) != null) {
104
			dialog.setFilterPath(LastOpened.getFolder(ID));
105
		}
106
		try {
107
			if (dialog.open() != null) {
108
				StatusLine.setMessage(TXMUIMessages.exportingResults);
109
				String filepath = dialog.getFilterPath()+ "/" + dialog.getFileName(); //$NON-NLS-1$
110
				
111
				final File zipfile = new File(filepath);
112
				File outdir = new File(Toolbox.getTxmHomePath(),  "results/import");
113
				DeleteDir.deleteDirectory(outdir);
114
				
115
				Zip.decompress(zipfile, outdir, false);
116
				
117
				LinkedHashMap<File, Properties> fileToProperties = new LinkedHashMap<>(); // to retrieve a new result from its old uuid
118
				HashMap<String, File> oldUUIDToPropertiesFile = new HashMap<>(); // to retrieve a new result from its old uuid
119
				HashMap<String, String> oldUUIDToNewUUID = new HashMap<>(); // to retrieve a new result from its old uuid
120
				HashMap<String, String> UUIDDependencies = new HashMap<>(); // to -> from, if from is null use the selected TXMResult as parent
121
				
122
				File[] files = outdir.listFiles();
123
				Arrays.sort(files);
124
				
125
				for (File parametersFile : files) {
126
					if (!parametersFile.getName().endsWith(".parameters")) continue;
127
					
128
					Properties props = new Properties();
129
					BufferedReader r = IOUtils.getReader(parametersFile);
130
					props.load(r);
131
					r.close();
132
					
133
					fileToProperties.put(parametersFile, props);
134
					String UUID = props.getProperty(TXMPreferences.RESULT_PARAMETERS_NODE_PATH);
135
					oldUUIDToPropertiesFile.put(UUID, parametersFile);
136
				}
137
				
138
				for (File parametersFile : outdir.listFiles()) {
139
					// if (TXMPreferences.PARENT_PARAMETERS_NODE_PATH.equals(ps)) continue;
140
					//if (TXMPreferences.RESULT_PARAMETERS_NODE_PATH.equals(ps)) continue;
141
					Properties props = fileToProperties.get(parametersFile);
142
					String UUID = props.getProperty(TXMPreferences.RESULT_PARAMETERS_NODE_PATH);
143
					String parentUUID = props.getProperty(TXMPreferences.PARENT_PARAMETERS_NODE_PATH);
144
					if (!oldUUIDToPropertiesFile.containsKey(parentUUID)) {
145
						UUIDDependencies.put(UUID, null);
146
					} else {
147
						UUIDDependencies.put(UUID, parentUUID);
148
					}
149
				}
150
				
151
				JobHandler jobhandler = new JobHandler(TXMUIMessages.importStarted) {
152
					
153
					@Override
154
					protected IStatus run(IProgressMonitor monitor) {
155
						
156
						for (File parametersFile: fileToProperties.keySet()) {
157
							TXMResult importedResult = null;
158
							try {
159
								this.runInit(monitor);
160
								monitor.beginTask(TXMUIMessages.exporting, 100);
161
								
162
								TXMResult defaultParentResult = (TXMResult) s;
163
								
164
								Properties props = fileToProperties.get(parametersFile);
165
								String UUID = props.getProperty(TXMPreferences.RESULT_PARAMETERS_NODE_PATH);
166
								
167
								String className = props.getProperty(TXMPreferences.CLASS, ""); //$NON-NLS-1$ //$NON-NLS-2$
168
								if (className == null || className.length() == 0) {
169
									Log.warning(NLS.bind("Warning: can not import object with no class name set with path={0}.", parametersFile)); //$NON-NLS-1$
170
									continue;
171
								}
172
								String bundleId = props.getProperty(TXMPreferences.BUNDLE_ID, ""); //$NON-NLS-1$
173
								if (bundleId == null || bundleId.length() == 0) {
174
									Log.warning(NLS.bind("Warning: can not import {0} object with no bundle id set.", parametersFile)); //$NON-NLS-1$
175
									continue;
176
								}
177
								Bundle bundle = Platform.getBundle(bundleId);
178
								if (bundle == null) {
179
									Log.warning(NLS.bind("Warning: can not import {0} object with bundle id={1}.", className, parametersFile)); //$NON-NLS-1$
180
									continue;
181
								}
182
								
183
								Class<?> cl = bundle.loadClass(className);
184
								
185
								String resultNodePath = defaultParentResult.getProject().getParametersNodeRootPath() + TXMResult.createUUID() + "_" + cl.getSimpleName();
186
								Log.info("Importing a "+cl.getSimpleName()+" from "+resultNodePath);
187
								TXMResult parent = defaultParentResult;
188
								if (UUIDDependencies.get(UUID) != null) {
189
									parent = TXMResult.getResult(oldUUIDToNewUUID.get(UUIDDependencies.get(UUID)));
190
								}
191
								
192
								
193
								
194
								Constructor<?> cons = cl.getConstructor(String.class);
195
								
196
								// fill the preference node with the right parameter **and then** parent right parent node path 
197
								for (Object p : props.keySet()) {
198
									String ps = p.toString();
199
									TXMPreferences.put(resultNodePath, ps, props.getProperty(ps, ""));
200
								}
201
								TXMPreferences.put(resultNodePath, TXMPreferences.PARENT_PARAMETERS_NODE_PATH, parent.getParametersNodePath());
202
								
203
								// this should create the right TXMResult, parameters string values are set
204
								importedResult = (TXMResult) cons.newInstance(resultNodePath);
205
								importedResult.autoLoadParametersFromAnnotations(); // update java parameters
206
								importedResult.loadParameters();
207
								
208
								oldUUIDToNewUUID.put(UUID, resultNodePath);
209
								
210
								//						if (r.importParameters(outfile)) {
211
								//							CorporaView.refreshObject(r);
212
								//							System.out.println(NLS.bind("Parameter imported from the {0} file.", outfile)); //$NON-NLS-1$
213
								//								
214
								//							this.syncExec(new Runnable() {
215
								//
216
								//								@Override
217
								//								public void run() {
218
								//									SWTEditorsUtils.refreshEditors(r);
219
								//								}
220
								//							});
221
								//						}
222
							}
223
							catch (ThreadDeath td) {
224
								if (importedResult != null) {
225
									importedResult.delete();
226
								}
227
								return Status.CANCEL_STATUS;
228
							}
229
							catch (Exception e) {
230
								Log.info(NLS.bind("Error while loading result {0}: {1}", s, e));
231
								org.txm.utils.logger.Log.printStackTrace(e);
232
								if (importedResult != null) {
233
									importedResult.delete();
234
								}
235
								//return Status.CANCEL_STATUS;
236
							}
237
							
238
						}
239
						monitor.done();
240
						JobsTimer.stopAndPrint();
241
						
242
						this.syncExec(new Runnable() {
243
							
244
							@Override
245
							public void run() {
246
								
247
								CorporaView.refresh();
248
							}
249
						});
250
						return Status.OK_STATUS;
251
					}
252
				};
253
				
254
				jobhandler.startJob();
255
			}
256
			
257
		}
258
		catch (Exception e1) {
259
			// TODO Auto-generated catch block
260
			e1.printStackTrace();
261
			return null;
262
		}
263
		return null;
264
	}
265
}
TXM/trunk/org.txm.rcp/src/main/java/org/txm/rcp/handlers/export/ExportResult.java (revision 3559)
1
// Copyright © 2010-2020 ENS de Lyon., University of Franche-Comté
2
// Copyright © 2007-2010 ENS de Lyon, CNRS, INRP, University of
3
// Lyon 2, University of Franche-Comté, University of Nice
4
// Sophia Antipolis, University of Paris 3.
5
//
6
// The TXM platform is free software: you can redistribute it
7
// and/or modify it under the terms of the GNU General Public
8
// License as published by the Free Software Foundation,
9
// either version 2 of the License, or (at your option) any
10
// later version.
11
//
12
// The TXM platform is distributed in the hope that it will be
13
// useful, but WITHOUT ANY WARRANTY; without even the implied
14
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15
// PURPOSE. See the GNU General Public License for more
16
// details.
17
//
18
// You should have received a copy of the GNU General
19
// Public License along with the TXM platform. If not, see
20
// http://www.gnu.org/licenses.
21
//
22
//
23
//
24
// $LastChangedDate:$
25
// $LastChangedRevision:$
26
// $LastChangedBy:$
27
//
28
package org.txm.rcp.handlers.export;
29

  
30
import java.io.File;
31
import java.io.IOException;
32

  
33
import org.eclipse.core.commands.AbstractHandler;
34
import org.eclipse.core.commands.ExecutionEvent;
35
import org.eclipse.core.commands.ExecutionException;
36
import org.eclipse.core.runtime.IProgressMonitor;
37
import org.eclipse.core.runtime.IStatus;
38
import org.eclipse.core.runtime.Status;
39
import org.eclipse.core.runtime.SubMonitor;
40
import org.eclipse.jface.viewers.IStructuredSelection;
41
import org.eclipse.osgi.util.NLS;
42
import org.eclipse.swt.SWT;
43
import org.eclipse.swt.widgets.FileDialog;
44
import org.eclipse.swt.widgets.Shell;
45
import org.eclipse.ui.handlers.HandlerUtil;
46
import org.txm.core.messages.TXMCoreMessages;
47
import org.txm.core.preferences.TBXPreferences;
48
import org.txm.core.preferences.TXMPreferences;
49
import org.txm.core.results.TXMResult;
50
import org.txm.rcp.JobsTimer;
51
import org.txm.rcp.StatusLine;
52
import org.txm.rcp.handlers.files.EditFile;
53
import org.txm.rcp.messages.TXMUIMessages;
54
import org.txm.rcp.swt.dialog.LastOpened;
55
import org.txm.rcp.utils.JobHandler;
56
import org.txm.utils.ExecTimer;
57
import org.txm.utils.logger.Log;
58

  
59
/**
60
 * Exports a result by calling the function toTxt(File f) then opens or not the result in the text editor according to the preferences.
61
 * 
62
 * @author mdecorde, sjacquot
63
 */
64
public class ExportResult extends AbstractHandler {
65
	
66
	private static final String ID = "org.txm.rcp.commands.function.ExportResult"; //$NON-NLS-1$
67
	
68
	/** The selection. */
69
	private IStructuredSelection selection;
70
	
71
	/** The lastopenedfile. */
72
	// private static String lastopenedfile;
73
	
74
	/**
75
	 * Export a TXM result in a CSV file.
76
	 *
77
	 * @param event the event
78
	 * @return the object
79
	 * @throws ExecutionException the execution exception
80
	 */
81
	@Override
82
	public Object execute(ExecutionEvent event) throws ExecutionException {
83
		selection = (IStructuredSelection) HandlerUtil.getCurrentSelection(event);
84
		final Object s = selection.getFirstElement();
85
		Shell shell = HandlerUtil.getActiveWorkbenchWindowChecked(event).getShell();
86
		
87
		// String txmhome = Toolbox.getTxmHomePath();
88
		FileDialog dialog = new FileDialog(shell, SWT.SAVE);
89
		
90
		String extensions[] = { "*.csv" }; //$NON-NLS-1$
91
		if (s instanceof TXMResult) {
92
			extensions = ((TXMResult) s).getExportTXTExtensions();
93
			dialog.setFileName(((TXMResult) s).getValidFileName());
94
		}
95
		dialog.setFilterExtensions(extensions);
96
		
97
		if (LastOpened.getFile(ID) != null) {
98
			dialog.setFilterPath(LastOpened.getFolder(ID));
99
		}
100
		
101
		if (dialog.open() != null) {
102
			StatusLine.setMessage(TXMUIMessages.exportingResults);
103
			String filepath = dialog.getFilterPath() + "/" + dialog.getFileName(); //$NON-NLS-1$
104
			if (!(filepath.endsWith(extensions[0].substring(1))))
105
				filepath += extensions[0].substring(1);
106
			
107
			final File outfile = new File(filepath);
108
			LastOpened.set(ID, outfile.getParent(), outfile.getName());
109
			try {
110
				outfile.createNewFile();
111
			}
112
			catch (IOException e1) {
113
				Log.warning(NLS.bind(TXMUIMessages.exportColonCantCreateFileP0ColonP1, outfile, e1));
114
			}
115
			if (!outfile.canWrite()) {
116
				Log.warning(NLS.bind(TXMUIMessages.impossibleToReadP0, outfile));
117
				return null;
118
			}
119
			if (!outfile.isFile()) {
120
				Log.warning("Error: " + outfile + " is not a file"); //$NON-NLS-1$ //$NON-NLS-2$
121
				return null;
122
			}
123
			
124
			final String encoding = TBXPreferences.getInstance().getString(TBXPreferences.EXPORT_ENCODING);
125
			String _colseparator = TBXPreferences.getInstance().getString(TBXPreferences.EXPORT_COL_SEPARATOR);
126
			final String colseparator = _colseparator;
127
			String _txtseparator = TBXPreferences.getInstance().getString(TBXPreferences.EXPORT_TXT_SEPARATOR);
128
			final String txtseparator = _txtseparator;
129
			
130
			JobHandler jobhandler = new JobHandler(TXMUIMessages.exportingResults) {
131
				
132
				@Override
133
				protected IStatus run(IProgressMonitor monitor) {
134
					
135
					// convert the monitor into sub-monitor
136
					SubMonitor subMonitor = SubMonitor.convert(monitor, TXMUIMessages.exporting, 100);
137
					
138
					try {
139
						
140
						this.runInit(subMonitor);
141
						
142
						if (s instanceof TXMResult) {
143
							TXMResult r = (TXMResult) s;
144
							
145
							// compute the result if needed
146
							if (!r.isAltered() && r.isDirty()) {
147
								r.compute(this);
148
							}
149
							Log.info(TXMUIMessages.bind(TXMUIMessages.exportingP0, r.getName()));
150
							ExecTimer.start();
151
							r.toTxt(outfile, encoding, colseparator, txtseparator);
152
							Log.info(TXMUIMessages.bind(TXMUIMessages.doneInP0, ExecTimer.stop()));
153
						}
154
						else {
155
							Log.warning(TXMUIMessages.TheExportedObjectIsNotATXMResultResult);
156
							return Status.CANCEL_STATUS;
157
						}
158
						
159
						if (outfile.exists()) {
160
							// Open internal editor in the UI thread
161
							if (TBXPreferences.getInstance().getBoolean(TBXPreferences.EXPORT_SHOW)) {
162
								this.syncExec(new Runnable() {
163
									
164
									@Override
165
									public void run() {
166
										EditFile.openfile(outfile.getAbsolutePath());
167
									}
168
								});
169
							}
170
						}
171
						else {
172
							Log.warning(NLS.bind(TXMUIMessages.failedToExportResultP0ColonP1, outfile.getAbsolutePath()));
173
						}
174
					}
175
					catch (ThreadDeath td) {
176
						TXMResult r = (TXMResult) s;
177
						r.resetComputingState();
178
						return Status.CANCEL_STATUS;
179
					}
180
					catch (Exception e) {
181
						Log.warning(NLS.bind(TXMUIMessages.failedToExportResultP0ColonP1, s, e));
182
						org.txm.utils.logger.Log.printStackTrace(e);
183
						return Status.CANCEL_STATUS;
184
					}
185
					finally {
186
						subMonitor.done();
187
						JobsTimer.stopAndPrint();
188
					}
189
					return Status.OK_STATUS;
190
				}
191
			};
192
			
193
			jobhandler.startJob();
194
		}
195
		return null;
196
	}
197
}
TXM/trunk/org.txm.rcp/src/main/java/org/txm/rcp/handlers/export/ImportResultParameter.java (revision 3559)
1
// Copyright © 2010-2020 ENS de Lyon., University of Franche-Comté
2
// Copyright © 2007-2010 ENS de Lyon, CNRS, INRP, University of
3
// Lyon 2, University of Franche-Comté, University of Nice
4
// Sophia Antipolis, University of Paris 3.
5
//
6
// The TXM platform is free software: you can redistribute it
7
// and/or modify it under the terms of the GNU General Public
8
// License as published by the Free Software Foundation,
9
// either version 2 of the License, or (at your option) any
10
// later version.
11
//
12
// The TXM platform is distributed in the hope that it will be
13
// useful, but WITHOUT ANY WARRANTY; without even the implied
14
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15
// PURPOSE. See the GNU General Public License for more
16
// details.
17
//
18
// You should have received a copy of the GNU General
19
// Public License along with the TXM platform. If not, see
20
// http://www.gnu.org/licenses.
21
//
22
//
23
//
24
// $LastChangedDate:$
25
// $LastChangedRevision:$
26
// $LastChangedBy:$
27
//
28
package org.txm.rcp.handlers.export;
29

  
30
import java.io.File;
31
import java.io.IOException;
32

  
33
import org.eclipse.core.commands.AbstractHandler;
34
import org.eclipse.core.commands.ExecutionEvent;
35
import org.eclipse.core.commands.ExecutionException;
36
import org.eclipse.core.runtime.IProgressMonitor;
37
import org.eclipse.core.runtime.IStatus;
38
import org.eclipse.core.runtime.Status;
39
import org.eclipse.jface.viewers.IStructuredSelection;
40
import org.eclipse.osgi.util.NLS;
41
import org.eclipse.swt.SWT;
42
import org.eclipse.swt.widgets.FileDialog;
43
import org.eclipse.swt.widgets.Shell;
44
import org.eclipse.ui.handlers.HandlerUtil;
45
import org.txm.core.preferences.TBXPreferences;
46
import org.txm.core.preferences.TXMPreferences;
47
import org.txm.core.results.TXMResult;
48
import org.txm.rcp.JobsTimer;
49
import org.txm.rcp.StatusLine;
50
import org.txm.rcp.handlers.files.EditFile;
51
import org.txm.rcp.messages.TXMUIMessages;
52
import org.txm.rcp.swt.dialog.LastOpened;
53
import org.txm.rcp.utils.JobHandler;
54
import org.txm.rcp.views.corpora.CorporaView;
55

  
56
// TODO: Auto-generated Javadoc
57
/**
58
 * export a result of a result by calling the function toTxt(File f) then show
59
 * the result in the text editor @ author mdecorde.
60
 */
61
public class ImportResultParameter extends AbstractHandler {
62
	
63
	private static final String ID = ImportResultParameter.class.getName();
64
	
65
	/**
66
	 * Export a TXM result parameters in the preferences format
67
	 *
68
	 * @param event the event
69
	 * @return the object
70
	 * @throws ExecutionException the execution exception
71
	 */
72
	@Override
73
	public Object execute(ExecutionEvent event) throws ExecutionException {
74
		IStructuredSelection selection = (IStructuredSelection) HandlerUtil.getCurrentSelection(event);
75
		final Object s = selection.getFirstElement();
76
		if (!(s instanceof TXMResult)) {
77
			return null;
78
		}
79
		Shell shell = HandlerUtil.getActiveWorkbenchWindowChecked(event).getShell();
80
		
81
		// String txmhome = Toolbox.getTxmHomePath();
82
		FileDialog dialog = new FileDialog(shell, SWT.OPEN);
83
		
84
		String extensions[] = { "*.parameters" }; //$NON-NLS-1$
85
		dialog.setFilterExtensions(extensions);
86
		
87
		if (LastOpened.getFile(ID) != null) {
88
			dialog.setFilterPath(LastOpened.getFolder(ID));
89
		}
90
		
91
		if (dialog.open() != null) {
92
			StatusLine.setMessage(TXMUIMessages.exportingResults);
93
			String filepath = dialog.getFilterPath() + "/" + dialog.getFileName(); //$NON-NLS-1$
94
			if (!(filepath.endsWith(extensions[0].substring(1)))) {
95
				filepath += extensions[0].substring(1);
96
			}
97
			
98
			final File outfile = new File(filepath);
99
			LastOpened.set(ID, outfile.getParent(), outfile.getName());
100
			try {
101
				outfile.createNewFile();
102
			}
103
			catch (IOException e1) {
104
				System.err.println(NLS.bind(TXMUIMessages.exportColonCantCreateFileP0ColonP1, outfile, e1));
105
			}
106
			if (!outfile.canWrite()) {
107
				System.out.println(NLS.bind(TXMUIMessages.impossibleToReadP0, outfile));
108
				return null;
109
			}
110
			if (!outfile.isFile()) {
111
				System.out.println("Error: " + outfile + " is not a file"); //$NON-NLS-1$ //$NON-NLS-2$
112
				return null;
113
			}
114
			
115
			JobHandler jobhandler = new JobHandler(TXMUIMessages.exportingResults) {
116
				
117
				@Override
118
				protected IStatus run(IProgressMonitor monitor) {
119
					try {
120
						this.runInit(monitor);
121
						monitor.beginTask(TXMUIMessages.exporting, 100);
122
						
123
						TXMResult parent = (TXMResult) s;
124
						TXMResult r = parent.importResult(outfile);
125
						CorporaView.refreshObject(r);
126
						if (r != null) {
127
							System.out.println(NLS.bind(TXMUIMessages.P0ResultsImportedFromTheP1File, r, outfile));
128
						}
129
					}
130
					catch (ThreadDeath td) {
131
						return Status.CANCEL_STATUS;
132
					}
133
					catch (Exception e) {
134
						System.out.println(NLS.bind(TXMUIMessages.failedToExportResultP0ColonP1, s, e));
135
						org.txm.utils.logger.Log.printStackTrace(e);
136
						return Status.CANCEL_STATUS;
137
					}
138
					finally {
139
						monitor.done();
140
						JobsTimer.stopAndPrint();
141
					}
142
					return Status.OK_STATUS;
143
				}
144
			};
145
			
146
			jobhandler.startJob();
147
		}
148
		return null;
149
	}
150
}
0 151

  
TXM/trunk/org.txm.rcp/src/main/java/org/txm/rcp/handlers/export/ImportResultChain.java (revision 3559)
1
// Copyright © 2010-2020 ENS de Lyon., University of Franche-Comté
2
// Copyright © 2007-2010 ENS de Lyon, CNRS, INRP, University of
3
// Lyon 2, University of Franche-Comté, University of Nice
4
// Sophia Antipolis, University of Paris 3.
5
//
6
// The TXM platform is free software: you can redistribute it
7
// and/or modify it under the terms of the GNU General Public
8
// License as published by the Free Software Foundation,
9
// either version 2 of the License, or (at your option) any
10
// later version.
11
//
12
// The TXM platform is distributed in the hope that it will be
13
// useful, but WITHOUT ANY WARRANTY; without even the implied
14
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15
// PURPOSE. See the GNU General Public License for more
16
// details.
17
//
18
// You should have received a copy of the GNU General
19
// Public License along with the TXM platform. If not, see
20
// http://www.gnu.org/licenses.
21
//
22
//
23
//
24
// $LastChangedDate:$
25
// $LastChangedRevision:$
26
// $LastChangedBy:$
27
//
28
package org.txm.rcp.handlers.export;
29

  
30
import java.io.BufferedReader;
31
import java.io.File;
32
import java.io.IOException;
33
import java.lang.reflect.Constructor;
34
import java.nio.file.FileSystem;
35
import java.nio.file.FileSystems;
36
import java.nio.file.Files;
37
import java.util.Arrays;
38
import java.util.Collections;
39
import java.util.HashMap;
40
import java.util.LinkedHashMap;
41
import java.util.Properties;
42

  
43
import org.eclipse.core.commands.AbstractHandler;
44
import org.eclipse.core.commands.ExecutionEvent;
45
import org.eclipse.core.commands.ExecutionException;
46
import org.eclipse.core.runtime.IProgressMonitor;
47
import org.eclipse.core.runtime.IStatus;
48
import org.eclipse.core.runtime.Platform;
49
import org.eclipse.core.runtime.Status;
50
import org.eclipse.jface.viewers.IStructuredSelection;
51
import org.eclipse.osgi.util.NLS;
52
import org.eclipse.swt.SWT;
53
import org.eclipse.swt.widgets.FileDialog;
54
import org.eclipse.swt.widgets.Shell;
55
import org.eclipse.ui.handlers.HandlerUtil;
56
import org.osgi.framework.Bundle;
57
import org.txm.Toolbox;
58
import org.txm.core.preferences.TXMPreferences;
59
import org.txm.core.results.TXMResult;
60
import org.txm.rcp.JobsTimer;
61
import org.txm.rcp.StatusLine;
62
import org.txm.rcp.messages.TXMUIMessages;
63
import org.txm.rcp.swt.dialog.LastOpened;
64
import org.txm.rcp.utils.JobHandler;
65
import org.txm.rcp.utils.SWTEditorsUtils;
66
import org.txm.rcp.views.corpora.CorporaView;
67
import org.txm.utils.DeleteDir;
68
import org.txm.utils.io.IOUtils;
69
import org.txm.utils.logger.Log;
70
import org.txm.utils.zip.Zip;
71

  
72
// TODO: Auto-generated Javadoc
73
/**
74
 * export a result of a result by calling the function toTxt(File f) then show
75
 * the result in the text editor @ author mdecorde.
76
 */
77
public class ImportResultChain extends AbstractHandler {
78
	
79
	private static final String ID = ImportResultChain.class.getName();
80
	
81
	/**
82
	 * Export a TXM result parameters in the preferences format
83
	 *
84
	 * @param event the event
85
	 * @return the object
86
	 * @throws ExecutionException the execution exception
87
	 */
88
	@Override
89
	public Object execute(ExecutionEvent event) throws ExecutionException {
90
		IStructuredSelection selection = (IStructuredSelection) HandlerUtil.getCurrentSelection(event);
91
		final Object s = selection.getFirstElement();
92
		if (!(s instanceof TXMResult)) {
93
			return null;
94
		}
95
		Shell shell = HandlerUtil.getActiveWorkbenchWindowChecked(event).getShell();
96
		
97
		// String txmhome = Toolbox.getTxmHomePath();
98
		FileDialog dialog = new FileDialog(shell, SWT.OPEN);
99
		
100
		String extensions[] = { "*.txmresults" }; //$NON-NLS-1$
101
		dialog.setFilterExtensions(extensions);
102
		
103
		if (LastOpened.getFile(ID) != null) {
104
			dialog.setFilterPath(LastOpened.getFolder(ID));
105
		}
106
		try {
107
			if (dialog.open() != null) {
108
				StatusLine.setMessage(TXMUIMessages.exportingResults);
109
				String filepath = dialog.getFilterPath()+ "/" + dialog.getFileName(); //$NON-NLS-1$
110
				
111
				final File zipfile = new File(filepath);
112
				File outdir = new File(Toolbox.getTxmHomePath(),  "results/import");
113
				DeleteDir.deleteDirectory(outdir);
114
				
115
				Zip.decompress(zipfile, outdir, false);
116
				
117
				LinkedHashMap<File, Properties> fileToProperties = new LinkedHashMap<>(); // to retrieve a new result from its old uuid
118
				HashMap<String, File> oldUUIDToPropertiesFile = new HashMap<>(); // to retrieve a new result from its old uuid
119
				HashMap<String, String> oldUUIDToNewUUID = new HashMap<>(); // to retrieve a new result from its old uuid
120
				HashMap<String, String> UUIDDependencies = new HashMap<>(); // to -> from, if from is null use the selected TXMResult as parent
121
				
122
				File[] files = outdir.listFiles();
123
				Arrays.sort(files);
124
				
125
				for (File parametersFile : files) {
126
					if (!parametersFile.getName().endsWith(".parameters")) continue;
127
					
128
					Properties props = new Properties();
129
					BufferedReader r = IOUtils.getReader(parametersFile);
130
					props.load(r);
131
					r.close();
132
					
133
					fileToProperties.put(parametersFile, props);
134
					String UUID = props.getProperty(TXMPreferences.RESULT_PARAMETERS_NODE_PATH);
135
					oldUUIDToPropertiesFile.put(UUID, parametersFile);
136
				}
137
				
138
				for (File parametersFile : outdir.listFiles()) {
139
					// if (TXMPreferences.PARENT_PARAMETERS_NODE_PATH.equals(ps)) continue;
140
					//if (TXMPreferences.RESULT_PARAMETERS_NODE_PATH.equals(ps)) continue;
141
					Properties props = fileToProperties.get(parametersFile);
142
					String UUID = props.getProperty(TXMPreferences.RESULT_PARAMETERS_NODE_PATH);
143
					String parentUUID = props.getProperty(TXMPreferences.PARENT_PARAMETERS_NODE_PATH);
144
					if (!oldUUIDToPropertiesFile.containsKey(parentUUID)) {
145
						UUIDDependencies.put(UUID, null);
146
					} else {
147
						UUIDDependencies.put(UUID, parentUUID);
148
					}
149
				}
150
				
151
				JobHandler jobhandler = new JobHandler(TXMUIMessages.importStarted) {
152
					
153
					@Override
154
					protected IStatus run(IProgressMonitor monitor) {
155
						
156
						for (File parametersFile: fileToProperties.keySet()) {
157
							TXMResult importedResult = null;
158
							try {
159
								this.runInit(monitor);
160
								monitor.beginTask(TXMUIMessages.exporting, 100);
161
								
162
								TXMResult defaultParentResult = (TXMResult) s;
163
								
164
								Properties props = fileToProperties.get(parametersFile);
165
								String UUID = props.getProperty(TXMPreferences.RESULT_PARAMETERS_NODE_PATH);
166
								
167
								String className = props.getProperty(TXMPreferences.CLASS, ""); //$NON-NLS-1$ //$NON-NLS-2$
168
								if (className == null || className.length() == 0) {
169
									Log.warning(NLS.bind("Warning: can not import object with no class name set with path={0}.", parametersFile)); //$NON-NLS-1$
170
									continue;
171
								}
172
								String bundleId = props.getProperty(TXMPreferences.BUNDLE_ID, ""); //$NON-NLS-1$
173
								if (bundleId == null || bundleId.length() == 0) {
174
									Log.warning(NLS.bind("Warning: can not import {0} object with no bundle id set.", parametersFile)); //$NON-NLS-1$
175
									continue;
176
								}
177
								Bundle bundle = Platform.getBundle(bundleId);
178
								if (bundle == null) {
179
									Log.warning(NLS.bind("Warning: can not import {0} object with bundle id={1}.", className, parametersFile)); //$NON-NLS-1$
180
									continue;
181
								}
182
								
183
								Class<?> cl = bundle.loadClass(className);
184
								
185
								String resultNodePath = defaultParentResult.getProject().getParametersNodeRootPath() + TXMResult.createUUID() + "_" + cl.getSimpleName();
186
								Log.info("Importing a "+cl.getSimpleName()+" from "+resultNodePath);
187
								TXMResult parent = defaultParentResult;
188
								if (UUIDDependencies.get(UUID) != null) {
189
									parent = TXMResult.getResult(oldUUIDToNewUUID.get(UUIDDependencies.get(UUID)));
190
								}
191
								
192
								
193
								
194
								Constructor<?> cons = cl.getConstructor(String.class);
195
								
196
								// fill the preference node with the right parameter **and then** parent right parent node path 
197
								for (Object p : props.keySet()) {
198
									String ps = p.toString();
199
									TXMPreferences.put(resultNodePath, ps, props.getProperty(ps, ""));
200
								}
201
								TXMPreferences.put(resultNodePath, TXMPreferences.PARENT_PARAMETERS_NODE_PATH, parent.getParametersNodePath());
202
								
203
								// this should create the right TXMResult, parameters string values are set
204
								importedResult = (TXMResult) cons.newInstance(resultNodePath);
205
								importedResult.autoLoadParametersFromAnnotations(); // update java parameters
206
								importedResult.loadParameters();
207
								
208
								oldUUIDToNewUUID.put(UUID, resultNodePath);
209
								
210
								//						if (r.importParameters(outfile)) {
211
								//							CorporaView.refreshObject(r);
212
								//							System.out.println(NLS.bind("Parameter imported from the {0} file.", outfile)); //$NON-NLS-1$
213
								//								
214
								//							this.syncExec(new Runnable() {
215
								//
216
								//								@Override
217
								//								public void run() {
218
								//									SWTEditorsUtils.refreshEditors(r);
219
								//								}
220
								//							});
221
								//						}
222
							}
223
							catch (ThreadDeath td) {
224
								if (importedResult != null) {
225
									importedResult.delete();
226
								}
227
								return Status.CANCEL_STATUS;
228
							}
229
							catch (Exception e) {
230
								Log.info(NLS.bind("Error while loading result {0}: {1}", s, e));
231
								org.txm.utils.logger.Log.printStackTrace(e);
232
								if (importedResult != null) {
233
									importedResult.delete();
234
								}
235
								//return Status.CANCEL_STATUS;
236
							}
237
							
238
						}
239
						monitor.done();
240
						JobsTimer.stopAndPrint();
241
						
242
						this.syncExec(new Runnable() {
243
							
244
							@Override
245
							public void run() {
246
								
247
								CorporaView.refresh();
248
							}
249
						});
250
						return Status.OK_STATUS;
251
					}
252
				};
253
				
254
				jobhandler.startJob();
255
			}
256
			
257
		}
258
		catch (Exception e1) {
259
			// TODO Auto-generated catch block
260
			e1.printStackTrace();
261
			return null;
262
		}
263
		return null;
264
	}
265
}
0 266

  
TXM/trunk/org.txm.rcp/src/main/java/org/txm/rcp/handlers/export/ExportResultParameter.java (revision 3559)
1
// Copyright © 2010-2020 ENS de Lyon., University of Franche-Comté
2
// Copyright © 2007-2010 ENS de Lyon, CNRS, INRP, University of
3
// Lyon 2, University of Franche-Comté, University of Nice
4
// Sophia Antipolis, University of Paris 3.
5
//
6
// The TXM platform is free software: you can redistribute it
7
// and/or modify it under the terms of the GNU General Public
8
// License as published by the Free Software Foundation,
9
// either version 2 of the License, or (at your option) any
10
// later version.
11
//
12
// The TXM platform is distributed in the hope that it will be
13
// useful, but WITHOUT ANY WARRANTY; without even the implied
14
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15
// PURPOSE. See the GNU General Public License for more
16
// details.
17
//
18
// You should have received a copy of the GNU General
19
// Public License along with the TXM platform. If not, see
20
// http://www.gnu.org/licenses.
21
//
22
//
23
//
24
// $LastChangedDate:$
25
// $LastChangedRevision:$
26
// $LastChangedBy:$
27
//
28
package org.txm.rcp.handlers.export;
29

  
30
import java.io.File;
31
import java.io.IOException;
32

  
33
import org.eclipse.core.commands.AbstractHandler;
34
import org.eclipse.core.commands.ExecutionEvent;
35
import org.eclipse.core.commands.ExecutionException;
36
import org.eclipse.core.runtime.IProgressMonitor;
37
import org.eclipse.core.runtime.IStatus;
38
import org.eclipse.core.runtime.Status;
39
import org.eclipse.core.runtime.SubMonitor;
40
import org.eclipse.jface.viewers.IStructuredSelection;
41
import org.eclipse.osgi.util.NLS;
42
import org.eclipse.swt.SWT;
43
import org.eclipse.swt.widgets.FileDialog;
44
import org.eclipse.swt.widgets.Shell;
45
import org.eclipse.ui.handlers.HandlerUtil;
46
import org.txm.core.preferences.TBXPreferences;
47
import org.txm.core.results.TXMResult;
48
import org.txm.rcp.JobsTimer;
49
import org.txm.rcp.StatusLine;
50
import org.txm.rcp.handlers.files.EditFile;
51
import org.txm.rcp.messages.TXMUIMessages;
52
import org.txm.rcp.swt.dialog.LastOpened;
53
import org.txm.rcp.utils.JobHandler;
54
import org.txm.utils.ExecTimer;
55
import org.txm.utils.logger.Log;
56

  
57
/**
58
 * Exports a result by calling the function toTxt(File f) then opens or not the result in the text editor according to the preferences.
59
 * 
60
 * @author mdecorde, sjacquot
61
 */
62
public class ExportResultParameter extends AbstractHandler {
63
	
64
	private static final String ID = "org.txm.rcp.commands.function.ExportResult"; //$NON-NLS-1$
65
	
66
	/** The selection. */
67
	private IStructuredSelection selection;
68
	
69
	/** The lastopenedfile. */
70
	// private static String lastopenedfile;
71
	
72
	/**
73
	 * Export a TXM result in a CSV file.
74
	 *
75
	 * @param event the event
76
	 * @return the object
77
	 * @throws ExecutionException the execution exception
78
	 */
79
	@Override
80
	public Object execute(ExecutionEvent event) throws ExecutionException {
81
		selection = (IStructuredSelection) HandlerUtil.getCurrentSelection(event);
82
		final Object s = selection.getFirstElement();
83
		Shell shell = HandlerUtil.getActiveWorkbenchWindowChecked(event).getShell();
84
		
85
		// String txmhome = Toolbox.getTxmHomePath();
86
		FileDialog dialog = new FileDialog(shell, SWT.SAVE);
87
		
88
		String extensions[] = { "*.csv" }; //$NON-NLS-1$
89
		if (s instanceof TXMResult) {
90
			extensions = ((TXMResult) s).getExportTXTExtensions();
91
			dialog.setFileName(((TXMResult) s).getValidFileName());
92
		}
93
		dialog.setFilterExtensions(extensions);
94
		
95
		if (LastOpened.getFile(ID) != null) {
96
			dialog.setFilterPath(LastOpened.getFolder(ID));
97
		}
98
		
99
		if (dialog.open() != null) {
100
			StatusLine.setMessage(TXMUIMessages.exportingResults);
101
			String filepath = dialog.getFilterPath() + "/" + dialog.getFileName(); //$NON-NLS-1$
102
			if (!(filepath.endsWith(extensions[0].substring(1))))
103
				filepath += extensions[0].substring(1);
104
			
105
			final File outfile = new File(filepath);
106
			LastOpened.set(ID, outfile.getParent(), outfile.getName());
107
			try {
108
				outfile.createNewFile();
109
			}
110
			catch (IOException e1) {
111
				Log.warning(NLS.bind(TXMUIMessages.exportColonCantCreateFileP0ColonP1, outfile, e1));
112
			}
113
			if (!outfile.canWrite()) {
114
				Log.warning(NLS.bind(TXMUIMessages.impossibleToReadP0, outfile));
115
				return null;
116
			}
117
			if (!outfile.isFile()) {
118
				Log.warning("Error: " + outfile + " is not a file"); //$NON-NLS-1$ //$NON-NLS-2$
119
				return null;
120
			}
121
			
122
			final String encoding = TBXPreferences.getInstance().getString(TBXPreferences.EXPORT_ENCODING);
123
			String _colseparator = TBXPreferences.getInstance().getString(TBXPreferences.EXPORT_COL_SEPARATOR);
124
			final String colseparator = _colseparator;
125
			String _txtseparator = TBXPreferences.getInstance().getString(TBXPreferences.EXPORT_TXT_SEPARATOR);
126
			final String txtseparator = _txtseparator;
127
			
128
			JobHandler jobhandler = new JobHandler(TXMUIMessages.exportingResults) {
129
				
130
				@Override
131
				protected IStatus run(IProgressMonitor monitor) {
132
					
133
					// convert the monitor into sub-monitor
134
					SubMonitor subMonitor = SubMonitor.convert(monitor, TXMUIMessages.exporting, 100);
135
					
136
					try {
137
						
138
						this.runInit(subMonitor);
139
						
140
						if (s instanceof TXMResult) {
141
							TXMResult r = (TXMResult) s;
142
							
143
							// compute the result if needed
144
							if (!r.isAltered() && r.isDirty()) {
145
								r.compute(this);
146
							}
147
							Log.info(TXMUIMessages.bind(TXMUIMessages.exportingP0, r.getName()));
148
							ExecTimer.start();
149
							r.toTxt(outfile, encoding, colseparator, txtseparator);
150
							Log.info(TXMUIMessages.bind(TXMUIMessages.doneInP0, ExecTimer.stop()));
151
						}
152
						else {
153
							Log.warning(TXMUIMessages.TheExportedObjectIsNotATXMResultResult);
154
							return Status.CANCEL_STATUS;
155
						}
156
						
157
						if (outfile.exists()) {
158
							// Open internal editor in the UI thread
159
							if (TBXPreferences.getInstance().getBoolean(TBXPreferences.EXPORT_SHOW)) {
160
								this.syncExec(new Runnable() {
161
									
162
									@Override
163
									public void run() {
164
										EditFile.openfile(outfile.getAbsolutePath());
165
									}
166
								});
167
							}
168
						}
169
						else {
170
							Log.warning(NLS.bind(TXMUIMessages.failedToExportResultP0ColonP1, outfile.getAbsolutePath()));
171
						}
172
					}
173
					catch (ThreadDeath td) {
174
						TXMResult r = (TXMResult) s;
175
						r.resetComputingState();
176
						return Status.CANCEL_STATUS;
177
					}
178
					catch (Exception e) {
179
						Log.warning(NLS.bind(TXMUIMessages.failedToExportResultP0ColonP1, s, e));
180
						org.txm.utils.logger.Log.printStackTrace(e);
181
						return Status.CANCEL_STATUS;
182
					}
183
					finally {
184
						subMonitor.done();
185
						JobsTimer.stopAndPrint();
186
					}
187
					return Status.OK_STATUS;
188
				}
189
			};
190
			
191
			jobhandler.startJob();
192
		}
193
		return null;
194
	}
195
}
0 196

  
TXM/trunk/org.txm.rcp/src/main/java/org/txm/rcp/handlers/export/ExportResultChain.java (revision 3559)
1
// Copyright © 2010-2020 ENS de Lyon., University of Franche-Comté
2
// Copyright © 2007-2010 ENS de Lyon, CNRS, INRP, University of
3
// Lyon 2, University of Franche-Comté, University of Nice
4
// Sophia Antipolis, University of Paris 3.
5
//
6
// The TXM platform is free software: you can redistribute it
7
// and/or modify it under the terms of the GNU General Public
8
// License as published by the Free Software Foundation,
9
// either version 2 of the License, or (at your option) any
10
// later version.
... Ce différentiel a été tronqué car il excède la taille maximale pouvant être affichée.

Formats disponibles : Unified diff