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-2007 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.api.diff;
042:
043: import org.netbeans.spi.diff.DiffControllerImpl;
044: import org.netbeans.spi.diff.DiffControllerProvider;
045: import org.openide.util.Lookup;
046:
047: import javax.swing.*;
048: import java.beans.PropertyChangeListener;
049: import java.io.IOException;
050:
051: /**
052: * Encapsulates a single Diff window that displays differences between two files (sources).
053: *
054: * @author Maros Sandor
055: * @since 1.18
056: */
057: public final class DiffController {
058:
059: /**
060: * Property change that indicates that set of differences OR the current difference changed. Current difference
061: * changes as the user navigates in the view and set of differences may change if the view is editable or a source
062: * changes programatically.
063: * Clients should update their state that depends on the current difference index or total number of differences.
064: */
065: public static final String PROP_DIFFERENCES = "(void) differencesChanged"; // NOI18N
066:
067: /**
068: * Enumerates Base (left) and Modified (right) panes of a Diff view for setLocation() method
069: */
070: public enum DiffPane {
071: Base, Modified
072: };
073:
074: /**
075: * Enumerates types of location for setLocation() method.
076: */
077: public enum LocationType {
078: LineNumber, DifferenceIndex
079: };
080:
081: private final DiffControllerImpl impl;
082:
083: /**
084: * Creates a Diff Controller for supplied left and right sources.
085: *
086: * @param base defines content of the Base Diff pane
087: * @param modified defines content of the Modified (possibly editable) Diff pane
088: * @return DiffController implementation of the DiffController class
089: * @throws java.io.IOException when the reading from input streams fails.
090: */
091: public static DiffController create(StreamSource base,
092: StreamSource modified) throws IOException {
093: DiffControllerProvider provider = Lookup.getDefault().lookup(
094: DiffControllerProvider.class);
095: if (provider != null) {
096: return new DiffController(provider.createDiffController(
097: base, modified));
098: } else {
099: DiffView view = Diff.getDefault()
100: .createDiff(base, modified);
101: return new DiffController(
102: new DiffControllerViewBridge(view));
103: }
104: }
105:
106: private DiffController(DiffControllerImpl impl) {
107: this .impl = impl;
108: }
109:
110: /**
111: * Ensure the requested location in the Diff view is visible on screen. Diff view can be requested to jump to
112: * a given line in either source or to a given Difference.
113: * Diff controller may ignore the request if it does not support this functionality.
114: * This method must be called from AWT thread.
115: *
116: * @param pane defines which pane the location parameter refers to
117: * @param type defines the location parameter, see below
118: * @param location depending on the type parameter this defines either a line number or a Difference index, both 0-based
119: * @throws IllegalArgumentException if location parameter is out of range for the given pane and location type
120: */
121: public void setLocation(DiffPane pane, LocationType type,
122: int location) {
123: impl.setLocation(pane, type, location);
124: }
125:
126: /**
127: * Intializes the Controller and creates visual presenter of the Diff.
128: *
129: * @return JComponent component to be embedded into client UI
130: */
131: public JComponent getJComponent() {
132: return impl.getJComponent();
133: }
134:
135: /**
136: * Gets total number of Differences between sources currently displayed in the Diff view.
137: *
138: * @return total number of Differences in sources, an integer >= 0
139: */
140: public int getDifferenceCount() {
141: return impl.getDifferenceCount();
142: }
143:
144: /**
145: * Gets the current (highlighted) difference in the Diff view.
146: *
147: * @return current difference index or -1 of there is no Current difference
148: */
149: public int getDifferenceIndex() {
150: return impl.getDifferenceIndex();
151: }
152:
153: /**
154: * Adds a property change listener.
155: *
156: * @param listener property change listener
157: */
158: public void addPropertyChangeListener(
159: PropertyChangeListener listener) {
160: impl.addPropertyChangeListener(listener);
161: }
162:
163: /**
164: * Removes a property change listener.
165: *
166: * @param listener property change listener
167: */
168: public void removePropertyChangeListener(
169: PropertyChangeListener listener) {
170: impl.removePropertyChangeListener(listener);
171: }
172:
173: /**
174: * If there is no registered DiffControllerProvider, this provides a bridge from DiffView to DiffControllerProvider.
175: */
176: private static class DiffControllerViewBridge extends
177: DiffControllerImpl {
178:
179: private final DiffView view;
180:
181: DiffControllerViewBridge(DiffView view) {
182: this .view = view;
183: }
184:
185: public void setLocation(DiffController.DiffPane pane,
186: DiffController.LocationType type, int location) {
187: if (type == DiffController.LocationType.DifferenceIndex) {
188: view.setCurrentDifference(location);
189: }
190: }
191:
192: public JComponent getJComponent() {
193: return (JComponent) view.getComponent();
194: }
195:
196: public int getDifferenceCount() {
197: return view.getDifferenceCount();
198: }
199: }
200: }
|