Class DesignerViewState

java.lang.Object
com.iizigo.panel.DesignerViewState

public final class DesignerViewState extends Object
The view state of the Panel Designer that lives in the JavaScript design view and that a page reload loses: the marks (the selection in the panel) in their order, whether the last selection came from the panel or from the Structure tree, the zoom level, and the scroll offsets of the design area and of every scrolled container.

One instance is filled by PanelEditor.captureViewState() and applied by PanelEditor.applyViewState(DesignerViewState). The same instance is written to and read from the workbench IMemento for editor close and open, so the refresh and the close/open paths share one state object, one save and one apply. The state is kept in memory for a refresh and in the per-user workbench memento for close/open; it is never stored in the panel file, which is versioned and shared.

The Structure tree selection and expansion are not part of this object: they survive a refresh untouched (the tree is an SWT control) and the memento already holds them through PropHelper.saveViewer. The tree selection and the panel marks are two different selections, each with its own order, and this object only carries the marks.

The methods that resolve names, parse the scroll data and format it for JavaScript are static and free of SWT so that they can be unit tested.

Author:
Christopher Mindus
  • Constructor Details

    • DesignerViewState

      public DesignerViewState()
      Creates an empty state.
  • Method Details

    • getMarks

      public List<String> getMarks()
      Gets the marks as names relative to the panel, in mark order.
      Returns:
      An unmodifiable list, index 0 is the reference mark.
    • setMarks

      public void setMarks(List<String> names)
      Sets the marks.
      Parameters:
      names - The names relative to the panel, in mark order.
    • isSelectionFromComponents

      public boolean isSelectionFromComponents()
      Gets the flag for the last selection made in the panel rather than in the tree.
      Returns:
      The flag.
    • setSelectionFromComponents

      public void setSelectionFromComponents(boolean on)
      Sets the flag for the last selection made in the panel rather than in the tree.
      Parameters:
      on - The flag.
    • getZoom

      public int getZoom()
      Gets the zoom level.
      Returns:
      The zoom level in percent, zero when unknown.
    • setZoom

      public void setZoom(int zoom)
      Sets the zoom level.
      Parameters:
      zoom - The zoom level in percent, zero for unknown.
    • getScrollX

      public int getScrollX()
      Gets the design area horizontal scroll offset.
      Returns:
      The offset in CSS pixels, -1 when unknown.
    • getScrollY

      public int getScrollY()
      Gets the design area vertical scroll offset.
      Returns:
      The offset in CSS pixels, -1 when unknown.
    • setScroll

      public void setScroll(int x, int y)
      Sets the design area scroll offsets.
      Parameters:
      x - The horizontal offset, -1 for unknown.
      y - The vertical offset, -1 for unknown.
    • getContainerScrolls

      public Map<String,int[]> getContainerScrolls()
      Gets the container scroll offsets.
      Returns:
      An unmodifiable map of relative container name (empty string for the panel) to {x,y}.
    • addContainerScroll

      public void addContainerScroll(String name, int x, int y)
      Adds a container scroll offset.
      Parameters:
      name - The container name relative to the panel, empty string for the panel itself.
      x - The horizontal offset.
      y - The vertical offset.
    • hasScroll

      public boolean hasScroll()
      Checks if any scroll offset is known.
      Returns:
      true if the design area offsets or any container offset is present.
    • save

      public void save(IMemento memento)
      Saves the state as a child of the memento.
      Parameters:
      memento - The memento.
    • load

      public static DesignerViewState load(IMemento memento)
      Loads the state from a memento.
      Parameters:
      memento - The memento, may be null.
      Returns:
      The state, or null if the memento is null or holds no state.
    • resolve

      public static <T> List<T> resolve(List<String> names, Class<T> type, Function<String,Object> lookup, String what, List<String> skips)
      Resolves names to targets one by one. A name whose target is missing, of the wrong type or refused by the lookup is skipped with a reason and the rest continues.
      Type Parameters:
      T - The target type.
      Parameters:
      names - The names to resolve, in order.
      type - The target type.
      lookup - The lookup: returns the target, or null when not found, or a String with the reason when the target exists but must be skipped.
      what - What is being resolved, for the skip messages, e.g. "mark".
      skips - The list receiving one message per skipped name.
      Returns:
      The resolved targets in the order of the names, without the skipped ones.
    • logSkips

      public static void logSkips(Class<?> originator, String operation, List<String> skips)
      Logs the skipped items of an apply operation: one line per skipped item and one summary line. The level is WARNING for now, so the frequency can be seen; it will become INFO if frequent.
      Parameters:
      originator - The logging class.
      operation - The operation, e.g. "refresh".
      skips - The skip messages, empty for none.
    • readScrollArray

      public void readScrollArray(Object[] a, List<String> skips)
      Reads the scroll data returned by the design view: an array of the design area offsets followed by triplets of container name, x and y. Elements that are not numbers or strings where expected are skipped with a reason.
      Parameters:
      a - The array returned by Browser.evaluate, may be null.
      skips - The list receiving one message per skipped element.
    • verifyScrollResult

      public void verifyScrollResult(Object[] r, List<String> skips)
      Verifies the offsets the design view reports after applying the scroll data: the same shape as readScrollArray(Object[],List), with -1,-1 for a container that was not found. An offset the page could not take (content smaller than before) or a container that is gone is reported as a skip.
      Parameters:
      r - The array returned by the apply script, may be null.
      skips - The list receiving one message per item not applied as captured.
    • toScrollArrayLiteral

      public String toScrollArrayLiteral()
      Formats the scroll offsets as a JavaScript array literal in the same shape as readScrollArray(Object[],List) reads: [x,y,"name",x,y,...].
      Returns:
      The literal, e.g. [10,20,"$components/c1",0,50].
    • toString

      public String toString()
      Gets a short description of the state for logging.
      Overrides:
      toString in class Object
      Returns:
      The description.