Revision 536

tmp/org.txm.searchengine.cqp.rcp/src/org/txm/searchengine/cqp/rcp/editor/DiagnosticEditor.java (revision 536)
1
package org.txm.searchengine.cqp.rcp.editor;
2

  
3
import java.io.File;
4

  
5
import org.eclipse.swt.SWT;
6
import org.eclipse.swt.browser.Browser;
7
import org.eclipse.swt.layout.GridData;
8
import org.eclipse.swt.layout.GridLayout;
9
import org.eclipse.swt.layout.RowData;
10
import org.eclipse.swt.widgets.Composite;
11
import org.eclipse.swt.widgets.Label;
12
import org.eclipse.swt.widgets.Spinner;
13
import org.eclipse.ui.IEditorInput;
14
import org.eclipse.ui.IEditorSite;
15
import org.eclipse.ui.PartInitException;
16
import org.txm.core.results.TXMParameters;
17
import org.txm.functions.diagnostic.Diagnostic;
18
import org.txm.rcp.editors.TXMEditorPart;
19
import org.txm.rcp.preferences.RCPPreferences;
20
import org.txm.searchengine.cqp.CQPPreferences;
21

  
22
public class DiagnosticEditor extends TXMEditorPart {
23

  
24
	private Spinner maxScore;
25
	private Browser browser;
26
	protected Diagnostic diag;
27

  
28
	@Override
29
	public void init(IEditorSite site, IEditorInput input) throws PartInitException {
30
		super.init(site, input);
31
		if (getResultData() instanceof Diagnostic)
32
			this.diag = (Diagnostic) getResultData();
33
	}
34
	
35
	@Override
36
	protected void initializeFields() {
37
		// TODO Auto-generated method stub
38
		
39
	}
40
	
41
	@Override
42
	public void createPartControl(Composite parent) {
43
		super.createPartControl(parent);
44
		
45
		Composite parametersArea = getCommandParametersGroup();
46
		
47
		new Label(parametersArea, SWT.NONE).setText("V max");
48

  
49
		maxScore = new Spinner(parametersArea, SWT.BORDER);
50
		maxScore.setToolTipText("The maximum number of word property values to show");
51
		maxScore.setMinimum(0);
52
		maxScore.setSelection(CQPPreferences.getInt(CQPPreferences.MAXVALUE, CQPPreferences.PREFERENCES_NODE));
53
		RowData gdata = new RowData();
54
		gdata.width = 100;
55
		maxScore.setLayoutData(gdata);
56
		Composite displayArea = getDisplayArea();
57
		displayArea.setLayout(new GridLayout(1, true));
58
		
59
		browser = new Browser(displayArea, SWT.NONE);
60
		browser.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true));
61
		
62
		computeResult();
63
	}
64
	
65
	@Override
66
	public TXMParameters getParameters() {
67
		TXMParameters params = super.getParameters();
68
		params.put(CQPPreferences.MAXVALUE, maxScore.getSelection());
69
		return params;
70
	}
71
	
72
	@Override
73
	public void refresh() {
74
		super.refresh();
75
		File file = diag.getHTMLFile();
76
		if (file.exists()) {
77
			browser.setUrl(file.getAbsolutePath());
78
		}
79
	}
80
}
0 81

  
tmp/org.txm.searchengine.cqp.rcp/src/org/txm/searchengine/cqp/rcp/command/ComputeDiagnostic.java (revision 536)
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.command;
29

  
30
import java.io.File;
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.osgi.util.NLS;
40
import org.eclipse.swt.SWT;
41
import org.eclipse.swt.widgets.MessageBox;
42
import org.eclipse.swt.widgets.Shell;
43
import org.eclipse.ui.IWorkbenchPage;
44
import org.eclipse.ui.PartInitException;
45
import org.eclipse.ui.handlers.HandlerUtil;
46
import org.txm.Toolbox;
47
import org.txm.core.preferences.TBXPreferences;
48
import org.txm.core.preferences.TXMPreferences;
49
import org.txm.core.results.TXMParameters;
50
import org.txm.functions.diagnostic.Diagnostic;
51
import org.txm.rcp.JobsTimer;
52
import org.txm.rcp.RCPMessages;
53
import org.txm.rcp.TXMWindows;
54
import org.txm.rcp.commands.OpenBrowser;
55
import org.txm.rcp.editors.TXMResultEditorInput;
56
import org.txm.rcp.editors.TXMWebBrowserEditor;
57
import org.txm.rcp.preferences.RCPPreferences;
58
import org.txm.rcp.utils.JobHandler;
59
import org.txm.searchengine.cqp.AbstractCqiClient;
60
import org.txm.searchengine.cqp.CQPEngine;
61
import org.txm.searchengine.cqp.CQPPreferences;
62
import org.txm.searchengine.cqp.NetCqiClient;
63
import org.txm.searchengine.cqp.corpus.Corpus;
64
import org.txm.searchengine.cqp.corpus.CorpusManager;
65
import org.txm.searchengine.cqp.rcp.editor.DiagnosticEditor;
66
import org.txm.utils.AsciiUtils;
67
import org.txm.utils.logger.Log;
68

  
69
// TODO: Auto-generated Javadoc
70
/**
71
 * Call the function Information on a MainCorpus, a SubCorpus and a Partition
72
 * show basic stats and give some properties values 
73
 * @author mdecorde.
74
 */
