Browse Source

Refactored some code.

Fixed possible issue of NullPointerException during DnD.
pull/15/head
weisj 5 years ago
parent
commit
9574862404
  1. 26
      src/main/java/com/weis/darklaf/components/tabframe/JTabFrame.java
  2. 1
      src/main/java/com/weis/darklaf/ui/tabbedpane/TabbedPaneTransferHandler.java
  3. 36
      src/main/java/com/weis/darklaf/ui/tabframe/DarkTabFrameUI.java
  4. 3
      src/main/java/com/weis/darklaf/ui/tabframe/TabFrameTransferHandler.java
  5. 46
      src/main/java/com/weis/darklaf/util/Pair.java

26
src/main/java/com/weis/darklaf/components/tabframe/JTabFrame.java

@ -897,7 +897,6 @@ public class JTabFrame extends JComponent {
public static class TabFramePosition {
private Alignment a;
private int index;
private Point point;
@Contract(pure = true)
public TabFramePosition(final Alignment a, final int index) {
@ -905,29 +904,24 @@ public class JTabFrame extends JComponent {
this.index = index;
}
@Contract(pure = true)
public TabFramePosition(final Alignment a, final int index, final Point p) {
this.a = a;
this.index = index;
this.point = p;
}
/**
* Set the alignment.
*
* @param a the alignment.
*/
public void setAlignment(final Alignment a) {
this.a = a;
}
/**
* Set the index.
*
* @param index the index.
*/
public void setIndex(final int index) {
this.index = index;
}
public Point getPoint() {
return point;
}
public void setPoint(final Point point) {
this.point = point;
}
/**
* The alignment position.
* This specifies at what location the tab is placed.

1
src/main/java/com/weis/darklaf/ui/tabbedpane/TabbedPaneTransferHandler.java

@ -333,6 +333,7 @@ public class TabbedPaneTransferHandler extends TransferHandler implements DropTa
@Override
public DataFlavor[] getTransferDataFlavors() {
if (tabFlavor == null) return new DataFlavor[0];
return new DataFlavor[]{tabFlavor};
}

36
src/main/java/com/weis/darklaf/ui/tabframe/DarkTabFrameUI.java

@ -32,6 +32,7 @@ import com.weis.darklaf.components.tabframe.TabFrameTab;
import com.weis.darklaf.components.tabframe.TabFrameUI;
import com.weis.darklaf.components.uiresource.JPanelUIResource;
import com.weis.darklaf.util.DarkUIUtil;
import com.weis.darklaf.util.Pair;
import org.jdesktop.jxlayer.JXLayer;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
@ -230,6 +231,11 @@ public class DarkTabFrameUI extends TabFrameUI implements AWTEventListener {
@Override
public JTabFrame.TabFramePosition getTabIndexAt(final JTabFrame tabFrame, @NotNull final Point p) {
return getTabIndexAtImpl(tabFrame, p).getFirst();
}
protected Pair<JTabFrame.TabFramePosition, Point> getTabIndexAtImpl(final JTabFrame tabFrame,
@NotNull final Point p) {
Component tabComp = null;
Alignment a = null;
Point pos = null;
@ -287,33 +293,39 @@ public class DarkTabFrameUI extends TabFrameUI implements AWTEventListener {
if (tabComp == null) {
var tab = maybeRestoreTabContainer(tabFrame, p);
if (tab.getAlignment() != null) {
return tab;
return new Pair<>(tab, pos);
}
} else {
layout.setDraggedOver(false);
}
if (tabComp == null) {
return new JTabFrame.TabFramePosition(null, -1);
return new Pair<>(new JTabFrame.TabFramePosition(null, -1), pos);
}
var tabs = tabFrame.tabsForAlignment(a);
for (var tab : tabs) {
var rect = getTabRect(tab, a, tabComp, true);
if (rect.contains(pos)) {
return new JTabFrame.TabFramePosition(a, tab.getIndex(), pos);
return new Pair<>(new JTabFrame.TabFramePosition(a, tab.getIndex()), pos);
}
}
return new JTabFrame.TabFramePosition(a, -1, pos);
return new Pair<>(new JTabFrame.TabFramePosition(a, -1), pos);
}
@Override
public JTabFrame.TabFramePosition getNearestTabIndexAt(final JTabFrame tabFrame, final Point pos) {
var tab = getTabIndexAt(tabFrame, pos);
return getNearestTabIndexAtImpl(tabFrame, pos).getFirst();
}
protected Pair<JTabFrame.TabFramePosition, Point> getNearestTabIndexAtImpl(final JTabFrame tabFrame,
final Point pos) {
var res = getTabIndexAtImpl(tabFrame, pos);
var tab = res.getFirst();
if (tab.getAlignment() != null && tab.getIndex() == -1) {
var p = tab.getPoint();
var p = res.getSecond();
var a = tab.getAlignment();
if (tabFrame.getTabCountAt(a) == 0) {
tab.setIndex(-1);
return tab;
return res;
}
int w = a == destAlign && destIndex == -1 ? dropSize.width : 0;
var comp = getTabContainer(a);
@ -344,7 +356,7 @@ public class DarkTabFrameUI extends TabFrameUI implements AWTEventListener {
break;
}
}
return tab;
return res;
}
public void setDropSize(final int width, final int height) {
@ -388,13 +400,15 @@ public class DarkTabFrameUI extends TabFrameUI implements AWTEventListener {
}
public JTabFrame.TabFramePosition getDropPosition(final JTabFrame tabFrame, final Point p) {
var tab = getNearestTabIndexAt(tabFrame, p);
var res = getNearestTabIndexAtImpl(tabFrame, p);
var tab = res.getFirst();
if (tab.getAlignment() != null) {
var a = tab.getAlignment();
int index = tab.getIndex();
if (index >= 0) {
var rect = getTabRect(tabFrame.getTabComponentAt(a, index), a, tabFrame.getTabContainer(a), false);
var pos = tab.getPoint();
var rect = getTabRect(tabFrame.getTabComponentAt(a, index), a,
tabFrame.getTabContainer(a), false);
var pos = res.getSecond();
if (isForward(a)) {
if (pos.x <= rect.x + rect.width / 2 && pos.x >= rect.x) {
tab.setIndex(tab.getIndex() - 1);

3
src/main/java/com/weis/darklaf/ui/tabframe/TabFrameTransferHandler.java

@ -61,7 +61,7 @@ import java.awt.event.MouseEvent;
public class TabFrameTransferHandler extends TransferHandler implements DropTargetListener, SwingConstants {
private static final String MIME_TYPE = DataFlavor.javaJVMLocalObjectMimeType
+ ";class=com.weis.darklaf.components.tabframe.TabFrame";
+ ";class=com.weis.darklaf.components.tabframe.JTabFrame";
private static TabbedPaneDragGestureRecognizer recognizer = null;
private final Timer timer;
private final Timer startTimer;
@ -381,6 +381,7 @@ public class TabFrameTransferHandler extends TransferHandler implements DropTarg
@Override
public DataFlavor[] getTransferDataFlavors() {
if (tabFlavor == null) return new DataFlavor[0];
return new DataFlavor[]{tabFlavor};
}

46
src/main/java/com/weis/darklaf/util/Pair.java

@ -0,0 +1,46 @@
/*
* MIT License
*
* Copyright (c) 2019 Jannis Weis
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.weis.darklaf.util;
import org.jetbrains.annotations.Contract;
public class Pair<T, H> {
private T first;
private H second;
@Contract(pure = true)
public Pair(final T first, final H second) {
this.first = first;
this.second = second;
}
public H getSecond() {
return second;
}
public T getFirst() {
return first;
}
}
Loading…
Cancel
Save