Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Merge branch 'release' into integration/master
Browse files Browse the repository at this point in the history
  • Loading branch information
seanf committed Oct 22, 2013
2 parents 31f93c6 + f1f4304 commit cfeca1f
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 19 deletions.
@@ -1,6 +1,5 @@
package org.zanata.webtrans.client.ui;

import com.allen_sauer.gwt.log.client.*;
import com.google.common.base.Strings;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JavaScriptObject;
Expand All @@ -19,7 +18,6 @@
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;

Expand Down Expand Up @@ -62,6 +60,7 @@ private native JavaScriptObject initCodeMirror(TextAreaElement element) /*-{
codeMirrorEditor.on("change", function() {
self.@org.zanata.webtrans.client.ui.CodeMirrorEditor::onChange()();
});
return codeMirrorEditor;
}-*/;
Expand Down
Expand Up @@ -22,6 +22,7 @@
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FocusPanel;
import com.google.gwt.user.client.ui.HTMLPanel;
Expand Down Expand Up @@ -59,6 +60,46 @@ public class Editor extends Composite implements ToggleEditor {
@UiField(provided = true)
TextAreaWrapper textArea;

// Timer period, in ms
private final int TYPING_TIMER_INTERVAL = 200;

// Validation will be forced after this many periods
private final int TYPING_TIMER_INTERVALS_UNTIL_VALIDATION = 5;

// Has a key been pressed since the timer was started or the last firing
private boolean keyPressedSinceTimer;

// Has a timer been started
private boolean timerStarted;

// The number of timer cycles since the last keydown
private int typingCycles;

// NB: In some cases, the idle detection may take almost 2 cycles
// 1. Key pressed at time = 0
// 2. Key pressed at time = 1ms (keyPressedSinceTimer = true)
// 3. Timer goes off at 200ms without validating
// 4. Timer goes off at 400ms and runs validation
// This could be fixed by using 2 separate timers
final Timer typingTimer = new Timer() {
@Override
public void run() {
if (keyPressedSinceTimer) {
// still typing, validate periodically
keyPressedSinceTimer = false;
typingCycles++;
if (typingCycles % TYPING_TIMER_INTERVALS_UNTIL_VALIDATION == 0) {
fireValidationEvent();
}
} else {
// finished, validate immediately
this.cancel();
timerStarted = false;
fireValidationEvent();
}
}
};

public Editor(String displayString, final int index,
final TargetContentsDisplay.Listener listener, final TransUnitId id) {
this.listener = listener;
Expand Down Expand Up @@ -104,9 +145,21 @@ private void fireValidationEvent() {
}
}

/**
* This gets triggered on each keydown event from both codemirror and plain
* text area.
*/
@UiHandler("textArea")
public void onValueChange(ValueChangeEvent<String> event) {
fireValidationEvent();
if (timerStarted) {
keyPressedSinceTimer = true;
} else {
// set false so that next keypress is detectable
keyPressedSinceTimer = false;
timerStarted = true;
typingCycles = 0;
typingTimer.scheduleRepeating(TYPING_TIMER_INTERVAL);
}
listener.setEditingState(id, UNSAVED);
}

Expand Down
Expand Up @@ -26,24 +26,12 @@
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.TextArea;

public class EditorTextArea extends TextArea implements TextAreaWrapper {
private static final int INITIAL_LINE_NUMBER = 2;
private static final int CHECK_INTERVAL = 50;
private String oldValue;
// this timer is used to fire validation and change editing state in a 50
// millisecond interval
// @see setEditing(boolean)
private final Timer typingTimer = new Timer() {
@Override
public void run() {
ValueChangeEvent.fireIfNotEqual(EditorTextArea.this, oldValue,
getText());
oldValue = getText();
}
};

private boolean editing;

public EditorTextArea(String displayString) {
Expand Down Expand Up @@ -93,10 +81,9 @@ public void refresh() {
public void setEditing(boolean isEditing) {
editing = isEditing;
if (isEditing) {
typingTimer.scheduleRepeating(CHECK_INTERVAL);
ValueChangeEvent.fireIfNotEqual(EditorTextArea.this, oldValue,
getText());
setFocus(true);
} else {
typingTimer.cancel();
}
}

Expand Down

0 comments on commit cfeca1f

Please sign in to comment.