Skip to content

Commit

Permalink
refactor: annotate deprecations, avoid access to internal class, and …
Browse files Browse the repository at this point in the history
…avoid redundant casts (#6)

* refactor: avoid access to internal class from outside

* refactor: avoid redundant cast

* refactor: annotate deprecated methods

* refactor: move up inner ShapeLabelpainter

* chore: add copyright header for new files
  • Loading branch information
miurahr authored Mar 31, 2024
1 parent 69f29ef commit 4d8e79b
Show file tree
Hide file tree
Showing 18 changed files with 226 additions and 165 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ protected UnderMouseInfo findComponentUnderMouse(MouseEvent e) {
Window[] children = w.getOwnedWindows();
for (int i = 0; i < children.length; i++) {
if (children[i] instanceof FloatingDockableContainer && children[i].isVisible()) {
Window wChild = (Window) children[i]; // assumed by the FloatingDockableContainer interface
Window wChild = children[i]; // assumed by the FloatingDockableContainer interface
Rectangle bounds = wChild.getBounds();
if (bounds.contains(p)) {
// an owned window is on top of the desktop, at current mouse position
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ public AutoHideExpandPanel() {
// this.setComponentZOrder(heavyPanel, 0); // top most
Method m = Container.class.getMethod("setComponentZOrder",
new Class[] { Component.class, int.class });
m.invoke(this, new Object[] { heavyPanel, new Integer(0) });
m.invoke(this, new Object[] { heavyPanel, 0 });
} catch (Exception ignore) {
}

Expand Down Expand Up @@ -276,7 +276,7 @@ private void installHeavyWeightParentIfNeeded(Dockable target) {
// this.setComponentZOrder(heavyPanel, 0); // top most
Method m = Container.class.getMethod("setComponentZOrder",
new Class[] { Component.class, int.class });
m.invoke(this, new Object[] { heavyPanel, new Integer(0) });
m.invoke(this, new Object[] { heavyPanel, 0 });
} catch (Exception ignore) {
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ private void acceptSplit(DockDropEvent event, DockingConstants.Split position) {
remainingDockable = tabAncestor.getDockableAt(1);
}
}
((DockDropEvent) event).acceptDrop();
event.acceptDrop();
if (container instanceof TabbedDockableContainer) {
split(event, container, position);
} else {
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/vlsolutions/swing/docking/DockKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ public void setKey(String dockKey) {
* @see #getKey()
* @deprecated use getKey instead
*/
@Deprecated
public String getDockName() {
return getKey();
}
Expand All @@ -297,6 +298,7 @@ public String getDockName() {
* @see #setKey(String)
* @deprecated use setKey instead
*/
@Deprecated
public void setDockName(String name) {
setKey(name);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ public enum ParentType {

/**
* @deprecated use the other createDockableContainer method
* ({@link #createDockableContainer(Dockable, int)}
* ({@link #createDockableContainer(Dockable, ParentType)}
*/
@Deprecated
public SingleDockableContainer createDockableContainer(Dockable dockable, boolean c) {
return createDockableContainer(dockable, ParentType.PARENT_SPLIT_CONTAINER);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public void removeDesktop(DockingDesktop desktop) {
Window w = (Window) it.next();
boolean ancestor = false;
for (int i = 0; i < desktops.size(); i++) {
DockingDesktop desk = (DockingDesktop) desktops.get(i);
DockingDesktop desk = desktops.get(i);
if (w.isAncestorOf(desk)) {
ancestor = true;
break;
Expand Down Expand Up @@ -288,9 +288,8 @@ public void writeXML(OutputStream stream) throws IOException {
*/
public void readXML(InputStream in) throws ParserConfigurationException, IOException, SAXException {
// remove all dockable states

for (int i = 0; i < desktops.size(); i++) {
DockingDesktop desk = (DockingDesktop) desktops.get(i);
DockingDesktop desk = desktops.get(i);
desk.clear();
}

Expand Down
19 changes: 10 additions & 9 deletions src/main/java/com/vlsolutions/swing/docking/DockingDesktop.java
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ public void mousePressed(MouseEvent e) {
expandPanel.installDocking(this);
expandPanel.setVisible(false);

add(mouseMotionGrabber, new Integer(MODAL_LAYER.intValue() - 1));
add(mouseMotionGrabber, MODAL_LAYER - 1);
add(expandPanel, MODAL_LAYER);

ComponentListener resizeListener = new ComponentAdapter() {
Expand Down Expand Up @@ -1515,12 +1515,12 @@ private void remove(TabbedDockableContainer tdc) {
viewParent.remove((Component) tdc);
Component other = viewParent.getRightComponent();
// replace viewParent by other in viewParent's hierarchy
Container viewGParent = (Container) viewParent.getParent();
Container viewGParent = viewParent.getParent();
DockingUtilities.replaceChild(viewGParent, viewParent, other);
} else {
viewParent.remove((Component) tdc);
Component other = viewParent.getLeftComponent();
Container viewGParent = (Container) viewParent.getParent();
Container viewGParent = viewParent.getParent();
DockingUtilities.replaceChild(viewGParent, viewParent, other);
}
} else if (parent == dockingPanel) { // no more views to show
Expand Down Expand Up @@ -2237,12 +2237,12 @@ protected void removeContainer(SingleDockableContainer dc) {
viewParent.remove((Component) dc);
Component other = viewParent.getRightComponent();
// replace viewParent by other in viewParent's hierarchy
Container viewGParent = (Container) viewParent.getParent();
Container viewGParent = viewParent.getParent();
DockingUtilities.replaceChild(viewGParent, viewParent, other);
} else {
viewParent.remove((Component) dc);
Component other = viewParent.getLeftComponent();
Container viewGParent = (Container) viewParent.getParent();
Container viewGParent = viewParent.getParent();
DockingUtilities.replaceChild(viewGParent, viewParent, other);
}
} else if (parent instanceof TabbedDockableContainer) {
Expand Down Expand Up @@ -2917,7 +2917,7 @@ private void xmlWriteBorderDockable(AutoHideButton btn, PrintWriter out) throws

private void xmlWriteDockableWithRelativePosition(Dockable dockable, PrintWriter out) throws IOException {
DockableState state = context.getDockableState(dockable);
RelativeDockablePosition position = (RelativeDockablePosition) state.getPosition();
RelativeDockablePosition position = state.getPosition();
boolean isCompound = dockable instanceof CompoundDockable;
if (isCompound) {
out.println("<Dockable compound=\"true\">");
Expand Down Expand Up @@ -2949,7 +2949,7 @@ private void xmlWriteCompoundDockableWithRelativePosition(CompoundDockable docka
* children
*/
DockableState state = context.getDockableState(dockable);
RelativeDockablePosition position = (RelativeDockablePosition) state.getPosition();
RelativeDockablePosition position = state.getPosition();
DockKey key = dockable.getDockKey();
out.println("<Key dockName=\"" + key.getKey() + "\"/>");
out.println("<RelativePosition x=\"" + position.getX() + "\" y=\"" + position.getY() + "\" w=\""
Expand Down Expand Up @@ -3109,7 +3109,7 @@ public void clear() {
ArrayList<Dockable> floatingDockables = context.getDockablesByState(this,
DockableState.Location.FLOATING);
for (int i = 0; i < floatingDockables.size(); i++) {
Dockable d = (Dockable) floatingDockables.get(i);
Dockable d = floatingDockables.get(i);
remove(d);
}
}
Expand Down Expand Up @@ -3955,6 +3955,7 @@ public void setDockableHeight(Dockable dockable, double height) {
* @deprecated use setResizeWeight() in every dockKey for a better resizing behaviour
*
*/
@Deprecated
public void setAutoResizableDockable(Dockable dockable) {
/* this.autoResizeableDockable = dockable; */
DockingUtilities.updateResizeWeights(dockingPanel);
Expand Down Expand Up @@ -4022,7 +4023,7 @@ public void removeFromTabbedGroup(Dockable dockable) { // 2005/07/13
group.remove(dockable);
if (group.size() == 1) { // end of grouping as there are no more
// dockables linked.
Dockable d = (Dockable) group.removeFirst();
Dockable d = group.removeFirst();
tabbedGroups.remove(d);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public static void setShadowDesktopStyle() {
DockingUISettings.getInstance().installUI();
UIManager.put("DockView.singleDockableBorder", new ShadowBorder());
UIManager.put("DockView.tabbedDockableBorder", new ShadowBorder(false));
UIManager.put("TabbedDockableContainer.tabPlacement", new Integer(SwingConstants.BOTTOM));
UIManager.put("TabbedDockableContainer.tabPlacement", SwingConstants.BOTTOM);
}

/**
Expand All @@ -148,7 +148,7 @@ public static void setFlatDesktopStyle() {
.createCompoundBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1), innerFlatSingleBorder);
UIManager.put("DockView.singleDockableBorder", flatSingleBorder);
UIManager.put("DockView.tabbedDockableBorder", null);
UIManager.put("TabbedDockableContainer.tabPlacement", new Integer(SwingConstants.TOP));
UIManager.put("TabbedDockableContainer.tabPlacement", SwingConstants.TOP);
}

/**
Expand All @@ -167,7 +167,7 @@ public static void setDottedDesktopStyle() {
UIManager.put("DockView.singleDockableBorder", innerFlatSingleBorder);
// BorderFactory.createLineBorder(shadow));
UIManager.put("DockView.tabbedDockableBorder", null);
UIManager.put("TabbedDockableContainer.tabPlacement", new Integer(SwingConstants.TOP));
UIManager.put("TabbedDockableContainer.tabPlacement", SwingConstants.TOP);

UIManager.put("SplitContainer.drawDotsDelimitors", Boolean.TRUE);
}
Expand All @@ -181,7 +181,7 @@ public static void setCustomSplitDesktopStyle(BufferedImage horizontalTile, Buff

// UIManager.put("DockView.singleDockableBorder", null);
UIManager.put("DockView.tabbedDockableBorder", null);
UIManager.put("TabbedDockableContainer.tabPlacement", new Integer(SwingConstants.TOP));
UIManager.put("TabbedDockableContainer.tabPlacement", SwingConstants.TOP);

UIManager.put("SplitContainer.drawDotsDelimitors", Boolean.FALSE);
UIManager.put("SplitContainer.hImage", horizontalTile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public void actionPerformed(ActionEvent actionEvent) {
closingState = CONFIRM;
for (int i = 0; i < model.states.length; i++) {
DockableState state = model.states[i];
boolean newVisible = ((Boolean) visibleViews.get(state)).booleanValue();
boolean newVisible = visibleViews.get(state);
boolean oldVisible = state.getLocation() != DockableState.Location.CLOSED;
if (oldVisible != newVisible) {
if (newVisible) {
Expand Down Expand Up @@ -285,7 +285,7 @@ class DockablesTableModel extends AbstractTableModel {

String[] colNames = { " ", "Name", "Visible" };

private DockableState[] states;
private final DockableState[] states;

DockablesTableModel(DockableState[] states) {
this.states = states;
Expand All @@ -309,14 +309,11 @@ public String getColumnName(int col) {
}

public boolean isCellEditable(int row, int col) {
if (col != 2)
return false;
Dockable dockable = states[row].getDockable();
if (dockable.getDockKey().isCloseEnabled()) {
return true;
} else {
if (col != 2) {
return false;
}
Dockable dockable = states[row].getDockable();
return dockable.getDockKey().isCloseEnabled();
}

public Class getColumnClass(int col) {
Expand All @@ -339,7 +336,7 @@ public Object getValueAt(int row, int col) {
case 1:
return state.getDockable().getDockKey().getName();
case 2:
return (Boolean) visibleViews.get(state);
return visibleViews.get(state);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,12 @@

package com.vlsolutions.swing.docking;

import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Component;
import java.awt.Composite;
import java.awt.Cursor;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
Expand All @@ -50,21 +44,13 @@ public class DragControlerGlassPane extends JComponent {

Cursor stopDragCursor, dragCursor, floatCursor, swapDragCursor;

Color innerColor = new Color(64, 64, 64, 64);

Color textColor = Color.WHITE;

Color textFillColor = new Color(32, 32, 32, 128);

Color textBorderColor = new Color(64, 64, 64);

boolean paintShapes = true;

ShapeLabelPainter labelPainter = new ShapeLabelPainter();
private final ShapeLabelPainter labelPainter = new ShapeLabelPainter(this);

ShapeOutlinePainter outlinePainer = new ShapeOutlinePainter();
private final ShapeOutlinePainter outlinePainer = new ShapeOutlinePainter();

private DragControler controler;
private final DragControler controler;

DragControlerGlassPane(DragControler controler) {
this.controler = controler;
Expand All @@ -80,6 +66,10 @@ public void setPaintShapes(boolean paintShapes) {
this.paintShapes = paintShapes;
}

DragControler getControler() {
return controler;
}

public void paintComponent(Graphics g) {
if (paintShapes) {
Graphics2D g2 = (Graphics2D) g;
Expand Down Expand Up @@ -128,59 +118,4 @@ public void showDragCursor() {
}
setCursor(dragCursor);
}

/** the object responsible for painting the shape label */
class ShapeLabelPainter {

private Color textColor = Color.WHITE;

private Color textFillColor = new Color(32, 32, 32, 128);
// private Color textFillColor = new Color(128, 128,128);

private Color textBorderColor = new Color(64, 64, 64);

public void paintLabel(Graphics2D g2, Shape s, String name) {
Rectangle bounds = s.getBounds();
FontMetrics fm = g2.getFontMetrics();
int w = fm.stringWidth(name);

g2.setColor(textFillColor);
int bx, by;
if (controler.isFloatingShape()) {
bx = bounds.x + bounds.width / 2 - w / 2;
by = bounds.y + bounds.height / 2 - fm.getHeight() / 2;
} else {
bx = 4 * ((bounds.x + bounds.width / 2 - w / 2) / 4);
// 2005/11/01 small hack to overcome small drifts of the label
// (for example when used on a tabbedpane and when the selected tab is
// one or two pixels bigger than a non selected tab.
// just snapping it with a 4*4 grid avoid those glitches (without changing
// too much the shapes algorithm).
by = 4 * ((bounds.y + bounds.height / 2 - fm.getHeight() / 2) / 4);
}
g2.fillRect(bx - 5, by, w + 10, fm.getHeight());
g2.setStroke(new BasicStroke(1));
g2.setColor(textBorderColor);
g2.drawRect(bx - 5, by, w + 10, fm.getHeight());
g2.setColor(textColor);
g2.drawString(name, bx, by + fm.getAscent());
}
}
}

/** the object responsible for painting the shape outline */
class ShapeOutlinePainter {

private Color innerColor = new Color(64, 64, 64);

public void paintShape(Graphics2D g2, Shape s) {
Composite old = g2.getComposite();
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.25f));
g2.setStroke(new BasicStroke(3));
g2.setColor(innerColor);
g2.fill(s);
g2.setComposite(old);
g2.setColor(Color.DARK_GRAY);
g2.draw(s);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,9 @@ public void setZOrder() {
// jdk1.5 only
try {
// desktop.setComponentZOrder(this, -1); // on top
Method m = Container.class.getMethod("setComponentZOrder",
new Class[] { Component.class, int.class });
m.invoke(getParent(), new Object[] { this, new Integer(0) });
} catch (Exception ignore) {
Method m = Container.class.getMethod("setComponentZOrder", Component.class, int.class);
m.invoke(getParent(), this, 0);
} catch (Exception ignored) {
}
label.setZOrder();
}
Expand Down Expand Up @@ -377,9 +376,9 @@ public void setZOrder() {
// desktop.setComponentZOrder(this, -2); // on top of heavy panel
Method m = Container.class.getMethod("setComponentZOrder",
new Class[] { Component.class, int.class });
m.invoke(getParent(), new Object[] { this, new Integer(0) });
} catch (Exception ignore) {
ignore.printStackTrace();
m.invoke(getParent(), this, 0);
} catch (Exception ignored) {
ignored.printStackTrace();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ public static int findAnchors(Component base, Container ancestorContainer) {
*
* @deprecated use getInsertionDockingAction / applyDockingAction instead
*/
@Deprecated
public static SingleDockableContainer insertDockable(Container relativeAncestorContainer,
Dockable dockable, RelativeDockablePosition position) {

Expand Down
Loading

0 comments on commit 4d8e79b

Please sign in to comment.