75
public class ComputeDiagnostic extends AbstractHandler {
76
	
77
	/** The selection. */
78
	private IStructuredSelection selection;
79
	
80
	/** The response. */
81
	int response = SWT.YES;
82

  
83
	/* (non-Javadoc)
84
	 * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent)
85
	 */
86
	@Override
87
	public Object execute(ExecutionEvent event) throws ExecutionException {
88
		if (!CQPEngine.isInitialized()) {
89
			System.out.println("SearchEngine is not ready. Aborting.");
90
			return false;
91
		}
92
		
93
		Object sel = HandlerUtil.getCurrentSelection(event);
94
		if (!(sel instanceof IStructuredSelection)) {
95
			Log.warning(RCPMessages.ComputeDiagnostic_0);
96
			return null;
97
		}
98
		
99
		selection = (IStructuredSelection) sel;
100
		if (!(selection.getFirstElement() instanceof Corpus)) {
101
			System.out.println(RCPMessages.ComputeDiagnostic_1+selection.getFirstElement());
102
			return null;
103
		}
104
		
105
		final Corpus corpus = (Corpus) selection.getFirstElement();
106

  
107
		String txmhome = TXMPreferences.getString(TBXPreferences.USER_TXM_HOME, TBXPreferences.PREFERENCES_NODE);
108
		if (!new File(txmhome, "informations").exists()) //$NON-NLS-1$
109
			new File(txmhome, "informations").mkdir(); //$NON-NLS-1$
110
		String filename = corpus.getParent().getName()+"-"+corpus.getName(); //$NON-NLS-1$
111
		filename = AsciiUtils.buildId(filename);
112
		final File diagfile = new File(
113
				txmhome, "results/" + filename + "-infos.html"); //$NON-NLS-1$ //$NON-NLS-2$
114

  
115
		AbstractCqiClient cqi = CorpusManager.getCorpusManager().getCqiClient();
116
		if (diagfile.exists() && cqi instanceof NetCqiClient) {
117
			Shell s = new Shell();
118
			MessageBox messageBox = new MessageBox(s, SWT.ICON_QUESTION
119
					| SWT.YES | SWT.NO);
120
			messageBox.setMessage(NLS.bind(RCPMessages.Diagnostique_1, diagfile.getName()));
121
			response = messageBox.open();
122
		}
123

  
124
		JobHandler jobhandler = new JobHandler(NLS.bind(RCPMessages.Diagnostique_0, corpus.getName())) {
125
			@Override
126
			protected IStatus run(IProgressMonitor monitor) {
127
				this.runInit(monitor);
128
				try {
129
					JobsTimer.start();
130
					//this.setCurrentMonitor(monitor);
131
					monitor.beginTask(NLS.bind(RCPMessages.Diagnostique_0, corpus.getName()), 100);
132
					final int maxvalue = CQPPreferences.getInt(CQPPreferences.MAXVALUE, CQPPreferences.PREFERENCES_NODE);
133
					diagfile.getParentFile().mkdir();
134
					
135
					if (monitor.isCanceled())
136
						return Status.CANCEL_STATUS;
137
					
138
					if (response == SWT.NO) return Status.CANCEL_STATUS;
139
					
140
						diagfile.delete();
141
						
142
						monitor.subTask(RCPMessages.Diagnostique_7);
143
						final Diagnostic diag = new Diagnostic(corpus);
144
						diag.setParameters(maxvalue);
145
						
146
						monitor.worked(5);
147
						if (monitor.isCanceled()) {
148
							monitor.done();
149
							return Status.CANCEL_STATUS;
150
						}
151
						
152
						monitor.subTask(RCPMessages.Diagnostique_8);
153
						diag.stepGeneralInfos();
154
						monitor.worked(10);
155
						if(monitor.isCanceled()) {
156
							monitor.done();
157
							return Status.CANCEL_STATUS;
158
						}
159
						
160
						monitor.subTask(RCPMessages.Diagnostique_9);
161
						diag.stepLexicalProperties();
162
						monitor.worked(20);
163
						if (monitor.isCanceled()) {
164
							monitor.done();
165
							return Status.CANCEL_STATUS;
166
						}
167
						
168
						monitor.subTask(RCPMessages.Diagnostique_10);
169
						diag.stepStructuralUnits();
170
						monitor.worked(50);
171
						if (monitor.isCanceled()) {
172
							monitor.done();
173
							return Status.CANCEL_STATUS;
174
						}
175
						
176
						if (monitor.isCanceled())
177
							return Status.CANCEL_STATUS;
178
						
179
						//if(diagfile.exists() && diagfile.canRead())
180
						diag.toHTML(diagfile);
181
						if (!diagfile.exists() || !diagfile.canRead()) {
182
							System.out.println(RCPMessages.Diagnostique_2+corpus+": "+diagfile);  //$NON-NLS-1$
183
							monitor.done();
184
							return Status.CANCEL_STATUS;
185
						}
186
					
187

  
188
					monitor.subTask(RCPMessages.Diagnostique_11);
189
					monitor.worked(5);
190
					syncExec(new Runnable() {
191
						@Override
192
						public void run() {
193
							try {
194
								open(diag);
195
							} catch (PartInitException e) {
196
								// TODO Auto-generated catch block
197
								e.printStackTrace();
198
							}
199
						}
200
					});
201

  
202
				} catch (ThreadDeath td) {
203
					return Status.CANCEL_STATUS;
204
				} catch (Exception e) {
205
					org.txm.rcp.utils.Logger.printStackTrace(e);
206
				} finally {
207
					monitor.done();
208
					JobsTimer.stopAndPrint();
209
				}	
210
				return Status.OK_STATUS;
211
			}
212
		};
213
		jobhandler.startJob();
214

  
215
		return null;
216
	}
217
	
218
	public static void open(Diagnostic diag) throws PartInitException {
219
		IWorkbenchPage page = TXMWindows.getActiveWindow().getActivePage();
220
		page.openEditor(new TXMResultEditorInput(diag), DiagnosticEditor.class.getName());
221
	}
222
}
0 223

  
tmp/org.txm.searchengine.cqp.rcp/src/org/txm/searchengine/cqp/rcp/preferences/DiagnosticPreferencePage.java (revision 536)
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.preferences;
29

  
30
import org.eclipse.jface.preference.IntegerFieldEditor;
31
import org.eclipse.ui.IWorkbench;
32
import org.eclipse.ui.preferences.ScopedPreferenceStore;
33
import org.txm.rcp.Application;
34
import org.txm.rcp.RCPMessages;
35
import org.txm.rcp.preferences.TXMPreferencePage;
36
import org.txm.rcp.preferences.TXMPreferenceStore;
37
import org.txm.searchengine.cqp.CQPPreferences;
38
// TODO: Auto-generated Javadoc
39
/**
40
 * This class represents a preference page that is contributed to the
41
 * Preferences dialog. By subclassing <samp>FieldEditorPreferencePage</samp>, we
42
 * can use the field support built into JFace that allows us to create a page
43
 * that is small and knows how to save, restore and apply itself.
44
 * <p>
45
 * This page is used to modify preferences only. They are stored in the
46
 * preference store that belongs to the main plug-in class. That way,
47
 * preferences can be accessed directly via the preference store.
48
 */
49

  
50
public class DiagnosticPreferencePage extends TXMPreferencePage {
51

  
52
	/** The diag_maxvalue. */
53
	private IntegerFieldEditor diag_maxvalue;
54
		
55
	@Override
56
	public void init(IWorkbench workbench) {
57
		this.setPreferenceStore(new TXMPreferenceStore(CQPPreferences.PREFERENCES_NODE));
58
	}
59
	
60
	/**
61
	 * Instantiates a new diagnostic preference page.
62
	 */
63
	public DiagnosticPreferencePage() {
64
		super();
65
		setTitle(RCPMessages.DiagnosticPreferencePage_1);
66
	}
67

  
68
	/**
69
	 * Creates the field editors. Field editors are abstractions of the common
70
	 * GUI blocks needed to manipulate various types of preferences. Each field
71
	 * editor knows how to save and restore itself.
72
	 */
73
	@Override
74
	public void createFieldEditors() {
75
		
76
		diag_maxvalue = new IntegerFieldEditor(CQPPreferences.MAXVALUE, RCPMessages.DiagnosticPreferencePage_2, getFieldEditorParent());
77
		diag_maxvalue.setValidRange(1, 9999);
78
		addField(diag_maxvalue);
79
	}
80
}
0 81

  
tmp/org.txm.searchengine.cqp.rcp/OSGI-INF/l10n/bundle_fr.properties (revision 536)
1
#Properties file for org.txm.searchengine.cqp.rcp
2
Bundle-Name = CQP RCP
3
command.label.36=Add Project
4
command.label.37=Add Base
5
command.label.65=Delete
6
command.label.148=Open biblio
7
command.name.52=Corpus in binary format...
8
command.label.8=Partition
9
command.name.63=Open Biblio
10
command.name.2=Create Partition
11
command.tooltip.4=Divide the selected item in several parts
12
command.tooltip.71=Export as a binary file
13

  
14
command.label.49=Description
15
command.label.48=Description
0 16

  
tmp/org.txm.searchengine.cqp.rcp/OSGI-INF/l10n/bundle.properties (revision 536)
9 9
command.name.63=Open Biblio
10 10
command.name.2=Create Partition
11 11
command.tooltip.4=Divide the selected item in several parts
12
command.tooltip.71=Export as a binary file
12
command.tooltip.71=Export as a binary file
13

  
14
command.label.49=Description
15
command.label.48=Description
tmp/org.txm.searchengine.cqp.rcp/plugin.xml (revision 536)
34 34
               optional="true">
