private String getColumnTitle( Field field, MessageSource messageSource, LocaleHolder localeHolder) { ViewColumn viewColumn = field.getAnnotation(ViewColumn.class); return viewColumn != null ? messageSource.getMessage(viewColumn.titleKey(), null, localeHolder.getLocale()) : field.getName(); }
public void initializeComponents( DiscountCardsTableModel tableModel, MessageSource messageSource, LocaleHolder localeHolder) { this.tableModel = tableModel; this.filters = new ArrayList<>(); FiltersPurpose filtersPurpose = tableModel.getFiltersPurpose(); FilterElementsListener filterElementsListener = new FilterElementsListener(); // panel for search operations filtersPanel = new JPanel(); filtersPanel.setBorder( BorderFactory.createCompoundBorder( BorderFactory.createTitledBorder( messageSource.getMessage( "mainForm.panel.search.title", null, localeHolder.getLocale())), BorderFactory.createEmptyBorder())); filtersPanel.setLayout(new GridBagLayout()); GridBagConstraints constraints = new GridBagConstraints(); constraints.fill = GridBagConstraints.HORIZONTAL; constraints.insets = new Insets(1, 5, 1, 5); boolean allCardsPurpose = FiltersPurpose.ALL_DISCOUNT_CARDS == filtersPurpose; for (Field field : DiscountCardJdo.class.getDeclaredFields()) { FilterColumn filterColumn = field.getAnnotation(FilterColumn.class); if (filterColumn != null) { if (allCardsPurpose && filterColumn.showForAllDiscountCards()) { FilterUnit filterUnit = new FilterUnit(); if (allCardsPurpose) { filterUnit.order = filterColumn.orderForAllDiscountCards(); } filterUnit.order = filterUnit.order == 0 ? Integer.MAX_VALUE : filterUnit.order; filterUnit.filterType = filterColumn.type(); filterUnit.columnTitle = getColumnTitle(field, messageSource, localeHolder); filterUnit.columnDatePattern = getColumnDatePattern(field); if (!filterColumn.labelKey().isEmpty()) { filterUnit.label = new JLabel( messageSource.getMessage( filterColumn.labelKey(), null, localeHolder.getLocale())); filterUnit.textField = new JTextField(filterColumn.editSize()); filterUnit.textField.getDocument().addDocumentListener(filterElementsListener); } if (filterColumn.checkBoxesNumber() > 0) { filterUnit.checkBoxes = new JCheckBox[filterColumn.checkBoxesNumber()]; for (int i = 0; i < filterColumn.checkBoxesNumber(); ++i) { String text = messageSource.getMessage( filterColumn.checkBoxesText()[i], null, localeHolder.getLocale()); String actionCommand = messageSource.getMessage( filterColumn.checkBoxesAction()[i], null, localeHolder.getLocale()); filterUnit.checkBoxes[i] = new JCheckBox(text); filterUnit.checkBoxes[i].setActionCommand(actionCommand); if (field.getName().equals("activationDate")) { filterUnit.checkBoxes[i].setSelected(true); } else { filterUnit.checkBoxes[i].setSelected(false); } filterUnit.checkBoxes[i].addItemListener( new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { int state = e.getStateChange(); if (state == ItemEvent.SELECTED) { applyFilters(); updateAnalyzeComponent(); } else if (state == ItemEvent.DESELECTED) { applyFilters(); updateAnalyzeComponent(); } } }); } } filters.add(filterUnit); } } } Collections.sort( filters, new Comparator<FilterUnit>() { @Override public int compare(FilterUnit filterUnit1, FilterUnit filterUnit2) { return filterUnit1.order - filterUnit2.order; } }); for (int i = 0; i < filters.size(); ++i) { if (filters.get(i).label != null) { filters.get(i).label.setLabelFor(filters.get(i).textField); constraints.fill = GridBagConstraints.NONE; constraints.gridwidth = GridBagConstraints.RELATIVE; constraints.anchor = GridBagConstraints.EAST; constraints.weightx = 0.0; filtersPanel.add(filters.get(i).label, constraints); constraints.fill = GridBagConstraints.HORIZONTAL; constraints.gridwidth = GridBagConstraints.REMAINDER; constraints.anchor = GridBagConstraints.EAST; constraints.weightx = 1.0; filtersPanel.add(filters.get(i).textField, constraints); } else if (filters.get(i).checkBoxes != null) { JPanel checkBoxPanel = new JPanel(); checkBoxPanel.setLayout(new BoxLayout(checkBoxPanel, BoxLayout.LINE_AXIS)); checkBoxPanel.add(Box.createHorizontalGlue()); for (int j = 0; j < filters.get(i).checkBoxes.length; ++j) { checkBoxPanel.add(filters.get(i).checkBoxes[j]); checkBoxPanel.add(Box.createHorizontalGlue()); } constraints.fill = GridBagConstraints.HORIZONTAL; constraints.gridwidth = GridBagConstraints.REMAINDER; constraints.anchor = GridBagConstraints.EAST; constraints.weightx = 1.0; filtersPanel.add(checkBoxPanel, constraints); } } JButton clearSearchButton = new JButton( messageSource.getMessage( "mainForm.button.clear.title", null, localeHolder.getLocale())); clearSearchButton.setPreferredSize(new Dimension(500, 20)); clearSearchButton.setMaximumSize(new Dimension(500, 20)); clearSearchButton.setMinimumSize(new Dimension(500, 20)); constraints.gridx = 1; constraints.gridy = filters.size() + 1; constraints.gridwidth = GridBagConstraints.REMAINDER; constraints.anchor = GridBagConstraints.EAST; constraints.weightx = 1.0; clearSearchButton.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { for (FilterUnit filterUnit : filters) { if (filterUnit.label != null) { filterUnit.textField.setText(""); } else if (filterUnit.checkBoxes != null) { for (int j = 0; j < filterUnit.checkBoxes.length; ++j) { filterUnit.checkBoxes[j].setSelected(true); } } } } }); filtersPanel.add(clearSearchButton, constraints); sorter = new TableRowSorter<>(tableModel); cardAnalyzeComponent.initializeComponents(tableModel, messageSource, localeHolder); }