Revision 2260

tmp/org.txm.backtomedia.rcp/src/org/txm/backtomedia/commands/function/BackToMedia.java (revision 2260)
39 39
import org.eclipse.jface.dialogs.InputDialog;
40 40
import org.eclipse.jface.viewers.IStructuredSelection;
41 41
import org.eclipse.osgi.util.NLS;
42
import org.eclipse.swt.widgets.Shell;
42 43
import org.eclipse.ui.IWorkbenchPart;
43 44
import org.eclipse.ui.handlers.HandlerUtil;
45
import org.eclipse.ui.part.EditorPart;
44 46
import org.txm.backtomedia.editors.vlcplayer.VLCPlayer;
45 47
import org.txm.backtomedia.editors.vlcplayer.VLCPlayerEditor;
46 48
import org.txm.backtomedia.preferences.BackToMediaPreferences;
47 49
import org.txm.concordance.core.functions.Line;
48 50
import org.txm.concordance.rcp.editors.ConcordanceEditor;
51
import org.txm.core.messages.TXMCoreMessages;
49 52
import org.txm.core.preferences.TXMPreferences;
50 53
import org.txm.objects.CorpusCommandPreferences;
54
import org.txm.rcp.editors.ITXMResultEditor;
51 55
import org.txm.rcp.swt.dialog.UsernamePasswordDialog;
52 56
import org.txm.rcp.utils.SWTEditorsUtils;
53 57
import org.txm.searchengine.core.SearchEngine;
54 58
import org.txm.searchengine.cqp.AbstractCqiClient;
55 59
import org.txm.searchengine.cqp.CQPSearchEngine;
60
import org.txm.searchengine.cqp.clientExceptions.CqiClientException;
56 61
import org.txm.searchengine.cqp.corpus.CQPCorpus;
57 62
import org.txm.searchengine.cqp.corpus.CorpusManager;
58 63
import org.txm.searchengine.cqp.corpus.Property;
64
import org.txm.searchengine.cqp.corpus.QueryResult;
59 65
import org.txm.searchengine.cqp.corpus.StructuralUnit;
60 66
import org.txm.searchengine.cqp.corpus.StructuralUnitProperty;
67
import org.txm.searchengine.cqp.corpus.query.CQLQuery;
61 68
import org.txm.searchengine.cqp.corpus.query.Match;
62 69
import org.txm.utils.logger.Log;
63 70

  
......
75 82
	/** The selection. */
76 83
	private IStructuredSelection selection;
77 84

  
78
	private static HashMap<ConcordanceEditor, VLCPlayerEditor> associatedEditors = new HashMap<ConcordanceEditor, VLCPlayerEditor>();
85
	private static HashMap<ITXMResultEditor<?>, VLCPlayerEditor> associatedEditors = new HashMap<ITXMResultEditor<?>, VLCPlayerEditor>();
79 86

  
80 87
	/* (non-Javadoc)
81 88
	 * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent)
82 89
	 */
83 90
	@Override
