From 72b706f031363ff4a88fe9dde461666edde29113 Mon Sep 17 00:00:00 2001 From: soarcreator Date: Sun, 16 Sep 2018 16:37:34 -0700 Subject: [PATCH 1/2] Allow tabs to adjust their size when there are too many tabs I fixed the problem when there are too many tabs on the Editor header. When the remaining space is not enough, all tabs become as small as it is necesary to be to squash into the space. --- app/src/processing/app/EditorHeader.java | 37 ++++++++++++++++++++---- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/app/src/processing/app/EditorHeader.java b/app/src/processing/app/EditorHeader.java index 25c09a8dfaa..2e36a4c3dfe 100644 --- a/app/src/processing/app/EditorHeader.java +++ b/app/src/processing/app/EditorHeader.java @@ -32,6 +32,7 @@ import java.awt.*; import java.awt.event.*; import java.io.IOException; +import java.util.ArrayList; import java.util.List; import javax.swing.*; @@ -241,19 +242,45 @@ public void paintComponent(Graphics screen) { tabRight = new int[codeCount]; } - int x = scale(6); // offset from left edge of the component - int i = 0; + List texts = new ArrayList(); + int totalWidth = 0; for (EditorTab tab : tabs) { SketchFile file = tab.getSketchFile(); String filename = file.getPrettyName(); // if modified, add the li'l glyph next to the name - String text = " " + filename + (file.isModified() ? " \u00A7" : " "); + String text = filename; + if (file.isModified()) + text += " \u00A7"; + texts.add(text); + + int textWidth = (int) + font.getStringBounds(text, g.getFontRenderContext()).getWidth(); + + int pieceCount = textWidth / PIECE_WIDTH; + int pieceWidth = pieceCount * PIECE_WIDTH; + totalWidth += PIECE_WIDTH * (pieceCount + 2) - 1; // left + middle + right - overlap(=1) + } + + int marginPieceCount = 0; + int marginForEveryPiece = PIECE_WIDTH * tabs.size(); + for (int i = 1; i <= 6; ++i) { + if (sizeW - totalWidth > marginForEveryPiece * i) { + marginPieceCount = i; + continue; + } + break; + } + + int x = scale(6); // offset from left edge of the component + int i = 0; + for (EditorTab tab : tabs) { + String text = texts.get(i); int textWidth = (int) font.getStringBounds(text, g.getFontRenderContext()).getWidth(); - int pieceCount = 2 + (textWidth / PIECE_WIDTH); + int pieceCount = marginPieceCount + (textWidth / PIECE_WIDTH); int pieceWidth = pieceCount * PIECE_WIDTH; int state = (i == editor.getCurrentTabIndex()) ? SELECTED : UNSELECTED; @@ -276,7 +303,7 @@ public void paintComponent(Graphics screen) { g.drawImage(pieces[state][RIGHT], x, 0, null); x += PIECE_WIDTH - 1; // overlap by 1 pixel - i++; + ++i; } menuLeft = sizeW - (16 + menuButtons[0].getWidth(this)); From e2a60ac944a8bf006d158d420b58ade8df78d9b4 Mon Sep 17 00:00:00 2001 From: soarcreator Date: Tue, 18 Sep 2018 12:07:24 -0700 Subject: [PATCH 2/2] Highlighting the words yellow in finding and replacing them When finding and replacing the words, all these words are now highlighted yellow for the sake of visibility. --- .../arduino/view/findreplace/FindReplace.java | 124 ++++++++++++++---- 1 file changed, 100 insertions(+), 24 deletions(-) diff --git a/app/src/cc/arduino/view/findreplace/FindReplace.java b/app/src/cc/arduino/view/findreplace/FindReplace.java index 03e7b10947d..df32d1b1684 100644 --- a/app/src/cc/arduino/view/findreplace/FindReplace.java +++ b/app/src/cc/arduino/view/findreplace/FindReplace.java @@ -33,6 +33,7 @@ import processing.app.Editor; import processing.app.EditorTab; import processing.app.helpers.OSUtils; +import processing.app.syntax.SketchTextArea; import java.awt.*; import java.awt.event.WindowAdapter; @@ -40,7 +41,13 @@ import javax.swing.JPopupMenu; import javax.swing.Action; import javax.swing.text.DefaultEditorKit; +import javax.swing.text.BadLocationException; +import javax.swing.text.DefaultHighlighter; +import javax.swing.text.Highlighter; +import javax.swing.text.Highlighter.Highlight; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; import static processing.app.I18n.tr; @@ -81,6 +88,10 @@ public void windowActivated(WindowEvent e) { findField.requestFocusInWindow(); findField.selectAll(); } + + public void windowDeactivated(WindowEvent windowEvent) { + clearHighlights(); + } }); restoreFindDialogState(state); @@ -167,18 +178,18 @@ private void initComponents() { JPopupMenu menu = new JPopupMenu(); Action cut = new DefaultEditorKit.CutAction(); cut.putValue(Action.NAME, tr("Cut")); - menu.add( cut ); + menu.add(cut); Action copy = new DefaultEditorKit.CopyAction(); copy.putValue(Action.NAME, tr("Copy")); - menu.add( copy ); + menu.add(copy); Action paste = new DefaultEditorKit.PasteAction(); paste.putValue(Action.NAME, tr("Paste")); - menu.add( paste ); + menu.add(paste); - findField.setComponentPopupMenu( menu ); - replaceField.setComponentPopupMenu( menu ); + findField.setComponentPopupMenu(menu); + replaceField.setComponentPopupMenu(menu); findButton.setText(tr("Find")); findButton.addActionListener(new java.awt.event.ActionListener() { @@ -303,38 +314,85 @@ private void replaceAllButtonActionPerformed(java.awt.event.ActionEvent evt) {// private javax.swing.JCheckBox wrapAroundBox; // End of variables declaration//GEN-END:variables + private List foundHighlights = new ArrayList(); + private Highlight selectedHighlight = null; + private int selectedHighlightIndex = -1; + private String lastSearchText = ""; + private String lastText = ""; + + private void clearHighlights() { + Highlighter highlighter = editor.getCurrentTab().getTextArea().getHighlighter(); + for (Highlight highlight : foundHighlights) { + editor.getCurrentTab().getTextArea().getHighlighter().removeHighlight(highlight); + } + foundHighlights.clear(); + selectedHighlight = null; + lastSearchText = ""; + lastText = ""; + } + private boolean find(boolean wrap, boolean backwards, boolean searchTabs, int originTab) { - String search = findField.getText(); + String searchText = findField.getText(); + int searchTextLength = searchText.length(); - if (search.length() == 0) { + if (searchTextLength == 0) { return false; } - String text = editor.getCurrentTab().getText(); + EditorTab currentTab = editor.getCurrentTab(); + SketchTextArea textarea = currentTab.getTextArea(); + String text = currentTab.getText(); if (ignoreCaseBox.isSelected()) { - search = search.toLowerCase(); + searchText = searchText.toLowerCase(); text = text.toLowerCase(); } - int nextIndex; + // search all indexes + if (!lastSearchText.equals(searchText) || !lastText.equals(text)) { + clearHighlights(); + + int index = text.indexOf(searchText, 0); + while (index >= 0) { + int endIndex = index + searchTextLength; + + try { + foundHighlights.add((Highlight)textarea.getHighlighter().addHighlight(index, endIndex, + new DefaultHighlighter.DefaultHighlightPainter(Color.yellow))); + } catch (BadLocationException e1) { + e1.printStackTrace(); + } + + index = text.indexOf(searchText, endIndex); + } + + lastSearchText = searchText; + lastText = text; + } + + Highlight nextHighlight = null; if (!backwards) { - // int selectionStart = editor.textarea.getSelectionStart(); int selectionEnd = editor.getCurrentTab().getSelectionStop(); - nextIndex = text.indexOf(search, selectionEnd); + for (Highlight h : foundHighlights) { + if (h.getStartOffset() < selectionEnd) { + continue; + } + nextHighlight = h; + break; + } } else { - // int selectionStart = editor.textarea.getSelectionStart(); int selectionStart = editor.getCurrentTab().getSelectionStart() - 1; - if (selectionStart >= 0) { - nextIndex = text.lastIndexOf(search, selectionStart); - } else { - nextIndex = -1; + for (Highlight h : foundHighlights) { + if (h.getStartOffset() > selectionStart) { + break; + } + nextHighlight = h; } } - if (nextIndex == -1) { + if (nextHighlight == null) { // Nothing found on this tab: Search other tabs if required if (searchTabs) { int numTabs = editor.getTabs().size(); @@ -353,6 +411,8 @@ private boolean find(boolean wrap, boolean backwards, boolean searchTabs, int or } } + clearHighlights(); + if (backwards) { editor.selectPrevTab(); this.setVisible(true); @@ -370,15 +430,31 @@ private boolean find(boolean wrap, boolean backwards, boolean searchTabs, int or } if (wrap) { - nextIndex = backwards ? text.lastIndexOf(search) : text.indexOf(search, 0); + nextHighlight = backwards ? foundHighlights.get(foundHighlights.size() - 1) : foundHighlights.get(0); } } - if (nextIndex != -1) { - EditorTab currentTab = editor.getCurrentTab(); - currentTab.getTextArea().getFoldManager().ensureOffsetNotInClosedFold(nextIndex); - currentTab.setSelection(nextIndex, nextIndex + search.length()); - currentTab.getTextArea().getCaret().setSelectionVisible(true); + if (nextHighlight != null) { + Highlighter highlighter = textarea.getHighlighter(); + int nextIndex = nextHighlight.getStartOffset(); + + try { + if (selectedHighlight != null) { + foundHighlights.add(selectedHighlightIndex, (Highlight)highlighter.addHighlight(selectedHighlight.getStartOffset(), selectedHighlight.getEndOffset(), selectedHighlight.getPainter())); + } + selectedHighlight = nextHighlight; + selectedHighlightIndex = foundHighlights.indexOf(selectedHighlight); + foundHighlights.remove(selectedHighlight); + + highlighter.removeHighlight(selectedHighlight); + + } catch (BadLocationException bl) { + bl.printStackTrace(); + } + + textarea.getFoldManager().ensureOffsetNotInClosedFold(nextIndex); + currentTab.setSelection(nextIndex, nextIndex + searchTextLength); + textarea.getCaret().setSelectionVisible(true); return true; }