import java.util.Iterator;
import java.util.List;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
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.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import ch.sahits.codegen.java.gui.util.EGUIGroup;
import ch.sahits.codegen.java.gui.util.SWTGUIHelper;
import ch.sahits.model.gui.IForm;
import ch.sahits.model.gui.IFormButton;
import ch.sahits.model.gui.IFormCheckbox;
import ch.sahits.model.gui.IFormCombo;
import ch.sahits.model.gui.IFormElement;
import ch.sahits.model.gui.IFormFileUpload;
import ch.sahits.model.gui.IFormGroup;
import ch.sahits.model.gui.IFormImageButton;
import ch.sahits.model.gui.IFormInput;
import ch.sahits.model.gui.IFormLabel;
import ch.sahits.model.gui.IFormList;
import ch.sahits.model.gui.IFormPasswordInput;
import ch.sahits.model.gui.IFormRadioButton;
import ch.sahits.model.gui.IFormResetButton;
import ch.sahits.model.gui.IFormSubmitButton;
import ch.sahits.model.internal.java.gui.Form;
public class SWTGUI {
private Display display = new Display();
private Shell shell = new Shell(display);
public SWTGUI(){
createControl(getShell());
}
/**
* Retrive the shell of this GUI element
* @return shell
*/
private Shell getShell(){
return shell;
}
/**
* Set the control up with the layout and the input fields
*/
private void createControl(Composite composite) {
Composite container = new Composite(composite, SWT.NULL);
container.setFont(composite.getFont());
GridLayout layout = new GridLayout();
container.setLayout(layout);
int nColumns = 2;
layout.numColumns = nColumns;
layout.verticalSpacing = 9;
IForm form = new Form();
SWTGUIHelper guiHelper = new SWTGUIHelper(form);
for (Iterator<IFormElement> iterator = form.elements().iterator(); iterator.hasNext();) {
IFormElement elem = (IFormElement) iterator.next();
if (elem instanceof IFormGroup){
IFormGroup group = (IFormGroup)elem;
if (guiHelper.isFormGroup(group)){
// process the different elements
EGUIGroup g = guiHelper.getGroupType(group);
switch (g){
case BUTTON:
case SUBMIT_BUTTON:{
IFormButton btn = (IFormButton) group.elements().get(1);
String name = guiHelper.toName(btn);
createButton(container, null, btn.getDefaultValue(), btn.getTooltip(), btn.isVisible(), btn.isEnabled());
break;
}
case CHECK_BOX:{
IFormGroup grp = (IFormGroup)group.elements().get(1);
IFormLabel lbl = (IFormLabel) group.elements().get(0);
String name1 = guiHelper.toName(lbl);
List<IFormElement> ch = grp.elements();
String[] name2 = new String[ch.size()];
for (int i = 0; i < name2.length; i++) {
name2[i]=guiHelper.toName(ch.get(i));
}
String[] tooltip = new String[ch.size()];
for (int i = 0; i < tooltip.length; i++) {
tooltip[i]=ch.get(i).getTooltip();
}
boolean[] visible = new boolean[ch.size()];
for (int i = 0; i < visible.length; i++) {
visible[i]=((IFormCheckbox)ch.get(i)).isVisible();
}
boolean[] editable = new boolean[ch.size()];
for (int i = 0; i < editable.length; i++) {
editable[i]=((IFormCheckbox)ch.get(i)).isEnabled();
}
boolean[] selected = new boolean[ch.size()];
for (int i = 0; i < selected.length; i++) {
selected[i]=((IFormCheckbox)ch.get(i)).isChecked();
}
createCheckBoxes(container, null, null, lbl.getText(), tooltip, visible, editable, selected);
break;
}
case COMBO:{
IFormLabel lbl = (IFormLabel) group.elements().get(0);
IFormCombo combo =(IFormCombo)group.elements().get(1);
String name1 = guiHelper.toName(lbl);
String name2 = guiHelper.toName(combo);
createCombo(container, null, null, lbl.getText(), lbl.isVisible(), combo.isVisible(), combo.isEnabled(), combo.getTooltip(), combo.getSelectedIndex(), combo.elements());
break;
}
case FILE_BROWSE:{
IFormLabel lbl = (IFormLabel) group.elements().get(0);
IFormFileUpload txt =(IFormFileUpload)group.elements().get(1);
String name1 = guiHelper.toName(lbl);
String name2 = guiHelper.toName(txt);
String name3 = guiHelper.getBrowseButtonName(txt);
createFileBrowse(container, null, null, null, lbl.getText(), lbl.isVisible(), txt.isVisible(), txt.isEnabled(), txt.getTooltip(), txt.getDefaultValue(), txt.getButtonText(), txt.acceptedTypes().toArray(new String[txt.acceptedTypes().size()]));
break;
}
case IMAGE_BUTTON:{
IFormImageButton btn = (IFormImageButton) group.elements().get(1);
String name = guiHelper.toName(btn);
createImageButton(container, null, btn.getImageSrc(), btn.getTooltip(), btn.isVisible(), btn.isEnabled());
break;
}
case INPUT:{
IFormLabel lbl = (IFormLabel) group.elements().get(0);
IFormInput txt =(IFormInput)group.elements().get(1);
String name1 = guiHelper.toName(lbl);
String name2 = guiHelper.toName(txt);
createInput(container, null, null, lbl.getText(), lbl.isVisible(), txt.isVisible(), txt.isEnabled(), txt.getTooltip(), txt.getDefaultValue());
break;
}
case LIST:{
IFormLabel lbl = (IFormLabel) group.elements().get(0);
IFormList list =(IFormList)group.elements().get(1);
String name1 = guiHelper.toName(lbl);
String name2 = guiHelper.toName(list);
createList(container, null, null, lbl.getText(), lbl.isVisible(), list.isVisible(), list.isEnabled(), list.getTooltip(), list.elements(), list.isMultiselect());
break;
}
case PASSWORD:{
IFormLabel lbl = (IFormLabel) group.elements().get(0);
IFormPasswordInput txt =(IFormPasswordInput)group.elements().get(1);
String name1 = guiHelper.toName(lbl);
String name2 = guiHelper.toName(txt);
createPassword(container, null, null, lbl.getText(), lbl.isVisible(), txt.isVisible(), txt.isEnabled(), txt.getTooltip(), txt.getDefaultValue());
break;
}
case RADIO_BUTTON:{
IFormGroup grp = (IFormGroup)group.elements().get(1);
IFormLabel lbl = (IFormLabel) group.elements().get(0);
String name1 = guiHelper.toName(lbl);
List<IFormElement> ch = grp.elements();
String[] name2 = new String[ch.size()];
for (int i = 0; i < name2.length; i++) {
name2[i]=guiHelper.toName(ch.get(i));
}
String[] tooltip = new String[ch.size()];
for (int i = 0; i < tooltip.length; i++) {
tooltip[i]=ch.get(i).getTooltip();
}
boolean[] visible = new boolean[ch.size()];
for (int i = 0; i < visible.length; i++) {
visible[i]=((IFormRadioButton)ch.get(i)).isVisible();
}
boolean[] editable = new boolean[ch.size()];
for (int i = 0; i < editable.length; i++) {
editable[i]=((IFormRadioButton)ch.get(i)).isEnabled();
}
boolean[] selected = new boolean[ch.size()];
for (int i = 0; i < selected.length; i++) {
selected[i]=((IFormRadioButton)ch.get(i)).isChecked();
}
String[] text = new String[ch.size()];
for (int i = 0; i < selected.length; i++) {
text[i]=((IFormRadioButton)ch.get(i)).getDefaultValue();
}
createRadioButtons(container, null, null, lbl.getText(), text, tooltip, visible, editable, selected);
break;
}
case RESET_BUTTON:{
IFormResetButton btn = (IFormResetButton) group.elements().get(1);
String name = guiHelper.toName(btn);
createResetButton(container, null, btn.getDefaultValue(), btn.getTooltip(), btn.isVisible(), btn.isEnabled());
break;
}
case TEXT:{
IFormLabel lbl = (IFormLabel) group.elements().get(0);
IFormInput txt =(IFormInput)group.elements().get(1);
String name1 = guiHelper.toName(lbl);
String name2 = guiHelper.toName(txt);
createTextArea(container, null, null, lbl.getText(), lbl.isVisible(), txt.isVisible(), txt.isEnabled(), txt.getTooltip(), txt.getDefaultValue());
break;
}
}
} // Is not a form group
} // the base element was not of type IFormGroup
}
}
/**
* Create a text input field with preceeding label
* @param parent container
* @param lbl Label field
* @param txt Text input field
* @param labelText text of the label
* @param lblVisibility is the label visible
* @param inputVisibility is the input field visible
* @param inputEditable ist the input field enabled and editibla
* @param inputTooltip tooltip of the input field may be null
* @param inputText text of the input field may be null
*/
private void createInput(Composite parent,Label lbl, Text txt,String labelText, boolean lblVisibility, boolean inputVisibility,boolean inputEditable,String inputTooltip,String inputText){
lbl = new Label(parent,SWT.NULL);
lbl.setText(labelText);
lbl.setVisible(lblVisibility);
txt = new Text(parent,SWT.BORDER | SWT.SINGLE);
GridData gd = new GridData(GridData.FILL_HORIZONTAL);
txt.setLayoutData(gd);
if (inputTooltip!=null)
txt.setToolTipText(inputTooltip);
txt.setVisible(inputVisibility);
txt.setEnabled(inputEditable);
txt.setEditable(inputEditable);
if (inputText!=null)
txt.setText(inputText);
}
/**
* Create a text input field with preceeding label
* @param parent container
* @param lbl Label field
* @param txt Text input field
* @param labelText text of the label
* @param lblVisibility is the label visible
* @param inputVisibility is the input field visible
* @param inputEditable ist the input field enabled and editibla
* @param inputTooltip tooltip of the input field may be null
* @param inputText text of the input field may be null
*/
private void createPassword(Composite parent,Label lbl, Text txt,String labelText, boolean lblVisibility, boolean inputVisibility,boolean inputEditable,String inputTooltip,String inputText){
lbl = new Label(parent,SWT.NULL);
lbl.setText(labelText);
lbl.setVisible(lblVisibility);
txt = new Text(parent,SWT.BORDER | SWT.PASSWORD);
GridData gd = new GridData(GridData.FILL_HORIZONTAL);
txt.setLayoutData(gd);
if (inputTooltip!=null)
txt.setToolTipText(inputTooltip);
txt.setVisible(inputVisibility);
txt.setEnabled(inputEditable);
txt.setEditable(inputEditable);
if (inputText!=null)
txt.setText(inputText);
}
/**
* Create a button
* @param parent container
* @param btn Button
* @param btnText button text
* @param tooltip button tooltip
* @param visible is the button visible
* @param editable is the button cklickable
*/
private void createButton(Composite parent,Button btn,String btnText,String tooltip,boolean visible,boolean editable) {
btn = new Button(parent,SWT.PUSH);
btn.setText(btnText);
btn.setToolTipText(tooltip);
btn.setEnabled(editable);
btn.setVisible(visible);
btn.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
// TODO: implement the SelectionEvent
}
});
}
/**
* Create a reset buttonbutton
* @param parent container
* @param btn Button
* @param btnText button text
* @param tooltip button tooltip
* @param visible is the button visible
* @param editable is the button cklickable
*/
private void createResetButton(Composite parent,Button btn,String btnText,String tooltip,boolean visible,boolean editable) {
btn = new Button(parent,SWT.PUSH);
btn.setText(btnText);
btn.setToolTipText(tooltip);
btn.setEnabled(editable);
btn.setVisible(visible);
btn.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
// set the default data
}
});
}
/**
* Create a button
* @param parent container
* @param btn Button
* @param imageSrc button image
* @param tooltip button tooltip
* @param visible is the button visible
* @param editable is the button cklickable
*/
private void createImageButton(Composite parent,Button btn,String imageSrc,String tooltip,boolean visible,boolean editable) {
btn = new Button(parent,SWT.PUSH);
Image image = new Image(parent.getDisplay(),imageSrc);
btn.setImage(image);
btn.setToolTipText(tooltip);
btn.setEnabled(editable);
btn.setVisible(visible);
btn.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
// TODO: implement the SelectionEvent
}
});
}
/**
* Create checkboxes
* @param parent container
* @param lbl Label field
* @param btn Array of Button fields
* @param lblText Label text
* @param text Array of texts of the buttons
* @param tooltip Array of tooltips of the buttons
* @param visible Array of the buttons visibility
* @param editable Array of the buttons editability
* @param selected Array of the buttons selectedness
*/
private void createCheckBoxes(Composite parent,Label lbl,Button[] btn,String lblText, String[] tooltip, boolean[] visible, boolean[] editable,boolean[] selected) {
Composite cont = new Composite(parent,SWT.NULL);
FillLayout fl = new FillLayout(SWT.HORIZONTAL);
cont.setLayout(fl);
lbl = new Label(parent,SWT.NULL);
lbl.setText(lblText);
lbl.setVisible(visible[0]);
for (int i = 0; i < selected.length; i++) {
createCheckbox(cont,btn[i],tooltip[i],visible[i],editable[i],selected[i]);
}
}
/**
* Create a checkbox
* @param parent container
* @param btn Button field
* @param tooltip of the button
* @param visible is the button visible
* @param editable is the button editable
* @param selected is the button selected
*/
private void createCheckbox(Composite parent,Button btn, String tooltip, boolean visible, boolean editable,boolean selected){
btn = new Button(parent,SWT.CHECK);
btn.setToolTipText(tooltip);
btn.setVisible(visible);
btn.setEnabled(editable);
btn.setSelection(selected);
btn.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
// TODO Implement SelectionEvent
}
});
}
/**
* Create radio Buttons
* @param parent container
* @param lbl Label field
* @param btn Array of Button fields
* @param lblText label text
* @param text Array of texts of the buttons
* @param tooltip Array of tooltips of the buttons
* @param visible Array of the buttons visibility
* @param editable Array of the buttons editability
* @param selected Array of the buttons selectedness
*/
private void createRadioButtons(Composite parent,Label lbl, Button[] btn,String lblText,String[] text, String[] tooltip, boolean[] visible, boolean[] editable,boolean[] selected) {
Composite cont = new Composite(parent,SWT.NULL);
FillLayout fl = new FillLayout(SWT.HORIZONTAL);
cont.setLayout(fl);
lbl = new Label(parent,SWT.NULL);
lbl.setText(lblText);
lbl.setVisible(visible[0]);
for (int i = 0; i < selected.length; i++) {
createRadioButton(cont,btn[i],text[i],tooltip[i],visible[i],editable[i],selected[i]);
}
}
/**
* Create a radio Button
* @param parent container
* @param btn Button field
* @param text Text of the button
* @param tooltip of the button
* @param visible is the button visible
* @param editable is the button editable
* @param selected is the button selected
*/
private void createRadioButton(Composite parent,Button btn,String text, String tooltip, boolean visible, boolean editable,boolean selected){
btn = new Button(parent,SWT.RADIO);
btn.setText(text);
btn.setToolTipText(tooltip);
btn.setVisible(visible);
btn.setEnabled(editable);
btn.setSelection(selected);
btn.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
// TODO Implement SelectionEvent
}
});
}
/**
* Create a text input field with preceeding label
* @param parent container
* @param lbl Label field
* @param list Combo field
* @param labelText text of the label
* @param lblVisibility is the label visible
* @param inputVisibility is the input field visible
* @param inputEditable ist the input field enabled and editibla
* @param tooltip tooltip of the input field may be null
* @param defaultSelection index of the default selection
* @param elements List of all elements
*/
private void createCombo(Composite parent,Label lbl, Combo combo,String labelText, boolean lblVisibility, boolean inputVisibility,boolean inputEditable,String tooltip, int defaultSelection,List<String> elements) {
lbl = new Label(parent,SWT.NULL);
lbl.setText(labelText);
lbl.setVisible(lblVisibility);
combo = new Combo(parent,SWT.DROP_DOWN);
combo.setVisible(inputVisibility);
combo.setEnabled(inputEditable);
combo.setToolTipText(tooltip);
for (Iterator<String> iterator = elements.iterator(); iterator.hasNext();) {
String type = (String) iterator.next();
combo.add(type);
}
combo.select(defaultSelection);
combo.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
// TODO: implement SelectionEvent
}
});
}
/**
* Create a text input field with preceeding label
* @param parent container
* @param lbl Label field
* @param list txt Input field
* @param btn Browse button
* @param labelText text of the label
* @param lblVisibility is the label visible
* @param inputVisibility is the input field visible
* @param inputEditable ist the input field enabled and editibla
* @param inputTooltip tooltip of the input field may be null
* @param inputText text of the input field may be null
*/
private void createFileBrowse(Composite parent,Label lbl,final Text txt, Button btn,String labelText, boolean lblVisibility, boolean inputVisibility,boolean inputEditable,String inputTooltip,String inputText,String btnText,final String[] fileTypes){
lbl = new Label(parent,SWT.NULL);
lbl.setText(labelText);
lbl.setVisible(lblVisibility);
Composite cont = new Composite(parent,SWT.NULL);
FillLayout fl = new FillLayout(SWT.HORIZONTAL);
cont.setLayout(fl);
txt.setParent(cont);
GridData gd = new GridData(GridData.FILL_HORIZONTAL);
txt.setLayoutData(gd);
if (inputTooltip!=null)
txt.setToolTipText(inputTooltip);
txt.setVisible(inputVisibility);
txt.setEnabled(inputEditable);
txt.setEditable(inputEditable);
if (inputText!=null)
txt.setText(inputText);
btn = new Button(cont,SWT.PUSH);
btn.setText(btnText);
btn.setToolTipText(inputTooltip);
btn.setEnabled(inputEditable);
btn.setVisible(inputVisibility);
btn.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
String selection = handleBroseFileSystem(fileTypes);
if (selection!=null){
txt.setText(selection);
}
}
});
}
/**
* Browse for the jet file
* @param filter Array of filters in the form of *.jpg
* @return selected file or null
*/
private String handleBroseFileSystem(String[] filter){
FileDialog dialog = new FileDialog(getShell(),SWT.OPEN);
dialog.setText("Browse file");
dialog.setFilterExtensions(filter);
dialog.setFilterNames(filter);
return dialog.open();
}
/**
* Create a text input field with preceeding label
* @param parent container
* @param lbl Label field
* @param list List field
* @param labelText text of the label
* @param lblVisibility is the label visible
* @param inputVisibility is the input field visible
* @param inputEditable ist the input field enabled and editibla
* @param tooltip tooltip of the input field may be null
* @param elements List of all elements
* @param multiselect selection of several elements allowed
*/
private void createList(Composite parent,Label lbl, org.eclipse.swt.widgets.List list,String labelText, boolean lblVisibility, boolean inputVisibility,boolean inputEditable,String tooltip,List<String> elements,boolean multiselect) {
lbl = new Label(parent,SWT.NULL);
lbl.setText(labelText);
lbl.setVisible(lblVisibility);
if (multiselect){
list = new org.eclipse.swt.widgets.List(parent,SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.MULTI);
} else {
list = new org.eclipse.swt.widgets.List(parent,SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
}
list.setVisible(inputVisibility);
list.setEnabled(inputEditable);
list.setToolTipText(tooltip);
for (Iterator<String> iterator = elements.iterator(); iterator.hasNext();) {
String type = (String) iterator.next();
list.add(type);
}
list.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
// TODO: implement SelectionEvent
}
});
}
/**
* Create a text input field with preceeding label
* @param parent container
* @param lbl Label field
* @param txt Text input field
* @param labelText text of the label
* @param lblVisibility is the label visible
* @param inputVisibility is the input field visible
* @param inputEditable ist the input field enabled and editibla
* @param inputTooltip tooltip of the input field may be null
* @param inputText text of the input field may be null
*/
private void createTextArea(Composite parent,Label lbl, Text txt,String labelText, boolean lblVisibility, boolean inputVisibility,boolean inputEditable,String inputTooltip,String inputText){
lbl = new Label(parent,SWT.NULL);
lbl.setText(labelText);
lbl.setVisible(lblVisibility);
txt = new Text(parent,SWT.BORDER | SWT.WRAP| SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
GridData gd = new GridData(GridData.FILL_HORIZONTAL);
txt.setLayoutData(gd);
if (inputTooltip!=null)
txt.setToolTipText(inputTooltip);
txt.setVisible(inputVisibility);
txt.setEnabled(inputEditable);
txt.setEditable(inputEditable);
if (inputText!=null)
txt.setText(inputText);
}
}