84 91
	public Object execute(ExecutionEvent event) throws ExecutionException {
85
		Object o = HandlerUtil.getActiveWorkbenchWindow(event).getActivePage().getSelection();
86
		if (!(o instanceof IStructuredSelection)) {
87
			Log.fine(MessagesMP.BackToMedia_0);
88
			return null;
89
		}
90 92

  
91
		IWorkbenchPart page = HandlerUtil.getActiveWorkbenchWindow(event).getActivePage().getActivePart();
93
		String corpus_parameter = event.getParameter("corpus"); //$NON-NLS-1$
94
		String text_parameter = event.getParameter("text"); //$NON-NLS-1$
95
		String word_parameter = event.getParameter("word"); //$NON-NLS-1$
96
		String time_parameter = event.getParameter("time"); //$NON-NLS-1$
92 97

  
93
		if (!(page instanceof ConcordanceEditor)) {
94
			Log.fine(MessagesMP.BackToMedia_1);
95
			System.out.println("Active page is not a ConcordanceEditor"); //$NON-NLS-1$
98
		if (corpus_parameter != null && text_parameter != null && (word_parameter != null || time_parameter != null)) {
99

  
100
			try {
101
				CQPCorpus cqpCorpus = CorpusManager.getCorpusManager().getCorpus(corpus_parameter);
102
				if (cqpCorpus == null) {
103
					Log.warning(NLS.bind("Corpus with ID={0} not found.", corpus_parameter));
104
					return null;
105
				}
106

  
107
				if (time_parameter != null) {
108
					return backToMedia(cqpCorpus, text_parameter, 0,0,0,0, time_parameter, time_parameter, null, HandlerUtil.getActiveWorkbenchWindow(event).getShell());
109
				} else {
110
					CQLQuery query = new CQLQuery(NLS.bind("[id=\"{0}\" & _.text_id=\"{1}\"]", word_parameter, text_parameter));
111
					QueryResult rez = cqpCorpus.query(query, "TMP", false);
112
					if(rez.getNMatch() == 1) {
113
						Match m = rez.getMatch(0);
114
						return backToMedia(cqpCorpus, text_parameter, m.getStart(), m.getEnd(), 10, 10, null, null, null, HandlerUtil.getActiveWorkbenchWindow(event).getShell());
115
					} else {
116
						Log.warning(NLS.bind("Fail to find word with id={0} in text with id={1}",word_parameter, text_parameter));
117
					}
118
				}
119
			} catch (Exception e) {
120
				Log.warning(TXMCoreMessages.bind("Fail open media for parameters corpus={0} text={1} word={2}: {3}",corpus_parameter, text_parameter, word_parameter, e));
121
				Log.printStackTrace(e);
122
			}
96 123
			return null;
97
		}
98
		ConcordanceEditor ce = (ConcordanceEditor) page;
99
		SearchEngine se = ce.getConcordance().getQuery().getSearchEngine();
100
		selection = (IStructuredSelection)o;
124
		} else {
125
			Object o = HandlerUtil.getActiveWorkbenchWindow(event).getActivePage().getSelection();
126
			if (!(o instanceof IStructuredSelection)) {
127
				Log.fine(MessagesMP.BackToMedia_0);
128
				return null;
129
			}
101 130

  
102
		Object s = selection.getFirstElement();
131
			IWorkbenchPart page = HandlerUtil.getActiveWorkbenchWindow(event).getActivePage().getActivePart();
103 132

  
104
		//System.out.println("Selected object: "+s);
105
		if (!(s instanceof Line)) {
106
			return null;
133
			if (!(page instanceof ConcordanceEditor)) {
134
				Log.fine(MessagesMP.BackToMedia_1);
135
				System.out.println("Active page is not a ConcordanceEditor"); //$NON-NLS-1$
136
				return null;
137
			}
138
			ConcordanceEditor ce = (ConcordanceEditor) page;
139
			selection = (IStructuredSelection)o;
140

  
141
			Object s = selection.getFirstElement();
142

  
143
			//System.out.println("Selected object: "+s);
144
			if (!(s instanceof Line)) {
145
				return null;
146
			}
147

  
148
			Line line = (Line)s;
149
			org.txm.objects.Match m = line.getMatch();
150
			String textid = line.getTextId();//line.getMatch().getValueForProperty(textP);// get text via text struc property id
151
			CQPCorpus corpus = line.getConcordance().getCorpus();
152

  
153
			return backToMedia(corpus, textid, m.getStart(), m.getEnd(), line.getConcordance().getLeftContextSize(), line.getConcordance().getRightContextSize(), null, null, ce, ce.getContainer().getShell());
107 154
		}
155
	}
108 156

  
109
		Line line = (Line)s;
110
		CQPCorpus corpus = line.getConcordance().getCorpus();
157
	public static Object backToMedia(CQPCorpus corpus, String textid, int keywordPosition, int keywordEndPosition, int leftContext, int rightContext, String sStartTime, String sEndTime, ITXMResultEditor<?> editor, Shell shell) {
158

  
111 159
		TXMPreferences alternative = BackToMediaPreferences.getInstance();
112 160

  
113 161
		// try reading project specific preferences
......
125 173
		String media_index = commandPreferences.get(BackToMediaPreferences.MEDIA_INDEX_NODE, alternative);
126 174

  
127 175
		try {
128
			String sStartTime = "0"; //$NON-NLS-1$
129
			String sEndTime = "0"; //$NON-NLS-1$
130
			int startTime = 0;
131
			int endTime = 0;
132
			//boolean useWordProperty = Platform.getPreferencesService().getBoolean(Application.PLUGIN_ID, BackToMediaPreferences.USEWORDPROPERTY, false, null);
133 176

  
177
			Log.fine(MessagesMP.BackToMedia_7+textid);
134 178

  
135
			StructuralUnit textS = corpus.getStructuralUnit("text"); //$NON-NLS-1$
136
			Property textP = textS.getProperty("id"); //$NON-NLS-1$
137
			String textid = line.getTextId();//line.getMatch().getValueForProperty(textP);// get text via text struc property id
179
			if (sStartTime == null || sEndTime == null) { // we need to find out the time to play
138 180

  
139
			Log.fine(MessagesMP.BackToMedia_7+textid);
181
				if (BackToMediaPreferences.WORDMODE.equals(sync_mode)) {
182
					String pTimeName = BackToMediaPreferences.getInstance().getString(BackToMediaPreferences.WORD_PROPERTY);
183
					if ("".equals(pTimeName)) pTimeName = "time"; //$NON-NLS-1$ //$NON-NLS-2$
140 184

  
141
			if (BackToMediaPreferences.WORDMODE.equals(sync_mode)) {
142
				String pTimeName = BackToMediaPreferences.getInstance().getString(BackToMediaPreferences.WORD_PROPERTY);
143
				if ("".equals(pTimeName)) pTimeName = "time"; //$NON-NLS-1$ //$NON-NLS-2$
185
					Property timeP = corpus.getProperty(pTimeName);
186
					if (timeP == null) {
187
						System.out.println(MessagesMP.BackToMedia_11+pTimeName);
188
						return false;
189
					}
144 190

  
145
				Property timeP = corpus.getProperty(pTimeName);
146
				if (timeP == null) {
147
					System.out.println(MessagesMP.BackToMedia_11+pTimeName);
148
					return false;
149
				}
150
				int keywordPosition = line.getKeywordPosition();
151
				int leftPosition = Math.max(keywordPosition - line.getConcordance().getLeftContextSize(), 0);
152
				int rightPosition = Math.min(keywordPosition + line.getConcordance().getRightContextSize(), corpus.getSize());
153
				int[] positions = {leftPosition, rightPosition};
154
				String[] times = CQPSearchEngine.getCqiClient().cpos2Str(timeP.getQualifiedName(), positions);
155
				sStartTime = times[0];
156
				sEndTime = times[1];
157
			} else if (BackToMediaPreferences.STRUCTUREMODE.equals(sync_mode)){
158
				StructuralUnit structure = corpus.getStructuralUnit(structurePropertyName);
159
				if (structure == null) {
160
					System.out.println(MessagesMP.bind(MessagesMP.BackToMedia_12, structurePropertyName));
161
					return null;
162
				}
163
				Property startProperty = structure.getProperty(startPropertyName);
164
				if (startProperty == null) {
165
					System.out.println(MessagesMP.bind(MessagesMP.BackToMedia_14, startPropertyName));
166
					return null;
167
				}
191
					int leftPosition = Math.max(keywordPosition - leftContext, 0);
192
					int rightPosition = Math.min(keywordPosition + rightContext, corpus.getSize());
193
					int[] positions = {leftPosition, rightPosition};
194
					String[] times = CQPSearchEngine.getCqiClient().cpos2Str(timeP.getQualifiedName(), positions);
195
					sStartTime = times[0];
196
					sEndTime = times[1];
197
				} else if (BackToMediaPreferences.STRUCTUREMODE.equals(sync_mode)){
198
					StructuralUnit structure = corpus.getStructuralUnit(structurePropertyName);
199
					if (structure == null) {
200
						System.out.println(MessagesMP.bind(MessagesMP.BackToMedia_12, structurePropertyName));
201
						return null;
202
					}
203
					Property startProperty = structure.getProperty(startPropertyName);
204
					if (startProperty == null) {
205
						System.out.println(MessagesMP.bind(MessagesMP.BackToMedia_14, startPropertyName));
206
						return null;
207
					}
168 208

  
169
				sStartTime = Match.getValueForProperty(startProperty, line.getMatch().getStart());
170
				Log.fine(MessagesMP.BackToMedia_16+sStartTime);
209
					sStartTime = Match.getValueForProperty(startProperty, keywordPosition);
210
					Log.fine(MessagesMP.BackToMedia_16+sStartTime);
171 211

  
172
				Property endProperty = structure.getProperty(endPropertyName);
173
				if (endProperty == null) {
174
					System.out.println(MessagesMP.bind(MessagesMP.BackToMedia_17, endPropertyName));
175
					return null;
176
				}
177
				sEndTime =  Match.getValueForProperty(endProperty, line.getMatch().getEnd());
178
				Log.fine(MessagesMP.BackToMedia_19+sEndTime);
179
			} else { // MILESTONE MODE
180
				StructuralUnit structure = corpus.getStructuralUnit(structurePropertyName);
181
				if (structure == null) {
182
					System.out.println(MessagesMP.bind(MessagesMP.BackToMedia_12, structurePropertyName));
183
					return null;
184
				}
185
				StructuralUnitProperty startProperty = structure.getProperty(startPropertyName);
186
				if (startProperty == null) {
187
					System.out.println(MessagesMP.bind(MessagesMP.BackToMedia_14, startPropertyName));
188
					return null;
189
				}
212
					Property endProperty = structure.getProperty(endPropertyName);
213
					if (endProperty == null) {
214
						System.out.println(MessagesMP.bind(MessagesMP.BackToMedia_17, endPropertyName));
215
						return null;
216
					}
217
					sEndTime =  Match.getValueForProperty(endProperty, keywordEndPosition);
218
					Log.fine(MessagesMP.BackToMedia_19+sEndTime);
219
				} else { // MILESTONE MODE
220
					StructuralUnit structure = corpus.getStructuralUnit(structurePropertyName);
221
					if (structure == null) {
222
						System.out.println(MessagesMP.bind(MessagesMP.BackToMedia_12, structurePropertyName));
223
						return null;
224
					}
225
					StructuralUnitProperty startProperty = structure.getProperty(startPropertyName);
226
					if (startProperty == null) {
227
						System.out.println(MessagesMP.bind(MessagesMP.BackToMedia_14, startPropertyName));
228
						return null;
229
					}
190 230

  
191
				AbstractCqiClient cqiClient = CorpusManager.getCorpusManager().getCqiClient();
192
				try {
193
					int[] struc = cqiClient.cpos2Struc(startProperty.getQualifiedName(),
194
							new int[] {line.getMatch().getStart(), line.getMatch().getEnd()});
195
					int[] struc2 = new int[] {struc[0], struc[1], struc[1]+1};
196
					String[] times = cqiClient.struc2Str(startProperty.getQualifiedName(), struc2);
197
					sStartTime = times[0];
198
					if (times[2] != null) {
199
						sEndTime = times[2];
200
					} else {
201
						sEndTime = times[1];
231
					AbstractCqiClient cqiClient = CorpusManager.getCorpusManager().getCqiClient();
232
					try {
233
						int[] struc = cqiClient.cpos2Struc(startProperty.getQualifiedName(),
234
								new int[] {keywordPosition, keywordEndPosition});
235
						int[] struc2 = new int[] {struc[0], struc[1], struc[1]+1};
236
						String[] times = cqiClient.struc2Str(startProperty.getQualifiedName(), struc2);
237
						sStartTime = times[0];
238
						if (times[2] != null) {
239
							sEndTime = times[2];
240
						} else {
241
							sEndTime = times[1];
242
						}
243

  
244
					} catch (Exception e) {
245
						return null;
202 246
					}
203

  
204
				} catch (Exception e) {
205
					return null;
206 247
				}
207 248
			}
208 249

  
209
			//try {
210
			//	startTime = (int)Float.parseFloat(sStartTime);
211
			//	endTime = (int)Float.parseFloat(sEndTime);
212
			//} catch(Exception e2) {
213
			//	System.out.println("Error: time format must be an Integer, unit is second. Found "+sStartTime);
214
			//	return null;
215
			//}
216

  
217 250
			File binDir = corpus.getProject().getProjectDirectory();
218 251
			String path = null;
219 252
			if (media_index != null && media_index.length() > 0) {
......
239 272
				if (media_auth) {
240 273

  
241 274
					if (secured_media_password == null) {
242
						UsernamePasswordDialog dialog = new UsernamePasswordDialog(ce.getShell(), new boolean[]{false, true}, media_directory);
275
						UsernamePasswordDialog dialog = new UsernamePasswordDialog(shell, new boolean[]{false, true}, media_directory);
243 276
						if (dialog.open() == dialog.OK) {
244 277
							System.setProperty(BackToMediaPreferences.MEDIA_AUTH_PASSWORD, dialog.getPassword());
245 278
							secured_media_password = dialog.getPassword();
......
292 325
			Log.fine(MessagesMP.BackToMedia_25+path);
293 326

  
294 327
			//System.out.println("Linked editors: "+associatedEditors);
295
			VLCPlayerEditor editor = associatedEditors.get(ce); 
296
			if (editor != null && !editor.isDirty() && !editor.getPlayer().isDisposed()) {
297
				editor.getPlayer().play(path, sStartTime, sEndTime);
298
				editor.getPlayer().setRepeat(true);
328
			VLCPlayerEditor vlcEditor = null;
329
			if (editor != null) {
330
				vlcEditor = associatedEditors.get(editor);
331
			}
332
			if (vlcEditor != null && !editor.isDirty() && !vlcEditor.getPlayer().isDisposed()) {
333
				vlcEditor.getPlayer().play(path, sStartTime, sEndTime);
334
				vlcEditor.getPlayer().setRepeat(true);
299 335
			} else {
300 336
				//System.out.println("new editor linked to "+ce);
301
				editor = OpenVLCPlayer.openEditor(path, sStartTime, sEndTime);
337
				vlcEditor = OpenVLCPlayer.openEditor(path, sStartTime, sEndTime);
302 338

  
303 339
				//editor.getPlayer().setRepeat(true);
304
				if (editor == null) {
340
				if (vlcEditor == null) {
305 341
					return null;
306 342
				}
307 343

  
308 344
				// move the editor in the window
309 345
				int position = BackToMediaPreferences.getInstance().getInt(BackToMediaPreferences.BACKTOMEDIA_POSITION);
310
				SWTEditorsUtils.addEditor(editor, ce, position);
346
				if (editor != null) {
347
					SWTEditorsUtils.addEditor(vlcEditor, (EditorPart) editor, position);
348
				}
311 349

  
312
				VLCPlayer player = editor.getPlayer();
350
				VLCPlayer player = vlcEditor.getPlayer();
313 351
				if (player != null) {
314 352
					player.hideStopButton();
315
					associatedEditors.put(ce, editor);
353
					associatedEditors.put(editor, vlcEditor);
316 354
				}
317 355
			}
318 356

  
319 357
			if (path != null) {
320
				editor.setPartName(textid);
358
				vlcEditor.setPartName(textid);
321 359
			}
322 360
			return null;
323 361

  
tmp/org.txm.backtomedia.rcp/plugin.xml (revision 2260)
59 59
            defaultHandler="org.txm.backtomedia.commands.function.BackToMedia"
60 60
            id="org.txm.backtomedia.commands.function.BackToMedia"
61 61
            name="%command.name.0">
62
         <commandParameter
63
               id="corpus"
64
               name="corpus"
65
               optional="true">
66
         </commandParameter>
67
         <commandParameter
68
               id="text"
69
               name="text"
70
               optional="true">
71
         </commandParameter>
72
         <commandParameter
73
               id="word"
74
               name="word"
75
               optional="true">
76
         </commandParameter>
77
         <commandParameter
78
               id="time"
79
               name="time"
80
               optional="true">
81
         </commandParameter>
62 82
      </command>
63 83
      <command
64 84
            defaultHandler="org.txm.backtomedia.commands.function.MediaPreferences"

Also available in: Unified diff