Révision 3819

TXM/trunk/bundles/org.txm.rcp/src/main/java/org/txm/rcp/handlers/export/ExportResultFromEditor.java (revision 3819)
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.IEditorPart;
46
import org.eclipse.ui.handlers.HandlerUtil;
47
import org.txm.core.preferences.TBXPreferences;
48
import org.txm.core.results.TXMResult;
49
import org.txm.rcp.JobsTimer;
50
import org.txm.rcp.StatusLine;
51
import org.txm.rcp.editors.ITXMResultEditor;
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 ExportResultFromEditor extends AbstractHandler {
65
	
66
	private static final String ID = ExportResultFromEditor.class.getName();
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
		
84
		IEditorPart ieditor = HandlerUtil.getActiveEditor(event);
85
		if (!(ieditor instanceof ITXMResultEditor)) {
86
			return null;
87
		}
88
		
89
		ITXMResultEditor<TXMResult> editor = (ITXMResultEditor)ieditor;
90
		final Object s = editor.getResult();
91
		
92
		Shell shell = HandlerUtil.getActiveWorkbenchWindowChecked(event).getShell();
93
		
94
		// String txmhome = Toolbox.getTxmHomePath();
95
		FileDialog dialog = new FileDialog(shell, SWT.SAVE);
96
		
97
		String extensions[] = { "*.csv" }; //$NON-NLS-1$
98
		if (s instanceof TXMResult) {
99
			extensions = ((TXMResult) s).getExportTXTExtensions();
100
			dialog.setFileName(((TXMResult) s).getValidFileName());
101
		}
102
		dialog.setFilterExtensions(extensions);
103
		
104
		if (LastOpened.getFile(ID) != null) {
105
			dialog.setFilterPath(LastOpened.getFolder(ID));
106
		}
107
		
108
		if (dialog.open() != null) {
109
			StatusLine.setMessage(TXMUIMessages.exportingResults);
110
			String filepath = dialog.getFilterPath() + "/" + dialog.getFileName(); //$NON-NLS-1$
111
			if (!(filepath.endsWith(extensions[0].substring(1))))
112
				filepath += extensions[0].substring(1);
113
			
114
			final File outfile = new File(filepath);
115
			LastOpened.set(ID, outfile.getParent(), outfile.getName());
116
			try {
117
				outfile.createNewFile();
118
			}
119
			catch (IOException e1) {
120
				Log.warning(NLS.bind(TXMUIMessages.exportColonCantCreateFileP0ColonP1, outfile, e1));
121
			}
122
			if (!outfile.canWrite()) {
123
				Log.warning(NLS.bind(TXMUIMessages.impossibleToReadP0, outfile));
124
				return null;
125
			}
126
			if (!outfile.isFile()) {
127
				Log.warning("Error: " + outfile + " is not a file"); //$NON-NLS-1$ //$NON-NLS-2$
128
				return null;
129
			}
130
			
131
			final String encoding = TBXPreferences.getInstance().getString(TBXPreferences.EXPORT_ENCODING);
132
			String _colseparator = TBXPreferences.getInstance().getString(TBXPreferences.EXPORT_COL_SEPARATOR);
133
			final String colseparator = _colseparator;
134
			String _txtseparator = TBXPreferences.getInstance().getString(TBXPreferences.EXPORT_TXT_SEPARATOR);
135
			final String txtseparator = _txtseparator;
136
			
137
			JobHandler jobhandler = new JobHandler(TXMUIMessages.exportingResults) {
138
				
139
				@Override
140
				protected IStatus run(IProgressMonitor monitor) {
141
					
142
					// convert the monitor into sub-monitor
143
					SubMonitor subMonitor = SubMonitor.convert(monitor, TXMUIMessages.exporting, 100);
144
					
145
					try {
146
						
147
						this.runInit(subMonitor);
148
						JobsTimer.start();
149
						
150
						if (s instanceof TXMResult) {
151
							TXMResult r = (TXMResult) s;
152
							
153
							// compute the result if needed
154
							if (!r.isAltered() && r.isDirty()) {
155
								r.compute(this);
156
							}
157
							Log.info(TXMUIMessages.bind(TXMUIMessages.exportingP0, r.getName()));
158
							ExecTimer.start();
159
							r.toTxt(outfile, encoding, colseparator, txtseparator);
160
							Log.info(TXMUIMessages.bind(TXMUIMessages.doneInP0, ExecTimer.stop()));
161
						}
162
						else {
163
							Log.warning(TXMUIMessages.TheExportedObjectIsNotATXMResultResult);
164
							return Status.CANCEL_STATUS;
165
						}
166
						
167
						if (outfile.exists()) {
168
							// Open internal editor in the UI thread
169
							if (TBXPreferences.getInstance().getBoolean(TBXPreferences.EXPORT_SHOW)) {
170
								this.syncExec(new Runnable() {
171
									
172
									@Override
173
									public void run() {
174
										EditFile.openfile(outfile.getAbsolutePath());
175
									}
176
								});
177
							}
178
						}
179
						else {
180
							Log.warning(NLS.bind(TXMUIMessages.failedToExportResultP0ColonP1, outfile.getAbsolutePath()));
181
						}
182
					}
183
					catch (ThreadDeath td) {
184
						TXMResult r = (TXMResult) s;
185
						r.resetComputingState();
186
						return Status.CANCEL_STATUS;
187
					}
188
					catch (Exception e) {
189
						Log.warning(NLS.bind(TXMUIMessages.failedToExportResultP0ColonP1, s, e));
190
						org.txm.utils.logger.Log.printStackTrace(e);
191
						return Status.CANCEL_STATUS;
192
					}
193
					finally {
194
						subMonitor.done();
195
						JobsTimer.stopAndPrint();
196
					}
197
					return Status.OK_STATUS;
198
				}
199
			};
200
			
201
			jobhandler.startJob();
202
		}
203
		return null;
204
	}
205
}
0 206

  
TXM/trunk/bundles/org.txm.rcp/plugin.xml (revision 3819)
2150 2150
                     </visibleWhen>
2151 2151
                  </command>
2152 2152
               </menu>
2153
            <command
2154
                  commandId="org.txm.rcp.handlers.export.ImportResultParameters"
2155
                  icon="icons/functions/import_parameters.png"
2156
                  label="%command.label.195"
2157
                  style="push"
2158
                  tooltip="Import a calculus from a .txmcmd file">
2159
               <visibleWhen
2160
                     checkEnabled="false">
2161
                  <and>
2162
                     <reference
2163
                           definitionId="OneTXMResultSelected">
2164
                     </reference>
2165
                  </and>
2166
               </visibleWhen>
2167
            </command>
2153
            <menu
2154
                  icon="platform:/plugin/org.eclipse.ui/icons/full/etool16/import_wiz.png"
2155
                  id="org.txm.rcp.corporaview.import"
2156
                  label="Import">
2157
               <command
2158
                     commandId="org.txm.rcp.handlers.export.ImportResultParameters"
2159
                     icon="icons/functions/import_parameters.png"
2160
                     label="%command.label.102"
2161
                     style="push"
2162
                     tooltip="Import a calculus from a .txmcmd file">
2163
                  <visibleWhen
2164
                        checkEnabled="false">
2165
                     <and>
2166
                        <reference
2167
                              definitionId="OneTXMResultSelected">
2168
                        </reference>
2169
                     </and>
2170
                  </visibleWhen>
2171
               </command>
2172
            </menu>
2168 2173
            
2169 2174
            <separator
2170 2175
                  name="org.txm.rcp.corporaview.corpus"
......
2859 2864
            name="%command.name.24">
2860 2865
      </command>
2861 2866
      <command
2867
            defaultHandler="org.txm.rcp.handlers.export.ExportResultFromEditor"
