Skip to content

Commit

Permalink
html type
Browse files Browse the repository at this point in the history
  • Loading branch information
JPMoresmau committed Jan 17, 2014
1 parent 3903897 commit 837047d
Show file tree
Hide file tree
Showing 8 changed files with 299 additions and 44 deletions.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Expand Up @@ -859,6 +859,9 @@ public final class UITexts extends NLS {
public static String worksheet_removeexpression_title;
public static String worksheet_removeexpression_message;
public static String worksheet_removeexpression_tooltip;
public static String worksheet_expression_empty;
public static String worksheet_expression_type;


private static final String BUNDLE_NAME = UITexts.class.getPackage()
.getName() + ".uitexts"; //$NON-NLS-1$
Expand Down
Expand Up @@ -863,7 +863,10 @@ maxConfigureFailures_title=Maximum number of 'configure' failures (-1: no limit)
worksheet_addexpression=Add...
worksheet_addexpression_title=Add a worksheet expression
worksheet_addexpression_message=Expression:
worksheet_expression_empty=Expression cannot be empty
worksheet_editexpression_title=Edit a worksheet expression
worksheet_expression_type=Result type:

worksheet_removeexpression_title=Remove a worksheet expression
worksheet_removeexpression_tooltip=Remove the expression
worksheet_removeexpression_message=Are you sure to remove the expression \n{0}?
Expand Up @@ -10,16 +10,20 @@
import net.sf.eclipsefp.haskell.ui.internal.util.UITexts;
import net.sf.eclipsefp.haskell.ui.util.HaskellUIImages;
import net.sf.eclipsefp.haskell.ui.util.IImageNames;
import org.eclipse.jface.dialogs.InputDialog;
import net.sf.eclipsefp.haskell.util.LangUtil;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.window.Window;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.browser.ProgressEvent;
import org.eclipse.swt.browser.ProgressListener;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
Expand All @@ -41,7 +45,7 @@
*/
public class EvalComposite extends Composite implements EvalHandler{
private final EvalExpression expression;
private final Text lResult;
private Control lResult;
private final Label lResultIcon;
/**
* @param arg0
Expand Down Expand Up @@ -75,13 +79,8 @@ public EvalComposite(final WorkSheetViewPage page, final EvalExpression expressi
lResultIcon=new Label( this, SWT.NONE );
lResultIcon.setBackground( getBackground() );
lResultIcon.setLayoutData( new GridData(GridData.VERTICAL_ALIGN_BEGINNING) );
lResult=new Text(this,SWT.MULTI | SWT.WRAP);
lResult.setEditable( false );
lResult.setBackground( getBackground() );
lResult.setLayoutData( new GridData(GridData.FILL_HORIZONTAL) );
GridData gdResult=new GridData(GridData.FILL_HORIZONTAL);
gdResult.horizontalSpan=2;
lResult.setLayoutData( gdResult );
buildEmptyControl();


tiRemove.addSelectionListener( new SelectionAdapter() {
@Override
Expand All @@ -97,9 +96,10 @@ public void widgetSelected(final org.eclipse.swt.events.SelectionEvent arg0) {

@Override
public void mouseDoubleClick( final MouseEvent event ) {
InputDialog id=new InputDialog( getShell(), UITexts.worksheet_editexpression_title, UITexts.worksheet_addexpression_message, getExpression(), null );
EvalExpressionDialog id=new EvalExpressionDialog( getShell(), UITexts.worksheet_editexpression_title, UITexts.worksheet_addexpression_message, expression );
if (id.open()==Window.OK){
expression.setExpression( id.getValue() );
expression.setResultType( id.getResultType() );
lExpr.setText( getExpression() );
page.save();
page.eval();
Expand All @@ -111,12 +111,25 @@ public void mouseDoubleClick( final MouseEvent event ) {
lResult.addMouseListener( dbl );
lResultIcon.addMouseListener( dbl );
this.addMouseListener( dbl );

setBackground( getDisplay().getSystemColor( SWT.COLOR_LIST_BACKGROUND ) );
}

public EvalExpression getEvalExpression() {
return expression;
}

/* (non-Javadoc)
* @see org.eclipse.swt.widgets.Control#setBackground(org.eclipse.swt.graphics.Color)
*/
@Override
public void setBackground( final Color arg0 ) {
super.setBackground( arg0 );
for (Control c:getChildren()){
c.setBackground( arg0 );
}
}

/**
* @return the expression
*/
Expand Down Expand Up @@ -144,6 +157,9 @@ public void setToolTipText( final String arg0 ) {
@Override
public void handleResult( final EvalResult er ) {
expression.setLastResult( er );
if (this.isDisposed()){
return;
}
getDisplay().syncExec( new Runnable(){
/* (non-Javadoc)
* @see java.lang.Runnable#run()
Expand All @@ -152,16 +168,7 @@ public void handleResult( final EvalResult er ) {
public void run() {
setToolTipText("");

if (er.getResult()!=null && er.getResult().length()>0){
lResult.setText( er.getResult() );
lResultIcon.setImage(HaskellUIImages.getImage( IImageNames.WORKSHEET_OK ));
} else if (er.getError()!=null && er.getError().length()>0){
lResult.setText( er.getError() );
lResultIcon.setImage(PlatformUI.getWorkbench().getSharedImages().getImage( ISharedImages.IMG_OBJS_ERROR_TSK ) );
} else {
lResultIcon.setImage( null );
lResult.setText( "" );
}
buildControl( er );
if (er.getType()!=null && er.getType().length()>0){
setToolTipText( er.getType() );
}
Expand All @@ -173,4 +180,86 @@ public void run() {

}

private void buildControl(final EvalResult er){
if (er.getResult()!=null && er.getResult().length()>0){
buildResultControl(er.getResult());
} else if (er.getError()!=null && er.getError().length()>0){
buildErrorControl( er.getError() );
} else {
buildEmptyControl();
}
}

private void buildResultControl(final String re){
switch (expression.getResultType()){
case HTML:
buildHTML( re );
break;
default:
buildText( re );
}

lResultIcon.setImage(HaskellUIImages.getImage( IImageNames.WORKSHEET_OK ));
}

private void setCurrentControl(final Control c){
lResult=c;
GridData gdResult=new GridData(GridData.FILL_HORIZONTAL);
gdResult.horizontalSpan=2;
lResult.setLayoutData( gdResult );
lResult.setBackground( getBackground() );
}

private void buildHTML(final String html){
Browser b=null;
if (!(lResult instanceof Browser)){
if (lResult!=null){
lResult.dispose();
lResult=null;
}
b=new Browser(this,SWT.TOP | SWT.RESIZE);
b.addProgressListener(new ProgressListener() {
@Override
public void completed(final ProgressEvent event) {
layout(true);
getParent().layout(true);
}
public void changed(final ProgressEvent arg0) {};
});
} else{
b=(Browser)lResult;
}

b.setText(LangUtil.unquote(html) );
setCurrentControl(b);
}


private void buildText(final String txt){
Text t=null;
if (!(lResult instanceof Text)){
if (lResult!=null){
lResult.dispose();
lResult=null;
}
t=new Text(this,SWT.MULTI | SWT.WRAP);
t.setEditable( false );
} else {
t=(Text)lResult;
}
t.setText( txt );
setCurrentControl(t);
}

private void buildErrorControl(final String error){
buildText(error);
lResultIcon.setImage(PlatformUI.getWorkbench().getSharedImages().getImage( ISharedImages.IMG_OBJS_ERROR_TSK ) );

}

private void buildEmptyControl(){
lResultIcon.setImage( null );
buildText( "" );
}

}
Expand Up @@ -29,6 +29,9 @@ public class EvalExpression implements Comparable<EvalExpression>{
* the index in the list of evaluation for that file
*/
private int index;

private ResultType resultType=ResultType.TEXT;

/**
*
*/
Expand All @@ -39,7 +42,12 @@ public EvalExpression() {
public EvalExpression(final IMarker m){
expression=m.getAttribute( WorkSheetViewPage.MARKER_EXPRESSION, "" );
index=m.getAttribute( WorkSheetViewPage.MARKER_INDEX, 0 );
}
try {
resultType=ResultType.valueOf( m.getAttribute( WorkSheetViewPage.MARKER_RESULT_TYPE, ResultType.TEXT.toString() ) );
} catch (IllegalArgumentException ignore){
// noop
}
}

/**
* save the expression as a marker on the file
Expand All @@ -51,6 +59,7 @@ public IMarker addMarker(final IFile f) throws CoreException{
IMarker m=f.createMarker( WorkSheetViewPage.MARKER_TYPE );
m.setAttribute( WorkSheetViewPage.MARKER_EXPRESSION, expression );
m.setAttribute( WorkSheetViewPage.MARKER_INDEX, index );
m.setAttribute( WorkSheetViewPage.MARKER_RESULT_TYPE, String.valueOf(resultType ));
return m;
}

Expand Down Expand Up @@ -102,4 +111,14 @@ public void setIndex( final int index ) {
}


public ResultType getResultType() {
return resultType;
}


public void setResultType( final ResultType resultType ) {
this.resultType = resultType;
}


}
@@ -0,0 +1,99 @@
/**
* Copyright (c) 2014 by JP Moresmau
* This code is made available under the terms of the Eclipse Public License,
* version 1.0 (EPL). See http://www.eclipse.org/legal/epl-v10.html
*/
package net.sf.eclipsefp.haskell.ui.internal.views.worksheet;

import net.sf.eclipsefp.haskell.ui.internal.util.UITexts;
import org.eclipse.jface.dialogs.IInputValidator;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.model.WorkbenchViewerComparator;


/**
* Input dialog for expressions and their rendering type
* @author JP Moresmau
*
*/
public class EvalExpressionDialog extends InputDialog {
private ResultType resultType=ResultType.TEXT;

/**
* @param parentShell
* @param dialogTitle
* @param dialogMessage
* @param initialValue
* @param validator
*/
public EvalExpressionDialog( final Shell parentShell, final String dialogTitle,
final String dialogMessage, final EvalExpression initialValue) {
super( parentShell, dialogTitle, dialogMessage, initialValue.getExpression(), new EvalExpressionValidator() );
this.resultType=initialValue.getResultType();
}

/* (non-Javadoc)
* @see org.eclipse.jface.dialogs.InputDialog#createDialogArea(org.eclipse.swt.widgets.Composite)
*/
@Override
protected Control createDialogArea( final Composite parent ) {
Composite c=(Composite)super.createDialogArea( parent );

Composite typeC=new Composite(c,SWT.NONE);
typeC.setLayoutData( new GridData(GridData.FILL_HORIZONTAL) );
typeC.setLayout( new GridLayout(2,false) );
Label typeL=new Label(typeC,SWT.NONE);
typeL.setText( UITexts.worksheet_expression_type );

ComboViewer cv=new ComboViewer( typeC,SWT.READ_ONLY | SWT.SINGLE);
cv.setContentProvider( new ArrayContentProvider() );
cv.setLabelProvider( new LabelProvider() );
cv.setComparator( new WorkbenchViewerComparator() );

cv.setInput( ResultType.values() );
cv.setSelection( new StructuredSelection(resultType) );
cv.addSelectionChangedListener( new ISelectionChangedListener() {

@Override
public void selectionChanged( final SelectionChangedEvent event ) {
resultType=(ResultType)((IStructuredSelection)event.getSelection()).getFirstElement();
}
} );
return c;
}


/**
* @return the resultType
*/
public ResultType getResultType() {
return resultType;
}

private static class EvalExpressionValidator implements IInputValidator {
/* (non-Javadoc)
* @see org.eclipse.jface.dialogs.IInputValidator#isValid(java.lang.String)
*/
@Override
public String isValid( final String newText ) {
if (newText==null || newText.trim().length()==0){
return UITexts.worksheet_expression_empty;
}
return null;
}
}
}
@@ -0,0 +1,20 @@
/**
* Copyright (c) 2014 by JP Moresmau
* This code is made available under the terms of the Eclipse Public License,
* version 1.0 (EPL). See http://www.eclipse.org/legal/epl-v10.html
*/
package net.sf.eclipsefp.haskell.ui.internal.views.worksheet;


/**
* Type of result we know how to render in an EvalComposite
* @author JP Moresmau
*
*/
public enum ResultType {
TEXT,
HTML,
JSON,
// SVG,
// IMAGE
}

0 comments on commit 837047d

Please sign in to comment.