com.google.gwt.editor.client.impl.AbstractEditorDelegate Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of vaadin-client Show documentation
Show all versions of vaadin-client Show documentation
Vaadin is a web application framework for Rich Internet Applications (RIA).
Vaadin enables easy development and maintenance of fast and
secure rich web
applications with a stunning look and feel and a wide browser support.
It features a server-side architecture with the majority of the logic
running
on the server. Ajax technology is used at the browser-side to ensure a
rich
and interactive user experience.
/*
* Copyright 2010 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.gwt.editor.client.impl;
import com.google.gwt.editor.client.CompositeEditor;
import com.google.gwt.editor.client.Editor;
import com.google.gwt.editor.client.EditorDelegate;
import com.google.gwt.editor.client.EditorError;
import com.google.gwt.editor.client.EditorVisitor;
import com.google.gwt.event.shared.HandlerRegistration;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* A base implementation of EditorDelegate for use by generated types.
*
* @param the type of object being edited
* @param the type of editor
*/
public abstract class AbstractEditorDelegate> implements
EditorDelegate {
/**
* The machinery for attaching and detaching editors from the hierarchy via a
* {@link CompositeEditor}. An instance of a Chain is only created when
* necessary for a given hierarchy type.
*
* @param the component element type
* @param the component editor type
*/
protected class Chain> implements
CompositeEditor.EditorChain {
private final CompositeEditor composedEditor;
private final Class composedElementType;
private final Map> map = new LinkedHashMap>();
/**
* Constructed via
* {@link AbstractEditorDelegate#createChain(CompositeEditor)}.
*/
Chain(CompositeEditor composedEditor, Class composedElementType) {
this.composedEditor = composedEditor;
this.composedElementType = composedElementType;
}
public void accept(EditorVisitor visitor) {
for (AbstractEditorDelegate delegate : map.values()) {
traverse(visitor, delegate);
}
}
public void attach(R object, S subEditor) {
AbstractEditorDelegate subDelegate = map.get(subEditor);
String subPath = path + composedEditor.getPathElement(subEditor);
if (subDelegate == null) {
@SuppressWarnings("unchecked")
AbstractEditorDelegate temp = (AbstractEditorDelegate) createComposedDelegate();
subDelegate = temp;
map.put(subEditor, subDelegate);
addSubDelegate(subDelegate, subPath, subEditor);
} else {
subDelegate.path = subPath;
}
subDelegate.setObject(ensureMutable(object));
traverse(createInitializerVisitor(), subDelegate);
}
public void detach(S subEditor) {
map.remove(subEditor);
}
public R getValue(S subEditor) {
AbstractEditorDelegate subDelegate = map.get(subEditor);
if (subDelegate == null) {
return null;
}
return subDelegate.getObject();
}
void traverse(EditorVisitor visitor, AbstractEditorDelegate delegate) {
R object = delegate.getObject();
new RootEditorContext(delegate, composedElementType, object).traverse(
visitor, delegate);
}
}
protected static String appendPath(String prefix, String path) {
if ("".equals(prefix)) {
return path;
} else {
return prefix + "." + path;
}
}
private boolean dirty;
private Chain, ?> editorChain;
private List errors;
private String path;
public abstract void accept(EditorVisitor visitor);
public abstract T getObject();
public String getPath() {
return path;
}
/**
* Just returns the last value passed to {@link #setDirty(boolean)}.
*/
public boolean isDirty() {
return dirty;
}
public void recordError(String message, Object value, Object userData) {
EditorError error = new SimpleError(this, message, value, userData);
errors.add(error);
}
public void recordError(String message, Object value, Object userData,
String extraPath, Editor> leafEditor) {
EditorError error = new SimpleError(this, message, value, userData,
extraPath, leafEditor);
errors.add(error);
}
public void setDirty(boolean dirty) {
this.dirty = dirty;
}
public abstract HandlerRegistration subscribe();
/**
* Initialize a sub-delegate whenever one is added to the editor hierarchy.
*/
protected > void addSubDelegate(
AbstractEditorDelegate subDelegate, String path, S subEditor) {
subDelegate.initialize(path, subEditor);
}
protected String appendPath(String path) {
if (path.length() == 0) {
return this.path;
}
return appendPath(this.path, path);
}
protected > void createChain(
Class composedElementType) {
@SuppressWarnings("unchecked")
CompositeEditor editor = (CompositeEditor) getEditor();
editorChain = new Chain(editor, composedElementType);
}
/**
* Only implemented by delegates for a {@link CompositeEditor}.
*/
protected AbstractEditorDelegate, ?> createComposedDelegate() {
throw new IllegalStateException();
}
protected EditorVisitor createInitializerVisitor() {
return new Initializer();
}
protected Q ensureMutable(Q object) {
return object;
}
protected abstract E getEditor();
protected Chain, ?> getEditorChain() {
return editorChain;
}
protected List getErrors() {
return errors;
}
protected void initialize(String pathSoFar, E editor) {
this.path = pathSoFar;
setEditor(editor);
errors = new ArrayList();
initializeSubDelegates();
}
protected abstract void initializeSubDelegates();
protected abstract void setEditor(E editor);
protected abstract void setObject(T object);
/**
* Indicates whether or not calls to {@link #flush} are expected as part of
* normal operation.
*/
protected boolean shouldFlush() {
return true;
}
}