2868
            id="org.txm.rcp.commands.function.ExportResultFromEditor"
2869
            name="ExportResultFromEditor">
2870
      </command>
2871
      <command
2862 2872
            defaultHandler="org.txm.rcp.handlers.export.ExportCommand"
2863 2873
            id="org.txm.rcp.handlers.export.ExportResultParameters"
2864 2874
            name="%command.name.35">
TXM/trunk/bundles/org.txm.rcp/OSGI-INF/l10n/bundle_fr.properties (revision 3819)
76 76
command.label.139       = Autres...
77 77
command.label.14        = \u00C0 Propos
78 78
command.label.140       = Afficher les num\u00E9ros de ligne
79
command.label.141       = Importer une commande...
80
command.label.142       = Exporter commande...
79
command.label.141       = Importer un calcul...
80
command.label.142       = Exporter calcul...
81 81
command.label.143       = Ex\u00E9cuter
82 82
command.label.144       = Activer l'affichage des caract\u00E8res non imprimables
83 83
command.label.145       = Retour \u00E0 l'\u00E9tat initial du fichier
......
135 135
command.label.192       = Supprimer
136 136
command.label.193       = Annotations au format XML-TEI...
137 137
command.label.194       = Calcul...
138
command.label.195       = Importer une commande...
138
command.label.195       = Importer un calcul...
139 139
command.label.196       = Copier
140 140
command.label.197       = Tout copier
141 141
command.label.198       = Couper
TXM/trunk/bundles/org.txm.rcp/OSGI-INF/l10n/bundle.properties (revision 3819)
34 34
command.label.10         = a binary corpus (.txm)...
35 35
command.label.100        = Find previous
36 36
command.label.101        = Chart...
37
command.label.102        = Command...
38
command.label.103        = Command...
37
command.label.102        = Calculus...
38
command.label.103        = Calculus...
39 39
command.label.104        = Delete line
40 40
command.label.105        = Merge lines
41 41
command.label.106        = Export table to re-import
......
77 77
command.label.139        = Other...
78 78
command.label.14         = About TXM
79 79
command.label.140        = Show Line Numbers
80
command.label.141        = Import a command...
81
command.label.142        = Export command......
80
command.label.141        = Import a calculus...
81
command.label.142        = Export calculus......
82 82
command.label.143        = Submit
83 83
command.label.144        = Toggle Show Non-printable Characters
84 84
command.label.145        = Revert to saved
......
135 135
command.label.191        = Add
136 136
command.label.192        = Delete
137 137
command.label.193        = Annotations in XML-TEI format...
138
command.label.194        = Command...
139
command.label.195        = Import a command...
138
command.label.194        = Caculus...
139
command.label.195        = Import a calculus...
140 140
command.label.196        = Copy
141 141
command.label.197        = Copy All
142 142
command.label.198        = Cut
TXM/trunk/bundles/org.txm.lexicaltable.rcp/src/org/txm/lexicaltable/rcp/editors/LexicalTableEditor.java (revision 3819)
408 408
			viewer.getTable().setLinesVisible(true);
409 409
			viewer.getTable().setHeaderVisible(true);
410 410

  
411
			viewer.addSelectionChangedListener(new ISelectionChangedListener() {
411
//			viewer.addSelectionChangedListener(new ISelectionChangedListener() {
412
//
413
//				@Override
414
//				public void selectionChanged(SelectionChangedEvent event) {
415
//					System.out.println("sel="+viewer.getStructuredSelection().toList());
416
//				}
417
//			});
412 418

  
413
				@Override
414
				public void selectionChanged(SelectionChangedEvent event) {
415
					System.out.println("sel="+viewer.getStructuredSelection().toList());
416
				}
417
			});
418 419

  
419

  
420 420
			// viewer.setContentProvider(ArrayContentProvider.getInstance());
421 421

  
422 422
			// FIXME: viewer comparator tests
