1 19 package org.netbeans.modules.diff.builtin.visualizer.editable; 20 21 import org.openide.util.NbBundle; 22 23 import javax.swing.plaf.basic.BasicSplitPaneDivider ; 24 import javax.swing.plaf.basic.BasicSplitPaneUI ; 25 import javax.swing.border.Border ; 26 import javax.swing.*; 27 import java.awt.*; 28 import java.awt.event.MouseEvent ; 29 import java.awt.event.MouseMotionListener ; 30 import java.awt.event.MouseListener ; 31 import java.util.*; 32 import java.util.List ; 33 34 39 class DiffSplitPaneDivider extends BasicSplitPaneDivider implements MouseMotionListener , MouseListener { 40 41 private final Image insertAllImage = org.openide.util.Utilities.loadImage("org/netbeans/modules/diff/builtin/visualizer/editable/move_all.png"); private final Image insertAllActiveImage = org.openide.util.Utilities.loadImage("org/netbeans/modules/diff/builtin/visualizer/editable/move_all_active.png"); 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 <HotSpot> hotspots = new ArrayList<HotSpot>(0); 52 53 private DiffSplitDivider mydivider; 54 55 DiffSplitPaneDivider(BasicSplitPaneUI 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 e) { 72 if (!e.isPopupTrigger()) { 73 HotSpot spot = getHotspotAt(e.getPoint()); 74 if (spot != null) { 75 performAction(); } 77 } 78 } 79 80 public void mouseExited(MouseEvent 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 e) { 89 } 91 92 public void mousePressed(MouseEvent e) { 93 } 95 96 public void mouseReleased(MouseEvent e) { 97 } 99 100 public void mouseDragged(MouseEvent e) { 101 } 103 104 public void mouseMoved(MouseEvent 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 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")); setOpaque(true); 141 } 142 143 public String getToolTipText(MouseEvent 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"); } 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 diffInfo = (master.getCurrentDifference() + 1) + "/" + master.getDifferenceCount(); 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 <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 |