From c089dc9cb4f8ce33668eb40f9aab684defa553f9 Mon Sep 17 00:00:00 2001 From: Manatsawin Hanmongkolchai Date: Wed, 2 Oct 2019 00:28:28 +0700 Subject: [PATCH 1/5] Add support for multiple note tabs --- .../notes/DeleteOnlyPageException.java | 7 + .../client/plugins/notes/NoteTab.java | 123 +++++++++ .../client/plugins/notes/NotesConfig.java | 5 + .../client/plugins/notes/NotesManager.java | 110 +++++++++ .../client/plugins/notes/NotesPanel.java | 233 +++++++++--------- .../client/plugins/notes/NotesPlugin.java | 18 +- .../plugins/notes/events/PageAdded.java | 13 + .../plugins/notes/events/PageDeleted.java | 13 + .../materialtabs/MaterialTabGroup.java | 19 +- .../client/plugins/notes/add_icon.png | Bin 0 -> 121 bytes 10 files changed, 416 insertions(+), 125 deletions(-) create mode 100644 runelite-client/src/main/java/net/runelite/client/plugins/notes/DeleteOnlyPageException.java create mode 100644 runelite-client/src/main/java/net/runelite/client/plugins/notes/NoteTab.java create mode 100644 runelite-client/src/main/java/net/runelite/client/plugins/notes/NotesManager.java create mode 100644 runelite-client/src/main/java/net/runelite/client/plugins/notes/events/PageAdded.java create mode 100644 runelite-client/src/main/java/net/runelite/client/plugins/notes/events/PageDeleted.java create mode 100644 runelite-client/src/main/resources/net/runelite/client/plugins/notes/add_icon.png diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/notes/DeleteOnlyPageException.java b/runelite-client/src/main/java/net/runelite/client/plugins/notes/DeleteOnlyPageException.java new file mode 100644 index 0000000000..31604cb5e5 --- /dev/null +++ b/runelite-client/src/main/java/net/runelite/client/plugins/notes/DeleteOnlyPageException.java @@ -0,0 +1,7 @@ +package net.runelite.client.plugins.notes; + +class DeleteOnlyPageException extends RuntimeException { + DeleteOnlyPageException() { + super("Cannot delete the only page"); + } +} diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/notes/NoteTab.java b/runelite-client/src/main/java/net/runelite/client/plugins/notes/NoteTab.java new file mode 100644 index 0000000000..1aad52881f --- /dev/null +++ b/runelite-client/src/main/java/net/runelite/client/plugins/notes/NoteTab.java @@ -0,0 +1,123 @@ +/* + * Copyright (c) 2019, whs + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.runelite.client.plugins.notes; + +import lombok.extern.slf4j.Slf4j; +import net.runelite.client.ui.ColorScheme; + +import javax.swing.*; +import javax.swing.border.EmptyBorder; +import javax.swing.text.BadLocationException; +import javax.swing.text.Document; +import javax.swing.undo.CannotUndoException; +import javax.swing.undo.UndoManager; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.FocusEvent; +import java.awt.event.FocusListener; + +@Slf4j +class NoteTab extends JPanel { + private final NotesManager manager; + private final JTextArea notesEditor = new JTextArea(); + private final UndoManager undoRedo = new UndoManager(); + + private int index; + + NoteTab(NotesManager mManager, int mIndex) { + manager = mManager; + index = mIndex; + + setLayout(new BorderLayout()); + setBackground(ColorScheme.DARKER_GRAY_COLOR); + + notesEditor.setTabSize(2); + notesEditor.setLineWrap(true); + notesEditor.setWrapStyleWord(true); + + notesEditor.setOpaque(false); + + notesEditor.setText(manager.getNotes().get(mIndex)); + + // setting the limit to a 500 as UndoManager registers every key press, + // which means that be default we would be able to undo only a sentence. + // note: the default limit is 100 + undoRedo.setLimit(500); + notesEditor.getDocument().addUndoableEditListener(e -> undoRedo.addEdit(e.getEdit())); + + notesEditor.getInputMap().put(KeyStroke.getKeyStroke("control Z"), "Undo"); + notesEditor.getInputMap().put(KeyStroke.getKeyStroke("control Y"), "Redo"); + + notesEditor.getActionMap().put("Undo", new AbstractAction("Undo") { + @Override + public void actionPerformed(ActionEvent e) { + try { + if (undoRedo.canUndo()) { + undoRedo.undo(); + } + } catch (CannotUndoException ex) { + log.warn("Notes Document Unable To Undo: " + ex); + } + } + }); + + notesEditor.getActionMap().put("Redo", new AbstractAction("Redo") { + @Override + public void actionPerformed(ActionEvent e) { + try { + if (undoRedo.canRedo()) { + undoRedo.redo(); + } + } catch (CannotUndoException ex) { + log.warn("Notes Document Unable To Redo: " + ex); + } + } + }); + + notesEditor.addFocusListener(new FocusListener() { + @Override + public void focusGained(FocusEvent e) { + + } + + @Override + public void focusLost(FocusEvent e) { + notesChanged(notesEditor.getDocument()); + } + + private void notesChanged(Document doc) { + try { + // get document text and save to config whenever editor is changed + String data = doc.getText(0, doc.getLength()); + manager.updateNote(index, data); + } catch (BadLocationException ex) { + log.warn("Notes Document Bad Location: " + ex); + } + } + }); + add(notesEditor, BorderLayout.CENTER); + setBorder(new EmptyBorder(10, 10, 10, 10)); + } +} diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/notes/NotesConfig.java b/runelite-client/src/main/java/net/runelite/client/plugins/notes/NotesConfig.java index d8834bed5c..82ac2ef3c6 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/notes/NotesConfig.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/notes/NotesConfig.java @@ -31,6 +31,11 @@ import net.runelite.client.config.ConfigItem; @ConfigGroup("notes") public interface NotesConfig extends Config { + String CONFIG_GROUP = "notes"; + String NOTES = "notes"; + + int MAX_NOTES = 5; + @ConfigItem( keyName = "notesData", name = "", diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/notes/NotesManager.java b/runelite-client/src/main/java/net/runelite/client/plugins/notes/NotesManager.java new file mode 100644 index 0000000000..0bb1aa9fc7 --- /dev/null +++ b/runelite-client/src/main/java/net/runelite/client/plugins/notes/NotesManager.java @@ -0,0 +1,110 @@ +/* + * Copyright (c) 2019, whs + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.runelite.client.plugins.notes; + +import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; +import joptsimple.internal.Strings; +import lombok.Getter; +import lombok.extern.slf4j.Slf4j; +import net.runelite.client.config.ConfigManager; +import net.runelite.client.eventbus.EventBus; +import net.runelite.client.plugins.notes.events.PageAdded; +import net.runelite.client.plugins.notes.events.PageDeleted; + +import javax.inject.Inject; +import javax.inject.Singleton; +import java.util.ArrayList; +import java.util.List; + +@Singleton +@Slf4j +public class NotesManager { + @Inject + private ConfigManager configManager; + + @Inject + private NotesConfig config; + + @Inject + private EventBus eventBus; + + @Getter + private List notes = new ArrayList<>(); + + void loadNotes() { + final String configJson = configManager.getConfiguration(NotesConfig.CONFIG_GROUP, NotesConfig.NOTES); + + notes = null; + if (!Strings.isNullOrEmpty(configJson)) { + final Gson gson = new Gson(); + notes = gson.fromJson(configJson, new TypeToken>() { + }.getType()); + } + + if (notes == null) { + notes = new ArrayList<>(); + } + + // migrate from legacy single tab notes + if (!config.notesData().isEmpty()) { + log.info("Adding tab for legacy note data"); + notes.add(0, config.notesData()); + } + } + + void updateNote(int index, String data) { + notes.set(index, data); + save(); + } + + void save() { + final Gson gson = new Gson(); + final String json = gson.toJson(notes); + configManager.setConfiguration(NotesConfig.CONFIG_GROUP, NotesConfig.NOTES, json); + + // Remove legacy notes + if (!config.notesData().isEmpty()) { + log.info("Removing legacy note data"); + config.notesData(""); + } + } + + void addPage() { + notes.add(""); + eventBus.post(PageAdded.class, new PageAdded(notes.size() - 1)); + save(); + } + + void deletePage(int index) { + if (notes.size() <= 1) { + throw new DeleteOnlyPageException(); + } + + notes.remove(index); + eventBus.post(PageDeleted.class, new PageDeleted(index)); + save(); + } +} diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/notes/NotesPanel.java b/runelite-client/src/main/java/net/runelite/client/plugins/notes/NotesPanel.java index 4092f2c15b..0e7121f6d6 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/notes/NotesPanel.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/notes/NotesPanel.java @@ -1,6 +1,7 @@ /* * Copyright (c) 2018 Charlie Waters * Copyright (c) 2018, Psikoi + * Copyright (c) 2019, whs * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -25,140 +26,144 @@ */ package net.runelite.client.plugins.notes; -import java.awt.BorderLayout; -import java.awt.event.ActionEvent; -import java.awt.event.FocusEvent; -import java.awt.event.FocusListener; -import javax.inject.Singleton; -import javax.swing.AbstractAction; -import javax.swing.BorderFactory; -import javax.swing.JPanel; -import javax.swing.JTextArea; -import javax.swing.KeyStroke; -import javax.swing.border.EmptyBorder; -import javax.swing.text.BadLocationException; -import javax.swing.text.Document; -import javax.swing.undo.CannotUndoException; -import javax.swing.undo.UndoManager; import lombok.extern.slf4j.Slf4j; +import net.runelite.client.eventbus.EventBus; +import net.runelite.client.plugins.notes.events.PageAdded; +import net.runelite.client.plugins.notes.events.PageDeleted; import net.runelite.client.ui.ColorScheme; import net.runelite.client.ui.PluginPanel; +import net.runelite.client.ui.components.materialtabs.MaterialTab; +import net.runelite.client.ui.components.materialtabs.MaterialTabGroup; +import net.runelite.client.util.ImageUtil; + +import javax.inject.Inject; +import javax.inject.Singleton; +import javax.swing.*; +import javax.swing.border.EmptyBorder; +import java.awt.*; +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; + +import static javax.swing.JOptionPane.*; @Slf4j @Singleton -class NotesPanel extends PluginPanel -{ - private final JTextArea notesEditor = new JTextArea(); - private final UndoManager undoRedo = new UndoManager(); +class NotesPanel extends PluginPanel { + @Inject + private NotesManager notesManager; - void init(final NotesConfig config) - { - // this may or may not qualify as a hack - // but this lets the editor pane expand to fill the whole parent panel - getParent().setLayout(new BorderLayout()); - getParent().add(this, BorderLayout.CENTER); + @Inject + private EventBus eventBus; - setLayout(new BorderLayout()); - setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); - setBackground(ColorScheme.DARK_GRAY_COLOR); + private final JPanel display = new JPanel(); + private final MaterialTabGroup tabGroup = new MaterialTabGroup(display); + private final ImageIcon addIcon = new ImageIcon(ImageUtil.getResourceStreamFromClass(getClass(), "add_icon.png")); + private MaterialTab addTab; + private List tabs = new ArrayList<>(); - notesEditor.setTabSize(2); - notesEditor.setLineWrap(true); - notesEditor.setWrapStyleWord(true); + void init(final NotesConfig config) { + eventBus.subscribe(PageAdded.class, this, this::onPageAdded); + eventBus.subscribe(PageDeleted.class, this, this::onPageDeleted); - JPanel notesContainer = new JPanel(); - notesContainer.setLayout(new BorderLayout()); - notesContainer.setBackground(ColorScheme.DARKER_GRAY_COLOR); + // this may or may not qualify as a hack + // but this lets the editor pane expand to fill the whole parent panel + getParent().setLayout(new BorderLayout()); + getParent().add(this, BorderLayout.CENTER); - notesEditor.setOpaque(false); + setLayout(new BorderLayout()); + setBackground(ColorScheme.DARK_GRAY_COLOR); - // load note text - String data = config.notesData(); - notesEditor.setText(data); + tabGroup.setBorder(new EmptyBorder(0, 0, 10, 0)); - // setting the limit to a 500 as UndoManager registers every key press, - // which means that be default we would be able to undo only a sentence. - // note: the default limit is 100 - undoRedo.setLimit(500); - notesEditor.getDocument().addUndoableEditListener(e -> undoRedo.addEdit(e.getEdit())); + buildAddTab(); - notesEditor.getInputMap().put(KeyStroke.getKeyStroke("control Z"), "Undo"); - notesEditor.getInputMap().put(KeyStroke.getKeyStroke("control Y"), "Redo"); + add(tabGroup, BorderLayout.NORTH); + add(display, BorderLayout.CENTER); + } - notesEditor.getActionMap().put("Undo", new AbstractAction("Undo") - { - @Override - public void actionPerformed(ActionEvent e) - { - try - { - if (undoRedo.canUndo()) - { - undoRedo.undo(); - } - } - catch (CannotUndoException ex) - { - log.warn("Notes Document Unable To Undo: " + ex); - } - } - }); + private void buildAddTab() { + addTab = new MaterialTab(addIcon, tabGroup, new JPanel()); + addTab.setOnSelectEvent(() -> { + notesManager.addPage(); + return false; + }); + } - notesEditor.getActionMap().put("Redo", new AbstractAction("Redo") - { - @Override - public void actionPerformed(ActionEvent e) - { - try - { - if (undoRedo.canRedo()) - { - undoRedo.redo(); - } - } - catch (CannotUndoException ex) - { - log.warn("Notes Document Unable To Redo: " + ex); - } - } - }); + void rebuild() { + tabs = new LinkedList<>(); + tabGroup.removeAll(); - notesEditor.addFocusListener(new FocusListener() - { - @Override - public void focusGained(FocusEvent e) - { + int totalNotes = notesManager.getNotes().size(); - } + for (int i = 0; i < totalNotes; i++) { + MaterialTab tab = buildTab(i); + tabs.add(tab); + tabGroup.addTab(tab); + } - @Override - public void focusLost(FocusEvent e) - { - notesChanged(notesEditor.getDocument()); - } + if (totalNotes < NotesConfig.MAX_NOTES) { + tabGroup.addTab(addTab); + } - private void notesChanged(Document doc) - { - try - { - // get document text and save to config whenever editor is changed - String data = doc.getText(0, doc.getLength()); - config.notesData(data); - } - catch (BadLocationException ex) - { - log.warn("Notes Document Bad Location: " + ex); - } - } - }); - notesContainer.add(notesEditor, BorderLayout.CENTER); - notesContainer.setBorder(new EmptyBorder(10, 10, 10, 10)); + if (tabs.size() > 0) { + // select the first tab + tabGroup.select(tabGroup.getTab(0)); + } - add(notesContainer, BorderLayout.CENTER); - } + revalidate(); + repaint(); + } - void setNotes(String data) - { - notesEditor.setText(data); - } + private void onPageAdded(PageAdded e) { + MaterialTab tab = buildTab(e.getIndex()); + tabs.add(tab); + tabGroup.addTab(tab); + + // re-add add button to make it last + tabGroup.removeTab(addTab); + if (notesManager.getNotes().size() < NotesConfig.MAX_NOTES) { + tabGroup.addTab(addTab); + } + + revalidate(); + repaint(); + } + + private void onPageDeleted(PageDeleted e) { + rebuild(); + } + + private MaterialTab buildTab(int index) { + String name = String.valueOf(index + 1); + NoteTab noteTab = new NoteTab(notesManager, index); + + MaterialTab materialTab = new MaterialTab(name, tabGroup, noteTab); + materialTab.setPreferredSize(new Dimension(30, 27)); + materialTab.setName(name); + + final JMenuItem deleteMenuItem = new JMenuItem(); + deleteMenuItem.setText(String.format("Delete note %s", name)); + + deleteMenuItem.addActionListener(e -> { + if (JOptionPane.showConfirmDialog(getRootFrame(), String.format("Delete note page %s?", name), "Notes", YES_NO_OPTION) != YES_OPTION) { + return; + } + try { + notesManager.deletePage(index); + } catch (DeleteOnlyPageException err) { + SwingUtilities.invokeLater(() -> JOptionPane.showMessageDialog(getRootFrame(), + "Cannot delete the last page", + "Notes", ERROR_MESSAGE)); + } + }); + + final JPopupMenu contextMenu = new JPopupMenu(); + contextMenu.setBorder(new EmptyBorder(5, 5, 5, 5)); + contextMenu.add(deleteMenuItem); + + materialTab.setComponentPopupMenu(contextMenu); + + return materialTab; + } } diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/notes/NotesPlugin.java b/runelite-client/src/main/java/net/runelite/client/plugins/notes/NotesPlugin.java index 4b5777c8fc..6896fb36d0 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/notes/NotesPlugin.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/notes/NotesPlugin.java @@ -25,9 +25,6 @@ package net.runelite.client.plugins.notes; import com.google.inject.Provides; -import java.awt.image.BufferedImage; -import javax.inject.Inject; -import javax.inject.Singleton; import net.runelite.client.config.ConfigManager; import net.runelite.client.eventbus.EventBus; import net.runelite.client.events.SessionOpen; @@ -37,6 +34,10 @@ import net.runelite.client.ui.ClientToolbar; import net.runelite.client.ui.NavigationButton; import net.runelite.client.util.ImageUtil; +import javax.inject.Inject; +import javax.inject.Singleton; +import java.awt.image.BufferedImage; + @PluginDescriptor( name = "Notes", description = "Enable the Notes panel", @@ -52,6 +53,9 @@ public class NotesPlugin extends Plugin @Inject private NotesConfig config; + @Inject + private NotesManager notesManager; + @Inject private EventBus eventBus; @@ -82,6 +86,9 @@ public class NotesPlugin extends Plugin .build(); clientToolbar.addNavigation(navButton); + + notesManager.loadNotes(); + panel.rebuild(); } @Override @@ -94,8 +101,7 @@ public class NotesPlugin extends Plugin private void onSessionOpen(SessionOpen event) { - // update notes - String data = config.notesData(); - panel.setNotes(data); + notesManager.loadNotes(); + panel.rebuild(); } } diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/notes/events/PageAdded.java b/runelite-client/src/main/java/net/runelite/client/plugins/notes/events/PageAdded.java new file mode 100644 index 0000000000..12428f98f1 --- /dev/null +++ b/runelite-client/src/main/java/net/runelite/client/plugins/notes/events/PageAdded.java @@ -0,0 +1,13 @@ +package net.runelite.client.plugins.notes.events; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.Setter; +import net.runelite.api.events.Event; + +@AllArgsConstructor +public class PageAdded implements Event { + @Getter + @Setter + private int index; +} diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/notes/events/PageDeleted.java b/runelite-client/src/main/java/net/runelite/client/plugins/notes/events/PageDeleted.java new file mode 100644 index 0000000000..fbc6fdbe6e --- /dev/null +++ b/runelite-client/src/main/java/net/runelite/client/plugins/notes/events/PageDeleted.java @@ -0,0 +1,13 @@ +package net.runelite.client.plugins.notes.events; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.Setter; +import net.runelite.api.events.Event; + +@AllArgsConstructor +public class PageDeleted implements Event { + @Getter + @Setter + private int index; +} diff --git a/runelite-client/src/main/java/net/runelite/client/ui/components/materialtabs/MaterialTabGroup.java b/runelite-client/src/main/java/net/runelite/client/ui/components/materialtabs/MaterialTabGroup.java index d6ef646b50..8fda3a1628 100644 --- a/runelite-client/src/main/java/net/runelite/client/ui/components/materialtabs/MaterialTabGroup.java +++ b/runelite-client/src/main/java/net/runelite/client/ui/components/materialtabs/MaterialTabGroup.java @@ -24,11 +24,10 @@ */ package net.runelite.client.ui.components.materialtabs; -import java.awt.BorderLayout; -import java.awt.FlowLayout; +import javax.swing.*; +import java.awt.*; import java.util.ArrayList; import java.util.List; -import javax.swing.JPanel; /** * This class will be a container (group) for the new Material Tabs. It will @@ -83,10 +82,20 @@ public class MaterialTabGroup extends JPanel return tabs.get(index); } - public void addTab(MaterialTab tab) - { + public void addTab(MaterialTab tab) { tabs.add(tab); add(tab, BorderLayout.NORTH); + + invalidate(); + repaint(); + } + + public void removeTab(MaterialTab tab) { + tabs.remove(tab); + remove(tab); + + invalidate(); + repaint(); } /*** diff --git a/runelite-client/src/main/resources/net/runelite/client/plugins/notes/add_icon.png b/runelite-client/src/main/resources/net/runelite/client/plugins/notes/add_icon.png new file mode 100644 index 0000000000000000000000000000000000000000..343c3dce0cd5c460af2627b8a4acb39f68cc86fe GIT binary patch literal 121 zcmeAS@N?(olHy`uVBq!ia0vp^d?3uh1|;P@bT0xa&H|6fVg?3oVGw3ym^DWND5&k} z;uxYaF*)G?V@#UG-}0FczsOIw5!T~$N>Is2IU-(UvQbc7qSu|vgo)vBE7!_33zDV) PwJ~_Q`njxgN@xNAsURJG literal 0 HcmV?d00001 From 5db8c66b660b43772ccfa6ee440f66b7ee4cc4ac Mon Sep 17 00:00:00 2001 From: Manatsawin Hanmongkolchai Date: Wed, 2 Oct 2019 01:00:36 +0700 Subject: [PATCH 2/5] Fix checkstyles --- .../notes/DeleteOnlyPageException.java | 10 +- .../client/plugins/notes/NoteTab.java | 173 ++++++++------ .../client/plugins/notes/NotesConfig.java | 6 +- .../client/plugins/notes/NotesManager.java | 127 +++++----- .../client/plugins/notes/NotesPanel.java | 226 ++++++++++-------- .../client/plugins/notes/NotesPlugin.java | 19 +- .../plugins/notes/events/PageAdded.java | 9 +- .../plugins/notes/events/PageDeleted.java | 9 +- .../materialtabs/MaterialTabGroup.java | 11 +- 9 files changed, 326 insertions(+), 264 deletions(-) diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/notes/DeleteOnlyPageException.java b/runelite-client/src/main/java/net/runelite/client/plugins/notes/DeleteOnlyPageException.java index 31604cb5e5..7fdf1aadc7 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/notes/DeleteOnlyPageException.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/notes/DeleteOnlyPageException.java @@ -1,7 +1,9 @@ package net.runelite.client.plugins.notes; -class DeleteOnlyPageException extends RuntimeException { - DeleteOnlyPageException() { - super("Cannot delete the only page"); - } +class DeleteOnlyPageException extends RuntimeException +{ + DeleteOnlyPageException() + { + super("Cannot delete the only page"); + } } diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/notes/NoteTab.java b/runelite-client/src/main/java/net/runelite/client/plugins/notes/NoteTab.java index 1aad52881f..d66026ff24 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/notes/NoteTab.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/notes/NoteTab.java @@ -24,100 +24,123 @@ */ package net.runelite.client.plugins.notes; -import lombok.extern.slf4j.Slf4j; -import net.runelite.client.ui.ColorScheme; - -import javax.swing.*; +import java.awt.BorderLayout; +import java.awt.event.ActionEvent; +import java.awt.event.FocusEvent; +import java.awt.event.FocusListener; +import javax.swing.AbstractAction; +import javax.swing.JPanel; +import javax.swing.JTextArea; +import javax.swing.KeyStroke; import javax.swing.border.EmptyBorder; import javax.swing.text.BadLocationException; import javax.swing.text.Document; import javax.swing.undo.CannotUndoException; import javax.swing.undo.UndoManager; -import java.awt.*; -import java.awt.event.ActionEvent; -import java.awt.event.FocusEvent; -import java.awt.event.FocusListener; +import lombok.extern.slf4j.Slf4j; +import net.runelite.client.ui.ColorScheme; @Slf4j -class NoteTab extends JPanel { - private final NotesManager manager; - private final JTextArea notesEditor = new JTextArea(); - private final UndoManager undoRedo = new UndoManager(); +class NoteTab extends JPanel +{ + private final NotesManager manager; + private final JTextArea notesEditor = new JTextArea(); + private final UndoManager undoRedo = new UndoManager(); - private int index; + private int index; - NoteTab(NotesManager mManager, int mIndex) { - manager = mManager; - index = mIndex; + NoteTab(NotesManager mManager, int mIndex) + { + manager = mManager; + index = mIndex; - setLayout(new BorderLayout()); - setBackground(ColorScheme.DARKER_GRAY_COLOR); + setLayout(new BorderLayout()); + setBackground(ColorScheme.DARKER_GRAY_COLOR); - notesEditor.setTabSize(2); - notesEditor.setLineWrap(true); - notesEditor.setWrapStyleWord(true); + notesEditor.setTabSize(2); + notesEditor.setLineWrap(true); + notesEditor.setWrapStyleWord(true); - notesEditor.setOpaque(false); + notesEditor.setOpaque(false); - notesEditor.setText(manager.getNotes().get(mIndex)); + notesEditor.setText(manager.getNotes().get(mIndex)); - // setting the limit to a 500 as UndoManager registers every key press, - // which means that be default we would be able to undo only a sentence. - // note: the default limit is 100 - undoRedo.setLimit(500); - notesEditor.getDocument().addUndoableEditListener(e -> undoRedo.addEdit(e.getEdit())); + // setting the limit to a 500 as UndoManager registers every key press, + // which means that be default we would be able to undo only a sentence. + // note: the default limit is 100 + undoRedo.setLimit(500); + notesEditor.getDocument().addUndoableEditListener(e -> undoRedo.addEdit(e.getEdit())); - notesEditor.getInputMap().put(KeyStroke.getKeyStroke("control Z"), "Undo"); - notesEditor.getInputMap().put(KeyStroke.getKeyStroke("control Y"), "Redo"); + notesEditor.getInputMap().put(KeyStroke.getKeyStroke("control Z"), "Undo"); + notesEditor.getInputMap().put(KeyStroke.getKeyStroke("control Y"), "Redo"); - notesEditor.getActionMap().put("Undo", new AbstractAction("Undo") { - @Override - public void actionPerformed(ActionEvent e) { - try { - if (undoRedo.canUndo()) { - undoRedo.undo(); - } - } catch (CannotUndoException ex) { - log.warn("Notes Document Unable To Undo: " + ex); - } - } - }); + notesEditor.getActionMap().put("Undo", new AbstractAction("Undo") + { + @Override + public void actionPerformed(ActionEvent e) + { + try + { + if (undoRedo.canUndo()) + { + undoRedo.undo(); + } + } + catch (CannotUndoException ex) + { + log.warn("Notes Document Unable To Undo: " + ex); + } + } + }); - notesEditor.getActionMap().put("Redo", new AbstractAction("Redo") { - @Override - public void actionPerformed(ActionEvent e) { - try { - if (undoRedo.canRedo()) { - undoRedo.redo(); - } - } catch (CannotUndoException ex) { - log.warn("Notes Document Unable To Redo: " + ex); - } - } - }); + notesEditor.getActionMap().put("Redo", new AbstractAction("Redo") + { + @Override + public void actionPerformed(ActionEvent e) + { + try + { + if (undoRedo.canRedo()) + { + undoRedo.redo(); + } + } + catch (CannotUndoException ex) + { + log.warn("Notes Document Unable To Redo: " + ex); + } + } + }); - notesEditor.addFocusListener(new FocusListener() { - @Override - public void focusGained(FocusEvent e) { + notesEditor.addFocusListener(new FocusListener() + { + @Override + public void focusGained(FocusEvent e) + { - } + } - @Override - public void focusLost(FocusEvent e) { - notesChanged(notesEditor.getDocument()); - } + @Override + public void focusLost(FocusEvent e) + { + notesChanged(notesEditor.getDocument()); + } - private void notesChanged(Document doc) { - try { - // get document text and save to config whenever editor is changed - String data = doc.getText(0, doc.getLength()); - manager.updateNote(index, data); - } catch (BadLocationException ex) { - log.warn("Notes Document Bad Location: " + ex); - } - } - }); - add(notesEditor, BorderLayout.CENTER); - setBorder(new EmptyBorder(10, 10, 10, 10)); - } + private void notesChanged(Document doc) + { + try + { + // get document text and save to config whenever editor is changed + String data = doc.getText(0, doc.getLength()); + manager.updateNote(index, data); + } + catch (BadLocationException ex) + { + log.warn("Notes Document Bad Location: " + ex); + } + } + }); + add(notesEditor, BorderLayout.CENTER); + setBorder(new EmptyBorder(10, 10, 10, 10)); + } } diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/notes/NotesConfig.java b/runelite-client/src/main/java/net/runelite/client/plugins/notes/NotesConfig.java index 82ac2ef3c6..043819c2e8 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/notes/NotesConfig.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/notes/NotesConfig.java @@ -31,10 +31,10 @@ import net.runelite.client.config.ConfigItem; @ConfigGroup("notes") public interface NotesConfig extends Config { - String CONFIG_GROUP = "notes"; - String NOTES = "notes"; + String CONFIG_GROUP = "notes"; + String NOTES = "notes"; - int MAX_NOTES = 5; + int MAX_NOTES = 5; @ConfigItem( keyName = "notesData", diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/notes/NotesManager.java b/runelite-client/src/main/java/net/runelite/client/plugins/notes/NotesManager.java index 0bb1aa9fc7..3d9213ea59 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/notes/NotesManager.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/notes/NotesManager.java @@ -26,6 +26,10 @@ package net.runelite.client.plugins.notes; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; +import java.util.ArrayList; +import java.util.List; +import javax.inject.Inject; +import javax.inject.Singleton; import joptsimple.internal.Strings; import lombok.Getter; import lombok.extern.slf4j.Slf4j; @@ -34,77 +38,84 @@ import net.runelite.client.eventbus.EventBus; import net.runelite.client.plugins.notes.events.PageAdded; import net.runelite.client.plugins.notes.events.PageDeleted; -import javax.inject.Inject; -import javax.inject.Singleton; -import java.util.ArrayList; -import java.util.List; - @Singleton @Slf4j -public class NotesManager { - @Inject - private ConfigManager configManager; +public class NotesManager +{ + @Inject + private ConfigManager configManager; - @Inject - private NotesConfig config; + @Inject + private NotesConfig config; - @Inject - private EventBus eventBus; + @Inject + private EventBus eventBus; - @Getter - private List notes = new ArrayList<>(); + @Getter + private List notes = new ArrayList<>(); - void loadNotes() { - final String configJson = configManager.getConfiguration(NotesConfig.CONFIG_GROUP, NotesConfig.NOTES); + void loadNotes() + { + final String configJson = configManager.getConfiguration(NotesConfig.CONFIG_GROUP, NotesConfig.NOTES); - notes = null; - if (!Strings.isNullOrEmpty(configJson)) { - final Gson gson = new Gson(); - notes = gson.fromJson(configJson, new TypeToken>() { - }.getType()); - } + notes = null; + if (!Strings.isNullOrEmpty(configJson)) + { + final Gson gson = new Gson(); + notes = gson.fromJson(configJson, new TypeToken>() + { + }.getType()); + } - if (notes == null) { - notes = new ArrayList<>(); - } + if (notes == null) + { + notes = new ArrayList<>(); + } - // migrate from legacy single tab notes - if (!config.notesData().isEmpty()) { - log.info("Adding tab for legacy note data"); - notes.add(0, config.notesData()); - } - } + // migrate from legacy single tab notes + if (!config.notesData().isEmpty()) + { + log.info("Adding tab for legacy note data"); + notes.add(0, config.notesData()); + } + } - void updateNote(int index, String data) { - notes.set(index, data); - save(); - } + void updateNote(int index, String data) + { + notes.set(index, data); + save(); + } - void save() { - final Gson gson = new Gson(); - final String json = gson.toJson(notes); - configManager.setConfiguration(NotesConfig.CONFIG_GROUP, NotesConfig.NOTES, json); + void save() + { + final Gson gson = new Gson(); + final String json = gson.toJson(notes); + configManager.setConfiguration(NotesConfig.CONFIG_GROUP, NotesConfig.NOTES, json); - // Remove legacy notes - if (!config.notesData().isEmpty()) { - log.info("Removing legacy note data"); - config.notesData(""); - } - } + // Remove legacy notes + if (!config.notesData().isEmpty()) + { + log.info("Removing legacy note data"); + config.notesData(""); + } + } - void addPage() { - notes.add(""); - eventBus.post(PageAdded.class, new PageAdded(notes.size() - 1)); - save(); - } + void addPage() + { + notes.add(""); + eventBus.post(PageAdded.class, new PageAdded(notes.size() - 1)); + save(); + } - void deletePage(int index) { - if (notes.size() <= 1) { - throw new DeleteOnlyPageException(); - } + void deletePage(int index) + { + if (notes.size() <= 1) + { + throw new DeleteOnlyPageException(); + } - notes.remove(index); - eventBus.post(PageDeleted.class, new PageDeleted(index)); - save(); - } + notes.remove(index); + eventBus.post(PageDeleted.class, new PageDeleted(index)); + save(); + } } diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/notes/NotesPanel.java b/runelite-client/src/main/java/net/runelite/client/plugins/notes/NotesPanel.java index 0e7121f6d6..4cc6ecbbbd 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/notes/NotesPanel.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/notes/NotesPanel.java @@ -26,6 +26,24 @@ */ package net.runelite.client.plugins.notes; +import java.awt.BorderLayout; +import java.awt.Dimension; +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; +import javax.inject.Inject; +import javax.inject.Singleton; +import javax.swing.ImageIcon; +import javax.swing.JMenuItem; +import javax.swing.JOptionPane; +import static javax.swing.JOptionPane.ERROR_MESSAGE; +import static javax.swing.JOptionPane.YES_NO_OPTION; +import static javax.swing.JOptionPane.YES_OPTION; +import static javax.swing.JOptionPane.getRootFrame; +import javax.swing.JPanel; +import javax.swing.JPopupMenu; +import javax.swing.SwingUtilities; +import javax.swing.border.EmptyBorder; import lombok.extern.slf4j.Slf4j; import net.runelite.client.eventbus.EventBus; import net.runelite.client.plugins.notes.events.PageAdded; @@ -36,134 +54,138 @@ import net.runelite.client.ui.components.materialtabs.MaterialTab; import net.runelite.client.ui.components.materialtabs.MaterialTabGroup; import net.runelite.client.util.ImageUtil; -import javax.inject.Inject; -import javax.inject.Singleton; -import javax.swing.*; -import javax.swing.border.EmptyBorder; -import java.awt.*; -import java.util.ArrayList; -import java.util.LinkedList; -import java.util.List; - -import static javax.swing.JOptionPane.*; - @Slf4j @Singleton -class NotesPanel extends PluginPanel { - @Inject - private NotesManager notesManager; +class NotesPanel extends PluginPanel +{ + @Inject + private NotesManager notesManager; - @Inject - private EventBus eventBus; + @Inject + private EventBus eventBus; - private final JPanel display = new JPanel(); - private final MaterialTabGroup tabGroup = new MaterialTabGroup(display); - private final ImageIcon addIcon = new ImageIcon(ImageUtil.getResourceStreamFromClass(getClass(), "add_icon.png")); - private MaterialTab addTab; - private List tabs = new ArrayList<>(); + private final JPanel display = new JPanel(); + private final MaterialTabGroup tabGroup = new MaterialTabGroup(display); + private final ImageIcon addIcon = new ImageIcon(ImageUtil.getResourceStreamFromClass(getClass(), "add_icon.png")); + private MaterialTab addTab; + private List tabs = new ArrayList<>(); - void init(final NotesConfig config) { - eventBus.subscribe(PageAdded.class, this, this::onPageAdded); - eventBus.subscribe(PageDeleted.class, this, this::onPageDeleted); + void init(final NotesConfig config) + { + eventBus.subscribe(PageAdded.class, this, this::onPageAdded); + eventBus.subscribe(PageDeleted.class, this, this::onPageDeleted); - // this may or may not qualify as a hack - // but this lets the editor pane expand to fill the whole parent panel - getParent().setLayout(new BorderLayout()); - getParent().add(this, BorderLayout.CENTER); + // this may or may not qualify as a hack + // but this lets the editor pane expand to fill the whole parent panel + getParent().setLayout(new BorderLayout()); + getParent().add(this, BorderLayout.CENTER); - setLayout(new BorderLayout()); - setBackground(ColorScheme.DARK_GRAY_COLOR); + setLayout(new BorderLayout()); + setBackground(ColorScheme.DARK_GRAY_COLOR); - tabGroup.setBorder(new EmptyBorder(0, 0, 10, 0)); + tabGroup.setBorder(new EmptyBorder(0, 0, 10, 0)); - buildAddTab(); + buildAddTab(); - add(tabGroup, BorderLayout.NORTH); - add(display, BorderLayout.CENTER); - } + add(tabGroup, BorderLayout.NORTH); + add(display, BorderLayout.CENTER); + } - private void buildAddTab() { - addTab = new MaterialTab(addIcon, tabGroup, new JPanel()); - addTab.setOnSelectEvent(() -> { - notesManager.addPage(); - return false; - }); - } + private void buildAddTab() + { + addTab = new MaterialTab(addIcon, tabGroup, new JPanel()); + addTab.setOnSelectEvent(() -> { + notesManager.addPage(); + return false; + }); + } - void rebuild() { - tabs = new LinkedList<>(); - tabGroup.removeAll(); + void rebuild() + { + tabs = new LinkedList<>(); + tabGroup.removeAll(); - int totalNotes = notesManager.getNotes().size(); + int totalNotes = notesManager.getNotes().size(); - for (int i = 0; i < totalNotes; i++) { - MaterialTab tab = buildTab(i); - tabs.add(tab); - tabGroup.addTab(tab); - } + for (int i = 0; i < totalNotes; i++) + { + MaterialTab tab = buildTab(i); + tabs.add(tab); + tabGroup.addTab(tab); + } - if (totalNotes < NotesConfig.MAX_NOTES) { - tabGroup.addTab(addTab); - } + if (totalNotes < NotesConfig.MAX_NOTES) + { + tabGroup.addTab(addTab); + } - if (tabs.size() > 0) { - // select the first tab - tabGroup.select(tabGroup.getTab(0)); - } + if (tabs.size() > 0) + { + // select the first tab + tabGroup.select(tabGroup.getTab(0)); + } - revalidate(); - repaint(); - } + revalidate(); + repaint(); + } - private void onPageAdded(PageAdded e) { - MaterialTab tab = buildTab(e.getIndex()); - tabs.add(tab); - tabGroup.addTab(tab); + private void onPageAdded(PageAdded e) + { + MaterialTab tab = buildTab(e.getIndex()); + tabs.add(tab); + tabGroup.addTab(tab); - // re-add add button to make it last - tabGroup.removeTab(addTab); - if (notesManager.getNotes().size() < NotesConfig.MAX_NOTES) { - tabGroup.addTab(addTab); - } + // re-add add button to make it last + tabGroup.removeTab(addTab); + if (notesManager.getNotes().size() < NotesConfig.MAX_NOTES) + { + tabGroup.addTab(addTab); + } - revalidate(); - repaint(); - } + revalidate(); + repaint(); + } - private void onPageDeleted(PageDeleted e) { - rebuild(); - } + private void onPageDeleted(PageDeleted e) + { + rebuild(); + } - private MaterialTab buildTab(int index) { - String name = String.valueOf(index + 1); - NoteTab noteTab = new NoteTab(notesManager, index); + private MaterialTab buildTab(int index) + { + String name = String.valueOf(index + 1); + NoteTab noteTab = new NoteTab(notesManager, index); - MaterialTab materialTab = new MaterialTab(name, tabGroup, noteTab); - materialTab.setPreferredSize(new Dimension(30, 27)); - materialTab.setName(name); + MaterialTab materialTab = new MaterialTab(name, tabGroup, noteTab); + materialTab.setPreferredSize(new Dimension(30, 27)); + materialTab.setName(name); - final JMenuItem deleteMenuItem = new JMenuItem(); - deleteMenuItem.setText(String.format("Delete note %s", name)); + final JMenuItem deleteMenuItem = new JMenuItem(); + deleteMenuItem.setText(String.format("Delete note %s", name)); - deleteMenuItem.addActionListener(e -> { - if (JOptionPane.showConfirmDialog(getRootFrame(), String.format("Delete note page %s?", name), "Notes", YES_NO_OPTION) != YES_OPTION) { - return; - } - try { - notesManager.deletePage(index); - } catch (DeleteOnlyPageException err) { - SwingUtilities.invokeLater(() -> JOptionPane.showMessageDialog(getRootFrame(), - "Cannot delete the last page", - "Notes", ERROR_MESSAGE)); - } - }); + deleteMenuItem.addActionListener(e -> { + if (JOptionPane.showConfirmDialog(getRootFrame(), String.format("Delete note page %s?", name), "Notes", YES_NO_OPTION) != YES_OPTION) + { + return; + } + try + { + notesManager.deletePage(index); + } + catch (DeleteOnlyPageException err) + { + SwingUtilities.invokeLater(() -> JOptionPane.showMessageDialog(getRootFrame(), + "Cannot delete the last page", + "Notes", ERROR_MESSAGE)); + } + }); - final JPopupMenu contextMenu = new JPopupMenu(); - contextMenu.setBorder(new EmptyBorder(5, 5, 5, 5)); - contextMenu.add(deleteMenuItem); + final JPopupMenu contextMenu = new JPopupMenu(); + contextMenu.setBorder(new EmptyBorder(5, 5, 5, 5)); + contextMenu.add(deleteMenuItem); - materialTab.setComponentPopupMenu(contextMenu); + materialTab.setComponentPopupMenu(contextMenu); - return materialTab; - } + return materialTab; + } } diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/notes/NotesPlugin.java b/runelite-client/src/main/java/net/runelite/client/plugins/notes/NotesPlugin.java index 6896fb36d0..7f5a5f4162 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/notes/NotesPlugin.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/notes/NotesPlugin.java @@ -25,6 +25,9 @@ package net.runelite.client.plugins.notes; import com.google.inject.Provides; +import java.awt.image.BufferedImage; +import javax.inject.Inject; +import javax.inject.Singleton; import net.runelite.client.config.ConfigManager; import net.runelite.client.eventbus.EventBus; import net.runelite.client.events.SessionOpen; @@ -34,10 +37,6 @@ import net.runelite.client.ui.ClientToolbar; import net.runelite.client.ui.NavigationButton; import net.runelite.client.util.ImageUtil; -import javax.inject.Inject; -import javax.inject.Singleton; -import java.awt.image.BufferedImage; - @PluginDescriptor( name = "Notes", description = "Enable the Notes panel", @@ -53,8 +52,8 @@ public class NotesPlugin extends Plugin @Inject private NotesConfig config; - @Inject - private NotesManager notesManager; + @Inject + private NotesManager notesManager; @Inject private EventBus eventBus; @@ -87,8 +86,8 @@ public class NotesPlugin extends Plugin clientToolbar.addNavigation(navButton); - notesManager.loadNotes(); - panel.rebuild(); + notesManager.loadNotes(); + panel.rebuild(); } @Override @@ -101,7 +100,7 @@ public class NotesPlugin extends Plugin private void onSessionOpen(SessionOpen event) { - notesManager.loadNotes(); - panel.rebuild(); + notesManager.loadNotes(); + panel.rebuild(); } } diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/notes/events/PageAdded.java b/runelite-client/src/main/java/net/runelite/client/plugins/notes/events/PageAdded.java index 12428f98f1..710d1b5fd4 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/notes/events/PageAdded.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/notes/events/PageAdded.java @@ -6,8 +6,9 @@ import lombok.Setter; import net.runelite.api.events.Event; @AllArgsConstructor -public class PageAdded implements Event { - @Getter - @Setter - private int index; +public class PageAdded implements Event +{ + @Getter + @Setter + private int index; } diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/notes/events/PageDeleted.java b/runelite-client/src/main/java/net/runelite/client/plugins/notes/events/PageDeleted.java index fbc6fdbe6e..060c65cd45 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/notes/events/PageDeleted.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/notes/events/PageDeleted.java @@ -6,8 +6,9 @@ import lombok.Setter; import net.runelite.api.events.Event; @AllArgsConstructor -public class PageDeleted implements Event { - @Getter - @Setter - private int index; +public class PageDeleted implements Event +{ + @Getter + @Setter + private int index; } diff --git a/runelite-client/src/main/java/net/runelite/client/ui/components/materialtabs/MaterialTabGroup.java b/runelite-client/src/main/java/net/runelite/client/ui/components/materialtabs/MaterialTabGroup.java index 8fda3a1628..b106428a96 100644 --- a/runelite-client/src/main/java/net/runelite/client/ui/components/materialtabs/MaterialTabGroup.java +++ b/runelite-client/src/main/java/net/runelite/client/ui/components/materialtabs/MaterialTabGroup.java @@ -24,10 +24,11 @@ */ package net.runelite.client.ui.components.materialtabs; -import javax.swing.*; -import java.awt.*; +import java.awt.BorderLayout; +import java.awt.FlowLayout; import java.util.ArrayList; import java.util.List; +import javax.swing.JPanel; /** * This class will be a container (group) for the new Material Tabs. It will @@ -82,7 +83,8 @@ public class MaterialTabGroup extends JPanel return tabs.get(index); } - public void addTab(MaterialTab tab) { + public void addTab(MaterialTab tab) + { tabs.add(tab); add(tab, BorderLayout.NORTH); @@ -90,7 +92,8 @@ public class MaterialTabGroup extends JPanel repaint(); } - public void removeTab(MaterialTab tab) { + public void removeTab(MaterialTab tab) + { tabs.remove(tab); remove(tab); From 710fd6a0b5f976c2fe78036ddae0b30a8d70972b Mon Sep 17 00:00:00 2001 From: Manatsawin Hanmongkolchai Date: Thu, 3 Oct 2019 22:14:42 +0700 Subject: [PATCH 3/5] Add initial note --- .../java/net/runelite/client/plugins/notes/NotesManager.java | 1 + 1 file changed, 1 insertion(+) diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/notes/NotesManager.java b/runelite-client/src/main/java/net/runelite/client/plugins/notes/NotesManager.java index 3d9213ea59..6861228905 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/notes/NotesManager.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/notes/NotesManager.java @@ -70,6 +70,7 @@ public class NotesManager if (notes == null) { notes = new ArrayList<>(); + notes.add(""); } // migrate from legacy single tab notes From d552f94de72b73998f5653e6be1bc701a00344d5 Mon Sep 17 00:00:00 2001 From: Manatsawin Hanmongkolchai Date: Thu, 3 Oct 2019 22:38:01 +0700 Subject: [PATCH 4/5] Configurable max note pages --- .../client/plugins/notes/NotesConfig.java | 17 +++++++++++++++-- .../client/plugins/notes/NotesManager.java | 6 ++++++ .../client/plugins/notes/NotesPanel.java | 19 ++++++++++++++++--- 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/notes/NotesConfig.java b/runelite-client/src/main/java/net/runelite/client/plugins/notes/NotesConfig.java index 043819c2e8..409e125ffc 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/notes/NotesConfig.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/notes/NotesConfig.java @@ -27,6 +27,7 @@ package net.runelite.client.plugins.notes; import net.runelite.client.config.Config; import net.runelite.client.config.ConfigGroup; import net.runelite.client.config.ConfigItem; +import net.runelite.client.config.Range; @ConfigGroup("notes") public interface NotesConfig extends Config @@ -34,8 +35,6 @@ public interface NotesConfig extends Config String CONFIG_GROUP = "notes"; String NOTES = "notes"; - int MAX_NOTES = 5; - @ConfigItem( keyName = "notesData", name = "", @@ -53,4 +52,18 @@ public interface NotesConfig extends Config description = "" ) void notesData(String str); + + @Range( + min = 1, + max = 5 + ) + @ConfigItem( + keyName = "maxNotes", + name = "Max Notes", + description = "Desired maximum amount of notes" + ) + default int maxNotes() + { + return 5; + } } diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/notes/NotesManager.java b/runelite-client/src/main/java/net/runelite/client/plugins/notes/NotesManager.java index 6861228905..bb77880b71 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/notes/NotesManager.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/notes/NotesManager.java @@ -78,6 +78,12 @@ public class NotesManager { log.info("Adding tab for legacy note data"); notes.add(0, config.notesData()); + + if (notes.size() == 2 && notes.get(1).equals("")) + { + // remove the default empty note page + notes.remove(1); + } } } diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/notes/NotesPanel.java b/runelite-client/src/main/java/net/runelite/client/plugins/notes/NotesPanel.java index 4cc6ecbbbd..2764c2802a 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/notes/NotesPanel.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/notes/NotesPanel.java @@ -45,6 +45,7 @@ import javax.swing.JPopupMenu; import javax.swing.SwingUtilities; import javax.swing.border.EmptyBorder; import lombok.extern.slf4j.Slf4j; +import net.runelite.api.events.ConfigChanged; import net.runelite.client.eventbus.EventBus; import net.runelite.client.plugins.notes.events.PageAdded; import net.runelite.client.plugins.notes.events.PageDeleted; @@ -69,11 +70,15 @@ class NotesPanel extends PluginPanel private final ImageIcon addIcon = new ImageIcon(ImageUtil.getResourceStreamFromClass(getClass(), "add_icon.png")); private MaterialTab addTab; private List tabs = new ArrayList<>(); + private NotesConfig config; - void init(final NotesConfig config) + void init(final NotesConfig mConfig) { + config = mConfig; + eventBus.subscribe(PageAdded.class, this, this::onPageAdded); eventBus.subscribe(PageDeleted.class, this, this::onPageDeleted); + eventBus.subscribe(ConfigChanged.class, this, this::onConfigChanged); // this may or may not qualify as a hack // but this lets the editor pane expand to fill the whole parent panel @@ -114,7 +119,7 @@ class NotesPanel extends PluginPanel tabGroup.addTab(tab); } - if (totalNotes < NotesConfig.MAX_NOTES) + if (totalNotes < config.maxNotes()) { tabGroup.addTab(addTab); } @@ -129,6 +134,14 @@ class NotesPanel extends PluginPanel repaint(); } + private void onConfigChanged(ConfigChanged e){ + if(!e.getGroup().equals(NotesConfig.CONFIG_GROUP)){ + return; + } + + rebuild(); + } + private void onPageAdded(PageAdded e) { MaterialTab tab = buildTab(e.getIndex()); @@ -137,7 +150,7 @@ class NotesPanel extends PluginPanel // re-add add button to make it last tabGroup.removeTab(addTab); - if (notesManager.getNotes().size() < NotesConfig.MAX_NOTES) + if (notesManager.getNotes().size() < config.maxNotes()) { tabGroup.addTab(addTab); } From c216301b6081fa6e990af2a92de2313f5acdf236 Mon Sep 17 00:00:00 2001 From: Manatsawin Hanmongkolchai Date: Thu, 3 Oct 2019 22:50:30 +0700 Subject: [PATCH 5/5] Fix checkstyles --- .../java/net/runelite/client/plugins/notes/NotesPanel.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/notes/NotesPanel.java b/runelite-client/src/main/java/net/runelite/client/plugins/notes/NotesPanel.java index 2764c2802a..3bfb882b51 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/notes/NotesPanel.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/notes/NotesPanel.java @@ -134,8 +134,10 @@ class NotesPanel extends PluginPanel repaint(); } - private void onConfigChanged(ConfigChanged e){ - if(!e.getGroup().equals(NotesConfig.CONFIG_GROUP)){ + private void onConfigChanged(ConfigChanged e) + { + if (!e.getGroup().equals(NotesConfig.CONFIG_GROUP)) + { return; }