From 54c219a218ca50331da28e4d12cccdc06cba608a Mon Sep 17 00:00:00 2001 From: Wisser Date: Wed, 11 Apr 2018 11:47:58 +0200 Subject: [PATCH] set caret to error position --- .../databrowser/metadata/MetaDataPanel.java | 4 +- .../ui/databrowser/sqlconsole/ErrorPanel.java | 15 ++++++- .../ui/databrowser/sqlconsole/SQLConsole.java | 45 ++++++++++++------- 3 files changed, 45 insertions(+), 19 deletions(-) diff --git a/src/main/gui/net/sf/jailer/ui/databrowser/metadata/MetaDataPanel.java b/src/main/gui/net/sf/jailer/ui/databrowser/metadata/MetaDataPanel.java index e14b61abe..b2a68018b 100644 --- a/src/main/gui/net/sf/jailer/ui/databrowser/metadata/MetaDataPanel.java +++ b/src/main/gui/net/sf/jailer/ui/databrowser/metadata/MetaDataPanel.java @@ -549,7 +549,7 @@ public abstract class MetaDataPanel extends javax.swing.JPanel { if (i >= 0) { Object value = outlineList.getModel().getElementAt(i); if (value instanceof OutlineInfo) { - setCaretPosition(((OutlineInfo) value).position); + setCaretPosition(((OutlineInfo) value).origPosition); } } } @@ -1534,12 +1534,14 @@ public abstract class MetaDataPanel extends javax.swing.JPanel { public boolean isEnd; public boolean withSeparator; public int rowCount; + public int origPosition; public OutlineInfo(MDTable mdTable, String alias, int level, int position, String scopeDescriptor) { this.mdTable = mdTable; this.alias = alias; this.level = level; this.position = position; + this.origPosition = position; this.scopeDescriptor = scopeDescriptor; } } diff --git a/src/main/gui/net/sf/jailer/ui/databrowser/sqlconsole/ErrorPanel.java b/src/main/gui/net/sf/jailer/ui/databrowser/sqlconsole/ErrorPanel.java index 097f4332c..7ad30ea49 100644 --- a/src/main/gui/net/sf/jailer/ui/databrowser/sqlconsole/ErrorPanel.java +++ b/src/main/gui/net/sf/jailer/ui/databrowser/sqlconsole/ErrorPanel.java @@ -15,10 +15,13 @@ */ package net.sf.jailer.ui.databrowser.sqlconsole; +import java.awt.Rectangle; + import javax.swing.Icon; import javax.swing.JLabel; import javax.swing.JScrollPane; import javax.swing.UIManager; +import javax.swing.text.BadLocationException; import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea; import org.fife.ui.rsyntaxtextarea.SyntaxConstants; @@ -37,8 +40,9 @@ public class ErrorPanel extends javax.swing.JPanel { * * @param errorMessage the error message * @param statement the statement + * @param errorPosition error position */ - public ErrorPanel(String errorMessage, String statement) { + public ErrorPanel(String errorMessage, String statement, int errorPosition) { initComponents(); errorTextArea.setText(errorMessage); errorTextArea.setCaretPosition(0); @@ -59,7 +63,14 @@ public class ErrorPanel extends javax.swing.JPanel { if (statement != null) { sqlEditorPane.setText(statement.trim()); } - sqlEditorPane.setCaretPosition(0); + try { + int l = sqlEditorPane.getLineOfOffset(errorPosition); + sqlEditorPane.setCaretPosition(errorPosition); + int lineHeight = sqlEditorPane.getLineHeight(); + sqlEditorPane.scrollRectToVisible(new Rectangle(0, Math.max(0, l - 2) * lineHeight, 1, 4 * lineHeight)); + } catch (Exception e) { + // ignore + } } /** diff --git a/src/main/gui/net/sf/jailer/ui/databrowser/sqlconsole/SQLConsole.java b/src/main/gui/net/sf/jailer/ui/databrowser/sqlconsole/SQLConsole.java index 9223a2014..8ba8d2db3 100644 --- a/src/main/gui/net/sf/jailer/ui/databrowser/sqlconsole/SQLConsole.java +++ b/src/main/gui/net/sf/jailer/ui/databrowser/sqlconsole/SQLConsole.java @@ -23,6 +23,7 @@ import java.awt.Font; import java.awt.Frame; import java.awt.GridBagConstraints; import java.awt.Insets; +import java.awt.Rectangle; import java.awt.Window; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; @@ -392,7 +393,7 @@ public abstract class SQLConsole extends javax.swing.JPanel { return; } - if (sql.length() > 100000) { + if (sql.length() > 20000) { stopped.set(false); pending.set(true); new Thread(new Runnable() { @@ -856,13 +857,14 @@ public abstract class SQLConsole extends javax.swing.JPanel { positionOffset = floor.getValue(); } status.errorPosition = statementStartOffset + pos + positionOffset; + status.origErrorPosition = pos; status.errorPositionIsKnown = true; } else { status.errorPosition = statementStartOffset; status.errorPositionIsKnown = false; } } - + if (error instanceof CancellationException) { CancellationHandler.reset(SQLConsole.this); queue.clear(); @@ -888,6 +890,14 @@ public abstract class SQLConsole extends javax.swing.JPanel { public void setCaretPosition(int position) { if (editorPane.getDocument().getLength() >= position) { + try { + int l = editorPane.getLineOfOffset(position); + editorPane.setCaretPosition(position); + int lineHeight = editorPane.getLineHeight(); + editorPane.scrollRectToVisible(new Rectangle(0, Math.max(0, l - 2) * lineHeight, 1, 4 * lineHeight)); + } catch (Exception e) { + // ignore + } editorPane.setCaretPosition(position); grabFocus(); } @@ -898,23 +908,26 @@ public abstract class SQLConsole extends javax.swing.JPanel { final int MAX_TOOLTIP_LENGTH = 100; List outlineInfos = new ArrayList(); TreeMap offsets = new TreeMap(); - provider.findAliases(SQLCompletionProvider.removeCommentsAndLiterals(sql), null, outlineInfos); sql = sqlPlusSupport.replaceVariables(sql, offsets); + provider.findAliases(SQLCompletionProvider.removeCommentsAndLiterals(sql), null, outlineInfos); adjustLevels(outlineInfos); List relocatedOutlineInfos = new ArrayList(); int indexOfInfoAtCaret = -1; int caretPos = editorPane.getCaretPosition(); OutlineInfo predInfo = null; + for (int i = 0; i < outlineInfos.size(); ++i) { + OutlineInfo info = outlineInfos.get(i); + Entry floor = offsets.floorEntry(info.position); + if (floor != null) { + info.origPosition = info.position + floor.getValue(); + } + } for (int i = 0; i < outlineInfos.size(); ++i) { OutlineInfo info = outlineInfos.get(i); if (info.isBegin || info.isEnd) { continue; } int pos = info.position; - Entry floor = offsets.floorEntry(pos); - if (floor != null) { - pos += floor.getValue(); - } if (pos + startPosition <= caretPos || indexOfInfoAtCaret < 0) { indexOfInfoAtCaret = relocatedOutlineInfos.size(); } @@ -966,6 +979,7 @@ public abstract class SQLConsole extends javax.swing.JPanel { if (succ == null || (succ.level != info.level || succ.mdTable == null)) { info.scopeDescriptor = pred.scopeDescriptor; info.position = pred.position; + info.origPosition = pred.origPosition; toRemove.add(pred); if (i - 1 < indexOfInfoAtCaret) { ++caretOffset; @@ -1064,7 +1078,8 @@ public abstract class SQLConsole extends javax.swing.JPanel { } private class Status { - public int errorPosition = -1; + public int origErrorPosition; + public int errorPosition = -1; public boolean errorPositionIsKnown = false; protected int linesExecuting; protected int linesExecuted; @@ -1114,20 +1129,19 @@ public abstract class SQLConsole extends javax.swing.JPanel { int col = errorPosition - editorPane.getLineStartOffset(errorLine) + 1; pos = "Error at line " + (errorLine + 1) + ", column " + col + ": "; } - editorPane.setCaretPosition(errorPosition); - editorPane.grabFocus(); + setCaretPosition(errorPosition); } catch (BadLocationException e) { } if (errorLine >= 0) { editorPane.setLineTrackingIcon(errorLine, scaledCancelIcon); } - showError(pos + error.getMessage(), statement); + showError(pos + error.getMessage(), statement, origErrorPosition); } else { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); error.printStackTrace(pw); String sStackTrace = sw.toString(); // stack trace as a string - showError(sStackTrace, statement); + showError(sStackTrace, statement, origErrorPosition); } } } @@ -1199,14 +1213,14 @@ public abstract class SQLConsole extends javax.swing.JPanel { } } - private void showError(String errorMessage, String statement) { + private void showError(String errorMessage, String statement, int errorPosition) { statusLabel.setVisible(true); statusLabel.setForeground(Color.RED); statusLabel.setText("Error"); removeLastErrorTab(); - JComponent rTabContainer = new ErrorPanel(errorMessage, statement); + JComponent rTabContainer = new ErrorPanel(errorMessage, statement, errorPosition); jTabbedPane1.add(rTabContainer); jTabbedPane1.setTabComponentAt(jTabbedPane1.indexOfComponent(rTabContainer), getTitlePanel(jTabbedPane1, rTabContainer, "Error")); @@ -1609,8 +1623,7 @@ public abstract class SQLConsole extends javax.swing.JPanel { sql += ";"; } editorPane.append(pre + sql + "\n"); - editorPane.setCaretPosition(editorPane.getDocument().getLength()); - editorPane.grabFocus(); + setCaretPosition(editorPane.getDocument().getLength()); if (!running.get()) { resetStatus(); }