All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.gwt.editor.client.impl.AbstractEditorContext Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2011 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.EditorContext;
import com.google.gwt.editor.client.EditorDelegate;
import com.google.gwt.editor.client.EditorVisitor;
import com.google.gwt.editor.client.HasEditorDelegate;
import com.google.gwt.editor.client.HasEditorErrors;
import com.google.gwt.editor.client.LeafValueEditor;
import com.google.gwt.editor.client.ValueAwareEditor;

/**
 * Base implementation of EditorContext.
 * 
 * @param  the type of data being edited
 */
public abstract class AbstractEditorContext implements EditorContext {
  private final String path;
  private final CompositeEditor compositeEditor;
  private AbstractEditorDelegate delegate;
  private final Editor editor;
  private final HasEditorDelegate hasEditorDelegate;
  private final HasEditorErrors hasEditorErrors;
  private boolean isHalted;
  private final LeafValueEditor leafValueEditor;
  private final ValueAwareEditor valueAwareEditor;

  public AbstractEditorContext(Editor editor, String path) {
    this.editor = editor;
    this.path = path;
    /*
     * TODO(bobv): Determine if pre-casting is better than demand-casting or
     * generating the asFoo methods.
     */
    compositeEditor = editor instanceof CompositeEditor
        ? (CompositeEditor) editor : null;
    hasEditorDelegate = editor instanceof HasEditorDelegate
        ? (HasEditorDelegate) editor : null;
    hasEditorErrors = editor instanceof HasEditorErrors
        ? (HasEditorErrors) editor : null;
    leafValueEditor = editor instanceof LeafValueEditor
        ? (LeafValueEditor) editor : null;
    valueAwareEditor = editor instanceof ValueAwareEditor
        ? (ValueAwareEditor) editor : null;
  }

  @SuppressWarnings("unchecked")
  public CompositeEditor asCompositeEditor() {
    return (CompositeEditor) compositeEditor;
  }

  public HasEditorDelegate asHasEditorDelegate() {
    return hasEditorDelegate;
  }

  public HasEditorErrors asHasEditorErrors() {
    return hasEditorErrors;
  }

  public LeafValueEditor asLeafValueEditor() {
    return leafValueEditor;
  }

  public ValueAwareEditor asValueAwareEditor() {
    return valueAwareEditor;
  }

  public abstract boolean canSetInModel();

  public abstract T checkAssignment(Object value);

  @SuppressWarnings(value = {"rawtypes", "unchecked"})
  public void doTraverseSyntheticCompositeEditor(EditorVisitor visitor) {
    Editor sample = this.asCompositeEditor().createEditorForTraversal();
    AbstractEditorDelegate subDelegate = delegate.createComposedDelegate();
    delegate.addSubDelegate(subDelegate, path, sample);
    delegate.getEditorChain().traverse(visitor, subDelegate);
  }

  public String getAbsolutePath() {
    // Not delegate.getPath() since delegate might be null for a leaf editor
    return path;
  }

  public abstract Class getEditedType();

  public Editor getEditor() {
    return editor;
  }

  public EditorDelegate getEditorDelegate() {
    return delegate;
  }

  public abstract T getFromModel();

  public void halt() {
    isHalted = true;
  }

  public boolean isHalted() {
    return isHalted;
  }

  public void setEditorDelegate(AbstractEditorDelegate delegate) {
    this.delegate = delegate;
  }

  public abstract void setInModel(T data);

  public void traverse(EditorVisitor visitor, AbstractEditorDelegate next) {
    if (visitor.visit(this) && next != null) {
      next.accept(visitor);
    }
    visitor.endVisit(this);
  }

  public void traverseSyntheticCompositeEditor(EditorVisitor visitor) {
    if (asCompositeEditor() == null) {
      throw new IllegalStateException();
    }
    doTraverseSyntheticCompositeEditor(visitor);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy