OSDN Git Service

Correct numerous table problems.
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / gui / BrowserWindow.java
index e800124..46a4b30 100644 (file)
@@ -55,6 +55,7 @@ import com.swabunga.spell.event.SpellCheckListener;
 import com.swabunga.spell.event.SpellChecker;\r
 import com.swabunga.spell.event.StringWordTokenizer;\r
 import com.trolltech.qt.core.QByteArray;\r
+import com.trolltech.qt.core.QCoreApplication;\r
 import com.trolltech.qt.core.QDataStream;\r
 import com.trolltech.qt.core.QDateTime;\r
 import com.trolltech.qt.core.QEvent;\r
@@ -65,6 +66,7 @@ import com.trolltech.qt.core.QIODevice;
 import com.trolltech.qt.core.QMimeData;\r
 import com.trolltech.qt.core.QTextCodec;\r
 import com.trolltech.qt.core.QUrl;\r
+import com.trolltech.qt.core.Qt;\r
 import com.trolltech.qt.core.Qt.Key;\r
 import com.trolltech.qt.core.Qt.KeyboardModifier;\r
 import com.trolltech.qt.core.Qt.KeyboardModifiers;\r
@@ -72,6 +74,7 @@ import com.trolltech.qt.gui.QAction;
 import com.trolltech.qt.gui.QApplication;\r
 import com.trolltech.qt.gui.QCalendarWidget;\r
 import com.trolltech.qt.gui.QClipboard;\r
+import com.trolltech.qt.gui.QClipboard.Mode;\r
 import com.trolltech.qt.gui.QColor;\r
 import com.trolltech.qt.gui.QComboBox;\r
 import com.trolltech.qt.gui.QDateEdit;\r
