Search in sources :

Example 1 with TriggerBindingConfigurationModel

use of com.redhat.devtools.intellij.tektoncd.utils.model.resources.TriggerBindingConfigurationModel in project intellij-tekton by redhat-developer.

the class TriggerStep method setContent.

public void setContent(Map<String, String> triggerBindingTemplates) {
    final int[] row = { 0 };
    String infoText = "<html>The following list allows you to associate one or more bindings to the event-listener ";
    infoText += "which will be created eventually. You are allowed to select none, one or many bindings. ";
    infoText += "Only newly-created bindings that are selected will be actually pushed on cluster.</html>";
    JLabel lblInfoText = new JLabel(infoText);
    addComponent(lblInfoText, defaultLabelFont, new EmptyBorder(10, 0, 10, 0), new Dimension(594, 91), buildGridBagConstraints(0, row[0], 2, GridBagConstraints.NORTHWEST, null));
    row[0] += 1;
    JLabel lblExistingBindings = new JLabel("<html><span style=\\\"font-family:serif;font-size:10px;font-weight:bold;\\\">Select one or more TriggerBindings</span></html>");
    addComponent(lblExistingBindings, null, MARGIN_BOTTOM_10, null, 0, row[0], GridBagConstraints.NORTHWEST);
    row[0] += 1;
    listBindingsAvailableOnCluster = new JBList();
    listBindingsAvailableOnCluster.setLayoutOrientation(JList.VERTICAL);
    listBindingsAvailableOnCluster.setBorder(new EmptyBorder(5, 2, 2, 2));
    // show name as tooltip
    listBindingsAvailableOnCluster.addMouseMotionListener(new MouseMotionAdapter() {

        @Override
        public void mouseMoved(MouseEvent e) {
            JList sourceList = (JList) e.getSource();
            int index = sourceList.locationToIndex(e.getPoint());
            if (index > -1) {
                sourceList.setToolTipText(sourceList.getModel().getElementAt(index).toString());
            }
        }
    });
    listBindingsAvailableOnCluster.setCellRenderer(new DefaultListCellRenderer() {

        @Override
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
            JLabel label = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            if (label.getText().endsWith(" NEW")) {
                String beautifierText = label.getText().replace(" NEW", "");
                label.setText(beautifierText);
                label.setIcon(AllIcons.Actions.New);
            }
            return label;
        }
    });
    fillAvailableBindingsList(null);
    JScrollPane outerListScrollPane = new JBScrollPane();
    outerListScrollPane.setViewportView(listBindingsAvailableOnCluster);
    outerListScrollPane.setPreferredSize(new Dimension(200, 350));
    // actions column
    btnAdd = createActionButton(AllIcons.General.Add, AllIcons.General.InlineAdd, "Add new binding");
    JButton btnRemove = createActionButton(AllIcons.General.Remove, IconLoader.getDisabledIcon(AllIcons.General.Remove), "Remove a newly-created selected binding");
    btnRemove.setEnabled(false);
    JButton btnFind = createActionButton(AllIcons.Actions.Find, IconLoader.getDisabledIcon(AllIcons.Actions.Find), "Show selected binding");
    btnFind.setEnabled(false);
    Box box = Box.createVerticalBox();
    box.add(btnAdd);
    box.add(btnRemove);
    box.add(btnFind);
    // right panel
    JPanel rightPanel = new JPanel();
    rightPanel.setBackground(backgroundTheme);
    // description panel
    JLabel lblDescriptionNewBinding = createDescriptionLabel("Add a new binding", AllIcons.General.Add, SwingConstants.LEFT, BORDER_LABEL_NAME);
    lblDescriptionNewBinding.setCursor(new Cursor(Cursor.HAND_CURSOR));
    JLabel lblDescriptionRemoveBinding = createDescriptionLabel("Remove a newly-created selected binding", AllIcons.General.Remove, SwingConstants.LEFT, BORDER_LABEL_NAME);
    JLabel lblDescriptionFindBinding = createDescriptionLabel("Show the content of the selected binding", AllIcons.Actions.Find, SwingConstants.LEFT, BORDER_LABEL_NAME);
    JLabel lblDescriptionGeneral1 = createDescriptionLabel("Select none or one or many bindings", null, -1, BORDER_LABEL_NAME);
    JLabel lblDescriptionGeneral2 = createDescriptionLabel("Go Next", null, -1, BORDER_LABEL_NAME);
    JPanel descriptionRightPanel = new JPanel(gridBagLayout);
    descriptionRightPanel.setBorder(new EmptyBorder(60, 30, 0, 0));
    descriptionRightPanel.setBackground(backgroundTheme);
    descriptionRightPanel.add(lblDescriptionNewBinding, buildGridBagConstraints(0, 0, 1, GridBagConstraints.WEST, null));
    descriptionRightPanel.add(lblDescriptionRemoveBinding, buildGridBagConstraints(0, 1, 1, GridBagConstraints.WEST, null));
    descriptionRightPanel.add(lblDescriptionFindBinding, buildGridBagConstraints(0, 2, 1, GridBagConstraints.WEST, null));
    descriptionRightPanel.add(lblDescriptionGeneral1, buildGridBagConstraints(0, 3, 1, GridBagConstraints.WEST, null));
    descriptionRightPanel.add(lblDescriptionGeneral2, buildGridBagConstraints(0, 4, 1, GridBagConstraints.WEST, null));
    // edit panel
    editRightPanel = new JPanel(gridBagLayout);
    editRightPanel.setBackground(backgroundTheme);
    editRightPanel.setBorder(new EmptyBorder(0, 15, 0, 0));
    editRightPanel.setVisible(false);
    btnSave = new JLabel("Save");
    Font font = btnSave.getFont();
    btnSave.setFont(font.deriveFont(font.getStyle() | Font.BOLD));
    btnSave.setForeground(BLUE);
    btnSave.setCursor(new Cursor(Cursor.HAND_CURSOR));
    JLabel btnClose = new JLabel("Close");
    Font fontClose = btnClose.getFont();
    btnClose.setFont(fontClose.deriveFont(fontClose.getStyle() | Font.BOLD));
    btnClose.setForeground(BLUE);
    btnClose.setCursor(new Cursor(Cursor.HAND_CURSOR));
    // top right panel
    JPanel topButtonsPanel = new JPanel(new FlowLayout());
    topButtonsPanel.setBackground(backgroundTheme);
    topButtonsPanel.add(btnSave);
    topButtonsPanel.add(btnClose);
    int internalRow = 0;
    editRightPanel.add(topButtonsPanel, buildGridBagConstraints(1, internalRow, -1, GridBagConstraints.EAST, null));
    internalRow++;
    lblSelectTemplate = new JLabel("Select a template");
    cmbPreMadeTriggerBindingTemplates = new ComboBox();
    cmbPreMadeTriggerBindingTemplates.addItem("");
    triggerBindingTemplates.keySet().stream().sorted().forEach(template -> cmbPreMadeTriggerBindingTemplates.addItem(template));
    cmbPreMadeTriggerBindingTemplates.addItemListener(itemEvent -> {
        if (itemEvent.getStateChange() == 1) {
            // when cmbPreMadeTriggerBindingTemplates combo box value changes, the new value is saved and preview is updated
            String templateSelected = (String) itemEvent.getItem();
            if (!templateSelected.isEmpty()) {
                String content = triggerBindingTemplates.get(templateSelected);
                textAreaNewTriggerBinding.setText(content);
            } else {
                textAreaNewTriggerBinding.setText("");
            }
            fireStateChanged();
        }
    });
    editRightPanel.add(lblSelectTemplate, buildGridBagConstraints(0, internalRow, 1, GridBagConstraints.WEST, null));
    editRightPanel.add(cmbPreMadeTriggerBindingTemplates, buildGridBagConstraints(1, internalRow, 1, GridBagConstraints.EAST, null));
    internalRow++;
    lblErrorNewBinding = new JLabel();
    lblErrorNewBinding.setVisible(false);
    lblErrorNewBinding.setForeground(Color.RED);
    editRightPanel.add(lblErrorNewBinding, buildGridBagConstraints(0, internalRow, 2, GridBagConstraints.WEST, null));
    internalRow++;
    textAreaNewTriggerBinding = new JTextArea(15, 35);
    textAreaNewTriggerBinding.setEditable(true);
    textAreaNewTriggerBinding.setText("");
    textAreaNewTriggerBinding.setFont(defaultLabelFont);
    textAreaNewTriggerBinding.setLineWrap(true);
    scrollTriggerBindingAreaPane = new JBScrollPane(textAreaNewTriggerBinding);
    Dimension sizeTextAreaNewTriggerBinding = textAreaNewTriggerBinding.getPreferredScrollableViewportSize();
    scrollTriggerBindingAreaPane.setPreferredSize(new Dimension(sizeTextAreaNewTriggerBinding.width, sizeTextAreaNewTriggerBinding.height + 10));
    editRightPanel.add(scrollTriggerBindingAreaPane, buildGridBagConstraints(0, internalRow, 2, GridBagConstraints.WEST, new Insets(10, 0, 0, 0)));
    internalRow++;
    rightPanel.add(descriptionRightPanel);
    rightPanel.add(editRightPanel);
    // listeners
    editRightPanel.addComponentListener(new ComponentAdapter() {

        public void componentShown(ComponentEvent e) {
            descriptionRightPanel.setVisible(false);
        }

        public void componentHidden(ComponentEvent e) {
            descriptionRightPanel.setVisible(true);
        }
    });
    lblDescriptionNewBinding.addMouseListener(new MouseAdapter() {

        Font original;

        @Override
        public void mouseClicked(MouseEvent e) {
            openEditRightPanel();
        }

        @Override
        public void mouseEntered(MouseEvent e) {
            original = e.getComponent().getFont();
            Map attributes = original.getAttributes();
            attributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
            e.getComponent().setFont(original.deriveFont(attributes));
        }

        @Override
        public void mouseExited(MouseEvent e) {
            e.getComponent().setFont(original);
        }
    });
    btnAdd.addActionListener(e -> {
        openEditRightPanel();
    });
    btnClose.addMouseListener(new MouseAdapter() {

        Font original;

        @Override
        public void mouseClicked(MouseEvent e) {
            if (editRightPanel.isVisible()) {
                btnAdd.setEnabled(true);
                textAreaNewTriggerBinding.setText("");
                lblErrorNewBinding.setVisible(false);
                cmbPreMadeTriggerBindingTemplates.setSelectedIndex(0);
                editRightPanel.setVisible(false);
            }
        }

        @Override
        public void mouseEntered(MouseEvent e) {
            original = e.getComponent().getFont();
            Map attributes = original.getAttributes();
            attributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
            e.getComponent().setFont(original.deriveFont(attributes));
        }

        @Override
        public void mouseExited(MouseEvent e) {
            e.getComponent().setFont(original);
        }
    });
    listBindingsAvailableOnCluster.addListSelectionListener(listSelectionEvent -> {
        if (!listBindingsAvailableOnCluster.getSelectedValuesList().isEmpty()) {
            if (listBindingsAvailableOnCluster.getSelectedValuesList().get(0).toString().endsWith(" NEW")) {
                btnRemove.setEnabled(true);
            } else {
                btnRemove.setEnabled(false);
            }
            btnFind.setEnabled(true);
        } else {
            btnRemove.setEnabled(false);
            btnFind.setEnabled(false);
        }
    });
    btnFind.addMouseListener(new MouseAdapter() {

        @Override
        public void mouseClicked(MouseEvent e) {
            String bindingSelected = listBindingsAvailableOnCluster.getSelectedValuesList().get(0).toString();
            String bindingContent = ((AddTriggerModel) model).getBindingsAvailableOnCluster().get(bindingSelected);
            btnSave.setVisible(false);
            lblSelectTemplate.setVisible(false);
            cmbPreMadeTriggerBindingTemplates.setVisible(false);
            textAreaNewTriggerBinding.setText(bindingContent);
            btnAdd.setEnabled(true);
            if (!editRightPanel.isVisible()) {
                editRightPanel.setVisible(true);
            }
        }
    });
    btnRemove.addMouseListener(new MouseAdapter() {

        @Override
        public void mouseClicked(MouseEvent e) {
            String bindingSelected = listBindingsAvailableOnCluster.getSelectedValuesList().get(0).toString();
            if (bindingSelected.endsWith(" NEW")) {
                ((AddTriggerModel) model).getBindingsAvailableOnCluster().remove(bindingSelected);
                fillAvailableBindingsList(null);
            }
        }
    });
    btnSave.addMouseListener(new MouseAdapter() {

        Font original;

        @Override
        public void mouseClicked(MouseEvent e) {
            String configuration = textAreaNewTriggerBinding.getText();
            String error = "";
            if (!configuration.isEmpty()) {
                TriggerBindingConfigurationModel bindingModel = new TriggerBindingConfigurationModel(configuration);
                if (bindingModel.isValid()) {
                    if (((AddTriggerModel) model).getBindingsAvailableOnCluster().keySet().stream().map(binding -> binding.replace(" NEW", "")).anyMatch(binding -> binding.equalsIgnoreCase(bindingModel.getName()))) {
                        error = "<html>The name has already been used for another TriggerBinding. <br> Please change it and save again!!</html>";
                    } else {
                        String bindingNameWithNewSuffix = bindingModel.getName() + " NEW";
                        ((AddTriggerModel) model).getBindingsAvailableOnCluster().put(bindingNameWithNewSuffix, textAreaNewTriggerBinding.getText());
                        fillAvailableBindingsList(bindingNameWithNewSuffix);
                    }
                } else {
                    error = bindingModel.getErrorMessage();
                }
            } else {
                error = "You cannot save an empty TriggerBinding.";
            }
            if (error.isEmpty()) {
                editRightPanel.setVisible(false);
                btnAdd.setEnabled(true);
                return;
            }
            // if the new binding written by the user is not valid, we should show an error message with some info
            lblErrorNewBinding.setText(error);
            lblErrorNewBinding.setVisible(true);
        }

        @Override
        public void mouseEntered(MouseEvent e) {
            original = e.getComponent().getFont();
            Map attributes = original.getAttributes();
            attributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
            e.getComponent().setFont(original.deriveFont(attributes));
        }

        @Override
        public void mouseExited(MouseEvent e) {
            e.getComponent().setFont(original);
        }
    });
    JPanel wrapperContentPanel = new JPanel(new BorderLayout());
    wrapperContentPanel.add(outerListScrollPane, BorderLayout.WEST);
    wrapperContentPanel.add(box, BorderLayout.CENTER);
    wrapperContentPanel.add(rightPanel, BorderLayout.EAST);
    addComponent(wrapperContentPanel, editorFont, null, null, 0, row[0], GridBagConstraints.NORTHWEST);
    row[0] += 1;
    adjustContentPanel();
}
Also used : Color(java.awt.Color) NO_BORDER(com.redhat.devtools.intellij.tektoncd.ui.UIConstants.NO_BORDER) TriggerBindingConfigurationModel(com.redhat.devtools.intellij.tektoncd.utils.model.resources.TriggerBindingConfigurationModel) Insets(java.awt.Insets) AllIcons(com.intellij.icons.AllIcons) TextAttribute(java.awt.font.TextAttribute) Cursor(java.awt.Cursor) SwingConstants(javax.swing.SwingConstants) ArrayList(java.util.ArrayList) Border(javax.swing.border.Border) Map(java.util.Map) MouseAdapter(java.awt.event.MouseAdapter) BLUE(com.redhat.devtools.intellij.tektoncd.ui.UIConstants.BLUE) AddTriggerModel(com.redhat.devtools.intellij.tektoncd.utils.model.actions.AddTriggerModel) BorderLayout(java.awt.BorderLayout) JComboBox(javax.swing.JComboBox) BORDER_LABEL_NAME(com.redhat.devtools.intellij.tektoncd.ui.UIConstants.BORDER_LABEL_NAME) ComboBox(com.intellij.openapi.ui.ComboBox) JBList(com.intellij.ui.components.JBList) FlowLayout(java.awt.FlowLayout) DefaultListCellRenderer(javax.swing.DefaultListCellRenderer) JButton(javax.swing.JButton) Font(java.awt.Font) JList(javax.swing.JList) Icon(javax.swing.Icon) GridBagConstraints(java.awt.GridBagConstraints) ComponentEvent(java.awt.event.ComponentEvent) Ints(com.google.common.primitives.Ints) Component(java.awt.Component) MouseMotionAdapter(java.awt.event.MouseMotionAdapter) MouseEvent(java.awt.event.MouseEvent) JBScrollPane(com.intellij.ui.components.JBScrollPane) Box(javax.swing.Box) ComponentAdapter(java.awt.event.ComponentAdapter) JScrollPane(javax.swing.JScrollPane) BaseStep(com.redhat.devtools.intellij.tektoncd.ui.wizard.BaseStep) IconLoader(com.intellij.openapi.util.IconLoader) Dimension(java.awt.Dimension) List(java.util.List) JLabel(javax.swing.JLabel) EmptyBorder(javax.swing.border.EmptyBorder) JTextArea(javax.swing.JTextArea) JPanel(javax.swing.JPanel) MARGIN_BOTTOM_10(com.redhat.devtools.intellij.tektoncd.ui.UIConstants.MARGIN_BOTTOM_10) JPanel(javax.swing.JPanel) FlowLayout(java.awt.FlowLayout) JTextArea(javax.swing.JTextArea) Insets(java.awt.Insets) JButton(javax.swing.JButton) AddTriggerModel(com.redhat.devtools.intellij.tektoncd.utils.model.actions.AddTriggerModel) Cursor(java.awt.Cursor) Font(java.awt.Font) BorderLayout(java.awt.BorderLayout) DefaultListCellRenderer(javax.swing.DefaultListCellRenderer) EmptyBorder(javax.swing.border.EmptyBorder) Component(java.awt.Component) TriggerBindingConfigurationModel(com.redhat.devtools.intellij.tektoncd.utils.model.resources.TriggerBindingConfigurationModel) ComponentAdapter(java.awt.event.ComponentAdapter) JScrollPane(javax.swing.JScrollPane) MouseEvent(java.awt.event.MouseEvent) JComboBox(javax.swing.JComboBox) ComboBox(com.intellij.openapi.ui.ComboBox) MouseAdapter(java.awt.event.MouseAdapter) JLabel(javax.swing.JLabel) JComboBox(javax.swing.JComboBox) ComboBox(com.intellij.openapi.ui.ComboBox) Box(javax.swing.Box) Dimension(java.awt.Dimension) MouseMotionAdapter(java.awt.event.MouseMotionAdapter) JBList(com.intellij.ui.components.JBList) ComponentEvent(java.awt.event.ComponentEvent) Map(java.util.Map) JList(javax.swing.JList) JBScrollPane(com.intellij.ui.components.JBScrollPane)

