OSDN Git Service

ノートコンテンツをwordテーブルに登録しないように変更。
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / NeverNote.java
index f60750e..1607030 100644 (file)
@@ -1,6 +1,7 @@
 /*
-  * This file is part of NixNote 
+  * This file is part of NixNote/NeighborNote 
  * Copyright 2009 Randy Baumgarte
+ * Copyright 2013 Yuki Takahashi
  * 
  * This file may be licensed under the terms of of the
  * GNU General Public License Version 2 (the ``GPL'').
@@ -81,6 +82,7 @@ import com.trolltech.qt.core.QModelIndex;
 import com.trolltech.qt.core.QSize;
 import com.trolltech.qt.core.QTemporaryFile;
 import com.trolltech.qt.core.QTextCodec;
+import com.trolltech.qt.core.QTextStream;
 import com.trolltech.qt.core.QThreadPool;
 import com.trolltech.qt.core.QTimer;
 import com.trolltech.qt.core.QTranslator;
@@ -248,9 +250,10 @@ public class NeverNote extends QMainWindow{
     NoteFilter                         filter;                                         // Note filter
     String                                     currentNoteGuid;                        // GUID of the current note 
     Note                                       currentNote;                            // The currently viewed note
-    boolean                                    noteDirty;                                      // Has the note been changed?
-    boolean                            inkNote;                   // if this is an ink note, it is read only
-    boolean                                    readOnly;                                       // Is this note read-only?
+    // ICHANGED
+    HashMap<Integer, Boolean>  noteDirty;                              // Has the note been changed?
+    HashMap<Integer, Boolean>  inkNote;                // if this is an ink note, it is read only
+    HashMap<Integer, Boolean>  readOnly;                               // Is this note read-only?
        
   
     ListManager                                listManager;                                    // DB runnable task
@@ -411,7 +414,7 @@ public class NeverNote extends QMainWindow{
                logger.log(logger.EXTREME, "Starting GUI build");
 
                QTranslator nevernoteTranslator = new QTranslator();
-               nevernoteTranslator.load(Global.getFileManager().getTranslateFilePath("nevernote_" + QLocale.system().name() + ".qm"));
+               nevernoteTranslator.load(Global.getFileManager().getTranslateFilePath("neighbornote_" + QLocale.system().name() + ".qm"));
                QApplication.instance().installTranslator(nevernoteTranslator);
 
                Global.originalPalette = QApplication.palette();
@@ -450,8 +453,8 @@ public class NeverNote extends QMainWindow{
                indexThread = new QThread(indexRunner, "Index Thread");
         indexRunner.indexAttachmentsLocally = Global.indexAttachmentsLocally();
         indexRunner.indexImageRecognition = Global.indexImageRecognition();
-        indexRunner.indexNoteBody = Global.indexNoteBody();
-        indexRunner.indexNoteTitle = Global.indexNoteTitle();
+//        indexRunner.indexNoteBody = Global.indexNoteBody();
+//        indexRunner.indexNoteTitle = Global.indexNoteTitle();
         indexRunner.specialIndexCharacters = Global.getSpecialIndexCharacters();
                indexThread.start();
                
@@ -596,6 +599,7 @@ public class NeverNote extends QMainWindow{
                tabWindows = new HashMap<Integer, TabBrowse>();
                tabBrowser = new TabBrowserWidget(this);
                tabBrowser.setStyleSheet("QTabBar::tab{width:150px;}");
+               tabBrowser.setMovable(true);
                TabBrowse tab = new TabBrowse(conn, tabBrowser, cbObserver);
                browserWindow = tab.getBrowserWindow();
                int index = tabBrowser.addNewTab(tab, "");
@@ -603,6 +607,12 @@ public class NeverNote extends QMainWindow{
                tabBrowser.setTabsClosable(true);
                tabBrowser.currentChanged.connect(this, "tabWindowChanged(int)");
                tabBrowser.tabCloseRequested.connect(this, "tabWindowClosing(int)");
+               
+               noteDirty = new HashMap<Integer, Boolean>();
+               noteDirty.put(index, false);
+               
+               inkNote = new HashMap<Integer, Boolean>();
+               readOnly = new HashMap<Integer, Boolean>();
 
                // ICHANGED
                // 履歴記録のハッシュマップを初期化
@@ -728,13 +738,21 @@ public class NeverNote extends QMainWindow{
                noteTableView.setVisible(Global.isWindowVisible("noteList"));
                menuBar.hideNoteList.setChecked(Global.isWindowVisible("noteList"));
                
-               if (!Global.isWindowVisible("editorButtonBar"))
+               // ICHANGED
+               if (!Global.isWindowVisible("editorButtonBar")) {
+                       menuBar.showEditorBar.setChecked(false);
                        toggleEditorButtonBar();
+               }
                
                if (!Global.isWindowVisible("leftPanel"))
                        menuBar.hideLeftSide.setChecked(true);
-               if (Global.isWindowVisible("noteInformation"))
+               
+               // ICHANGED
+               if (Global.isWindowVisible("noteInformation")) {
+                       menuBar.noteAttributes.setChecked(true);
                        toggleNoteInformation();
+               }
+               
                quotaBar.setVisible(Global.isWindowVisible("quota"));
                // IFIXED quotaBar.isVisible() → Global.isWindowVisible("quota")
                // なぜかquotaBar.isVisible()が常にfalseを返すようなので修正
@@ -787,7 +805,6 @@ public class NeverNote extends QMainWindow{
                fromHistory = false;
                */
                
