Codebase list jd-gui / 51036aa1-31f9-4199-8deb-e896fba34b49/upstream services / src / main / java / org / jd / gui / view / component / AbstractTextPage.java
51036aa1-31f9-4199-8deb-e896fba34b49/upstream

Tree @51036aa1-31f9-4199-8deb-e896fba34b49/upstream (Download .tar.gz)

AbstractTextPage.java @51036aa1-31f9-4199-8deb-e896fba34b49/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
/*
 * 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.component;

import org.fife.ui.rsyntaxtextarea.*;
import org.fife.ui.rsyntaxtextarea.folding.FoldManager;
import org.fife.ui.rtextarea.*;
import org.jd.gui.api.feature.ContentSearchable;
import org.jd.gui.api.feature.LineNumberNavigable;
import org.jd.gui.api.feature.PreferencesChangeListener;
import org.jd.gui.api.feature.UriOpenable;
import org.jd.gui.util.exception.ExceptionUtil;

import javax.swing.*;
import javax.swing.text.BadLocationException;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelListener;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

public class AbstractTextPage extends JPanel implements LineNumberNavigable, ContentSearchable, UriOpenable, PreferencesChangeListener {
    protected static final String FONT_SIZE_KEY = "ViewerPreferences.fontSize";

    protected static final ImageIcon COLLAPSED_ICON = new ImageIcon(AbstractTextPage.class.getClassLoader().getResource("org/jd/gui/images/plus.png"));
    protected static final ImageIcon EXPANDED_ICON = new ImageIcon(AbstractTextPage.class.getClassLoader().getResource("org/jd/gui/images/minus.png"));

    protected static final Color DOUBLE_CLICK_HIGHLIGHT_COLOR = new Color(0x66ff66);
    protected static final Color SEARCH_HIGHLIGHT_COLOR = new Color(0xffff66);
    protected static final Color SELECT_HIGHLIGHT_COLOR = new Color(0xF49810);

    protected static final RSyntaxTextAreaEditorKit.DecreaseFontSizeAction DECREASE_FONT_SIZE_ACTION = new RSyntaxTextAreaEditorKit.DecreaseFontSizeAction();
    protected static final RSyntaxTextAreaEditorKit.IncreaseFontSizeAction INCREASE_FONT_SIZE_ACTION = new RSyntaxTextAreaEditorKit.IncreaseFontSizeAction();

    protected RSyntaxTextArea textArea;
    protected RTextScrollPane scrollPane;

    protected Map<String, String> preferences;

    public AbstractTextPage() {
        super(new BorderLayout());

        textArea = newSyntaxTextArea();
        textArea.setSyntaxEditingStyle(getSyntaxStyle());
        textArea.setCodeFoldingEnabled(true);
        textArea.setAntiAliasingEnabled(true);
        textArea.setCaretPosition(0);
        textArea.setEditable(false);
        textArea.setDropTarget(null);
        textArea.setPopupMenu(null);
        textArea.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 2) {
                    textArea.setMarkAllHighlightColor(DOUBLE_CLICK_HIGHLIGHT_COLOR);
                    SearchEngine.markAll(textArea, newSearchContext(textArea.getSelectedText(), true, true, true, false));
                }
            }
        });

        KeyStroke ctrlA = KeyStroke.getKeyStroke(KeyEvent.VK_A, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask());
        KeyStroke ctrlC = KeyStroke.getKeyStroke(KeyEvent.VK_C, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask());
        KeyStroke ctrlV = KeyStroke.getKeyStroke(KeyEvent.VK_V, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask());
        InputMap inputMap = textArea.getInputMap();
        inputMap.put(ctrlA, "none");
        inputMap.put(ctrlC, "none");
        inputMap.put(ctrlV, "none");

        try {
            Theme theme = Theme.load(getClass().getClassLoader().getResourceAsStream("rsyntaxtextarea/themes/eclipse.xml"));
            theme.apply(textArea);
        } catch (IOException e) {
            assert ExceptionUtil.printStackTrace(e);
        }

        scrollPane = new RTextScrollPane(textArea);
        scrollPane.setFoldIndicatorEnabled(true);
        scrollPane.setFont(textArea.getFont());

        final MouseWheelListener[] mouseWheelListeners = scrollPane.getMouseWheelListeners();

        // Remove default listeners
        for (MouseWheelListener listener : mouseWheelListeners) {
            scrollPane.removeMouseWheelListener(listener);
        }

        scrollPane.addMouseWheelListener(e -> {
            if ((e.getModifiers() & (Event.META_MASK|Event.CTRL_MASK)) != 0) {
                int x = e.getX() + scrollPane.getX() - textArea.getX();
                int y = e.getY() + scrollPane.getY() - textArea.getY();
                int offset = textArea.viewToModel(new Point(x, y));

                // Update font size
                if (e.getWheelRotation() > 0) {
                    DECREASE_FONT_SIZE_ACTION.actionPerformedImpl(null, textArea);
                } else {
                    INCREASE_FONT_SIZE_ACTION.actionPerformedImpl(null, textArea);
                }

                // Save preferences
                if (preferences != null) {
                    preferences.put(FONT_SIZE_KEY, String.valueOf(textArea.getFont().getSize()));
                }

                try {
                    Rectangle newRectangle = textArea.modelToView(offset);
                    int newY = newRectangle.y + (newRectangle.height >> 1);

                    // Scroll
                    Point viewPosition = scrollPane.getViewport().getViewPosition();
                    viewPosition.y = Math.max(viewPosition.y +newY - y, 0);
                    scrollPane.getViewport().setViewPosition(viewPosition);
                } catch (BadLocationException ee) {
                    assert ExceptionUtil.printStackTrace(ee);
                }
            } else {
                // Call default listeners
                for (MouseWheelListener listener : mouseWheelListeners) {
                    listener.mouseWheelMoved(e);
                }
            }
        });

        Gutter gutter = scrollPane.getGutter();
        gutter.setFoldIcons(COLLAPSED_ICON, EXPANDED_ICON);
        gutter.setFoldIndicatorForeground(gutter.getBorderColor());

        add(scrollPane, BorderLayout.CENTER);
        add(new RoundMarkErrorStrip(textArea), BorderLayout.LINE_END);
    }

    protected RSyntaxTextArea newSyntaxTextArea() { return new RSyntaxTextArea(); }

    public String getText() { return textArea.getText(); }

    public JScrollPane getScrollPane() {
        return scrollPane;
    }

    public void setText(String text) {
        textArea.setText(text);
        textArea.setCaretPosition(0);
    }

    public String getSyntaxStyle() { return SyntaxConstants.SYNTAX_STYLE_NONE; }

    /**
     * @see org.fife.ui.rsyntaxtextarea.RSyntaxUtilities#selectAndPossiblyCenter
     * Force center and do not select
     */
    public void setCaretPositionAndCenter(DocumentRange range) {
        final int start = range.getStartOffset();
        final int end = range.getEndOffset();
        boolean foldsExpanded = false;
        FoldManager fm = textArea.getFoldManager();

        if (fm.isCodeFoldingSupportedAndEnabled()) {
            foldsExpanded = fm.ensureOffsetNotInClosedFold(start);
            foldsExpanded |= fm.ensureOffsetNotInClosedFold(end);
        }

        if (!foldsExpanded) {
            try {
                Rectangle rec = textArea.modelToView(start);

                if (rec != null) {
                    // Visible
                    setCaretPositionAndCenter(start, end, rec);
                } else {
                    // Not visible yet
                    SwingUtilities.invokeLater(() -> {
                        try {
                            Rectangle r = textArea.modelToView(start);
                            if (r != null) {
                                setCaretPositionAndCenter(start, end, r);
                            }
                        } catch (BadLocationException e) {
                            assert ExceptionUtil.printStackTrace(e);
                        }
                    });
                }
            } catch (BadLocationException e) {
                assert ExceptionUtil.printStackTrace(e);
            }
        }
    }

    protected void setCaretPositionAndCenter(int start, int end, Rectangle r) {
        if (end != start) {
            try {
                r = r.union(textArea.modelToView(end));
            } catch (BadLocationException e) {
                assert ExceptionUtil.printStackTrace(e);
            }
        }

        Rectangle visible = textArea.getVisibleRect();

        // visible.x = r.x - (visible.width - r.width) / 2;
        visible.y = r.y - (visible.height - r.height) / 2;

        Rectangle bounds = textArea.getBounds();
        Insets i = textArea.getInsets();
        //bounds.x = i.left;
        bounds.y = i.top;
        //bounds.width -= i.left + i.right;
        bounds.height -= i.top + i.bottom;

        //if (visible.x < bounds.x) {
        //    visible.x = bounds.x;
        //}
        //if (visible.x + visible.width > bounds.x + bounds.width) {
        //    visible.x = bounds.x + bounds.width - visible.width;
        //}
        if (visible.y < bounds.y) {
            visible.y = bounds.y;
        }
        if (visible.y + visible.height > bounds.y + bounds.height) {
            visible.y = bounds.y + bounds.height - visible.height;
        }

        textArea.scrollRectToVisible(visible);
        textArea.setCaretPosition(start);
    }

    // --- LineNumberNavigable --- //
    public int getMaximumLineNumber() {
        try {
            return textArea.getLineOfOffset(textArea.getDocument().getLength()) + 1;
        } catch (BadLocationException e) {
            assert ExceptionUtil.printStackTrace(e);
            return 0;
        }
    }

    public void goToLineNumber(int lineNumber) {
        try {
            textArea.setCaretPosition(textArea.getLineStartOffset(lineNumber-1));
        } catch (BadLocationException e) {
            assert ExceptionUtil.printStackTrace(e);
        }
    }

    public boolean checkLineNumber(int lineNumber) { return true; }

    // --- ContentSearchable --- //
    public boolean highlightText(String text, boolean caseSensitive) {
        if (text.length() > 1) {
            textArea.setMarkAllHighlightColor(SEARCH_HIGHLIGHT_COLOR);
            textArea.setCaretPosition(textArea.getSelectionStart());

            SearchContext context = newSearchContext(text, caseSensitive, false, true, false);
            SearchResult result = SearchEngine.find(textArea, context);

            if (!result.wasFound()) {
                textArea.setCaretPosition(0);
                result = SearchEngine.find(textArea, context);
            }

            return result.wasFound();
        } else {
            return true;
        }
    }

    public void findNext(String text, boolean caseSensitive) {
        if (text.length() > 1) {
            textArea.setMarkAllHighlightColor(SEARCH_HIGHLIGHT_COLOR);

            SearchContext context = newSearchContext(text, caseSensitive, false, true, false);
            SearchResult result = SearchEngine.find(textArea, context);

            if (!result.wasFound()) {
                textArea.setCaretPosition(0);
                SearchEngine.find(textArea, context);
            }
        }
    }

    public void findPrevious(String text, boolean caseSensitive) {
        if (text.length() > 1) {
            textArea.setMarkAllHighlightColor(SEARCH_HIGHLIGHT_COLOR);

            SearchContext context = newSearchContext(text, caseSensitive, false, false, false);
            SearchResult result = SearchEngine.find(textArea, context);

            if (!result.wasFound()) {
                textArea.setCaretPosition(textArea.getDocument().getLength());
                SearchEngine.find(textArea, context);
            }
        }
    }

    protected SearchContext newSearchContext(String searchFor, boolean matchCase, boolean wholeWord, boolean searchForward, boolean regexp) {
        SearchContext context = new SearchContext(searchFor, matchCase);
        context.setMarkAll(true);
        context.setWholeWord(wholeWord);
        context.setSearchForward(searchForward);
        context.setRegularExpression(regexp);
        return context;
    }

    // --- UriOpenable --- //
    public boolean openUri(URI uri) {
        String query = uri.getQuery();

        if (query != null) {
            Map<String, String> parameters = parseQuery(query);

            if (parameters.containsKey("lineNumber")) {
                String lineNumber = parameters.get("lineNumber");

                try {
                    goToLineNumber(Integer.parseInt(lineNumber));
                    return true;
                } catch (NumberFormatException e) {
                    assert ExceptionUtil.printStackTrace(e);
                }
            } else if (parameters.containsKey("position")) {
                String position = parameters.get("position");

                try {
                    int pos = Integer.parseInt(position);
                    if (textArea.getDocument().getLength() > pos) {
                        setCaretPositionAndCenter(new DocumentRange(pos, pos));
                        return true;
                    }
                } catch (NumberFormatException e) {
                    assert ExceptionUtil.printStackTrace(e);
                }
            } else if (parameters.containsKey("highlightFlags")) {
                String highlightFlags = parameters.get("highlightFlags");

                if ((highlightFlags.indexOf('s') != -1) && parameters.containsKey("highlightPattern")) {
                    textArea.setMarkAllHighlightColor(SELECT_HIGHLIGHT_COLOR);
                    textArea.setCaretPosition(0);

                    // Highlight all
                    String searchFor = createRegExp(parameters.get("highlightPattern"));
                    SearchContext context =  newSearchContext(searchFor, true, false, true, true);
                    SearchResult result = SearchEngine.find(textArea, context);

                    if (result.getMatchRange() != null) {
                        textArea.setCaretPosition(result.getMatchRange().getStartOffset());
                    }

                    return true;
                }
            }
        }

        return false;
    }

    protected Map<String, String> parseQuery(String query) {
        HashMap<String, String> parameters = new HashMap<>();

        // Parse parameters
        try {
            for (String param : query.split("&")) {
                int index = param.indexOf('=');

                if (index == -1) {
                    parameters.put(URLDecoder.decode(param, "UTF-8"), "");
                } else {
                    String key = param.substring(0, index);
                    String value = param.substring(index + 1);
                    parameters.put(URLDecoder.decode(key, "UTF-8"), URLDecoder.decode(value, "UTF-8"));
                }
            }
        } catch (UnsupportedEncodingException e) {
            assert ExceptionUtil.printStackTrace(e);
        }

        return parameters;
    }

    /**
     * Create a simple regular expression
     *
     * Rules:
     *  '*'        matchTypeEntries 0 ou N characters
     *  '?'        matchTypeEntries 1 character
     */
    public static String createRegExp(String pattern) {
        int patternLength = pattern.length();
        StringBuilder sbPattern = new StringBuilder(patternLength * 2);

        for (int i = 0; i < patternLength; i++) {
            char c = pattern.charAt(i);

            if (c == '*') {
                sbPattern.append(".*");
            } else if (c == '?') {
                sbPattern.append('.');
            } else if (c == '.') {
                sbPattern.append("\\.");
            } else {
                sbPattern.append(c);
            }
        }

        return sbPattern.toString();
    }

    // --- PreferencesChangeListener --- //
    public void preferencesChanged(Map<String, String> preferences) {
        String fontSize = preferences.get(FONT_SIZE_KEY);

        if (fontSize != null) {
            try {
                textArea.setFont(textArea.getFont().deriveFont(Float.parseFloat(fontSize)));
            } catch (Exception e) {
                assert ExceptionUtil.printStackTrace(e);
            }
        }

        this.preferences = preferences;
    }
}