1 18 package org.apache.beehive.netui.databinding.datagrid.api.rendering; 19 20 import java.util.HashMap ; 21 import javax.servlet.jsp.JspContext ; 22 import javax.servlet.http.HttpServletRequest ; 23 24 import org.apache.beehive.netui.databinding.datagrid.api.sort.SortDirection; 25 import org.apache.beehive.netui.databinding.datagrid.api.DataGridResourceProvider; 26 import org.apache.beehive.netui.databinding.datagrid.api.DataGridConfig; 27 import org.apache.beehive.netui.databinding.datagrid.api.DataGridURLBuilder; 28 import org.apache.beehive.netui.databinding.datagrid.api.DataGridState; 29 import org.apache.beehive.netui.databinding.datagrid.api.DataGridStateFactory; 30 import org.apache.beehive.netui.databinding.datagrid.runtime.rendering.table.TableRenderer; 31 import org.apache.beehive.netui.databinding.datagrid.runtime.util.PagedDataSet; 32 import org.apache.beehive.netui.databinding.datagrid.runtime.util.JspUtil; 33 import org.apache.beehive.netui.tags.rendering.AbstractRenderAppender; 34 import org.apache.beehive.netui.util.Bundle; 35 36 43 public class DataGridTagModel { 44 45 48 public static final int RENDER_STATE_START = 10; 49 50 53 public static final int RENDER_STATE_CAPTION = 20; 54 55 58 public static final int RENDER_STATE_HEADER = 30; 59 60 63 public static final int RENDER_STATE_GRID = 40; 64 65 68 public static final int RENDER_STATE_FOOTER = 50; 69 70 73 public static final int RENDER_STATE_END = 60; 74 75 private static final int RENDER_STATE_UNINIT = -1; 76 77 private int _renderState = RENDER_STATE_UNINIT; 78 private boolean _disableDefaultPagerRendering = false; 79 private boolean _renderRowGroups = false; 80 private String _name = null; 81 82 private JspContext _jspContext = null; 83 private HttpServletRequest _request = null; 84 private StyleModel _styleModel = null; 85 private PagerRenderer _pagerRenderer = null; 86 private PagedDataSet _dataSet = null; 87 private TableRenderer _tableRenderer = null; 88 private DataGridResourceProvider _resourceProvider = null; 89 private DataGridState _dataGridState = null; 90 private DataGridURLBuilder _urlBuilder = null; 91 private DataGridConfig _dataGridConfig = null; 92 93 100 public DataGridTagModel(String name, DataGridConfig dataGridConfig, JspContext jspContext) { 101 super(); 102 103 _jspContext = jspContext; 104 _request = JspUtil.getRequest(_jspContext); 105 106 _name = name; 107 _dataGridConfig = dataGridConfig; 108 109 DataGridStateFactory stateFactory = DataGridStateFactory.getInstance(_jspContext); 110 _dataGridState = stateFactory.getDataGridState(_name, _dataGridConfig); 111 _urlBuilder = stateFactory.getDataGridURLBuilder(_name, _dataGridConfig); 112 } 113 114 119 public String getName() { 120 return _name; 121 } 122 123 128 public JspContext getJspContext() { 129 return _jspContext; 130 } 131 132 149 public int getRenderState() { 150 return _renderState; 151 } 152 153 171 public void changeRenderState(int renderState) { 172 switch(_renderState) { 173 case RENDER_STATE_UNINIT: 174 _renderState = RENDER_STATE_START; 175 break; 176 case RENDER_STATE_START: 177 _renderState = RENDER_STATE_CAPTION; 178 break; 179 case RENDER_STATE_CAPTION: 180 _renderState = RENDER_STATE_HEADER; 181 break; 182 case RENDER_STATE_HEADER: 183 _renderState = RENDER_STATE_GRID; 184 break; 185 case RENDER_STATE_GRID: 186 _renderState = RENDER_STATE_FOOTER; 187 break; 188 case RENDER_STATE_FOOTER: 189 _renderState = RENDER_STATE_END; 190 break; 191 default: 192 throw new IllegalStateException (Bundle.getErrorString("DataGridTagModel_InvalidStateTransition")); 193 } 194 195 return; 196 } 197 198 203 public PagedDataSet getDataSet() { 204 return _dataSet; 205 } 206 207 214 public void setDataSet(PagedDataSet dataSet) { 215 216 _dataSet = dataSet; 217 _dataGridState.getPagerModel().setDataSetSize(_dataSet.getSize()); 218 } 219 220 226 public PagerRenderer getPagerRenderer() { 227 if(_pagerRenderer == null) 228 setPagerRenderer(_dataGridConfig.getDefaultPagerRenderer()); 229 230 return _pagerRenderer; 231 } 232 233 238 public void setPagerRenderer(PagerRenderer pagerRenderer) { 239 240 _pagerRenderer = pagerRenderer; 241 _pagerRenderer.setDataGridTagModel(this); 242 } 243 244 250 public DataGridResourceProvider getResourceProvider() { 251 return _resourceProvider; 252 } 253 254 258 public void setResourceProvider(DataGridResourceProvider resourceProvider) { 259 _resourceProvider = resourceProvider; 260 } 261 262 267 public boolean isDisableDefaultPagerRendering() { 268 return _disableDefaultPagerRendering; 269 } 270 271 278 public void setDisableDefaultPagerRendering(boolean disableDefaultPagerRendering) { 279 _disableDefaultPagerRendering = disableDefaultPagerRendering; 280 } 281 282 292 public boolean isRenderRowGroups() { 293 return _renderRowGroups; 294 } 295 296 301 public void setRenderRowGroups(boolean renderRowGroups) { 302 _renderRowGroups = renderRowGroups; 303 } 304 305 309 public TableRenderer getTableRenderer() { 310 return _tableRenderer; 311 } 312 313 317 public void setTableRenderer(TableRenderer tableRenderer) { 318 _tableRenderer = tableRenderer; 319 } 320 321 325 public StyleModel getStyleModel() { 326 return _styleModel; 327 } 328 329 333 public void setStyleModel(StyleModel styleModel) { 334 _styleModel = styleModel; 335 } 336 337 342 public String getMessage(String key) { 343 assert _resourceProvider != null : "Received a null resource provider"; 344 return _resourceProvider.getMessage(key); 345 } 346 347 354 public String formatMessage(String key, Object [] args) { 355 assert _resourceProvider != null : "Received a null resource provider"; 356 return _resourceProvider.formatMessage(key, args); 357 } 358 359 369 public void addResourceOverride(String key, String value) { 370 OverridableDataGridResourceProvider overrideResourceProvider = null; 371 if(!(_resourceProvider instanceof OverridableDataGridResourceProvider)) { 372 overrideResourceProvider = new OverridableDataGridResourceProvider(_resourceProvider); 373 _resourceProvider = overrideResourceProvider; 374 } 375 else { 376 assert _resourceProvider instanceof OverridableDataGridResourceProvider; 377 overrideResourceProvider = (OverridableDataGridResourceProvider)_resourceProvider; 378 } 379 380 overrideResourceProvider.addResourceOverride(key, value); 381 } 382 383 391 public String getResourcePath() { 392 393 return _request.getContextPath() + "/" + getMessage(IDataGridMessageKeys.DATAGRID_RESOURCE_PATH); 394 } 395 396 413 public String getSortImagePath(SortDirection sortDirection) { 414 415 if(sortDirection == SortDirection.ASCENDING) 416 return getMessage(IDataGridMessageKeys.SORT_ASC_IMAGE_PATH); 417 else if(sortDirection == SortDirection.DESCENDING) 418 return getMessage(IDataGridMessageKeys.SORT_DESC_IMAGE_PATH); 419 else { 420 assert sortDirection == SortDirection.NONE : "Encountered an invalid sort direction."; 421 return getDefaultSortImagePath(); 422 } 423 } 424 425 431 public String getDefaultSortImagePath() { 432 return getMessage(IDataGridMessageKeys.SORT_NONE_IMAGE_PATH); 433 } 434 435 440 public void renderPager(AbstractRenderAppender appender) { 441 if(getPagerRenderer() != null) 442 appender.append(getPagerRenderer().render()); 443 } 444 445 452 public DataGridState getState() { 453 return _dataGridState; 454 } 455 456 463 public DataGridURLBuilder getUrlBuilder() { 464 return _urlBuilder; 465 } 466 467 476 public int getCurrentIndex() { 477 assert _dataSet != null; 478 return _dataSet.getCurrentIndex(); 479 } 480 481 486 public Object getCurrentItem() { 487 assert _dataSet != null; 488 return _dataSet.getCurrentItem(); 489 } 490 491 496 public String getDataSource() { 497 assert _dataSet != null; 498 return _dataSet.getDataSource(); 499 } 500 501 507 public Object nextDataItem() { 508 assert _dataSet != null; 509 return _dataSet.next(); 510 } 511 512 517 public boolean hasNextDataItem() { 518 assert _dataSet != null; 519 return _dataSet.hasNext(); 520 } 521 522 527 528 537 private final class OverridableDataGridResourceProvider 538 extends DataGridResourceProvider { 539 540 private DataGridResourceProvider _delegate = null; 541 private HashMap _resourceOverrides = null; 542 543 private OverridableDataGridResourceProvider(DataGridResourceProvider resourceProvider) { 544 _delegate = resourceProvider; 545 } 546 547 private void addResourceOverride(String key, String value) { 548 if(_resourceOverrides == null) 549 _resourceOverrides = new HashMap (); 550 551 552 _resourceOverrides.put(key, value); 553 } 554 555 public String getMessage(String key) { 556 String msg = internalGetMessage(key); 557 if(msg != null) 558 return msg; 559 else 560 return _delegate.getMessage(key); 561 } 562 563 public String formatMessage(String key, Object [] args) { 564 String pattern = internalGetMessage(key); 565 if(pattern != null) 566 return internalFormatMessage(pattern, args); 567 else 568 return _delegate.formatMessage(key, args); 569 } 570 571 private final String internalGetMessage(String key) { 572 if(_resourceOverrides != null && _resourceOverrides.containsKey(key)) 573 return (String )_resourceOverrides.get(key); 574 else 575 return null; 576 } 577 } 578 } 579 | Popular Tags |