-               noteDirty = false;
                if (!currentNoteGuid.trim().equals("")) {
                        currentNote = conn.getNoteTable().getNote(currentNoteGuid, true,true,false,false,true);
                }
@@ -899,8 +916,12 @@ public class NeverNote extends QMainWindow{
        }
                
                if (Global.checkVersionUpgrade()) {
-                       // ICHANGED TODO とりあえず封印
-                       // checkForUpdates();
+                       checkForUpdates();
+               }
+               
+               // ICHANGED
+               if (currentNoteGuid == null || currentNoteGuid.equals("")) {
+                       menuBar.noteAddNewTab.setEnabled(false);
                }
        }
        
@@ -1431,7 +1452,7 @@ public class NeverNote extends QMainWindow{
                saveNoteColumnPositions();
                saveNoteIndexWidth();
                showColumns();
-        ConfigDialog settings = new ConfigDialog(this);
+        ConfigDialog settings = new ConfigDialog(this, conn);
         String dateFormat = Global.getDateFormat();
         String timeFormat = Global.getTimeFormat();
         
@@ -1440,8 +1461,8 @@ public class NeverNote extends QMainWindow{
         
         settings.exec();
         indexRunner.indexAttachmentsLocally = Global.indexAttachmentsLocally();
-        indexRunner.indexNoteBody = Global.indexNoteBody();
-        indexRunner.indexNoteTitle = Global.indexNoteTitle();
+//        indexRunner.indexNoteBody = Global.indexNoteBody();
+//        indexRunner.indexNoteTitle = Global.indexNoteTitle();
         indexRunner.specialIndexCharacters = Global.getSpecialIndexCharacters();
         indexRunner.indexImageRecognition = Global.indexImageRecognition();
         if (Global.showTrayIcon() || Global.minimizeOnClose())
@@ -2518,7 +2539,15 @@ public class NeverNote extends QMainWindow{
                        }
                }
                browserWindow.setTag(names.toString());
-               noteDirty = true;
+               
+               // ICHANGED
+               for (TabBrowse tab: tabWindows.values()) {
+                       if (tab.getBrowserWindow().getNote().getGuid().equals(guid)) {
+                               int index = tabBrowser.indexOf(tab);
+                               noteDirty.put(index, true);
+                               break;
+                       }
+               }
                
                // Now, we need to add any new tags to the tag tree
                for (int i=0; i<newTags.size(); i++) 
@@ -2934,6 +2963,7 @@ public class NeverNote extends QMainWindow{
                status.setTagCount(listManager.getTagIndex().size());
                status.setResourceCount(conn.getNoteTable().noteResourceTable.getResourceCount());
                status.setWordCount(conn.getWordsTable().getWordCount());
+               status.setHistoryCount(conn.getHistoryTable().getHistoryCount());
                waitCursor(false);
                status.exec();
        }
@@ -2974,7 +3004,12 @@ public class NeverNote extends QMainWindow{
                if (!file.open(new QIODevice.OpenMode(QIODevice.OpenModeFlag.ReadOnly,
                 QIODevice.OpenModeFlag.Text)))
                        return;
-               textBox.setText(file.readAll().toString());
+               // ICHANGED 日本語文字化け対策
+               QTextCodec codec = QTextCodec.codecForName("UTF-8");
+               QTextStream textStream = new QTextStream(file);
+               textStream.setCodec(codec);
+               textBox.setText(textStream.readAll().toString());
+               
                file.close();
                dialog.setWindowTitle(tr("Release Notes"));
                dialog.setLayout(layout);
@@ -3555,6 +3590,12 @@ public class NeverNote extends QMainWindow{
                                        
                Global.showDeleted = true;
        }
+       
+       menuBar.noteAddNewTab.setEnabled(newButton.isEnabled());
+               if (currentNoteGuid == null || currentNoteGuid.equals("")) {
+                       menuBar.noteAddNewTab.setEnabled(false);
+               }
+       
        listManager.loadNotesIndex();
        noteIndexUpdated(false);
 ////           browserWindow.setEnabled(newButton.isEnabled());
@@ -3574,6 +3615,10 @@ public class NeverNote extends QMainWindow{
                listManager.setSelectedSavedSearch("");
                newButton.setEnabled(!newButton.isEnabled());
                menuBar.noteAdd.setEnabled(newButton.isEnabled());
+               menuBar.noteAddNewTab.setEnabled(newButton.isEnabled());
+               if (currentNoteGuid == null || currentNoteGuid.equals("")) {
+                       menuBar.noteAddNewTab.setEnabled(false);
+               }
                menuBar.noteAdd.setVisible(true);
                browserWindow.clear();
                
@@ -3612,6 +3657,11 @@ public class NeverNote extends QMainWindow{
                Global.showDeleted = false;
        newButton.setEnabled(true);
        menuBar.noteAdd.setEnabled(true);
+               if (currentNoteGuid == null || currentNoteGuid.equals("")) {
+                       menuBar.noteAddNewTab.setEnabled(false);
+               } else {
+                       menuBar.noteAddNewTab.setEnabled(true);
+               }
        menuBar.noteAdd.setVisible(true);
                trashTree.blockSignals(true);
                trashTree.clearSelection();
@@ -3936,6 +3986,9 @@ public class NeverNote extends QMainWindow{
                menuBar.noteOnlineHistoryAction.setEnabled(true);
                menuBar.noteMergeAction.setEnabled(true);
                selectedNoteGUIDs.clear();
+               if (currentNoteGuid != null && !currentNoteGuid.equals("") && !Global.showDeleted) {
+                       menuBar.noteAddNewTab.setEnabled(true);
+               }
                if (selections.size() != 1 || Global.showDeleted) {
                        menuBar.noteDuplicateAction.setEnabled(false);
                }
@@ -4745,6 +4798,11 @@ public class NeverNote extends QMainWindow{
                TabBrowse newBrowser = new TabBrowse(conn, tabBrowser, cbObserver);
                showEditorButtons(newBrowser.getBrowserWindow());
                
+               String noteTitle = note.getTitle();
+               int index = tabBrowser.addNewTab(newBrowser, noteTitle);
+               tabWindows.put(index, newBrowser);
+               noteDirty.put(index, false);
+               
                // noteTableViewの選択を変更するとselectionChangedが発生してしまうので一度切断
                noteTableView.selectionModel().selectionChanged.disconnect(this, "noteTableSelection()");
                loadNoteBrowserInformation(newBrowser.getBrowserWindow(), guid, note);
@@ -4753,10 +4811,6 @@ public class NeverNote extends QMainWindow{
                
                setupBrowserWindowListeners(newBrowser.getBrowserWindow(), false);
                
-               String noteTitle = note.getTitle();
-               int index = tabBrowser.addNewTab(newBrowser, noteTitle);
-               tabWindows.put(index, newBrowser);
-               
                // ExtendedInformationを必要があれば表示する
                toggleNoteInformation();
                // Sourceを必要があれば表示する
@@ -4810,6 +4864,9 @@ public class NeverNote extends QMainWindow{
                // ノートを削除
                tabWindows.remove(index);
                tabBrowser.removeTab(index);
+               noteDirty.remove(index);
+               inkNote.remove(index);
+               readOnly.remove(index);
 
                // 履歴記録のハッシュマップを削除
                historyGuids.remove(index);
@@ -4822,6 +4879,18 @@ public class NeverNote extends QMainWindow{
                        TabBrowse tab = tabWindows.get(i + 1);
                        tabWindows.put(i, tab);
                        tabWindows.remove(i + 1);
+                       // noteDirty
+                       boolean isNoteDirty = noteDirty.get(i + 1);
+                       noteDirty.put(i, isNoteDirty);
+                       noteDirty.remove(i + 1);
+                       // inkNote
+                       boolean isInkNote = inkNote.get(i + 1);
+                       inkNote.put(i, isInkNote);
+                       inkNote.remove(i + 1);
+                       // readOnly
+                       boolean isReadOnly = readOnly.get(i + 1);
+                       readOnly.put(i, isReadOnly);
+                       readOnly.remove(i + 1);
                        // historyGuids
                        ArrayList<String> histGuids = historyGuids.get(i + 1);
                        historyGuids.put(i, histGuids);
@@ -4846,15 +4915,18 @@ public class NeverNote extends QMainWindow{
                
                // ノート追加前に開いていたノートとの関連性を記録するためにguidをとっておく
                TabBrowse prevTab = (TabBrowse)tabBrowser.currentWidget();
-               String prevTabGuid = prevTab.getBrowserWindow().getNote().getGuid();
+               String prevTabGuid = null;
+               if (prevTab.getBrowserWindow() != null && prevTab.getBrowserWindow().getNote() != null) {
+                       prevTabGuid = prevTab.getBrowserWindow().getNote().getGuid();
+               }
                
                openEmptyTabEditor();
                addNote();
                
                // 追加されたノートのguidを取得し、ノート追加操作履歴としてデータベースに登録
-               TabBrowse addedTab = (TabBrowse)tabBrowser.currentWidget();
-               String addedTabGuid = addedTab.getBrowserWindow().getNote().getGuid();
                if (prevTabGuid != null && !prevTabGuid.equals("")) {
+                       TabBrowse addedTab = (TabBrowse)tabBrowser.currentWidget();
+                       String addedTabGuid = addedTab.getBrowserWindow().getNote().getGuid();
                        if (addedTabGuid != null && !addedTabGuid.equals("")) {
                                if (!prevTabGuid.equals(addedTabGuid)) {
                                        conn.getHistoryTable().addHistory("addNewNote", prevTabGuid, addedTabGuid);
@@ -4873,6 +4945,7 @@ public class NeverNote extends QMainWindow{
                
                int index = tabBrowser.addNewTab(newBrowser, "");
                tabWindows.put(index, newBrowser);
+               noteDirty.put(index, false);
                
                // ExtendedInformationを必要があれば表示する
                toggleNoteInformation();
@@ -4895,61 +4968,79 @@ public class NeverNote extends QMainWindow{
     //** These functions deal with Note specific things
     //***************************************************************
     //***************************************************************    
+       // ICHANGED
        private void setNoteDirty() {
+               for (String guid: selectedNoteGUIDs) {
+                       setNoteDirty(guid);
+               }
+       }
+       
+       // ICHANGED
+       private void setNoteDirty(String targetGuid) {
                logger.log(logger.EXTREME, "Entering NeverNote.setNoteDirty()");
                
                // Find if the note is being edited externally.  If it is, update it.
-               if (externalWindows.containsKey(currentNoteGuid)) {
+               if (externalWindows.containsKey(targetGuid)) {
                        QTextCodec codec = QTextCodec.codecForName("UTF-8");
                QByteArray unicode =  codec.fromUnicode(browserWindow.getContent());
-                       ExternalBrowse window = externalWindows.get(currentNoteGuid);
+                       ExternalBrowse window = externalWindows.get(targetGuid);
                window.getBrowserWindow().setContent(unicode);
                }
                
-               // ICHANGED ↓↓↓ここから↓↓↓
                // 他のタブで同じノートを開いていないか探す。もしあったら、内容を更新する。
-               Collection<TabBrowse> tabBrowsers = tabWindows.values();
-               Iterator<TabBrowse> tabIterator = tabBrowsers.iterator();
                Collection<Integer> tabIndexes = tabWindows.keySet();
                Iterator<Integer>       indexIterator = tabIndexes.iterator();
                
-               while (tabIterator.hasNext()) {
-                       TabBrowse tab = tabIterator.next();
+               for (TabBrowse tab: tabWindows.values()) {
                        int index = indexIterator.next();
                        String guid = tab.getBrowserWindow().getNote().getGuid();
                        
                        QTextCodec codec = QTextCodec.codecForName("UTF-8");
                        QByteArray unicode = codec.fromUnicode(browserWindow.getContent());
                        
-                       if (guid.equals(currentNoteGuid)) {
+                       if (guid.equals(guid)) {
                                if (index != tabBrowser.currentIndex()) {
                                        TabBrowse window = tabWindows.get(index);
                                        window.getBrowserWindow().setContent(unicode);
                                }
                        }
                }
-               // ICHANGED ↑↑↑ここまで↑↑↑
                
+               // ターゲットノートがタブで開かれていて、かつDirty = trueかどうかを取得する
                // If the note is dirty, then it is unsynchronized by default.
-               if (noteDirty) 
+               int index = -1;
+               boolean isNoteDirty = false;
+               for (TabBrowse tab: tabWindows.values()) {
+                       if (tab.getBrowserWindow().getNote().getGuid().equals(targetGuid)) {
+                               index = tabBrowser.indexOf(tab);
+                               isNoteDirty = noteDirty.get(index);
+                               break;
+                       }
+               }
+               if (isNoteDirty) {
                        return;
+               }
                
                // Set the note as dirty and check if its status is synchronized in the display table
-               noteDirty = true;
-               if (listManager.getNoteMetadata().containsKey(currentNoteGuid) && 
-                               listManager.getNoteMetadata().get(currentNoteGuid).isDirty()) {
+               // まだダーティでなく、かつタブで開かれている場合にnoteDirty = trueにする
+               if (index >= 0) {
+                       noteDirty.put(index, true);
+               }
+
+               if (listManager.getNoteMetadata().containsKey(targetGuid) &&
+                               listManager.getNoteMetadata().get(targetGuid).isDirty()) {
                                return;
                }
                
                // If this wasn't already marked as unsynchronized, then we need to update the table
-               listManager.getNoteTableModel().updateNoteSyncStatus(currentNoteGuid, false);
-//     listManager.getUnsynchronizedNotes().add(currentNoteGuid);
+               listManager.getNoteTableModel().updateNoteSyncStatus(targetGuid, false);
+//     listManager.getUnsynchronizedNotes().add(targetGuid);
        for (int i=0; i<listManager.getNoteTableModel().rowCount(); i++) {
                QModelIndex modelIndex =  listManager.getNoteTableModel().index(i, Global.noteTableGuidPosition);
                if (modelIndex != null) {
                        SortedMap<Integer, Object> ix = listManager.getNoteTableModel().itemData(modelIndex);
                        String tableGuid =  (String)ix.values().toArray()[0];
-                       if (tableGuid.equals(currentNoteGuid)) {
+                       if (tableGuid.equals(targetGuid)) {
                                listManager.getNoteTableModel().proxyModel.setData(i, Global.noteTableSynchronizedPosition, "false");
                                return;
                        }
@@ -4965,7 +5056,9 @@ public class NeverNote extends QMainWindow{
        noteCache.remove(guid);
                noteCache.put(guid, unicode.toString());
        if (guid.equals(currentNoteGuid)) {
-               noteDirty = true;
+               // ICHANGED
+               int index = tabBrowser.currentIndex();
+               noteDirty.put(index, true);
                browserWindow.setContent(unicode);
        } 
        if (save) {
@@ -4989,11 +5082,23 @@ public class NeverNote extends QMainWindow{
        }
        
     private void saveNote() {
-       if (noteDirty) {
-               saveNote(currentNoteGuid, browserWindow);
-               thumbnailRunner.addWork("GENERATE "+ currentNoteGuid);
-               noteDirty = false;
-       } 
+       // ICHANGED
+       // すべてのタブに対して、Dirtyを確認し、trueならセーブする
+       Collection<Integer> dirtyIndex = noteDirty.keySet();
+       Iterator<Integer> indexIterator = dirtyIndex.iterator();
+       for (boolean isNoteDirty: noteDirty.values()) {
+               int index = indexIterator.next();
+               if (isNoteDirty) {
+                       if (index < 0) {
+                               return;
+                       }
+                       BrowserWindow b = tabWindows.get(index).getBrowserWindow();
+                       String guid = b.getNote().getGuid();
+                       saveNote(guid, b);
+                       thumbnailRunner.addWork("GENERATE "+ guid);
+                       noteDirty.put(index, false);
+               }
+       }
     }
     private void saveNote(String guid, BrowserWindow window) {
                logger.log(logger.EXTREME, "Inside NeverNote.saveNote()");
@@ -5029,12 +5134,15 @@ public class NeverNote extends QMainWindow{
                        browserWindow.setEnabled(false);
                        return;
                }
-               inkNote = false;
-               readOnly = false;
-               if (Global.showDeleted || currentNoteGuid == null || currentNoteGuid.equals(""))
-                       readOnly = true;
+               // ICHANGED
+               inkNote.put(tabBrowser.currentIndex(), false);
+               readOnly.put(tabBrowser.currentIndex(), false);
+               
+               if (Global.showDeleted || currentNoteGuid == null || currentNoteGuid.equals("")) {
+                       readOnly.put(tabBrowser.currentIndex(), true);
+               }
                Global.cryptCounter =0;
-               if (readOnly) {
+               if (readOnly.get(tabBrowser.currentIndex())) {
                        browserWindow.setReadOnly(true);
                }
                
@@ -5056,11 +5164,22 @@ public class NeverNote extends QMainWindow{
                loadNoteBrowserInformation(browserWindow, currentNoteGuid, currentNote);
        }
 
+       // ICHANGED
        private void loadNoteBrowserInformation(BrowserWindow browser, String guid, Note note) {
                NoteFormatter   formatter = new NoteFormatter(logger, conn, tempFiles);
                formatter.setNote(note, Global.pdfPreview());
                formatter.setHighlight(listManager.getEnSearch());
                QByteArray js;
+               int tabIndex = -1;
+               
+               // 対象のタブインデックスを取得
+               for (TabBrowse tab: tabWindows.values()) {
+                       if (tab.getBrowserWindow() == browser) {
+                               tabIndex = tabBrowser.indexOf(tab);
+                               break;
+                       }
+               }
+               
                if (!noteCache.containsKey(guid)) {
                        js = new QByteArray();
                        // We need to prepend the note with <HEAD></HEAD> or encoded characters are ugly 
@@ -5085,7 +5204,7 @@ public class NeverNote extends QMainWindow{
                        noteCache.put(guid, js.toString());
 
                        if (formatter.resourceError)
-                               resourceErrorMessage();
+                               resourceErrorMessage(tabIndex);
                        if (formatter.formatError) {
                                waitCursor(false);
                             QMessageBox.information(this, tr("Error"),
@@ -5093,35 +5212,50 @@ public class NeverNote extends QMainWindow{
                                                " To protect your data this note is being marked as read-only."));      
                             waitCursor(true);
                        }
-                       readOnly = formatter.readOnly;
-                       inkNote = formatter.inkNote;
-                       if (readOnly)
+                       
+                       if (tabIndex >= 0) {
+                               readOnly.put(tabIndex, formatter.readOnly);
+                               inkNote.put(tabIndex, formatter.inkNote);
+                       } 
+                       
+                       if (tabIndex >= 0 && readOnly.get(tabIndex)) {
                                readOnlyCache.put(guid, true);
-                       if (inkNote)
+                       }
+                       if (tabIndex >= 0 && inkNote.get(tabIndex)) {
                                inkNoteCache.put(guid, true);
+                       }
+                       
                } else {
                        logger.log(logger.HIGH, "Note content is being pulled from the cache");
                        String cachedContent = formatter.modifyCachedTodoTags(noteCache.get(guid));
                        js = new QByteArray(cachedContent);
                        browser.setContent(js);
-                       if (readOnlyCache.containsKey(guid))
-                                       readOnly = true;
-                       if (inkNoteCache.containsKey(guid))
-                                       inkNote = true;
+                       if (readOnlyCache.containsKey(guid) && tabIndex >= 0) {
+                               readOnly.put(tabIndex, true);
+                       } else {
+                               readOnly.put(tabIndex, false);
+                       }
+                       if (inkNoteCache.containsKey(guid) && tabIndex >= 0) {
+                               inkNote.put(tabIndex, true);
+                       } else {
+                               inkNote.put(tabIndex, false);
+                       }
                }
                if (conn.getNoteTable().isThumbnailNeeded(guid)) {
                        thumbnailHTMLReady(guid, js, Global.calculateThumbnailZoom(js.toString()));
                }
-               if (readOnly || inkNote || 
-                               (note.getAttributes() != null && note.getAttributes().getContentClass() != null && note.getAttributes().getContentClass() != ""))
+               if (tabIndex >= 0 && (readOnly.get(tabIndex) || inkNote.get(tabIndex) || 
+                               (note.getAttributes() != null && note.getAttributes().getContentClass() != null && note.getAttributes().getContentClass() != "")))
                        browser.getBrowser().page().setContentEditable(false);  // We don't allow editing of ink notes
                else
                        browser.getBrowser().page().setContentEditable(true);
-               browser.setReadOnly(readOnly);
-               deleteButton.setEnabled(!readOnly);
-               tagButton.setEnabled(!readOnly);
-               menuBar.noteDelete.setEnabled(!readOnly);
-               menuBar.noteTags.setEnabled(!readOnly);
+               if (tabIndex >= 0) {
+                       browser.setReadOnly(readOnly.get(tabIndex));
+                       deleteButton.setEnabled(!readOnly.get(tabIndex));
+                       tagButton.setEnabled(!readOnly.get(tabIndex));
+                       menuBar.noteDelete.setEnabled(!readOnly.get(tabIndex));
+                       menuBar.noteTags.setEnabled(!readOnly.get(tabIndex));
+               }
                browser.setNote(note);
                
                if (note != null && note.getNotebookGuid() != null && 
@@ -5162,7 +5296,15 @@ public class NeverNote extends QMainWindow{
                browser.setAllTags(tagList);
                
                browser.setCurrentTags(note.getTagNames());
-               noteDirty = false;
+               // ICHANGED
+               for (TabBrowse tab: tabWindows.values()) {
+                       if (tab.getBrowserWindow().getNote().getGuid().equals(guid)) {
+                               int index = tabBrowser.indexOf(tab);
+                               noteDirty.put(index, false);
+                               break;
+                       }
+               }
+               
                scrollToGuid(guid);
                
                browser.loadingData(false);
@@ -5309,6 +5451,7 @@ public class NeverNote extends QMainWindow{
                }
                if (selectedNoteGUIDs.size() == 0 && !currentNoteGuid.equals("")) 
                        selectedNoteGUIDs.add(currentNoteGuid);
+               closeTabs(selectedNoteGUIDs);
                for (int i=0; i<selectedNoteGUIDs.size(); i++) {
                        listManager.deleteNote(selectedNoteGUIDs.get(i));
                }
@@ -5344,6 +5487,7 @@ public class NeverNote extends QMainWindow{
                                }
                        }
                }
+                       closeTabs(selectedNoteGUIDs);
                        listManager.expungeNote(selectedNoteGUIDs.get(i));
                        
                        // ICHANGED
@@ -5354,33 +5498,20 @@ public class NeverNote extends QMainWindow{
                }
        }
        currentNoteGuid = "";
-       
-       // ICHANGED ↓↓↓ここから↓↓↓
-               // 削除したノートを外部ウィンドウで開いていたら、閉じる
-               Collection<ExternalBrowse>      windows = externalWindows.values();
-               Iterator<ExternalBrowse>        windowIterator = windows.iterator();
-               Collection<String>                      guids = externalWindows.keySet();
-               Iterator<String>                        guidIterator = guids.iterator();
-               List<ExternalBrowse>            closeWindows = new ArrayList<ExternalBrowse>(); // イテレータ操作中に中身をいじっちゃダメなので
-               
-               while (windowIterator.hasNext()) {
-                       ExternalBrowse browser = windowIterator.next();
-                       String guid = guidIterator.next();
-                       
-                       for (int i = 0; i < selectedNoteGUIDs.size(); i++) {
-                               if (guid.equals(selectedNoteGUIDs.get(i))) {
-                                       closeWindows.add(browser);
-                               }
-                       }
-               }
-               
-               for (int i = closeWindows.size() - 1; i >= 0; i--) {
-                       closeWindows.get(i).close();
+       closeExternalWindows(selectedNoteGUIDs);
+               if (currentNoteGuid == null || currentNoteGuid.equals("")) {
+                       menuBar.noteAddNewTab.setEnabled(false);
                }
-               // ICHANGED ↑↑↑ここまで↑↑↑
                
-       // ICHANGED ↓↓↓ここから↓↓↓
-       // 削除したノートをタブで開いていたら、閉じる
+       listManager.loadNotesIndex();
+       noteIndexUpdated(false);
+       refreshEvernoteNote(true);
+       scrollToGuid(currentNoteGuid);
+       logger.log(logger.HIGH, "Leaving NeverNote.deleteNote");
+    }
+    
+    // 対象ノートをタブで開いていたら閉じる
+    private void closeTabs(List<String> noteGUIDs) {
                Collection<TabBrowse> tabBrowsers = tabWindows.values();
                Iterator<TabBrowse> tabIterator = tabBrowsers.iterator();
                Collection<Integer> tabIndexes = tabWindows.keySet();
@@ -5392,8 +5523,8 @@ public class NeverNote extends QMainWindow{
                        int index = indexIterator.next();
                        String guid = tab.getBrowserWindow().getNote().getGuid();
                        
-                       for(int i = 0; i < selectedNoteGUIDs.size(); i++){
-                               if(guid.equals(selectedNoteGUIDs.get(i))){
+                       for(int i = 0; i < noteGUIDs.size(); i++){
+                               if(guid.equals(noteGUIDs.get(i))){
                                        closeIndexes.add(index);
                                }
                        }
@@ -5402,16 +5533,33 @@ public class NeverNote extends QMainWindow{
                for(int i = closeIndexes.size() - 1; i >= 0; i--){
                        tabWindowClosing(closeIndexes.get(i));
                }
-               // ICHANGED ↑↑↑ここまで↑↑↑              
-       
-       listManager.loadNotesIndex();
-       noteIndexUpdated(false);
-       refreshEvernoteNote(true);
-       scrollToGuid(currentNoteGuid);
-       logger.log(logger.HIGH, "Leaving NeverNote.deleteNote");
     }
+    
+    // 対象ノートを外部ウィンドウで開いていたら閉じる
+    private void closeExternalWindows(List<String> noteGUIDs) {
+               Collection<ExternalBrowse>      windows = externalWindows.values();
+               Iterator<ExternalBrowse>        windowIterator = windows.iterator();
+               Collection<String>                      guids = externalWindows.keySet();
+               Iterator<String>                        guidIterator = guids.iterator();
+               List<ExternalBrowse>            closeWindows = new ArrayList<ExternalBrowse>(); // イテレータ操作中に中身をいじっちゃダメなので
+               
+               while (windowIterator.hasNext()) {
+                       ExternalBrowse browser = windowIterator.next();
+                       String guid = guidIterator.next();
+                       
+                       for (int i = 0; i < noteGUIDs.size(); i++) {
+                               if (guid.equals(noteGUIDs.get(i))) {
+                                       closeWindows.add(browser);
+                               }
+                       }
+               }
+               
+               for (int i = closeWindows.size() - 1; i >= 0; i--) {
+                       closeWindows.get(i).close();
+               }
+    }
+    
     // Add a new note
-    // ICHANGED @SuppressWarnings("unused") を削除
        private void addNote() {
        logger.log(logger.HIGH, "Inside NeverNote.addNote");
 //     browserWindow.setEnabled(true);
@@ -5521,12 +5669,6 @@ public class NeverNote extends QMainWindow{
        // IFIXED こいつのせいで、ノート追加時にcurrentNoteGuidが更新されないので消す
        // noteTableView.clearSelection();
        
-       refreshEvernoteNote(true);
-       listManager.countNotebookResults(listManager.getNoteIndex());
-       browserWindow.titleLabel.setFocus();
-       browserWindow.titleLabel.selectAll();
-//     notebookTree.updateCounts(listManager.getNotebookIndex(), listManager.getNotebookCounter());
-       
        // ICHANGED 新規に作成したノートとそれまで開いていたノートの関連性を追加
        if (prevCurrentNoteGuid != null && !prevCurrentNoteGuid.equals("")) {
                if (currentNoteGuid != null && !currentNoteGuid.equals("")) {
@@ -5534,6 +5676,12 @@ public class NeverNote extends QMainWindow{
                }
        }
        
+       refreshEvernoteNote(true);
+       listManager.countNotebookResults(listManager.getNoteIndex());
+       browserWindow.titleLabel.setFocus();
+       browserWindow.titleLabel.selectAll();
+//     notebookTree.updateCounts(listManager.getNotebookIndex(), listManager.getNotebookCounter());
+       
        // If the window is hidden, then we want to popup this in an external window & 
        if (!isVisible())
                listDoubleClick();
@@ -5616,7 +5764,9 @@ public class NeverNote extends QMainWindow{
                // ICHANGED
                for(int i = 0; i < tabBrowser.count(); i++){
                        TabBrowse b = (TabBrowse)tabBrowser.widget(i);
-                       b.getBrowserWindow().getNote().setGuid(newGuid);
+                       if (b.getBrowserWindow().getNote().getGuid().equals(oldGuid)) {
+                               b.getBrowserWindow().getNote().setGuid(newGuid);
+                       }
                }
 
        for (int i=0; i<listManager.getNoteIndex().size(); i++) {
@@ -6301,8 +6451,12 @@ public class NeverNote extends QMainWindow{
        //**********************************************************
        //**********************************************************
        // An error has happended fetching a resource.  let the user know
-       private void resourceErrorMessage() {
-               if (inkNote)
+    // ICHANGED
+       private void resourceErrorMessage(int tabIndex) {
+               if (tabIndex < 0) {
+                       return;
+               }
+               if (inkNote.get(tabIndex))
                        return;
                waitCursor(false);
                QMessageBox.information(this, tr("DOUGH!!!"), tr("Well, this is embarrassing."+
@@ -6315,7 +6469,7 @@ public class NeverNote extends QMainWindow{
                "Don't get angry.  I'm doing it to prevent you from messing up\n"+
                "this note on the Evernote servers.  Sorry."+
                "\n\nP.S. You might want to re-synchronize to see if it corrects this problem.\nWho knows, you might get lucky."));
-               inkNote = true;
+               inkNote.put(tabIndex, true);
                browserWindow.setReadOnly(true);
                waitCursor(true);
        }
@@ -6421,7 +6575,23 @@ public class NeverNote extends QMainWindow{
        public void refreshLists() {
                logger.log(logger.EXTREME, "Entering NeverNote.refreshLists");
                updateQuotaBar();
-               listManager.refreshLists(currentNote, noteDirty, browserWindow.getContent());
+               // ICHANGED
+               // すべてのタブのノートを調べて、Dirtyならばセーブする。その後refreshListsする。
+               Collection<Integer> tabIndex = noteDirty.keySet();
+               Iterator<Integer> indexIterator = tabIndex.iterator();
+               HashMap<Integer, Note> saveNotes = new HashMap<Integer, Note>();
+               HashMap<Integer, String> saveContents = new HashMap<Integer, String>();
+               for (boolean isNoteDirty: noteDirty.values()) {
+                       int index = indexIterator.next();
+                       if (isNoteDirty) {
+                               saveNotes.put(index, tabWindows.get(index).getBrowserWindow().getNote());
+                               saveContents.put(index, tabWindows.get(index).getBrowserWindow().getContent());
+                       }
+               }
+               
+               listManager.saveUpdatedNotes(saveNotes, saveContents);
+               listManager.refreshLists();
+
                tagIndexUpdated(true);
                notebookIndexUpdated();
                savedSearchIndexUpdated();
@@ -7398,6 +7568,10 @@ public class NeverNote extends QMainWindow{
        // toggle the add buttons
        newButton.setEnabled(!newButton.isEnabled());
        menuBar.noteAdd.setEnabled(newButton.isEnabled());
+       menuBar.noteAddNewTab.setEnabled(newButton.isEnabled());
+               if (currentNoteGuid == null || currentNoteGuid.equals("")) {
+                       menuBar.noteAddNewTab.setEnabled(false);
+               }
        menuBar.noteAdd.setVisible(true);
        
        if (!toDeleted) {       // 生存ノートテーブルへ
@@ -7456,7 +7630,7 @@ public class NeverNote extends QMainWindow{
                                String tableGuid = (String) ix.values().toArray()[0];
                                if (tableGuid.equals(rensoNotePressedItemGuid)) {
                                        noteTableView.selectRow(i);
-                                       return;
+                                       break;
                                }
                        }
                }
@@ -7549,4 +7723,37 @@ public class NeverNote extends QMainWindow{
        public String getCurrentNoteGuid() {
                return currentNoteGuid;
        }
+       
+       @SuppressWarnings("unused")
+       // タブ入れ替えによってタブインデックスが変わったので、インデックスで管理しているハッシュマップ達も入れ替える
+       private void tabIndexChanged(int from, int to) {
+               // tabWindows
+               TabBrowse tab = tabWindows.get(from);
+               tabWindows.put(from, tabWindows.get(to));
+               tabWindows.put(to, tab);
+               // noteDirty
+               boolean isNoteDirty = noteDirty.get(from);
+               noteDirty.put(from, noteDirty.get(to));
+               noteDirty.put(to, isNoteDirty);
+               // inkNote
+               boolean isInkNote = inkNote.get(from);
+               inkNote.put(from, inkNote.get(to));
+               inkNote.put(to, isInkNote);
+               // readOnly
+               boolean isReadOnly = readOnly.get(from);
+               readOnly.put(from, readOnly.get(to));
+               readOnly.put(to, isReadOnly);
+               // historyGuids
+               ArrayList<String> histGuids = historyGuids.get(from);
+               historyGuids.put(from, historyGuids.get(to));
+               historyGuids.put(to, histGuids);
+               // historyPosition
+               int histPosition = historyPosition.get(from);
+               historyPosition.put(from, historyPosition.get(to));
+               historyPosition.put(to, histPosition);
+               // fromHistory
+               boolean fromHist = fromHistory.get(from);
+               fromHistory.put(from,  fromHistory.get(to));
+               fromHistory.put(to, fromHist);
+       }
 }