Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for bookmarked locations #549

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>27.0.1-jre</version>
</dependency>
<dependency>
<groupId>org.dishevelled</groupId>
<artifactId>dsh-eventlist-view</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
Expand Down Expand Up @@ -115,4 +125,4 @@
<type>maven-plugin</type>
</dependency>
</dependencies>
</project>
</project>
2 changes: 2 additions & 0 deletions src/main/java/amidst/AmidstSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class AmidstSettings {
public final Setting<Boolean> showOceanFeatures;
public final Setting<Boolean> showNetherFortresses;
public final Setting<Boolean> showEndCities;
public final Setting<Boolean> showBookmarks;

public final Setting<Boolean> smoothScrolling;
public final Setting<Boolean> fragmentFading;
Expand Down Expand Up @@ -65,6 +66,7 @@ public AmidstSettings(Preferences preferences) {
showOceanFeatures = Setting.createBoolean( preferences, "oceanFeaturesIcons", true);
showNetherFortresses = Setting.createBoolean( preferences, "netherFortressIcons", false);
showEndCities = Setting.createBoolean( preferences, "endCityIcons", false);
showBookmarks = Setting.createBoolean( preferences, "bookmarkIcons", true);

smoothScrolling = Setting.createBoolean( preferences, "mapFlicking", true);
fragmentFading = Setting.createBoolean( preferences, "mapFading", true);
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/amidst/fragment/layer/LayerBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ private List<LayerDeclaration> createDeclarations(AmidstSettings settings, List<
declare(settings, declarations, enabledLayers, LayerIds.OCEAN_FEATURES, Dimension.OVERWORLD, false, settings.showOceanFeatures);
declare(settings, declarations, enabledLayers, LayerIds.NETHER_FORTRESS, Dimension.OVERWORLD, false, settings.showNetherFortresses);
declare(settings, declarations, enabledLayers, LayerIds.END_CITY, Dimension.END, false, settings.showEndCities);
declare(settings, declarations, enabledLayers, LayerIds.BOOKMARKS, Dimension.OVERWORLD, false, settings.showBookmarks);
// @formatter:on
return Collections.unmodifiableList(Arrays.asList(declarations));
}
Expand Down Expand Up @@ -144,7 +145,8 @@ private Iterable<FragmentLoader> createLoaders(
new WorldIconLoader<>(declarations.get(LayerIds.WOODLAND_MANSION),world.getWoodlandMansionProducer()),
new WorldIconLoader<>(declarations.get(LayerIds.OCEAN_FEATURES), world.getOceanFeaturesProducer()),
new WorldIconLoader<>(declarations.get(LayerIds.NETHER_FORTRESS), world.getNetherFortressProducer()),
new WorldIconLoader<>(declarations.get(LayerIds.END_CITY), world.getEndCityProducer(), Fragment::getEndIslands)
new WorldIconLoader<>(declarations.get(LayerIds.END_CITY), world.getEndCityProducer(), Fragment::getEndIslands),
new WorldIconLoader<>(declarations.get(LayerIds.BOOKMARKS), world.getBookmarkProducer())
));
// @formatter:on
}
Expand Down Expand Up @@ -173,7 +175,8 @@ private Iterable<FragmentDrawer> createDrawers(
new WorldIconDrawer(declarations.get(LayerIds.WOODLAND_MANSION),zoom, worldIconSelection),
new WorldIconDrawer(declarations.get(LayerIds.OCEAN_FEATURES), zoom, worldIconSelection),
new WorldIconDrawer(declarations.get(LayerIds.NETHER_FORTRESS), zoom, worldIconSelection),
new WorldIconDrawer(declarations.get(LayerIds.END_CITY), zoom, worldIconSelection)
new WorldIconDrawer(declarations.get(LayerIds.END_CITY), zoom, worldIconSelection),
new WorldIconDrawer(declarations.get(LayerIds.BOOKMARKS), zoom, worldIconSelection)
));
// @formatter:on
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/amidst/fragment/layer/LayerIds.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class LayerIds {
public static final int OCEAN_FEATURES = 14;
public static final int NETHER_FORTRESS = 15;
public static final int END_CITY = 16;
public static final int NUMBER_OF_LAYERS = 17;
public static final int BOOKMARKS = 17;
public static final int NUMBER_OF_LAYERS = 18;
// @formatter:on
}
5 changes: 5 additions & 0 deletions src/main/java/amidst/gui/main/Actions.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ public void switchProfile() {
application.displayProfileSelectWindow();
}

