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:
042: package org.netbeans.modules.mercurial.ui.diff;
043:
044: import org.netbeans.api.diff.StreamSource;
045: import org.netbeans.api.diff.DiffController;
046: import org.netbeans.modules.mercurial.Mercurial;
047: import org.netbeans.modules.mercurial.FileInformation;
048: import org.netbeans.modules.mercurial.util.HgCommand;
049: import org.netbeans.modules.mercurial.HgException;
050: import org.netbeans.modules.mercurial.util.HgUtils;
051: import org.openide.util.NbBundle;
052: import org.openide.DialogDisplayer;
053: import org.openide.NotifyDescriptor;
054: import org.openide.NotifyDescriptor.Exception;
055:
056: import java.io.File;
057: import java.io.IOException;
058: import java.util.*;
059: import java.text.MessageFormat;
060:
061: /**
062: * Represents on DIFF setup.
063: *
064: * @author Maros Sandor
065: */
066: public final class Setup {
067:
068: /**
069: * What was locally changed? The right pane contains local file.
070: *
071: * <p>Local addition, removal or change is displayed in
072: * the right pane as addition, removal or change respectively
073: * (i.e. not reversed as removal, addition or change).
074: *
075: * <pre>
076: * diff from-BASE to-LOCAL
077: * </pre>
078: */
079: public static final int DIFFTYPE_LOCAL = 0;
080:
081: /**
082: * What was remotely changed? The right pane contains remote file.
083: *
084: * <p>Remote addition, removal or change is displayed in
085: * the right pane as addition, removal or change respectively
086: * (i.e. not reversed as removal, addition or change).
087: *
088: * <pre>
089: * diff from-BASE to-HEAD
090: * </pre>
091: */
092: public static final int DIFFTYPE_REMOTE = 1;
093:
094: /**
095: * What was locally changed comparing to recent head?
096: * The Right pane contains local file.
097: *
098: * <p> Local addition, removal or change is displayed in
099: * the right pane as addition, removal or change respectively
100: * (i.e. not reversed as removal, addition or change).
101: *
102: * <pre>
103: * diff from-HEAD to-LOCAL
104: * </pre>
105: */
106: public static final int DIFFTYPE_ALL = 2;
107:
108: public static final String REVISION_PRISTINE = "PRISTINE"; // NOI18N
109: public static final String REVISION_BASE = "BASE"; // NOI18N
110: public static final String REVISION_CURRENT = "LOCAL"; // NOI18N
111: public static final String REVISION_HEAD = "HEAD"; // NOI18N
112:
113: private final File baseFile;
114:
115: /**
116: * Name of the file's property if the setup represents a property diff setup, null otherwise.
117: */
118: private final String propertyName;
119:
120: private final String firstRevision;
121: private final String secondRevision;
122: private FileInformation info;
123:
124: private DiffStreamSource firstSource;
125: private DiffStreamSource secondSource;
126:
127: private DiffController view;
128: private DiffNode node;
129:
130: private String title;
131:
132: public Setup(File baseFile, String propertyName, int type) {
133: this .baseFile = baseFile;
134: this .propertyName = propertyName;
135: info = Mercurial.getInstance().getFileStatusCache().getStatus(
136: baseFile);
137: int status = info.getStatus();
138:
139: ResourceBundle loc = NbBundle.getBundle(Setup.class);
140: String firstTitle;
141: String secondTitle;
142:
143: // the first source
144:
145: switch (type) {
146: case DIFFTYPE_LOCAL:
147:
148: // from-BASE
149:
150: if (match(
151: status,
152: FileInformation.STATUS_NOTVERSIONED_NEWLOCALLY
153: | FileInformation.STATUS_VERSIONED_ADDEDLOCALLY)) {
154: firstRevision = REVISION_BASE;
155:
156: firstTitle = loc.getString("MSG_DiffPanel_LocalNew"); // NOI18N
157: } else if (match(status,
158: FileInformation.STATUS_VERSIONED_NEWINREPOSITORY)) {
159: firstRevision = null;
160: firstTitle = NbBundle.getMessage(Setup.class,
161: "LBL_Diff_NoLocalFile"); // NOI18N
162: } else if (match(
163: status,
164: FileInformation.STATUS_VERSIONED_DELETEDLOCALLY
165: | FileInformation.STATUS_VERSIONED_REMOVEDLOCALLY)) {
166: firstRevision = REVISION_BASE;
167: firstTitle = MessageFormat.format(loc
168: .getString("MSG_DiffPanel_BaseRevision"),
169: new Object[] { firstRevision }); // NOI18N
170: } else {
171: firstRevision = REVISION_BASE;
172: firstTitle = MessageFormat.format(loc
173: .getString("MSG_DiffPanel_BaseRevision"),
174: new Object[] { firstRevision }); // NOI18N
175: }
176:
177: break;
178:
179: default:
180: throw new IllegalArgumentException("Unknow diff type: "
181: + type); // NOI18N
182: }
183:
184: // the second source
185:
186: switch (type) {
187: case DIFFTYPE_LOCAL:
188:
189: // to-LOCAL
190:
191: if (match(status, FileInformation.STATUS_VERSIONED_CONFLICT)) {
192: secondRevision = REVISION_CURRENT;
193: secondTitle = MessageFormat.format(loc
194: .getString("MSG_DiffPanel_LocalConflict"),
195: new Object[] { secondRevision }); // NOI18N
196: } else if (match(
197: status,
198: FileInformation.STATUS_NOTVERSIONED_NEWLOCALLY
199: | FileInformation.STATUS_VERSIONED_ADDEDLOCALLY)) {
200: secondRevision = REVISION_CURRENT;
201: secondTitle = loc.getString("MSG_DiffPanel_LocalNew"); // NOI18N
202: } else if (match(status,
203: FileInformation.STATUS_VERSIONED_NEWINREPOSITORY)) {
204: secondRevision = null;
205: secondTitle = NbBundle.getMessage(Setup.class,
206: "LBL_Diff_NoLocalFile"); // NOI18N
207: } else if (match(
208: status,
209: FileInformation.STATUS_VERSIONED_DELETEDLOCALLY
210: | FileInformation.STATUS_VERSIONED_REMOVEDLOCALLY)) {
211: secondRevision = null;
212: secondTitle = loc
213: .getString("MSG_DiffPanel_LocalDeleted"); // NOI18N
214: } else {
215: secondRevision = REVISION_CURRENT;
216: secondTitle = MessageFormat.format(loc
217: .getString("MSG_DiffPanel_LocalModified"),
218: new Object[] { secondRevision }); // NOI18N
219: }
220: break;
221:
222: default:
223: throw new IllegalArgumentException("Unknow diff type: "
224: + type); // NOI18N
225: }
226:
227: firstSource = new DiffStreamSource(baseFile, firstRevision,
228: firstTitle);
229: secondSource = new DiffStreamSource(baseFile, secondRevision,
230: secondTitle);
231: title = "<html>"
232: + Mercurial.getInstance().getMercurialAnnotator()
233: .annotateNameHtml(baseFile, info); // NOI18N
234: }
235:
236: /**
237: * Text file setup for arbitrary revisions.
238: * @param firstRevision first revision or <code>null</code> for inital.
239: * @param secondRevision second revision
240: */
241: public Setup(File baseFile, String firstRevision,
242: String secondRevision) {
243: this .baseFile = baseFile;
244: this .propertyName = null;
245: this .firstRevision = firstRevision;
246: this .secondRevision = secondRevision;
247: firstSource = new DiffStreamSource(baseFile, firstRevision,
248: firstRevision);
249: secondSource = new DiffStreamSource(baseFile, secondRevision,
250: secondRevision);
251: }
252:
253: public String getPropertyName() {
254: return propertyName;
255: }
256:
257: public File getBaseFile() {
258: return baseFile;
259: }
260:
261: public FileInformation getInfo() {
262: return info;
263: }
264:
265: public void setView(DiffController view) {
266: this .view = view;
267: }
268:
269: public DiffController getView() {
270: return view;
271: }
272:
273: public StreamSource getFirstSource() {
274: return firstSource;
275: }
276:
277: public StreamSource getSecondSource() {
278: return secondSource;
279: }
280:
281: public void setNode(DiffNode node) {
282: this .node = node;
283: }
284:
285: public DiffNode getNode() {
286: return node;
287: }
288:
289: public String toString() {
290: return title;
291: }
292:
293: /**
294: * Loads data
295: * @param group that carries shared state. Note that this group must not be executed later on.
296: */
297: void initSources() throws IOException {
298: if (firstSource != null)
299: firstSource.init();
300: if (secondSource != null)
301: secondSource.init();
302: }
303:
304: private static boolean match(int status, int mask) {
305: return (status & mask) != 0;
306: }
307: }
|