statistical-algorithms-impo.../src/main/java/org/gcube/portlets/user/statisticalalgorithmsimporter/client/maindata/CodeEditPanel.java

113 lines
3.3 KiB
Java

package org.gcube.portlets.user.statisticalalgorithmsimporter.client.maindata;
import java.util.ArrayList;
import org.gcube.portlets.user.statisticalalgorithmsimporter.client.event.SessionExpiredEvent;
import org.gcube.portlets.user.statisticalalgorithmsimporter.client.rpc.StatAlgoImporterServiceAsync;
import org.gcube.portlets.user.statisticalalgorithmsimporter.client.type.SessionExpiredType;
import org.gcube.portlets.user.statisticalalgorithmsimporter.client.utils.UtilsGXT3;
import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.code.CodeData;
import org.gcube.portlets.user.statisticalalgorithmsimporter.shared.exception.StatAlgoImporterSessionExpiredException;
import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.sencha.gxt.widget.core.client.ContentPanel;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer.VerticalLayoutData;
import edu.ycp.cs.dh.acegwt.client.ace.AceEditor;
import edu.ycp.cs.dh.acegwt.client.ace.AceEditorMode;
import edu.ycp.cs.dh.acegwt.client.ace.AceEditorTheme;
/**
*
* @author giancarlo email: <a
* href="mailto:g.panichi@isti.cnr.it">g.panichi@isti.cnr.it</a>
*
*/
public class CodeEditPanel extends ContentPanel {
private EventBus eventBus;
private AceEditor editor;
public CodeEditPanel(EventBus eventBus) {
super();
Log.debug("CodeEditPanel");
this.eventBus = eventBus;
// msgs = GWT.create(ServiceCategoryMessages.class);
init();
create();
}
private void init() {
forceLayoutOnResize = true;
setBodyBorder(false);
setBorders(false);
setHeaderVisible(false);
setResize(true);
}
private void create() {
editor = new AceEditor();
VerticalLayoutContainer v = new VerticalLayoutContainer();
v.add(editor, new VerticalLayoutData(1, 1));
add(v);
editor.startEditor();
editor.setShowPrintMargin(false);
}
private void loadCode() {
StatAlgoImporterServiceAsync.INSTANCE
.getCode(new AsyncCallback<ArrayList<CodeData>>() {
public void onFailure(Throwable caught) {
if (caught instanceof StatAlgoImporterSessionExpiredException) {
eventBus.fireEvent(new SessionExpiredEvent(
SessionExpiredType.EXPIREDONSERVER));
} else {
Log.error("Error retrieving code: "
+ caught.getLocalizedMessage());
UtilsGXT3.alert("Error",
caught.getLocalizedMessage());
}
caught.printStackTrace();
}
public void onSuccess(ArrayList<CodeData> result) {
Log.debug("loaded " + result.size() + " code lines");
if (result != null && result.size() > 0) {
String text = new String();
for (CodeData codeData : result) {
// Log.debug("Read: " + codeData);
text += codeData.getCodeLine() + "\r\n";
}
editor.setShowPrintMargin(false);
editor.setMode(AceEditorMode.R);
editor.setTheme(AceEditorTheme.ECLIPSE);
editor.setText(text);
} else {
editor.setShowPrintMargin(false);
editor.setMode(AceEditorMode.R);
editor.setTheme(AceEditorTheme.ECLIPSE);
editor.setText("");
}
forceLayout();
}
});
}
public void codeUpdate() {
loadCode();
}
}