KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > versioning > diff > DiffSidebarManager


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

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 JavaDoc;
34 import javax.swing.text.Document JavaDoc;
35 import java.io.File JavaDoc;
36 import java.util.WeakHashMap JavaDoc;
37 import java.util.Map JavaDoc;
38 import java.util.prefs.PreferenceChangeListener JavaDoc;
39 import java.util.prefs.PreferenceChangeEvent JavaDoc;
40
41 /**
42  * Central place of diff integration into editor and errorstripe.
43  *
44  * @author Maros Sandor
45  */

46 class DiffSidebarManager implements PreferenceChangeListener JavaDoc {
47
48     static final String JavaDoc SIDEBAR_ENABLED = "diff.sidebarEnabled"; // NOI18N
49

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     /**
62      * Holds created sidebars (to be able to show/hide them all at once). It is just a set, values are always null.
63      */

64     private final Map JavaDoc<DiffSidebar, Object JavaDoc> sideBars = new WeakHashMap JavaDoc<DiffSidebar, Object JavaDoc>();
65
66     private DiffSidebarManager() {
67         sidebarEnabled = VersioningConfig.getDefault().getPreferences().getBoolean(SIDEBAR_ENABLED, true);
68         VersioningConfig.getDefault().getPreferences().addPreferenceChangeListener(this);
69     }
70
71     /**
72      * Creates a new task needed by a diff sidebar to update its structures (compute diff).
73      *
74      * @param runnable a runnable task
75      * @return RP task
76      */

77     RequestProcessor.Task createDiffSidebarTask(Runnable JavaDoc runnable) {
78         return Utils.createTask(runnable);
79     }
80
81     JComponent createSideBar(JTextComponent JavaDoc target) {
82         return getSideBar(target);
83     }
84
85     private DiffSidebar getSideBar(JTextComponent JavaDoc 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 JavaDoc file = null;
96                 Document JavaDoc 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 JavaDoc 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 JavaDoc evt) {
138         if (evt.getKey().equals(SIDEBAR_ENABLED)) {
139             setSidebarEnabled(Boolean.valueOf(evt.getNewValue()));
140         }
141     }
142 }
143
Popular Tags