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 { public static class TabFramePosition {
private Alignment a; private Alignment a;
private int index; private int index;
private Point point;
@Contract(pure = true) @Contract(pure = true)
public TabFramePosition(final Alignment a, final int index) { public TabFramePosition(final Alignment a, final int index) {
@ -905,29 +904,24 @@ public class JTabFrame extends JComponent {
this.index = index; this.index = index;
} }
@Contract(pure = true) /**
public TabFramePosition(final Alignment a, final int index, final Point p) { * Set the alignment.
this.a = a; *
this.index = index; * @param a the alignment.
this.point = p; */
}
public void setAlignment(final Alignment a) { public void setAlignment(final Alignment a) {
this.a = a; this.a = a;
} }
/**
* Set the index.
*
* @param index the index.
*/
public void setIndex(final int index) { public void setIndex(final int index) {
this.index = index; this.index = index;
} }
public Point getPoint() {
return point;
}
public void setPoint(final Point point) {
this.point = point;
}
/** /**
* The alignment position. * The alignment position.
* This specifies at what location the tab is placed. * 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 @Override
public DataFlavor[] getTransferDataFlavors() { public DataFlavor[] getTransferDataFlavors() {
if (tabFlavor == null) return new DataFlavor[0];
return new DataFlavor[]{tabFlavor}; 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.tabframe.TabFrameUI;
import com.weis.darklaf.components.uiresource.JPanelUIResource; import com.weis.darklaf.components.uiresource.JPanelUIResource;
import com.weis.darklaf.util.DarkUIUtil; import com.weis.darklaf.util.DarkUIUtil;
import com.weis.darklaf.util.Pair;
import org.jdesktop.jxlayer.JXLayer; import org.jdesktop.jxlayer.JXLayer;
import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -230,6 +231,11 @@ public class DarkTabFrameUI extends TabFrameUI implements AWTEventListener {
@Override @Override
public JTabFrame.TabFramePosition getTabIndexAt(final JTabFrame tabFrame, @NotNull final Point p) { 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; Component tabComp = null;
Alignment a = null; Alignment a = null;
Point pos = null; Point pos = null;
@ -287,33 +293,39 @@ public class DarkTabFrameUI extends TabFrameUI implements AWTEventListener {
if (tabComp == null) { if (tabComp == null) {
var tab = maybeRestoreTabContainer(tabFrame, p); var tab = maybeRestoreTabContainer(tabFrame, p);
if (tab.getAlignment() != null) { if (tab.getAlignment() != null) {
return tab; return new Pair<>(tab, pos);
} }
} else { } else {
layout.setDraggedOver(false); layout.setDraggedOver(false);
} }
if (tabComp == null) { if (tabComp == null) {
return new JTabFrame.TabFramePosition(null, -1); return new Pair<>(new JTabFrame.TabFramePosition(null, -1), pos);
} }
var tabs = tabFrame.tabsForAlignment(a); var tabs = tabFrame.tabsForAlignment(a);
for (var tab : tabs) { for (var tab : tabs) {
var rect = getTabRect(tab, a, tabComp, true); var rect = getTabRect(tab, a, tabComp, true);
if (rect.contains(pos)) { 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 @Override
public JTabFrame.TabFramePosition getNearestTabIndexAt(final JTabFrame tabFrame, final Point pos) { 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) { if (tab.getAlignment() != null && tab.getIndex() == -1) {
var p = tab.getPoint(); var p = res.getSecond();
var a = tab.getAlignment(); var a = tab.getAlignment();
if (tabFrame.getTabCountAt(a) == 0) { if (tabFrame.getTabCountAt(a) == 0) {
tab.setIndex(-1); tab.setIndex(-1);
return tab; return res;
} }
int w = a == destAlign && destIndex == -1 ? dropSize.width : 0; int w = a == destAlign && destIndex == -1 ? dropSize.width : 0;
var comp = getTabContainer(a); var comp = getTabContainer(a);
@ -344,7 +356,7 @@ public class DarkTabFrameUI extends TabFrameUI implements AWTEventListener {
break; break;
} }
} }
return tab; return res;
} }
public void setDropSize(final int width, final int height) { 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) { 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) { if (tab.getAlignment() != null) {
var a = tab.getAlignment(); var a = tab.getAlignment();
int index = tab.getIndex(); int index = tab.getIndex();
if (index >= 0) { if (index >= 0) {
var rect = getTabRect(tabFrame.getTabComponentAt(a, index), a, tabFrame.getTabContainer(a), false); var rect = getTabRect(tabFrame.getTabComponentAt(a, index), a,
var pos = tab.getPoint(); tabFrame.getTabContainer(a), false);
var pos = res.getSecond();
if (isForward(a)) { if (isForward(a)) {
if (pos.x <= rect.x + rect.width / 2 && pos.x >= rect.x) { if (pos.x <= rect.x + rect.width / 2 && pos.x >= rect.x) {
tab.setIndex(tab.getIndex() - 1); 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 { public class TabFrameTransferHandler extends TransferHandler implements DropTargetListener, SwingConstants {
private static final String MIME_TYPE = DataFlavor.javaJVMLocalObjectMimeType 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 static TabbedPaneDragGestureRecognizer recognizer = null;
private final Timer timer; private final Timer timer;
private final Timer startTimer; private final Timer startTimer;
@ -381,6 +381,7 @@ public class TabFrameTransferHandler extends TransferHandler implements DropTarg
@Override @Override
public DataFlavor[] getTransferDataFlavors() { public DataFlavor[] getTransferDataFlavors() {
if (tabFlavor == null) return new DataFlavor[0];
return new DataFlavor[]{tabFlavor}; 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