Skip to content

Instantly share code, notes, and snippets.

@mbruch
Last active December 17, 2015 05:18
Show Gist options
  • Save mbruch/5556483 to your computer and use it in GitHub Desktop.
Save mbruch/5556483 to your computer and use it in GitHub Desktop.
package com.example.miniscout.client.editor;
import java.net.URI;
import com.example.miniscout.shared.Field;
import com.example.miniscout.shared.Form;
public class DatabaseService {
public static Form load(URI uri) {
Form form = new Form();
form.name = "In Memory Form";
form.fields = new Field[] { new Field("string", "Firstname", "Max"),
new Field("string", "Lastname", "Mustermann"),
new Field("string", "Job Role", "Chief Architect"),
new Field("string", "Company", "Codetrails UG"),
new Field("string", "Street", "Seestraße 7") };
return form;
}
public static void save(Form form, URI uri) {
// in memory database does not persist
}
}
package com.example.miniscout.client.editor;
import java.net.URI;
import java.util.LinkedList;
import java.util.List;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.EditorPart;
import com.example.miniscout.client.fields.FormField;
import com.example.miniscout.client.fields.StringFormField;
import com.example.miniscout.client.fields.FormField.Listener;
import com.example.miniscout.client.fields.FormField.ValueChangedEvent;
import com.example.miniscout.shared.Field;
import com.example.miniscout.shared.Form;
public class Editor extends EditorPart implements Listener<ValueChangedEvent> {
private URI input;
private Form form;
private List<FormField> fields;
private boolean dirty;
@Override
public void init(IEditorSite site, IEditorInput in) throws PartInitException {
setSite(site);
setInput(in);
this.input = ((ContactEditorInput) in).getInput();
form = DatabaseService.load(input);
}
@Override
public void createPartControl(Composite parent) {
Composite container = new Composite(parent, SWT.NONE);
container.setLayout(new GridLayout(2, false));
fields = new LinkedList<FormField>();
for (Field field : form.fields) {
FormField f = new StringFormField();
fields.add(f);
f.createControls(container, field);
f.addListener(this);
}
parent.layout();
}
@Override
public void setFocus() {
}
@Override
public boolean isDirty() {
return dirty;
}
@Override
public void doSave(IProgressMonitor monitor) {
for (FormField f : fields) {
f.save();
}
DatabaseService.save(form, input);
dirty = false;
firePropertyChange(IEditorPart.PROP_DIRTY);
}
@Override
public boolean isSaveAsAllowed() {
return false;
}
@Override
public void doSaveAs() {
}
@Override
public void onEvent(ValueChangedEvent event) {
dirty = true;
firePropertyChange(IEditorPart.PROP_DIRTY);
}
}
package com.example.miniscout.client.fields;
import java.util.HashSet;
import java.util.Set;
import org.eclipse.swt.widgets.Composite;
import com.example.miniscout.shared.Field;
public abstract class FormField {
public class ValueChangedEvent {
public FormField field;
protected ValueChangedEvent(FormField field) {
this.field = field;
}
}
public static interface Listener<T> {
void onEvent(T event);
}
private Set<Listener<ValueChangedEvent>> listeners = new HashSet<Listener<ValueChangedEvent>>();
public abstract void createControls(Composite parent, Field field);
public abstract void save();
public void addListener(Listener<ValueChangedEvent> listener) {
listeners.add(listener);
}
public void removeListener(Listener<ValueChangedEvent> listener) {
listeners.remove(listener);
}
public void dispose() {
listeners.clear();
}
public void fireValueChangedEvent() {
for (Listener<ValueChangedEvent> l : listeners) {
l.onEvent(new ValueChangedEvent(this));
}
}
}
package com.example.miniscout.client.fields;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Spinner;
import com.example.miniscout.shared.Field;
public class IntFormField extends FormField {
private Spinner spinner;
private Label label;
private Field field;
@Override
public void createControls(Composite parent, Field field) {
this.field = field;
label = new Label(parent, SWT.None);
label.setText(field.name);
spinner = new Spinner(parent, SWT.BORDER);
spinner.setValues(Integer.parseInt(field.value), 0, 150, 0, 1, 1);
spinner.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
fireValueChangedEvent();
}
});
}
@Override
public void save() {
field.value = spinner.getText();
}
}
package com.example.miniscout.client.fields;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import com.example.miniscout.shared.Field;
public class StringFormField extends FormField {
private Text text;
private Label label;
private Field field;
@Override
public void createControls(Composite parent, Field field) {
this.field = field;
label = new Label(parent, SWT.None);
label.setText(field.name);
text = new Text(parent, SWT.BORDER);
text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
text.setText(field.value);
text.addModifyListener(new ModifyListener() {
@Override
public void modifyText(ModifyEvent e) {
fireValueChangedEvent();
}
});
}
@Override
public void save() {
field.value = text.getText();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment