|
| 1 | +package com.longforus.apidebugger.ui; |
| 2 | + |
| 3 | +import com.google.gson.JsonElement; |
| 4 | +import com.google.gson.JsonNull; |
| 5 | +import com.google.gson.JsonParser; |
| 6 | +import com.longforus.apidebugger.MyValueHandler; |
| 7 | +import java.awt.BorderLayout; |
| 8 | +import java.awt.event.MouseEvent; |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.Enumeration; |
| 11 | +import java.util.List; |
| 12 | +import javax.swing.JMenuItem; |
| 13 | +import javax.swing.JOptionPane; |
| 14 | +import javax.swing.JPanel; |
| 15 | +import javax.swing.JPopupMenu; |
| 16 | +import javax.swing.JScrollPane; |
| 17 | +import javax.swing.JTree; |
| 18 | +import javax.swing.event.MouseInputAdapter; |
| 19 | +import javax.swing.event.TreeSelectionListener; |
| 20 | +import javax.swing.tree.DefaultTreeModel; |
| 21 | +import javax.swing.tree.TreePath; |
| 22 | +import javax.swing.tree.TreeSelectionModel; |
| 23 | + |
| 24 | +/** |
| 25 | + * Implements the embeddable swing widget for editing JSON. |
| 26 | + * |
| 27 | + * This class is not thread safe. |
| 28 | + * |
| 29 | + * @author Stephen Owens |
| 30 | + * |
| 31 | + * |
| 32 | + * <p> |
| 33 | + * Copyright 2011 Stephen P. Owens : [email protected] |
| 34 | + * </p> |
| 35 | + * <p> |
| 36 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 37 | + * you may not use this file except in compliance with the License. |
| 38 | + * You may obtain a copy of the License at |
| 39 | + * </p> |
| 40 | + * <p> |
| 41 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 42 | + * </p> |
| 43 | + * <p> |
| 44 | + * Unless required by applicable law or agreed to in writing, software |
| 45 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 46 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 47 | + * See the License for the specific language governing permissions and |
| 48 | + * limitations under the License. |
| 49 | + * </p> |
| 50 | + */ |
| 51 | +public class JsonEditPanel extends JPanel { |
| 52 | + /** |
| 53 | + * Using default serial version id. |
| 54 | + */ |
| 55 | + private static final long serialVersionUID = 1L; |
| 56 | + |
| 57 | + // Default visibility to let classes in this module access this element directly. |
| 58 | + JTree jTree; |
| 59 | + private final JPopupMenu mPopupMenu; |
| 60 | + |
| 61 | + public enum UpdateType { |
| 62 | + REPLACE, |
| 63 | + INSERT, |
| 64 | + APPEND, |
| 65 | + AS_CHILD |
| 66 | + } |
| 67 | + |
| 68 | + ; |
| 69 | + |
| 70 | + public enum AllowedOps { |
| 71 | + REPLACE, |
| 72 | + INSERT, |
| 73 | + APPEND, |
| 74 | + AS_CHILD, |
| 75 | + DELETE, |
| 76 | + RENAME, |
| 77 | + GET_JSON |
| 78 | + } |
| 79 | + |
| 80 | + ; |
| 81 | + |
| 82 | + /** |
| 83 | + * Default constructor for the JsonEditPanel object. |
| 84 | + * |
| 85 | + * Creates an empty tree. |
| 86 | + */ |
| 87 | + public JsonEditPanel() { |
| 88 | + setLayout(new BorderLayout()); |
| 89 | + JsonJTreeNode root = new JsonJTreeNode(null, -1, new JsonNull()); |
| 90 | + jTree = new JTree(root); |
| 91 | + jTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); |
| 92 | + mPopupMenu = new JPopupMenu(); |
| 93 | + JMenuItem copy = new JMenuItem("Copy"); |
| 94 | + copy.addActionListener(e -> { |
| 95 | + JsonJTreeNode component = (JsonJTreeNode) jTree.getLastSelectedPathComponent(); |
| 96 | + String text; |
| 97 | + switch (component.dataType) { |
| 98 | + case OBJECT: |
| 99 | + case ARRAY: |
| 100 | + text = component.asJsonElement().toString(); |
| 101 | + break; |
| 102 | + default: |
| 103 | + text = component.toSSearchStr(); |
| 104 | + } |
| 105 | + MyValueHandler.INSTANCE.setSysClipboardText(text); |
| 106 | + }); |
| 107 | + mPopupMenu.add(copy); |
| 108 | + jTree.addMouseListener(new MouseInputAdapter() { |
| 109 | + @Override |
| 110 | + public void mouseClicked(MouseEvent e) { |
| 111 | + super.mouseClicked(e); |
| 112 | + if (e.getButton() == MouseEvent.BUTTON3) { |
| 113 | + mPopupMenu.show(jTree, e.getX(), e.getY()); |
| 114 | + } |
| 115 | + } |
| 116 | + }); |
| 117 | + add(new JScrollPane(jTree), BorderLayout.CENTER); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Allow the owner of the component to listen for tree selection events. |
| 122 | + * |
| 123 | + * @param tsl A handler for tree selection events |
| 124 | + */ |
| 125 | + public void addTreeSelectionListener(TreeSelectionListener tsl) { |
| 126 | + jTree.addTreeSelectionListener(tsl); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Replaces the current tree structure in the contained JTree component |
| 131 | + * with a new structure built from the JSON string provided. |
| 132 | + * |
| 133 | + * @param json - the JSON to update the tree control with |
| 134 | + * @param updateType - if a node is selected when this method is called |
| 135 | + * then the updateType indicates where the new json goes: |
| 136 | + * REPLACE - replace current selected node with new JSON |
| 137 | + * INSERT - place node before selected node |
| 138 | + * APPEND - place node after selected node |
| 139 | + * AS_CHILD - append to end of child nodes or insert new child node if no |
| 140 | + * children present. Selected node must be of type ARRAY or OBJECT |
| 141 | + */ |
| 142 | + @SuppressWarnings("unchecked") |
| 143 | + public void setJson(String json, UpdateType updateType) { |
| 144 | + TreePath selection = jTree.getSelectionPath(); |
| 145 | + if (selection == null) { |
| 146 | + if (updateType == UpdateType.REPLACE) { |
| 147 | + JsonElement root = new JsonParser().parse(json); |
| 148 | + JsonJTreeNode rootNode = new JsonJTreeNode(null, -1, root); |
| 149 | + jTree.setModel(new DefaultTreeModel(rootNode)); |
| 150 | + } else { |
| 151 | + JOptionPane.showMessageDialog(this, "Only replace JSON and get JSON are supported when no node is selected.", "Notice", JOptionPane.INFORMATION_MESSAGE); |
| 152 | + } |
| 153 | + } else { |
| 154 | + JsonJTreeNode selectedNode = (JsonJTreeNode) selection.getLastPathComponent(); |
| 155 | + JsonJTreeNode parent = (JsonJTreeNode) selectedNode.getParent(); |
| 156 | + switch (updateType) { |
| 157 | + case REPLACE: { |
| 158 | + if (parent == null) { |
| 159 | + JsonElement root = new JsonParser().parse(json); |
| 160 | + JsonJTreeNode rootNode = new JsonJTreeNode(null, -1, root); |
| 161 | + jTree.setModel(new DefaultTreeModel(rootNode)); |
| 162 | + return; |
| 163 | + } |
| 164 | + JsonElement root = new JsonParser().parse(json); |
| 165 | + JsonJTreeNode replacementNode = new JsonJTreeNode(selectedNode.fieldName, selectedNode.index, root); |
| 166 | + int index = selectedNode.getParent().getIndex(selectedNode); |
| 167 | + selectedNode.removeFromParent(); |
| 168 | + parent.insert(replacementNode, index); |
| 169 | + ((DefaultTreeModel) jTree.getModel()).reload(parent); |
| 170 | + } |
| 171 | + break; |
| 172 | + case INSERT: |
| 173 | + case APPEND: { |
| 174 | + if (parent == null) { |
| 175 | + JOptionPane.showMessageDialog(this, "You cannot append to the root element.", "Notice", JOptionPane.INFORMATION_MESSAGE); |
| 176 | + return; |
| 177 | + } |
| 178 | + JsonElement root = new JsonParser().parse(json); |
| 179 | + JsonJTreeNode replacementNode = new JsonJTreeNode(selectedNode.fieldName, selectedNode.index, root); |
| 180 | + int index = selectedNode.getParent().getIndex(selectedNode); |
| 181 | + if (updateType.equals(UpdateType.APPEND)) { |
| 182 | + index++; |
| 183 | + } |
| 184 | + parent.insert(replacementNode, index); |
| 185 | + ((DefaultTreeModel) jTree.getModel()).reload(parent); |
| 186 | + } |
| 187 | + break; |
| 188 | + case AS_CHILD: { |
| 189 | + JsonElement root = new JsonParser().parse(json); |
| 190 | + String fieldName = null; |
| 191 | + int arrayIndex = -1; |
| 192 | + if (selectedNode.dataType.equals(JsonJTreeNode.DataType.ARRAY)) { |
| 193 | + Enumeration en = selectedNode.children(); |
| 194 | + int count = 0; |
| 195 | + while (en.hasMoreElements()) { |
| 196 | + en.nextElement(); |
| 197 | + count++; |
| 198 | + } |
| 199 | + arrayIndex = count; |
| 200 | + } else if (selectedNode.dataType.equals(JsonJTreeNode.DataType.OBJECT)) { |
| 201 | + fieldName = "new-field"; |
| 202 | + } else { |
| 203 | + JOptionPane.showMessageDialog(this, "Vaue type entities can not have children.", "Notice", JOptionPane.INFORMATION_MESSAGE); |
| 204 | + return; |
| 205 | + } |
| 206 | + JsonJTreeNode newNode = new JsonJTreeNode(fieldName, arrayIndex, root); |
| 207 | + selectedNode.add(newNode); |
| 208 | + ((DefaultTreeModel) jTree.getModel()).reload(selectedNode); |
| 209 | + } |
| 210 | + break; |
| 211 | + } |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + /** |
| 216 | + * Renames the selected node if it is a renameable node. |
| 217 | + */ |
| 218 | + public void renameNode() { |
| 219 | + TreePath selection = jTree.getSelectionPath(); |
| 220 | + if (selection == null) { |
| 221 | + JOptionPane.showMessageDialog(this, "No node is selected for rename.", "Notice", JOptionPane.INFORMATION_MESSAGE); |
| 222 | + return; |
| 223 | + } |
| 224 | + JsonJTreeNode node = (JsonJTreeNode) selection.getLastPathComponent(); |
| 225 | + JsonJTreeNode parent = (JsonJTreeNode) node.getParent(); |
| 226 | + if (parent == null) { |
| 227 | + JOptionPane.showMessageDialog(this, "It is not possible to assign a name to the root node.", "Notice", JOptionPane.INFORMATION_MESSAGE); |
| 228 | + return; |
| 229 | + } |
| 230 | + if (!parent.dataType.equals(JsonJTreeNode.DataType.OBJECT)) { |
| 231 | + JOptionPane.showMessageDialog(this, "Only object fields may be renamed.", "Notice", JOptionPane.INFORMATION_MESSAGE); |
| 232 | + return; |
| 233 | + } |
| 234 | + |
| 235 | + String newName = JOptionPane.showInputDialog("Enter new name for " + node.fieldName); |
| 236 | + node.fieldName = newName; |
| 237 | + ((DefaultTreeModel) jTree.getModel()).nodeChanged(node); |
| 238 | + } |
| 239 | + |
| 240 | + /** |
| 241 | + * Deletes selected node or sets entire model to null if root node or no node is selected. |
| 242 | + */ |
| 243 | + public void deleteNode() { |
| 244 | + TreePath selection = jTree.getSelectionPath(); |
| 245 | + if (selection == null) { |
| 246 | + // Replace root with emptyness |
| 247 | + jTree.setModel(new DefaultTreeModel(new JsonJTreeNode(null, -1, new JsonNull()))); |
| 248 | + } else { |
| 249 | + JsonJTreeNode node = (JsonJTreeNode) selection.getLastPathComponent(); |
| 250 | + JsonJTreeNode parent = (JsonJTreeNode) node.getParent(); |
| 251 | + if (parent == null) { |
| 252 | + // Replace root with emptyness |
| 253 | + jTree.setModel(new DefaultTreeModel(new JsonJTreeNode(null, -1, new JsonNull()))); |
| 254 | + } else { |
| 255 | + node.removeFromParent(); |
| 256 | + ((DefaultTreeModel) jTree.getModel()).reload(parent); |
| 257 | + } |
| 258 | + } |
| 259 | + } |
| 260 | + |
| 261 | + /** |
| 262 | + * Returns the current JSON from the JTree |
| 263 | + * |
| 264 | + * @return the JSON as it is represented by the current state of the Tree model |
| 265 | + */ |
| 266 | + public String getJson() { |
| 267 | + TreePath selection = jTree.getSelectionPath(); |
| 268 | + JsonJTreeNode node = null; |
| 269 | + if (selection == null) { |
| 270 | + ((DefaultTreeModel) jTree.getModel()).reload(); |
| 271 | + node = (JsonJTreeNode) jTree.getModel().getRoot(); |
| 272 | + } else { |
| 273 | + ((DefaultTreeModel) jTree.getModel()).reload(node); |
| 274 | + node = (JsonJTreeNode) selection.getLastPathComponent(); |
| 275 | + } |
| 276 | + if (node != null) { |
| 277 | + return node.asJsonElement().toString(); |
| 278 | + } else { |
| 279 | + return null; |
| 280 | + } |
| 281 | + } |
| 282 | + |
| 283 | + /** |
| 284 | + * Determine what can currently be asked of the component in terms of tree modifiation operations. |
| 285 | + * |
| 286 | + * @return a list of AllowedOps enumerations to indicate the commands that are currently allowed upon the Component |
| 287 | + */ |
| 288 | + public List<AllowedOps> getAllowedOperations() { |
| 289 | + List<AllowedOps> result = new ArrayList<AllowedOps>(); |
| 290 | + result.add(AllowedOps.REPLACE); |
| 291 | + result.add(AllowedOps.GET_JSON); |
| 292 | + |
| 293 | + TreePath selection = jTree.getSelectionPath(); |
| 294 | + if (selection == null) { |
| 295 | + return result; |
| 296 | + } |
| 297 | + |
| 298 | + JsonJTreeNode selectedNode = (JsonJTreeNode) selection.getLastPathComponent(); |
| 299 | + JsonJTreeNode parentNode = null; |
| 300 | + |
| 301 | + if (selectedNode != null) { |
| 302 | + result.add(AllowedOps.DELETE); |
| 303 | + parentNode = (JsonJTreeNode) selectedNode.getParent(); |
| 304 | + } |
| 305 | + if (parentNode != null) { |
| 306 | + result.add(AllowedOps.APPEND); |
| 307 | + result.add(AllowedOps.INSERT); |
| 308 | + } |
| 309 | + if (selectedNode.dataType.equals(JsonJTreeNode.DataType.ARRAY) || selectedNode.dataType.equals(JsonJTreeNode.DataType.OBJECT)) { |
| 310 | + result.add(AllowedOps.AS_CHILD); |
| 311 | + } |
| 312 | + if ((parentNode != null) && (parentNode.dataType.equals(JsonJTreeNode.DataType.OBJECT))) { |
| 313 | + result.add(AllowedOps.RENAME); |
| 314 | + } |
| 315 | + return result; |
| 316 | + } |
| 317 | +} |
0 commit comments