......
428 428
			// viewer.setContentProvider(ArrayContentProvider.getInstance());
429 429
			viewer.setContentProvider(new LineContentProvider());
430 430

  
431

  
432 431
			viewer.getTable().setLayoutData(new GridData(GridData.FILL_BOTH));
433 432

  
434 433
			// first dummy column
TXM/trunk/bundles/org.txm.lexicaltable.rcp/plugin.xml (revision 3819)
12 12
   </extension>
13 13
   <extension
14 14
         point="org.eclipse.ui.menus">
15
            <menuContribution
16
                  locationURI="menu:menu.file.import">
17
               <command
18
                     commandId="org.txm.lexicaltable.rcp.handlers.ImportTable"
19
                     icon="platform:/plugin/org.txm.rcp/icons/functions/import_data.png"
20
                     label="Data..."
21
                     style="push">
22
                  <visibleWhen
23
                        checkEnabled="false">
24
                     <reference
25
                           definitionId="OneLexicalTableSelected">
26
                     </reference>
27
                  </visibleWhen>
28
               </command>
29
            </menuContribution>
15 30
      <menuContribution
16
            locationURI="popup:org.txm.rcp.views.corpora.CorporaView?after=org.txm.rcp.corporaview.corpus.manage">
31
            locationURI="menu:menu.corpus">
17 32
         <command
18 33
               commandId="org.txm.lexicaltable.rcp.handlers.ComputeLexicalTable"
19 34
               icon="icons/lexicaltable.png"
......
33 48
               </or>
34 49
            </visibleWhen>
35 50
         </command>
36
         <command
37
               commandId="org.txm.lexicaltable.rcp.handlers.ImportTable"
38
               icon="platform:/plugin/org.eclipse.ui/icons/full/etool16/import_wiz.png"
39
               style="push">
40
            <visibleWhen
41
                  checkEnabled="false">
42
               <reference
43
                     definitionId="OneLexicalTableSelected">
44
               </reference>
45
            </visibleWhen>
46
         </command>
47 51
      </menuContribution>
48 52
      <menuContribution
49
            locationURI="menu:menu.corpus">
53
            allPopups="false"
54
            locationURI="toolbar:org.txm.rcp.toolbartools">
50 55
         <command
51 56
               commandId="org.txm.lexicaltable.rcp.handlers.ComputeLexicalTable"
52 57
               icon="icons/lexicaltable.png"
......
55 60
                  checkEnabled="false">
56 61
               <or>
57 62
                  <reference
58
                        definitionId="OnePartitionSelected">
59
                  </reference>
60
                  <reference
61 63
                        definitionId="OnePartitionIndexSelected">
62 64
                  </reference>
63 65
                  <reference
......
68 70
         </command>
69 71
         <command
70 72
               commandId="org.txm.lexicaltable.rcp.handlers.ImportTable"
71
               icon="platform:/plugin/org.eclipse.ui/icons/full/etool16/import_wiz.png"
73
               icon="platform:/plugin/org.txm.rcp/icons/functions/import_data.png"
72 74
               style="push">
73 75
            <visibleWhen
74 76
                  checkEnabled="false">
......
80 82
      </menuContribution>
81 83
      <menuContribution
82 84
            allPopups="false"
83
            locationURI="toolbar:org.txm.rcp.toolbartools">
85
            locationURI="toolbar:org.txm.rcp.toolbartools?after=org.txm.partition.rcp.handlers.ComputePartitionDimensionsBarChart">
84 86
         <command
85 87
               commandId="org.txm.lexicaltable.rcp.handlers.ComputeLexicalTable"
86 88
               icon="icons/lexicaltable.png"
89
               id="ComputeLexicalTableFromPartition"
87 90
               style="push">
88 91
            <visibleWhen
89 92
                  checkEnabled="false">
90 93
               <or>
91 94
                  <reference
92
                        definitionId="OnePartitionIndexSelected">
