Codebase list jd-gui / 0af4a626-0b34-4685-a8a4-c18e8bd66f2d/upstream app / src / main / java / org / jd / gui / view / SearchInConstantPoolsView.java
0af4a626-0b34-4685-a8a4-c18e8bd66f2d/upstream

Tree @0af4a626-0b34-4685-a8a4-c18e8bd66f2d/upstream (Download .tar.gz)

SearchInConstantPoolsView.java @0af4a626-0b34-4685-a8a4-c18e8bd66f2d/upstreamraw · history · blame

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/*
 * Copyright (c) 2008-2019 Emmanuel Dupuy.
 * This project is distributed under the GPLv3 license.
 * This is a Copyleft license that gives the user the right to use,
 * copy and modify the code freely for non-commercial purposes.
 */

package org.jd.gui.view;

import org.jd.gui.api.API;
import org.jd.gui.api.feature.ContainerEntryGettable;
import org.jd.gui.api.feature.TreeNodeExpandable;
import org.jd.gui.api.feature.UriGettable;
import org.jd.gui.api.model.Container;
import org.jd.gui.model.container.DelegatingFilterContainer;
import org.jd.gui.spi.TreeNodeFactory;
import org.jd.gui.util.function.TriConsumer;
import org.jd.gui.util.swing.SwingUtil;
import org.jd.gui.view.component.Tree;
import org.jd.gui.view.renderer.TreeNodeRenderer;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.TreeExpansionEvent;
import javax.swing.event.TreeExpansionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import java.awt.*;
import java.awt.event.*;
import java.net.URI;
import java.util.*;
import java.util.function.BiConsumer;

public class SearchInConstantPoolsView<T extends DefaultMutableTreeNode & ContainerEntryGettable & UriGettable> {
    protected static final ContainerComparator CONTAINER_COMPARATOR = new ContainerComparator();

    public static final int SEARCH_TYPE = 1;
    public static final int SEARCH_CONSTRUCTOR = 2;
    public static final int SEARCH_METHOD = 4;
    public static final int SEARCH_FIELD = 8;
    public static final int SEARCH_STRING = 16;
    public static final int SEARCH_MODULE = 32;
    public static final int SEARCH_DECLARATION = 64;
    public static final int SEARCH_REFERENCE = 128;

    protected API api;
    protected Set<URI> accepted = new HashSet<>();
    protected Set<URI> expanded = new HashSet<>();

    protected JDialog searchInConstantPoolsDialog;
    protected JTextField searchInConstantPoolsEnterTextField;
    protected JLabel searchInConstantPoolsLabel;
    protected JCheckBox searchInConstantPoolsCheckBoxType;
    protected JCheckBox searchInConstantPoolsCheckBoxField;
    protected JCheckBox searchInConstantPoolsCheckBoxConstructor;
    protected JCheckBox searchInConstantPoolsCheckBoxMethod;
    protected JCheckBox searchInConstantPoolsCheckBoxString;
    protected JCheckBox searchInConstantPoolsCheckBoxModule;
    protected JCheckBox searchInConstantPoolsCheckBoxDeclarations;
    protected JCheckBox searchInConstantPoolsCheckBoxReferences;
    protected Tree searchInConstantPoolsTree;

