update copyright
[fedora-idea.git] / plugins / cvs / cvs-plugin / src / com / intellij / cvsSupport2 / application / CvsInfo.java
bloba1ea43c4d28e6130824399652d24f6762ae3b232
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.cvsSupport2.application;
18 import com.intellij.CvsBundle;
19 import com.intellij.cvsSupport2.CvsUtil;
20 import com.intellij.cvsSupport2.config.CvsApplicationLevelConfiguration;
21 import com.intellij.cvsSupport2.config.CvsRootConfiguration;
22 import com.intellij.cvsSupport2.connections.CvsConnectionSettings;
23 import com.intellij.cvsSupport2.connections.login.CvsLoginWorker;
24 import com.intellij.cvsSupport2.cvsExecution.ModalityContext;
25 import com.intellij.cvsSupport2.cvsIgnore.IgnoredFilesInfo;
26 import com.intellij.cvsSupport2.cvsIgnore.IgnoredFilesInfoImpl;
27 import com.intellij.cvsSupport2.errorHandling.ErrorRegistry;
28 import com.intellij.cvsSupport2.javacvsImpl.io.ReadWriteStatistics;
29 import com.intellij.openapi.application.ApplicationManager;
30 import com.intellij.openapi.project.Project;
31 import com.intellij.openapi.ui.MessageType;
32 import com.intellij.openapi.vfs.VirtualFile;
33 import com.intellij.openapi.vcs.changes.ui.ChangesViewBalloonProblemNotifier;
34 import com.intellij.util.ThreeState;
35 import org.jetbrains.annotations.Nullable;
36 import org.netbeans.lib.cvsclient.admin.Entries;
37 import org.netbeans.lib.cvsclient.admin.Entry;
38 import org.netbeans.lib.cvsclient.command.CommandException;
39 import org.netbeans.lib.cvsclient.connection.IConnection;
41 import java.io.File;
42 import java.util.ArrayList;
43 import java.util.Collection;
44 import java.util.HashSet;
46 /**
47 * author: lesya
49 public class CvsInfo {
51 private volatile IgnoredFilesInfo myIgnoreFilter;
52 private CvsConnectionSettings myConnectionSettings;
54 private String myRepository;
55 private String myStickyTag;
56 private Entries myEntries;
58 private boolean myIsLoaded = false;
60 private final VirtualFile myParent;
61 private final CvsEntriesManager myCvsEntriesManager;
62 private static final VirtualFile DUMMY_ROOT = null;
64 private boolean myStickyTagIsLoaded = false;
66 public CvsInfo(VirtualFile parent, CvsEntriesManager cvsEntriesManager) {
67 myParent = parent;
68 myCvsEntriesManager = cvsEntriesManager;
71 public synchronized CvsConnectionSettings getConnectionSettings() {
72 checkIsLoaded();
73 return myConnectionSettings;
76 private void checkIsLoaded() {
78 if (myIsLoaded) return;
79 try {
80 loadCvsRoot();
81 loadEntries();
82 loadRepository();
83 loadStickyTag();
85 if (myRepository != null && myConnectionSettings != null && myConnectionSettings.REPOSITORY != null) {
86 myRepository = CvsUtil.getRelativeRepositoryPath(myRepository, myConnectionSettings.REPOSITORY);
89 myCvsEntriesManager.watchForCvsAdminFiles(myParent);
91 finally {
92 myIsLoaded = true;
96 private void loadStickyTag() {
97 myStickyTag = CvsUtil.loadStickyTagFrom(getParentFile());
100 private void loadRepository() {
101 myRepository = CvsUtil.loadRepositoryFrom(getParentFile());
104 private void loadCvsRoot() {
105 String cvsRoot = CvsUtil.loadRootFrom(getParentFile());
106 try {
107 if (cvsRoot == null) {
108 myConnectionSettings = getAbsentSettings();
110 else {
111 myConnectionSettings = myCvsEntriesManager.createConnectionSettingsOn(cvsRoot);
114 catch (Exception ex) {
115 myConnectionSettings = new MyInvalidCvsConnectionSettings();
119 public synchronized IgnoredFilesInfo getIgnoreFilter() {
120 checkFilterIsLoaded();
121 return myIgnoreFilter;
124 private void checkFilterIsLoaded() {
125 if (myIgnoreFilter == null) {
126 if (myParent == null) {
127 myIgnoreFilter = IgnoredFilesInfo.IGNORE_NOTHING;
129 else if (myCvsEntriesManager.fileIsIgnored(myParent)) {
130 myIgnoreFilter = IgnoredFilesInfo.IGNORE_ALL;
132 else if (!CvsUtil.fileIsUnderCvs(myParent)) {
133 myIgnoreFilter = IgnoredFilesInfo.IGNORE_NOTHING;
135 else {
136 myIgnoreFilter = IgnoredFilesInfoImpl.createForFile(CvsUtil.cvsignoreFileFor(getParentFile()));
141 private Entries getCvsEntries() {
142 checkIsLoaded();
143 return myEntries;
146 private void loadEntries() {
147 if (myParent != DUMMY_ROOT) {
148 myEntries = createEntriesFor(getParentFile());
150 else {
151 myEntries = new Entries();
155 public synchronized Collection<Entry> getEntries() {
156 return new HashSet<Entry>(getCvsEntries().getEntries());
159 @Nullable
160 private File getParentFile() {
161 return myParent == null ? null : new File(myParent.getPath());
164 private static Entries createEntriesFor(File parent) {
165 Entries entries = CvsUtil.getEntriesIn(new File(parent.getPath()));
166 if (entries == null) {
167 return new Entries();
169 else {
170 return entries;
175 public void clearFilter() {
176 myIgnoreFilter = null;
179 public synchronized boolean isLoaded() {
180 return myIsLoaded;
183 public synchronized Entry setEntryAndReturnReplacedEntry(Entry entry) {
184 Entry previousEntry = getEntryNamed(entry.getFileName());
185 appendEntry(entry);
186 return previousEntry;
189 private void appendEntry(Entry newEntry) {
190 getCvsEntries().addEntry(newEntry);
193 public synchronized void removeEntryNamed(String fileName) {
194 removeEntry(fileName);
197 private void removeEntry(String fileName) {
198 getCvsEntries().removeEntry(fileName);
201 public synchronized VirtualFile getKey() {
202 return myParent;
205 public synchronized Entry getEntryNamed(String name) {
206 return getCvsEntries().getEntry(name);
209 public synchronized String getRepository() {
210 checkIsLoaded();
211 return myRepository;
214 public synchronized String getStickyTag() {
215 checkStickyTagIsLoaded();
216 return myStickyTag;
219 private void checkStickyTagIsLoaded() {
220 if (!myStickyTagIsLoaded){
221 loadStickyTag();
222 myStickyTagIsLoaded = true;
226 public synchronized void cacheAll() {
227 checkIsLoaded();
230 public synchronized void clearAll() {
231 myEntries = null;
232 myRepository = null;
233 myStickyTagIsLoaded = false;
234 myConnectionSettings = null;
235 myIsLoaded = false;
238 public synchronized void clearStickyInformation() {
239 myStickyTagIsLoaded = false;
242 private static class CvsConnectionSettingsHolder {
243 private static final CvsConnectionSettings ABSENT_SETTINGS = new MyInvalidCvsConnectionSettings();
246 public static CvsConnectionSettings getAbsentSettings() {
247 return CvsConnectionSettingsHolder.ABSENT_SETTINGS;
250 private static class CvsInfoHolder {
251 private static final CvsInfo DUMMY = new DummyCvsInfo();
254 public static CvsInfo getDummyCvsInfo() {
255 return CvsInfoHolder.DUMMY;
258 private static class MyInvalidCvsConnectionSettings extends CvsConnectionSettings {
259 public MyInvalidCvsConnectionSettings() {
260 super(CvsApplicationLevelConfiguration.createNewConfiguration(CvsApplicationLevelConfiguration.getInstance()));
263 public int getDefaultPort() {
264 return 0;
267 public IConnection createConnection(ReadWriteStatistics statistics) {
268 throw new RuntimeException(CvsBundle.message("exception.text.cannot.connect.with.invalid.root"));
271 protected IConnection createOriginalConnection(ErrorRegistry errorRegistry, CvsRootConfiguration cvsRootConfiguration) {
272 throw new RuntimeException(CvsBundle.message("exception.text.cannot.connect.with.invalid.root"));
275 public CommandException processException(CommandException t) {
276 return t;
279 @Override
280 public void setOffline(boolean offline) {
281 throw new RuntimeException(CvsBundle.message("exception.text.cannot.do.setoffline.with.invalid.root"));
284 @Override
285 public boolean isOffline() {
286 return true;
289 public CvsLoginWorker getLoginWorker(ModalityContext executor, final Project project) {
290 return new CvsLoginWorker() {
291 public boolean promptForPassword() {
292 return true;
295 public ThreeState silentLogin(boolean forceCheck) {
296 ApplicationManager.getApplication().invokeLater(new Runnable() {
297 public void run() {
298 new ChangesViewBalloonProblemNotifier(project, CvsBundle.message("message.error.invalid.cvs.root", myStringRepsentation),
299 MessageType.ERROR).run();
302 return ThreeState.NO;
305 public void goOffline() {
306 setOffline(true);
311 public boolean isValid() {
312 return false;
316 @SuppressWarnings({"NonSynchronizedMethodOverridesSynchronizedMethod"})
317 private static class DummyCvsInfo extends CvsInfo {
318 public DummyCvsInfo() {
319 super(null, null);
322 public CvsConnectionSettings getConnectionSettings() {
323 return getAbsentSettings();
326 public IgnoredFilesInfo getIgnoreFilter() {
327 return IgnoredFilesInfoImpl.EMPTY_FILTER;
330 public Collection<Entry> getEntries() {
331 return new ArrayList<Entry>();
334 public void clearFilter() {
337 public boolean isLoaded() {
338 return true;
341 public Entry setEntryAndReturnReplacedEntry(Entry entry) {
342 return null;
345 public void removeEntryNamed(String fileName) {
348 public VirtualFile getKey() {
349 return null;
352 public Entry getEntryNamed(String name) {
353 return null;