95
                        definitionId="OnePartitionSelected">
93 96
                  </reference>
94
                  <reference
95
                        definitionId="OneLexicalTableAbleSelected">
96
                  </reference>
97 97
               </or>
98 98
            </visibleWhen>
99 99
         </command>
100
         <command
101
               commandId="org.txm.lexicaltable.rcp.handlers.ImportTable"
102
               icon="platform:/plugin/org.eclipse.ui/icons/full/etool16/import_wiz.png"
103
               style="push">
104
            <visibleWhen
105
                  checkEnabled="false">
106
               <reference
107
                     definitionId="OneLexicalTableSelected">
108
               </reference>
109
            </visibleWhen>
110
         </command>
111 100
      </menuContribution>
112 101
      <menuContribution
113
            allPopups="false"
114
            locationURI="toolbar:org.txm.rcp.toolbartools?after=org.txm.partition.rcp.handlers.ComputePartitionDimensionsBarChart">
102
            locationURI="popup:org.txm.rcp.views.corpora.CorporaView?after=org.txm.rcp.corporaview.corpus.manage">
115 103
         <command
116 104
               commandId="org.txm.lexicaltable.rcp.handlers.ComputeLexicalTable"
117 105
               icon="icons/lexicaltable.png"
118
               id="ComputeLexicalTableFromPartition"
119 106
               style="push">
120 107
            <visibleWhen
121 108
                  checkEnabled="false">
......
123 110
                  <reference
124 111
                        definitionId="OnePartitionSelected">
125 112
                  </reference>
113
                  <reference
114
                        definitionId="OnePartitionIndexSelected">
115
                  </reference>
116
                  <reference
117
                        definitionId="OneLexicalTableAbleSelected">
118
                  </reference>
126 119
               </or>
127 120
            </visibleWhen>
128 121
         </command>
129 122
      </menuContribution>
130 123
            <menuContribution
124
                  locationURI="popup:org.txm.rcp.corporaview.import">
125
               <command
126
                     commandId="org.txm.lexicaltable.rcp.handlers.ImportTable"
127
                     icon="platform:/plugin/org.txm.rcp/icons/functions/import_data.png"
128
                     label="Data..."
129
                     style="push">
130
                  <visibleWhen
131
                        checkEnabled="false">
132
                     <reference
133
                           definitionId="OneLexicalTableSelected">
134
                     </reference>
135
                  </visibleWhen>
136
               </command>
137
            </menuContribution>
138
            <menuContribution
131 139
            locationURI="popup:org.txm.lexicaltable.rcp.editors.LexicalTableEditor">
132 140
               <command
133 141
                     commandId="org.txm.lexicaltable.rcp.handlers.CopyLines"
......
149 157
         </separator>
150 158
         <command
151 159
               commandId="org.txm.lexicaltable.rcp.handlers.ImportTable"
152
               icon="platform:/plugin/org.eclipse.ui/icons/full/etool16/import_wiz.png"
160
               icon="platform:/plugin/org.txm.rcp/icons/functions/import_data.png"
153 161
               style="push">
154 162
         </command>
163
         <command
164
               commandId="org.txm.rcp.commands.function.ExportResultFromEditor"
165
               icon="platform:/plugin/org.txm.rcp/icons/functions/export_data.png"
166
               label="Data..."
167
               style="push">
168
            <visibleWhen
169
                  checkEnabled="false">
170
               <and>
171
                  <reference
172
                        definitionId="OneTXMResultSelected">
173
                  </reference>
174
                  <not>
175
                     <or>
176
                        <reference
177
                              definitionId="OnePartitionSelected">
178
                        </reference>
179
                        <reference
180
                              definitionId="OneCorpusSelected">
181
                        </reference>
182
                     </or>
183
                  </not>
184
               </and>
185
            </visibleWhen>
186
         </command>
155 187
      </menuContribution>
156 188
      
157 189
   </extension>

Formats disponibles : Unified diff