package es.iiia.sgi;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.Platform;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IMemento;
import org.eclipse.ui.IPropertyListener;
import org.eclipse.ui.IWorkbenchPreferenceConstants;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.internal.util.PrefUtil;
import org.eclipse.ui.intro.IIntroPart;
import org.eclipse.ui.intro.IIntroSite;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import es.iiia.sgi.preferences.PreferenceConstants;
import es.iiia.sgi.tools.SimpleMail;
import es.iiia.shapegrammar.utils.SimpleXmlParser;
public class IntroductionScreen implements IIntroPart, SelectionListener,
ModifyListener {
private Composite composite;
private ArrayList<String[]> answers;
@Override
public Object getAdapter(Class adapter) {
return null;
}
@Override
public IIntroSite getIntroSite() {
return null;
}
@Override
public void init(IIntroSite site, IMemento memento)
throws PartInitException {
}
@Override
public void standbyStateChanged(boolean standby) {
}
@Override
public void saveState(IMemento memento) {
}
@Override
public void addPropertyListener(IPropertyListener listener) {
}
@Override
public void createPartControl(Composite parent) {
parent.setLayout(new FillLayout());
GridLayout layout = new GridLayout();
layout.numColumns = 2;
GridData lgd = new GridData(GridData.FILL, GridData.FILL, true, false);
lgd.heightHint = 30;
lgd.minimumWidth = 150;
GridData cgd = new GridData(GridData.CENTER, GridData.CENTER, true, false);
cgd.horizontalSpan = 2;
GridData tgd = new GridData(GridData.BEGINNING, GridData.BEGINNING, false, false);
tgd.minimumWidth = 200;
tgd.widthHint = 200;
ScrolledComposite sc = new ScrolledComposite(parent,
SWT.V_SCROLL | SWT.BORDER);
composite = new Composite(sc, SWT.NONE);
composite.setLayout(layout);
sc.setMinHeight(1100);
sc.setContent(composite);
sc.setExpandVertical(true);
sc.setExpandHorizontal(true);
// init title
Label mainLabel = new Label(composite, SWT.NONE);
mainLabel.setText("Welcome to Shape Grammar Interpreter");
mainLabel.setFont(new Font(parent.getDisplay(), "Arial", 18, SWT.BOLD));
GridData tgridData = new GridData();
tgridData.horizontalSpan = 2;
tgridData.horizontalAlignment = GridData.CENTER;
mainLabel.setLayoutData(tgridData);
mainLabel = new Label(composite, SWT.NONE);
mainLabel.setText("Please help us to improve our tool by sending us the evaluation");
mainLabel.setFont(new Font(parent.getDisplay(), "Arial", 13, SWT.NORMAL));
tgridData = new GridData();
tgridData.horizontalSpan = 2;
tgridData.horizontalAlignment = GridData.CENTER;
tgridData.verticalAlignment = GridData.CENTER;
//tgridData.grabExcessHorizontalSpace = true;
tgridData.minimumHeight = 30;
tgridData.heightHint = 30;
mainLabel.setLayoutData(tgridData);
// init evaluation form from Xml
answers = new ArrayList<String[]>();
SimpleXmlParser parser = new SimpleXmlParser();
Element ele = parser.getDocument(getFormDefinition())
.getDocumentElement();
NodeList nl = ele.getElementsByTagName("field");
if (nl != null && nl.getLength() > 0) {
for (int i = 0; i < nl.getLength(); i++) {
// get the shape element
Element el = (Element) nl.item(i);
// init answer field
String[] answer = new String[2];
answer[0] = el.getAttribute("name");
answers.add(answer);
// add field
String type = el.getAttribute("type");
if (type.equals("label")) {
mainLabel = new Label(composite, SWT.NONE);
mainLabel.setText(el.getAttribute("name"));
mainLabel.setFont(new Font(parent.getDisplay(), "Arial", 11, SWT.BOLD));
tgridData = new GridData();
tgridData.horizontalSpan = 2;
tgridData.horizontalAlignment = GridData.CENTER;
mainLabel.setLayoutData(tgridData);
continue;
}
// add label
Label nameLabel = new Label(composite, SWT.WRAP | SWT.BORDER | SWT.RIGHT);
nameLabel.setText(el.getAttribute("name"));
nameLabel.setLayoutData(lgd);
// boolean field
if (type.equals("bool")) {
Composite comp = new Composite(composite, SWT.NONE);
comp.setLayout(new RowLayout());
Button yesButton = new Button(comp, SWT.RADIO);
yesButton.setText("Yes");
yesButton.setData("ID_" + i + "_True");
yesButton.addSelectionListener(this);
Button noButton = new Button(comp, SWT.RADIO);
noButton.setText("No");
noButton.setData("ID_" + i + "_False");
noButton.addSelectionListener(this);
comp.setLayoutData(tgd);
}
// text field
else if (type.equals("text")) {
Text nameText = new Text(composite, SWT.BORDER);
nameText.setData("ID_" + i);
nameText.addModifyListener(this);
GridData gridData = new GridData();
gridData.horizontalAlignment = SWT.FILL;
gridData.grabExcessHorizontalSpace = true;
nameText.setLayoutData(gridData);
// nameText.setText("");
}
// multi text field
else if (type.equals("multiline")) {
Text nameText = new Text(composite, SWT.BORDER | SWT.WRAP
| SWT.MULTI);
nameText.setData("ID_" + i);
nameText.addModifyListener(this);
GridData gridData = new GridData();
gridData.horizontalAlignment = SWT.FILL;
gridData.grabExcessHorizontalSpace = true;
gridData.verticalAlignment = SWT.FILL;
gridData.grabExcessVerticalSpace = true;
gridData.minimumHeight = 60;
nameText.setLayoutData(gridData);
// addressText.setText("This text field and the List\nbelow share any excess space.");
}
// 5 option field
else if (type.equals("5options")) {
Composite comp = new Composite(composite, SWT.NONE);
comp.setLayout(new RowLayout());
for (int j = 0; j < 5; j++) {
Button optionButton = new Button(comp, SWT.RADIO);
optionButton.setText(Integer.toString(j + 1));
optionButton.setData("ID_" + i + "_" + j);
optionButton.addSelectionListener(this);
}
comp.setLayoutData(tgd);
}
}
}
// add send button
final Button sendButton = new Button(composite, SWT.PUSH);
sendButton.setText("Send Evaluation");
sendButton.addSelectionListener(new SelectionListener() {
@Override
public void widgetSelected(SelectionEvent e) {
try {
sendButton.setText("Sending ...");
SimpleMail.sendMail(getMail());
sendButton.setText("Sent!");
sendButton.setEnabled(false);
PrefUtil.getAPIPreferenceStore().setValue(PreferenceConstants.EVALUATION_SENT, true);
} catch (Exception e1) {
e1.printStackTrace();
}
}
@Override
public void widgetDefaultSelected(SelectionEvent e) {
// TODO Auto-generated method stub
}
});
GridData sgridData = new GridData();
sendButton.setLayoutData(cgd);
}
@Override
public void dispose() {
// TODO Auto-generated method stub
}
@Override
public Image getTitleImage() {
URL base = Platform.getBundle("es.iiia.sgi").getEntry(
"icons/program_16.png");
try {
return new Image(Display.getCurrent(), FileLocator.toFileURL(base)
.toString().replace("file:", ""));
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
public String getTitle() {
return "Welcome";
}
@Override
public void removePropertyListener(IPropertyListener listener) {
}
@Override
public void setFocus() {
}
protected String getFormDefinition() {
URL base = Platform.getBundle("es.iiia.sgi").getEntry(
"forms/evaluation.xml");
try {
return FileLocator.toFileURL(base).toString();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
public void widgetSelected(SelectionEvent e) {
String[] idxs = e.widget.getData().toString().split("_");
answers.get(Integer.parseInt(idxs[1]))[1] = idxs[2];
//System.out.println(e.widget.getData() + " Selected ...");
}
@Override
public void widgetDefaultSelected(SelectionEvent e) {
}
@Override
public void modifyText(ModifyEvent e) {
String[] idxs = e.widget.getData().toString().split("_");
answers.get(Integer.parseInt(idxs[1]))[1] = ((Text) e.widget).getText();
//System.out.println(e.widget.getData() + " Changed ...");
}
// private method
private String getMail() {
StringBuffer mailText = new StringBuffer();
try {
URL whatismyip = new URL("http://automation.whatismyip.com/n09230945.asp");
BufferedReader in = new BufferedReader(new InputStreamReader(
whatismyip.openStream()));
String ip = in.readLine(); //you get the IP as a String
mailText.append("User: " + ip + "\r\n");
} catch (Exception e) {
System.out.println("Exception caught =" + e.getMessage());
}
String nameOS = "os.name";
String versionOS = "os.version";
String architectureOS = "os.arch";
mailText.append("\nThe information about OS");
mailText.append("\nName of the OS: " + System.getProperty(nameOS));
mailText.append("\nVersion of the OS: " + System.getProperty(versionOS));
mailText.append("\nArchitecture of The OS: " + System.getProperty(architectureOS));
mailText.append("\n\n");
for (int i=0; i<answers.size(); i++) {
mailText.append("\n" + answers.get(i)[0] + " = " + answers.get(i)[1]);
}
return mailText.toString();
}
}