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 javax.swing.JButton;
044: import java.util.logging.Level;
045: import org.openide.DialogDisplayer;
046: import org.openide.NotifyDescriptor;
047: import org.openide.util.NbBundle;
048:
049: /**
050: *
051: * @author Tomas Stupka
052: */
053: public class ExceptionHandler {
054:
055: public final static int EX_UNKNOWN = 0;
056: public final static int EX_ACTION_CANCELED_BY_USER = 2;
057: public final static int EX_NO_REPOSITORY = 4;
058: public final static int EX_ALREADY_TRACKED = 8;
059: public final static int EX_NOT_TRACKED = 16;
060: public final static int EX_CANNOT_READ_COMMIT_MSG = 32;
061: public final static int EX_UNABLE_EXECUTE_CMD = 64;
062:
063: static final String ACTION_CANCELED_BY_USER = org.openide.util.NbBundle
064: .getMessage(ExceptionHandler.class,
065: "MSG_ActionCanceledByUser"); // NOI18N
066: private final HgException exception;
067: private final int exceptionMask;
068:
069: public ExceptionHandler(HgException exception) {
070: this .exception = exception;
071: exceptionMask = getMask(exception.getMessage());
072: }
073:
074: protected int getExceptionMask() {
075: return exceptionMask;
076: }
077:
078: protected HgException getException() {
079: return exception;
080: }
081:
082: private static int getMask(String msg) {
083: if (msg == null || msg.trim().equals("")) { // NOI18N
084: return EX_UNKNOWN;
085: }
086: msg = msg.toLowerCase();
087: if (isCancelledAction(msg)) {
088: return EX_ACTION_CANCELED_BY_USER;
089: } else if (isNoRepository(msg)) {
090: return EX_NO_REPOSITORY;
091: } else if (isNotTracked(msg)) {
092: return EX_NOT_TRACKED;
093: } else if (isCannotReadCommitMsg(msg)) {
094: return EX_CANNOT_READ_COMMIT_MSG;
095: } else if (isUnableExecuteCmd(msg)) {
096: return EX_UNABLE_EXECUTE_CMD;
097: } else if (isAlreadyTracked(msg)) {
098: return EX_ALREADY_TRACKED;
099: }
100: return EX_UNKNOWN;
101: }
102:
103: private static boolean isCancelledAction(String msg) {
104: return msg.equals(ACTION_CANCELED_BY_USER);
105: }
106:
107: public static boolean isNoRepository(String msg) {
108: msg = msg.toLowerCase();
109: return msg
110: .indexOf("abort: There is no Mercurial repository here") > -1; // NOI18N
111: }
112:
113: public static boolean isUpdateSpansBranches(String msg) {
114: msg = msg.toLowerCase();
115: return msg.indexOf("abort: update spans branches") > -1; // NOI18N
116: }
117:
118: public static boolean isAlreadyTracked(String msg) {
119: return msg.indexOf(" already tracked!") > -1; // NOI18N
120: }
121:
122: public static boolean isNotTracked(String msg) {
123: return msg.indexOf(" not tracked!") > -1; // NOI18N
124: }
125:
126: public static boolean isCannotReadCommitMsg(String msg) {
127: return msg.indexOf("abort: can't read commit message") > -1; // NOI18N
128: }
129:
130: public static boolean isUnableExecuteCmd(String msg) {
131: return msg.indexOf("unable to execute hg command") > -1; // NOI18N
132: }
133:
134: public static boolean isUnableClone(String msg) {
135: return msg.indexOf("abort: destination ") > -1; // NOI18N
136: }
137:
138: public static boolean isNotFound(String msg) {
139: return msg.indexOf("not found!") > -1; // NOI18N
140: }
141:
142: public static boolean isNoChangeNeeded(String msg) {
143: return msg.indexOf("no change needed") > -1; // NOI18N
144: }
145:
146: // TODO: decide how to report exceptions to the user - information dialog??
147: /** Analyzes exception and notifies user. */
148: public void notifyException() {
149: if (isCancelledAction(exception.getMessage())) {
150: cancelledAction();
151: return;
152: }
153: Mercurial.LOG.log(Level.INFO, null, exception);
154: }
155:
156: public void notifyException(boolean notCanceled) {
157: if (!notCanceled) {
158: cancelledAction();
159: return;
160: }
161: Mercurial.LOG.log(Level.INFO, null, exception);
162: }
163:
164: public static String parseExceptionMessage(HgException exception) {
165: String msg = exception.getMessage();
166: int idx = msg.lastIndexOf("hg: "); // NOI18N
167: if (idx > -1) {
168: msg = msg.substring(idx);
169: }
170: return msg;
171: }
172:
173: private void cancelledAction() {
174: JButton ok = new JButton(NbBundle.getMessage(
175: ExceptionHandler.class, "CTL_Action_OK")); // NOI18N
176: NotifyDescriptor descriptor = new NotifyDescriptor(
177: ACTION_CANCELED_BY_USER,
178: NbBundle.getMessage(ExceptionHandler.class,
179: "CTL_ActionCanceled_Title"), // NOI18N
180: NotifyDescriptor.DEFAULT_OPTION,
181: NotifyDescriptor.WARNING_MESSAGE, new Object[] { ok },
182: ok);
183: DialogDisplayer.getDefault().notify(descriptor);
184: return;
185: }
186:
187: }
|