package nl.fontys.sofa.limo.view.custom.panel; import java.awt.BorderLayout; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.event.ActionEvent; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.util.ArrayList; import java.util.List; import javax.swing.BoxLayout; import javax.swing.DefaultCellEditor; import javax.swing.DefaultComboBoxModel; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.TableColumn; import nl.fontys.sofa.limo.api.service.provider.EventService; import nl.fontys.sofa.limo.domain.component.event.Event; import nl.fontys.sofa.limo.domain.component.event.ExecutionState; import nl.fontys.sofa.limo.view.custom.table.EventTableModel; import nl.fontys.sofa.limo.view.util.IconUtil; import nl.fontys.sofa.limo.view.util.LIMOResourceBundle; import nl.fontys.sofa.limo.view.wizard.event.EventWizardAction; import org.openide.util.Lookup; /** * Base class for wizards or property sheets that have to add events to a domain * object. The table model and the add action has to be implemented by the * implementing class. * * @author Sven Mäurer */ public abstract class EventsPanel extends JPanel { protected JTable eventsTable; protected JButton addButton; protected JButton deleteButton; protected JButton editButton; protected JComboBox<Event> eventsComboBox; protected JComboBox<ExecutionState> executionStateComboBox; protected EventService service; protected List<Event> allEvents; protected EventTableModel eventsTableModel; protected DefaultComboBoxModel eventsComboBoxModel; protected JPanel sidebarPanel; public EventsPanel() { initComponents(); } private void initComponents() { assignComponents(); initEventService(); buildView(); setTable(); setAddButtonListener(); setEditButtonListener(); setDeleteButtonListener(); checkButtonsState(); } /** * Initialize the view components. */ protected void assignComponents() { eventsComboBox = new JComboBox(); eventsTableModel = new EventTableModel(); eventsTable = new JTable(eventsTableModel); executionStateComboBox = new JComboBox<>(ExecutionState.values()); addButton = new JButton(new ImageIcon(IconUtil.getIcon(IconUtil.UI_ICON.VALID))); editButton = new JButton(new ImageIcon(IconUtil.getIcon(IconUtil.UI_ICON.EDIT))); deleteButton = new JButton(new ImageIcon(IconUtil.getIcon(IconUtil.UI_ICON.TRASH))); } private void initEventService() { service = Lookup.getDefault().lookup(EventService.class); allEvents = service.findAll(); } protected void checkButtonsState() { deleteButton.setEnabled(eventsTableModel.getRowCount() > 0 && eventsTable.getSelectedRow() != -1); editButton.setEnabled(eventsTableModel.getRowCount() > 0 && eventsTable.getSelectedRow() != -1); addButton.setEnabled(eventsComboBox.getModel().getSize() > 0); } public List<Event> getEvents() { return eventsTableModel.getEvents(); } /** * The add action where you have to add the event to the table model and * adapt the view status. */ protected abstract void setAddButtonListener(); /** * The edit action where you have to edit the event to the table model and * adapt the view status. */ protected void setEditButtonListener() { editButton.addActionListener((ActionEvent e) -> { if (eventsTable.getSelectedRow() >= 0) { Event editEvent = eventsTableModel.getEvents().get(eventsTable.getSelectedRow()); EventWizardAction wiz = new EventWizardAction(true); wiz.setEvent(editEvent); wiz.actionPerformed(null); eventsTableModel.fireTableDataChanged(); } }); } private void setTable() { TableColumn dependencyCol = eventsTable.getColumnModel().getColumn(1); dependencyCol.setCellEditor(new DefaultCellEditor(executionStateComboBox)); setTableModel(); } /** * Set the table model based on the parts of the event you need. */ protected abstract void setTableModel(); /** * Delete an event from the table model and adapt the view status. */ private void setDeleteButtonListener() { deleteButton.addActionListener((ActionEvent e) -> { if (eventsTable.getSelectedRow() >= 0) { eventsTableModel.getEvents().remove(eventsTable.getSelectedRow()); eventsTableModel.fireTableDataChanged(); checkButtonsState(); } }); } /** * Set the events as the events of the table model. * * @param events to be used. */ public void update(List<Event> events) { eventsTableModel.getEvents().clear(); eventsTableModel.setEvents(events); eventsTableModel.fireTableDataChanged(); checkButtonsState(); } private void buildView() { setLayout(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); c.fill = GridBagConstraints.HORIZONTAL; c.weightx = 0.2; c.gridx = 0; c.gridy = 0; c.gridwidth = 1; add(new JLabel(LIMOResourceBundle.getString("EVENT")), c); c.weightx = 0.7; c.gridx = 1; c.gridy = 0; add(eventsComboBox, c); JPanel panel = new JPanel(new BorderLayout()); panel.add(new JScrollPane(eventsTable), BorderLayout.CENTER); eventsTable.addMouseListener(new MouseListener() { @Override public void mouseClicked(MouseEvent e) { if (e.getSource().equals(eventsTable)) { checkButtonsState(); } } @Override public void mousePressed(MouseEvent e) { //Not requierd } @Override public void mouseReleased(MouseEvent e) { //Not requierd } @Override public void mouseEntered(MouseEvent e) { //Not requierd } @Override public void mouseExited(MouseEvent e) { //Not requierd } }); sidebarPanel = new JPanel(); sidebarPanel.setLayout(new BoxLayout(sidebarPanel, BoxLayout.Y_AXIS)); sidebarPanel.add(addButton); sidebarPanel.add(editButton); sidebarPanel.add(deleteButton); panel.add(sidebarPanel, BorderLayout.EAST); c.weightx = 1; c.gridx = 0; c.gridy = 1; c.gridwidth = 5; add(panel, c); } protected void setTableAndCheckbox() { ArrayList<String> allEventsName = new ArrayList<>(); List<Event> usedEvents; if (eventsTableModel.getEvents() != null) { usedEvents = new ArrayList<>(eventsTableModel.getEvents()); } else { usedEvents = new ArrayList<>(); } if (allEvents != null) { for (Event event : allEvents) { boolean valid = true; for (Event used : usedEvents) { if (event.getName() != null && used.getName() != null) { valid = !event.getName().equals(used.getName()); } if (!valid) { break; } } if (valid) { allEventsName.add(event.getName()); } } addButton.setEnabled(!allEvents.isEmpty()); eventsComboBox.setModel(new DefaultComboBoxModel(allEventsName.toArray())); } else { allEvents = new ArrayList<>(); eventsComboBox.setModel(new DefaultComboBoxModel(new String[]{})); } } }