35 35
         </commandParameter>
36 36
      </command>
37
      <command
38
            defaultHandler="org.txm.searchengine.cqp.rcp.command.ComputeDiagnostic"
39
            id="org.txm.searchengine.cqp.rcp.command.ComputeDiagnostic"
40
            name="Information">
41
      </command>
37 42
   </extension>
38 43
   <extension
39 44
         point="org.eclipse.ui.menus">
......
173 178
      </menuContribution>
174 179
      <menuContribution
175 180
            locationURI="toolbar:org.txm.rcp.toolbarcorpus">
181
            <command
182
                  commandId="org.txm.searchengine.cqp.rcp.command.ComputeDiagnostic"
183
                  icon="icons/functions/diagnostique.png"
184
                  id="menu.corpus.description"
185
                  label="%command.label.48"
186
                  style="push"
187
                  tooltip="%command.tooltip.60">
188
               <visibleWhen
189
                     checkEnabled="false">
190
                  <reference
191
                        definitionId="OneCorpusSelected">
192
                  </reference>
193
               </visibleWhen>
194
            </command>
176 195
         <command
177 196
               commandId="org.txm.rcp.commands.base.CreateSubCorpus"
178 197
               icon="icons/functions/SubCorpus.png"
......
201 220
      </menuContribution>
202 221
      <menuContribution
203 222
            locationURI="popup:org.txm.rcp.views.corpora.CorporaView">
223
            <command
224
                  commandId="org.txm.searchengine.cqp.rcp.command.ComputeDiagnostic"
225
                  icon="icons/functions/diagnostique.png"
226
                  id="menu.corpus.description"
227
                  label="%command.label.48"
228
                  style="push"
229
                  tooltip="%command.tooltip.60">
230
               <visibleWhen
231
                     checkEnabled="false">
232
                  <reference
233
                        definitionId="OneCorpusSelected">
234
                  </reference>
235
               </visibleWhen>
236
            </command>
204 237
         <command
205 238
               commandId="org.txm.rcp.commands.base.CreateSubCorpus"
206 239
               icon="icons/functions/SubCorpus.png"
......
229 262
      </menuContribution>
230 263
      <menuContribution
231 264
            locationURI="menu:menu.corpus">
265
            <command
266
                  commandId="org.txm.searchengine.cqp.rcp.command.ComputeDiagnostic"
267
                  icon="icons/functions/diagnostique.png"
268
                  id="menu.corpus.description"
269
                  label="%command.label.48"
270
                  style="push"
271
                  tooltip="%command.tooltip.60">
272
               <visibleWhen
273
                     checkEnabled="false">
274
                  <reference
275
                        definitionId="OneCorpusSelected">
276
                  </reference>
277
               </visibleWhen>
278
            </command>
232 279
         <command
233 280
               commandId="org.txm.rcp.commands.base.CreateSubCorpus"
234 281
               icon="icons/functions/SubCorpus.png"
......
265 312
            name="CQP">
266 313
      </page>
267 314
   </extension>
315
   <extension
316
         point="org.eclipse.ui.editors">
317
      <editor
318
            class="org.txm.searchengine.cqp.rcp.editor.DiagnosticEditor"
319
            default="false"
320
            icon="icons/functions/Biblio.png"
321
            id="org.txm.searchengine.cqp.rcp.editor.DiagnosticEditor"
322
            name="Diagnostic">
323
      </editor>
324
   </extension>
268 325

  
269 326
</plugin>
tmp/org.txm.searchengine.cqp.rcp/META-INF/MANIFEST.MF (revision 536)
13 13
 org.eclipse.ui
14 14
Export-Package: org.txm.rcp.commands.base,
15 15
 org.txm.searchengine.cqp.rcp,
16
 org.txm.searchengine.cqp.rcp.editor,
16 17
 org.txm.searchengine.cqp.rcp.preferences
tmp/org.txm.specificities.rcp/src/org/txm/specificities/rcp/handlers/ComputeSpecifictiesBarChart.java (revision 536)
19 19
import org.txm.rcp.IImageKeys;
20 20
import org.txm.specificities.core.functions.Specificities;
21 21
import org.txm.specificities.core.preferences.SpecificitiesPreferences;
22
import org.txm.specificities.rcp.editors.SpecificitiesTableEditor;
22
import org.txm.specificities.rcp.editors.SpecificitiesEditor;
23 23
import org.txm.specificities.rcp.editors.SpecificitiesTableLines;
24 24
import org.txm.specificities.rcp.messages.SpecificitiesUIMessages;
25 25

  
......
35 35
			IWorkbenchWindow iww = HandlerUtil.getActiveWorkbenchWindow(event);
36 36
			IWorkbenchPage iwp = iww.getActivePage();
37 37
			IEditorPart ieditor = iwp.getActiveEditor();
38
			if (!(ieditor instanceof SpecificitiesTableEditor)) {
38
			if (!(ieditor instanceof SpecificitiesEditor)) {
39 39
				System.out.println("Editor is not SpecificitiesTableEditor: " + ieditor.getClass());
40 40
				return null;
41 41
			}
42 42

  
43
			SpecificitiesTableEditor editor = (SpecificitiesTableEditor)ieditor;
43
			SpecificitiesEditor editor = (SpecificitiesEditor)ieditor;
44 44
			Specificities specificitiesResult = editor.getSpecificitiesResult();
45 45
			Table specificitesTable = editor.getSpecificitesTable();
46 46
			SpecificitiesTableLines tableLines = editor.getTableLines();
tmp/org.txm.specificities.rcp/src/org/txm/specificities/rcp/handlers/ComputeSpecifities.java (revision 536)
63 63
import org.txm.specificities.rcp.dialogs.ComputeSpecificitiesDialog;
64 64
import org.txm.specificities.rcp.dialogs.ComputeSpecificitiesForSubcorpusDialog;
65 65
import org.txm.specificities.rcp.editors.SpecificitiesResultEditorInput;
66
import org.txm.specificities.rcp.editors.SpecificitiesTableEditor;
66
import org.txm.specificities.rcp.editors.SpecificitiesEditor;
67 67
import org.txm.statsengine.core.StatException;
68 68
import org.txm.statsengine.r.rcp.views.RVariablesView;
69 69
import org.txm.utils.logger.Log;
......
210 210

  
211 211
							editorInput = new SpecificitiesResultEditorInput(specifResFinal);
212 212

  
213
							TXMEditorPart.openEditor(editorInput, SpecificitiesTableEditor.ID);
213
							TXMEditorPart.openEditor(editorInput, SpecificitiesEditor.ID);
214 214
						}
