281 lines
8.4 KiB
Java
281 lines
8.4 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.resource.StatAlgoImporterResources;
|
|
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 org.gcube.portlets.user.statisticalalgorithmsimporter.shared.project.Project;
|
|
|
|
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.cell.core.client.ButtonCell.ButtonScale;
|
|
import com.sencha.gxt.cell.core.client.ButtonCell.IconAlign;
|
|
import com.sencha.gxt.core.client.util.Margins;
|
|
import com.sencha.gxt.widget.core.client.ContentPanel;
|
|
import com.sencha.gxt.widget.core.client.button.TextButton;
|
|
import com.sencha.gxt.widget.core.client.container.BoxLayoutContainer.BoxLayoutData;
|
|
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer;
|
|
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer.VerticalLayoutData;
|
|
import com.sencha.gxt.widget.core.client.event.SelectEvent;
|
|
import com.sencha.gxt.widget.core.client.event.SelectEvent.SelectHandler;
|
|
import com.sencha.gxt.widget.core.client.toolbar.ToolBar;
|
|
|
|
import edu.ycp.cs.dh.acegwt.client.ace.AceEditor;
|
|
import edu.ycp.cs.dh.acegwt.client.ace.AceEditorCursorPosition;
|
|
import edu.ycp.cs.dh.acegwt.client.ace.AceEditorMode;
|
|
import edu.ycp.cs.dh.acegwt.client.ace.AceEditorTheme;
|
|
import edu.ycp.cs.dh.acegwt.client.ace.AceSelection;
|
|
import edu.ycp.cs.dh.acegwt.client.ace.AceSelectionListener;
|
|
|
|
/**
|
|
*
|
|
* @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;
|
|
|
|
private TextButton btnSave;
|
|
private TextButton btnAddInput;
|
|
private TextButton btnAddOutput;
|
|
|
|
public CodeEditPanel(Project project, EventBus eventBus) {
|
|
super();
|
|
Log.debug("CodeEditPanel");
|
|
this.eventBus = eventBus;
|
|
|
|
// msgs = GWT.create(ServiceCategoryMessages.class);
|
|
init();
|
|
create(project);
|
|
|
|
}
|
|
|
|
private void init() {
|
|
forceLayoutOnResize = true;
|
|
setBodyBorder(false);
|
|
setBorders(false);
|
|
setHeaderVisible(false);
|
|
setResize(true);
|
|
|
|
}
|
|
|
|
private void create(Project project) {
|
|
btnSave = new TextButton("Save");
|
|
btnSave.setIcon(StatAlgoImporterResources.INSTANCE.add16());
|
|
btnSave.setScale(ButtonScale.SMALL);
|
|
btnSave.setIconAlign(IconAlign.LEFT);
|
|
btnSave.setToolTip("Save");
|
|
btnSave.addSelectHandler(new SelectHandler() {
|
|
|
|
@Override
|
|
public void onSelect(SelectEvent event) {
|
|
saveCode();
|
|
}
|
|
|
|
});
|
|
|
|
btnAddInput = new TextButton("Input");
|
|
btnAddInput.setIcon(StatAlgoImporterResources.INSTANCE.add16());
|
|
btnAddInput.setScale(ButtonScale.SMALL);
|
|
btnAddInput.setIconAlign(IconAlign.LEFT);
|
|
btnAddInput.setToolTip("Add input variable from code");
|
|
btnAddInput.addSelectHandler(new SelectHandler() {
|
|
|
|
@Override
|
|
public void onSelect(SelectEvent event) {
|
|
|
|
}
|
|
|
|
});
|
|
btnAddInput.disable();
|
|
|
|
btnAddOutput = new TextButton("Output");
|
|
btnAddOutput.setIcon(StatAlgoImporterResources.INSTANCE.add16());
|
|
btnAddOutput.setScale(ButtonScale.SMALL);
|
|
btnAddOutput.setIconAlign(IconAlign.LEFT);
|
|
btnAddOutput.setToolTip("Add output variable from code");
|
|
btnAddOutput.addSelectHandler(new SelectHandler() {
|
|
|
|
@Override
|
|
public void onSelect(SelectEvent event) {
|
|
|
|
}
|
|
|
|
});
|
|
btnAddOutput.disable();
|
|
|
|
ToolBar toolBar = new ToolBar();
|
|
toolBar.add(btnSave, new BoxLayoutData(new Margins(0)));
|
|
toolBar.add(btnAddInput, new BoxLayoutData(new Margins(0)));
|
|
toolBar.add(btnAddOutput, new BoxLayoutData(new Margins(0)));
|
|
|
|
editor = new AceEditor();
|
|
|
|
VerticalLayoutContainer v = new VerticalLayoutContainer();
|
|
v.add(toolBar, new VerticalLayoutData(1, -1, new Margins(0)));
|
|
v.add(editor, new VerticalLayoutData(1, 1, new Margins(0)));
|
|
|
|
add(v);
|
|
editor.startEditor();
|
|
editor.setShowPrintMargin(false);
|
|
|
|
if (project != null && project.getMainCode() != null) {
|
|
loadCode();
|
|
}
|
|
|
|
editor.getSelection().addSelectionListener(new AceSelectionListener() {
|
|
|
|
@Override
|
|
public void onChangeSelection(AceSelection selection) {
|
|
if (selection != null && !selection.isEmpty()) {
|
|
btnAddInput.enable();
|
|
btnAddOutput.enable();
|
|
} else {
|
|
btnAddInput.disable();
|
|
btnAddOutput.disable();
|
|
}
|
|
|
|
}
|
|
});
|
|
|
|
}
|
|
|
|
protected void saveCode() {
|
|
String code = editor.getText();
|
|
StatAlgoImporterServiceAsync.INSTANCE.saveCode(code,
|
|
new AsyncCallback<Void>() {
|
|
|
|
@Override
|
|
public void onFailure(Throwable caught) {
|
|
if (caught instanceof StatAlgoImporterSessionExpiredException) {
|
|
eventBus.fireEvent(new SessionExpiredEvent(
|
|
SessionExpiredType.EXPIREDONSERVER));
|
|
} else {
|
|
Log.error("Error on save code: "
|
|
+ caught.getLocalizedMessage());
|
|
UtilsGXT3.alert("Error",
|
|
caught.getLocalizedMessage());
|
|
}
|
|
caught.printStackTrace();
|
|
|
|
}
|
|
|
|
@Override
|
|
public void onSuccess(Void result) {
|
|
Log.debug("Code is saved!");
|
|
UtilsGXT3.info("Code Saved", "Code is saved!");
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
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(Project project) {
|
|
if (project != null && project.getMainCode() != null) {
|
|
loadCode();
|
|
}
|
|
|
|
}
|
|
|
|
protected String getSelectedText() {
|
|
String selectedText = null;
|
|
|
|
AceSelection editorSelection = editor.getSelection();
|
|
if (!editorSelection.isEmpty()) {
|
|
AceEditorCursorPosition startPosition;
|
|
AceEditorCursorPosition endPosition;
|
|
if (editorSelection.isBackwards()) {
|
|
startPosition = editorSelection.getSelectionLead();
|
|
endPosition = editorSelection.getSelectionAnchor();
|
|
editorSelection.select(startPosition.getRow(),
|
|
startPosition.getColumn(), endPosition.getRow(),
|
|
endPosition.getColumn());
|
|
} else {
|
|
startPosition = editorSelection.getSelectionAnchor();
|
|
endPosition = editorSelection.getSelectionLead();
|
|
|
|
}
|
|
|
|
if (editorSelection.isMultiLine()) {
|
|
String tempTest;
|
|
for (int i = startPosition.getRow(); i < endPosition.getRow() + 1; i++) {
|
|
if (i == startPosition.getRow()) {
|
|
tempTest = editor.getLine(i);
|
|
tempTest.substring(startPosition.getColumn());
|
|
selectedText = tempTest;
|
|
} else {
|
|
if (i == endPosition.getRow()) {
|
|
tempTest = editor.getLine(i);
|
|
tempTest.substring(0, endPosition.getColumn() + 1);
|
|
selectedText += tempTest;
|
|
} else {
|
|
selectedText += editor.getLine(i);
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
String tempTest;
|
|
tempTest = editor.getLine(startPosition.getRow());
|
|
selectedText = tempTest.substring(startPosition.getColumn(),
|
|
endPosition.getColumn() + 1);
|
|
}
|
|
}
|
|
|
|
Log.debug("Selected Text: " + selectedText);
|
|
return selectedText;
|
|
}
|
|
|
|
}
|