Adding Git source, NetBeans project files, GPL v2 LICENSE, ant build file.
[nbgit.git] / src / org / netbeans / modules / git / ExceptionHandler.java
blobe7e5335db6ca7fbb798824deca34db51526bed46
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6 * The contents of this file are subject to the terms of either the GNU
7 * General Public License Version 2 only ("GPL") or the Common
8 * Development and Distribution License("CDDL") (collectively, the
9 * "License"). You may not use this file except in compliance with the
10 * License. You can obtain a copy of the License at
11 * http://www.netbeans.org/cddl-gplv2.html
12 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13 * specific language governing permissions and limitations under the
14 * License. When distributing the software, include this License Header
15 * Notice in each file and include the License file at
16 * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17 * particular file as subject to the "Classpath" exception as provided
18 * by Sun in the GPL Version 2 section of the License file that
19 * accompanied this code. If applicable, add the following below the
20 * License Header, with the fields enclosed by brackets [] replaced by
21 * your own identifying information:
22 * "Portions Copyrighted [year] [name of copyright owner]"
24 * Contributor(s):
26 * The Original Software is NetBeans. The Initial Developer of the Original
27 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28 * Microsystems, Inc. All Rights Reserved.
29 * Portions Copyright 2008 Alexander Coles (Ikonoklastik Productions).
31 * If you wish your version of this file to be governed by only the CDDL
32 * or only the GPL Version 2, indicate your decision by adding
33 * "[Contributor] elects to include this software in this distribution
34 * under the [CDDL or GPL Version 2] license." If you do not indicate a
35 * single choice of license, a recipient has the option to distribute
36 * your version of this file under either the CDDL, the GPL Version 2 or
37 * to extend the choice of license to its licensees as provided above.
38 * However, if you add GPL Version 2 code and therefore, elected the GPL
39 * Version 2 license, then the option applies only if the new code is
40 * made subject to such option by the copyright holder.
42 package org.netbeans.modules.git;
44 import java.util.logging.Level;
45 import javax.swing.JButton;
46 import org.openide.DialogDisplayer;
47 import org.openide.NotifyDescriptor;
48 import org.openide.util.NbBundle;
51 public class ExceptionHandler {
53 public final static int EX_UNKNOWN = 0;
54 public final static int EX_ACTION_CANCELED_BY_USER = 1;
55 public final static int EX_NO_REPOSITORY = 4;
56 public final static int EX_ALREADY_TRACKED = 8;
57 public final static int EX_NOT_TRACKED = 16;
58 public final static int EX_CANNOT_READ_COMMIT_MSG = 32;
59 public final static int EX_UNABLE_EXECUTE_CMD = 64;
61 static final String ACTION_CANCELED_BY_USER = ""; // TODO: initialize from strings
63 private final GitException exception;
64 private final int exceptionMask;
66 public ExceptionHandler(GitException exception) {
67 this.exception = exception;
68 exceptionMask = getMask(exception.getMessage());
71 protected int getExceptionMask() {
72 return exceptionMask;
75 protected GitException getException() {
76 return exception;
79 private static int getMask(String msg) {
80 if (msg == null || msg.trim().equals("")) { // NOI18N
82 return EX_UNKNOWN;
84 msg = msg.toLowerCase();
85 if (isCancelledAction(msg)) {
86 return EX_ACTION_CANCELED_BY_USER;
87 } else if (isNoRepository(msg)) {
88 return EX_NO_REPOSITORY;
89 } else if (isNotTracked(msg)) {
90 return EX_NOT_TRACKED;
91 } else if (isCannotReadCommitMsg(msg)) {
92 return EX_CANNOT_READ_COMMIT_MSG;
93 } else if (isUnableExecuteCmd(msg)) {
94 return EX_UNABLE_EXECUTE_CMD;
95 } else if (isAlreadyTracked(msg)) {
96 return EX_ALREADY_TRACKED;
98 return EX_UNKNOWN;
101 private static boolean isCancelledAction(String msg) {
102 return msg.equals(ACTION_CANCELED_BY_USER);
105 public static boolean isNoRepository(String msg) {
106 msg = msg.toLowerCase();
107 return msg.indexOf("abort: There is no Git repository here") > -1; // NOI18N
110 public static boolean isUpdateSpansBranches(String msg) {
111 msg = msg.toLowerCase();
112 return msg.indexOf("abort: update spans branches") > -1; // NOI18N
115 private static boolean isAlreadyTracked(String msg) {
116 return msg.indexOf(" already tracked!") > -1; // NOI18N
119 private static boolean isNotTracked(String msg) {
120 return msg.indexOf(" not tracked!") > -1; // NOI18N
123 private static boolean isCannotReadCommitMsg(String msg) {
124 return msg.indexOf("abort: can't read commit message") > -1; // NOI18N
127 public static boolean isUnableExecuteCmd(String msg) {
128 return msg.indexOf("unable to execute Git command") > -1; // NOI18N
131 public static boolean isUnableClone(String msg) {
132 return msg.indexOf("abort: destination ") > -1; // NOI18N
135 public static boolean isNotFound(String msg) {
136 return msg.indexOf("not found!") > -1; // NOI18N
139 public static boolean isNoChangeNeeded(String msg) {
140 return msg.indexOf("no change needed") > -1; // NOI18N
143 // TODO: decide how to report exceptions to the user - information dialog??
144 /** Analyzes exception and notifies user. **/
145 public void notifyException() {
146 if(isCancelledAction(exception.getMessage())) {
147 cancelledAction();
148 return;
150 Git.LOG.log(Level.INFO, null, exception);
153 public void notifyException(boolean notCancelled) {
154 if (!notCancelled) {
155 cancelledAction();
156 return;
158 Git.LOG.log(Level.INFO, null, exception);
161 public static String parseExceptionMessage(GitException exception) {
162 String msg = exception.getMessage();
163 int idx = msg.lastIndexOf("git: "); // NOI18N
164 if (idx > -1) {
165 msg = msg.substring(idx);
167 return msg;
170 private void cancelledAction() {
171 JButton ok = new JButton(NbBundle.getMessage(ExceptionHandler.class, "CTL_Action_OK")); // NOI18N
173 NotifyDescriptor descriptor = new NotifyDescriptor(
174 ACTION_CANCELED_BY_USER,
175 NbBundle.getMessage(ExceptionHandler.class, "CTL_ActionCanceled_Title"), // NOI18N
176 NotifyDescriptor.DEFAULT_OPTION,
177 NotifyDescriptor.WARNING_MESSAGE,
178 new Object[]{ok },
179 ok);
180 DialogDisplayer.getDefault().notify(descriptor);