Révision 2492

tmp/org.txm.searchengine.cqp.rcp/src/org/txm/searchengine/cqp/rcp/handlers/base/CreatePartition.java (revision 2492)
1
// Copyright © 2010-2013 ENS de Lyon.
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.searchengine.cqp.rcp.handlers.base;
29

  
30
import org.eclipse.core.commands.AbstractHandler;
31
import org.eclipse.core.commands.ExecutionEvent;
32
import org.eclipse.core.commands.ExecutionException;
33
import org.eclipse.core.runtime.IProgressMonitor;
34
import org.eclipse.core.runtime.IStatus;
35
import org.eclipse.core.runtime.Status;
36
import org.eclipse.jface.viewers.IStructuredSelection;
37
import org.eclipse.jface.window.Window;
38
import org.eclipse.osgi.util.NLS;
39
import org.eclipse.swt.widgets.Shell;
40
import org.eclipse.ui.IWorkbenchWindow;
41
import org.eclipse.ui.handlers.HandlerUtil;
42
import org.txm.rcp.JobsTimer;
43
import org.txm.rcp.actions.CreatePartitionDialog;
44
import org.txm.rcp.messages.TXMUIMessages;
45
import org.txm.rcp.utils.JobHandler;
46
import org.txm.rcp.views.corpora.CorporaView;
47
import org.txm.searchengine.cqp.corpus.CQPCorpus;
48
import org.txm.searchengine.cqp.corpus.Partition;
49
import org.txm.utils.logger.Log;
50

  
51
/**
52
 * Command which creates a Partition from a Corpus.
53
 * 
54
 * @author mdecorde
55
 */
