You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Tables.java 34KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  1. package com.vaadin.tests.components.table;
  2. import java.util.ArrayList;
  3. import java.util.Arrays;
  4. import java.util.LinkedHashMap;
  5. import java.util.List;
  6. import com.vaadin.event.Action;
  7. import com.vaadin.event.Action.Handler;
  8. import com.vaadin.event.ItemClickEvent.ItemClickListener;
  9. import com.vaadin.server.Resource;
  10. import com.vaadin.shared.ui.MultiSelectMode;
  11. import com.vaadin.shared.ui.label.ContentMode;
  12. import com.vaadin.shared.ui.table.CollapseMenuContent;
  13. import com.vaadin.shared.ui.table.TableConstants;
  14. import com.vaadin.tests.components.select.AbstractSelectTestCase;
  15. import com.vaadin.ui.Button;
  16. import com.vaadin.ui.Label;
  17. import com.vaadin.ui.Table;
  18. import com.vaadin.ui.Table.Align;
  19. import com.vaadin.ui.Table.CellStyleGenerator;
  20. import com.vaadin.ui.Table.ColumnGenerator;
  21. import com.vaadin.ui.Table.ColumnHeaderMode;
  22. import com.vaadin.ui.Table.ColumnResizeEvent;
  23. import com.vaadin.ui.Table.ColumnResizeListener;
  24. import com.vaadin.ui.Table.FooterClickEvent;
  25. import com.vaadin.ui.Table.FooterClickListener;
  26. import com.vaadin.ui.Table.GeneratedRow;
  27. import com.vaadin.ui.Table.HeaderClickEvent;
  28. import com.vaadin.ui.Table.HeaderClickListener;
  29. import com.vaadin.ui.Table.RowGenerator;
  30. import com.vaadin.ui.Table.RowHeaderMode;
  31. public class Tables<T extends Table> extends AbstractSelectTestCase<T>
  32. implements ItemClickListener, HeaderClickListener, FooterClickListener,
  33. ColumnResizeListener {
  34. protected static final String CATEGORY_ROWS = "Rows";
  35. private static final String CATEGORY_HEADER = "Header";
  36. private static final String CATEGORY_FOOTER = "Footer";
  37. private static final String CATEGORY_COLUMNS = "Columns";
  38. @SuppressWarnings("unchecked")
  39. @Override
  40. protected Class<T> getTestClass() {
  41. return (Class<T>) Table.class;
  42. }
  43. /* COMMANDS */
  44. private Command<T, Align> columnAlignmentCommand = new Command<T, Align>() {
  45. @Override
  46. public void execute(T c, Align alignment, Object propertyId) {
  47. c.setColumnAlignment(propertyId, alignment);
  48. }
  49. };
  50. private Command<T, Boolean> columnVisibleCommand = new Command<T, Boolean>() {
  51. @Override
  52. public void execute(Table c, Boolean visible, Object propertyId) {
  53. List<Object> visibleColumns = new ArrayList<Object>(Arrays.asList(c
  54. .getVisibleColumns()));
  55. if (visible) {
  56. // Table should really check this... Completely fails without
  57. // the check (#
  58. if (!visibleColumns.contains(propertyId)) {
  59. visibleColumns.add(propertyId);
  60. }
  61. } else {
  62. visibleColumns.remove(propertyId);
  63. }
  64. c.setVisibleColumns(visibleColumns.toArray());
  65. }
  66. };
  67. private Command<T, Boolean> columnCollapsed = new Command<T, Boolean>() {
  68. @Override
  69. public void execute(T c, Boolean collapsed, Object propertyId) {
  70. c.setColumnCollapsed(propertyId, collapsed);
  71. }
  72. };
  73. private Command<T, Boolean> columnCollapsibleCommand = new Command<T, Boolean>() {
  74. @Override
  75. public void execute(T c, Boolean collapsible, Object propertyId) {
  76. c.setColumnCollapsible(propertyId, collapsible);
  77. }
  78. };
  79. protected Command<T, Boolean> columnResizeListenerCommand = new Command<T, Boolean>() {
  80. @Override
  81. public void execute(Table c, Boolean value, Object data) {
  82. if (value) {
  83. c.addListener((ColumnResizeListener) Tables.this);
  84. } else {
  85. c.removeListener((ColumnResizeListener) Tables.this);
  86. }
  87. }
  88. };
  89. protected Command<T, Boolean> headerClickListenerCommand = new Command<T, Boolean>() {
  90. @Override
  91. public void execute(T c, Boolean value, Object data) {
  92. if (value) {
  93. c.addListener((HeaderClickListener) Tables.this);
  94. } else {
  95. c.removeListener((HeaderClickListener) Tables.this);
  96. }
  97. }
  98. };
  99. protected Command<T, Boolean> footerClickListenerCommand = new Command<T, Boolean>() {
  100. @Override
  101. public void execute(Table c, Boolean value, Object data) {
  102. if (value) {
  103. c.addListener((FooterClickListener) Tables.this);
  104. } else {
  105. c.removeListener((FooterClickListener) Tables.this);
  106. }
  107. }
  108. };
  109. protected Command<T, RowHeaderMode> rowHeaderModeCommand = new Command<T, RowHeaderMode>() {
  110. @Override
  111. public void execute(Table c, RowHeaderMode value, Object data) {
  112. if (value == RowHeaderMode.PROPERTY) {
  113. c.setItemCaptionPropertyId("Property 3");
  114. }
  115. c.setRowHeaderMode(value);
  116. }
  117. };
  118. protected Command<T, String> footerTextCommand = new Command<T, String>() {
  119. @Override
  120. public void execute(Table c, String value, Object data) {
  121. for (Object propertyId : c.getContainerPropertyIds()) {
  122. if (value != null) {
  123. c.setColumnFooter(propertyId,
  124. value.replace("{id}", propertyId.toString()));
  125. } else {
  126. c.setColumnFooter(propertyId, null);
  127. }
  128. }
  129. }
  130. };
  131. protected Command<T, Object> alignColumnLeftCommand = new Command<T, Object>() {
  132. @Override
  133. public void execute(T c, Object propertyId, Object data) {
  134. c.setColumnAlignment(propertyId, (Align) data);
  135. }
  136. };
  137. private Command<T, ContextMenu> contextMenuCommand = new Command<T, ContextMenu>() {
  138. @Override
  139. public void execute(T c, final ContextMenu value, Object data) {
  140. c.removeAllActionHandlers();
  141. if (value != null) {
  142. c.addActionHandler(new Handler() {
  143. @Override
  144. public void handleAction(Action action, Object sender,
  145. Object target) {
  146. log("Action " + action.getCaption() + " performed on "
  147. + target);
  148. }
  149. @Override
  150. public Action[] getActions(Object target, Object sender) {
  151. return value.getActions(target, sender);
  152. }
  153. });
  154. }
  155. }
  156. };
  157. private Command<T, Integer> columnWidthCommand = new Command<T, Integer>() {
  158. @Override
  159. public void execute(T c, Integer width, Object propertyId) {
  160. c.setColumnWidth(propertyId, width);
  161. }
  162. };
  163. private Command<T, Resource> columnIconCommand = new Command<T, Resource>() {
  164. @Override
  165. public void execute(T c, Resource icon, Object propertyId) {
  166. c.setColumnIcon(propertyId, icon);
  167. }
  168. };
  169. private Command<T, ColumnHeaderMode> columnHeaderModeCommand = new Command<T, ColumnHeaderMode>() {
  170. @Override
  171. public void execute(T c, ColumnHeaderMode columnHeaderMode, Object data) {
  172. c.setColumnHeaderMode(columnHeaderMode);
  173. }
  174. };
  175. private Command<T, String> columnHeaderCommand = new Command<T, String>() {
  176. @Override
  177. public void execute(T c, String header, Object propertyId) {
  178. c.setColumnHeader(propertyId, header);
  179. }
  180. };
  181. private Command<T, Float> columnExpandRatioCommand = new Command<T, Float>() {
  182. @Override
  183. public void execute(T c, Float expandRatio, Object propertyId) {
  184. c.setColumnExpandRatio(propertyId, expandRatio);
  185. }
  186. };
  187. private class GeneratedColumn {
  188. private Class<?> type;
  189. private String width;
  190. private boolean html;
  191. public GeneratedColumn(Class<?> type, String width, boolean html) {
  192. super();
  193. this.type = type;
  194. this.width = width;
  195. this.html = html;
  196. }
  197. }
  198. String generatedColumnId = "Generated ";
  199. int generatedColumnNextNr = 1;
  200. private Command<T, GeneratedColumn> addGeneratedColumnCommand = new Command<T, GeneratedColumn>() {
  201. @Override
  202. public void execute(T c, final GeneratedColumn col, Object data) {
  203. while (c.getColumnGenerator(generatedColumnId
  204. + generatedColumnNextNr) != null) {
  205. generatedColumnNextNr++;
  206. }
  207. c.addGeneratedColumn(generatedColumnId + generatedColumnNextNr,
  208. new ColumnGenerator() {
  209. @Override
  210. public Object generateCell(Table source, Object itemId,
  211. Object columnId) {
  212. String value = "";
  213. if (col.html) {
  214. value = "<i>" + itemId + "</i>" + "/" + "<b>"
  215. + columnId + "</b>";
  216. } else {
  217. value = itemId + "/" + columnId;
  218. }
  219. if (col.type == Button.class) {
  220. Button b = new Button();
  221. b.setCaption(value);
  222. b.setWidth(col.width);
  223. return b;
  224. } else if (col.type == Label.class) {
  225. Label l = new Label();
  226. l.setWidth(col.width);
  227. if (col.html) {
  228. l.setValue(value);
  229. l.setContentMode(ContentMode.HTML);
  230. } else {
  231. l.setValue(value);
  232. }
  233. return l;
  234. } else if (col.type == String.class) {
  235. return value;
  236. } else if (col.type == Object.class) {
  237. return new Object();
  238. }
  239. return null;
  240. }
  241. });
  242. generatedColumnNextNr++;
  243. createColumnOptions(false);
  244. }
  245. };
  246. private Command<T, Object> removeGeneratedColumnsCommand = new Command<T, Object>() {
  247. @Override
  248. public void execute(T c, Object value, Object data) {
  249. for (int i = 0; i < generatedColumnNextNr; i++) {
  250. String columnId = generatedColumnId + i;
  251. if (c.getColumnGenerator(columnId) != null) {
  252. c.removeGeneratedColumn(columnId);
  253. }
  254. }
  255. createColumnOptions(false);
  256. }
  257. };
  258. private class CellStyleInfo {
  259. private final String styleName;
  260. private final Object itemId;
  261. private final Object propertyId;
  262. public CellStyleInfo(String styleName, Object itemId, Object propertyId) {
  263. this.styleName = styleName;
  264. this.itemId = itemId;
  265. this.propertyId = propertyId;
  266. }
  267. public boolean appliesTo(Object itemId, Object propertyId) {
  268. return (this.itemId != null && this.itemId.equals(itemId))
  269. && (this.propertyId == propertyId || (this.propertyId != null && this.propertyId
  270. .equals(propertyId)));
  271. }
  272. }
  273. private Command<T, CellStyleInfo> cellStyleCommand = new Command<T, CellStyleInfo>() {
  274. @Override
  275. public void execute(T c, final CellStyleInfo cellStyleInfo, Object data) {
  276. if (cellStyleInfo == null) {
  277. c.setCellStyleGenerator(null);
  278. } else {
  279. c.setCellStyleGenerator(new CellStyleGenerator() {
  280. @Override
  281. public String getStyle(Table source, Object itemId,
  282. Object propertyId) {
  283. if (cellStyleInfo.appliesTo(itemId, propertyId)) {
  284. return cellStyleInfo.styleName;
  285. }
  286. return null;
  287. }
  288. });
  289. }
  290. }
  291. };
  292. private class GeneratedRowInfo {
  293. private final int nth;
  294. private final String[] text;
  295. private final boolean isHtml;
  296. public GeneratedRowInfo(int nth, boolean isHtml, String... text) {
  297. this.nth = nth;
  298. this.isHtml = isHtml;
  299. this.text = text;
  300. }
  301. public boolean appliesTo(Object itemId) {
  302. int ix = Integer.valueOf(itemId.toString().substring(5));
  303. return ix % nth == 0;
  304. }
  305. @Override
  306. public String toString() {
  307. return String.format("%d, %s, %s", nth, isHtml ? "true" : "false",
  308. Arrays.toString(text));
  309. }
  310. }
  311. private Command<T, GeneratedRowInfo> rowGeneratorCommand = new Command<T, GeneratedRowInfo>() {
  312. @Override
  313. public void execute(T c, final GeneratedRowInfo generatedRowInfo,
  314. Object data) {
  315. if (generatedRowInfo == null) {
  316. c.setRowGenerator(null);
  317. } else {
  318. c.setRowGenerator(new RowGenerator() {
  319. @Override
  320. public GeneratedRow generateRow(Table table, Object itemId) {
  321. if (generatedRowInfo.appliesTo(itemId)) {
  322. GeneratedRow generatedRow = new GeneratedRow(
  323. generatedRowInfo.text);
  324. generatedRow
  325. .setHtmlContentAllowed(generatedRowInfo.isHtml);
  326. return generatedRow;
  327. }
  328. return null;
  329. }
  330. });
  331. }
  332. }
  333. };
  334. private Command<T, Boolean> setSortEnabledCommand = new Command<T, Boolean>() {
  335. @Override
  336. public void execute(T c, Boolean value, Object data) {
  337. c.setSortDisabled(!value);
  338. }
  339. };
  340. /* COMMANDS END */
  341. @Override
  342. protected void createActions() {
  343. super.createActions();
  344. createPageLengthSelect(CATEGORY_SIZE);
  345. createSelectionModeSelect(CATEGORY_SELECTION);
  346. createValueSelection(CATEGORY_SELECTION);
  347. createItemClickListener(CATEGORY_LISTENERS);
  348. createColumnResizeListenerCheckbox(CATEGORY_LISTENERS);
  349. createHeaderClickListenerCheckbox(CATEGORY_LISTENERS);
  350. createFooterClickListenerCheckbox(CATEGORY_LISTENERS);
  351. createRowHeaderModeSelect(CATEGORY_DATA_SOURCE);
  352. createHeaderVisibilitySelect(CATEGORY_HEADER);
  353. createHeaderTextCheckbox(CATEGORY_HEADER);
  354. createFooterVisibilityCheckbox(CATEGORY_FOOTER);
  355. createFooterTextSelect(CATEGORY_FOOTER);
  356. createColumnReorderingAllowedCheckbox(CATEGORY_FEATURES);
  357. createColumnCollapsingAllowedCheckbox(CATEGORY_FEATURES);
  358. createContextMenuAction(CATEGORY_FEATURES);
  359. createColumnHeaderMode(CATEGORY_FEATURES);
  360. createAddGeneratedColumnAction(CATEGORY_FEATURES);
  361. createCellStyleAction(CATEGORY_FEATURES);
  362. createGeneratedRowAction(CATEGORY_FEATURES);
  363. createBooleanAction("Sort enabled", CATEGORY_FEATURES, true,
  364. setSortEnabledCommand);
  365. createColumnOptions(true);
  366. createCollapsibleMenuContentSelect(CATEGORY_FEATURES);
  367. }
  368. private void createAddGeneratedColumnAction(String categoryFeatures) {
  369. String category = "Generated columns";
  370. createCategory(category, categoryFeatures);
  371. createClickAction("Add Button", category, addGeneratedColumnCommand,
  372. new GeneratedColumn(Button.class, null, false));
  373. createClickAction("Add 200px wide Button", category,
  374. addGeneratedColumnCommand, new GeneratedColumn(Button.class,
  375. "200px", false));
  376. createClickAction("Add 100% wide Button", category,
  377. addGeneratedColumnCommand, new GeneratedColumn(Button.class,
  378. "100%", false));
  379. createClickAction("Add Label", category, addGeneratedColumnCommand,
  380. new GeneratedColumn(Label.class, null, false));
  381. createClickAction("Add 100px Label", category,
  382. addGeneratedColumnCommand, new GeneratedColumn(Label.class,
  383. "100px", false));
  384. createClickAction("Add 100% wide Label", category,
  385. addGeneratedColumnCommand, new GeneratedColumn(Label.class,
  386. "100%", false));
  387. createClickAction("Remove generated columns", category,
  388. removeGeneratedColumnsCommand, null);
  389. createClickAction("Add string as generated column", category,
  390. addGeneratedColumnCommand, new GeneratedColumn(String.class,
  391. "", false));
  392. createClickAction("Add HTML string as generated column", category,
  393. addGeneratedColumnCommand, new GeneratedColumn(String.class,
  394. "", true));
  395. createClickAction("Add 100px HTML Label", category,
  396. addGeneratedColumnCommand, new GeneratedColumn(Label.class,
  397. "100px", true));
  398. createClickAction("Add Object as generated column", category,
  399. addGeneratedColumnCommand, new GeneratedColumn(Object.class,
  400. "", false));
  401. }
  402. private void createCellStyleAction(String categoryFeatures) {
  403. LinkedHashMap<String, CellStyleInfo> options = new LinkedHashMap<String, CellStyleInfo>();
  404. options.put("None", null);
  405. options.put("Red row", new CellStyleInfo(
  406. "tables-test-cell-style-red-row", "Item 2", null));
  407. options.put("Red cell", new CellStyleInfo(
  408. "tables-test-cell-style-red-row", "Item 2", "Property 2"));
  409. createSelectAction("Cell style generator", categoryFeatures, options,
  410. "None", cellStyleCommand, true);
  411. }
  412. private void createGeneratedRowAction(String categoryFeatures) {
  413. LinkedHashMap<String, GeneratedRowInfo> options = new LinkedHashMap<String, GeneratedRowInfo>();
  414. options.put("None", null);
  415. options.put("Every fifth row, spanned", new GeneratedRowInfo(5, false,
  416. "foobarbaz this is a long one that should span."));
  417. int props = getComponent().getContainerPropertyIds().size();
  418. String[] text = new String[props];
  419. for (int ix = 0; ix < props; ix++) {
  420. text[ix] = "foo" + ix;
  421. }
  422. options.put("Every tenth row, no spanning", new GeneratedRowInfo(10,
  423. false, text));
  424. options.put(
  425. "Every eight row, spanned, html formatted",
  426. new GeneratedRowInfo(8, true,
  427. "<b>foo</b> <i>bar</i> <span style='color:red;text-size:0.5em;'>baz</span>"));
  428. options.put("Every row, spanned", new GeneratedRowInfo(1, false,
  429. "spanned"));
  430. createSelectAction("Row generator", categoryFeatures, options, "None",
  431. rowGeneratorCommand, true);
  432. }
  433. private void createColumnHeaderMode(String category) {
  434. LinkedHashMap<String, ColumnHeaderMode> columnHeaderModeOptions = new LinkedHashMap<String, ColumnHeaderMode>();
  435. columnHeaderModeOptions.put("Hidden", ColumnHeaderMode.HIDDEN);
  436. columnHeaderModeOptions.put("Id", ColumnHeaderMode.ID);
  437. columnHeaderModeOptions.put("Explicit", ColumnHeaderMode.EXPLICIT);
  438. columnHeaderModeOptions.put("Explicit defaults id",
  439. ColumnHeaderMode.EXPLICIT_DEFAULTS_ID);
  440. createSelectAction("Column header mode", category,
  441. columnHeaderModeOptions, "Explicit defaults id",
  442. columnHeaderModeCommand);
  443. }
  444. private void createValueSelection(String categorySelection) {
  445. LinkedHashMap<String, Object> options = new LinkedHashMap<String, Object>();
  446. options.put("null", null);
  447. for (int i = 1; i <= 10; i++) {
  448. options.put("Item " + i, "Item " + i);
  449. }
  450. createSelectAction("Value", categorySelection, options, null,
  451. setValueCommand);
  452. }
  453. private void createContextMenuAction(String category) {
  454. LinkedHashMap<String, ContextMenu> options = new LinkedHashMap<String, ContextMenu>();
  455. options.put("None", null);
  456. options.put("Item without icon", new ContextMenu("No icon", null));
  457. ContextMenu cm = new ContextMenu();
  458. cm.addItem("Caption only", null);
  459. cm.addItem("Has icon", ICON_16_USER_PNG_UNCACHEABLE);
  460. options.put("With and without icon", cm);
  461. options.put("Only one large icon", new ContextMenu("Icon",
  462. ICON_64_EMAIL_REPLY_PNG_UNCACHEABLE));
  463. options.put("Empty", new ContextMenu() {
  464. @Override
  465. public Action[] getActions(Object target, Object sender) {
  466. return null;
  467. }
  468. });
  469. options.put("Edit/New", new ContextMenu() {
  470. @Override
  471. public Action[] getActions(Object itemId, Object component) {
  472. if (itemId == null) {
  473. return new Action[] { new Action("New..."),
  474. new Action("Common action") };
  475. } else {
  476. return new Action[] { new Action("Edit " + itemId),
  477. new Action("Common action") };
  478. }
  479. }
  480. });
  481. createSelectAction("Context menu", category, options, "None",
  482. contextMenuCommand, true);
  483. }
  484. private void createColumnReorderingAllowedCheckbox(String category) {
  485. createBooleanAction("Column reordering allowed", category, true,
  486. new Command<T, Boolean>() {
  487. @Override
  488. public void execute(Table c, Boolean value, Object data) {
  489. c.setColumnReorderingAllowed(value);
  490. }
  491. });
  492. }
  493. private void createColumnCollapsingAllowedCheckbox(String category) {
  494. createBooleanAction("Column collapsing allowed", category, true,
  495. new Command<T, Boolean>() {
  496. @Override
  497. public void execute(T c, Boolean value, Object data) {
  498. c.setColumnCollapsingAllowed(value);
  499. }
  500. });
  501. }
  502. private void createCollapsibleMenuContentSelect(String category) {
  503. createSelectAction("Collapsible menu content", category,
  504. CollapseMenuContent.class,
  505. TableConstants.DEFAULT_COLLAPSE_MENU_CONTENT,
  506. new Command<T, CollapseMenuContent>() {
  507. @Override
  508. public void execute(T c, CollapseMenuContent value,
  509. Object data) {
  510. c.setCollapseMenuContent(value);
  511. }
  512. });
  513. }
  514. private void createColumnOptions(boolean init) {
  515. if (!init && !hasCategory(CATEGORY_COLUMNS)) {
  516. return;
  517. }
  518. long start = System.currentTimeMillis();
  519. if (!init) {
  520. removeCategory(CATEGORY_COLUMNS);
  521. }
  522. for (Object id : getComponent().getContainerPropertyIds()) {
  523. String name = id.toString();
  524. createCategory(name, CATEGORY_COLUMNS);
  525. createColumnOption(name, id);
  526. }
  527. for (int i = 0; i < generatedColumnNextNr; i++) {
  528. String id = generatedColumnId + i;
  529. String name = id;
  530. if (getTestComponents().get(0).getColumnGenerator(id) != null) {
  531. createCategory(name, CATEGORY_COLUMNS);
  532. createColumnOption(name, id);
  533. }
  534. }
  535. long end = System.currentTimeMillis();
  536. System.err.println("Create options took " + (end - start) + "ms");
  537. }
  538. private class Timer {
  539. private long start, last;
  540. private Timer() {
  541. start = System.currentTimeMillis();
  542. last = System.currentTimeMillis();
  543. }
  544. public void log(String msg) {
  545. long now = System.currentTimeMillis();
  546. System.err.println("[This: " + (now - last) + "ms, total: "
  547. + (now - start) + "ms]: " + msg);
  548. last = now;
  549. }
  550. }
  551. private void createColumnOption(String category, Object propertyId) {
  552. Timer t = new Timer();
  553. createBooleanAction("Visible", category, true, columnVisibleCommand,
  554. propertyId);
  555. t.log("Visible");
  556. createBooleanAction("Collapsed", category, false, columnCollapsed,
  557. propertyId);
  558. t.log("Collapsed");
  559. LinkedHashMap<String, Align> options = new LinkedHashMap<String, Align>();
  560. options.put("Left", Align.LEFT);
  561. options.put("Center", Align.CENTER);
  562. options.put("Right", Align.RIGHT);
  563. createSelectAction("Alignment", category, options, "Left",
  564. columnAlignmentCommand, propertyId);
  565. t.log("Alignment");
  566. LinkedHashMap<String, Integer> widthOptions = new LinkedHashMap<String, Integer>();
  567. widthOptions.put("- remove -", -1);
  568. for (int i : new int[] { 0, 1, 10, 100, 200, 400 }) {
  569. widthOptions.put(i + "px", i);
  570. }
  571. createSelectAction("Width", category, widthOptions, "- remove -",
  572. columnWidthCommand, propertyId);
  573. t.log("Width");
  574. LinkedHashMap<String, Resource> iconOptions = new LinkedHashMap<String, Resource>();
  575. iconOptions.put("- none -", null);
  576. iconOptions.put("ok 16x16", ICON_16_USER_PNG_CACHEABLE);
  577. iconOptions.put("help 16x16", ICON_16_HELP_PNG_CACHEABLE);
  578. iconOptions.put("folder 16x16", ICON_16_FOLDER_PNG_CACHEABLE);
  579. iconOptions.put("attention 32x32", ICON_32_ATTENTION_PNG_CACHEABLE);
  580. createSelectAction("Icon", category, iconOptions, "- none -",
  581. columnIconCommand, propertyId);
  582. t.log("Icon");
  583. LinkedHashMap<String, String> columnHeaderOptions = new LinkedHashMap<String, String>();
  584. columnHeaderOptions.put("- none -", null);
  585. columnHeaderOptions.put("A", "A");
  586. columnHeaderOptions.put("A nice column", "A nice column");
  587. createSelectAction("Column header", category, columnHeaderOptions,
  588. "- none -", columnHeaderCommand, propertyId);
  589. t.log("Header");
  590. LinkedHashMap<String, Float> expandOptions = new LinkedHashMap<String, Float>();
  591. expandOptions.put("- remove -", -1f);
  592. for (float i : new float[] { 0, 1, 2, 3, 4, 5 }) {
  593. expandOptions.put(i + "", i);
  594. }
  595. createSelectAction("Expand ratio", category, expandOptions,
  596. "- remove -", columnExpandRatioCommand, propertyId);
  597. t.log("Expand");
  598. createBooleanAction("Collapsible", category, true,
  599. columnCollapsibleCommand, propertyId);
  600. // Footer text (move)
  601. // Header text (move)
  602. }
  603. private void createRowHeaderModeSelect(String category) {
  604. LinkedHashMap<String, RowHeaderMode> options = new LinkedHashMap<String, RowHeaderMode>();
  605. options.put("Explicit", RowHeaderMode.EXPLICIT);
  606. options.put("Explicit defaults id", RowHeaderMode.EXPLICIT_DEFAULTS_ID);
  607. options.put("Hidden", RowHeaderMode.HIDDEN);
  608. options.put("Icon only", RowHeaderMode.ICON_ONLY);
  609. options.put("Id", RowHeaderMode.ID);
  610. options.put("Index", RowHeaderMode.INDEX);
  611. options.put("Item", RowHeaderMode.ITEM);
  612. options.put("'Property 3' property", RowHeaderMode.PROPERTY);
  613. createSelectAction("Row header mode", category, options, "Hidden",
  614. rowHeaderModeCommand);
  615. }
  616. private void createFooterTextSelect(String category) {
  617. LinkedHashMap<String, String> options = new LinkedHashMap<String, String>();
  618. options.put("None", null);
  619. options.put("Footer X", "Footer {id}");
  620. options.put("X", "{id}");
  621. createSelectAction("Texts in footer", category, options, "None",
  622. footerTextCommand);
  623. }
  624. private void createHeaderTextCheckbox(String category) {
  625. LinkedHashMap<String, String> options = new LinkedHashMap<String, String>();
  626. options.put("None", null);
  627. options.put("Col: {id}", "Col: {id}");
  628. options.put("Header {id} - every second", "Header {id}");
  629. createSelectAction("Texts in header", category, options, "None",
  630. new Command<T, String>() {
  631. @Override
  632. public void execute(T c, String value, Object data) {
  633. int nr = 0;
  634. for (Object propertyId : c.getContainerPropertyIds()) {
  635. nr++;
  636. if (value != null && value.equals("Header {id}")
  637. && nr % 2 == 0) {
  638. c.setColumnHeader(propertyId, null);
  639. } else if (value != null) {
  640. c.setColumnHeader(
  641. propertyId,
  642. value.replace("{id}",
  643. propertyId.toString()));
  644. } else {
  645. c.setColumnHeader(propertyId, null);
  646. }
  647. }
  648. }
  649. });
  650. }
  651. private void createHeaderClickListenerCheckbox(String category) {
  652. createBooleanAction("Header click listener", category, false,
  653. headerClickListenerCommand);
  654. }
  655. private void createFooterClickListenerCheckbox(String category) {
  656. createBooleanAction("Footer click listener", category, false,
  657. footerClickListenerCommand);
  658. }
  659. private void createColumnResizeListenerCheckbox(String category) {
  660. createBooleanAction("Column resize listener", category, false,
  661. columnResizeListenerCommand);
  662. }
  663. // TODO:
  664. // setCurrentPageFirstItemIndex()
  665. // Editable
  666. // Cache rate
  667. // CurrentPageFirstItemId
  668. protected void createFooterVisibilityCheckbox(String category) {
  669. createBooleanAction("Footer visible", category, true,
  670. new Command<T, Boolean>() {
  671. @Override
  672. public void execute(T c, Boolean value, Object data) {
  673. c.setFooterVisible(value);
  674. }
  675. });
  676. }
  677. protected void createHeaderVisibilitySelect(String category) {
  678. LinkedHashMap<String, ColumnHeaderMode> options = new LinkedHashMap<String, ColumnHeaderMode>();
  679. options.put("Explicit", ColumnHeaderMode.EXPLICIT);
  680. options.put("Explicit defaults id",
  681. ColumnHeaderMode.EXPLICIT_DEFAULTS_ID);
  682. options.put("Id", ColumnHeaderMode.ID);
  683. options.put("Hidden", ColumnHeaderMode.HIDDEN);
  684. createSelectAction("Header mode", category, options,
  685. "Explicit defaults id", new Command<T, ColumnHeaderMode>() {
  686. @Override
  687. public void execute(T c, ColumnHeaderMode value, Object data) {
  688. c.setColumnHeaderMode(value);
  689. }
  690. });
  691. }
  692. protected void createPageLengthSelect(String category) {
  693. LinkedHashMap<String, Integer> options = new LinkedHashMap<String, Integer>();
  694. options.put("0", 0);
  695. options.put("5", 5);
  696. options.put("10", 10);
  697. options.put("20", 20);
  698. options.put("50", 50);
  699. createSelectAction("PageLength", category, options, "10",
  700. new Command<T, Integer>() {
  701. @Override
  702. public void execute(Table t, Integer value, Object data) {
  703. t.setPageLength(value);
  704. }
  705. });
  706. }
  707. private enum SelectMode {
  708. NONE, SINGLE, MULTI_SIMPLE, MULTI;
  709. }
  710. protected void createSelectionModeSelect(String category) {
  711. LinkedHashMap<String, SelectMode> options = new LinkedHashMap<String, SelectMode>();
  712. options.put("None", SelectMode.NONE);
  713. options.put("Single", SelectMode.SINGLE);
  714. options.put("Multi - simple", SelectMode.MULTI_SIMPLE);
  715. options.put("Multi - ctrl/shift", SelectMode.MULTI);
  716. createSelectAction("Selection Mode", category, options,
  717. "Multi - ctrl/shift", new Command<T, SelectMode>() {
  718. @Override
  719. public void execute(Table t, SelectMode value, Object data) {
  720. switch (value) {
  721. case NONE:
  722. t.setSelectable(false);
  723. break;
  724. case SINGLE:
  725. t.setMultiSelect(false);
  726. t.setSelectable(true);
  727. break;
  728. case MULTI_SIMPLE:
  729. t.setSelectable(true);
  730. t.setMultiSelect(true);
  731. t.setMultiSelectMode(MultiSelectMode.SIMPLE);
  732. break;
  733. case MULTI:
  734. t.setSelectable(true);
  735. t.setMultiSelect(true);
  736. t.setMultiSelectMode(MultiSelectMode.DEFAULT);
  737. break;
  738. }
  739. }
  740. });
  741. }
  742. @Override
  743. public void columnResize(ColumnResizeEvent event) {
  744. log("ColumnResize on " + event.getPropertyId() + " from "
  745. + event.getPreviousWidth() + " to " + event.getCurrentWidth());
  746. }
  747. @Override
  748. public void footerClick(FooterClickEvent event) {
  749. log("FooterClick on " + event.getPropertyId() + " using "
  750. + event.getButtonName());
  751. }
  752. @Override
  753. public void headerClick(HeaderClickEvent event) {
  754. log("HeaderClick on " + event.getPropertyId() + " using "
  755. + event.getButtonName());
  756. }
  757. @Override
  758. protected void updateContainer() {
  759. super.updateContainer();
  760. // Recreate for the new properties
  761. createColumnOptions(false);
  762. }
  763. }