OSDN Git Service

複数タブを開いた状態で終了したら、次回起動時にそれらのタブを復元する。
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / NeverNote.java
index 08793af..32fe99e 100644 (file)
@@ -600,25 +600,12 @@ public class NeverNote extends QMainWindow{
                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, "");
-               tabWindows.put(index, tab);
                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
-               // 履歴記録のハッシュマップを初期化
-               historyGuids.put(index, new ArrayList<String>());
-               historyPosition.put(index, 0);
-               fromHistory.put(index, false);
                
         mainLeftRightSplitter.addWidget(leftSplitter1);
         mainLeftRightSplitter.addWidget(browserIndexSplitter);
@@ -792,23 +779,75 @@ public class NeverNote extends QMainWindow{
                trayIcon.setToolTip(tr("NeighborNote"));
                trayIcon.setContextMenu(trayMenu);
                trayIcon.activated.connect(this, "trayActivated(com.trolltech.qt.gui.QSystemTrayIcon$ActivationReason)");
-
-               currentNoteGuid="";
-               currentNoteGuid = Global.getLastViewedNoteGuid();
-               if (currentNoteGuid.equals(""))
-                       currentNote = new Note();
                
-               // ICHANGED
-               /* 上に移動したので要らない
-               historyGuids = new ArrayList<String>();
-               historyPosition = 0;
-               fromHistory = false;
-               */
+               // 前回のタブを復元
+               noteTableView.load(true);
+               HashMap<Integer, String> tabs = new HashMap<Integer, String>();
+               if (Global.getLastViewedTabs() != null) {
+                       tabs.putAll(Global.getLastViewedTabs());
+               }
+               
+               Collection<String>              guids = tabs.values();
+               Iterator<String>                guidIterator = guids.iterator();
+               Collection<Integer>             indexes = tabs.keySet();
+               Iterator<Integer>               indexIterator = indexes.iterator();
+               noteDirty = new HashMap<Integer, Boolean>();
+               
+               while (guidIterator.hasNext()) {
+                       String guid = guidIterator.next();
+                       int tabIndex = indexIterator.next();
+                       
+                       currentNoteGuid = guid;
+                       noteDirty.put(tabIndex, false);
+                       if (!currentNoteGuid.trim().equals("")) {
+                               currentNote = conn.getNoteTable().getNote(currentNoteGuid, true, true, false, false, true);
+                               if (currentNote != null) {
+                                       openTabEditor(guid, tabIndex);
+                               }
+                       }
+               }
                
-               if (!currentNoteGuid.trim().equals("")) {
+               // タブが0個だったら作る
+               if (tabBrowser.count() <= 0) {
+                       TabBrowse tab = new TabBrowse(conn, tabBrowser, cbObserver);
+                       browserWindow = tab.getBrowserWindow();
+                       tabBrowser.currentChanged.disconnect();
+                       int index = tabBrowser.addNewTab(tab, "");
+                       tabBrowser.currentChanged.connect(this, "tabWindowChanged(int)");
+                       tabWindows.put(index, tab);
+                       noteDirty.put(index, false);
+                       
+                       // 履歴記録のハッシュマップを初期化
+                       historyGuids.put(index, new ArrayList<String>());
+                       historyPosition.put(index, 0);
+                       fromHistory.put(index, false);
+               }
+               
+               currentNoteGuid = Global.getLastViewedNoteGuid();
+               if (currentNoteGuid.trim().equals("")) {
+                       currentNote = new Note();
+               } else {
                        currentNote = conn.getNoteTable().getNote(currentNoteGuid, true,true,false,false,true);
                }
                
+               // タブのフォーカスをcurrentNoteGuidのノートに移す
+               if (tabBrowser.count() >= 2) {
+                       Collection<TabBrowse>   tabBrowsers = tabWindows.values();
+                       Iterator<TabBrowse>             tabBrowserIterator = tabBrowsers.iterator();
+                       Collection<Integer>             tabIndexes = tabWindows.keySet();
+                       Iterator<Integer>               tabIndexIterator = tabIndexes.iterator();
+                       
+                       while (tabBrowserIterator.hasNext()) {
+                               TabBrowse tab = tabBrowserIterator.next();
+                               int i = tabIndexIterator.next();
+                               String guid = tab.getBrowserWindow().getNote().getGuid();
+                               if (currentNoteGuid.equals(guid)) {
+                                       tabBrowser.setCurrentIndex(i);
+                                       break;
+                               }
+                       }
+               }
+               
                noteIndexUpdated(true);
                showColumns();
                menuBar.showEditorBar.setChecked(Global.isWindowVisible("editorButtonBar"));
@@ -1278,6 +1317,20 @@ public class NeverNote extends QMainWindow{
                
                Global.saveWindowMaximized(isMaximized());
                Global.saveCurrentNoteGuid(currentNoteGuid);
+               
+               // 開いていたタブ群をsettingsに保存しておく
+               Collection<TabBrowse>           tabBrowsers = tabWindows.values();
+               Iterator<TabBrowse>                     tabIterator = tabBrowsers.iterator();
+               Collection<Integer>                     tabIndexes = tabWindows.keySet();
+               Iterator<Integer>                       indexIterator = tabIndexes.iterator();
+               HashMap<Integer, String>        tabs = new HashMap<Integer, String>();
+               while (tabIterator.hasNext()) {
+                       TabBrowse tab = tabIterator.next();
+                       int index = indexIterator.next();
+                       String guid = tab.getBrowserWindow().getNote().getGuid();
+                       tabs.put(index, guid);
+               }
+               Global.setLastViewedTabs(tabs);
                        
                int sortCol = noteTableView.proxyModel.sortColumn();
                int sortOrder = noteTableView.proxyModel.sortOrder().value();
@@ -4789,17 +4842,36 @@ public class NeverNote extends QMainWindow{
                        conn.getHistoryTable().addHistory("rensoItemClick", prevCurrentNoteGuid, rensoNotePressedItemGuid);
                }
        }
-
-       // ICHANGED
+       
        private void openTabEditor(String guid) {
-               
+               openTabEditor(guid, -1);
+       }
+
+       // オーバーロード insertIndex < 0 ならば普通に追加
+       private void openTabEditor(String guid, int insertIndex) {
                Note note = conn.getNoteTable().getNote(guid, true, true, false, true, true);
                // 新しいタブエディタを作成
                TabBrowse newBrowser = new TabBrowse(conn, tabBrowser, cbObserver);
                showEditorButtons(newBrowser.getBrowserWindow());
                
                String noteTitle = note.getTitle();
-               int index = tabBrowser.addNewTab(newBrowser, noteTitle);
+               int index;
+               // 最初のタブだけ、追加後に自動的にフォーカスが移ってしまうので、一度切断する
+               if (tabBrowser.count() == 0) {
+                       tabBrowser.currentChanged.disconnect();
+                       if (insertIndex < 0) {
+                               index = tabBrowser.addNewTab(newBrowser, noteTitle);
+                       } else {
+                               index = tabBrowser.insertNewTab(insertIndex, newBrowser, noteTitle);
+                       }
+                       tabBrowser.currentChanged.connect(this, "tabWindowChanged(int)");
+               } else {
+                       if (insertIndex < 0) {
+                               index = tabBrowser.addNewTab(newBrowser, noteTitle);
+                       } else {
+                               index = tabBrowser.insertNewTab(insertIndex, newBrowser, noteTitle);
+                       }
+               }
                tabWindows.put(index, newBrowser);
                noteDirty.put(index, false);
                
@@ -4835,6 +4907,8 @@ public class NeverNote extends QMainWindow{
                                addBrowseHistory();
                        }
                }
+               
+               rensoNoteList.refreshRensoNoteList(currentNoteGuid);
        }
 
        // ICHANGED タブが閉じられた