package votorola.s.gwt.scene.dum; // Copyright 2010, Michael Allan. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Votorola Software"), to deal in the Votorola Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Votorola Software, and to permit persons to whom the Votorola Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Votorola Software. THE VOTOROLA SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE VOTOROLA SOFTWARE OR THE USE OR OTHER DEALINGS IN THE VOTOROLA SOFTWARE. import com.google.web.bindery.event.shared.HandlerRegistration; import com.google.gwt.user.client.ui.*; import com.google.gwt.view.client.SelectionChangeEvent; import java.util.Arrays; import votorola.a.web.gwt.*; import votorola.s.gwt.scene.*; import votorola.s.gwt.scene.feed.*; import votorola.s.gwt.stage.poll.*; import votorola.g.hold.*; import votorola.g.web.gwt.*; /** A view of a dummy scene rendered as a cubic grid collapsed to a planar grid. Thus only * the lateral x and y positions are visible, as though the point of view was along the * z-axis (e.g. top down) where depth is imperceptible. * * @see Live example of a DummySceneV * (right) */ public final class DummySceneV extends Grid { /** Constructs a DummySceneV. */ public DummySceneV( DummyScene _scene ) { super( SIZE, SIZE ); scene = _scene; addStyleName( "dum-DummySceneV" ); addStyleName( "plain" ); } // - W i d g e t ---------------------------------------------------------------------- protected @Override void onLoad() { spool = new Spool1(); new Scoper(); new Spotlighter(); super.onLoad(); } protected @Override void onUnload() { super.onUnload(); spool.unwind(); spool = null; // single use } //// P r i v a t e /////////////////////////////////////////////////////////////////////// /** @param ii the array in which to store row and column indeces, in that order. * @return the same ii array, or a newly constructed one if it was null. */ private static int[] calculateCellIndeces( final DummyScene.MappedPoll poll, int[] ii ) { if( ii == null ) ii = new int[2]; final float x = poll.x(); final float y = 1 - poll.y(); // reflected, so origin at bottom ii[0] = (int)(y * SIZE); // row ii[1] = (int)(x * SIZE); // col return ii; } private final DummyScene scene; private static final int SIZE = 40; // the larger, the slower to layout private Spool spool; // ==================================================================================== private final class Scoper implements ScopeChangeHandler { Scoper() { spool.add( new Hold() { final HandlerRegistration hR = scene.scoping().addHandler( Scoper.this ); public void release() { hR.removeHandler(); } }); rescope(); // init state } public void onScopeChange( final ScopeChangeEvent e ) { rescope(); } private void rescope() { final HTMLTable.CellFormatter f = getCellFormatter(); int row = 0; int col; for( ; row < SIZE; ++row ) { col = 0; for( ; col < SIZE; ++col ) { f.removeStyleName( row, col, "multi" ); f.removeStyleName( row, col, "p" ); } } final int[] ii = new int[2]; for( final DummyScene.MappedPoll poll: scene.polls().values() ) { if( !scene.inScope( poll )) continue; calculateCellIndeces( poll, ii ); row = ii[0]; col = ii[1]; final String allNames = f.getStyleName( row, col ); if( ElementX.hasClassName( allNames, "p" )) f.addStyleName( row, col, "multi" ); else f.addStyleName( row, col, "p" ); } } } // ==================================================================================== private final class Spotlighter implements ScopeChangeHandler, SelectionChangeEvent.Handler { Spotlighter() { spool.add( new Hold() { final HandlerRegistration hR = scene.scoping().addHandler( Spotlighter.this ); public void release() { hR.removeHandler(); } }); spool.add( new Hold() { final HandlerRegistration hR = Scenes.i().biteSelection().addSelectionChangeHandler( Spotlighter.this ); public void release() { hR.removeHandler(); } }); respotlight(); // init state } private int[] iiLast; public void onScopeChange( final ScopeChangeEvent e ) { respotlight(); } public void onSelectionChange( SelectionChangeEvent e ) { respotlight(); } private void respotlight() { int[] ii = null; // thus far final BiteJS bite = Scenes.i().biteSelection().getSelectedObject(); if( bite != null ) { final PollJS poll = bite.poll(); if( poll != null ) { final String pollName = poll.name(); if( pollName != null ) { final DummyScene.MappedPoll pollM = scene.polls().get( pollName ); if( pollM != null && scene.inScope( pollM )) { ii = calculateCellIndeces( pollM, ii ); } } } } if( Arrays.equals( ii, iiLast )) return; final HTMLTable.CellFormatter f = getCellFormatter(); if( iiLast != null ) f.removeStyleName( iiLast[0], iiLast[1], "spotlighted" ); iiLast = ii; if( ii != null ) f.addStyleName( ii[0], ii[1], "spotlighted" ); } } }