From: <al...@us...> - 2008-12-04 08:40:22
|
Revision: 1920 http://omegat.svn.sourceforge.net/omegat/?rev=1920&view=rev Author: alex73 Date: 2008-12-04 08:40:18 +0000 (Thu, 04 Dec 2008) Log Message: ----------- Change for RTL support Modified Paths: -------------- branches/prototypes/new-editor/src/org/omegat/gui/editor/EditorController.java branches/prototypes/new-editor/src/org/omegat/gui/editor/OmDocument.java branches/prototypes/new-editor/src/org/omegat/gui/editor/OmEditorKit.java branches/prototypes/new-editor/src/org/omegat/gui/editor/OmTextArea.java branches/prototypes/new-editor/src/org/omegat/gui/editor/SegmentElementsDescription.java branches/prototypes/new-editor/src/org/omegat/gui/editor/ViewParagraph.java branches/prototypes/new-editor/src/org/omegat/gui/editor/ViewSegmentMark.java Modified: branches/prototypes/new-editor/src/org/omegat/gui/editor/EditorController.java =================================================================== --- branches/prototypes/new-editor/src/org/omegat/gui/editor/EditorController.java 2008-12-02 22:31:43 UTC (rev 1919) +++ branches/prototypes/new-editor/src/org/omegat/gui/editor/EditorController.java 2008-12-04 08:40:18 UTC (rev 1920) @@ -55,6 +55,7 @@ import org.omegat.core.events.IEntryEventListener; import org.omegat.core.events.IFontChangedEventListener; import org.omegat.core.events.IProjectEventListener; +import org.omegat.gui.editor.OmDocument.ORIENTATION; import org.omegat.gui.main.DockableScrollPane; import org.omegat.gui.main.MainWindow; import org.omegat.util.Log; @@ -114,6 +115,10 @@ private enum SHOW_TYPE { INTRO, EMPTY_PROJECT, FIRST_ENTRY, NO_CHANGE }; + + private OmDocument.ORIENTATION currentOrientation; + protected boolean sourceLangIsRTL, targetLangIsRTL; + public EditorController(final MainWindow mainWindow) { this.mw = mainWindow; @@ -148,6 +153,7 @@ } else { showType = SHOW_TYPE.EMPTY_PROJECT; } + setInitialOrientation(); break; case CLOSE: history.clear(); @@ -248,6 +254,57 @@ } /** + * Decide what document orientation should be default for source/target + * languages. + */ + private void setInitialOrientation() { + String sourceLang = Core.getProject().getProjectProperties() + .getSourceLanguage().getLanguageCode(); + String targetLang = Core.getProject().getProjectProperties() + .getTargetLanguage().getLanguageCode(); + + sourceLangIsRTL = EditorUtils.isRTL(sourceLang); + targetLangIsRTL = EditorUtils.isRTL(targetLang); + + if (sourceLangIsRTL != targetLangIsRTL) { + currentOrientation = OmDocument.ORIENTATION.DIFFER; + } else { + if (sourceLangIsRTL) { + currentOrientation = ORIENTATION.RTL; + } else { + currentOrientation = ORIENTATION.LTR; + } + } + } + + /** + * Toggle component orientation: LTR, RTL, language dependent. + */ + protected void toggleOrientation() { + ORIENTATION newOrientation = currentOrientation; + switch (currentOrientation) { + case LTR: + newOrientation = ORIENTATION.RTL; + break; + case RTL: + if (sourceLangIsRTL != targetLangIsRTL) { + newOrientation = ORIENTATION.DIFFER; + } else { + newOrientation = ORIENTATION.LTR; + } + break; + case DIFFER: + newOrientation = ORIENTATION.LTR; + break; + } + LOGGER.info("Switch document orientation from " + currentOrientation + + " to " + newOrientation); + currentOrientation = newOrientation; + editor.getOmDocument().setOrientation(currentOrientation); + editor.repaint(); + } + + /** * {@inheritDoc} */ public void requestFocus() { @@ -297,10 +354,8 @@ segmentNumberOffset + i, false); } - String sourceLang=Core.getProject().getProjectProperties().getSourceLanguage().getLanguageCode(); - String targetLang=Core.getProject().getProjectProperties().getTargetLanguage().getLanguageCode(); try { - m_docSegList = doc.initialize(text, descriptions,sourceLang,targetLang); + m_docSegList = doc.initialize(text, descriptions,currentOrientation); } catch (BadLocationException ex) { LOGGER.log(Level.SEVERE, "Error initialize document", ex); } Modified: branches/prototypes/new-editor/src/org/omegat/gui/editor/OmDocument.java =================================================================== --- branches/prototypes/new-editor/src/org/omegat/gui/editor/OmDocument.java 2008-12-02 22:31:43 UTC (rev 1919) +++ branches/prototypes/new-editor/src/org/omegat/gui/editor/OmDocument.java 2008-12-04 08:40:18 UTC (rev 1920) @@ -28,7 +28,6 @@ import java.awt.Font; import java.awt.font.TextAttribute; import java.util.Enumeration; -import java.util.logging.Logger; import javax.swing.text.AbstractDocument; import javax.swing.text.AttributeSet; @@ -39,7 +38,6 @@ import javax.swing.text.Element; import javax.swing.text.MutableAttributeSet; import javax.swing.text.Position; -import javax.swing.text.SimpleAttributeSet; import javax.swing.text.Style; import javax.swing.text.StyleConstants; import javax.swing.text.StyleContext; @@ -65,9 +63,6 @@ * @author Alex Buloichik (ale...@gm...) */ public class OmDocument extends AbstractDocument implements StyledDocument { - /** Local logger. */ - private static final Logger LOGGER = Logger.getLogger(OmDocument.class - .getName()); enum ORIENTATION { /** Both segments is left aligned. */ @@ -84,7 +79,6 @@ /** Root element for full document. */ private OmElementMain root; - protected boolean sourceLangIsRTL, targetLangIsRTL; private ORIENTATION currentOrientation; /** @@ -119,22 +113,10 @@ * exception */ OmElementSegment[] initialize(StringBuilder text, - SegmentElementsDescription[] descriptions, String sourceLang, - String targetLang) throws BadLocationException { + SegmentElementsDescription[] descriptions, ORIENTATION orientation) + throws BadLocationException { - // Define orientation - sourceLangIsRTL = EditorUtils.isRTL(sourceLang); - targetLangIsRTL = EditorUtils.isRTL(targetLang); - if (sourceLangIsRTL != targetLangIsRTL) { - currentOrientation = ORIENTATION.DIFFER; - } else { - if (sourceLangIsRTL) { - currentOrientation = ORIENTATION.RTL; - } else { - currentOrientation = ORIENTATION.LTR; - } - } - + currentOrientation = orientation; try { writeLock(); getContent().insertString(0, text.toString()); @@ -343,7 +325,7 @@ protected void insertUpdate(DefaultDocumentEvent chng, AttributeSet attr) { UIThreadsUtil.mustBeSwingThread(); - super.insertUpdate(chng, attr);//TODO + super.insertUpdate(chng, attr);// TODO try { int segmentIndex = root.getElementIndex(chng.getOffset()); @@ -427,30 +409,10 @@ } /** - * Toggle component orientation: LTR, RTL, language dependent. + * Set new orientation. */ - void toggleOrientation() { - ORIENTATION newOrientation = currentOrientation; - switch (currentOrientation) { - case LTR: - newOrientation = ORIENTATION.RTL; - break; - case RTL: - if (sourceLangIsRTL != targetLangIsRTL) { - newOrientation = ORIENTATION.DIFFER; - } else { - newOrientation = ORIENTATION.LTR; - } - break; - case DIFFER: - newOrientation = ORIENTATION.LTR; - break; - } - LOGGER.info("Switch document orientation from " + currentOrientation - + " to " + newOrientation); + void setOrientation(ORIENTATION newOrientation) { currentOrientation = newOrientation; - - controller.editor.repaint(); // View mainDocView = controller.editor.getUI().getRootView( // controller.editor).getView(0); // mainDocView.removeAll(); @@ -642,7 +604,7 @@ public String getName() { return "paragraph"; } - + public boolean isLangRTL() { return langRTL; } @@ -650,10 +612,11 @@ public void setLangRTL(boolean langRTL) { this.langRTL = langRTL; MutableAttributeSet attrs = (MutableAttributeSet) getAttributes(); - attrs.addAttribute(TextAttribute.RUN_DIRECTION, new Boolean(langRTL)); + attrs.addAttribute(TextAttribute.RUN_DIRECTION, + new Boolean(langRTL)); } - public boolean isRTLAligned() { + public boolean isRightAligned() { switch (currentOrientation) { case LTR: return false; Modified: branches/prototypes/new-editor/src/org/omegat/gui/editor/OmEditorKit.java =================================================================== --- branches/prototypes/new-editor/src/org/omegat/gui/editor/OmEditorKit.java 2008-12-02 22:31:43 UTC (rev 1919) +++ branches/prototypes/new-editor/src/org/omegat/gui/editor/OmEditorKit.java 2008-12-04 08:40:18 UTC (rev 1920) @@ -66,7 +66,7 @@ return new BoxView(elem, View.Y_AXIS); } else if (kind.equals("paragraph")) { OmDocument.OmElementParagraph par = (OmDocument.OmElementParagraph) elem; - return new ViewParagraph(par, par.isRTLAligned()); + return new ViewParagraph(par, par.isLangRTL(), par.isRightAligned()); } else if (kind.equals("segment")) { return new BoxView(elem, View.Y_AXIS); } else if (kind.equals("segmentmark")) { Modified: branches/prototypes/new-editor/src/org/omegat/gui/editor/OmTextArea.java =================================================================== --- branches/prototypes/new-editor/src/org/omegat/gui/editor/OmTextArea.java 2008-12-02 22:31:43 UTC (rev 1919) +++ branches/prototypes/new-editor/src/org/omegat/gui/editor/OmTextArea.java 2008-12-04 08:40:18 UTC (rev 1920) @@ -157,7 +157,7 @@ } else if (isKey(e, KeyEvent.VK_O, KeyEvent.CTRL_MASK | KeyEvent.SHIFT_MASK)) { // handle Ctrl+Shift+O - toggle orientation LTR-RTL - getOmDocument().toggleOrientation(); + controller.toggleOrientation(); } // leave standard processing if need Modified: branches/prototypes/new-editor/src/org/omegat/gui/editor/SegmentElementsDescription.java =================================================================== --- branches/prototypes/new-editor/src/org/omegat/gui/editor/SegmentElementsDescription.java 2008-12-02 22:31:43 UTC (rev 1919) +++ branches/prototypes/new-editor/src/org/omegat/gui/editor/SegmentElementsDescription.java 2008-12-04 08:40:18 UTC (rev 1920) @@ -24,7 +24,6 @@ package org.omegat.gui.editor; -import java.awt.font.TextAttribute; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.List; @@ -230,7 +229,8 @@ OmElementSegment segElement, String text, int offsetFromDocumentBegin) { int sourceParagraphsCount; - paragraphElements.add(doc.new OmElementParagraph(segElement, new SimpleAttributeSet())); + paragraphElements.add(doc.new OmElementParagraph(segElement, + new SimpleAttributeSet())); // add sources addLines(segElement, ATTR_SOURCE, text.substring(0, @@ -239,7 +239,7 @@ sourceParagraphsCount = paragraphElements.size() - 1; - // add <segment 0000> + // add begin segment mark addSegmentMark(segElement, ATTR_SEGMENT_MARK, text.substring( translationBeginTagStart, translationBeginTagEnd), offsetFromDocumentBegin + translationBeginTagStart, false, @@ -249,7 +249,7 @@ translationBeginTagEnd, translationEndTagStart), offsetFromDocumentBegin + translationBeginTagEnd, needToCheckSpelling, OmContent.POSITION_TYPE.INSIDE_EDITABLE); - // add <end segment> + // add end segment mark addSegmentMark(segElement, ATTR_SEGMENT_MARK, text.substring( translationEndTagStart, translationEndTagEnd), offsetFromDocumentBegin + translationEndTagStart, false, @@ -263,10 +263,10 @@ paragraphElements.remove(paragraphElements.size() - 1); for (int i = 0; i < sourceParagraphsCount; i++) { - paragraphElements.get(i).setLangRTL(doc.sourceLangIsRTL); + paragraphElements.get(i).setLangRTL(doc.controller.sourceLangIsRTL); } for (int i = sourceParagraphsCount; i < paragraphElements.size(); i++) { - paragraphElements.get(i).setLangRTL(doc.targetLangIsRTL); + paragraphElements.get(i).setLangRTL(doc.controller.targetLangIsRTL); } return paragraphElements.toArray(new Element[paragraphElements.size()]); @@ -314,7 +314,8 @@ textElements.toArray(new Element[textElements.size()])); textElements.clear(); - paragraphElements.add(doc.new OmElementParagraph(section, new SimpleAttributeSet())); + paragraphElements.add(doc.new OmElementParagraph(section, + new SimpleAttributeSet())); prevPos = pos + 1; } addLine(attrs, partText.substring(prevPos), offsetFromDocumentBegin Modified: branches/prototypes/new-editor/src/org/omegat/gui/editor/ViewParagraph.java =================================================================== --- branches/prototypes/new-editor/src/org/omegat/gui/editor/ViewParagraph.java 2008-12-02 22:31:43 UTC (rev 1919) +++ branches/prototypes/new-editor/src/org/omegat/gui/editor/ViewParagraph.java 2008-12-04 08:40:18 UTC (rev 1920) @@ -1,8 +1,10 @@ package org.omegat.gui.editor; import javax.swing.text.Element; +import javax.swing.text.FlowView; import javax.swing.text.ParagraphView; import javax.swing.text.StyleConstants; +import javax.swing.text.View; /** * Class for use some protected properties. @@ -10,10 +12,66 @@ * @author Alex Buloichik (ale...@gm...) */ public class ViewParagraph extends ParagraphView { - public ViewParagraph(final Element elem, final boolean isRTL) { + public ViewParagraph(final Element elem, final boolean isRTL, final boolean isRightAligned) { super(elem); - System.out.println("par rtl="+isRTL); - setJustification(isRTL ? StyleConstants.ALIGN_RIGHT + strategy = new LayoutStrategy(isRTL); + setJustification(isRightAligned ? StyleConstants.ALIGN_RIGHT : StyleConstants.ALIGN_LEFT); } + + @Override + protected View createRow() { + // TODO Auto-generated method stub + return super.createRow(); + } + + public static class LayoutStrategy extends FlowView.FlowStrategy { + protected boolean isRTL; + + public LayoutStrategy(final boolean isRTL) { + this.isRTL = isRTL; + } + + @Override + protected int layoutRow(FlowView fv, int rowIndex, int pos) { + int res = super.layoutRow(fv, rowIndex, pos); + + if (isRTL) { + // Need to swap "begin" and "end" mark view, because paragraph + // is RTL + View row = fv.getView(rowIndex); + int n = row.getViewCount(); + if (n > 1) { + // only when more than one view, because nothing to do if + // there + // is no 2 view + if (row.getView(0) instanceof ViewSegmentMark) { + // the first view should always be mark + int secondIndex = -1; + View[] replace = new View[n]; + for (int i = 0; i < n; i++) { + replace[i] = row.getView(i); + if (replace[i] instanceof ViewSegmentMark) { + /* + * index of latest segment view, length-2 in + * most cases + */ + secondIndex = i; + } + } + if (secondIndex >= 0) { + // swap it + System.out.println("replace"); + View firstView = replace[0]; + replace[0] = replace[secondIndex]; + replace[secondIndex] = firstView; + row.replace(0, n, replace); + } + } + } + } + + return res; + } + } } Modified: branches/prototypes/new-editor/src/org/omegat/gui/editor/ViewSegmentMark.java =================================================================== --- branches/prototypes/new-editor/src/org/omegat/gui/editor/ViewSegmentMark.java 2008-12-02 22:31:43 UTC (rev 1919) +++ branches/prototypes/new-editor/src/org/omegat/gui/editor/ViewSegmentMark.java 2008-12-04 08:40:18 UTC (rev 1920) @@ -41,7 +41,7 @@ /** * Class for display segmentation marks. It better to paint marks by own - * component, because we will not have problems with RTL writing. + * component, because we will not have problems with RTL writing in this case. * * @author Alex Buloichik (ale...@gm...) */ @@ -62,7 +62,7 @@ updateDrawSettings(); switch (axis) { case View.X_AXIS: - return super.getAlignment(axis); + return 0;//super.getAlignment(axis); case View.Y_AXIS: float h = fontMetrics.getHeight(); float d = fontMetrics.getDescent(); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |