1 | package com.jsql.view.swing.tab.dnd; | |
2 | ||
3 | import com.jsql.util.LogLevelUtil; | |
4 | import org.apache.logging.log4j.LogManager; | |
5 | import org.apache.logging.log4j.Logger; | |
6 | ||
7 | import javax.swing.*; | |
8 | import java.awt.*; | |
9 | import java.awt.datatransfer.DataFlavor; | |
10 | import java.awt.datatransfer.Transferable; | |
11 | import java.awt.datatransfer.UnsupportedFlavorException; | |
12 | import java.awt.dnd.DragSource; | |
13 | import java.awt.image.BufferedImage; | |
14 | import java.io.IOException; | |
15 | import java.util.Objects; | |
16 | import java.util.Optional; | |
17 | ||
18 | public class TabTransferHandler extends TransferHandler { | |
19 | | |
20 | /** | |
21 | * Log4j logger sent to view. | |
22 | */ | |
23 | private static final Logger LOGGER = LogManager.getRootLogger(); | |
24 | | |
25 | protected final DataFlavor localObjectFlavor; | |
26 | | |
27 | protected DnDTabbedPane source; | |
28 | ||
29 | public TabTransferHandler() { | |
30 | | |
31 | super(); | |
32 | | |
33 | this.localObjectFlavor = new DataFlavor(DnDTabData.class, "DnDTabData"); | |
34 | } | |
35 | | |
36 | @Override | |
37 | protected Transferable createTransferable(JComponent c) { | |
38 | | |
39 |
1
1. createTransferable : negated conditional → NO_COVERAGE |
if (c instanceof DnDTabbedPane) { |
40 | this.source = (DnDTabbedPane) c; | |
41 | } | |
42 | ||
43 |
1
1. createTransferable : replaced return value with null for com/jsql/view/swing/tab/dnd/TabTransferHandler::createTransferable → NO_COVERAGE |
return new Transferable() { |
44 | | |
45 | @Override | |
46 | public DataFlavor[] getTransferDataFlavors() { | |
47 |
1
1. getTransferDataFlavors : replaced return value with null for com/jsql/view/swing/tab/dnd/TabTransferHandler$1::getTransferDataFlavors → NO_COVERAGE |
return new DataFlavor[] { TabTransferHandler.this.localObjectFlavor }; |
48 | } | |
49 | | |
50 | @Override | |
51 | public boolean isDataFlavorSupported(DataFlavor flavor) { | |
52 |
2
1. isDataFlavorSupported : replaced boolean return with false for com/jsql/view/swing/tab/dnd/TabTransferHandler$1::isDataFlavorSupported → NO_COVERAGE 2. isDataFlavorSupported : replaced boolean return with true for com/jsql/view/swing/tab/dnd/TabTransferHandler$1::isDataFlavorSupported → NO_COVERAGE |
return Objects.equals(TabTransferHandler.this.localObjectFlavor, flavor); |
53 | } | |
54 | | |
55 | @Override | |
56 | public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException { | |
57 |
1
1. getTransferData : negated conditional → NO_COVERAGE |
if (this.isDataFlavorSupported(flavor)) { |
58 |
1
1. getTransferData : replaced return value with null for com/jsql/view/swing/tab/dnd/TabTransferHandler$1::getTransferData → NO_COVERAGE |
return new DnDTabData(TabTransferHandler.this.source); |
59 | } else { | |
60 | throw new UnsupportedFlavorException(flavor); | |
61 | } | |
62 | } | |
63 | }; | |
64 | } | |
65 | | |
66 | @Override | |
67 | public boolean canImport(TransferHandler.TransferSupport support) { | |
68 | | |
69 |
2
1. canImport : negated conditional → NO_COVERAGE 2. canImport : negated conditional → NO_COVERAGE |
if (!support.isDrop() || !support.isDataFlavorSupported(this.localObjectFlavor)) { |
70 |
1
1. canImport : replaced boolean return with true for com/jsql/view/swing/tab/dnd/TabTransferHandler::canImport → NO_COVERAGE |
return false; |
71 | } | |
72 | | |
73 |
1
1. canImport : removed call to javax/swing/TransferHandler$TransferSupport::setDropAction → NO_COVERAGE |
support.setDropAction(TransferHandler.MOVE); |
74 | var tdl = support.getDropLocation(); | |
75 | var pt = tdl.getDropPoint(); | |
76 | | |
77 | DnDTabbedPane target = (DnDTabbedPane) support.getComponent(); | |
78 | | |
79 |
1
1. canImport : removed call to com/jsql/view/swing/tab/dnd/DnDTabbedPane::autoScrollTest → NO_COVERAGE |
target.autoScrollTest(pt); |
80 | DnDTabbedPane.DnDDropLocation dl = target.dropLocationForPointDnD(pt); | |
81 | int idx = dl.getIndex(); | |
82 | ||
83 | var isDroppable = false; | |
84 |
3
1. canImport : negated conditional → NO_COVERAGE 2. canImport : changed conditional boundary → NO_COVERAGE 3. canImport : negated conditional → NO_COVERAGE |
boolean isAreaContains = target.getTabAreaBounds().contains(pt) && idx >= 0; |
85 | | |
86 |
1
1. canImport : negated conditional → NO_COVERAGE |
if (target.equals(this.source)) { |
87 |
4
1. canImport : Replaced integer addition with subtraction → NO_COVERAGE 2. canImport : negated conditional → NO_COVERAGE 3. canImport : negated conditional → NO_COVERAGE 4. canImport : negated conditional → NO_COVERAGE |
isDroppable = isAreaContains && idx != target.dragTabIndex && idx != target.dragTabIndex + 1; |
88 | } else { | |
89 |
4
1. canImport : negated conditional → NO_COVERAGE 2. lambda$canImport$0 : replaced Boolean return with True for com/jsql/view/swing/tab/dnd/TabTransferHandler::lambda$canImport$0 → NO_COVERAGE 3. canImport : negated conditional → NO_COVERAGE 4. lambda$canImport$0 : negated conditional → NO_COVERAGE |
isDroppable = Optional.ofNullable(this.source).map(c -> !c.isAncestorOf(target)).orElse(false) && isAreaContains; |
90 | } | |
91 | ||
92 | // [JDK-6700748] Cursor flickering during D&D when using CellRendererPane with validation - Java Bug System | |
93 | // https://bugs.openjdk.java.net/browse/JDK-6700748 | |
94 |
1
1. canImport : negated conditional → NO_COVERAGE |
Cursor cursor = isDroppable ? DragSource.DefaultMoveDrop : DragSource.DefaultMoveNoDrop; |
95 | Component glassPane = target.getRootPane().getGlassPane(); | |
96 |
1
1. canImport : removed call to java/awt/Component::setCursor → NO_COVERAGE |
glassPane.setCursor(cursor); |
97 |
1
1. canImport : removed call to com/jsql/view/swing/tab/dnd/DnDTabbedPane::setCursor → NO_COVERAGE |
target.setCursor(cursor); |
98 | ||
99 |
1
1. canImport : removed call to javax/swing/TransferHandler$TransferSupport::setShowDropLocation → NO_COVERAGE |
support.setShowDropLocation(isDroppable); |
100 |
1
1. canImport : removed call to com/jsql/view/swing/tab/dnd/DnDTabbedPane$DnDDropLocation::setDroppable → NO_COVERAGE |
dl.setDroppable(isDroppable); |
101 |
1
1. canImport : removed call to com/jsql/view/swing/tab/dnd/DnDTabbedPane::setDropLocation → NO_COVERAGE |
target.setDropLocation(dl, isDroppable); |
102 |
2
1. canImport : replaced boolean return with true for com/jsql/view/swing/tab/dnd/TabTransferHandler::canImport → NO_COVERAGE 2. canImport : replaced boolean return with false for com/jsql/view/swing/tab/dnd/TabTransferHandler::canImport → NO_COVERAGE |
return isDroppable; |
103 | } | |
104 | | |
105 | private BufferedImage makeDragTabImage(DnDTabbedPane tabbedPane) { | |
106 | | |
107 | Rectangle rect = tabbedPane.getBoundsAt(tabbedPane.dragTabIndex); | |
108 | var image = new BufferedImage(tabbedPane.getWidth(), tabbedPane.getHeight(), BufferedImage.TYPE_INT_ARGB); | |
109 | Graphics g2 = image.createGraphics(); | |
110 |
1
1. makeDragTabImage : removed call to com/jsql/view/swing/tab/dnd/DnDTabbedPane::paint → NO_COVERAGE |
tabbedPane.paint(g2); |
111 |
1
1. makeDragTabImage : removed call to java/awt/Graphics::dispose → NO_COVERAGE |
g2.dispose(); |
112 | | |
113 |
2
1. makeDragTabImage : negated conditional → NO_COVERAGE 2. makeDragTabImage : changed conditional boundary → NO_COVERAGE |
if (rect.x < 0) { |
114 |
2
1. makeDragTabImage : removed negation → NO_COVERAGE 2. makeDragTabImage : removed call to java/awt/Rectangle::translate → NO_COVERAGE |
rect.translate(-rect.x, 0); |
115 | } | |
116 | | |
117 |
2
1. makeDragTabImage : changed conditional boundary → NO_COVERAGE 2. makeDragTabImage : negated conditional → NO_COVERAGE |
if (rect.y < 0) { |
118 |
2
1. makeDragTabImage : removed call to java/awt/Rectangle::translate → NO_COVERAGE 2. makeDragTabImage : removed negation → NO_COVERAGE |
rect.translate(0, -rect.y); |
119 | } | |
120 | | |
121 |
3
1. makeDragTabImage : Replaced integer addition with subtraction → NO_COVERAGE 2. makeDragTabImage : negated conditional → NO_COVERAGE 3. makeDragTabImage : changed conditional boundary → NO_COVERAGE |
if (rect.x + rect.width > image.getWidth()) { |
122 |
1
1. makeDragTabImage : Replaced integer subtraction with addition → NO_COVERAGE |
rect.width = image.getWidth() - rect.x; |
123 | } | |
124 | | |
125 |
3
1. makeDragTabImage : negated conditional → NO_COVERAGE 2. makeDragTabImage : changed conditional boundary → NO_COVERAGE 3. makeDragTabImage : Replaced integer addition with subtraction → NO_COVERAGE |
if (rect.y + rect.height > image.getHeight()) { |
126 |
1
1. makeDragTabImage : Replaced integer subtraction with addition → NO_COVERAGE |
rect.height = image.getHeight() - rect.y; |
127 | } | |
128 | | |
129 |
1
1. makeDragTabImage : replaced return value with null for com/jsql/view/swing/tab/dnd/TabTransferHandler::makeDragTabImage → NO_COVERAGE |
return image.getSubimage(rect.x, rect.y, rect.width, rect.height); |
130 | } | |
131 | | |
132 | @Override | |
133 | public int getSourceActions(JComponent c) { | |
134 | | |
135 |
1
1. getSourceActions : negated conditional → NO_COVERAGE |
if (c instanceof DnDTabbedPane) { |
136 | | |
137 | DnDTabbedPane src = (DnDTabbedPane) c; | |
138 |
1
1. getSourceActions : removed call to javax/swing/JRootPane::setGlassPane → NO_COVERAGE |
c.getRootPane().setGlassPane(new GhostGlassPane(src)); |
139 | | |
140 |
2
1. getSourceActions : changed conditional boundary → NO_COVERAGE 2. getSourceActions : negated conditional → NO_COVERAGE |
if (src.dragTabIndex < 0) { |
141 | return TransferHandler.NONE; | |
142 | } | |
143 | | |
144 |
1
1. getSourceActions : removed call to com/jsql/view/swing/tab/dnd/TabTransferHandler::setDragImage → NO_COVERAGE |
this.setDragImage(this.makeDragTabImage(src)); |
145 |
1
1. getSourceActions : removed call to java/awt/Component::setVisible → NO_COVERAGE |
c.getRootPane().getGlassPane().setVisible(true); |
146 | | |
147 |
1
1. getSourceActions : replaced int return with 0 for com/jsql/view/swing/tab/dnd/TabTransferHandler::getSourceActions → NO_COVERAGE |
return TransferHandler.MOVE; |
148 | } | |
149 | | |
150 | return TransferHandler.NONE; | |
151 | } | |
152 | | |
153 | @Override | |
154 | public boolean importData(TransferHandler.TransferSupport support) { | |
155 | | |
156 |
1
1. importData : negated conditional → NO_COVERAGE |
if (!this.canImport(support)) { |
157 |
1
1. importData : replaced boolean return with true for com/jsql/view/swing/tab/dnd/TabTransferHandler::importData → NO_COVERAGE |
return false; |
158 | } | |
159 | ||
160 | DnDTabbedPane target = (DnDTabbedPane) support.getComponent(); | |
161 | DnDTabbedPane.DnDDropLocation dl = target.getDropLocation(); | |
162 | | |
163 | try { | |
164 | DnDTabData data = (DnDTabData) support.getTransferable().getTransferData(this.localObjectFlavor); | |
165 | DnDTabbedPane src = data.tabbedPane; | |
166 | int index = dl.getIndex(); | |
167 | | |
168 |
1
1. importData : negated conditional → NO_COVERAGE |
if (target.equals(src)) { |
169 |
1
1. importData : removed call to com/jsql/view/swing/tab/dnd/DnDTabbedPane::convertTab → NO_COVERAGE |
src.convertTab(src.dragTabIndex, index); |
170 | } else { | |
171 |
1
1. importData : removed call to com/jsql/view/swing/tab/dnd/DnDTabbedPane::exportTab → NO_COVERAGE |
src.exportTab(src.dragTabIndex, target, index); |
172 | } | |
173 | | |
174 |
1
1. importData : replaced boolean return with false for com/jsql/view/swing/tab/dnd/TabTransferHandler::importData → NO_COVERAGE |
return true; |
175 | | |
176 | } catch (UnsupportedFlavorException | IOException e) { | |
177 | LOGGER.log(LogLevelUtil.CONSOLE_JAVA, e, e); | |
178 | } | |
179 | | |
180 |
1
1. importData : replaced boolean return with true for com/jsql/view/swing/tab/dnd/TabTransferHandler::importData → NO_COVERAGE |
return false; |
181 | } | |
182 | | |
183 | @Override | |
184 | protected void exportDone(JComponent c, Transferable data, int action) { | |
185 | | |
186 | DnDTabbedPane src = (DnDTabbedPane) c; | |
187 | | |
188 |
1
1. exportDone : removed call to java/awt/Component::setVisible → NO_COVERAGE |
src.getRootPane().getGlassPane().setVisible(false); |
189 |
1
1. exportDone : removed call to com/jsql/view/swing/tab/dnd/DnDTabbedPane::setDropLocation → NO_COVERAGE |
src.setDropLocation(null, false); |
190 |
1
1. exportDone : removed call to com/jsql/view/swing/tab/dnd/DnDTabbedPane::repaint → NO_COVERAGE |
src.repaint(); |
191 |
1
1. exportDone : removed call to com/jsql/view/swing/tab/dnd/DnDTabbedPane::setCursor → NO_COVERAGE |
src.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); |
192 | } | |
193 | } | |
Mutations | ||
39 |
1.1 |
|
43 |
1.1 |
|
47 |
1.1 |
|
52 |
1.1 2.2 |
|
57 |
1.1 |
|
58 |
1.1 |
|
69 |
1.1 2.2 |
|
70 |
1.1 |
|
73 |
1.1 |
|
79 |
1.1 |
|
84 |
1.1 2.2 3.3 |
|
86 |
1.1 |
|
87 |
1.1 2.2 3.3 4.4 |
|
89 |
1.1 2.2 3.3 4.4 |
|
94 |
1.1 |
|
96 |
1.1 |
|
97 |
1.1 |
|
99 |
1.1 |
|
100 |
1.1 |
|
101 |
1.1 |
|
102 |
1.1 2.2 |
|
110 |
1.1 |
|
111 |
1.1 |
|
113 |
1.1 2.2 |
|
114 |
1.1 2.2 |
|
117 |
1.1 2.2 |
|
118 |
1.1 2.2 |
|
121 |
1.1 2.2 3.3 |
|
122 |
1.1 |
|
125 |
1.1 2.2 3.3 |
|
126 |
1.1 |
|
129 |
1.1 |
|
135 |
1.1 |
|
138 |
1.1 |
|
140 |
1.1 2.2 |
|
144 |
1.1 |
|
145 |
1.1 |
|
147 |
1.1 |
|
156 |
1.1 |
|
157 |
1.1 |
|
168 |
1.1 |
|
169 |
1.1 |
|
171 |
1.1 |
|
174 |
1.1 |
|
180 |
1.1 |
|
188 |
1.1 |
|
189 |
1.1 |
|
190 |
1.1 |
|
191 |
1.1 |