Révision 2485

tmp/org.txm.searchengine.cqp.rcp/src/org/txm/searchengine/cqp/rcp/handlers/base/ExportCorpus.java (revision 2485)
37 37
import org.eclipse.core.runtime.Status;
38 38
import org.eclipse.jface.dialogs.MessageDialog;
39 39
import org.eclipse.jface.viewers.IStructuredSelection;
40
import org.eclipse.jface.window.Window;
40 41
import org.eclipse.osgi.util.NLS;
41 42
import org.eclipse.swt.SWT;
42 43
import org.eclipse.swt.widgets.FileDialog;
......
47 48
import org.txm.rcp.swt.dialog.LastOpened;
48 49
import org.txm.rcp.utils.JobHandler;
49 50
import org.txm.searchengine.cqp.corpus.MainCorpus;
51
import org.txm.searchengine.cqp.rcp.dialogs.ExportCorpusDialog;
50 52

  
51 53
/**
52 54
 * Export a corpus to the .txm binary format (Zip)
53 55
 */
54 56
public class ExportCorpus extends AbstractHandler {
55

  
57
	
56 58
	public final static String ID = "org.txm.rcp.commands.base.ExportCorpus"; //$NON-NLS-1$
59
	
57 60
	/** The selection. */
58 61
	private IStructuredSelection selection;
59

  
62
	
60 63
	/** The lastopenedfile. */
61 64
	private static String lastopenedfile;
62

  
65
	
63 66
	/*
64 67
	 * (non-Javadoc)
65
	 * 
66 68
	 * @see
67 69
	 * org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands
68 70
	 * .ExecutionEvent)
......
70 72
	@Override
71 73
	public Object execute(ExecutionEvent event) throws ExecutionException {
72 74
		selection = (IStructuredSelection) HandlerUtil.getCurrentSelection(event);
73

  
75
		
74 76
		Shell shell = HandlerUtil.getActiveWorkbenchWindowChecked(event)
75 77
				.getShell();
76

  
78
		
77 79
		String txmhome = Toolbox.getTxmHomePath();
78 80
		if (!(selection.getFirstElement() instanceof MainCorpus))
79 81
			return null;
80

  
82
		
81 83
		final MainCorpus corpus = (MainCorpus) selection.getFirstElement();
82

  
84
		
83 85
		if (corpus.isModified()) {
84
			boolean ret = MessageDialog.openConfirm(shell, "Export - "+corpus.getName()+" annotations not saved", "Do you wish to export without the current annotations?\n\n(to include the current annotations in the exported corpus, you need to save them before calling the \"Export\" command)");
86
			boolean ret = MessageDialog.openConfirm(shell, "Export - " + corpus.getName() + " annotations not saved",
87
					"Do you wish to export without the current annotations?\n\n(to include the current annotations in the exported corpus, you need to save them before calling the \"Export\" command)");
85 88
			if (!ret) return null;
86 89
		}
87 90
		
88
		FileDialog dialog = new FileDialog(shell, SWT.SAVE);
89
		String[] exts = {"*.txm", "*.zip"}; //$NON-NLS-1$ //$NON-NLS-2$
90
		dialog.setFilterExtensions(exts);
91
		if (LastOpened.getFile(ID) != null) {
92
			lastopenedfile = LastOpened.getFolder(ID)
93
					+ "/" + LastOpened.getFile(ID); //$NON-NLS-1$
94
		} else if (lastopenedfile != null) {
95
			dialog.setFilterPath(new File(lastopenedfile).getAbsolutePath());
96
		} else {
97
			dialog.setFilterPath(txmhome + "/samples"); //$NON-NLS-1$
98
		}
99

  
100
		dialog.setFileName(corpus.getName()+".txm");
91
		ExportCorpusDialog dialog = new ExportCorpusDialog(shell, corpus);
101 92
		
102
		if (dialog.open() != null) {
103
			String filepath = dialog.getFilterPath()
104
					+ "/" + dialog.getFileName(); //$NON-NLS-1$
105
			if (!(filepath.endsWith(".txm") || filepath.endsWith(".zip"))) { //$NON-NLS-1$ //$NON-NLS-2$
106
				filepath += ".txm"; //$NON-NLS-1$
107
			}
108
			LastOpened.set(ID, dialog.getFilterPath(), dialog.getFileName());
109
			lastopenedfile = filepath;
110
			final File outfile = new File(filepath);
111
			JobHandler jobhandler = new JobHandler(NLS.bind(
112
					TXMUIMessages.exportingP0ThisMayTakeAWhile, corpus.getName())) {
93
		if (dialog.open() == Window.OK) {
94
			
95
			final File outfile = dialog.getZipFile();
96
			JobHandler jobhandler = new JobHandler(NLS.bind(TXMUIMessages.exportingP0ThisMayTakeAWhile, corpus.getName())) {
97
				
113 98
				@Override
114 99
				protected IStatus run(IProgressMonitor monitor) {
115 100
					this.runInit(monitor);
116 101
					try {
117
						monitor.beginTask(
118
								NLS.bind(TXMUIMessages.exportingP0ThisMayTakeAWhile,
119
										corpus.getName()), 100);
102
						monitor.beginTask(NLS.bind(TXMUIMessages.exportingP0ThisMayTakeAWhile, corpus.getName()), 100);
120 103
						
121

  
122
						if (!corpus.export(outfile, this)) {
123
							System.out.println(NLS.bind(
124
									TXMUIMessages.failedToExportCorpusP0, corpus.getName()));
104
						if (!corpus.export(outfile, this, dialog.getIgnoreNames())) {
105
							System.out.println(NLS.bind(TXMUIMessages.failedToExportCorpusP0, corpus.getName()));
125 106
						}
126

  
127
						System.out.println(TXMUIMessages.bind(TXMUIMessages.doneTheCreatedBinaryFileIsP0,outfile.getAbsolutePath()));
107
						
108
						System.out.println(TXMUIMessages.bind(TXMUIMessages.doneTheCreatedBinaryFileIsP0, outfile.getAbsolutePath()));
128 109
						monitor.worked(100);
129
					} catch (ThreadDeath td) {
110
					}
111
					catch (ThreadDeath td) {
130 112
						return Status.CANCEL_STATUS;
131
					} finally {
113
					}
114
					finally {
132 115
						monitor.done();
133 116
					}
134 117
					return Status.OK_STATUS;
......
136 119
			};
137 120
			jobhandler.startJob();
138 121
		}
139

  
122
		
140 123
		return null;
141 124
	}
142
}
125
}
tmp/org.txm.searchengine.cqp.rcp/src/org/txm/searchengine/cqp/rcp/dialogs/ExportCorpusDialog.java (revision 2485)
1
package org.txm.searchengine.cqp.rcp.dialogs;
2

  
3

  
4
import java.io.File;
5
import java.io.FileFilter;
6
import java.util.Arrays;
7
import java.util.Comparator;
8
import java.util.HashSet;
9
import java.util.LinkedHashMap;
10
import java.util.List;
11

  
12
import org.eclipse.jface.dialogs.Dialog;
13
import org.eclipse.jface.dialogs.IDialogConstants;
14
import org.eclipse.jface.viewers.ArrayContentProvider;
15
import org.eclipse.jface.viewers.CellLabelProvider;
16
import org.eclipse.jface.viewers.TableViewer;
17
import org.eclipse.jface.viewers.TableViewerColumn;
18
import org.eclipse.jface.viewers.ViewerCell;
19
import org.eclipse.osgi.util.NLS;
20
import org.eclipse.swt.SWT;
21
import org.eclipse.swt.events.SelectionEvent;
22
import org.eclipse.swt.events.SelectionListener;
23
import org.eclipse.swt.layout.GridData;
24
import org.eclipse.swt.layout.GridLayout;
25
import org.eclipse.swt.widgets.Button;
26
import org.eclipse.swt.widgets.Composite;
27
import org.eclipse.swt.widgets.Control;
28
import org.eclipse.swt.widgets.FileDialog;
29
import org.eclipse.swt.widgets.Label;
30
import org.eclipse.swt.widgets.Shell;
31
import org.eclipse.swt.widgets.TableItem;
32
import org.eclipse.swt.widgets.Text;
33
import org.txm.Toolbox;
34
import org.txm.objects.Project;
35
import org.txm.rcp.swt.dialog.LastOpened;
36
import org.txm.searchengine.cqp.corpus.CQPCorpus;
37

  
38
/**
39
 * Allow to select the file to create and select elements to include in the binary
40
 * 
41
 *
42
 */
43
public class ExportCorpusDialog extends Dialog {
44
	
45
	private List<String> defaultFiles = Arrays.asList("HTML", "txm", "data", "registry", "annotations", "analec", "temporary_annotations");
46
	
47
	private File zipFile;
48
	
49
	private LinkedHashMap<File, Boolean> selectedFiles;
50
	
51
	private CQPCorpus corpus;
52
	
53
	private TableViewer treeViewer;
54
	
55
	private TableViewerColumn nameColumn;
56
	
57
	private Text textField;
58
	
59
	private Button selectButton;
60
	
61
	public final static String ID = ExportCorpusDialog.class.getName();
62
	
63
	private static String lastopenedfile;
64
	
65
	/**
66
	 * 
67
	 * @param parentShell
68
	 * @param title
69
	 * @param question
70
	 * @param text
71
	 */
72
	public ExportCorpusDialog(Shell parentShell, CQPCorpus corpus) {
73
		super(parentShell);
74
		this.corpus = corpus;
75
	}
76
	
77
	@Override
78
	protected void configureShell(Shell newShell) {
79
		super.configureShell(newShell);
80
		newShell.setText(NLS.bind("Exporting {0}...", corpus.getName()));
81
		newShell.setMinimumSize(400, 200);
82
	}
83
	
84
	@Override
85
	protected Control createButtonBar(Composite parent) {
86
		Control c = super.createButtonBar(parent);
87
		getButton(IDialogConstants.OK_ID).setEnabled(false);
88
		return c;
89
	}
90
	
91
	@Override
92
	protected Control createDialogArea(Composite parent) {
93
		Composite comp = (Composite) super.createDialogArea(parent);
94
		
95
		GridLayout layout = (GridLayout) comp.getLayout();
96
		layout.numColumns = 3;
97
		
98
		Label l = new Label(comp, SWT.LEFT);
99
		l.setText("Output file");
100
		l.setLayoutData(new GridData(GridData.BEGINNING, GridData.CENTER, false, false));
101
		
102
		textField = new Text(comp, SWT.SINGLE | SWT.BORDER);
103
		textField.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false));
104
		
105
		selectButton = new Button(comp, SWT.PUSH);
106
		selectButton.setText("...");
107
		selectButton.addSelectionListener(new SelectionListener() {
108
			
109
			@Override
110
			public void widgetSelected(SelectionEvent e) {
111
				FileDialog dialog = new FileDialog(selectButton.getShell());
112
				
113
				String[] exts = { "*.txm", "*.zip" }; //$NON-NLS-1$ //$NON-NLS-2$
114
				dialog.setFilterExtensions(exts);
115
				if (LastOpened.getFile(ID) != null) {
116
					lastopenedfile = LastOpened.getFolder(ID) + "/" + LastOpened.getFile(ID); //$NON-NLS-1$
117
				}
118
				else if (lastopenedfile != null) {
119
					dialog.setFilterPath(new File(lastopenedfile).getAbsolutePath());
120
				}
121
				else {
122
					dialog.setFilterPath(Toolbox.getTxmHomePath() + "/samples"); //$NON-NLS-1$
123
				}
124
				
125
				dialog.setFileName(corpus.getName() + ".txm");
126
				
127
				String path = dialog.open();
128
				if (path != null) {
129
					textField.setText(path);
130
					getButton(IDialogConstants.OK_ID).setEnabled(true);
131
				}
132
				else {
133
					textField.setText("");
134
					getButton(IDialogConstants.OK_ID).setEnabled(false);
135
				}
136
			}
137
			
138
			@Override
139
			public void widgetDefaultSelected(SelectionEvent e) {}
140
		});
141
		
142
		selectedFiles = new LinkedHashMap<>();
143
		Project project = corpus.getProject();
144
		File directory = project.getProjectDirectory();
145
		if (directory.exists()) {
146
			
147
			File[] files = directory.listFiles(new FileFilter() {
148
				
149
				@Override
150
				public boolean accept(File f) {
151
					return !f.isHidden() && f.isDirectory() && !defaultFiles.contains(f.getName());
152
				}
153
			});
154
			Arrays.sort(files, new Comparator<File>() {
155
				
156
				@Override
157
				public int compare(File arg0, File arg1) {
158
					if (arg0.isDirectory() && arg1.isDirectory()) {
159
						return arg0.getName().compareTo(arg1.getName());
160
					}
161
					else if (arg0.isFile() && arg1.isFile()) {
162
						return arg0.getName().compareTo(arg1.getName());
163
					}
164
					else {
165
						if (arg0.isDirectory()) {
166
							return -1;
167
						}
168
						else {
169
							return 1;
170
						}
171
					}
172
				}
173
			});
174
			for (File f : files) {
175
				if (!f.isHidden() && f.isDirectory()) {
176
					
177
					if (defaultFiles.contains(f.getName())) {
178
						selectedFiles.put(f, true);
179
					}
180
					else {
181
						selectedFiles.put(f, false);
182
					}
183
				}
184
			}
185
			
186
			if (files.length > 0) {
187
				treeViewer = new TableViewer(comp, SWT.CHECK | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
188
				
189
				treeViewer.setContentProvider(new ArrayContentProvider());
190
				
191
				treeViewer.getTable().addListener(SWT.Selection, event -> {
192
					
193
					if (event.detail == SWT.CHECK) {
194
						TableItem item = (TableItem) event.item;
195
						selectedFiles.put((File) event.item.getData(), item.getChecked());
196
					}
197
				});
198
				
199
				treeViewer.getTable().setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true, 3, 1));
200
				treeViewer.getTable().setHeaderVisible(true);
201
				treeViewer.getTable().setLinesVisible(true);
202
				
203
				nameColumn = new TableViewerColumn(treeViewer, SWT.NONE);
204
				nameColumn.getColumn().setText("Extra files");
205
				nameColumn.getColumn().pack();
206
				nameColumn.setLabelProvider(new CellLabelProvider() {
207
					
208
					@Override
209
					public void update(ViewerCell cell) {
210
						Object element = cell.getElement();
211
						
212
						if (element instanceof File) {
213
							File f = (File) element;
214
							cell.setText(f.getName());
215
						}
216
						else {
217
							cell.setText("" + element.getClass());
218
						}
219
					}
220
				});
221
				
222
				treeViewer.setInput(files);
223
				for (int i = 0; i < files.length; i++) {
224
					treeViewer.getTable().getItem(i).setChecked(selectedFiles.get(files[i]));
225
				}
226
				
227
				Button selectAllButton = new Button(comp, SWT.PUSH);
228
				selectAllButton.setText("Select All");
229
				selectAllButton.addSelectionListener(new SelectionListener() {
230
					
231
					@Override
232
					public void widgetSelected(SelectionEvent e) {
233
						massSelect(true);
234
					}
235
					
236
					@Override
237
					public void widgetDefaultSelected(SelectionEvent e) {}
238
				});
239
				
240
				Button clearAllButton = new Button(comp, SWT.PUSH);
241
				clearAllButton.setText("Deselect All");
242
				clearAllButton.addSelectionListener(new SelectionListener() {
243
					
244
					@Override
245
					public void widgetSelected(SelectionEvent e) {
246
						massSelect(false);
247
					}
248
					
249
					@Override
250
					public void widgetDefaultSelected(SelectionEvent e) {}
251
				});
252
			}
253
		}
