1 15 package org.apache.tapestry.contrib.palette; 16 17 import java.util.ArrayList ; 18 import java.util.Collections ; 19 import java.util.HashMap ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 import java.util.Map ; 23 24 import org.apache.tapestry.BaseComponent; 25 import org.apache.tapestry.IAsset; 26 import org.apache.tapestry.IForm; 27 import org.apache.tapestry.IMarkupWriter; 28 import org.apache.tapestry.IRequestCycle; 29 import org.apache.tapestry.IScript; 30 import org.apache.tapestry.PageRenderSupport; 31 import org.apache.tapestry.Tapestry; 32 import org.apache.tapestry.TapestryUtils; 33 import org.apache.tapestry.components.Block; 34 import org.apache.tapestry.form.IPropertySelectionModel; 35 import org.apache.tapestry.form.RequirableField; 36 import org.apache.tapestry.form.RequirableFieldSupport; 37 import org.apache.tapestry.html.Body; 38 import org.apache.tapestry.valid.IValidationDelegate; 39 import org.apache.tapestry.valid.ValidationStrings; 40 import org.apache.tapestry.valid.ValidatorException; 41 42 209 210 public abstract class Palette extends BaseComponent implements RequirableField 211 { 212 private static final int MAP_SIZE = 7; 213 214 218 private Map _symbols; 219 220 221 public abstract void setAvailableColumn(PaletteColumn column); 222 223 224 public abstract void setSelectedColumn(PaletteColumn column); 225 226 public abstract void setName(String name); 227 228 public abstract void setForm(IForm form); 229 230 231 public abstract void setRequiredMessage(String message); 232 233 protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle) 234 { 235 238 IForm form = TapestryUtils.getForm(cycle, this); 239 240 if (form.wasPrerendered(writer, this)) 241 return; 242 243 setForm(form); 244 245 IValidationDelegate delegate = form.getDelegate(); 246 247 delegate.setFormComponent(this); 248 249 form.getElementId(this); 250 251 RequirableFieldSupport requirableFieldSupport = getRequirableFieldSupport(); 252 253 if (form.isRewinding()) 254 requirableFieldSupport.rewind(this, writer, cycle); 255 256 259 if (!cycle.isRewinding()) 260 { 261 263 _symbols = new HashMap (MAP_SIZE); 264 265 runScript(cycle); 266 267 constructColumns(); 268 269 requirableFieldSupport.render(this, writer, cycle); 270 } 271 272 super.renderComponent(writer, cycle); 273 } 274 275 278 public void bind(IMarkupWriter writer, IRequestCycle cycle) throws ValidatorException 279 { 280 handleSubmission(cycle); 281 } 282 283 286 protected void finishLoad() 287 { 288 setRequiredMessage(ValidationStrings.getMessagePattern(ValidationStrings.REQUIRED_SELECT_FIELD, getPage().getLocale())); 289 } 290 291 294 public String getSubmittedValue(IRequestCycle cycle) 295 { 296 return cycle.getParameter(getName()); 297 } 298 299 protected void cleanupAfterRender(IRequestCycle cycle) 300 { 301 _symbols = null; 302 303 setAvailableColumn(null); 304 setSelectedColumn(null); 305 306 super.cleanupAfterRender(cycle); 307 } 308 309 312 private void runScript(IRequestCycle cycle) 313 { 314 PageRenderSupport pageRenderSupport = TapestryUtils.getPageRenderSupport(cycle, this); 315 316 setImage(pageRenderSupport, cycle, "selectImage", getSelectImage()); 317 setImage(pageRenderSupport, cycle, "selectDisabledImage", getSelectDisabledImage()); 318 setImage(pageRenderSupport, cycle, "deselectImage", getDeselectImage()); 319 setImage(pageRenderSupport, cycle, "deselectDisabledImage", getDeselectDisabledImage()); 320 321 if (isSortUser()) 322 { 323 setImage(pageRenderSupport, cycle, "upImage", getUpImage()); 324 setImage(pageRenderSupport, cycle, "upDisabledImage", getUpDisabledImage()); 325 setImage(pageRenderSupport, cycle, "downImage", getDownImage()); 326 setImage(pageRenderSupport, cycle, "downDisabledImage", getDownDisabledImage()); 327 } 328 329 _symbols.put("palette", this); 330 331 getScript().execute(cycle, pageRenderSupport, _symbols); 332 } 333 334 338 private void setImage(PageRenderSupport pageRenderSupport, IRequestCycle cycle, 339 String symbolName, IAsset asset) 340 { 341 String URL = asset.buildURL(cycle); 342 String reference = pageRenderSupport.getPreloadedImageReference(URL); 343 344 _symbols.put(symbolName, reference); 345 } 346 347 public Map getSymbols() 348 { 349 return _symbols; 350 } 351 352 355 private void constructColumns() 356 { 357 359 List selected = getSelected(); 360 361 if (selected == null) 362 selected = Collections.EMPTY_LIST; 363 364 String sortMode = getSort(); 365 366 boolean sortUser = sortMode.equals(SortMode.USER); 367 368 List selectedOptions = null; 369 370 if (sortUser) 371 { 372 int count = selected.size(); 373 selectedOptions = new ArrayList (count); 374 375 for (int i = 0; i < count; i++) 376 selectedOptions.add(null); 377 } 378 379 PaletteColumn availableColumn = new PaletteColumn((String ) _symbols.get("availableName"), 380 getRows()); 381 PaletteColumn selectedColumn = new PaletteColumn(getName(), getRows()); 382 383 386 IPropertySelectionModel model = getModel(); 387 388 int count = model.getOptionCount(); 389 390 for (int i = 0; i < count; i++) 391 { 392 Object optionValue = model.getOption(i); 393 394 PaletteOption o = new PaletteOption(model.getValue(i), model.getLabel(i)); 395 396 int index = selected.indexOf(optionValue); 397 boolean isSelected = index >= 0; 398 399 if (sortUser && isSelected) 400 { 401 selectedOptions.set(index, o); 402 continue; 403 } 404 405 PaletteColumn c = isSelected ? selectedColumn : availableColumn; 406 407 c.addOption(o); 408 } 409 410 if (sortUser) 411 { 412 Iterator i = selectedOptions.iterator(); 413 while (i.hasNext()) 414 { 415 PaletteOption o = (PaletteOption) i.next(); 416 selectedColumn.addOption(o); 417 } 418 } 419 420 if (sortMode.equals(SortMode.VALUE)) 421 { 422 availableColumn.sortByValue(); 423 selectedColumn.sortByValue(); 424 } 425 else if (sortMode.equals(SortMode.LABEL)) 426 { 427 availableColumn.sortByLabel(); 428 selectedColumn.sortByLabel(); 429 } 430 431 setAvailableColumn(availableColumn); 432 setSelectedColumn(selectedColumn); 433 } 434 435 private void handleSubmission(IRequestCycle cycle) 436 { 437 String [] values = cycle.getParameters(getName()); 438 439 int count = Tapestry.size(values); 440 441 if (count == 0) 442 return; 443 444 List selected = new ArrayList (count); 445 IPropertySelectionModel model = getModel(); 446 447 for (int i = 0; i < count; i++) 448 { 449 String value = values[i]; 450 Object option = model.translateValue(value); 451 452 selected.add(option); 453 } 454 455 setSelected(selected); 456 } 457 458 public boolean isSortUser() 459 { 460 return getSort() == SortMode.USER; 461 } 462 463 466 public String getDisplayName() 467 { 468 return null; 469 } 470 471 public abstract Block getAvailableTitleBlock(); 472 473 public abstract IAsset getDeselectDisabledImage(); 474 475 public abstract IAsset getDeselectImage(); 476 477 public abstract IAsset getDownDisabledImage(); 478 479 public abstract IAsset getDownImage(); 480 481 public abstract IAsset getSelectDisabledImage(); 482 483 public abstract IPropertySelectionModel getModel(); 484 485 public abstract int getRows(); 486 487 public abstract Block getSelectedTitleBlock(); 488 489 public abstract IAsset getSelectImage(); 490 491 public abstract String getSort(); 492 493 public abstract IAsset getUpDisabledImage(); 494 495 public abstract IAsset getUpImage(); 496 497 502 public boolean isDisabled() 503 { 504 return false; 505 } 506 507 508 509 public abstract List getSelected(); 510 511 512 513 public abstract void setSelected(List selected); 514 515 519 public abstract IScript getScript(); 520 521 525 public abstract RequirableFieldSupport getRequirableFieldSupport(); 526 } | Popular Tags |