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

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