254
		
255
		return comp;
256
	}
257
	
258
	protected void massSelect(boolean b) {
259
		for (int i = 0; i < treeViewer.getTable().getItems().length; i++) {
260
			treeViewer.getTable().getItem(i).setChecked(b);
261
		}
262
		for (File f : selectedFiles.keySet()) {
263
			selectedFiles.put(f, b);
264
		}
265
	}
266
	
267
	@Override
268
	protected boolean isResizable() {
269
		return true;
270
	}
271
	
272
	@Override
273
	protected void buttonPressed(int buttonId) {
274
		if (buttonId == Dialog.OK) {
275
			zipFile = new File(textField.getText());
276
		}
277
		super.buttonPressed(buttonId);
278
	}
279
	
280
	public File getZipFile() {
281
		return zipFile;
282
	}
283
	
284
	public LinkedHashMap<File, Boolean> getFileSelection() {
285
		return selectedFiles;
286
	}
287
	
288
	public HashSet<String> getIgnoreNames() {
289
		HashSet<String> ignore = new HashSet<>();
290
		for (File f : selectedFiles.keySet()) {
291
			if (!selectedFiles.get(f)) {
292
				ignore.add(f.getName());
293
			}
294
		}
295
		return ignore;
296
	}
297
}
0 298

  

Formats disponibles : Unified diff