Aggregations

Ints (com.google.common.primitives.Ints)1 AllIcons (com.intellij.icons.AllIcons)1 ComboBox (com.intellij.openapi.ui.ComboBox)1 IconLoader (com.intellij.openapi.util.IconLoader)1 JBList (com.intellij.ui.components.JBList)1 JBScrollPane (com.intellij.ui.components.JBScrollPane)1 BLUE (com.redhat.devtools.intellij.tektoncd.ui.UIConstants.BLUE)1 BORDER_LABEL_NAME (com.redhat.devtools.intellij.tektoncd.ui.UIConstants.BORDER_LABEL_NAME)1 MARGIN_BOTTOM_10 (com.redhat.devtools.intellij.tektoncd.ui.UIConstants.MARGIN_BOTTOM_10)1 NO_BORDER (com.redhat.devtools.intellij.tektoncd.ui.UIConstants.NO_BORDER)1 BaseStep (com.redhat.devtools.intellij.tektoncd.ui.wizard.BaseStep)1 AddTriggerModel (com.redhat.devtools.intellij.tektoncd.utils.model.actions.AddTriggerModel)1 TriggerBindingConfigurationModel (com.redhat.devtools.intellij.tektoncd.utils.model.resources.TriggerBindingConfigurationModel)1 BorderLayout (java.awt.BorderLayout)1 Color (java.awt.Color)1 Component (java.awt.Component)1 Cursor (java.awt.Cursor)1 Dimension (java.awt.Dimension)1 FlowLayout (java.awt.FlowLayout)1 Font (java.awt.Font)1