215 215
					});
216 216
					
tmp/org.txm.specificities.rcp/src/org/txm/specificities/rcp/editors/SpecificitiesTableEditor.java (revision 536)
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.specificities.rcp.editors;
29

  
30
import java.lang.reflect.InvocationTargetException;
31
import java.text.Collator;
32
import java.util.ArrayList;
33
import java.util.Comparator;
34
import java.util.List;
35
import java.util.Locale;
36

  
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.jface.action.MenuManager;
41
import org.eclipse.jface.operation.IRunnableWithProgress;
42
import org.eclipse.jface.viewers.IStructuredContentProvider;
43
import org.eclipse.jface.viewers.TableViewer;
44
import org.eclipse.jface.viewers.Viewer;
45
import org.eclipse.osgi.util.NLS;
46
import org.eclipse.swt.SWT;
47
import org.eclipse.swt.events.SelectionEvent;
48
import org.eclipse.swt.events.SelectionListener;
49
import org.eclipse.swt.layout.GridData;
50
import org.eclipse.swt.layout.GridLayout;
51
import org.eclipse.swt.widgets.Composite;
52
import org.eclipse.swt.widgets.Label;
53
import org.eclipse.swt.widgets.Menu;
54
import org.eclipse.swt.widgets.ScrollBar;
55
import org.eclipse.swt.widgets.Spinner;
56
import org.eclipse.swt.widgets.Table;
57
import org.eclipse.swt.widgets.TableColumn;
58
import org.eclipse.ui.IEditorInput;
59
import org.eclipse.ui.IEditorSite;
60
import org.eclipse.ui.PartInitException;
61
import org.eclipse.ui.PlatformUI;
62
import org.eclipse.ui.part.EditorPart;
63
import org.txm.core.results.TXMResult;
64
import org.txm.objects.TxmObject;
65
import org.txm.rcp.RCPMessages;
66
import org.txm.rcp.StatusLine;
67
import org.txm.rcp.editors.TXMEditorPart;
68
import org.txm.rcp.editors.TableKeyListener;
69
import org.txm.rcp.utils.JobHandler;
70
import org.txm.specificities.core.functions.Specificities;
71
import org.txm.specificities.core.preferences.SpecificitiesPreferences;
72
import org.txm.specificities.rcp.messages.SpecificitiesUIMessages;
73
import org.txm.statsengine.core.StatException;
74
import org.txm.utils.logger.Log;
75
/**
76
 * Display a table editor with specificites index.
77
 * 
78
 * @author sloiseau, mdecorde, sjaquot
79
 * 
80
 */
