Révision 3572

TXM/trunk/org.txm.core/src/java/org/txm/core/results/TXMResult.java (revision 3572)
5 5
import java.io.IOException;
6 6
import java.lang.reflect.Constructor;
7 7
import java.lang.reflect.Field;
8
import java.nio.file.Path;
8 9
import java.text.DateFormat;
9 10
import java.text.SimpleDateFormat;
10 11
import java.util.ArrayList;
TXM/trunk/org.txm.core.tests/projectSet.psf (revision 3572)
33 33
<project reference="1.0.1,https://forge.cbp.ens-lyon.fr/svn/txm/TXM/trunk/org.txm.concordance.core,org.txm.concordance.core,301a722c9435001c11f0c7133adee6fa;https://forge.cbp.ens-lyon.fr/svn/txm/;https://forge.cbp.ens-lyon.fr/svn/txm/;branches;tags;trunk;true;865c2c80-10f3-4e06-9fd7-61c4de9d188b;https://forge.cbp.ens-lyon.fr/svn/txm;;false;&lt;https://forge.cbp.ens-lyon.fr:443&gt; Redmine SVN Repository;;0"/>
34 34
<project reference="1.0.1,https://forge.cbp.ens-lyon.fr/svn/txm/TXM/trunk/org.txm.concordance.feature,org.txm.concordance.feature,301a722c9435001c11f0c7133adee6fa;https://forge.cbp.ens-lyon.fr/svn/txm/;https://forge.cbp.ens-lyon.fr/svn/txm/;branches;tags;trunk;true;865c2c80-10f3-4e06-9fd7-61c4de9d188b;https://forge.cbp.ens-lyon.fr/svn/txm;;false;&lt;https://forge.cbp.ens-lyon.fr:443&gt; Redmine SVN Repository;;0"/>
35 35
<project reference="1.0.1,https://forge.cbp.ens-lyon.fr/svn/txm/TXM/trunk/org.txm.concordance.rcp,org.txm.concordance.rcp,301a722c9435001c11f0c7133adee6fa;https://forge.cbp.ens-lyon.fr/svn/txm/;https://forge.cbp.ens-lyon.fr/svn/txm/;branches;tags;trunk;true;865c2c80-10f3-4e06-9fd7-61c4de9d188b;https://forge.cbp.ens-lyon.fr/svn/txm;;false;&lt;https://forge.cbp.ens-lyon.fr:443&gt; Redmine SVN Repository;;0"/>
36
<project reference="1.0.1,https://forge.cbp.ens-lyon.fr/svn/txm/TXM/trunk/org.txm.connlu.core,org.txm.conllu.core,301a722c9435001c11f0c7133adee6fa;https://forge.cbp.ens-lyon.fr/svn/txm/;https://forge.cbp.ens-lyon.fr/svn/txm/;branches;tags;trunk;true;865c2c80-10f3-4e06-9fd7-61c4de9d188b;https://forge.cbp.ens-lyon.fr/svn/txm;;false;&lt;https://forge.cbp.ens-lyon.fr:443&gt; Redmine SVN Repository;;0"/>
36
<project reference="1.0.1,https://forge.cbp.ens-lyon.fr/svn/txm/TXM/trunk/org.txm.conllu.core,org.txm.conllu.core,301a722c9435001c11f0c7133adee6fa;https://forge.cbp.ens-lyon.fr/svn/txm/;https://forge.cbp.ens-lyon.fr/svn/txm/;branches;tags;trunk;true;865c2c80-10f3-4e06-9fd7-61c4de9d188b;https://forge.cbp.ens-lyon.fr/svn/txm;;false;&lt;https://forge.cbp.ens-lyon.fr:443&gt; Redmine SVN Repository;;0"/>
37 37
<project reference="1.0.1,https://forge.cbp.ens-lyon.fr/svn/txm/TXM/trunk/org.txm.conllu.feature,org.txm.conllu.feature,301a722c9435001c11f0c7133adee6fa;https://forge.cbp.ens-lyon.fr/svn/txm/;https://forge.cbp.ens-lyon.fr/svn/txm/;branches;tags;trunk;true;865c2c80-10f3-4e06-9fd7-61c4de9d188b;https://forge.cbp.ens-lyon.fr/svn/txm;;false;&lt;https://forge.cbp.ens-lyon.fr:443&gt; Redmine SVN Repository;;0"/>
38 38
<project reference="1.0.1,https://forge.cbp.ens-lyon.fr/svn/txm/TXM/trunk/org.txm.conllu.rcp,org.txm.conllu.rcp,301a722c9435001c11f0c7133adee6fa;https://forge.cbp.ens-lyon.fr/svn/txm/;https://forge.cbp.ens-lyon.fr/svn/txm/;branches;tags;trunk;true;865c2c80-10f3-4e06-9fd7-61c4de9d188b;https://forge.cbp.ens-lyon.fr/svn/txm;;false;&lt;https://forge.cbp.ens-lyon.fr:443&gt; Redmine SVN Repository;;0"/>
39 39
<project reference="1.0.1,https://forge.cbp.ens-lyon.fr/svn/txm/TXM/trunk/org.txm.cooccurrence.chartsengine,org.txm.cooccurrence.chartsengine,301a722c9435001c11f0c7133adee6fa;https://forge.cbp.ens-lyon.fr/svn/txm/;https://forge.cbp.ens-lyon.fr/svn/txm/;branches;tags;trunk;true;865c2c80-10f3-4e06-9fd7-61c4de9d188b;https://forge.cbp.ens-lyon.fr/svn/txm;;false;&lt;https://forge.cbp.ens-lyon.fr:443&gt; Redmine SVN Repository;;0"/>
TXM/trunk/org.txm.annotation.kr.rcp/plugin.xml (revision 3572)
262 262
              icon="icons/functions/pencil_save.png"
263 263
              label="%command.label.111"
