Skip to content

Commit

Permalink
Merge pull request #70 from adamk33n3r/67-feature-duplicate
Browse files Browse the repository at this point in the history
Clone alerts and drag to reorder
  • Loading branch information
adamk33n3r authored Jun 3, 2023
2 parents 383675b + be26c55 commit d527b82
Show file tree
Hide file tree
Showing 20 changed files with 147 additions and 145 deletions.
20 changes: 16 additions & 4 deletions src/main/java/com/adamk33n3r/runelite/watchdog/AlertManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@ public class AlertManager {
@Named("watchdog.pluginVersion")
private String pluginVersion;

private static final Type ALERT_LIST_TYPE;
public static final Type ALERT_TYPE;
public static final Type ALERT_LIST_TYPE;

static {
ALERT_TYPE = new TypeToken<Alert>() {}.getType();
ALERT_LIST_TYPE = new TypeToken<List<Alert>>() {}.getType();
}

Expand Down Expand Up @@ -97,6 +99,19 @@ public void removeAlert(Alert alert) {
SwingUtilities.invokeLater(this.watchdogPanel::rebuild);
}

public void cloneAlert(Alert alert) {
String json = this.gson.toJson(alert, ALERT_TYPE);
Alert clonedAlert = this.gson.fromJson(json, ALERT_TYPE);
clonedAlert.setName(clonedAlert.getName() + " Clone");
this.addAlert(clonedAlert);
}

public void moveAlertTo(Alert alert, int pos) {
this.alerts.remove(alert);
this.alerts.add(pos, alert);
this.saveAlerts();
}

public void moveAlertToTop(Alert alert) {
this.alerts.remove(alert);
this.alerts.add(0, alert);
Expand Down Expand Up @@ -135,8 +150,6 @@ public void moveAlertDown(Alert alert) {
this.saveAlerts();
}

public void moveNotificationUp(Alert alert, Notification notification) {}

public void loadAlerts() {
final String json = this.configManager.getConfiguration(WatchdogConfig.CONFIG_GROUP_NAME, WatchdogConfig.ALERTS);
this.importAlerts(json, false, false);
Expand Down Expand Up @@ -243,5 +256,4 @@ private void handleUpgrades() {
this.saveAlerts();
}
}

}
29 changes: 19 additions & 10 deletions src/main/java/com/adamk33n3r/runelite/watchdog/WatchdogPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
import net.runelite.client.plugins.config.ConfigPlugin;
import net.runelite.client.plugins.info.InfoPanel;
import net.runelite.client.plugins.timetracking.TimeTrackingPlugin;
import net.runelite.client.ui.DynamicGridLayout;
import net.runelite.client.ui.MultiplexingPluginPanel;
import net.runelite.client.ui.PluginPanel;
import net.runelite.client.ui.components.DragAndDropReorderPane;
import net.runelite.client.util.ImageUtil;
import net.runelite.client.util.LinkBrowser;

Expand All @@ -29,10 +29,10 @@
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
import java.awt.BorderLayout;
Expand All @@ -44,8 +44,6 @@
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.util.Arrays;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

@Slf4j
public class WatchdogPanel extends PluginPanel {
Expand Down Expand Up @@ -90,6 +88,8 @@ public class WatchdogPanel extends PluginPanel {
public static final ImageIcon KOFI_ICON_HOVER;
public static final ImageIcon CONFIG_ICON;
public static final ImageIcon CONFIG_ICON_HOVER;
public static final ImageIcon EXPORT_ICON = new ImageIcon(ImageUtil.loadImageResource(ConfigPlugin.class, "mdi_export.png"));
public static final ImageIcon IMPORT_ICON = new ImageIcon(ImageUtil.loadImageResource(WatchdogPanel.class, "mdi_import.png"));

static {
final BufferedImage addIcon = ImageUtil.loadImageResource(TimeTrackingPlugin.class, "add_icon.png");
Expand Down Expand Up @@ -185,23 +185,32 @@ public void rebuild() {

this.add(topPanel, BorderLayout.NORTH);

DragAndDropReorderPane dragAndDropReorderPane = new DragAndDropReorderPane();
dragAndDropReorderPane.addDragListener((c) -> {
int pos = dragAndDropReorderPane.getPosition(c);
AlertListItem alertListItem = (AlertListItem) c;
// log.debug("drag listener: " + alertListItem.getAlert().getName() + " to " + pos);
alertManager.moveAlertTo(alertListItem.getAlert(), pos);
});
JPanel alertPanel = new JPanel(new BorderLayout());
JPanel alertWrapperWrapper = new JPanel(new DynamicGridLayout(0, 1, 3, 3));
alertPanel.add(alertWrapperWrapper, BorderLayout.NORTH);
alertPanel.add(dragAndDropReorderPane, BorderLayout.NORTH);

for (Alert alert : this.alertManager.getAlerts()) {
AlertListItem alertListItem = new AlertListItem(this, this.alertManager, alert);
alertWrapperWrapper.add(alertListItem);
AlertListItem alertListItem = new AlertListItem(this, this.alertManager, alert, dragAndDropReorderPane);
dragAndDropReorderPane.add(alertListItem);
}
this.add(new JScrollPane(alertPanel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER), BorderLayout.CENTER);

JPanel importExportGroup = new JPanel(new GridLayout(1, 2, 5, 0));
JButton importButton = new JButton("Import");
JButton importButton = new JButton("Import", IMPORT_ICON);
importButton.setHorizontalTextPosition(SwingConstants.LEFT);
importButton.addActionListener(ev -> {
ImportExportDialog importExportDialog = new ImportExportDialog(SwingUtilities.getWindowAncestor(this));
importExportDialog.setVisible(true);
});
importExportGroup.add(importButton);
JButton exportButton = new JButton("Export");
JButton exportButton = new JButton("Export", EXPORT_ICON);
exportButton.setHorizontalTextPosition(SwingConstants.LEFT);
exportButton.addActionListener(ev -> {
ImportExportDialog importExportDialog = new ImportExportDialog(SwingUtilities.getWindowAncestor(this), WatchdogPlugin.getInstance().getConfig().alerts());
importExportDialog.setVisible(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ public TriggerType getType() {
.orElse(null);
}

public void moveNotificationTo(Notification notification, int pos) {
this.notifications.remove(notification);
this.notifications.add(pos, notification);
}

public void moveNotificationToTop(Notification notification) {
this.notifications.remove(notification);
this.notifications.add(0, notification);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,68 @@
import com.adamk33n3r.runelite.watchdog.alerts.Alert;
import com.adamk33n3r.runelite.watchdog.ui.panels.PanelUtils;

import net.runelite.client.plugins.config.ConfigPlugin;
import net.runelite.client.ui.DynamicGridLayout;
import net.runelite.client.ui.PluginPanel;
import net.runelite.client.ui.components.MouseDragEventForwarder;
import net.runelite.client.util.ImageUtil;
import net.runelite.client.util.SwingUtil;

import lombok.Getter;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;

public class AlertListItem extends JPanel {
public static final ImageIcon DELETE_ICON;
public static final ImageIcon DELETE_ICON_HOVER;
public static final ImageIcon CLONE_ICON = new ImageIcon(ImageUtil.loadImageResource(ConfigPlugin.class, "mdi_content-duplicate.png"));
public static final ImageIcon DELETE_ICON = new ImageIcon(ImageUtil.loadImageResource(ConfigPlugin.class, "mdi_delete.png"));
public static final ImageIcon DRAG_VERT = new ImageIcon(ImageUtil.loadImageResource(WatchdogPanel.class, "mdi_drag-vertical.png"));

static {
final BufferedImage deleteImg = ImageUtil.resizeCanvas(ImageUtil.loadImageResource(WatchdogPanel.class, "delete_icon.png"), 10, 10);
DELETE_ICON = new ImageIcon(deleteImg);
DELETE_ICON_HOVER = new ImageIcon(ImageUtil.luminanceOffset(deleteImg, -80));
DELETE_ICON_HOVER = new ImageIcon(ImageUtil.luminanceOffset(DELETE_ICON.getImage(), -80));
}
public AlertListItem(WatchdogPanel panel, AlertManager alertManager, Alert alert) {

private static final int ROW_HEIGHT = 30;
private static final int PADDING = 2;

@Getter
private final Alert alert;

public AlertListItem(WatchdogPanel panel, AlertManager alertManager, Alert alert, JComponent parent) {
this.alert = alert;

this.setLayout(new BorderLayout(5, 0));
this.setPreferredSize(new Dimension(PluginPanel.PANEL_WIDTH, 30));
this.setBorder(new EmptyBorder(PADDING, 0, PADDING, 0));
this.setPreferredSize(new Dimension(PluginPanel.PANEL_WIDTH, ROW_HEIGHT + PADDING * 2));


JPanel frontGroup = new JPanel(new DynamicGridLayout(1, 0, 3, 0));

JButton dragHandle = new JButton(DRAG_VERT);
SwingUtil.removeButtonDecorations(dragHandle);
dragHandle.setPreferredSize(new Dimension(8, 16));
MouseDragEventForwarder mouseDragEventForwarder = new MouseDragEventForwarder(parent);
dragHandle.addMouseListener(mouseDragEventForwarder);
dragHandle.addMouseMotionListener(mouseDragEventForwarder);
frontGroup.add(dragHandle);

ToggleButton toggleButton = new ToggleButton();
toggleButton.setSelected(alert.isEnabled());
toggleButton.addItemListener(i -> {
alert.setEnabled(toggleButton.isSelected());
alertManager.saveAlerts();
});
this.add(toggleButton, BorderLayout.LINE_START);
toggleButton.setOpaque(false);
frontGroup.add(toggleButton);

this.add(frontGroup, BorderLayout.LINE_START);

final JButton alertButton = new JButton(alert.getName());
alertButton.setToolTipText(alert.getName());
Expand All @@ -49,24 +78,11 @@ public AlertListItem(WatchdogPanel panel, AlertManager alertManager, Alert alert
final JPanel actionButtons = new JPanel(new DynamicGridLayout(1, 0, 0, 0));
this.add(actionButtons, BorderLayout.LINE_END);

UpDownArrows upDownArrows = new UpDownArrows("Move Alert up (hold shift for top)", (btn, modifiers) -> {
if ((modifiers & ActionEvent.SHIFT_MASK) != 0) {
alertManager.moveAlertToTop(alert);
} else {
alertManager.moveAlertUp(alert);
}
panel.rebuild();
}, "Move Alert down (hold shift for bottom)", (btn, modifiers) -> {
if ((modifiers & ActionEvent.SHIFT_MASK) != 0) {
alertManager.moveAlertToBottom(alert);
} else {
alertManager.moveAlertDown(alert);
}
panel.rebuild();
}, true);
actionButtons.add(upDownArrows);
actionButtons.add(PanelUtils.createActionButton(CLONE_ICON, CLONE_ICON, "Clone Alert", (btn, modifiers) -> {
alertManager.cloneAlert(alert);
}));

final JButton deleteButton = PanelUtils.createActionButton(DELETE_ICON, DELETE_ICON_HOVER, "Delete Alert", (btn, modifiers) -> {
final JButton deleteButton = PanelUtils.createActionButton(DELETE_ICON, DELETE_ICON, "Delete Alert", (btn, modifiers) -> {
int result = JOptionPane.showConfirmDialog(this, "Are you sure you want to delete the " + alert.getName() + " alert?", "Delete?", JOptionPane.YES_NO_OPTION, JOptionPane.ERROR_MESSAGE);
if (result == JOptionPane.YES_OPTION) {
alertManager.removeAlert(alert);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import java.awt.event.FocusListener;

public class MessageNotificationPanel extends NotificationPanel {
public MessageNotificationPanel(MessageNotification notification, NotificationsPanel parentPanel, Runnable onChangeListener, PanelUtils.ButtonClickListener onRemove) {
public MessageNotificationPanel(MessageNotification notification, NotificationsPanel parentPanel, Runnable onChangeListener, PanelUtils.OnRemove onRemove) {
super(notification, parentPanel, onChangeListener, onRemove);

FlatTextArea flatTextArea = new FlatTextArea("Enter your message...", true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
import com.adamk33n3r.runelite.watchdog.notifications.Notification;
import com.adamk33n3r.runelite.watchdog.ui.AlertListItem;
import com.adamk33n3r.runelite.watchdog.ui.StretchedStackedLayout;
import com.adamk33n3r.runelite.watchdog.ui.UpDownArrows;
import com.adamk33n3r.runelite.watchdog.ui.panels.NotificationsPanel;
import com.adamk33n3r.runelite.watchdog.ui.panels.PanelUtils;

import net.runelite.client.ui.ColorScheme;
import net.runelite.client.ui.components.MouseDragEventForwarder;
import net.runelite.client.util.ImageUtil;

import lombok.Getter;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
Expand All @@ -21,7 +23,6 @@
import javax.swing.border.EmptyBorder;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;

public abstract class NotificationPanel extends JPanel {
Expand Down Expand Up @@ -63,13 +64,17 @@ public abstract class NotificationPanel extends JPanel {
CLOCK_ICON = new ImageIcon(ImageUtil.luminanceOffset(clockIcon, -80));
}

@Getter
protected Notification notification;
protected JPanel settings = new JPanel(new StretchedStackedLayout(3, 3));

private static final Border NAME_BOTTOM_BORDER = new CompoundBorder(
BorderFactory.createMatteBorder(0, 0, 1, 0, ColorScheme.DARK_GRAY_COLOR),
BorderFactory.createMatteBorder(5, 10, 5, 0, ColorScheme.DARKER_GRAY_COLOR));

public NotificationPanel(Notification notification, NotificationsPanel parentPanel, Runnable onChangeListener, PanelUtils.ButtonClickListener onRemove) {
public NotificationPanel(Notification notification, NotificationsPanel parentPanel, Runnable onChangeListener, PanelUtils.OnRemove onRemove) {
this.notification = notification;

this.setLayout(new BorderLayout());
this.setBorder(new EmptyBorder(3, 0, 0, 0));
JPanel container = new JPanel(new StretchedStackedLayout(3, 3));
Expand All @@ -88,32 +93,18 @@ public NotificationPanel(Notification notification, NotificationsPanel parentPan
nameLabel.setToolTipText(notificationType.getTooltip());
nameWrapper.add(nameLabel, BorderLayout.WEST);

MouseDragEventForwarder mouseDragEventForwarder = new MouseDragEventForwarder(parentPanel.getNotificationContainer());
nameWrapper.addMouseListener(mouseDragEventForwarder);
nameWrapper.addMouseMotionListener(mouseDragEventForwarder);
nameLabel.addMouseListener(mouseDragEventForwarder);
nameLabel.addMouseMotionListener(mouseDragEventForwarder);

// Right buttons
JPanel rightActions = new JPanel(new FlowLayout(FlowLayout.RIGHT, 6, 0));
rightActions.setBorder(new EmptyBorder(4, 0, 0, 0));
rightActions.setBackground(ColorScheme.DARKER_GRAY_COLOR);
nameWrapper.add(rightActions, BorderLayout.EAST);


UpDownArrows upDownArrows = new UpDownArrows("Move Notification up (hold shift for top)", (btn, modifiers) -> {
if ((modifiers & ActionEvent.SHIFT_MASK) != 0) {
notification.getAlert().moveNotificationToTop(notification);
} else {
notification.getAlert().moveNotificationUp(notification);
}
onChangeListener.run();
parentPanel.rebuild();
}, "Move Notification down (hold shift for bottom)", (btn, modifiers) -> {
if ((modifiers & ActionEvent.SHIFT_MASK) != 0) {
notification.getAlert().moveNotificationToBottom(notification);
} else {
notification.getAlert().moveNotificationDown(notification);
}
onChangeListener.run();
parentPanel.rebuild();
}, true);
rightActions.add(upDownArrows);

JButton focusBtn = PanelUtils.createToggleActionButton(
FOREGROUND_ICON,
FOREGROUND_ICON_HOVER,
Expand All @@ -139,7 +130,7 @@ public NotificationPanel(Notification notification, NotificationsPanel parentPan
AlertListItem.DELETE_ICON,
AlertListItem.DELETE_ICON_HOVER,
"Remove this notification",
onRemove);
(btn, modifiers) -> onRemove.elementRemoved(this));
rightActions.add(deleteBtn);

this.settings.setBorder(new EmptyBorder(5, 10, 5, 10));
Expand Down
Loading

0 comments on commit d527b82

Please sign in to comment.