@@ -236,7 +239,9 @@ public class BrowserWindow extends QWidget {
     SuggestionListener spellListener;\r
        private final HashMap<String,Integer> previewPageList;  \r
        boolean insertHyperlink = true;\r
-       \r
+       boolean insideTable = false;\r
+       boolean insideEncryption = false;\r
+\r
        \r
        public static class SuggestionListener implements SpellCheckListener {\r
                public boolean abortSpellCheck = false;\r
@@ -1049,11 +1054,40 @@ public class BrowserWindow extends QWidget {
                if (!mime.hasText())\r
                        return;\r
                String text = mime.text();\r
-               clipboard.setText(text);\r
+               clipboard.clear();\r
+               clipboard.setText(text, Mode.Clipboard);\r
                browser.page().triggerAction(WebAction.Paste);\r
-               QApplication.clipboard().setMimeData(mime);\r
-               browser.setFocus();\r
 \r
+               // This is done because pasting into an encryption block\r
+               // can cause multiple cells (which can't happen).  It \r
+               // just goes through the table, extracts the data, & \r
+               // puts it back as one table cell.\r
+               if (insideEncryption) {\r
+                       String js = new String( "function fixEncryption() { "\r
+                                       +"   var selObj = window.getSelection();"\r
+                                       +"   var selRange = selObj.getRangeAt(0);"\r
+                                       +"   var workingNode = window.getSelection().anchorNode;"\r
+                                       +"   while(workingNode != null && workingNode.nodeName.toLowerCase() != 'table') { " \r
+                                       +"           workingNode = workingNode.parentNode;"\r
+                                       +"   } "\r
+                                       +"   workingNode.innerHTML = window.jambi.fixEncryptionPaste(workingNode.innerHTML);"\r
+                                       +"} fixEncryption();");\r
+                       browser.page().mainFrame().evaluateJavaScript(js);\r
+               }\r
+       }\r
+       \r
+       // This basically removes all the table tags and returns just the contents.\r
+       // This is called by JavaScript to fix encryption pastes.\r
+       public String fixEncryptionPaste(String data) {\r
+               data = data.replace("<tbody>", "");\r
+               data = data.replace("</tbody>", "");\r
+               data = data.replace("<tr>", "");\r
+               data = data.replace("</tr>", "");\r
+               data = data.replace("<td>", "");\r
+               data = data.replace("</td>", "<br>");\r
+               data = data.replace("<br><br>", "<br>");\r
+\r
+               return "<tbody><tr><td>"+data+"</td></tr></tbody>";\r
        }\r
        \r
        // insert date/time\r
@@ -1612,19 +1646,110 @@ public class BrowserWindow extends QWidget {
 \r
        // Tab button was pressed\r
        public void tabPressed() {\r
-               if (!insideList) {\r
+               if (insideEncryption)\r
+                       return;\r
+               if (!insideList && !insideTable) {\r
                        String script_start = new String(\r
                        "document.execCommand('insertHtml', false, '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');");\r
                        browser.page().mainFrame().evaluateJavaScript(script_start);\r
-               } else \r
+                       return;\r
+               }\r
+               if (insideList) {\r
                        indentClicked();\r
+               }\r
+               if (insideTable) {\r
+                       String js = new String( "function getCursorPosition() { "\r
+                                       +"   var selObj = window.getSelection();"\r
+                                       +"   var selRange = selObj.getRangeAt(0);"\r
+                                       +"   var workingNode = window.getSelection().anchorNode;"\r
+                                       +"   var rowCount = 0;"\r
+                                       +"   var colCount = 0;"\r
+                                       +"   while(workingNode != null && workingNode.nodeName.toLowerCase() != 'table') { " \r
+                                       +"      if (workingNode.nodeName.toLowerCase()=='tr') {"\r
+                                       +"         rowCount = rowCount+1;"\r
+                                       +"      }"\r
+                                       +"      if (workingNode.nodeName.toLowerCase() == 'td') {"\r
+                                       +"         colCount = colCount+1;"\r
+                                       +"      }"\r
+                                       +"      if (workingNode.previousSibling != null)"\r
+                                       +"          workingNode = workingNode.previousSibling;"\r
+                                       +"      else "\r
+                                       +"           workingNode = workingNode.parentNode;"\r
+                                       +"   }"\r
+                                       +"   var nodes = workingNode.getElementsByTagName('tr');"\r
+                                       +"   var tableRows = nodes.length;"\r
+                                       +"   nodes = nodes[0].getElementsByTagName('td');"\r
+                                       +"   var tableColumns = nodes.length;"\r
+                                       +"   window.jambi.setTableCursorPositionTab(rowCount, colCount, tableRows, tableColumns);"\r
+                                       +"} getCursorPosition();");\r
+                       browser.page().mainFrame().evaluateJavaScript(js);\r
+               }\r
        }\r
        \r
+       // If a user presses tab from within a table\r
+       public void setTableCursorPositionTab(int currentRow, int currentCol, int tableRows, int tableColumns) {\r
+               if (tableRows == currentRow && currentCol == tableColumns) {\r
+                       insertTableRow();\r
+               }\r
+               KeyboardModifiers modifiers = new KeyboardModifiers(KeyboardModifier.NoModifier);\r
+               QKeyEvent right = new QKeyEvent(Type.KeyPress, Qt.Key.Key_Right.value(), modifiers);\r
+               QKeyEvent end = new QKeyEvent(Type.KeyPress, Qt.Key.Key_End.value(), modifiers);\r
+               QKeyEvent end2 = new QKeyEvent(Type.KeyPress, Qt.Key.Key_End.value(), modifiers);\r
+               getBrowser().focusWidget();\r
+               QCoreApplication.postEvent(getBrowser(), end);\r
+               QCoreApplication.postEvent(getBrowser(), right);\r
+               QCoreApplication.postEvent(getBrowser(), end2);\r
+       }\r
+               \r
        public void backtabPressed() {\r
+               if (insideEncryption) \r
+                       return;\r
                if (insideList)\r
                        outdentClicked();\r
+               if (insideTable) {\r
+                       String js = new String( "function getCursorPosition() { "\r
+                                       +"   var selObj = window.getSelection();"\r
+                                       +"   var selRange = selObj.getRangeAt(0);"\r
+                                       +"   var workingNode = window.getSelection().anchorNode;"\r
+                                       +"   var rowCount = 0;"\r
+                                       +"   var colCount = 0;"\r
+                                       +"   while(workingNode != null && workingNode.nodeName.toLowerCase() != 'table') { " \r
+                                       +"      if (workingNode.nodeName.toLowerCase()=='tr') {"\r
+                                       +"         rowCount = rowCount+1;"\r
+                                       +"      }"\r
+                                       +"      if (workingNode.nodeName.toLowerCase() == 'td') {"\r
+                                       +"         colCount = colCount+1;"\r
+                                       +"      }"\r
+                                       +"      if (workingNode.previousSibling != null)"\r
+                                       +"          workingNode = workingNode.previousSibling;"\r
+                                       +"      else "\r
+                                       +"           workingNode = workingNode.parentNode;"\r
+                                       +"   }"\r
+                                       +"   var nodes = workingNode.getElementsByTagName('tr');"\r
+                                       +"   var tableRows = nodes.length;"\r
+                                       +"   nodes = nodes[0].getElementsByTagName('td');"\r
+                                       +"   var tableColumns = nodes.length;"\r
+                                       +"   window.jambi.setTableCursorPositionBackTab(rowCount, colCount, tableRows, tableColumns);"\r
+                                       +"} getCursorPosition();");\r
+                       browser.page().mainFrame().evaluateJavaScript(js);\r
+                       \r
+               }\r
        }\r
        \r
+       // If a user presses backtab from within a table\r
+       public void setTableCursorPositionBackTab(int currentRow, int currentCol, int tableRows, int tableColumns) {\r
+               if (currentRow  == 1 && currentCol == 1) {\r
+                       return;\r
+               }\r
+               KeyboardModifiers modifiers = new KeyboardModifiers(KeyboardModifier.NoModifier);\r
+               QKeyEvent left = new QKeyEvent(Type.KeyPress, Qt.Key.Key_Left.value(), modifiers);\r
+               QKeyEvent home = new QKeyEvent(Type.KeyPress, Qt.Key.Key_Home.value(), modifiers);\r
+               getBrowser().focusWidget();\r
+               QCoreApplication.postEvent(getBrowser(), home);\r
+               QCoreApplication.postEvent(getBrowser(), left);\r
+       }\r
+       \r
+       \r
        public void setInsideList() {\r
                insideList = true;\r
        }\r
@@ -2363,6 +2488,7 @@ public class BrowserWindow extends QWidget {
                browser.rotateImageLeft.setEnabled(false);\r
                browser.rotateImageRight.setEnabled(false);\r
                browser.insertTableAction.setEnabled(true);\r
+               browser.deleteTableColumnAction.setEnabled(false);\r
                browser.insertTableRowAction.setEnabled(false);\r
                browser.insertTableColumnAction.setEnabled(false);\r
                browser.deleteTableRowAction.setEnabled(false);\r
@@ -2370,6 +2496,8 @@ public class BrowserWindow extends QWidget {
                insertHyperlink = true;\r
                currentHyperlink ="";\r
                insideList = false;\r
+               insideTable = false;\r
+               insideEncryption = false;\r
                forceTextPaste = false;\r
                \r
                String js = new String( "function getCursorPos() {"\r
@@ -2380,7 +2508,7 @@ public class BrowserWindow extends QWidget {
                        +"   var workingNode = window.getSelection().anchorNode.parentNode;"\r
                        +"   while(workingNode != null) { " \r
 //                     +"      window.jambi.printNode(workingNode.nodeName);"\r
-                       +"      if (workingNode.nodeName=='TABLE') { if (workingNode.getAttribute('class').toLowerCase() == 'en-crypt-temp') window.jambi.forceTextPaste(); }"\r
+                       +"      if (workingNode.nodeName=='TABLE') { if (workingNode.getAttribute('class').toLowerCase() == 'en-crypt-temp') window.jambi.insideEncryption(); }"\r
                        +"      if (workingNode.nodeName=='B') window.jambi.boldActive();"\r
                        +"      if (workingNode.nodeName=='I') window.jambi.italicActive();"\r
                        +"      if (workingNode.nodeName=='U') window.jambi.underlineActive();"\r
@@ -2403,6 +2531,10 @@ public class BrowserWindow extends QWidget {
                System.out.println("Node Vaule: " +n);\r
        }\r
        \r
+       public void insideEncryption() {\r
+               insideEncryption = true;\r
+               forceTextPaste();\r
+       }\r
        \r
        //****************************************************************\r
        //* Insert a table row\r
@@ -2509,8 +2641,10 @@ public class BrowserWindow extends QWidget {
                browser.insertTableRowAction.setEnabled(true);\r
                browser.insertTableColumnAction.setEnabled(true);\r
                browser.deleteTableRowAction.setEnabled(true);\r
+               browser.deleteTableColumnAction.setEnabled(true);\r
                browser.insertTableAction.setEnabled(false);\r
                browser.encryptAction.setEnabled(false);\r
+               insideTable = true;\r
        }\r
        \r
        public void setInsideLink(String link) {\r