33 |
33 |
import org.eclipse.core.commands.AbstractHandler;
|
34 |
34 |
import org.eclipse.core.commands.ExecutionEvent;
|
35 |
35 |
import org.eclipse.core.commands.ExecutionException;
|
|
36 |
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
|
36 |
37 |
import org.eclipse.jface.viewers.IStructuredSelection;
|
37 |
38 |
import org.eclipse.ui.IWorkbenchPart;
|
38 |
39 |
import org.eclipse.ui.handlers.HandlerUtil;
|
... | ... | |
42 |
43 |
import org.txm.concordance.core.functions.Line;
|
43 |
44 |
import org.txm.concordance.rcp.editors.ConcordanceEditor;
|
44 |
45 |
import org.txm.core.preferences.TXMPreferences;
|
|
46 |
import org.txm.objects.CorpusCommandPreferences;
|
45 |
47 |
import org.txm.searchengine.core.SearchEngine;
|
|
48 |
import org.txm.searchengine.cqp.AbstractCqiClient;
|
46 |
49 |
import org.txm.searchengine.cqp.CQPSearchEngine;
|
|
50 |
import org.txm.searchengine.cqp.clientExceptions.CqiClientException;
|
47 |
51 |
import org.txm.searchengine.cqp.corpus.CQPCorpus;
|
|
52 |
import org.txm.searchengine.cqp.corpus.CorpusManager;
|
48 |
53 |
import org.txm.searchengine.cqp.corpus.Property;
|
49 |
54 |
import org.txm.searchengine.cqp.corpus.StructuralUnit;
|
|
55 |
import org.txm.searchengine.cqp.corpus.StructuralUnitProperty;
|
50 |
56 |
import org.txm.searchengine.cqp.corpus.query.Match;
|
51 |
57 |
import org.txm.utils.logger.Log;
|
52 |
58 |
|
... | ... | |
92 |
98 |
Object s = selection.getFirstElement();
|
93 |
99 |
|
94 |
100 |
//System.out.println("Selected object: "+s);
|
95 |
|
if (s instanceof Line) {
|
|
101 |
if (!(s instanceof Line)) {
|
|
102 |
return null;
|
|
103 |
}
|
96 |
104 |
|
97 |
|
String structurePropertyName = BackToMediaPreferences.getInstance().getString(BackToMediaPreferences.STRUCTURE); //$NON-NLS-1$
|
98 |
|
String startPropertyName = BackToMediaPreferences.getInstance().getString(BackToMediaPreferences.STRUCTURE_START_PROPERTY); //$NON-NLS-1$
|
99 |
|
String endPropertyName = BackToMediaPreferences.getInstance().getString(BackToMediaPreferences.STRUCTURE_END_PROPERTY); //$NON-NLS-1$
|
|
105 |
Line line = (Line)s;
|
|
106 |
CQPCorpus corpus = line.getConcordance().getCorpus();
|
|
107 |
TXMPreferences alternative = BackToMediaPreferences.getInstance();
|
100 |
108 |
|
101 |
|
Line line = (Line)s;
|
102 |
|
CQPCorpus corpus = line.getConcordance().getCorpus();
|
|
109 |
// try reading project specific preferences
|
|
110 |
//TODO see with SJ how to implement the "command > project > preferences > default preferences" preference order
|
|
111 |
CorpusCommandPreferences commandPreferences = corpus.getProject().getCommandPreferences("backtomedia");
|
|
112 |
String structurePropertyName = commandPreferences.get(BackToMediaPreferences.STRUCTURE, alternative); //$NON-NLS-1$
|
|
113 |
String startPropertyName = commandPreferences.get(BackToMediaPreferences.STRUCTURE_START_PROPERTY, alternative); //$NON-NLS-1$
|
|
114 |
String endPropertyName = commandPreferences.get(BackToMediaPreferences.STRUCTURE_END_PROPERTY, alternative); //$NON-NLS-1$
|
|
115 |
String sync_mode = commandPreferences.get(BackToMediaPreferences.SYNCMODE, alternative);
|
|
116 |
String media_directory = commandPreferences.get(BackToMediaPreferences.MEDIA_DIRECTORY, alternative);
|
|
117 |
String media_format = commandPreferences.get(BackToMediaPreferences.MEDIA_FORMAT, alternative);
|
|
118 |
String media_index = commandPreferences.get(BackToMediaPreferences.MEDIA_INDEX_NODE, alternative);
|
103 |
119 |
|
104 |
|
try {
|
105 |
|
String sStartTime = "0"; //$NON-NLS-1$
|
106 |
|
String sEndTime = "0"; //$NON-NLS-1$
|
107 |
|
int startTime = 0;
|
108 |
|
int endTime = 0;
|
109 |
|
//boolean useWordProperty = Platform.getPreferencesService().getBoolean(Application.PLUGIN_ID, BackToMediaPreferences.USEWORDPROPERTY, false, null);
|
110 |
|
String sync_mode = BackToMediaPreferences.getInstance().getString(BackToMediaPreferences.SYNCMODE);
|
|
120 |
try {
|
|
121 |
String sStartTime = "0"; //$NON-NLS-1$
|
|
122 |
String sEndTime = "0"; //$NON-NLS-1$
|
|
123 |
int startTime = 0;
|
|
124 |
int endTime = 0;
|
|
125 |
//boolean useWordProperty = Platform.getPreferencesService().getBoolean(Application.PLUGIN_ID, BackToMediaPreferences.USEWORDPROPERTY, false, null);
|
111 |
126 |
|
112 |
|
StructuralUnit textS = corpus.getStructuralUnit("text"); //$NON-NLS-1$
|
113 |
|
Property textP = textS.getProperty("id"); //$NON-NLS-1$
|
114 |
|
String textid = line.getTextId();//line.getMatch().getValueForProperty(textP);// get text via text struc property id
|
115 |
|
|
116 |
|
Log.fine(MessagesMP.BackToMedia_7+textid);
|
117 |
127 |
|
118 |
|
if (BackToMediaPreferences.WORDMODE.equals(sync_mode)) {
|
119 |
|
String pTimeName = BackToMediaPreferences.getInstance().getString(BackToMediaPreferences.WORD_PROPERTY);
|
120 |
|
if ("".equals(pTimeName)) pTimeName = "time"; //$NON-NLS-1$ //$NON-NLS-2$
|
|
128 |
StructuralUnit textS = corpus.getStructuralUnit("text"); //$NON-NLS-1$
|
|
129 |
Property textP = textS.getProperty("id"); //$NON-NLS-1$
|
|
130 |
String textid = line.getTextId();//line.getMatch().getValueForProperty(textP);// get text via text struc property id
|
121 |
131 |
|
122 |
|
Property timeP = corpus.getProperty(pTimeName);
|
123 |
|
if (timeP == null) {
|
124 |
|
System.out.println(MessagesMP.BackToMedia_11+pTimeName);
|
125 |
|
return false;
|
126 |
|
}
|
127 |
|
int keywordPosition = line.getKeywordPosition();
|
128 |
|
int leftPosition = Math.max(keywordPosition - line.getConcordance().getLeftContextSize(), 0);
|
129 |
|
int rightPosition = Math.min(keywordPosition + line.getConcordance().getRightContextSize(), corpus.getSize());
|
130 |
|
int[] positions = {leftPosition, rightPosition};
|
131 |
|
String[] times = CQPSearchEngine.getCqiClient().cpos2Str(timeP.getQualifiedName(), positions);
|
|
132 |
Log.fine(MessagesMP.BackToMedia_7+textid);
|
|
133 |
|
|
134 |
if (BackToMediaPreferences.WORDMODE.equals(sync_mode)) {
|
|
135 |
String pTimeName = BackToMediaPreferences.getInstance().getString(BackToMediaPreferences.WORD_PROPERTY);
|
|
136 |
if ("".equals(pTimeName)) pTimeName = "time"; //$NON-NLS-1$ //$NON-NLS-2$
|
|
137 |
|
|
138 |
Property timeP = corpus.getProperty(pTimeName);
|
|
139 |
if (timeP == null) {
|
|
140 |
System.out.println(MessagesMP.BackToMedia_11+pTimeName);
|
|
141 |
return false;
|
|
142 |
}
|
|
143 |
int keywordPosition = line.getKeywordPosition();
|
|
144 |
int leftPosition = Math.max(keywordPosition - line.getConcordance().getLeftContextSize(), 0);
|
|
145 |
int rightPosition = Math.min(keywordPosition + line.getConcordance().getRightContextSize(), corpus.getSize());
|
|
146 |
int[] positions = {leftPosition, rightPosition};
|
|
147 |
String[] times = CQPSearchEngine.getCqiClient().cpos2Str(timeP.getQualifiedName(), positions);
|
|
148 |
sStartTime = times[0];
|
|
149 |
sEndTime = times[1];
|
|
150 |
} else if (BackToMediaPreferences.STRUCTUREMODE.equals(sync_mode)){
|
|
151 |
StructuralUnit structure = corpus.getStructuralUnit(structurePropertyName);
|
|
152 |
if (structure == null) {
|
|
153 |
System.out.println(MessagesMP.bind(MessagesMP.BackToMedia_12, structurePropertyName));
|
|
154 |
return null;
|
|
155 |
}
|
|
156 |
Property startProperty = structure.getProperty(startPropertyName);
|
|
157 |
if (startProperty == null) {
|
|
158 |
System.out.println(MessagesMP.bind(MessagesMP.BackToMedia_14, startPropertyName));
|
|
159 |
return null;
|
|
160 |
}
|
|
161 |
|
|
162 |
sStartTime = Match.getValueForProperty(startProperty, line.getMatch().getStart());
|
|
163 |
Log.fine(MessagesMP.BackToMedia_16+sStartTime);
|
|
164 |
|
|
165 |
Property endProperty = structure.getProperty(endPropertyName);
|
|
166 |
if (endProperty == null) {
|
|
167 |
System.out.println(MessagesMP.bind(MessagesMP.BackToMedia_17, endPropertyName));
|
|
168 |
return null;
|
|
169 |
}
|
|
170 |
sEndTime = Match.getValueForProperty(endProperty, line.getMatch().getEnd());
|
|
171 |
Log.fine(MessagesMP.BackToMedia_19+sEndTime);
|
|
172 |
} else { // MILESTONE MODE
|
|
173 |
StructuralUnit structure = corpus.getStructuralUnit(structurePropertyName);
|
|
174 |
if (structure == null) {
|
|
175 |
System.out.println(MessagesMP.bind(MessagesMP.BackToMedia_12, structurePropertyName));
|
|
176 |
return null;
|
|
177 |
}
|
|
178 |
StructuralUnitProperty startProperty = structure.getProperty(startPropertyName);
|
|
179 |
if (startProperty == null) {
|
|
180 |
System.out.println(MessagesMP.bind(MessagesMP.BackToMedia_14, startPropertyName));
|
|
181 |
return null;
|
|
182 |
}
|
|
183 |
|
|
184 |
AbstractCqiClient cqiClient = CorpusManager.getCorpusManager().getCqiClient();
|
|
185 |
try {
|
|
186 |
int[] struc = cqiClient.cpos2Struc(startProperty.getQualifiedName(),
|
|
187 |
new int[] {line.getMatch().getStart(), line.getMatch().getEnd()});
|
|
188 |
int[] struc2 = new int[] {struc[0], struc[1], struc[1]+1};
|
|
189 |
String[] times = cqiClient.struc2Str(startProperty.getQualifiedName(), struc2);
|
132 |
190 |
sStartTime = times[0];
|
133 |
|
sEndTime = times[1];
|
134 |
|
} else if (BackToMediaPreferences.STRUCTUREMODE.equals(sync_mode)){
|
135 |
|
StructuralUnit structure = corpus.getStructuralUnit(structurePropertyName);
|
136 |
|
if (structure == null) {
|
137 |
|
System.out.println(MessagesMP.bind(MessagesMP.BackToMedia_12, structurePropertyName));
|
138 |
|
return null;
|
|
191 |
if (times[2] != null) {
|
|
192 |
sEndTime = times[2];
|
|
193 |
} else {
|
|
194 |
sEndTime = times[1];
|
139 |
195 |
}
|
140 |
|
Property startProperty = structure.getProperty(startPropertyName);
|
141 |
|
if (startProperty == null) {
|
142 |
|
System.out.println(MessagesMP.bind(MessagesMP.BackToMedia_14, startPropertyName));
|
143 |
|
return null;
|
144 |
|
}
|
145 |
|
|
146 |
|
sStartTime = Match.getValueForProperty(startProperty, line.getMatch().getStart());
|
147 |
|
Log.fine(MessagesMP.BackToMedia_16+sStartTime);
|
148 |
196 |
|
149 |
|
Property endProperty = structure.getProperty(endPropertyName);
|
150 |
|
if (endProperty == null) {
|
151 |
|
System.out.println(MessagesMP.bind(MessagesMP.BackToMedia_17, endPropertyName));
|
152 |
|
return null;
|
153 |
|
}
|
154 |
|
sEndTime = Match.getValueForProperty(endProperty, line.getMatch().getEnd());
|
155 |
|
Log.fine(MessagesMP.BackToMedia_19+sEndTime);
|
156 |
|
} else {
|
157 |
|
StructuralUnit structure = corpus.getStructuralUnit(structurePropertyName);
|
158 |
|
if (structure == null) {
|
159 |
|
System.out.println(MessagesMP.bind(MessagesMP.BackToMedia_12, structurePropertyName));
|
160 |
|
return null;
|
161 |
|
}
|
162 |
|
Property startProperty = structure.getProperty(startPropertyName);
|
163 |
|
if (startProperty == null) {
|
164 |
|
System.out.println(MessagesMP.bind(MessagesMP.BackToMedia_14, startPropertyName));
|
165 |
|
return null;
|
166 |
|
}
|
167 |
|
|
168 |
|
sStartTime = Match.getValueForProperty(startProperty, line.getMatch().getStart());
|
169 |
|
|
170 |
|
int end = line.getMatch().getEnd()+1;
|
171 |
|
sEndTime = Match.getValueForProperty(startProperty, end); // should be after the next milestone
|
172 |
|
Log.fine(MessagesMP.BackToMedia_16+sStartTime);
|
|
197 |
} catch (Exception e) {
|
|
198 |
return null;
|
173 |
199 |
}
|
|
200 |
}
|
174 |
201 |
|
175 |
|
// try {
|
176 |
|
// startTime = (int)Float.parseFloat(sStartTime);
|
177 |
|
// endTime = (int)Float.parseFloat(sEndTime);
|
178 |
|
// } catch(Exception e2) {
|
179 |
|
// System.out.println("Error: time format must be an Integer, unit is second. Found "+sStartTime);
|
180 |
|
// return null;
|
181 |
|
// }
|
|
202 |
// try {
|
|
203 |
// startTime = (int)Float.parseFloat(sStartTime);
|
|
204 |
// endTime = (int)Float.parseFloat(sEndTime);
|
|
205 |
// } catch(Exception e2) {
|
|
206 |
// System.out.println("Error: time format must be an Integer, unit is second. Found "+sStartTime);
|
|
207 |
// return null;
|
|
208 |
// }
|
182 |
209 |
|
183 |
|
File binDir = corpus.getProject().getProjectDirectory();
|
|
210 |
File binDir = corpus.getProject().getProjectDirectory();
|
|
211 |
File audioFile = null;
|
|
212 |
if (media_index != null && media_index.length() > 0) {
|
|
213 |
IEclipsePreferences node = corpus.getProject().getPreferencesScope().getNode(media_index);
|
|
214 |
String path = node.get(textid, null);
|
|
215 |
audioFile = new File(path);
|
|
216 |
} else if (media_directory != null && media_directory.length() > 0 && media_format != null && media_format.length() > 0) {
|
|
217 |
audioFile = new File(media_directory, textid+"."+media_format); //$NON-NLS-1$
|
|
218 |
} else {
|
184 |
219 |
File mediaDir = new File(binDir, "media"); //$NON-NLS-1$
|
185 |
|
File audioFile = new File(mediaDir, textid+".mp3"); //$NON-NLS-1$
|
|
220 |
audioFile = new File(mediaDir, textid+".mp3"); //$NON-NLS-1$
|
186 |
221 |
if (!audioFile.exists()) audioFile = new File(mediaDir, textid+".ogg"); //$NON-NLS-1$
|
187 |
222 |
if (!audioFile.exists()) audioFile = new File(mediaDir, textid+".wav"); //$NON-NLS-1$
|
188 |
223 |
if (!audioFile.exists()) audioFile = new File(mediaDir, textid+".mp4"); //$NON-NLS-1$
|
189 |
224 |
if (!audioFile.exists()) audioFile = new File(mediaDir, textid+".avi"); //$NON-NLS-1$
|
190 |
225 |
if (!audioFile.exists()) audioFile = new File(mediaDir, textid+".mov"); //$NON-NLS-1$
|
191 |
|
|
192 |
|
Log.fine(MessagesMP.BackToMedia_25+audioFile);
|
193 |
|
if (audioFile.exists()) {
|
194 |
|
//System.out.println("Linked editors: "+associatedEditors);
|
195 |
|
VLCPlayerEditor editor = associatedEditors.get(ce);
|
196 |
|
if (editor != null && !editor.isDirty() && !editor.getPlayer().isDisposed()) {
|
197 |
|
editor.getPlayer().play(audioFile.getPath(), sStartTime, sEndTime);
|
198 |
|
editor.getPlayer().setRepeat(true);
|
199 |
|
} else {
|
200 |
|
//System.out.println("new editor linked to "+ce);
|
201 |
|
editor = OpenVLCPlayer.openEditor(audioFile.getPath(), sStartTime, sEndTime);
|
202 |
|
//editor.getPlayer().setRepeat(true);
|
203 |
|
if (editor == null) {
|
204 |
|
return null;
|
205 |
|
}
|
206 |
|
VLCPlayer player = editor.getPlayer();
|
207 |
|
if (player != null) {
|
208 |
|
player.hideStopButton();
|
209 |
|
associatedEditors.put(ce, editor);
|
210 |
|
}
|
211 |
|
}
|
212 |
|
if (audioFile != null)
|
213 |
|
editor.setPartName(audioFile.getName());
|
214 |
|
return null;
|
215 |
|
} else {
|
|
226 |
if (!audioFile.exists()) {
|
216 |
227 |
System.out.println(MessagesMP.BackToMedia_26+textid);
|
217 |
228 |
System.out.println(MessagesMP.BackToMedia_27+mediaDir+MessagesMP.BackToMedia_28);
|
218 |
229 |
System.out.println(MessagesMP.BackToMedia_29);
|
219 |
230 |
}
|
220 |
|
} catch (Exception e2) {
|
221 |
|
System.out.println(MessagesMP.BackToMedia_30+e2);
|
222 |
|
Log.printStackTrace(e2);
|
223 |
|
return null;
|
224 |
231 |
}
|
|
232 |
|
|
233 |
Log.fine(MessagesMP.BackToMedia_25+audioFile);
|
|
234 |
|
|
235 |
//System.out.println("Linked editors: "+associatedEditors);
|
|
236 |
VLCPlayerEditor editor = associatedEditors.get(ce);
|
|
237 |
String path = audioFile.getPath();
|
|
238 |
if (editor != null && !editor.isDirty() && !editor.getPlayer().isDisposed()) {
|
|
239 |
editor.getPlayer().play(path, sStartTime, sEndTime);
|
|
240 |
editor.getPlayer().setRepeat(true);
|
|
241 |
} else {
|
|
242 |
//System.out.println("new editor linked to "+ce);
|
|
243 |
editor = OpenVLCPlayer.openEditor(path, sStartTime, sEndTime);
|
|
244 |
//editor.getPlayer().setRepeat(true);
|
|
245 |
if (editor == null) {
|
|
246 |
return null;
|
|
247 |
}
|
|
248 |
VLCPlayer player = editor.getPlayer();
|
|
249 |
if (player != null) {
|
|
250 |
player.hideStopButton();
|
|
251 |
associatedEditors.put(ce, editor);
|
|
252 |
}
|
|
253 |
}
|
|
254 |
if (audioFile != null)
|
|
255 |
editor.setPartName(audioFile.getName());
|
|
256 |
return null;
|
|
257 |
|
|
258 |
} catch (Exception e2) {
|
|
259 |
System.out.println(MessagesMP.BackToMedia_30+e2);
|
|
260 |
Log.printStackTrace(e2);
|
|
261 |
return null;
|
225 |
262 |
}
|
226 |
|
return null;
|
227 |
263 |
}
|
228 |
264 |
}
|