KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > diff > builtin > visualizer > editable > DiffSplitPaneDivider


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.diff.builtin.visualizer.editable;
20
21 import org.openide.util.NbBundle;
22
23 import javax.swing.plaf.basic.BasicSplitPaneDivider JavaDoc;
24 import javax.swing.plaf.basic.BasicSplitPaneUI JavaDoc;
25 import javax.swing.border.Border JavaDoc;
26 import javax.swing.*;
27 import java.awt.*;
28 import java.awt.event.MouseEvent JavaDoc;
29 import java.awt.event.MouseMotionListener JavaDoc;
30 import java.awt.event.MouseListener JavaDoc;
31 import java.util.*;
32 import java.util.List JavaDoc;
33
34 /**
35  * Split pane divider with Diff decorations.
36  *
37  * @author Maros Sandor
38  */

39 class DiffSplitPaneDivider extends BasicSplitPaneDivider JavaDoc implements MouseMotionListener JavaDoc, MouseListener JavaDoc {
40     
41     private final Image insertAllImage = org.openide.util.Utilities.loadImage("org/netbeans/modules/diff/builtin/visualizer/editable/move_all.png"); // NOI18N
42
private final Image insertAllActiveImage = org.openide.util.Utilities.loadImage("org/netbeans/modules/diff/builtin/visualizer/editable/move_all_active.png"); // NOI18N
43
private final int actionIconsHeight;
44     private final int actionIconsWidth;
45     private final Point POINT_ZERO = new Point(0, 0);
46     
47     private final EditableDiffView master;
48
49     private Point lastMousePosition = POINT_ZERO;
50     private HotSpot lastHotSpot = null;
51     private java.util.List JavaDoc<HotSpot> hotspots = new ArrayList<HotSpot>(0);
52     
53     private DiffSplitDivider mydivider;
54
55     DiffSplitPaneDivider(BasicSplitPaneUI JavaDoc splitPaneUI, EditableDiffView master) {
56         super(splitPaneUI);
57         this.master = master;
58
59         actionIconsHeight = insertAllImage.getHeight(this);
60         actionIconsWidth = insertAllImage.getWidth(this);
61         
62         setBorder(null);
63         setLayout(new BorderLayout());
64         mydivider = new DiffSplitDivider();
65         add(mydivider);
66         
67         addMouseListener(this);
68         addMouseMotionListener(this);
69     }
70
71     public void mouseClicked(MouseEvent JavaDoc e) {
72         if (!e.isPopupTrigger()) {
73             HotSpot spot = getHotspotAt(e.getPoint());
74             if (spot != null) {
75                 performAction(); // there is only one hotspot
76
}
77         }
78     }
79
80     public void mouseExited(MouseEvent JavaDoc e) {
81         lastMousePosition = POINT_ZERO;
82         if (lastHotSpot != null) {
83             mydivider.repaint(lastHotSpot.getRect());
84         }
85         lastHotSpot = null;
86     }
87
88     public void mouseEntered(MouseEvent JavaDoc e) {
89         // not interested
90
}
91
92     public void mousePressed(MouseEvent JavaDoc e) {
93         // not interested
94
}
95
96     public void mouseReleased(MouseEvent JavaDoc e) {
97         // not interested
98
}
99     
100     public void mouseDragged(MouseEvent JavaDoc e) {
101         // not interested
102
}
103
104     public void mouseMoved(MouseEvent JavaDoc e) {
105         Point p = e.getPoint();
106         lastMousePosition = p;
107         HotSpot spot = getHotspotAt(p);
108         if (lastHotSpot != spot) {
109             mydivider.repaint(lastHotSpot == null ? spot.getRect() : lastHotSpot.getRect());
110         }
111         lastHotSpot = spot;
112         setCursor(spot != null ? Cursor.getPredefinedCursor(Cursor.HAND_CURSOR) : Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR));
113     }
114
115     private void performAction() {
116         master.rollback(null);
117     }
118     
119     public void setBorder(Border JavaDoc border) {
120         super.setBorder(BorderFactory.createEmptyBorder());
121     }
122
123     DiffSplitDivider getDivider() {
124         return mydivider;
125     }
126
127     private HotSpot getHotspotAt(Point p) {
128         for (HotSpot hotspot : hotspots) {
129           if (hotspot.getRect().contains(p)) {
130               return hotspot;
131           }
132         }
133         return null;
134     }
135     
136     private class DiffSplitDivider extends JComponent {
137     
138         public DiffSplitDivider() {
139             setBackground(UIManager.getColor("SplitPane.background")); // NOI18N
140
setOpaque(true);
141         }
142
143         public String JavaDoc getToolTipText(MouseEvent JavaDoc event) {
144             Point p = event.getPoint();
145             HotSpot spot = getHotspotAt(p);
146             if (spot == null) return null;
147             return NbBundle.getMessage(DiffSplitDivider.class, "TT_DiffPanel_MoveAll"); // NOI18N
148
}
149         
150         protected void paintComponent(Graphics gr) {
151             Graphics2D g = (Graphics2D) gr.create();
152             Rectangle clip = g.getClipBounds();
153         
154             Rectangle rightView = master.getEditorPane2().getScrollPane().getViewport().getViewRect();
155             Rectangle leftView = master.getEditorPane1().getScrollPane().getViewport().getViewRect();
156             
157             int editorsOffset = master.getEditorPane2().getLocation().y + master.getEditorPane2().getInsets().top;
158             
159             int rightOffset = -rightView.y + editorsOffset;
160             int leftOffset = -leftView.y + editorsOffset;
161
162             g.setColor(getBackground());
163             g.fillRect(clip.x, clip.y, clip.width, clip.height);
164
165             String JavaDoc diffInfo = (master.getCurrentDifference() + 1) + "/" + master.getDifferenceCount(); // NOI18N
166
int width = g.getFontMetrics().stringWidth(diffInfo);
167             g.setColor(Color.BLACK);
168             g.drawString(diffInfo, (getWidth() - width) / 2, g.getFontMetrics().getHeight());
169             
170             if (clip.y < editorsOffset) {
171                 g.setClip(clip.x, editorsOffset, clip.width, clip.height);
172             }
173
174             int rightY = getWidth() - 1;
175
176             g.setColor(Color.LIGHT_GRAY);
177             g.drawLine(0, clip.y, 0, clip.height);
178             g.drawLine(rightY, clip.y, rightY, clip.height);
179             
180             DiffViewManager.DecoratedDifference [] decoratedDiffs = master.getManager().getDecorations();
181             int [] xCoords = new int[4];
182             int [] yCoords = new int[4];
183             for (DiffViewManager.DecoratedDifference dd : decoratedDiffs) {
184                 g.setColor(master.getColor(dd.getDiff()));
185                 if (dd.getBottomLeft() == -1) {
186                     xCoords[0] = 0; yCoords[0] = dd.getTopLeft() + leftOffset;
187                     xCoords[1] = rightY; yCoords[1] = dd.getTopRight() + rightOffset;
188                     xCoords[2] = rightY; yCoords[2] = dd.getBottomRight() + rightOffset;
189                     g.fillPolygon(xCoords, yCoords, 3);
190                     g.setColor(master.getColorLines());
191                     g.drawLine(0, dd.getTopLeft() + leftOffset, rightY, dd.getTopRight() + rightOffset);
192                     g.drawLine(0, dd.getTopLeft() + leftOffset, rightY, dd.getBottomRight() + rightOffset);
193                 } else if (dd.getBottomRight() == -1) {
194                     xCoords[0] = 0; yCoords[0] = dd.getTopLeft() + leftOffset;
195                     xCoords[1] = rightY; yCoords[1] = dd.getTopRight() + rightOffset;
196                     xCoords[2] = 0; yCoords[2] = dd.getBottomLeft() + leftOffset;
197                     g.fillPolygon(xCoords, yCoords, 3);
198                     g.setColor(Color.LIGHT_GRAY);
199                     g.drawLine(0, yCoords[0], 0, yCoords[2]);
200                     g.setColor(master.getColorLines());
201                     g.drawLine(0, dd.getTopLeft() + leftOffset, rightY, dd.getTopRight() + rightOffset);
202                     g.drawLine(0, dd.getBottomLeft() + leftOffset, rightY, dd.getTopRight() + rightOffset);
203                 } else {
204                     xCoords[0] = 0; yCoords[0] = dd.getTopLeft() + leftOffset;
205                     xCoords[1] = rightY; yCoords[1] = dd.getTopRight() + rightOffset;
206                     xCoords[2] = rightY; yCoords[2] = dd.getBottomRight() + rightOffset;
207                     xCoords[3] = 0; yCoords[3] = dd.getBottomLeft() + leftOffset;
208                     g.fillPolygon(xCoords, yCoords, 4);
209                     g.setColor(Color.LIGHT_GRAY);
210                     g.drawLine(0, yCoords[0], 0, yCoords[3]);
211                     g.setColor(master.getColorLines());
212                     g.drawLine(0, dd.getTopLeft() + leftOffset, rightY, dd.getTopRight() + rightOffset);
213                     g.drawLine(0, dd.getBottomLeft() + leftOffset, rightY, dd.getBottomRight() + rightOffset);
214                 }
215             }
216             
217             if (master.isActionsEnabled()) {
218                 List JavaDoc<HotSpot> newActionIcons = new ArrayList<HotSpot>();
219                 Rectangle hotSpot = new Rectangle((getWidth() - actionIconsWidth) /2, editorsOffset, actionIconsWidth, actionIconsHeight);
220                 if (hotSpot.contains(lastMousePosition)) {
221                     g.drawImage(insertAllActiveImage, hotSpot.x, hotSpot.y, this);
222                 } else {
223                     g.drawImage(insertAllImage, hotSpot.x, hotSpot.y, this);
224                 }
225                 newActionIcons.add(new HotSpot(hotSpot, null));
226                 hotspots = newActionIcons;
227             }
228         }
229     }
230 }
231
Popular Tags