56
public class CreatePartition extends AbstractHandler {
57
	
58
	/** The window. */
59
	private IWorkbenchWindow window;
60
	
61
	/** The selection. */
62
	private IStructuredSelection selection;
63
	
64
	/*
65
	 * (non-Javadoc)
66
	 * @see
67
	 * org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands
68
	 * .ExecutionEvent)
69
	 */
70
	@Override
71
	public Object execute(ExecutionEvent event) throws ExecutionException {
72
		selection = (IStructuredSelection) HandlerUtil.getCurrentSelection(event);
73
		
74
		final CQPCorpus corpus = (CQPCorpus) selection.getFirstElement();
75
		Shell shell = HandlerUtil.getActiveWorkbenchWindowChecked(event).getShell();
76
		final CreatePartitionDialog d = new CreatePartitionDialog(shell, corpus);
77
		int code = d.open();
78
		if (code == Window.OK) {
79
			try {
80
				JobHandler jobhandler = new JobHandler(NLS.bind(TXMUIMessages.creatingANewPartitionWithP0, corpus.getName())) {
81
					
82
					@Override
83
					protected IStatus run(IProgressMonitor monitor) {
84
						this.runInit(monitor);
85
						try {
86
							JobsTimer.start();
87
							monitor.beginTask(NLS.bind(TXMUIMessages.creatingANewPartitionWithP0, corpus.getName()), 100);
88
							
89
							final Partition partition = d.getPartition();
90
							if (partition == null) {
91
								monitor.done();
92
								Log.severe(TXMUIMessages.errorColonThePartitionWasNotCreated);
93
								return Status.CANCEL_STATUS;
94
							}
95
							try {
96
								partition.compute(monitor);
97
								
98
								if (partition.getPartsCount() == 0) {
99
									Log.severe(TXMUIMessages.errorColonThePartitionCreatedHasNoPart);
100
									return null;
101
								}
102
							}
103
							catch (Exception e) {
104
								Log.printStackTrace(e);
105
							}
106
							monitor.worked(95);
107
							
108
							monitor.subTask(TXMUIMessages.refreshingCorpora);
109
							syncExec(new Runnable() {
110
								
111
								@Override
112
								public void run() {
113
									CorporaView.refresh();
114
									CorporaView.expand(partition.getParent());
115
								}
116
							});
117
							monitor.worked(100);
118
						}
119
						catch (ThreadDeath td) {
120
							return Status.CANCEL_STATUS;
121
						}
122
						finally {
123
							monitor.done();
124
							JobsTimer.stopAndPrint();
125
						}
126
						return Status.OK_STATUS;
127
					}
128
				};
129
				jobhandler.startJob();
130
				
131
			}
132
			catch (Exception e) {
133
				Log.severe(NLS.bind(TXMUIMessages.errorWhileCreatingAPartitionColonP0, e));
134
			}
135
		}
136
		return null;
137
	}
138
}
tmp/org.txm.searchengine.cqp.rcp/src/org/txm/searchengine/cqp/rcp/handlers/base/ComputePartition.java (revision 2492)
1
// Copyright © 2010-2013 ENS de Lyon.
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.searchengine.cqp.rcp.handlers.base;
29

  
30
import java.util.ArrayList;
31

  
32
import org.eclipse.core.commands.AbstractHandler;
33
import org.eclipse.core.commands.ExecutionEvent;
34
import org.eclipse.core.commands.ExecutionException;
35
import org.eclipse.core.runtime.IProgressMonitor;
36
import org.eclipse.core.runtime.IStatus;
37
import org.eclipse.core.runtime.Status;
38
import org.eclipse.jface.viewers.IStructuredSelection;
39
import org.eclipse.jface.window.Window;
40
import org.eclipse.osgi.util.NLS;
41
import org.eclipse.swt.widgets.Shell;
42
import org.eclipse.ui.IWorkbenchWindow;
43
import org.eclipse.ui.handlers.HandlerUtil;
44
import org.txm.rcp.JobsTimer;
45
import org.txm.rcp.actions.CreatePartitionDialog;
46
import org.txm.rcp.messages.TXMUIMessages;
47
import org.txm.rcp.utils.JobHandler;
48
import org.txm.rcp.views.corpora.CorporaView;
49
import org.txm.searchengine.cqp.corpus.CQPCorpus;
50
import org.txm.searchengine.cqp.corpus.Part;
51
import org.txm.searchengine.cqp.corpus.Partition;
52
import org.txm.utils.logger.Log;
53

  
54
/**
55
 * Command which creates a Partition from a Corpus.
56
 * 
57
 * @author mdecorde
58
 */
