001: /*+
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.mercurial;
042:
043: import java.beans.PropertyChangeListener;
044: import java.beans.PropertyChangeSupport;
045: import java.io.*;
046: import java.util.HashMap;
047: import java.util.logging.Logger;
048: import java.util.logging.Level;
049: import org.netbeans.modules.mercurial.util.HgUtils;
050: import org.netbeans.modules.versioning.spi.VCSContext;
051: import org.netbeans.modules.versioning.spi.VersioningSupport;
052: import org.openide.util.RequestProcessor;
053: import org.openide.filesystems.FileObject;
054: import org.openide.filesystems.FileUtil;
055: import org.netbeans.modules.mercurial.ui.diff.Setup;
056: import org.netbeans.modules.mercurial.util.HgCommand;
057: import org.openide.util.NbBundle;
058: import javax.swing.JOptionPane;
059: import java.util.prefs.Preferences;
060: import org.openide.NotifyDescriptor;
061: import org.openide.DialogDisplayer;
062:
063: /**
064: * Main entry point for Mercurial functionality, use getInstance() to get the Mercurial object.
065: *
066: * @author Maros Sandor
067: */
068: public class Mercurial {
069: public static final String MERCURIAL_OUTPUT_TAB_TITLE = org.openide.util.NbBundle
070: .getMessage(Mercurial.class, "CTL_Mercurial_DisplayName"); // NOI18N
071: public static final String CHANGESET_STR = "changeset:"; // NOI18N
072:
073: static final String PROP_ANNOTATIONS_CHANGED = "annotationsChanged"; // NOI18N
074: static final String PROP_VERSIONED_FILES_CHANGED = "versionedFilesChanged"; // NOI18N
075: public static final String PROP_CHANGESET_CHANGED = "changesetChanged"; // NOI18N
076:
077: public static final Logger LOG = Logger
078: .getLogger("org.netbeans.modules.mercurial"); // NOI18N
079:
080: private static final int STATUS_DIFFABLE = FileInformation.STATUS_VERSIONED_UPTODATE
081: | FileInformation.STATUS_VERSIONED_MODIFIEDLOCALLY
082: | FileInformation.STATUS_VERSIONED_MODIFIEDINREPOSITORY
083: | FileInformation.STATUS_VERSIONED_CONFLICT
084: | FileInformation.STATUS_VERSIONED_MERGE
085: | FileInformation.STATUS_VERSIONED_REMOVEDINREPOSITORY
086: | FileInformation.STATUS_VERSIONED_MODIFIEDINREPOSITORY
087: | FileInformation.STATUS_VERSIONED_MODIFIEDINREPOSITORY;
088:
089: private static final String MERCURIAL_SUPPORTED_VERSION_093 = "0.9.3"; // NOI18N
090: private static final String MERCURIAL_SUPPORTED_VERSION_094 = "0.9.4"; // NOI18N
091: private static final String MERCURIAL_SUPPORTED_VERSION_095 = "0.9.5"; // NOI18N
092: private static Mercurial instance;
093: private final PropertyChangeSupport support = new PropertyChangeSupport(
094: this );
095:
096: public static synchronized Mercurial getInstance() {
097: if (instance == null) {
098: instance = new Mercurial();
099: instance.init();
100: }
101: return instance;
102: }
103:
104: private MercurialAnnotator mercurialAnnotator;
105: private MercurialInterceptor mercurialInterceptor;
106: private FileStatusCache fileStatusCache;
107: private HashMap<String, RequestProcessor> processorsToUrl;
108: private boolean goodVersion;
109: private String version;
110: private String runVersion;
111: private boolean checkedVersion;
112:
113: private Mercurial() {
114: }
115:
116: private void init() {
117: checkedVersion = false;
118: setDefaultPath();
119: fileStatusCache = new FileStatusCache();
120: mercurialAnnotator = new MercurialAnnotator();
121: mercurialInterceptor = new MercurialInterceptor();
122: checkVersion(); // Does the Hg check but postpones querying user until menu is activated
123: }
124:
125: private void setDefaultPath() {
126: // Set default executable location for mercurial on mac
127: if (System.getProperty("os.name").equals("Mac OS X")) { // NOI18N
128: String defaultPath = HgModuleConfig.getDefault()
129: .getExecutableBinaryPath();
130: if (defaultPath == null || defaultPath.length() == 0) {
131: String[] pathNames = {
132: "/Library/Frameworks/Python.framework/Versions/Current/bin", // NOI18N
133: "/usr/bin", "/usr/local/bin",
134: "/opt/local/bin/", "/sw/bin" }; // NOI18N
135: for (int i = 0; i < pathNames.length; i++) {
136: if (HgModuleConfig.getDefault().isExecPathValid(
137: pathNames[i])) {
138: HgModuleConfig.getDefault()
139: .setExecutableBinaryPath(pathNames[i]); // NOI18N
140: break;
141: }
142: }
143: }
144: }
145: }
146:
147: private void checkVersion() {
148: version = HgCommand.getHgVersion();
149: LOG.log(Level.FINE, "version: {0}", version); // NOI18N
150: if (version != null) {
151: goodVersion = version
152: .startsWith(MERCURIAL_SUPPORTED_VERSION_093)
153: || version
154: .startsWith(MERCURIAL_SUPPORTED_VERSION_094)
155: || version
156: .startsWith(MERCURIAL_SUPPORTED_VERSION_095);
157: if (!goodVersion) {
158: Preferences prefs = HgModuleConfig.getDefault()
159: .getPreferences();
160: runVersion = prefs.get(HgModuleConfig.PROP_RUN_VERSION,
161: null);
162: if (runVersion != null && runVersion.equals(version)) {
163: goodVersion = true;
164: }
165: }
166: } else {
167: goodVersion = false;
168: }
169: }
170:
171: public void checkVersionNotify() {
172: if (version != null && !goodVersion) {
173: if (runVersion == null || !runVersion.equals(version)) {
174: Preferences prefs = HgModuleConfig.getDefault()
175: .getPreferences();
176: NotifyDescriptor descriptor = new NotifyDescriptor.Confirmation(
177: NbBundle.getMessage(Mercurial.class,
178: "MSG_VERSION_CONFIRM_QUERY", version)); // NOI18N
179: descriptor.setTitle(NbBundle.getMessage(
180: Mercurial.class, "MSG_VERSION_CONFIRM")); // NOI18N
181: descriptor.setMessageType(JOptionPane.WARNING_MESSAGE);
182: descriptor
183: .setOptionType(NotifyDescriptor.YES_NO_OPTION);
184:
185: Object res = DialogDisplayer.getDefault().notify(
186: descriptor);
187: OutputLogger logger = getLogger(Mercurial.MERCURIAL_OUTPUT_TAB_TITLE);
188: if (res == NotifyDescriptor.YES_OPTION) {
189: goodVersion = true;
190: prefs.put(HgModuleConfig.PROP_RUN_VERSION, version);
191: logger.outputInRed(NbBundle.getMessage(
192: Mercurial.class, "MSG_USING_VERSION_MSG",
193: version)); // NOI18N);
194: } else {
195: prefs.remove(HgModuleConfig.PROP_RUN_VERSION);
196: logger.outputInRed(NbBundle.getMessage(
197: Mercurial.class,
198: "MSG_NOT_USING_VERSION_MSG", version)); // NOI18N);
199: }
200: logger.closeLog();
201: } else {
202: goodVersion = true;
203: }
204: } else if (version == null) {
205: Preferences prefs = HgModuleConfig.getDefault()
206: .getPreferences();
207: prefs.remove(HgModuleConfig.PROP_RUN_VERSION);
208: OutputLogger logger = getLogger(Mercurial.MERCURIAL_OUTPUT_TAB_TITLE);
209: logger.outputInRed(NbBundle.getMessage(Mercurial.class,
210: "MSG_VERSION_NONE_OUTPUT_MSG")); // NOI18N);
211: HgUtils.warningDialog(Mercurial.class,
212: "MSG_VERSION_NONE_TITLE", "MSG_VERSION_NONE_MSG");// NOI18N
213: logger.closeLog();
214: }
215: }
216:
217: public MercurialAnnotator getMercurialAnnotator() {
218: return mercurialAnnotator;
219: }
220:
221: MercurialInterceptor getMercurialInterceptor() {
222: return mercurialInterceptor;
223: }
224:
225: /**
226: * Gets the File Status Cache for the mercurial repository
227: *
228: * @return FileStatusCache for the repository
229: */
230: public FileStatusCache getFileStatusCache() {
231: return fileStatusCache;
232: }
233:
234: /**
235: * Tests <tt>.hg</tt> directory itself.
236: */
237: public boolean isAdministrative(File file) {
238: String name = file.getName();
239: return isAdministrative(name) && file.isDirectory();
240: }
241:
242: public boolean isAdministrative(String fileName) {
243: return fileName.equals(".hg"); // NOI18N
244: }
245:
246: /**
247: * Tests whether a file or directory should receive the STATUS_NOTVERSIONED_NOTMANAGED status.
248: * All files and folders that have a parent with CVS/Repository file are considered versioned.
249: *
250: * @param file a file or directory
251: * @return false if the file should receive the STATUS_NOTVERSIONED_NOTMANAGED status, true otherwise
252: */
253: public boolean isManaged(File file) {
254: return VersioningSupport.getOwner(file) instanceof MercurialVCS
255: && !HgUtils.isPartOfMercurialMetadata(file);
256: }
257:
258: public File getTopmostManagedParent(File file) {
259: if (HgUtils.isPartOfMercurialMetadata(file)) {
260: for (; file != null; file = file.getParentFile()) {
261: if (isAdministrative(file)) {
262: file = file.getParentFile();
263: break;
264: }
265: }
266: }
267: File topmost = null;
268: for (; file != null; file = file.getParentFile()) {
269: if (org.netbeans.modules.versioning.util.Utils
270: .isScanForbidden(file))
271: break;
272: if (new File(file, ".hg").canWrite()) { // NOI18N
273: topmost = file;
274: break;
275: }
276: }
277: return topmost;
278: }
279:
280: public HgFileNode[] getNodes(VCSContext context, int includeStatus) {
281: File[] files = fileStatusCache
282: .listFiles(context, includeStatus);
283: HgFileNode[] nodes = new HgFileNode[files.length];
284: for (int i = 0; i < files.length; i++) {
285: nodes[i] = new HgFileNode(files[i]);
286: }
287: return nodes;
288: }
289:
290: /**
291: * Uses content analysis to return the mime type for files.
292: *
293: * @param file file to examine
294: * @return String mime type of the file (or best guess)
295: */
296: public String getMimeType(File file) {
297: FileObject fo = FileUtil.toFileObject(file);
298: String foMime;
299: if (fo == null) {
300: foMime = "content/unknown"; // NOI18N
301: } else {
302: foMime = fo.getMIMEType();
303: if ("content/unknown".equals(foMime)) { // NOI18N
304: foMime = "text/plain"; // NOI18N
305: }
306: }
307: if ((fileStatusCache.getStatus(file).getStatus() & FileInformation.STATUS_VERSIONED) == 0) {
308: return HgUtils.isFileContentBinary(file) ? "application/octet-stream"
309: : foMime; // NOI18N
310: } else {
311: return foMime;
312: }
313: }
314:
315: public boolean isGoodVersion() {
316: return goodVersion;
317: }
318:
319: public boolean isGoodVersionAndNotify() {
320: if (checkedVersion == false) {
321: checkVersionNotify();
322: checkedVersion = true;
323: }
324: return goodVersion;
325: }
326:
327: public void versionedFilesChanged() {
328: support.firePropertyChange(PROP_VERSIONED_FILES_CHANGED, null,
329: null);
330: }
331:
332: public void refreshAllAnnotations() {
333: support
334: .firePropertyChange(PROP_ANNOTATIONS_CHANGED, null,
335: null);
336: }
337:
338: public void changesetChanged(File repository) {
339: support.firePropertyChange(PROP_CHANGESET_CHANGED, repository,
340: null);
341: }
342:
343: public void addPropertyChangeListener(
344: PropertyChangeListener listener) {
345: support.addPropertyChangeListener(listener);
346: }
347:
348: public void removePropertyChangeListener(
349: PropertyChangeListener listener) {
350: support.removePropertyChangeListener(listener);
351: }
352:
353: public void getOriginalFile(File workingCopy, File originalFile) {
354: FileInformation info = fileStatusCache.getStatus(workingCopy);
355: if ((info.getStatus() & STATUS_DIFFABLE) == 0)
356: return;
357:
358: try {
359: File original = VersionsCache.getInstance()
360: .getFileRevision(workingCopy, Setup.REVISION_BASE);
361: if (original == null) {
362: throw new IOException("Unable to get BASE revision of "
363: + workingCopy);
364: }
365: org.netbeans.modules.versioning.util.Utils
366: .copyStreamsCloseAll(new FileOutputStream(
367: originalFile),
368: new FileInputStream(original));
369: original.delete();
370: } catch (IOException e) {
371: Logger.getLogger(Mercurial.class.getName()).log(Level.INFO,
372: "Unable to get original file", e); // NOI18N
373: }
374: }
375:
376: /**
377: * Serializes all Hg requests (moves them out of AWT).
378: */
379: public RequestProcessor getRequestProcessor() {
380: return getRequestProcessor((String) null);
381: }
382:
383: /**
384: * Serializes all Hg requests (moves them out of AWT).
385: */
386: public RequestProcessor getRequestProcessor(File file) {
387: return getRequestProcessor(file.getAbsolutePath());
388: }
389:
390: public RequestProcessor getRequestProcessor(String url) {
391: if (processorsToUrl == null) {
392: processorsToUrl = new HashMap<String, RequestProcessor>();
393: }
394:
395: String key;
396: if (url != null) {
397: key = url;
398: } else {
399: key = "ANY_URL"; // NOI18N
400: }
401:
402: RequestProcessor rp = processorsToUrl.get(key);
403: if (rp == null) {
404: rp = new RequestProcessor("Mercurial - " + key, 1, true); // NOI18N
405: processorsToUrl.put(key, rp);
406: }
407: return rp;
408: }
409:
410: public void clearRequestProcessor(String url) {
411: if (processorsToUrl != null & url != null) {
412: processorsToUrl.remove(url);
413: }
414: }
415:
416: /**
417: *
418: * @param repositoryRoot String of Mercurial repository so that logger writes to correct output tab. Can be null
419: * in which case the logger will not print anything
420: * @return OutputLogger logger to write to
421: */
422: public OutputLogger getLogger(String repositoryRoot) {
423: return OutputLogger.getLogger(repositoryRoot);
424: }
425: }
|