81
public class SpecificitiesTableEditor extends TXMEditorPart {
82

  
83
	public static String ID = SpecificitiesTableEditor.class.getName();
84

  
85
	/** The specificities table. */
86
	private Table specificitesTable;
87
	private TableViewer viewer;
88

  
89
	/** Column for table typeNameTable. */
90
	private TableColumn typeNameColumn;
91

  
92
	/** Column for table typeFrequencyColumn. */
93
	private TableColumn typeFrequencyColumn;
94

  
95
	/** The current comparator: updated when user click on a column header */
96
	protected Comparator currentComparator;
97

  
98
	/** All columns for the table specificitesTable. */
99
	private List<TableColumn> specificitesTableColumn = new ArrayList<TableColumn>();
100

  
101
	/** The result of specificities*/
102
	private Specificities specificitiesResult;
103

  
104
	/** The table lines : table input */
105
	private SpecificitiesTableLines tableLines;
106

  
107
	/** The source. */
108
	private Object source;
109

  
110
	/* (non-Javadoc)
111
	 * @see org.eclipse.ui.part.EditorPart#init(org.eclipse.ui.IEditorSite, org.eclipse.ui.IEditorInput)
112
	 */
113
	@Override
114
	public void init(IEditorSite site, IEditorInput input) throws PartInitException {
115
		super.init(site, input);
116

  
117
		SpecificitiesResultEditorInput spinput = (SpecificitiesResultEditorInput) getEditorInput();
118
		this.specificitiesResult = (Specificities) ((SpecificitiesResultEditorInput) getEditorInput()).getResult();
119
		if (this.specificitiesResult.getLexicalTable()!= null && this.specificitiesResult.getLexicalTable().getPartition() != null)	{
120
			setPartName(this.specificitiesResult.getLexicalTable().getPartition().getName()+": "+specificitiesResult.getName()); //$NON-NLS-1$
121
		} else if (spinput.getSubcorpus() != null) {
122
			setPartName(spinput.getSubcorpus().getName()+": "+specificitiesResult.getName()); //$NON-NLS-1$
123
		} else {
124
			setPartName(specificitiesResult.getName());
125
		}
126
	}
127

  
128
	/* (non-Javadoc)
129
	 * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
130
	 */
131
	@Override
132
	public void createPartControl(Composite parent) {
133
		super.createPartControl(parent);
134

  
135
		Composite parametersArea = getCommandParametersGroup();
136
		
137
		new Label(parametersArea, SWT.NONE).setText("Maximum score");
138
		
139
		Spinner maxScore = new Spinner(parametersArea, SWT.BORDER);
140
		maxScore.setToolTipText("The maximum score to show");
141
		maxScore.setMinimum(0);
142
		maxScore.setSelection(SpecificitiesPreferences.getInt(SpecificitiesPreferences.MAX_SCORE, SpecificitiesPreferences.PREFERENCES_NODE));
143
		
144
		Composite displayArea = getDisplayArea();
145
		//displayArea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
146
		try {
147
			composeDisplayArea(displayArea);
148
		} catch (StatException e) {
149
			System.err.println(NLS.bind(SpecificitiesUIMessages.SpecificitiesTableEditor_0, e.getMessage()));
150
		}
151
	}
152

  
153
	/** Sort. */
154
	protected void sort() {
155
		if (currentComparator != null) {
156
			try {
157
				PlatformUI.getWorkbench().getProgressService().busyCursorWhile(
158
						new IRunnableWithProgress() {
159
							@Override
160
							public void run(IProgressMonitor monitor)
161
									throws InvocationTargetException,
162
									InterruptedException {
163
								tableLines.sort(currentComparator);
164
							}
165
						});
166
				StatusLine.setMessage(RCPMessages.SORT_DONE);
167
				fillDisplay();
168
			} catch (Exception e) {
169
				System.err.println(NLS.bind(SpecificitiesUIMessages.SpecificitiesTableEditor_1, e)); 
170
			}
171
		}
172
	}
173

  
174
	/**
175
	 * Compose display area.
176
	 *
177
	 * @param parent the parent
178
	 * @throws StatException the stat exception
179
	 */
180
	private void composeDisplayArea(final Composite parent) throws StatException {
181

  
182
		GridLayout layout = new GridLayout(2, false);
183
		layout.marginWidth = layout.marginHeight = layout.horizontalSpacing = 0;
184
		parent.setLayout(layout);
185

  
186
		// create table viewer
187
		viewer = new TableViewer(parent, SWT.VIRTUAL | SWT.MULTI | SWT.FULL_SELECTION);
188
		viewer.getTable().addKeyListener(new TableKeyListener(viewer));
189
		viewer.getTable().setLinesVisible(true);
190
		viewer.getTable().setHeaderVisible(true);
191
		viewer.setLabelProvider(new SpecificitiesTableLabelProvider());
192

  
193
		specificitesTable = viewer.getTable();
194
		GridData table2Data = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 2);
195
		specificitesTable.setLayoutData(table2Data);
196

  
197
		// create 1st column
198
		typeNameColumn = new TableColumn(specificitesTable,
199
				SWT.NONE);
200
		typeNameColumn.setText(RCPMessages.ReferencerEditor_9);
201
		typeNameColumn.setToolTipText(RCPMessages.ReferencerEditor_9);
202
		typeNameColumn.setAlignment(SWT.RIGHT);
203
		typeNameColumn.pack();
204
		typeNameColumn.setWidth((int) (typeNameColumn.getWidth()*1.5));
205

  
206
		Object oparent = specificitiesResult.getParent();
207
		Locale locale = Locale.getDefault();
208
		if (oparent instanceof TxmObject) {
209
			String lang = (String) ((TxmObject) oparent).getAttribute("lang"); //$NON-NLS-1$
210
			if (lang != null)
211
				locale = new Locale(lang);
212
		}
213
		final Collator collator = Collator.getInstance(locale);
214
		collator.setStrength(Collator.TERTIARY);
215
		typeNameColumn.addSelectionListener(new SelectionListener() {
216
			@Override
217
			public void widgetSelected(SelectionEvent e) {
218
				StatusLine.setMessage(RCPMessages.SORT_COLUMN);
219
				Comparator column_comparator = new Comparator() {
220
					@Override
221
					public int compare(Object o1, Object o2) {
222
						Object[] line1 = (Object[]) o1;
223
						Object[] line2 = (Object[]) o2;
224
						return collator.compare(line1[0], line2[0]);
225
					}
226
				};
227
				if (specificitesTable.getSortColumn() != typeNameColumn) {
228
					specificitesTable.setSortColumn(
229
							typeNameColumn);
230
					specificitesTable.setSortDirection(SWT.UP);
231
				} else if (specificitesTable
232
						.getSortDirection() == SWT.UP) {
233
					specificitesTable.setSortDirection(
234
							SWT.DOWN);
235
					column_comparator = new ReverseComparator(column_comparator);
236
				} else {
237
					specificitesTable.setSortDirection(SWT.UP);
238
				}
239
				currentComparator = column_comparator;
240
				sort();
241
				StatusLine.setMessage(""); //$NON-NLS-1$
242
			}
243

  
244
			@Override
245
			public void widgetDefaultSelected(SelectionEvent e) {
246
			}
247
		});
248

  
249
		// create 2nd column
250
		typeFrequencyColumn = new TableColumn(viewer
251
				.getTable(), SWT.NONE);
252
		typeFrequencyColumn.setText(RCPMessages.FrequencyListEditorInput_4
253
				+ " "+SpecificitiesUIMessages.SpecificitiesTableEditor_11+" "
254
				+ specificitiesResult.getCorpusSize()); 
255
		typeFrequencyColumn.setToolTipText(RCPMessages.FrequencyListEditorInput_4
256
				+ SpecificitiesUIMessages.SpecificitiesTableEditor_11
257
				+ specificitiesResult.getCorpusSize()); 
258
		typeFrequencyColumn.setAlignment(SWT.RIGHT);
259
		typeFrequencyColumn.pack();
260
		typeFrequencyColumn.addSelectionListener(new SelectionListener() {
261
			@Override
262
			public void widgetSelected(SelectionEvent e) {
263
				StatusLine.setMessage(RCPMessages.SORT_COLUMN);
264
				Comparator<Object[]> column_comparator = new Comparator<Object[]>() {
265
					@Override
266
					public int compare(Object[] line1, Object[] line2) { // Compare two
267
						return ((Integer) line1[1]).compareTo(((Integer) line2[1]));
268
					}
269
				};
270
				if (specificitesTable.getSortColumn() != typeFrequencyColumn) {
271
					specificitesTable.setSortColumn(
272
							typeFrequencyColumn);
273
					specificitesTable.setSortDirection(SWT.UP);
274
				} else if (specificitesTable
275
						.getSortDirection() == SWT.UP) {
276
					specificitesTable.setSortDirection(
277
							SWT.DOWN);
278
					column_comparator = new ReverseComparator(column_comparator);
279
				} else {
280
					specificitesTable.setSortDirection(SWT.UP);
281
				}
282
				currentComparator = column_comparator;
283
				sort();
284
				StatusLine.setMessage(""); //$NON-NLS-1$
285
			}
286

  
287
			@Override
288
			public void widgetDefaultSelected(SelectionEvent e) {
289
			}
290
		});
291

  
292
		// add a separator column
293
		TableColumn separatorColumn = new TableColumn(specificitesTable, SWT.NONE);
294
		separatorColumn.setResizable(false);
295
		specificitesTableColumn.add(separatorColumn);
296

  
297
		String[] partNames = null;
298
		int[] partSize = null;
299
		try {
300
			partNames = specificitiesResult.getPartShortNames();
301
			partSize = specificitiesResult.getPartSizes();
302
		} catch (StatException e) {
303
			System.err.println(NLS.bind(SpecificitiesUIMessages.SpecificitiesTableEditor_5, e));
304
			Log.printStackTrace(e);
305
			return;
306
		}
307
		// System.out.println("partnames: "+Arrays.toString(partNames));
308
		// for (int i = 0; i <
309
		// specificitesResult.getSpecificitesIndex()[0].length; i++)
310
		for (int i = 0; i < partNames.length ; i++) { // for each part
311
			final int columnIndex = i;
312

  
313
			//FREQ COLUMN
314
			final TableColumn freqpartTableColumn = new TableColumn(
315
					specificitesTable, SWT.NONE);
316
			freqpartTableColumn.setText(partNames[i] + " t=" + partSize[i]); //$NON-NLS-1$ 
317
			freqpartTableColumn.setToolTipText(partNames[i] + " t=" + partSize[i]); //$NON-NLS-1$ 
318
			// System.out.println("create column "+partNames[i]);
319
			freqpartTableColumn.setAlignment(SWT.RIGHT);
320
			freqpartTableColumn.pack();
321
			freqpartTableColumn.addSelectionListener(new SelectionListener() {
322
				@Override
323
				public void widgetSelected(SelectionEvent e) {
324
					StatusLine.setMessage(RCPMessages.SORT_COLUMN);
325
					Comparator<Object> column_comparator = new Comparator<Object>() {
326
						@Override
327
						public int compare(Object o1, Object o2) { // Compare
328
							// two Integer
329
							Object[] line1 = (Object[]) o1;
330
							Object[] line2 = (Object[]) o2;
331
							return new Integer(
332
									((int[]) line1[2])[columnIndex])
333
							.compareTo(new Integer(
334
									((int[]) line2[2])[columnIndex]));
335
						}
336
					};
337

  
338
					if (specificitesTable.getSortColumn() != freqpartTableColumn) {
339
						specificitesTable.setSortColumn(freqpartTableColumn);
340
						specificitesTable.setSortDirection(SWT.DOWN);
341
						column_comparator = new ReverseComparator(column_comparator);
342
					} else if (specificitesTable.getSortDirection() == SWT.UP) {
343
						specificitesTable.setSortDirection(SWT.DOWN);
344
						column_comparator = new ReverseComparator(column_comparator);
345
					} else {
346
						specificitesTable.setSortDirection(SWT.UP);
347
					}
348
					currentComparator = column_comparator;
349
					sort();
350
					StatusLine.setMessage(""); //$NON-NLS-1$
351
				}
352

  
353
				@Override
354
				public void widgetDefaultSelected(SelectionEvent e) {
355
				}
356
			});
357
			specificitesTableColumn.add(freqpartTableColumn);
358

  
359
			//SCORE COLUMN
360
			final TableColumn partTableColumn = new TableColumn(
361
					specificitesTable, SWT.NONE);
362
			partTableColumn.setText("score");//partNames[i] + " t=" + partSize[i]); //$NON-NLS-1$ 
363
			partTableColumn.setToolTipText(partNames[i] + " t=" + partSize[i]); //$NON-NLS-1$ 
364
			// System.out.println("create column "+partNames[i]);
365
			partTableColumn.setAlignment(SWT.RIGHT);
366
			partTableColumn.pack();
367
			partTableColumn.addSelectionListener(new SelectionListener() {
368
				@Override
369
				public void widgetSelected(SelectionEvent e) {
370
					StatusLine.setMessage(RCPMessages.SORT_COLUMN);
371
					Comparator<Object> column_comparator = new Comparator<Object>() {
372
						@Override
373
						public int compare(Object o1, Object o2) { // Compare
374
							Object[] line1 = (Object[]) o1;
375
							Object[] line2 = (Object[]) o2;
376
							return new Double(
377
									((double[]) line1[3])[columnIndex])
378
							.compareTo(new Double(
379
									((double[]) line2[3])[columnIndex]));
380
						}
381
					};
382
					if (specificitesTable.getSortColumn() != partTableColumn) {
383
						specificitesTable.setSortColumn(partTableColumn);
384
						specificitesTable.setSortDirection(SWT.DOWN);
385
						column_comparator = new ReverseComparator(column_comparator);
386
					} else if (specificitesTable.getSortDirection() == SWT.UP) {
387
						specificitesTable.setSortDirection(SWT.DOWN);
388
						column_comparator = new ReverseComparator(column_comparator);
389
					} else {
390
						specificitesTable.setSortDirection(
391
								SWT.UP);
392
					}
393
					currentComparator = column_comparator;
394
					sort();
395
					StatusLine.setMessage(""); //$NON-NLS-1$
396
				}
397

  
398
				@Override
399
				public void widgetDefaultSelected(SelectionEvent e) {
400
				}
401
			});
402

  
403
			specificitesTableColumn.add(partTableColumn);
404
		}
405

  
406
		specificitesTable.setHeaderVisible(true);
407

  
408
		// Horizontal bar on second table takes up a little extra space.
409
		// To keep vertical scroll bars in sink, force table1 to end above
410
		// horizontal scrollbar
411
		ScrollBar hBarRight = specificitesTable.getHorizontalBar();
412
		Label spacer = new Label(parent, SWT.NONE);
413
		GridData spacerData = new GridData();
414
		spacerData.heightHint = hBarRight.getSize().y;
415
		spacer.setLayoutData(spacerData);
416
		spacer.setVisible(false);
417

  
418
		// --------------------------------------
419
		// make the contextual menu for displaying graphics.
420
		// --------------------------------------
421

  
422
		// Menu menu = new Menu(specificitesTable); // old ways
423
		MenuManager menuManager = new MenuManager();
424
		Menu menu = menuManager.createContextMenu(specificitesTable);
425
		specificitesTable.setMenu(menu);
426
		getSite().setSelectionProvider(viewer);
427
		getSite().registerContextMenu(menuManager, viewer);
428

  
429

  
430

  
431
		// and first sort
432
		specificitesTable.setSortColumn(typeNameColumn);
433
		specificitesTable.setSortDirection(SWT.UP);
434
		currentComparator = new Comparator() {
435
			@Override
436
			public int compare(Object o1, Object o2) {
437
				Object[] line1 = (Object[]) o1;
438
				Object[] line2 = (Object[]) o2;
439
				return collator.compare(line1[0], line2[0]);
440
			}
441
		};
442

  
443
		// compute the specif scores
444
		computeResult();
445

  
446
	}