59
public class ComputePartition extends AbstractHandler {
60
	
61
	/** The window. */
62
	private IWorkbenchWindow window;
63
	
64
	/** The selection. */
65
	private IStructuredSelection selection;
66
	
67
	/*
68
	 * (non-Javadoc)
69
	 * @see
70
	 * org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands
71
	 * .ExecutionEvent)
72
	 */
73
	@Override
74
	public Object execute(ExecutionEvent event) throws ExecutionException {
75
		selection = (IStructuredSelection) HandlerUtil.getCurrentSelection(event);
76
		
77
		final CQPCorpus corpus = (CQPCorpus) selection.getFirstElement();
78
		Shell shell = HandlerUtil.getActiveWorkbenchWindowChecked(event).getShell();
79
		final CreatePartitionDialog d = new CreatePartitionDialog(shell, corpus);
80
		int code = d.open();
81
		if (code == Window.OK) {
82
			try {
83
				JobHandler jobhandler = new JobHandler(NLS.bind(TXMUIMessages.creatingANewPartitionWithP0, corpus.getName())) {
84
					
85
					@Override
86
					protected IStatus run(IProgressMonitor monitor) {
87
						this.runInit(monitor);
88
						try {
89
							JobsTimer.start();
90
							monitor.beginTask(NLS.bind(TXMUIMessages.creatingANewPartitionWithP0, corpus.getName()), 100);
91
							
92
							final Partition partition = d.getPartition();
93
							if (partition == null) {
94
								monitor.done();
95
								Log.warning(TXMUIMessages.errorColonThePartitionWasNotCreated);
96
								return Status.CANCEL_STATUS;
97
							}
98
							try {
99
								partition.compute(monitor);
100
								
101
								// ensure the partition viability
102
								if (partition.getPartsCount() == 0) {
103
									Log.warning(TXMUIMessages.errorColonThePartitionCreatedHasNoPart);
104
									return Status.CANCEL_STATUS;
105
								}
106
								ArrayList<Part> emptyParts = new ArrayList<>();
107
								for (Part p : partition.getParts()) {
108
									if (p.getNMatch() == 0) {
109
										emptyParts.add(p);
110
									}
111
								}
112
								if (emptyParts.size() == partition.getPartsCount()) {
113
									Log.warning(NLS.bind("Error: all parts {0} are all empty.", partition.getPartsCount()));
114
									return Status.CANCEL_STATUS;
115
								}
116
								else if (emptyParts.size() != 0) {
117
									Log.warning(NLS.bind("Warning: some parts are empty: {0}", emptyParts));
118
								}
119
							}
120
							catch (Exception e) {
121
								Log.printStackTrace(e);
122
								return Status.CANCEL_STATUS;
123
							}
124
							monitor.worked(95);
125
							
126
							monitor.subTask(TXMUIMessages.refreshingCorpora);
127
							syncExec(new Runnable() {
128
								
129
								@Override
130
								public void run() {
131
									CorporaView.refresh();
132
									CorporaView.expand(partition.getParent());
133
								}
134
							});
135
							monitor.worked(100);
136
						}
137
						catch (ThreadDeath td) {
138
							return Status.CANCEL_STATUS;
139
						}
140
						finally {
141
							monitor.done();
142
							JobsTimer.stopAndPrint();
143
						}
144
						return Status.OK_STATUS;
145
					}
146
				};
147
				jobhandler.startJob();
148
				
149
			}
150
			catch (Exception e) {
151
				Log.severe(NLS.bind(TXMUIMessages.errorWhileCreatingAPartitionColonP0, e));
152
			}
153
		}
154
		return null;
155
	}
156
}
0 157

  
tmp/org.txm.searchengine.cqp.rcp/plugin.xml (revision 2492)
15 15
            name="%command.name.52">
16 16
      </command>
17 17
      <command
18
            defaultHandler="org.txm.searchengine.cqp.rcp.handlers.base.CreatePartition"
19
            id="org.txm.searchengine.cqp.rcp.handlers.base.CreatePartition"
18
            defaultHandler="org.txm.searchengine.cqp.rcp.handlers.base.ComputePartition"
19
            id="org.txm.searchengine.cqp.rcp.handlers.base.ComputePartition"
20 20
            name="%command.name.2">
21 21
         <commandParameter
22 22
               id="CorpusParameter"
......
155 155
            </visibleWhen>
156 156
         </command>
157 157
         <command
158
               commandId="org.txm.searchengine.cqp.rcp.handlers.base.CreatePartition"
158
               commandId="org.txm.searchengine.cqp.rcp.handlers.base.ComputePartition"
159 159
               icon="icons/functions/partition.png"
160 160
               style="push">
161 161
            <visibleWhen
......
196 196
            </visibleWhen>
197 197
         </command>
198 198
         <command
199
               commandId="org.txm.searchengine.cqp.rcp.handlers.base.CreatePartition"
199
               commandId="org.txm.searchengine.cqp.rcp.handlers.base.ComputePartition"
200 200
               icon="icons/functions/partition.png"
201 201
               style="push">
202 202
            <visibleWhen
......
271 271
            </visibleWhen>
272 272
         </command>
273 273
                        <command
274
                  commandId="org.txm.searchengine.cqp.rcp.handlers.base.CreatePartition"
274
                  commandId="org.txm.searchengine.cqp.rcp.handlers.base.ComputePartition"
275 275
                  icon="icons/functions/partition.png"
276 276
                  style="push"
277 277
                  tooltip="%command.tooltip.4">

Formats disponibles : Unified diff