264 264
              style="push">
265
           <visibleWhen
266
                 checkEnabled="false">
267
              <reference
268
                    definitionId="CorpusNeedtoSaveAnnotation">
269
              </reference>
270
           </visibleWhen>
265 271
        </command>
266 272
     </menuContribution>
267 273
     <menuContribution
TXM/trunk/org.txm.annotation.kr.rcp/src/org/txm/annotation/kr/rcp/commands/SaveAnnotationsAndUpdateCorpus.java (revision 3572)
1 1
package org.txm.annotation.kr.rcp.commands;
2 2

  
3
import java.util.LinkedHashMap;
3
import java.util.ArrayList;
4
import java.util.Collections;
5
import java.util.List;
4 6

  
5 7
import org.eclipse.core.commands.AbstractHandler;
6 8
import org.eclipse.core.commands.ExecutionEvent;
......
14 16
import org.eclipse.osgi.util.NLS;
15 17
import org.eclipse.swt.widgets.Display;
16 18
import org.eclipse.ui.handlers.HandlerUtil;
17
import org.txm.annotation.kr.core.AnnotationManager;
18 19
import org.txm.annotation.kr.core.KRAnnotationEngine;
19 20
import org.txm.annotation.kr.core.preferences.KRAnnotationPreferences;
21
import org.txm.concordance.rcp.editors.ConcordanceEditor;
22
import org.txm.core.results.TXMResult;
20 23
import org.txm.objects.Workspace;
21 24
import org.txm.rcp.commands.workspace.UpdateCorpus;
22 25
import org.txm.rcp.messages.TXMUIMessages;
......
30 33
	
31 34
	public static final String ID = SaveAnnotationsAndUpdateCorpus.class.getCanonicalName();
32 35
	
33
	LinkedHashMap<String, MainCorpus> selectedCorporaToSave = null;
34
	
35 36
	@Override