@CalledOnlyBy(AmidstThread.EDT)
public void displayBookmarks() {
dialogs.displayBookmarks();
}

@CalledOnlyBy(AmidstThread.EDT)
public void exit() {
if (BiomeExporter.isExporterRunning()) {
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/amidst/gui/main/MainWindowDialogs.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import amidst.documentation.CalledOnlyBy;
import amidst.documentation.NotThreadSafe;
import amidst.logging.AmidstMessageBox;
import amidst.gui.main.bookmarks.BookmarkDialog;
import amidst.mojangapi.RunningLauncherProfile;
import amidst.mojangapi.world.WorldSeed;
import amidst.mojangapi.world.WorldType;
Expand Down Expand Up @@ -90,8 +91,13 @@ private Path showSaveDialogAndGetSelectedFileOrNull(JFileChooser fileChooser) {
return null;
}
}

@CalledOnlyBy(AmidstThread.EDT)
public void displayBookmarks() {
new BookmarkDialog(frame).setVisible(true);
}

@CalledOnlyBy(AmidstThread.EDT)
@CalledOnlyBy(AmidstThread.EDT)
public void displayInfo(String title, String message) {
AmidstMessageBox.displayInfo(frame, title, message);
}
Expand Down
46 changes: 46 additions & 0 deletions src/main/java/amidst/gui/main/bookmarks/Bookmark.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package amidst.gui.main.bookmarks;

import java.io.Serializable;

import java.util.Objects;

/**
* Bookmark.
*/
public final class Bookmark implements Serializable {
private final long x;
private final long z;
private final String label;
private final int hashCode;

private Bookmark(final long x, final long z, final String label) {
if (label == null) {
throw new NullPointerException("label must not be null");
}
this.x = x;
this.z = z;
this.label = label;
this.hashCode = Objects.hash(this.x, this.z, this.label);
}

public long getX() {
return x;
}

public long getZ() {
return z;
}

public String getLabel() {
return label;
}

@Override
public int hashCode() {
return hashCode;
}

public static Bookmark valueOf(final long x, final long z, final String label) {
return new Bookmark(x, z, label);
}
}
99 changes: 99 additions & 0 deletions src/main/java/amidst/gui/main/bookmarks/BookmarkChooser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package amidst.gui.main.bookmarks;

import java.awt.Component;
import java.awt.Dialog;
import java.awt.Frame;
import java.awt.Window;

import javax.swing.JDialog;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

import javax.swing.border.EmptyBorder;

import org.dishevelled.layout.LabelFieldPanel;

final class BookmarkChooser extends LabelFieldPanel {
private final JTextField x;
private final JTextField z;
private final JTextField label;

BookmarkChooser(final int x, final int z) {
super();
this.x = new JTextField(String.valueOf(x));
this.z = new JTextField(String.valueOf(z));
label = new JTextField();
label.requestFocus();

layoutComponents();
}

private void layoutComponents() {
setBorder(new EmptyBorder(12, 12, 0, 12));
addField("X", x);
addField("Z", z);
addField("Label", label);
addFinalSpacing(12);
}

JTextField x() {
return x;
}

JTextField z() {
return z;
}

JTextField label() {
return label;
}

boolean ready() {
// todo validate
return true;
}

Bookmark getBookmark() {
if (ready()) {
return Bookmark.valueOf(Integer.parseInt(x.getText()), Integer.parseInt(z.getText()), label.getText());
}
return null;
}

public static Bookmark showDialog(final Component component, final String title, final int x, final int z) {
if (component == null) {
throw new IllegalArgumentException("component must not be null");
}
if (title == null) {
throw new IllegalArgumentException("title must not be null");
}
BookmarkChooser chooserPane = new BookmarkChooser(x, z);
BookmarkChooserDialog dialog = createDialog(component, title, true, chooserPane);
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setLocationRelativeTo(component);
dialog.show();
return dialog.getBookmark();
}

static BookmarkChooserDialog createDialog(final Component component, final String title, final boolean modal, final BookmarkChooser chooserPane) {
if (component == null) {
throw new IllegalArgumentException("component must not be null");
}
if (title == null) {
throw new IllegalArgumentException("title must not be null");
}
if (chooserPane == null) {
throw new IllegalArgumentException("chooserPane must not be null");
}
Window window = SwingUtilities.windowForComponent(component);
BookmarkChooserDialog dialog;
if (window instanceof Frame) {
dialog = new BookmarkChooserDialog((Frame) window, title, modal, chooserPane);
}
else { //if (window instanceof Dialog)
dialog = new BookmarkChooserDialog((Dialog) window, title, modal, chooserPane);
}
dialog.getAccessibleContext().setAccessibleDescription(title);
return dialog;
}
}
133 changes: 133 additions & 0 deletions src/main/java/amidst/gui/main/bookmarks/BookmarkChooserDialog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package amidst.gui.main.bookmarks;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dialog;
import java.awt.Frame;

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;

import javax.swing.border.EmptyBorder;

import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import org.dishevelled.layout.ButtonPanel;

class BookmarkChooserDialog extends JDialog {
private boolean canceled = true;

private final AbstractAction cancel = new AbstractAction("Cancel") {
@Override
public void actionPerformed(final ActionEvent event)
{
cancel();
}
};
private final AbstractAction ok = new AbstractAction("OK") {
@Override
public void actionPerformed(final ActionEvent event)
{
ok();
}
};

private final JButton okButton;
private final BookmarkChooser chooser;

BookmarkChooserDialog(final Dialog owner, final String title, final boolean modal, final BookmarkChooser chooser)
{
super(owner, title, modal);

ok.setEnabled(false);
okButton = new JButton(ok);
this.chooser = chooser;

initialize();
}

BookmarkChooserDialog(final Frame owner, final String title, final boolean modal, final BookmarkChooser chooser)
{
super(owner, title, modal);

ok.setEnabled(false);
okButton = new JButton(ok);
this.chooser = chooser;

initialize();
}

private void initialize()
{
getRootPane().setDefaultButton(okButton);
createListeners();
layoutComponents();
setSize(320, 200);
}

private void createListeners()
{
DocumentListener l = new DocumentListener() {
@Override
public void changedUpdate(final DocumentEvent e) {
ok.setEnabled(chooser.ready());
}

@Override
public void insertUpdate(final DocumentEvent e) {
ok.setEnabled(chooser.ready());
}

@Override
public void removeUpdate(final DocumentEvent e) {
ok.setEnabled(chooser.ready());
}
};

chooser.x().getDocument().addDocumentListener(l);
chooser.z().getDocument().addDocumentListener(l);
chooser.label().getDocument().addDocumentListener(l);
}

private void layoutComponents()
{
ButtonPanel buttonPanel = new ButtonPanel();
buttonPanel.setBorder(new EmptyBorder(0, 12, 12, 12));
buttonPanel.add(cancel);
buttonPanel.add(okButton);

JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
mainPanel.add("Center", chooser);
mainPanel.add("South", buttonPanel);

setContentPane(mainPanel);
}

private void cancel()
{
canceled = true;
hide();
}

private void ok()
{
canceled = false;
hide();
}

boolean wasCanceled()
{
return canceled;
}

Bookmark getBookmark()
{
return wasCanceled() ? null : chooser.getBookmark();
}
}
Loading