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();
}
Aggregations