447

  
448
	public Table getSpecificitesTable() {
449
		return specificitesTable;
450
	}
451

  
452
	/**
453
	 * Fills display.
454
	 */
455
	private void fillDisplay() {
456

  
457
		viewer.setInput(tableLines);
458

  
459
		// Pack the columns so the label of the sort column is not truncated due of the sorting order arrow display
460
		// Refresh and pack the columns
461
		viewer.getControl().setRedraw(false);
462
		viewer.refresh();
463
		for (TableColumn col : viewer.getTable().getColumns()) {
464
			col.pack();
465
		}
466
		viewer.getControl().setRedraw(true);
467
	}
468

  
469
	/* (non-Javadoc)
470
	 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
471
	 */
472
	@Override
473
	public void setFocus() {
474
		StatusLine.setMessage(SpecificitiesUIMessages.SpecificitiesTableEditor_17);
475
		specificitesTable.setFocus();
476
	}
477

  
478
	/**
479
	 * Instantiates a new specificities table editor.
480
	 */
481
	public SpecificitiesTableEditor() {
482
	}
483

  
484
	/* (non-Javadoc)
485
	 * @see org.eclipse.ui.part.EditorPart#doSave(org.eclipse.core.runtime.IProgressMonitor)
486
	 */
487
	@Override
488
	public void doSave(IProgressMonitor monitor) {
489
	}
490

  
491
	/* (non-Javadoc)
492
	 * @see org.eclipse.ui.part.EditorPart#doSaveAs()
493
	 */
494
	@Override
495
	public void doSaveAs() {
496
	}
497

  
498
	/* (non-Javadoc)
499
	 * @see org.eclipse.ui.part.EditorPart#isDirty()
500
	 */
501
	@Override
502
	public boolean isDirty() {
503
		return false;
504
	}
