/examples/src/main/java/com/googlecode/gflot/examples/client/MainView.java

http://gflot.googlecode.com/ · Java · 421 lines · 359 code · 52 blank · 10 comment · 19 complexity · 7af115d2486927c3ecdec0760768db19 MD5 · raw file

  1. package com.googlecode.gflot.examples.client;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import com.google.gwt.core.client.GWT;
  5. import com.google.gwt.event.dom.client.ChangeEvent;
  6. import com.google.gwt.event.dom.client.ClickEvent;
  7. import com.google.gwt.place.shared.Place;
  8. import com.google.gwt.place.shared.PlaceChangeEvent;
  9. import com.google.gwt.place.shared.PlaceChangeEvent.Handler;
  10. import com.google.gwt.place.shared.PlaceController;
  11. import com.google.gwt.uibinder.client.UiBinder;
  12. import com.google.gwt.uibinder.client.UiField;
  13. import com.google.gwt.uibinder.client.UiHandler;
  14. import com.google.gwt.user.client.ui.AcceptsOneWidget;
  15. import com.google.gwt.user.client.ui.Anchor;
  16. import com.google.gwt.user.client.ui.Hyperlink;
  17. import com.google.gwt.user.client.ui.ListBox;
  18. import com.google.gwt.user.client.ui.ResizeComposite;
  19. import com.google.gwt.user.client.ui.ScrollPanel;
  20. import com.google.gwt.user.client.ui.Widget;
  21. import com.google.web.bindery.event.shared.EventBus;
  22. import com.googlecode.gflot.examples.client.examples.bar.BarPlace;
  23. import com.googlecode.gflot.examples.client.examples.decimation.DecimationPlace;
  24. import com.googlecode.gflot.examples.client.examples.export.ExportPlace;
  25. import com.googlecode.gflot.examples.client.examples.hover.HoverPlace;
  26. import com.googlecode.gflot.examples.client.examples.image.ImagePlace;
  27. import com.googlecode.gflot.examples.client.examples.interactivelegend.InteractiveLegendPlace;
  28. import com.googlecode.gflot.examples.client.examples.line.LinePlace;
  29. import com.googlecode.gflot.examples.client.examples.markings.MarkingsPlace;
  30. import com.googlecode.gflot.examples.client.examples.multipleaxes.MultipleAxesPlace;
  31. import com.googlecode.gflot.examples.client.examples.navigate.NavigatePlace;
  32. import com.googlecode.gflot.examples.client.examples.overview.OverviewPlace;
  33. import com.googlecode.gflot.examples.client.examples.pie.PiePlace;
  34. import com.googlecode.gflot.examples.client.examples.selection.SelectionPlace;
  35. import com.googlecode.gflot.examples.client.examples.sliding.SlidingPlace;
  36. import com.googlecode.gflot.examples.client.examples.stack.StackPlace;
  37. import com.googlecode.gflot.examples.client.examples.threshold.ThresholdPlace;
  38. import com.googlecode.gflot.examples.client.examples.tracking.TrackingPlace;
  39. import com.googlecode.gflot.examples.client.resources.Resources;
  40. import com.googlecode.gflot.examples.client.source.PlaceWithSources;
  41. public class MainView
  42. extends ResizeComposite
  43. implements Handler
  44. {
  45. private static MainViewUiBinder uiBinder = GWT.create( MainViewUiBinder.class );
  46. interface MainViewUiBinder
  47. extends UiBinder<Widget, MainView>
  48. {
  49. }
  50. private static interface Predicate
  51. {
  52. boolean apply( Place place );
  53. }
  54. private static class Link
  55. {
  56. private Hyperlink link;
  57. private Predicate predicate;
  58. public Link( Hyperlink link, Predicate predicate )
  59. {
  60. this.link = link;
  61. this.predicate = predicate;
  62. }
  63. public boolean isPlaceMatchLink( Place place )
  64. {
  65. return predicate.apply( place );
  66. }
  67. public Hyperlink getLink()
  68. {
  69. return link;
  70. }
  71. }
  72. private final PlaceController placeController;
  73. private List<Link> links;
  74. @UiField( provided = true )
  75. Resources res;
  76. @UiField
  77. Hyperlink barLink;
  78. @UiField
  79. Hyperlink decimationLink;
  80. @UiField
  81. Hyperlink hoverLink;
  82. @UiField
  83. Hyperlink imageLink;
  84. @UiField
  85. Hyperlink interactiveLegendLink;
  86. @UiField
  87. Hyperlink markingsLink;
  88. @UiField
  89. Hyperlink multipleAxesLink;
  90. @UiField
  91. Hyperlink overviewLink;
  92. @UiField
  93. Hyperlink pieLink;
  94. @UiField
  95. Hyperlink selectionLink;
  96. @UiField
  97. Hyperlink simpleLink;
  98. @UiField
  99. Hyperlink slidingLink;
  100. @UiField
  101. Hyperlink stackLink;
  102. @UiField
  103. Hyperlink exportLink;
  104. @UiField
  105. Hyperlink thresholdLink;
  106. @UiField
  107. Hyperlink navigateLink;
  108. @UiField
  109. Hyperlink trackingLink;
  110. @UiField
  111. Anchor exampleLink;
  112. @UiField
  113. Anchor sourceLink;
  114. @UiField
  115. ListBox sourceList;
  116. @UiField
  117. ScrollPanel container;
  118. public MainView( EventBus eventBus, PlaceController placeController, Resources res )
  119. {
  120. this.placeController = placeController;
  121. this.res = res;
  122. initWidget( uiBinder.createAndBindUi( this ) );
  123. links = new ArrayList<Link>();
  124. links.add( new Link( barLink, new Predicate() {
  125. @Override
  126. public boolean apply( Place place )
  127. {
  128. return place instanceof BarPlace;
  129. }
  130. } ) );
  131. links.add( new Link( decimationLink, new Predicate() {
  132. @Override
  133. public boolean apply( Place place )
  134. {
  135. return place instanceof DecimationPlace;
  136. }
  137. } ) );
  138. links.add( new Link( hoverLink, new Predicate() {
  139. @Override
  140. public boolean apply( Place place )
  141. {
  142. return place instanceof HoverPlace;
  143. }
  144. } ) );
  145. links.add( new Link( imageLink, new Predicate() {
  146. @Override
  147. public boolean apply( Place place )
  148. {
  149. return place instanceof ImagePlace;
  150. }
  151. } ) );
  152. links.add( new Link( interactiveLegendLink, new Predicate() {
  153. @Override
  154. public boolean apply( Place place )
  155. {
  156. return place instanceof InteractiveLegendPlace;
  157. }
  158. } ) );
  159. links.add( new Link( markingsLink, new Predicate() {
  160. @Override
  161. public boolean apply( Place place )
  162. {
  163. return place instanceof MarkingsPlace;
  164. }
  165. } ) );
  166. links.add( new Link( multipleAxesLink, new Predicate() {
  167. @Override
  168. public boolean apply( Place place )
  169. {
  170. return place instanceof MultipleAxesPlace;
  171. }
  172. } ) );
  173. links.add( new Link( overviewLink, new Predicate() {
  174. @Override
  175. public boolean apply( Place place )
  176. {
  177. return place instanceof OverviewPlace;
  178. }
  179. } ) );
  180. links.add( new Link( pieLink, new Predicate() {
  181. @Override
  182. public boolean apply( Place place )
  183. {
  184. return place instanceof PiePlace;
  185. }
  186. } ) );
  187. links.add( new Link( selectionLink, new Predicate() {
  188. @Override
  189. public boolean apply( Place place )
  190. {
  191. return place instanceof SelectionPlace;
  192. }
  193. } ) );
  194. links.add( new Link( simpleLink, new Predicate() {
  195. @Override
  196. public boolean apply( Place place )
  197. {
  198. return place instanceof LinePlace;
  199. }
  200. } ) );
  201. links.add( new Link( slidingLink, new Predicate() {
  202. @Override
  203. public boolean apply( Place place )
  204. {
  205. return place instanceof SlidingPlace;
  206. }
  207. } ) );
  208. links.add( new Link( stackLink, new Predicate() {
  209. @Override
  210. public boolean apply( Place place )
  211. {
  212. return place instanceof StackPlace;
  213. }
  214. } ) );
  215. links.add( new Link( exportLink, new Predicate() {
  216. @Override
  217. public boolean apply( Place place )
  218. {
  219. return place instanceof ExportPlace;
  220. }
  221. } ) );
  222. links.add( new Link( thresholdLink, new Predicate() {
  223. @Override
  224. public boolean apply( Place place )
  225. {
  226. return place instanceof ThresholdPlace;
  227. }
  228. } ) );
  229. links.add( new Link( navigateLink, new Predicate() {
  230. @Override
  231. public boolean apply( Place place )
  232. {
  233. return place instanceof NavigatePlace;
  234. }
  235. } ) );
  236. links.add( new Link( trackingLink, new Predicate() {
  237. @Override
  238. public boolean apply( Place place )
  239. {
  240. return place instanceof TrackingPlace;
  241. }
  242. } ) );
  243. eventBus.addHandler( PlaceChangeEvent.TYPE, this );
  244. }
  245. public AcceptsOneWidget getContainer()
  246. {
  247. return container;
  248. }
  249. @Override
  250. public void onPlaceChange( PlaceChangeEvent event )
  251. {
  252. Place newPlace = event.getNewPlace();
  253. // select the link corresponding to the place
  254. for ( Link link : links )
  255. {
  256. if ( link.isPlaceMatchLink( newPlace ) )
  257. {
  258. link.getLink().addStyleName( res.style().menuLinkSelected() );
  259. }
  260. else
  261. {
  262. link.getLink().removeStyleName( res.style().menuLinkSelected() );
  263. }
  264. }
  265. // clear the source list because we are going to update it with the raw source file corresponding to the new
  266. // place
  267. sourceList.clear();
  268. if ( newPlace instanceof PlaceWithSources )
  269. {
  270. exampleLink.setVisible( true );
  271. sourceLink.setVisible( true );
  272. PlaceWithSources<?> place = (PlaceWithSources<?>) newPlace;
  273. sourceList.addItem( "Example", place.getSourceFilename() );
  274. String[] rawFilenames = place.getRawSourceFilenames();
  275. if ( null != rawFilenames && rawFilenames.length > 0 )
  276. {
  277. // add the raw source files to the list and show the list
  278. String text = sourceLink.getText();
  279. if ( !text.endsWith( ":" ) )
  280. {
  281. sourceLink.setText( text + ":" );
  282. }
  283. sourceList.setVisible( true );
  284. int indexRawSource = 0;
  285. // starting at 1 because the first item is the example
  286. int i = 1;
  287. for ( String filename : rawFilenames )
  288. {
  289. sourceList.addItem( filename, filename );
  290. if ( place.isRawSource() && filename.equals( place.getFilename() ) )
  291. {
  292. indexRawSource = i;
  293. }
  294. i++;
  295. }
  296. sourceList.setSelectedIndex( indexRawSource );
  297. }
  298. else
  299. {
  300. // no raw source file, we hide the list
  301. String text = sourceLink.getText();
  302. if ( text.endsWith( ":" ) )
  303. {
  304. sourceLink.setText( text.substring( 0, text.length() - 1 ) );
  305. }
  306. sourceList.setVisible( false );
  307. }
  308. if ( null == place.getFilename() )
  309. {
  310. sourceLink.removeStyleName( res.style().sourceLinkSelected() );
  311. exampleLink.addStyleName( res.style().sourceLinkSelected() );
  312. container.getElement().getStyle().clearBackgroundColor();
  313. container.getElement().getStyle().clearProperty( "border" );
  314. }
  315. else
  316. {
  317. exampleLink.removeStyleName( res.style().sourceLinkSelected() );
  318. sourceLink.addStyleName( res.style().sourceLinkSelected() );
  319. container.getElement().getStyle().setBackgroundColor( "#eee" );
  320. container.getElement().getStyle().setProperty( "border", "1px solid #c3c3c3" );
  321. }
  322. }
  323. else
  324. {
  325. // should not happen
  326. exampleLink.setVisible( false );
  327. sourceLink.setVisible( false );
  328. sourceList.setVisible( false );
  329. }
  330. }
  331. @UiHandler( "exampleLink" )
  332. void onClickExampleLink( ClickEvent e )
  333. {
  334. Place currentPlace = placeController.getWhere();
  335. if ( currentPlace instanceof PlaceWithSources )
  336. {
  337. PlaceWithSources<?> place = (PlaceWithSources<?>) currentPlace;
  338. if ( null != place.getFilename() )
  339. {
  340. // we were on the source page, we create a new place from the previous one and go to this new place
  341. placeController.goTo( place.createPlace() );
  342. }
  343. }
  344. }
  345. @UiHandler( "sourceLink" )
  346. void onClickSourceLink( ClickEvent e )
  347. {
  348. Place currentPlace = placeController.getWhere();
  349. if ( currentPlace instanceof PlaceWithSources )
  350. {
  351. PlaceWithSources<?> place = (PlaceWithSources<?>) currentPlace;
  352. // we were on the example page, we create a new place from the previous one and go to this new place
  353. placeController.goTo( place.createPlace( place.getSourceFilename(),
  354. sourceList.isVisible() && sourceList.getSelectedIndex() > 0 ) );
  355. }
  356. }
  357. @UiHandler( "sourceList" )
  358. void onChangeSourceList( ChangeEvent e )
  359. {
  360. Place currentPlace = placeController.getWhere();
  361. if ( currentPlace instanceof PlaceWithSources )
  362. {
  363. PlaceWithSources<?> place = (PlaceWithSources<?>) currentPlace;
  364. int selectedIndex = sourceList.getSelectedIndex();
  365. // we were on the example page, we create a new place from the previous one and go to this new place
  366. placeController.goTo( place.createPlace( sourceList.getValue( selectedIndex ), selectedIndex > 0 ) );
  367. }
  368. }
  369. }