    @SuppressWarnings("unchecked")
    public SearchInConstantPoolsView(
            API api, JFrame mainFrame,
            BiConsumer<String, Integer> changedPatternCallback,
            TriConsumer<URI, String, Integer> selectedTypeCallback) {
        this.api = api;
        // Build GUI
        SwingUtil.invokeLater(() -> {
            searchInConstantPoolsDialog = new JDialog(mainFrame, "Search", false);

            JPanel panel = new JPanel();
            panel.setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15));
            panel.setLayout(new BorderLayout());
            searchInConstantPoolsDialog.add(panel);

            // Box for search criteria
            Box vbox = Box.createVerticalBox();

            Box hbox = Box.createHorizontalBox();
            hbox.add(new JLabel("Search string (* = any string, ? = any character):"));
            hbox.add(Box.createHorizontalGlue());
            vbox.add(hbox);

            vbox.add(Box.createVerticalStrut(10));

            // Text field
            vbox.add(searchInConstantPoolsEnterTextField = new JTextField(30));
            searchInConstantPoolsEnterTextField.addKeyListener(new KeyAdapter() {
                @Override public void keyTyped(KeyEvent e)  {
                    switch (e.getKeyChar()) {
                        case '=': case '(': case ')': case '{': case '}': case '[': case ']':
                            e.consume();
                            break;
                        default:
                            if (Character.isDigit(e.getKeyChar()) && (searchInConstantPoolsEnterTextField.getText().length() == 0)) {
                                // First character can not be a digit
                                e.consume();
                            }
                            break;
                    }
                }
                @Override public void keyPressed(KeyEvent e) {
                    if (e.getKeyCode() == KeyEvent.VK_DOWN) {
                        DefaultMutableTreeNode root = (DefaultMutableTreeNode)searchInConstantPoolsTree.getModel().getRoot();
                        if (root.getChildCount() > 0) {
                            searchInConstantPoolsTree.requestFocus();
                            if (searchInConstantPoolsTree.getSelectionCount() == 0) {
                                searchInConstantPoolsTree.setSelectionPath(new TreePath(((DefaultMutableTreeNode)root.getChildAt(0)).getPath()));
                            }
                            e.consume();
                        }
                    }
                }
            });
            searchInConstantPoolsEnterTextField.getDocument().addDocumentListener(new DocumentListener() {
                @Override public void insertUpdate(DocumentEvent e) { call(); }
                @Override public void removeUpdate(DocumentEvent e) { call(); }
                @Override public void changedUpdate(DocumentEvent e) { call(); }
                protected void call() { changedPatternCallback.accept(searchInConstantPoolsEnterTextField.getText(), getFlags()); }
            });

            vbox.add(Box.createVerticalStrut(10));

            hbox = Box.createHorizontalBox();
            vbox.add(hbox);

            JPanel subpanel = new JPanel();
            subpanel.setBorder(BorderFactory.createTitledBorder("Search For"));
            subpanel.setLayout(new BorderLayout());
            hbox.add(subpanel);

            Box subhbox = Box.createHorizontalBox();
            subpanel.add(subhbox, BorderLayout.WEST);

            ItemListener checkBoxListener = (e) -> {
                changedPatternCallback.accept(searchInConstantPoolsEnterTextField.getText(), getFlags());
                searchInConstantPoolsEnterTextField.requestFocus();
            };

            JPanel subsubpanel = new JPanel();
            subsubpanel.setLayout(new GridLayout(2, 1));
            subsubpanel.add(searchInConstantPoolsCheckBoxType = new JCheckBox("Type", true));
            searchInConstantPoolsCheckBoxType.addItemListener(checkBoxListener);
            subsubpanel.add(searchInConstantPoolsCheckBoxField = new JCheckBox("Field"));
            searchInConstantPoolsCheckBoxField.addItemListener(checkBoxListener);
            subhbox.add(subsubpanel);

            subsubpanel = new JPanel();
            subsubpanel.setLayout(new GridLayout(2, 1));
            subsubpanel.add(searchInConstantPoolsCheckBoxConstructor = new JCheckBox("Constructor"));
            searchInConstantPoolsCheckBoxConstructor.addItemListener(checkBoxListener);
            subsubpanel.add(searchInConstantPoolsCheckBoxMethod = new JCheckBox("Method"));
            searchInConstantPoolsCheckBoxMethod.addItemListener(checkBoxListener);
            subhbox.add(subsubpanel);

            subsubpanel = new JPanel();
            subsubpanel.setLayout(new GridLayout(2, 1));
            subsubpanel.add(searchInConstantPoolsCheckBoxString = new JCheckBox("String Constant"));
            searchInConstantPoolsCheckBoxString.addItemListener(checkBoxListener);
            subsubpanel.add(searchInConstantPoolsCheckBoxModule = new JCheckBox("Java Module"));
            searchInConstantPoolsCheckBoxModule.addItemListener(checkBoxListener);
            subhbox.add(subsubpanel);

            subpanel = new JPanel();
            subpanel.setBorder(BorderFactory.createTitledBorder("Limit To"));
            subpanel.setLayout(new BorderLayout());
            hbox.add(subpanel);

            subhbox = Box.createHorizontalBox();
            subpanel.add(subhbox, BorderLayout.WEST);

            subsubpanel = new JPanel();
            subsubpanel.setLayout(new GridLayout(2, 1));
            subsubpanel.add(searchInConstantPoolsCheckBoxDeclarations = new JCheckBox("Declarations", true));
            searchInConstantPoolsCheckBoxDeclarations.addItemListener(checkBoxListener);
            subsubpanel.add(searchInConstantPoolsCheckBoxReferences = new JCheckBox("References", true));
            searchInConstantPoolsCheckBoxReferences.addItemListener(checkBoxListener);
            subhbox.add(subsubpanel);

            vbox.add(Box.createVerticalStrut(10));

            hbox = Box.createHorizontalBox();
            hbox.add(searchInConstantPoolsLabel = new JLabel("Matching types:"));
            hbox.add(Box.createHorizontalGlue());
            vbox.add(hbox);

            vbox.add(Box.createVerticalStrut(10));
            panel.add(vbox, BorderLayout.NORTH);

            JScrollPane scrollPane = new JScrollPane(searchInConstantPoolsTree = new Tree());
            searchInConstantPoolsTree.setShowsRootHandles(true);
            searchInConstantPoolsTree.setModel(new DefaultTreeModel(new DefaultMutableTreeNode()));
            searchInConstantPoolsTree.setCellRenderer(new TreeNodeRenderer());
            searchInConstantPoolsTree.addKeyListener(new KeyAdapter() {
                @Override public void keyPressed(KeyEvent e) {
                    if (e.getKeyCode() == KeyEvent.VK_UP) {
                        if (searchInConstantPoolsTree.getLeadSelectionRow() == 0) {
                            searchInConstantPoolsEnterTextField.requestFocus();
                            e.consume();
                        }
                    }
                }
            });
            searchInConstantPoolsTree.addMouseListener(new MouseAdapter() {
                @Override public void mouseClicked(MouseEvent e) {
                    if (e.getClickCount() == 2) {
                        T node = (T)searchInConstantPoolsTree.getLastSelectedPathComponent();
                        if (node != null) {
                            selectedTypeCallback.accept(node.getUri(), searchInConstantPoolsEnterTextField.getText(), getFlags());
                        }
                    }
                }
            });
            searchInConstantPoolsTree.addTreeExpansionListener(new TreeExpansionListener() {
                @Override public void treeExpanded(TreeExpansionEvent e) {
                    DefaultTreeModel model = (DefaultTreeModel)searchInConstantPoolsTree.getModel();
                    T node = (T)e.getPath().getLastPathComponent();
                    // Expand node and find the first leaf
                    while (true) {
                        populate(model, node);
                        if (node.getChildCount() == 0) {
                            break;
                        }
                        node = (T)node.getChildAt(0);
                    }
                    searchInConstantPoolsTree.setSelectionPath(new TreePath(node.getPath()));
                }
                @Override public void treeCollapsed(TreeExpansionEvent e) {}
            });
            scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
            scrollPane.setPreferredSize(new Dimension(400, 150));
            panel.add(scrollPane, BorderLayout.CENTER);

            vbox = Box.createVerticalBox();

            vbox.add(Box.createVerticalStrut(25));

            hbox = Box.createHorizontalBox();
            hbox.add(Box.createHorizontalGlue());
            JButton searchInConstantPoolsOpenButton = new JButton("Open");
            hbox.add(searchInConstantPoolsOpenButton);
            searchInConstantPoolsOpenButton.setEnabled(false);
            Action searchInConstantPoolsOpenActionListener = new AbstractAction() {
                @Override public void actionPerformed(ActionEvent actionEvent) {
                    T selectedTreeNode = (T)searchInConstantPoolsTree.getLastSelectedPathComponent();
                    if (selectedTreeNode != null) {
                        selectedTypeCallback.accept(selectedTreeNode.getUri(), searchInConstantPoolsEnterTextField.getText(), getFlags());
                    }
                }
            };
            searchInConstantPoolsOpenButton.addActionListener(searchInConstantPoolsOpenActionListener);
            hbox.add(Box.createHorizontalStrut(5));
            JButton searchInConstantPoolsCancelButton = new JButton("Cancel");
            hbox.add(searchInConstantPoolsCancelButton);
            Action searchInConstantPoolsCancelActionListener = new AbstractAction() {
                @Override public void actionPerformed(ActionEvent actionEvent) { searchInConstantPoolsDialog.setVisible(false); }
            };
            searchInConstantPoolsCancelButton.addActionListener(searchInConstantPoolsCancelActionListener);

            vbox.add(hbox);

            panel.add(vbox, BorderLayout.SOUTH);

            // Last setup
            JRootPane rootPane = searchInConstantPoolsDialog.getRootPane();
            rootPane.setDefaultButton(searchInConstantPoolsOpenButton);
            rootPane.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "SearchInConstantPoolsView.cancel");
            rootPane.getActionMap().put("SearchInConstantPoolsView.cancel", searchInConstantPoolsCancelActionListener);

