1
2 /*
3 * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 *
6 * This code is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 only, as
8 * published by the Free Software Foundation. Oracle designates this
9 * particular file as subject to the "Classpath" exception as provided
10 * by Oracle in the LICENSE file that accompanied this code.
11 *
12 * This code is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * version 2 for more details (a copy is included in the LICENSE file that
16 * accompanied this code).
17 *
18 * You should have received a copy of the GNU General Public License version
19 * 2 along with this work; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
23 * or visit www.oracle.com if you need additional information or have any
24 * questions.
25 */
26 package hat.tools.textmodel.ui;
27
28 import hat.tools.textmodel.JavaTextModel;
29 import hat.tools.textmodel.TextModel;
30
31 import java.awt.Font;
32 import java.awt.Graphics;
33 import java.awt.event.MouseAdapter;
34 import java.awt.event.MouseEvent;
35 import java.util.HashMap;
36 import java.util.List;
37 import java.util.Map;
38
39 public class JavaTextModelViewer extends TextModelViewer {
40 FuncOpTextModelViewer funcOpTextModelViewer;
41 Map<ElementSpan, List<ElementSpan>> javaToOp = new HashMap<>();
42
43 static class JavaTextPane extends TextModelViewer.TextViewerPane<JavaTextModelViewer> {
44 public void paintComponent(Graphics g) {
45 super.paintComponent(g);
46 }
47 JavaTextPane(Font font, boolean editable) {
48 super(font, editable);
49 }
50 }
51
52 @Override
53 public TextModel createTextModel(String text) {
54 return JavaTextModel.of(styleMapper.jTextPane.getText());
55 }
56
57
58 JavaTextModelViewer(TextModel textModel,StyleMapper styleMapper) {
59 super(textModel, styleMapper);
60 final var thisTextViewer = this;
61 ((JavaTextPane) this.styleMapper.jTextPane).setViewer(this);
62 styleMapper.jTextPane.addMouseListener(new MouseAdapter() {
63 @Override
64 public void mouseClicked(MouseEvent e) {
65 var clickedElement = getElementFromMouseEvent(e);
66 funcOpTextModelViewer.removeHighlights();
67 removeHighlights();
68 if (clickedElement != null) {
69 var elementsReferencedByClickedElement = javaToOp.keySet().stream()
70 .filter(fromElementSpan ->
71 fromElementSpan.includes(clickedElement.getStartOffset())
72 ).toList();
73 if (!elementsReferencedByClickedElement.isEmpty()) {
74 elementsReferencedByClickedElement.forEach(fromElementSpan -> {
75 thisTextViewer.highlight(fromElementSpan, javaToOp.get(fromElementSpan));
76 });
77 }
78 }
79 }
80 });
81 }
82
83 }