1 19 package org.netbeans.modules.versioning.diff; 20 21 import org.netbeans.modules.editor.errorstripe.privatespi.MarkProvider; 22 import org.netbeans.modules.versioning.spi.OriginalContent; 23 import org.netbeans.modules.versioning.spi.VersioningSystem; 24 import org.netbeans.modules.versioning.VersioningManager; 25 import org.netbeans.modules.versioning.VersioningConfig; 26 import org.netbeans.modules.versioning.util.Utils; 27 import org.openide.loaders.DataObject; 28 import org.openide.filesystems.FileObject; 29 import org.openide.filesystems.FileUtil; 30 import org.openide.util.RequestProcessor; 31 32 import javax.swing.*; 33 import javax.swing.text.JTextComponent ; 34 import javax.swing.text.Document ; 35 import java.io.File ; 36 import java.util.WeakHashMap ; 37 import java.util.Map ; 38 import java.util.prefs.PreferenceChangeListener ; 39 import java.util.prefs.PreferenceChangeEvent ; 40 41 46 class DiffSidebarManager implements PreferenceChangeListener { 47 48 static final String SIDEBAR_ENABLED = "diff.sidebarEnabled"; 50 private static DiffSidebarManager instance; 51 52 static synchronized DiffSidebarManager getInstance() { 53 if (instance == null) { 54 instance = new DiffSidebarManager(); 55 } 56 return instance; 57 } 58 59 private boolean sidebarEnabled; 60 61 64 private final Map <DiffSidebar, Object > sideBars = new WeakHashMap <DiffSidebar, Object >(); 65 66 private DiffSidebarManager() { 67 sidebarEnabled = VersioningConfig.getDefault().getPreferences().getBoolean(SIDEBAR_ENABLED, true); 68 VersioningConfig.getDefault().getPreferences().addPreferenceChangeListener(this); 69 } 70 71 77 RequestProcessor.Task createDiffSidebarTask(Runnable runnable) { 78 return Utils.createTask(runnable); 79 } 80 81 JComponent createSideBar(JTextComponent target) { 82 return getSideBar(target); 83 } 84 85 private DiffSidebar getSideBar(JTextComponent target) { 86 synchronized(sideBars) { 87 DiffSidebar sideBar = null; 88 for (DiffSidebar bar : sideBars.keySet()) { 89 if (bar.getTextComponent() == target) { 90 sideBar = bar; 91 break; 92 } 93 } 94 if (sideBar == null) { 95 File file = null; 96 Document doc = target.getDocument(); 97 DataObject dobj = (DataObject) doc.getProperty(Document.StreamDescriptionProperty); 98 if (dobj != null) { 99 FileObject fo = dobj.getPrimaryFile(); 100 file = FileUtil.toFile(fo); 101 } 102 if (file == null) return null; 103 104 OriginalContent originalContent = null; 105 VersioningSystem vs = VersioningManager.getInstance().getOwner(file); 106 if (vs != null) { 107 originalContent = vs.getVCSOriginalContent(file); 108 } 109 if (originalContent == null) return null; 110 111 sideBar = new DiffSidebar(target, originalContent); 112 sideBars.put(sideBar, null); 113 sideBar.setSidebarVisible(sidebarEnabled); 114 } 115 return sideBar; 116 } 117 } 118 119 private void setSidebarEnabled(boolean enable) { 120 synchronized(sideBars) { 121 for (DiffSidebar sideBar : sideBars.keySet()) { 122 sideBar.setSidebarVisible(enable); 123 } 124 sidebarEnabled = enable; 125 } 126 } 127 128 MarkProvider createMarkProvider(JTextComponent target) { 129 DiffSidebar sideBar = getSideBar(target); 130 return (sideBar != null) ? sideBar.getMarkProvider() : null; 131 } 132 133 DiffSidebar t9y_getSidebar() { 134 return sideBars.keySet().iterator().next(); 135 } 136 137 public void preferenceChange(PreferenceChangeEvent evt) { 138 if (evt.getKey().equals(SIDEBAR_ENABLED)) { 139 setSidebarEnabled(Boolean.valueOf(evt.getNewValue())); 140 } 141 } 142 } 143 | Popular Tags |