505

  
506
	/* (non-Javadoc)
507
	 * @see org.eclipse.ui.part.EditorPart#isSaveAsAllowed()
508
	 */
509
	@Override
510
	public boolean isSaveAsAllowed() {
511
		return false;
512
	}
513

  
514
	/**
515
	 * Gets the specificities result.
516
	 *
517
	 * @return the specificities result
518
	 */
519
	public Specificities getSpecificitiesResult() {
520
		return this.specificitiesResult;
521
	}
522

  
523
	public SpecificitiesTableLines getTableLines() {
524
		return tableLines;
525
	}
526

  
527
	@Override
528
	public boolean isResultUsingParent(TXMResult parent) {
529
		if (this.specificitiesResult == null) return false;
530
		if (this.specificitiesResult.getCorpus() == null) return false;
531
		return this.specificitiesResult.getCorpus().getName().equals(parent.getName());
532
	}
533

  
534
	@Override
535
	public void computeResult() {
536
		JobHandler job = new JobHandler("Computing specificities...") {
537

  
538
			@Override
539
			protected IStatus run(IProgressMonitor monitor) {
540
				this.runInit(monitor);
541
				try {
542
					if (!specificitiesResult.compute(true, monitor)) {
543
						System.out.println("Error: Specificities not computed.");
544
						return Status.CANCEL_STATUS;
545
					}
546

  
547
					// units
548
					String[] typeNames = specificitiesResult.getTypeNames();
549
					// units' total freq
550
					int[] typeFreq = specificitiesResult.getFormFrequencies();
551
					// units' index fr each part
552
					double[][] specIndex = specificitiesResult.getSpecificitesIndex();
553
					int[][] specFreqs = specificitiesResult.getFrequency();
554
					
555
					// units' total in parts
556
					//final int[] sortedPartIndexes = specificitesResult.getSortedPartIndexes();
557

  
558
					// Create an array of lines to fill the tables
559
					//		System.out.println("len types: "+typeNames.length);
560
					//		System.out.println("len freq: "+typeFreq.length);
561
					//		System.out.println("len spec: "+specFreqs.length);
562
					//		System.out.println("len specidx: "+specIndex.length);
563
					Object[] lines = new Object[typeNames.length];
564
					for (int j = 0; j < lines.length; j++)
565
						lines[j] = new Object[] { typeNames[j], new Integer(typeFreq[j]), specFreqs[j], specIndex[j] };
566

  
567
					// final SpecificitesTableLines
568
					tableLines = new SpecificitiesTableLines(lines);
569
					
570
					// set the TableViewer input
571
					viewer.setContentProvider(new IStructuredContentProvider() {
572
						@Override
573
						public Object[] getElements(Object inputElement) {
574
							return ((SpecificitiesTableLines) inputElement).getLines();
575
						}
576

  
577
						@Override
578
						public void dispose() { }
579

  
580
						@Override
581
						public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
582
						}
583
					});
584
					
585
					this.syncExec(new Runnable() {
586
						@Override
587
						public void run() {
588
							viewer.refresh();
589
							SpecificitiesTableEditor.this.firePropertyChange(EditorPart.PROP_DIRTY);
590
							// Draw lines
591
							fillDisplay();
592
							sort();
593
							specificitesTable.setFocus();
594
						}
595
					});
596
					
597
				} catch (Exception e) {
598
					System.out.println("Error while computing specificities: "+e.getLocalizedMessage());
599
					Log.printStackTrace(e);
600
				}
601
				return Status.OK_STATUS;
602
			}
603
		};
604
		job.schedule();
605
	}
606

  
607
	@Override
608
	protected void initializeFields() {
609
		// TODO Auto-generated method stub
610

  
611
	}
612
}
tmp/org.txm.specificities.rcp/src/org/txm/specificities/rcp/editors/SpecificitiesEditor.java (revision 536)
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.specificities.rcp.editors;
29

  
30
import java.lang.reflect.InvocationTargetException;
31
import java.text.Collator;
32
import java.util.ArrayList;
33
import java.util.Comparator;
34
import java.util.List;
35
import java.util.Locale;
36

  
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.jface.action.MenuManager;
41
import org.eclipse.jface.operation.IRunnableWithProgress;
42
import org.eclipse.jface.viewers.IStructuredContentProvider;
43
import org.eclipse.jface.viewers.TableViewer;
44
import org.eclipse.jface.viewers.Viewer;
45
import org.eclipse.osgi.util.NLS;
46
import org.eclipse.swt.SWT;
47
import org.eclipse.swt.events.SelectionEvent;
48
import org.eclipse.swt.events.SelectionListener;
49
import org.eclipse.swt.layout.GridData;
50
import org.eclipse.swt.layout.GridLayout;
51
import org.eclipse.swt.widgets.Composite;
52
import org.eclipse.swt.widgets.Label;
53
import org.eclipse.swt.widgets.Menu;
54
import org.eclipse.swt.widgets.ScrollBar;
55
import org.eclipse.swt.widgets.Spinner;
56
import org.eclipse.swt.widgets.Table;
57
import org.eclipse.swt.widgets.TableColumn;
58
import org.eclipse.ui.IEditorInput;
59
import org.eclipse.ui.IEditorSite;
60
import org.eclipse.ui.PartInitException;
61
import org.eclipse.ui.PlatformUI;
62
import org.eclipse.ui.part.EditorPart;
63
import org.txm.core.results.TXMResult;
64
import org.txm.index.core.functions.Index;
65
import org.txm.lexicaltable.core.functions.LexicalTable;
66
import org.txm.objects.TxmObject;
67
import org.txm.rcp.RCPMessages;
68
import org.txm.rcp.StatusLine;
69
import org.txm.rcp.editors.TXMEditorPart;
70
import org.txm.rcp.editors.TableKeyListener;
71
import org.txm.rcp.utils.JobHandler;
72
import org.txm.specificities.core.functions.Specificities;
73
import org.txm.specificities.core.preferences.SpecificitiesPreferences;
74
import org.txm.specificities.rcp.messages.SpecificitiesUIMessages;
75
import org.txm.statsengine.core.StatException;
76
import org.txm.utils.logger.Log;
77
/**
78
 * Display a table editor with specificites index.
79
 * 
80
 * @author sloiseau, mdecorde, sjaquot
81
 * 
82
 */