            searchInConstantPoolsDialog.setMinimumSize(searchInConstantPoolsDialog.getSize());

            searchInConstantPoolsEnterTextField.addFocusListener(new FocusAdapter() {
                @Override public void focusGained(FocusEvent e) {
                    searchInConstantPoolsTree.clearSelection();
                    searchInConstantPoolsOpenButton.setEnabled(false);
                }
            });

            searchInConstantPoolsTree.addFocusListener(new FocusAdapter() {
                @Override public void focusGained(FocusEvent e) {
                    searchInConstantPoolsOpenButton.setEnabled(searchInConstantPoolsTree.getSelectionCount() > 0);
                }
            });

            // Prepare to display
            searchInConstantPoolsDialog.pack();
            searchInConstantPoolsDialog.setLocationRelativeTo(searchInConstantPoolsDialog.getParent());
        });
    }

    @SuppressWarnings("unchecked")
    protected void populate(DefaultTreeModel model, T node) {
        // Populate node
        populate(node);
        // Populate children
        int i = node.getChildCount();
        while (i-- > 0) {
            T child = (T)node.getChildAt(i);
            if ((child instanceof TreeNodeExpandable) && !expanded.contains(child.getUri())) {
                populate(child);
            }
        }
        // Refresh
        model.reload(node);
    }

    @SuppressWarnings("unchecked")
    protected void populate(T node) {
        if ((node instanceof TreeNodeExpandable) && !expanded.contains(node.getUri())) {
            // Populate
            ((TreeNodeExpandable)node).populateTreeNode(api);
            expanded.add(node.getUri());
            // Filter
            int i = node.getChildCount();
            while (i-- > 0) {
                if (!accepted.contains(((T)node.getChildAt(i)).getUri())) {
                    node.remove(i);
                }
            }
        }
    }

    public void show() {
        SwingUtil.invokeLater(() -> {
            searchInConstantPoolsEnterTextField.selectAll();
            // Show
            searchInConstantPoolsDialog.setVisible(true);
            searchInConstantPoolsEnterTextField.requestFocus();
        });
    }

    public boolean isVisible() { return searchInConstantPoolsDialog.isVisible(); }

    public String getPattern() { return searchInConstantPoolsEnterTextField.getText(); }

    public int getFlags() {
        int flags = 0;

        if (searchInConstantPoolsCheckBoxType.isSelected())
            flags += SEARCH_TYPE;
        if (searchInConstantPoolsCheckBoxConstructor.isSelected())
            flags += SEARCH_CONSTRUCTOR;
        if (searchInConstantPoolsCheckBoxMethod.isSelected())
            flags += SEARCH_METHOD;
        if (searchInConstantPoolsCheckBoxField.isSelected())
            flags += SEARCH_FIELD;
        if (searchInConstantPoolsCheckBoxString.isSelected())
            flags += SEARCH_STRING;
        if (searchInConstantPoolsCheckBoxModule.isSelected())
            flags += SEARCH_MODULE;
        if (searchInConstantPoolsCheckBoxDeclarations.isSelected())
            flags += SEARCH_DECLARATION;
        if (searchInConstantPoolsCheckBoxReferences.isSelected())
            flags += SEARCH_REFERENCE;

        return flags;
    }

    public void showWaitCursor() {
        SwingUtil.invokeLater(() -> searchInConstantPoolsDialog.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)));
    }

    public void hideWaitCursor() {
        SwingUtil.invokeLater(() -> searchInConstantPoolsDialog.setCursor(Cursor.getDefaultCursor()));
    }

    @SuppressWarnings("unchecked")
    public void updateTree(Collection<DelegatingFilterContainer> containers, int matchingTypeCount) {
        SwingUtil.invokeLater(() -> {
            DefaultTreeModel model = (DefaultTreeModel)searchInConstantPoolsTree.getModel();
            T root = (T)model.getRoot();

            // Reset tree nodes
            root.removeAllChildren();

            accepted.clear();
            expanded.clear();

            if (containers != null) {
                ArrayList<DelegatingFilterContainer> list = new ArrayList<>(containers);

                list.sort(CONTAINER_COMPARATOR);

                for (DelegatingFilterContainer container : list) {
                    // Init uri set
                    accepted.addAll(container.getUris());
                    // Populate tree
                    Container.Entry parentEntry = container.getRoot().getParent();
                    TreeNodeFactory treeNodeFactory = api.getTreeNodeFactory(parentEntry);

                    if (treeNodeFactory != null) {
                        root.add(treeNodeFactory.make(api, parentEntry));
                    }
                }

                // Expand node and find the first leaf
                T node = root;
                while (true) {
                    populate(model, node);
                    if (node.getChildCount() == 0) {
                        break;
                    }
                    node = (T)node.getChildAt(0);
                }
                searchInConstantPoolsTree.setSelectionPath(new TreePath(node.getPath()));
            } else {
                model.reload();
            }

            // Update matching item counter
            switch (matchingTypeCount) {
                case 0:
                    searchInConstantPoolsLabel.setText("Matching entries:");
                    break;
                case 1:
                    searchInConstantPoolsLabel.setText("1 matching entry:");
                    break;
                default:
                    searchInConstantPoolsLabel.setText(matchingTypeCount + " matching entries:");
            }
        });
    }

    protected static class ContainerComparator implements Comparator<Container> {
        @Override
        public int compare(Container c1, Container c2) {
            return c1.getRoot().getUri().compareTo(c2.getRoot().getUri());
        }
    }
}