|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package com.github.cameltooling.idea.preference.propertyplaceholder; |
| 18 | + |
| 19 | +import com.intellij.openapi.project.Project; |
| 20 | +import com.intellij.openapi.ui.DialogWrapper; |
| 21 | +import com.intellij.ui.ToolbarDecorator; |
| 22 | +import com.intellij.ui.components.JBLabel; |
| 23 | +import com.intellij.ui.components.JBList; |
| 24 | +import com.intellij.util.ui.JBUI; |
| 25 | +import org.jetbrains.annotations.Nullable; |
| 26 | + |
| 27 | +import javax.swing.*; |
| 28 | +import java.awt.*; |
| 29 | +import java.util.ArrayList; |
| 30 | +import java.util.List; |
| 31 | + |
| 32 | +class PropertyPlaceholderEditDialog extends DialogWrapper { |
| 33 | + |
| 34 | + private JTextField startField; |
| 35 | + private JTextField endField; |
| 36 | + |
| 37 | + private DefaultListModel<String> namespacesModel; |
| 38 | + private JBList<String> namespacesList; |
| 39 | + |
| 40 | + PropertyPlaceholderEditDialog(Project project) { |
| 41 | + super(project, true); |
| 42 | + init(); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + protected void init() { |
| 47 | + setTitle("Edit Property Placeholder Definition"); |
| 48 | + startField = new JTextField(); |
| 49 | + endField = new JTextField(); |
| 50 | + namespacesModel = new DefaultListModel<>(); |
| 51 | + namespacesList = new JBList<>(namespacesModel); |
| 52 | + namespacesList.setVisibleRowCount(6); |
| 53 | + super.init(); |
| 54 | + } |
| 55 | + |
| 56 | + @Nullable |
| 57 | + @Override |
| 58 | + protected JComponent createCenterPanel() { |
| 59 | + JPanel panel = new JPanel(new GridBagLayout()); |
| 60 | + GridBagConstraints gbcLabel = new GridBagConstraints(); |
| 61 | + gbcLabel.gridx = 0; |
| 62 | + gbcLabel.anchor = GridBagConstraints.WEST; |
| 63 | + gbcLabel.insets = JBUI.insets(5, 5, 0, 5); |
| 64 | + |
| 65 | + GridBagConstraints gbcField = new GridBagConstraints(); |
| 66 | + gbcField.gridx = 1; |
| 67 | + gbcField.weightx = 1.0; |
| 68 | + gbcField.fill = GridBagConstraints.HORIZONTAL; |
| 69 | + gbcField.insets = JBUI.insets(5, 0, 0, 5); |
| 70 | + |
| 71 | + int row = 0; |
| 72 | + gbcLabel.gridy = row; |
| 73 | + gbcField.gridy = row; |
| 74 | + panel.add(new JBLabel("Start token:"), gbcLabel); |
| 75 | + panel.add(startField, gbcField); |
| 76 | + |
| 77 | + row++; |
| 78 | + gbcLabel.gridy = row; |
| 79 | + gbcField.gridy = row; |
| 80 | + panel.add(new JBLabel("End token:"), gbcLabel); |
| 81 | + panel.add(endField, gbcField); |
| 82 | + |
| 83 | + row++; |
| 84 | + gbcLabel.gridy = row; |
| 85 | + gbcField.gridy = row; |
| 86 | + gbcField.weighty = 1.0; |
| 87 | + gbcField.fill = GridBagConstraints.BOTH; |
| 88 | + |
| 89 | + panel.add(new JBLabel("XML namespaces:"), gbcLabel); |
| 90 | + |
| 91 | + // Decorate the list with add/remove/edit actions |
| 92 | + ToolbarDecorator decorator = ToolbarDecorator.createDecorator(namespacesList) |
| 93 | + .setAddAction(button -> addNamespace()) |
| 94 | + .setEditAction(button -> editSelectedNamespace()) |
| 95 | + .setRemoveAction(button -> removeSelectedNamespaces()); |
| 96 | + JComponent listPanel = decorator.createPanel(); |
| 97 | + panel.add(listPanel, gbcField); |
| 98 | + |
| 99 | + startField.setPreferredSize(new Dimension(200, startField.getPreferredSize().height)); |
| 100 | + endField.setPreferredSize(new Dimension(200, endField.getPreferredSize().height)); |
| 101 | + |
| 102 | + return panel; |
| 103 | + } |
| 104 | + |
| 105 | + private void addNamespace() { |
| 106 | + String value = JOptionPane.showInputDialog(getContentPanel(), "New namespace:", "Add Namespace", JOptionPane.PLAIN_MESSAGE); |
| 107 | + if (value != null) { |
| 108 | + String trimmed = value.trim(); |
| 109 | + if (!trimmed.isEmpty() && !containsNamespace(trimmed)) { |
| 110 | + namespacesModel.addElement(trimmed); |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + private void editSelectedNamespace() { |
| 116 | + int idx = namespacesList.getSelectedIndex(); |
| 117 | + if (idx >= 0) { |
| 118 | + String current = namespacesModel.get(idx); |
| 119 | + String value = JOptionPane.showInputDialog(getContentPanel(), "Edit namespace:", current); |
| 120 | + if (value != null) { |
| 121 | + String trimmed = value.trim(); |
| 122 | + if (!trimmed.isEmpty() && (!containsNamespace(trimmed) || trimmed.equals(current))) { |
| 123 | + namespacesModel.set(idx, trimmed); |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + private void removeSelectedNamespaces() { |
| 130 | + int[] indices = namespacesList.getSelectedIndices(); |
| 131 | + if (indices.length > 0) { |
| 132 | + // Remove from last to first to keep indices valid |
| 133 | + for (int i = indices.length - 1; i >= 0; i--) { |
| 134 | + namespacesModel.remove(indices[i]); |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + private boolean containsNamespace(String ns) { |
| 140 | + for (int i = 0; i < namespacesModel.getSize(); i++) { |
| 141 | + if (namespacesModel.get(i).equals(ns)) return true; |
| 142 | + } |
| 143 | + return false; |
| 144 | + } |
| 145 | + |
| 146 | + @Override |
| 147 | + public JComponent getPreferredFocusedComponent() { |
| 148 | + return startField; |
| 149 | + } |
| 150 | + |
| 151 | + void initValues(PropertyPlaceholderSettingsEntry entry) { |
| 152 | + if (entry != null) { |
| 153 | + startField.setText(entry.getStartToken()); |
| 154 | + endField.setText(entry.getEndToken()); |
| 155 | + namespacesModel.clear(); |
| 156 | + for (String ns : entry.getNamespaces()) { |
| 157 | + if (ns != null) { |
| 158 | + String trimmed = ns.trim(); |
| 159 | + if (!trimmed.isEmpty() && !containsNamespace(trimmed)) { |
| 160 | + namespacesModel.addElement(trimmed); |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + String getStartToken() { |
| 168 | + return startField.getText().trim(); |
| 169 | + } |
| 170 | + |
| 171 | + String getEndToken() { |
| 172 | + return endField.getText().trim(); |
| 173 | + } |
| 174 | + |
| 175 | + List<String> getNamespaces() { |
| 176 | + List<String> result = new ArrayList<>(); |
| 177 | + for (int i = 0; i < namespacesModel.getSize(); i++) { |
| 178 | + result.add(namespacesModel.get(i)); |
| 179 | + } |
| 180 | + return result; |
| 181 | + } |
| 182 | + |
| 183 | +} |
0 commit comments