📄 exporttradtranscript.java
字号:
File exportFile = promptForFile(ElanLocale.getString( "ExportTradTranscript.Title"), FileExtension.TEXT_EXT); if (exportFile == null) { return false; } // export.... return doExport(exportFile, selectedTiers, charsPerLine); } /** * Applies localized strings to the ui elements. For historic reasons the * string identifiers start with "TokenizeDialog" */ protected void updateLocale() { super.updateLocale(); setTitle(ElanLocale.getString("ExportTradTranscript.Title")); titleLabel.setText(ElanLocale.getString("ExportTradTranscript.Title")); rootTiersCB.setText(ElanLocale.getString( "ExportTradTranscript.Label.RootTiers")); optionsPanel.setBorder(new TitledBorder(ElanLocale.getString( "ExportDialog.Label.Options"))); wrapLinesCB.setText(ElanLocale.getString( "ExportTradTranscript.Label.WrapLines")); charPerLineLabel.setText(ElanLocale.getString( "ExportTradTranscript.Label.NumberChars")); timeCodeCB.setText(ElanLocale.getString( "ExportTradTranscript.Label.IncludeTimeCode")); labelsCB.setText(ElanLocale.getString( "ExportTradTranscript.Label.IncludeTierLabels")); selectionCB.setText(ElanLocale.getString("ExportDialog.Restrict")); } /** * Creates a label string of length <code>numchars</code>. A number of * space characters will be added to the input string to make it the * right length. * * @param name the input string * @param numchars the new length * * @return the input string with the right number of space characters added * to it */ private String getMarginString(String name, int numchars) { int nameLength = 0; if (name != null) { nameLength = name.length(); } StringBuffer bf = new StringBuffer(name); for (int i = 0; i < (numchars - nameLength); i++) { bf.append(SPACE); } return bf.toString(); } /** * Split a string into an array of substrings, each not longer than the * max annotation length. * * @param val the string to split * @param maxAnnotationLength the maximum length of the substrings * * @return an array of substrings */ private String[] breakValue(String val, int maxAnnotationLength) { if (val == null) { return new String[] { }; } if ((val.indexOf(SPACE) < 0) || (val.length() < maxAnnotationLength)) { return new String[] { val }; } ArrayList vals = new ArrayList(); String sub = null; while (val.length() > maxAnnotationLength) { sub = val.substring(0, maxAnnotationLength); int breakSpace = sub.lastIndexOf(SPACE_CHAR); if (breakSpace < 0) { breakSpace = val.indexOf(SPACE_CHAR); if (breakSpace < 0) { vals.add(val); break; } else { vals.add(val.substring(0, breakSpace + 1)); val = val.substring(breakSpace + 1); } } else { vals.add(sub.substring(0, breakSpace + 1)); val = val.substring(breakSpace + 1); } if (val.length() <= maxAnnotationLength) { vals.add(val); break; } } return (String[]) vals.toArray(new String[] { }); } //****************************** // actual export methods from here, for the time being //****************************** /** * The actual writing. If this class is to survive alot of the export stuff * should go to another class. * * @param fileName path to the file, not null * @param orderedTiers tier names, ordered by the user, min size 1 * @param charsPerLine num of chars per line if linewrap is selected * * @return true if all went well, false otherwise */ private boolean doExport(final File exportFile, final List orderedTiers, final int charsPerLine) { boolean selectionOnly = selectionCB.isSelected(); boolean wrapLines = wrapLinesCB.isSelected(); boolean includeTimeCodes = timeCodeCB.isSelected(); boolean includeLabels = labelsCB.isSelected(); int labelMargin = 0; Hashtable marginStrings = null; String tcLabel = "TC"; String emptyLabel = "empty"; int maxAnnotationLength = charsPerLine; if (includeLabels) { marginStrings = new Hashtable(); String name; for (int i = 0; i < orderedTiers.size(); i++) { name = (String) orderedTiers.get(i); if (name.length() > labelMargin) { labelMargin = name.length(); } } labelMargin += LABEL_VALUE_MARGIN; for (int i = 0; i < orderedTiers.size(); i++) { name = (String) orderedTiers.get(i); marginStrings.put(name, getMarginString(name, labelMargin)); } // add timecode label if (includeTimeCodes) { if (!marginStrings.containsKey(tcLabel)) { marginStrings.put(tcLabel, getMarginString(tcLabel, labelMargin)); } else { String tcl; for (int count = 1; count < 100; count++) { tcl = tcLabel + "-" + count; if (!marginStrings.containsKey(tcl)) { tcLabel = tcl; marginStrings.put(tcLabel, getMarginString(tcLabel, labelMargin)); break; } } } } // add empty string if (!marginStrings.containsKey(emptyLabel)) { marginStrings.put(emptyLabel, getMarginString("", labelMargin)); } else { String tcl; for (int count = 1; count < 100; count++) { tcl = emptyLabel + "-" + count; if (!marginStrings.containsKey(tcl)) { emptyLabel = tcl; marginStrings.put(emptyLabel, getMarginString("", labelMargin)); break; } } } } if (wrapLines && includeLabels) { maxAnnotationLength = charsPerLine - labelMargin; } long bb = 0; long eb = Long.MAX_VALUE; if (selectionOnly && (selection != null)) { bb = selection.getBeginTime(); eb = selection.getEndTime(); } // the parameters are set, create an ordered set of Annotation records TreeSet records = new TreeSet(); String tierName; TierImpl t; Annotation ann; for (int i = 0; i < orderedTiers.size(); i++) { tierName = (String) orderedTiers.get(i); t = (TierImpl) transcription.getTierWithId(tierName); if (t == null) { continue; } Vector v = t.getAnnotations(); for (int j = 0; j < v.size(); j++) { ann = (Annotation) v.get(j); if (TimeRelation.overlaps(ann, bb, eb)) { records.add(new IndexedExportRecord(ann, i)); } if (ann.getBeginTimeBoundary() > eb) { break; } } } // create output stream BufferedWriter writer = null; try { FileOutputStream out = new FileOutputStream(exportFile); OutputStreamWriter osw = null; try { osw = new OutputStreamWriter(out, encoding); } catch (UnsupportedCharsetException uce) { osw = new OutputStreamWriter(out, "UTF-8"); } writer = new BufferedWriter(osw); // do the writing Iterator recIter = records.iterator(); IndexedExportRecord record; String val; String[] valLines; while (recIter.hasNext()) { record = (IndexedExportRecord) recIter.next(); val = record.getValue().replace(NL_CHAR, SPACE_CHAR); if (includeLabels) { writer.write((String) marginStrings.get( record.getTierName())); } if (!wrapLines) { writer.write(val); } else { if (!(val.length() > maxAnnotationLength)) { writer.write(val); } else { valLines = breakValue(val, maxAnnotationLength); for (int i = 0; i < valLines.length; i++) { if (i != 0) { writer.write((String) marginStrings.get( emptyLabel)); } writer.write(valLines[i]); if (i != (valLines.length - 1)) { writer.write(NEW_LINE); } } } } writer.write(NEW_LINE); if (includeTimeCodes) { writer.write((String) marginStrings.get(tcLabel)); writer.write(TimeFormatter.toString(record.getBeginTime())); writer.write(TIME_SEP); writer.write(TimeFormatter.toString(record.getEndTime())); writer.write(NEW_LINE + NEW_LINE); } } writer.flush(); writer.close(); } catch (Exception ex) { // FileNotFound, IO, Security, Null etc JOptionPane.showMessageDialog(null, ElanLocale.getString("ExportDialog.Message.Error"), ElanLocale.getString("Message.Warning"), JOptionPane.WARNING_MESSAGE); return false; } finally { try { writer.close(); } catch (Exception ee) { } } return true; } /** * Moves selected tiers up in the list of tiers. */ private void moveDown() { if ((tierTable == null) || (model == null) || (model.getRowCount() < 2)) { return; } int[] selected = tierTable.getSelectedRows(); for (int i = selected.length - 1; i >= 0; i--) { int row = selected[i]; if ((row < (model.getRowCount() - 1)) && !tierTable.isRowSelected(row + 1)) { model.moveRow(row, row, row + 1); tierTable.changeSelection(row, 0, true, false); tierTable.changeSelection(row + 1, 0, true, false); } } } /** * Moves selected tiers up in the list of tiers. */ private void moveUp() { if ((tierTable == null) || (model == null) || (model.getRowCount() < 2)) { return; } int[] selected = tierTable.getSelectedRows(); for (int i = 0; i < selected.length; i++) { int row = selected[i]; if ((row > 0) && !tierTable.isRowSelected(row - 1)) { model.moveRow(row, row, row - 1); tierTable.changeSelection(row, 0, true, false); tierTable.changeSelection(row - 1, 0, true, false); } } } //*********************** // inner classes //*********************** /** * A class that extends AnnotationDataRecord with an index, that denotes * its position in the tier output order and that implements Comparable.<br> * Note: this class has a natural ordering that is inconsistent with * equals. * * @author Han Sloetjes */ private class IndexedExportRecord extends AnnotationDataRecord implements Comparable { private int index; /** * Constructor. * * @param annotation the annotation * @param index the index in the tier order */ IndexedExportRecord(Annotation annotation, int index) { super(annotation); this.index = index; } /** * Returns the index in the tier order. * * @return the index in the tier order */ public int getIndex() { return index; } /** * Performs a two step comparison: <br> * - compare the begintimes - when they are the same, compare the * index * * @param o the object to compare with * * @return a negative integer, zero, or a positive integer as this * object is less than, equal to, or greater than the * specified object * * @throws ClassCastException if the parameter is not an * IndexedExportRecord */ public int compareTo(Object o) throws ClassCastException { if (!(o instanceof IndexedExportRecord)) { throw new ClassCastException( "Object is not an IndexedExportRecord"); } IndexedExportRecord other = (IndexedExportRecord) o; if (this.getBeginTime() < other.getBeginTime()) { return -1; } else if (this.getBeginTime() > other.getBeginTime()) { return 1; } if (this.index < other.getIndex()) { return -1; } else if (this.index > other.getIndex()) { return 1; } return 0; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -