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.modules.diff.builtin.visualizer.editable;
042:
043: import org.netbeans.spi.diff.DiffVisualizer;
044: import org.netbeans.api.diff.Difference;
045: import org.netbeans.api.diff.DiffView;
046: import org.netbeans.api.diff.StreamSource;
047: import org.netbeans.modules.diff.builtin.DiffPresenter;
048: import org.openide.util.NbBundle;
049: import org.openide.util.Utilities;
050:
051: import javax.swing.*;
052: import java.awt.Component;
053: import java.awt.Insets;
054: import java.awt.event.ActionEvent;
055: import java.io.Reader;
056: import java.io.IOException;
057: import java.beans.PropertyChangeListener;
058: import java.beans.PropertyChangeEvent;
059:
060: /**
061: * Registration of the editable visualizer.
062: *
063: * @author Maros Sandor
064: */
065: public class EditableDiffVisualizer extends DiffVisualizer {
066:
067: /**
068: * Get the display name of this diff visualizer, CALLED VIA REFLECTION.
069: */
070: public String getDisplayName() {
071: return NbBundle.getMessage(EditableDiffVisualizer.class,
072: "CTL_EditableDiffVisualizer_Name"); // NOI18N
073: }
074:
075: /**
076: * Get a short description of this diff visualizer, CALLED VIA REFLECTION.
077: */
078: public String getShortDescription() {
079: return NbBundle.getMessage(EditableDiffVisualizer.class,
080: "CTL_EditableDiffVisualizer_Desc"); // NOI18N
081: }
082:
083: public Component createView(Difference[] diffs, String name1,
084: String title1, Reader r1, String name2, String title2,
085: Reader r2, String MIMEType) throws IOException {
086: return createDiff(diffs,
087: StreamSource.createSource(name1, title1, MIMEType, r1),
088: StreamSource.createSource(name2, title2, MIMEType, r2))
089: .getComponent();
090: }
091:
092: public DiffView createDiff(Difference[] diffs, StreamSource s1,
093: StreamSource s2) throws IOException {
094: EDVManager manager = new EDVManager(s1, s2);
095: manager.init();
096: return manager.getView();
097: }
098:
099: private static class EDVManager implements PropertyChangeListener {
100:
101: private final StreamSource s1;
102: private final StreamSource s2;
103:
104: private EditableDiffView view;
105:
106: private Action nextAction;
107: private Action prevAction;
108:
109: public EDVManager(StreamSource s1, StreamSource s2) {
110: this .s1 = s1;
111: this .s2 = s2;
112: }
113:
114: public EditableDiffView getView() {
115: return view;
116: }
117:
118: public void init() throws IOException {
119: view = new EditableDiffView(s1, s2);
120: view.addPropertyChangeListener(this );
121: JComponent component = (JComponent) view.getComponent();
122:
123: JToolBar toolbar = new JToolBar();
124: toolbar.setBorder(BorderFactory.createEmptyBorder());
125: toolbar.setRollover(true);
126:
127: nextAction = new AbstractAction() {
128: public void actionPerformed(ActionEvent e) {
129: onNext();
130: }
131: };
132: nextAction
133: .putValue(
134: Action.SMALL_ICON,
135: new ImageIcon(
136: Utilities
137: .loadImage("org/netbeans/modules/diff/builtin/visualizer/editable/diff-next.png"))); // NOI18N
138: JButton nextButton = new JButton(nextAction);
139: nextButton.setMargin(new Insets(2, 2, 2, 2));
140: toolbar.add(nextButton);
141:
142: prevAction = new AbstractAction() {
143: public void actionPerformed(ActionEvent e) {
144: onPrev();
145: }
146: };
147: prevAction
148: .putValue(
149: Action.SMALL_ICON,
150: new ImageIcon(
151: Utilities
152: .loadImage("org/netbeans/modules/diff/builtin/visualizer/editable/diff-prev.png"))); // NOI18N
153: JButton prevButton = new JButton(prevAction);
154: prevButton.setMargin(new Insets(2, 2, 2, 2));
155: toolbar.add(prevButton);
156:
157: component.putClientProperty(DiffPresenter.PROP_TOOLBAR,
158: toolbar);
159: component.getActionMap().put("jumpNext", nextAction); // NOI18N
160: component.getActionMap().put("jumpPrev", prevAction); // NOI18N
161:
162: refreshComponents();
163: }
164:
165: private void onPrev() {
166: int idx = view.getCurrentDifference();
167: if (idx > 0) {
168: view.setCurrentDifference(idx - 1);
169: }
170: }
171:
172: private void onNext() {
173: int idx = view.getCurrentDifference();
174: if (idx < view.getDifferenceCount() - 1) {
175: view.setCurrentDifference(idx + 1);
176: }
177: }
178:
179: public void propertyChange(PropertyChangeEvent evt) {
180: refreshComponents();
181: }
182:
183: private void refreshComponents() {
184: nextAction.setEnabled(view.getCurrentDifference() < view
185: .getDifferenceCount() - 1);
186: prevAction.setEnabled(view.getCurrentDifference() > 0);
187: }
188: }
189: }
|