36 37
	public Object execute(ExecutionEvent event) throws ExecutionException {
37 38
		
38
		selectedCorporaToSave = null;
39 39
		ISelection sel = HandlerUtil.getCurrentSelection(event);
40
		IStructuredSelection selection = null;
41
		if (sel instanceof IStructuredSelection) {
42
			selection = (IStructuredSelection) sel;
40
		if (!(sel instanceof IStructuredSelection)) {
41
			Log.info("No available selection to target the corpus annotations to save");	
42
			return null;
43 43
		}
44
		if (selection.size() > 0) {
45
			if (selection.getFirstElement() instanceof MainCorpus) {
46
				MainCorpus corpus = (MainCorpus) selection.getFirstElement();
47
				
48
				AnnotationManager am;
49
				try {
50
					am = KRAnnotationEngine.getAnnotationManager(corpus);
51
					if (am == null) {
52
						Log.warning("No annotation to save for: "+corpus);
53
						return null; // nothing to do
54
					}
55
					
56
					if (!KRAnnotationEngine.needToSaveAnnotations(corpus)) {
57
						Log.warning("No annotation to save for: "+corpus);
58
						return null;
59
					}
60
					
61
					selectedCorporaToSave = new LinkedHashMap<>();
62
					selectedCorporaToSave.put(corpus.getName(), corpus);
63
				}
64
				catch (Exception e) {
65
					// TODO Auto-generated catch block
66
					e.printStackTrace();
67
				}
68
				
69
				
70
				
44
		IStructuredSelection selection = (IStructuredSelection) sel;
45
		
46
		List<MainCorpus> corpora = Workspace.getInstance().getDeepChildren(MainCorpus.class);
47
		Collections.sort(corpora);
48
		ArrayList<MainCorpus> corpusWithAnnotationsToSave = new ArrayList<>();
49
		for (MainCorpus corpus : corpora) {
50
			if (KRAnnotationEngine.needToSaveAnnotations(corpus)) {
51
				corpusWithAnnotationsToSave.add(corpus);
71 52
			}
72 53
		}
73
		//		
74
		//		MainCorpus corpus = null;
75
		//
76
		//		if (HandlerUtil.getActiveEditor(event) instanceof ConcordanceEditor) {
77
		//			corpus = ((ConcordanceEditor) HandlerUtil.getActiveEditor(event)).getCorpus().getMainCorpus();
78
		//		} else {
79
		//			Object s = selection.getFirstElement();
80
		//			if (s == null) {
81
		//				Log.info("No available selection to target the corpus annotations to save");
82
		//				return null;
83
		//			} else if ((s instanceof MainCorpus)) {
84
		//				corpus = (MainCorpus) s;
85
		//			} else if (s instanceof TXMResult) {
86
		//				corpus = ((TXMResult) s).getFirstParent(MainCorpus.class);
87
		//			} else {
88
		//				Log.info("No available selection to target the corpus annotations to save");
89
		//				return null;
90
		//			}
91
		//		}
92
		//		
54

  
55
//		if (HandlerUtil.getActiveEditor(event) instanceof ConcordanceEditor) {
56
//			corpus = ((ConcordanceEditor) HandlerUtil.getActiveEditor(event)).getCorpus().getMainCorpus();
57
//		} else {
58
//			Object s = selection.getFirstElement();
59
//			if (s == null) {
60
//				Log.info("No available selection to target the corpus annotations to save");
61
//				return null;
62
//			} else if ((s instanceof MainCorpus)) {
63
//				corpus = (MainCorpus) s;
64
//			} else if (s instanceof TXMResult) {
65
//				corpus = ((TXMResult) s).getFirstParent(MainCorpus.class);
66
//			} else {
67
//				Log.info("No available selection to target the corpus annotations to save");
68
//				return null;
69
//			}
70
//		}
93 71
		
94
		LinkedHashMap<String, MainCorpus> corporaWithAnnotationsToSave = null;
95
		if (selectedCorporaToSave == null) {
96
			
97
			corporaWithAnnotationsToSave = new LinkedHashMap<>();
98
			for (MainCorpus corpus : Workspace.getInstance().getDeepChildren(MainCorpus.class)) {
99
				if (!corpus.getProject().isOpen()) continue;
100
				AnnotationManager am;
101
				try {
102
					am = KRAnnotationEngine.getAnnotationManager(corpus);
103
				}
104
				catch (Exception e) {
105
					// TODO Auto-generated catch block
106
					e.printStackTrace();
107
					continue;
108
				}
109
				if (am == null) {
110
					continue; // nothing to do
111
				}
112
				
113
				if (!KRAnnotationEngine.needToSaveAnnotations(corpus)) {
114
					continue;
115
				}
116
				
117
				corporaWithAnnotationsToSave.put(corpus.getName(), corpus);
118
			}
119
			
120
			if (corporaWithAnnotationsToSave.size() == 0) {
121
				Log.info("No CQP annotation found to save in the corpora.");
122
				return null;
123
			}
72
		if (corpusWithAnnotationsToSave.size() == 0) {
73
			Log.info(NLS.bind("No CQP annotations found to save in the corpora: {0}", corpora));
74
			return null;
124 75
		}
125 76
		
126 77
		try {
127
			//			if (!KRAnnotationEngine.needToSaveAnnotations(corpus)) {
128
			//				Log.info(NLS.bind("No CQP annotation found to save in the {0} corpus.", corpus));
129
			//				return null;
130
			//			}
131 78
			
132 79
			// System.out.println("DISPLAY="+Display.getDefault());
133 80
			final Boolean[] doit = new Boolean[1];
134 81
			doit[0] = true;
82
			final MainCorpus[] corpus = new MainCorpus[1];
135 83
			
136 84
			final Boolean[] doupdateedition = new Boolean[1];
137 85
			doupdateedition[0] = KRAnnotationPreferences.getInstance().getBoolean(KRAnnotationPreferences.UPDATE_EDITION);
138 86
			
139
			UpdateCorpusDialog dialog = new UpdateCorpusDialog(Display.getDefault().getActiveShell(), corporaWithAnnotationsToSave);
140
			dialog.setTitle("Select corpus to save and update options");
141
			dialog.setMessages(null, dialog.getBottomMessage());
142
			dialog.setDoForceUpdate(false);
143
			dialog.setDoUpdateEdition(KRAnnotationPreferences.getInstance().getBoolean(KRAnnotationPreferences.UPDATE_EDITION));
144
			
145 87
			Display.getDefault().syncExec(new Runnable() {
146 88
				
147 89
				@Override
148 90
				public void run() {
91
					UpdateCorpusDialog dialog = new UpdateCorpusDialog(Display.getDefault().getActiveShell());
92
					dialog.setTitle("Before saving annotations...");
93
					dialog.setMessages("Save Annotations and update the corpus.", dialog.getBottomMessage());
94
					dialog.setDoForceUpdate(false);
95
					dialog.setDoUpdateEdition(KRAnnotationPreferences.getInstance().getBoolean(KRAnnotationPreferences.UPDATE_EDITION));
96
					if (selection.getFirstElement() != null && selection.getFirstElement() instanceof MainCorpus) {
97
						dialog.setCorpusToUpdate((MainCorpus) selection.getFirstElement());
98
					}
99
					dialog.setCorporaToUpdate(corpusWithAnnotationsToSave);
149 100
					
150 101
					doit[0] = dialog.open() == Window.OK;
151 102
					doupdateedition[0] = dialog.getDoUpdateEdition();
152
					if (dialog.getSelectedCorpus() != null) {
153
						selectedCorporaToSave = dialog.getSelectedCorpus();
154
					}
103
					corpus[0] = dialog.getCorpustoUpdate();
155 104
				}
156 105
			});
157 106
			if (doit[0] == false) {
158 107
				return null;
159 108
			}
160 109
			
161
			if (selectedCorporaToSave.size() == 0) {
162
				Log.info("No corpus selected. Aborting.");
110
			final MainCorpus fcorpus = corpus[0];
111
			
112
			Log.info(NLS.bind("Saving {0} annotations...", fcorpus.getName()));
113
			JobHandler job = SaveAnnotations.save(fcorpus);
114
			if (job == null) {
163 115
				return null;
164 116
			}
165
			
166
			
167
			JobHandler job = null;
168
			
169
			Log.info(NLS.bind("Saving {0} annotations...", selectedCorporaToSave.keySet()));
170
			for (MainCorpus corpus :  selectedCorporaToSave.values()) {
171
				try {
172
					job = SaveAnnotations.save(corpus);
173
					if (job == null) {
174
						return null;
175
					} else {
176
						job.join();
177
					}
178
				}
179
				catch (Exception e) {
180
					// TODO Auto-generated catch block
181
					e.printStackTrace();
182
					return null;
183
				}
184
			}
185
			
186
			if (job != null && job.getState() == Status.OK_STATUS.getCode()) {
117

  
118
			if (job.getState() == Status.OK_STATUS.getCode()) {
187 119
				if (KRAnnotationPreferences.getInstance().getBoolean(KRAnnotationPreferences.UPDATE)) {
188 120
					
189 121
					JobHandler job2 = new JobHandler("Updating corpus editions and indexes", true) {
......
193 125
							this.runInit(monitor);
194 126
							
195 127
							try {
196
								monitor.setTaskName("Updating corpora: "+selectedCorporaToSave.values());
197
								for (MainCorpus fcorpus :  selectedCorporaToSave.values()) {
198
									JobHandler job2 = UpdateCorpus.update(fcorpus, false, doupdateedition[0]);
199
									if (fcorpus != null && job2 != null) {
200
										try {
201
											job2.join();
128
								monitor.setTaskName("Updating corpus");
129
								if (fcorpus != null && UpdateCorpus.update(fcorpus, false, doupdateedition[0]) != null) {
130
									monitor.worked(50);
131
									this.syncExec(new Runnable() {
132
										
133
										@Override
134
										public void run() {
135
											CorporaView.refreshObject(fcorpus);
202 136
										}
203
										catch (InterruptedException e) {
204
											// TODO Auto-generated catch block
205
											e.printStackTrace();
206
										}
207
										monitor.worked(50);
208
									}
209
									else {
210
										monitor.worked(50);
211
										Log.warning("Fail to update corpus. Aborting");
212
										Log.warning("Fix XML-TXM files and call command 'UpdateCorpus'");
213
										return Status.CANCEL_STATUS;
214
									}
137
									});
138
									return Status.OK_STATUS;
215 139
								}
216
								
217
								this.syncExec(new Runnable() {
218
									
219
									@Override
220
									public void run() {
221
										CorporaView.refresh();
222
									}
223
								});
224
								return Status.OK_STATUS;
225
								
140
								else {
141
									monitor.worked(50);
142
									Log.warning("Fail to update corpus. Aborting");
143
									Log.warning("Fix XML-TXM files and call command 'UpdateCorpus'");
144
									return Status.CANCEL_STATUS;
145
								}
226 146
							} catch(ThreadDeath ex) {
227 147
								Log.info(TXMUIMessages.executionCanceled);
228 148
								return Status.CANCEL_STATUS;
......
234 154
					Log.warning("CQP indexes not updated. Only the XML-TXM files have been updated. Call Update corpus later to access the annotations");
235 155
				}
236 156
			}
157

  
158
			CorporaView.refresh();
159
			
160
			return fcorpus;
237 161
		}
238 162
		catch (Exception e) {
239 163
			// TODO Auto-generated catch block
......
241 165
			return null;
242 166
		}
243 167
		
244
		CorporaView.refresh();
245
		
246
		return corporaWithAnnotationsToSave;
247 168
	}
248 169
}
TXM/trunk/org.txm.annotation.kr.rcp/src/org/txm/annotation/kr/rcp/commands/SaveAnnotations.java (revision 3572)
1 1
package org.txm.annotation.kr.rcp.commands;
2 2

  
3
import java.util.ArrayList;
4
import java.util.HashMap;
5
import java.util.LinkedHashMap;
6

  
7 3
import org.eclipse.core.commands.AbstractHandler;
8 4
import org.eclipse.core.commands.ExecutionEvent;
9 5
import org.eclipse.core.commands.ExecutionException;
......
15 11
import org.eclipse.jface.viewers.IStructuredSelection;
16 12
import org.eclipse.osgi.util.NLS;
17 13
import org.eclipse.swt.widgets.Display;
18
import org.eclipse.ui.dialogs.ListDialog;
19
import org.eclipse.ui.dialogs.ListSelectionDialog;
20 14
import org.eclipse.ui.handlers.HandlerUtil;
21 15
import org.txm.annotation.kr.core.AnnotationManager;
22 16
import org.txm.annotation.kr.core.KRAnnotationEngine;
23
import org.txm.objects.Workspace;
24 17
import org.txm.rcp.messages.TXMUIMessages;
25
import org.txm.rcp.swt.dialog.ListMultiSelectionDialog;
26
import org.txm.rcp.swt.dialog.MultipleChoiceDialog;
27
import org.txm.rcp.swt.dialog.MultipleValueDialog;
28 18
import org.txm.rcp.utils.JobHandler;
29 19
import org.txm.rcp.utils.SWTEditorsUtils;
30 20
import org.txm.rcp.views.corpora.CorporaView;
......
38 28
	@Override
39 29
	public Object execute(ExecutionEvent event) throws ExecutionException {
40 30
		
41
		LinkedHashMap<String, MainCorpus> corporaWithAnnotationsToSave = new LinkedHashMap<>();
42
		for (MainCorpus corpus : Workspace.getInstance().getDeepChildren(MainCorpus.class)) {
43
			if (!corpus.getProject().isOpen()) continue;
44
			AnnotationManager am;
45
			try {
46
				am = KRAnnotationEngine.getAnnotationManager(corpus);
47
			}
48
			catch (Exception e) {
49
				// TODO Auto-generated catch block
50
				e.printStackTrace();
51
				continue;
52
			}
53
			if (am == null) {
54
				continue; // nothing to do
55
			}
56
			
57
			if (!KRAnnotationEngine.needToSaveAnnotations(corpus)) {
58
				continue;
59
			}
60
			
61
			corporaWithAnnotationsToSave.put(corpus.getName(), corpus);
62
		}
31
		ISelection sel = HandlerUtil.getCurrentSelection(event);
32
		if (!(sel instanceof IStructuredSelection)) return null;
33
		IStructuredSelection selection = (IStructuredSelection) sel;
63 34
		
64
		if (corporaWithAnnotationsToSave.size() == 0) {
65
			Log.info("No annotation to do in corpora.");
35
		Object s = selection.getFirstElement();
36
		if (!(s instanceof MainCorpus))
66 37
			return null;
38
		MainCorpus corpus = (MainCorpus) s;
39
		try {
40
			JobHandler job = save(corpus);
41
			if (job == null) {
42
				return null;
43
			}
67 44
		}
68
		
69
		ListMultiSelectionDialog<MainCorpus> dialog = new ListMultiSelectionDialog<MainCorpus>(HandlerUtil.getActiveShell(event), "Select corpus to save", "Select in the list the corpus you wich to save annotations", corporaWithAnnotationsToSave);
70
		if (dialog.open() != MultipleChoiceDialog.OK) {
45
		catch (Exception e) {
46
			// TODO Auto-generated catch block
47
			e.printStackTrace();
71 48
			return null;
72 49
		}
73 50
		
74
		//		Object s = selection.getFirstElement();
75
		//		if (!(s instanceof MainCorpus))
76
		//			return null;
77
		
78
		for (MainCorpus corpus :  dialog.getSelectedValues().values()) {
79
			try {
80
				JobHandler job = save(corpus);
81
				if (job == null) {
82
					return null;
83
				} else {
84
					job.join();
85
				}
86
			}
87
			catch (Exception e) {
88
				// TODO Auto-generated catch block
89
				e.printStackTrace();
90
				return null;
91
			}
92
		}
93
		
94 51
		CorporaView.refresh();
95 52
		
96
		return dialog.getSelectedValues();
53
		return corpus;
97 54
	}
98 55
	
99 56
	public static JobHandler save(final MainCorpus corpus) throws Exception {
TXM/trunk/org.txm.rcp/src/main/java/org/txm/rcp/swt/dialog/UpdateCorpusDialog.java (revision 3572)
1 1
package org.txm.rcp.swt.dialog;
2 2

  
3
import java.util.LinkedHashMap;
3
import java.util.ArrayList;
4 4

  
5 5
import org.eclipse.jface.dialogs.Dialog;
6
import org.eclipse.jface.viewers.ArrayContentProvider;
6 7
import org.eclipse.jface.viewers.IStructuredSelection;
8
import org.eclipse.jface.viewers.ListViewer;
9
import org.eclipse.jface.viewers.StructuredSelection;
7 10
import org.eclipse.swt.SWT;
8 11
import org.eclipse.swt.layout.GridData;
9 12
import org.eclipse.swt.layout.GridLayout;
13
import org.eclipse.swt.layout.RowLayout;
10 14
import org.eclipse.swt.widgets.Button;
11 15
import org.eclipse.swt.widgets.Composite;
12 16
import org.eclipse.swt.widgets.Control;
17
import org.eclipse.swt.widgets.Group;
13 18
import org.eclipse.swt.widgets.Label;
14 19
import org.eclipse.swt.widgets.List;
15 20
import org.eclipse.swt.widgets.Shell;
16
import org.txm.objects.CorpusBuild;
21
import org.txm.objects.Workspace;
17 22
import org.txm.rcp.messages.TXMUIMessages;
23
import org.txm.rcp.swt.GLComposite;
24
import org.txm.rcp.swt.provider.SimpleLabelProvider;
25
import org.txm.searchengine.cqp.CQPSearchEngine;
18 26
import org.txm.searchengine.cqp.corpus.MainCorpus;
19 27

  
20 28
public class UpdateCorpusDialog extends Dialog {
21

  
29
	
22 30
	protected String title = TXMUIMessages.UpdateCorpus;
23 31
	protected String topMessage = TXMUIMessages.UpdateTheCorpusFromItsXMLTXMFiles;
24 32
	protected String bottomMessage = TXMUIMessages.AllEditorsUsingThisCorpusWillBeClosedandtheCorpusWillBeUpdatedContinue;
......
30 38
	public boolean doOpenCorpusParameters = false;
31 39
	public boolean doUpdateEditions = false;
32 40
	public boolean doForceUpdate = false;
33
	private LinkedHashMap<String, MainCorpus> corpora;
34
	private LinkedHashMap<String, MainCorpus> selectedCorpora;
35
	private List valuesList;
41
	public MainCorpus corpusToUpdate = null;
42
	private Button modifiesTextsCheck;
43
	private ListViewer corpusList;
44
	private java.util.List<MainCorpus> corpora;
36 45
	
37
	public UpdateCorpusDialog(Shell parentShell, LinkedHashMap<String, MainCorpus> corpora) {
46
	
47
	
48
	public UpdateCorpusDialog(Shell parentShell) {
38 49
		
39 50
		super(parentShell);
40
		this.corpora = corpora;
41 51
	}
42 52
	
53
	
43 54
	@Override
44 55
	protected void configureShell(Shell newShell) {
45 56
		super.configureShell(newShell);
......
48 59
	
49 60
	@Override
50 61
	protected Control createDialogArea(Composite parent) {
62
		
51 63
		Composite comp = (Composite) super.createDialogArea(parent);
52

  
64
		
53 65
		GridLayout layout = (GridLayout) comp.getLayout();
54 66
		layout.numColumns = 1;
55 67
		layout.verticalSpacing = 20;
56 68
		layout.marginWidth = 15;
57 69
		
58
		if (topMessage !=null && topMessage.length() > 0) {
59
			createTopMessageArea(comp);
60
		}
70
		createTopMessageArea(comp);
61 71
		
62
		if (corpora != null && corpora.size() > 1) {
63
			createCorporaSelectionArea(comp);
64
		} else {
65
			selectedCorpora = corpora;
66
		}
67
		
68 72
		createButtonsArea(comp);
69 73
		
70 74
		createBottomMessageArea(comp);
......
72 76
		return comp;
73 77
	}
74 78
	
75
	private void createCorporaSelectionArea(Composite p) {
79
	protected void createButtonsArea(Composite comp) {
76 80
		
77
		this.selectedCorpora = new LinkedHashMap<>();
81
		GridData data = null;
78 82
		
79
		Label valuesListLabel = new Label(p, SWT.NONE);
80
		valuesListLabel.setText("Select the corpus annotations to save :");
83
		if (corpora != null) {
84
			
85
			GLComposite line = new GLComposite(comp, SWT.NONE, "");
86
			data = new GridData(GridData.FILL, GridData.FILL, true, false);
87
			line.setLayoutData(data);
88
			line.getLayout().numColumns = 1;
89
			
90
			Label l = new Label(line, SWT.NONE);
91
			data = new GridData(GridData.FILL, GridData.FILL, false, false);
92
			l.setLayoutData(data);
93
			l.setText(TXMUIMessages.corpusToProcess);
94
			data = new GridData(GridData.FILL, GridData.FILL, true, false);
95
			
96
			corpusList = new ListViewer(line, SWT.SINGLE | SWT.READ_ONLY);
97
			data = new GridData(GridData.FILL, GridData.FILL, true, false);
98
			corpusList.getList().setLayoutData(data);
99
			corpusList.setLabelProvider(new SimpleLabelProvider());
100
			corpusList.setContentProvider(new ArrayContentProvider());
101
			
102
			corpusList.setInput(corpora);
103
			
104
			if (corpusToUpdate != null && corpora.contains(corpusToUpdate)) {
105
				corpusList.setSelection(new StructuredSelection(corpusToUpdate));
106
			}
107
			else if (corpora.size() > 0) {
108
				corpusList.getList().select(0);
109
			}
110
		}
81 111
		
82
		valuesList = new List(p, SWT.MULTI | SWT.BORDER);
83
		GridData data = new GridData(GridData.FILL_HORIZONTAL);
84
		valuesList.setLayoutData(data);
85
		valuesList.setItems(corpora.keySet().toArray(new String[corpora.size()]));
86
		valuesList.selectAll();
87
	}
88

  
89
	protected void createButtonsArea(Composite comp) {
112
		Group textsGroup = new Group(comp, SWT.NONE);
113
		data = new GridData(GridData.FILL, GridData.FILL, true, false);
114
		textsGroup.setLayoutData(data);
115
		textsGroup.setLayout(new RowLayout(SWT.HORIZONTAL));
116
		textsGroup.setText(TXMUIMessages.textsToProcess);
90 117
		
91
		Label titleLabel = new Label(comp, SWT.NONE);
92
		titleLabel.setText("Options :");
118
		modifiesTextsCheck = new Button(textsGroup, SWT.RADIO);
119
		modifiesTextsCheck.setText(TXMUIMessages.withModifications);
120
		modifiesTextsCheck.setToolTipText("Only the modified corpus texts are updated");
121
		modifiesTextsCheck.setSelection(!doForceUpdate);
93 122
		
94
		GridData data = new GridData(GridData.FILL_HORIZONTAL);
123
		forceUpdateCheck = new Button(textsGroup, SWT.RADIO);
124
		forceUpdateCheck.setText(TXMUIMessages.common_all);
125
		forceUpdateCheck.setToolTipText("All the corpus texts will be updated, modified or not");
126
		forceUpdateCheck.setSelection(doForceUpdate);
95 127
		
96
		openCorpusParameters = new Button(comp, SWT.CHECK);
97
		data = new GridData(GridData.FILL_HORIZONTAL);
98
		openCorpusParameters.setLayoutData(data);
99
		openCorpusParameters.setText(TXMUIMessages.ShowMoreUpdateParametersBeforeUpdating);
100
		openCorpusParameters.setToolTipText(TXMUIMessages.UseThisToChangeTheCorpusParametersBeforeUpdatingTheCorpus);
101
		openCorpusParameters.setSelection(doOpenCorpusParameters);
102

  
128
		//		Group todoGroup = new Group(comp, SWT.NONE);
129
		//		data = new GridData(GridData.FILL, GridData.FILL, true, false);
130
		//		todoGroup.setLayoutData(data);
131
		//		todoGroup.setLayout(new RowLayout(SWT.HORIZONTAL));
132
		//		todoGroup.setText("Update work to do");
133
		//		
134
		//		Button updateIndexCheck = new Button(todoGroup, SWT.CHECK);
135
		//		data = new GridData(GridData.FILL_HORIZONTAL);
136
		//		//updateIndexCheck.setLayoutData(data);
137
		//		updateIndexCheck.setText("Search indexes");
138
		//		updateIndexCheck.setToolTipText("This work cannot be disabled");
139
		//		updateIndexCheck.setSelection(true);
140
		//		updateIndexCheck.setEnabled(false);
141
		
142
		
103 143
		updateEditionsCheck = new Button(comp, SWT.CHECK);
104 144
		data = new GridData(GridData.FILL_HORIZONTAL);
105 145
		updateEditionsCheck.setLayoutData(data);
......
107 147
		updateEditionsCheck.setToolTipText(TXMUIMessages.IfNotCheckedTheEditionPagesAreNotUpdatedAndMightNotReflectThechanges);
108 148
		updateEditionsCheck.setSelection(doUpdateEditions);
109 149
		
110
		forceUpdateCheck = new Button(comp, SWT.CHECK);
150
		openCorpusParameters = new Button(comp, SWT.CHECK);
111 151
		data = new GridData(GridData.FILL_HORIZONTAL);
112
		forceUpdateCheck.setLayoutData(data);
113
		forceUpdateCheck.setText(TXMUIMessages.ForceUpdateOfNonModifiedTexts);
114
		forceUpdateCheck.setToolTipText(TXMUIMessages.WhenCheckedThisOptionMakeTXMUpdateAllIndexesAndEditionsOfAllTexts);
115
		forceUpdateCheck.setSelection(doForceUpdate);
152
		openCorpusParameters.setLayoutData(data);
153
		openCorpusParameters.setText(TXMUIMessages.ShowMoreUpdateParametersBeforeUpdating);
154
		openCorpusParameters.setToolTipText(TXMUIMessages.UseThisToChangeTheCorpusParametersBeforeUpdatingTheCorpus);
155
		openCorpusParameters.setSelection(doOpenCorpusParameters);
116 156
	}
117

  
157
	
118 158
	protected void createTopMessageArea(Composite comp) {
119 159
		
120 160
		Label mainMessage = new Label(comp, SWT.WRAP);
......
130 170
		GridData data = new GridData(GridData.FILL_HORIZONTAL);
131 171
		mainMessage.setLayoutData(data);
132 172
	}
133

  
173
	
134 174
	@Override
135 175
	protected void buttonPressed(int buttonId) {
136 176
		if (buttonId == Dialog.OK) {
137 177
			doUpdateEditions = updateEditionsCheck.getSelection();
138 178
			doForceUpdate = forceUpdateCheck.getSelection();
139 179
			doOpenCorpusParameters = openCorpusParameters.getSelection();
140
			if (corpora != null) {
141
				selectedCorpora = new LinkedHashMap<>();
142
				for (String item : valuesList.getSelection()) {
143
					selectedCorpora.put(item, corpora.get(item));
144
				}
145
			}
146 180
		}
147 181
		
148 182
		super.buttonPressed(buttonId);
149 183
	}
150

  
184
	
151 185
	/**
152 186
	 * 
153 187
	 * @return the update edition button value AFTER this dialog is closed
......
177 211
	
178 212
	/**
179 213
	 * 
214
	 * @return the selected corpus to update
215
	 */
216
	public MainCorpus getCorpustoUpdate() {
217
		
218
		return this.corpusToUpdate;
219
	}
220
	
221
	/**
222
	 * 
223
	 * @return the selected corpus to update
224
	 */
225
	public void setCorpusToUpdate(MainCorpus corpus) {
226
		
227
		this.corpusToUpdate = corpus;
228
	}
229
	
230
	/**
231
	 * 
232
	 * @return set the corpora to save
233
	 */
234
	public void setCorporaToUpdate(java.util.List<MainCorpus> corpora) {
235
		
236
		this.corpora = corpora;
237
	}
238
	
239
	/**
240
	 * 
180 241
	 * @return the force update button value AFTER this dialog is closed
181 242
	 */
182 243
	public void setDoForceUpdate(boolean doForceUpdate) {
183 244
		
184 245
		this.doForceUpdate = doForceUpdate;
185 246
	}
186

  
247
	
187 248
	public boolean getDoOpenCorpusParameters() {
188 249
		
189 250
		return doOpenCorpusParameters;
......
193 254
		
194 255
		this.doOpenCorpusParameters = doOpenCorpusParameters;
195 256
	}
196

  
257
	
197 258
	public void setMessages(String topMessage, String bottomMessage) {
198 259
		
199 260
		this.topMessage = topMessage;
......
212 273
		
213 274
		this.title = title;
214 275
	}
215

  
216
	public LinkedHashMap<String, MainCorpus> getSelectedCorpus() {
217
		
218
		return selectedCorpora;
219
	}
220 276
}
TXM/trunk/org.txm.rcp/src/main/java/org/txm/rcp/handlers/export/ExportResultChain.java (revision 3572)
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 ExportResultChain extends AbstractHandler {
65
	
66
	private static final String ID = ExportResultChain.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/ImportResultChain.java (revision 3572)
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
}
TXM/trunk/org.txm.rcp/src/main/java/org/txm/rcp/handlers/export/ExportCommand.java (revision 3572)
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.HashSet;
33

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

  
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 ExportCommand extends AbstractHandler {
62
	
63
	/**
64
	 * Export a TXM result parameters in the preferences format
65
	 *
66
	 * @param event the event
67
	 * @return the object
68
	 * @throws ExecutionException the execution exception
69
	 */
70
	@Override
71
	public Object execute(ExecutionEvent event) throws ExecutionException {
72
		IStructuredSelection selection = (IStructuredSelection) HandlerUtil.getCurrentSelection(event);
73
		
74
		Object o = selection.getFirstElement();
75
		if (selection .isEmpty() || !(o instanceof TXMResult)) {
76
			Log.warning("Slection must be a TXMResult");
77
		}
78
		
79
		TXMResult result = (TXMResult)o;
80
		
81
		Shell shell = HandlerUtil.getActiveWorkbenchWindowChecked(event).getShell();
82
		
83
		ExportCommandDialog dialog = new ExportCommandDialog(shell, result);
84
		
85
		if (dialog.open() == dialog.OK) {
86
			StatusLine.setMessage(TXMUIMessages.exportingResults);
87
			
88
			
89
			String filepath = dialog.getZipFile().getAbsolutePath();
90
			if (!(filepath.endsWith(".txmcmd"))) {
91
				filepath += filepath + ".txmcmd";
92
			}
93
			
94
			final File zipfiledir = new File(filepath); // temporary directory to setup the archive
95
			LastOpened.set(ExportCommand.class.getName(), zipfiledir.getParent(), zipfiledir.getName()); 
96
			zipfiledir.delete();
97
			zipfiledir.mkdirs();
98
			
99
			final File zipfile = new File(filepath+".tmp");
100
			
101
			JobHandler jobhandler = new JobHandler(TXMUIMessages.exportingResults) {
102
				
103
				@Override
104
				protected IStatus run(IProgressMonitor monitor) {
105
					
106
					// convert the monitor into sub-monitor
107
					SubMonitor subMonitor = SubMonitor.convert(monitor, TXMUIMessages.exporting, 100);
108
					this.runInit(subMonitor);
109
					
110
					try {
111
						
112
						HashSet<TXMResult> toExport = new HashSet<TXMResult>();
113
						for (Object o : selection.toList()) {
114
							if (!(o instanceof TXMResult)) continue;
115
							TXMResult r = (TXMResult)o;
116
							toExport.add(r);
117
							TXMResult parent = ((TXMResult)o).getParent();
118
							
119
							// add invisible parents to rebuild the result
120
							while(parent != null && !parent.isVisible()) { // insivisble parent must be added to the exported results
121
								
122
								toExport.add(parent);
123
								parent = parent.getParent();
124
							}
125
							
126
							// add children 
127
							if (dialog.getExportChildren()) {
128
								for (TXMResult c : r.getDeepChildren()) {
129
									toExport.add(c);
130
								}
131
							}
132
							
133
							// add parents 
134
							if (dialog.getExportParents()) {
135
								TXMResult p = r.getParent();
136
								while (p != null && (!(p instanceof MainCorpus))) {
137
									toExport.add(p);
138
									p = p.getParent();
139
								}
140
							}
141
						}
142
						
143
						ArrayList<File> files = new ArrayList<>();
144
						for (TXMResult r : toExport) {
145
							
146
							if (r.isAltered()) {
147
								System.out.println(TXMUIMessages.ExportResultParameters_WarningOnlyTheParametersAreExportedManualChangesAreNotTransfered);
148
							}
149
							else {
150
								r.compute(subMonitor); // refresh result
151
							}
152
							
153
							File outfile = new File(zipfiledir, (TXMPreferences.getNode(r.getParametersNodePath()).name()+".parameters"));
154
							if (r.toParametersFile(outfile)) {
155
								files.add(outfile);
156
							}
157
						}
158
						
159
						System.out.println(NLS.bind(TXMUIMessages.ExportResultParameters_ParametersExportedToTheP0File, zipfiledir));
160
						Zip.compress(files.toArray(new File[files.size()]), zipfile, Zip.DEFAULT_LEVEL_COMPRESSION, null, null);
161
						DeleteDir.deleteDirectory(zipfiledir);
162
						zipfile.renameTo(zipfiledir);
163
					}
164
					catch (ThreadDeath td) {
165
						
166
						return Status.CANCEL_STATUS;
167
					}
168
					catch (Exception e) {
169
						System.out.println(NLS.bind(TXMUIMessages.failedToExportResultP0ColonP1, selection.toList(), e));
170
						org.txm.utils.logger.Log.printStackTrace(e);
171
						return Status.CANCEL_STATUS;
172
					}
173
					finally {
174
						subMonitor.done();
175
						JobsTimer.stopAndPrint();
176
					}
177
					return Status.OK_STATUS;
178
				}
179
			};
180
			
181
			jobhandler.startJob();
182
		}
183
		return null;
184
	}
185
}
0 186

  
TXM/trunk/org.txm.rcp/src/main/java/org/txm/rcp/handlers/export/ExportCommandDialog.java (revision 3572)
1
package org.txm.rcp.handlers.export;
2

  
3
import java.io.File;
4
import java.util.Calendar;
5
import org.eclipse.jface.dialogs.Dialog;
6
import org.eclipse.jface.dialogs.IDialogConstants;
7
import org.eclipse.osgi.util.NLS;
8
import org.eclipse.swt.SWT;
9
import org.eclipse.swt.events.SelectionEvent;
10
import org.eclipse.swt.events.SelectionListener;
11
import org.eclipse.swt.layout.GridData;
12
import org.eclipse.swt.layout.GridLayout;
13
import org.eclipse.swt.widgets.Button;
14
import org.eclipse.swt.widgets.Composite;
15
import org.eclipse.swt.widgets.Control;
16
import org.eclipse.swt.widgets.FileDialog;
17
import org.eclipse.swt.widgets.Label;
18
import org.eclipse.swt.widgets.Shell;
19
import org.eclipse.swt.widgets.Text;
20
import org.txm.Toolbox;
21
import org.txm.core.preferences.TXMPreferences;
22
import org.txm.core.results.TXMResult;
23

  
24
/**
25
 * 
26
 * Allow to select the file to create and select elements to include in the binary
27
 * 
28
 */
29
public class ExportCommandDialog extends Dialog {
30
	
31
	private File zipFile;
32
	
33
	private TXMResult result;
34
	
35
	private Text textField;
36
	
37
	private Button selectButton;
38

  
39
	private Button exportChildrenButton;
40

  
41
	private Button exportParentsButton;
42

  
43
	private boolean exportParents;
44

  
45
	private boolean exportChildren;
46
	
47
	public final static String ID = ExportCommandDialog.class.getName();
48
	
49
	/**
50
	 * 
51
	 * @param parentShell
52
	 * @param title
53
	 * @param question
54
	 * @param text
55
	 */
56
	public ExportCommandDialog(Shell parentShell, TXMResult result) {
57
		super(parentShell);
58
		this.result = result;
59
	}
60
	
61
	@Override
62
	protected void configureShell(Shell newShell) {
63
		super.configureShell(newShell);
64
		newShell.setText(NLS.bind("Exporting command {0}", result.getName()));
65
		newShell.setMinimumSize(400, 300);
66
	}
67
	
68
	@Override
69
	protected Control createDialogArea(Composite parent) {
70
		
71
		Composite comp = (Composite) super.createDialogArea(parent);
72
		
73
		GridLayout layout = (GridLayout) comp.getLayout();
74
		layout.numColumns = 3;
75
		
76
		Label l = new Label(comp, SWT.LEFT);
77
		l.setText("Output file");
78
		l.setLayoutData(new GridData(GridData.BEGINNING, GridData.CENTER, false, false));
79
		
80
		textField = new Text(comp, SWT.SINGLE | SWT.BORDER);
81
		textField.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false));
82
		String previousPath = TXMPreferences.getString("path", ExportCommandDialog.class.getName()); //$NON-NLS-1$
83
		if (previousPath == null || previousPath.length() == 0) {
84
			previousPath = System.getProperty("user.home"); // //$NON-NLS-1$
85
		}
86
		textField.setText(previousPath + "/" + result.getProject().getName().toUpperCase() + "-" +result.getResultType() + "-" +result.getValidFileName() + "-"+ Toolbox.dateformat.format(Calendar.getInstance().getTime()) + ".txmcmd"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
87
		
88
		selectButton = new Button(comp, SWT.PUSH);
89
		selectButton.setText("..."); //$NON-NLS-1$
90
		selectButton.addSelectionListener(new SelectionListener() {
91
			
92
			@Override
93
			public void widgetSelected(SelectionEvent e) {
94
				FileDialog dialog = new FileDialog(selectButton.getShell(), SWT.SAVE);
... Ce différentiel a été tronqué car il excède la taille maximale pouvant être affichée.

Formats disponibles : Unified diff