83
public class SpecificitiesEditor extends TXMEditorPart {
84

  
85
	public static String ID = SpecificitiesEditor.class.getName();
86

  
87
	/** The specificities table. */
88
	private Table specificitesTable;
89
	private TableViewer viewer;
90

  
91
	/** Column for table typeNameTable. */
92
	private TableColumn typeNameColumn;
93

  
94
	/** Column for table typeFrequencyColumn. */
95
	private TableColumn typeFrequencyColumn;
96

  
97
	/** The current comparator: updated when user click on a column header */
98
	protected Comparator currentComparator;
99

  
100
	/** All columns for the table specificitesTable. */
101
	private List<TableColumn> specificitesTableColumn = new ArrayList<TableColumn>();
102

  
103
	/** The result of specificities*/
104
	private Specificities specificitiesResult;
105

  
106
	/** The table lines : table input */
107
	private SpecificitiesTableLines tableLines;
108

  
109
	/** The source. */
110
	private Object source;
111

  
112
	/* (non-Javadoc)
113
	 * @see org.eclipse.ui.part.EditorPart#init(org.eclipse.ui.IEditorSite, org.eclipse.ui.IEditorInput)
114
	 */
115
	@Override
116
	public void init(IEditorSite site, IEditorInput input) throws PartInitException {
117
		super.init(site, input);
118

  
119
		SpecificitiesResultEditorInput spinput = (SpecificitiesResultEditorInput) getEditorInput();
120
		this.specificitiesResult = (Specificities) ((SpecificitiesResultEditorInput) getEditorInput()).getResult();
121
		if (this.specificitiesResult.getLexicalTable()!= null && this.specificitiesResult.getLexicalTable().getPartition() != null)	{
122
			setPartName(this.specificitiesResult.getLexicalTable().getPartition().getName()+": "+specificitiesResult.getName()); //$NON-NLS-1$
123
		} else if (spinput.getSubcorpus() != null) {
124
			setPartName(spinput.getSubcorpus().getName()+": "+specificitiesResult.getName()); //$NON-NLS-1$
125
		} else {
126
			setPartName(specificitiesResult.getName());
127
		}
128
	}
129

  
130
	/* (non-Javadoc)
131
	 * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
132
	 */
133
	@Override
134
	public void createPartControl(Composite parent) {
135
		super.createPartControl(parent);
136

  
137
		Composite parametersArea = getCommandParametersGroup();
138
		
139
		new Label(parametersArea, SWT.NONE).setText("Maximum score");
140

  
141
		Spinner maxScore = new Spinner(parametersArea, SWT.BORDER);
142
		maxScore.setToolTipText("The maximum score to show");
143
		maxScore.setMinimum(0);
144
		maxScore.setSelection(SpecificitiesPreferences.getInt(SpecificitiesPreferences.MAX_SCORE, SpecificitiesPreferences.PREFERENCES_NODE));
145
		
146
		Composite displayArea = getDisplayArea();
147
		//displayArea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
148
		try {
149
			composeDisplayArea(displayArea);
150
		} catch (StatException e) {
151
			System.err.println(NLS.bind(SpecificitiesUIMessages.SpecificitiesTableEditor_0, e.getMessage()));
152
		}
153
	}
154

  
155
	/** Sort. */
156
	protected void sort() {
157
		if (currentComparator != null) {
158
			try {
159
				PlatformUI.getWorkbench().getProgressService().busyCursorWhile(
160
						new IRunnableWithProgress() {
161
							@Override
162
							public void run(IProgressMonitor monitor)
163
									throws InvocationTargetException,
164
									InterruptedException {
165
								tableLines.sort(currentComparator);
166
							}
167
						});
168
				StatusLine.setMessage(RCPMessages.SORT_DONE);
169
				fillDisplay();
170
			} catch (Exception e) {
171
				System.err.println(NLS.bind(SpecificitiesUIMessages.SpecificitiesTableEditor_1, e)); 
172
			}
173
		}
174
	}
175

  
176
	/**
177
	 * Compose display area.
178
	 *
179
	 * @param parent the parent
180
	 * @throws StatException the stat exception
181
	 */
182
	private void composeDisplayArea(final Composite parent) throws StatException {
183

  
184
		GridLayout layout = new GridLayout(2, false);
185
		layout.marginWidth = layout.marginHeight = layout.horizontalSpacing = 0;
186
		parent.setLayout(layout);
187

  
188
		// create table viewer
189
		viewer = new TableViewer(parent, SWT.VIRTUAL | SWT.MULTI | SWT.FULL_SELECTION);
190
		viewer.getTable().addKeyListener(new TableKeyListener(viewer));
191
		viewer.getTable().setLinesVisible(true);
192
		viewer.getTable().setHeaderVisible(true);
193
		viewer.setLabelProvider(new SpecificitiesTableLabelProvider());
194

  
195
		specificitesTable = viewer.getTable();
196
		GridData table2Data = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 2);
197
		specificitesTable.setLayoutData(table2Data);
198

  
199
		// create 1st column
200
		typeNameColumn = new TableColumn(specificitesTable,
201
				SWT.NONE);
202
		typeNameColumn.setText(RCPMessages.ReferencerEditor_9);
203
		typeNameColumn.setToolTipText(RCPMessages.ReferencerEditor_9);
204
		typeNameColumn.setAlignment(SWT.RIGHT);
205
		typeNameColumn.pack();
206
		typeNameColumn.setWidth((int) (typeNameColumn.getWidth()*1.5));
207

  
208
		Object oparent = specificitiesResult.getParent();
209
		Locale locale = Locale.getDefault();
210
		if (oparent instanceof TxmObject) {
211
			String lang = (String) ((TxmObject) oparent).getAttribute("lang"); //$NON-NLS-1$
212
			if (lang != null)
213
				locale = new Locale(lang);
214
		}
215
		final Collator collator = Collator.getInstance(locale);
216
		collator.setStrength(Collator.TERTIARY);
217
		typeNameColumn.addSelectionListener(new SelectionListener() {
218
			@Override
219
			public void widgetSelected(SelectionEvent e) {
220
				StatusLine.setMessage(RCPMessages.SORT_COLUMN);
221
				Comparator column_comparator = new Comparator() {
222
					@Override
223
					public int compare(Object o1, Object o2) {
224
						Object[] line1 = (Object[]) o1;
225
						Object[] line2 = (Object[]) o2;
226
						return collator.compare(line1[0], line2[0]);
227
					}
228
				};
229
				if (specificitesTable.getSortColumn() != typeNameColumn) {
230
					specificitesTable.setSortColumn(
231
							typeNameColumn);
232
					specificitesTable.setSortDirection(SWT.UP);
233
				} else if (specificitesTable
234
						.getSortDirection() == SWT.UP) {
235
					specificitesTable.setSortDirection(
236
							SWT.DOWN);
237
					column_comparator = new ReverseComparator(column_comparator);
238
				} else {
239
					specificitesTable.setSortDirection(SWT.UP);
240
				}
241
				currentComparator = column_comparator;
242
				sort();
243
				StatusLine.setMessage(""); //$NON-NLS-1$
244
			}
245

  
246
			@Override
247
			public void widgetDefaultSelected(SelectionEvent e) {
248
			}
249
		});
250

  
251
		// create 2nd column
252
		typeFrequencyColumn = new TableColumn(viewer
253
				.getTable(), SWT.NONE);
254
		typeFrequencyColumn.setText(RCPMessages.FrequencyListEditorInput_4
255
				+ " "+SpecificitiesUIMessages.SpecificitiesTableEditor_11+" "
256
				+ specificitiesResult.getCorpusSize()); 
257
		typeFrequencyColumn.setToolTipText(RCPMessages.FrequencyListEditorInput_4
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff