Skip to content

Commit

Permalink
See #17858: Update code for newer Java features
Browse files Browse the repository at this point in the history
Also add a note that `toUnmodifiableList` does not like nulls.

git-svn-id: https://josm.openstreetmap.de/svn/trunk@19120 0c6e7542-c601-0410-84e7-c038aed88b3b
  • Loading branch information
taylor.smock committed Jun 20, 2024
1 parent 064ab0a commit 0d2e9e9
Show file tree
Hide file tree
Showing 10 changed files with 19 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,8 @@ public static String getReportHeader() {
DisplayMode dm = gd.getDisplayMode();
if (dm != null) {
AffineTransform transform = gd.getDefaultConfiguration().getDefaultTransform();
// Java 11: use DisplayMode#toString
text.format(Locale.ROOT, " %d\u00D7%d (scaling %.2f\u00D7%.2f)",
dm.getWidth(), dm.getHeight(), transform.getScaleX(), transform.getScaleY());
text.format(Locale.ROOT, " %s (scaling %.2f\u00D7%.2f)",
dm, transform.getScaleX(), transform.getScaleY());
}
}
text.println();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@

/**
* Panel to inspect one or more OsmPrimitives.
*
* <p>
* Gives an unfiltered view of the object's internal state.
* Might be useful for power users to give more detailed bug reports and
* to better understand the JOSM data representation.
Expand Down Expand Up @@ -128,15 +128,15 @@ protected static String buildMapPaintText() {
for (IPrimitive osm : sel) {
String heading = tr("Styles for \"{0}\":", osm.getDisplayName(DefaultNameFormatter.getInstance()));
txtMappaint.println(heading);
txtMappaint.println(repeatString("=", heading.length()));
txtMappaint.println("=".repeat(heading.length()));

MultiCascade mc = new MultiCascade();

for (StyleSource s : elemstyles.getStyleSources()) {
if (s.active) {
heading = tr("{0} style \"{1}\"", getSort(s), s.getDisplayString());
txtMappaint.println(heading);
txtMappaint.println(repeatString("-", heading.length()));
txtMappaint.println("-".repeat(heading.length()));
s.apply(mc, osm, scale, false);
txtMappaint.println(tr("Display range: {0}", mc.range));
for (Entry<String, Cascade> e : mc.getLayers()) {
Expand All @@ -149,7 +149,7 @@ protected static String buildMapPaintText() {
txtMappaint.println();
heading = tr("List of generated Styles:");
txtMappaint.println(heading);
txtMappaint.println(repeatString("-", heading.length()));
txtMappaint.println("-".repeat(heading.length()));
StyleElementList sl = elemstyles.get(osm, scale, nc);
for (StyleElement s : sl) {
txtMappaint.print(" * ");
Expand Down Expand Up @@ -178,11 +178,6 @@ protected static String buildMapPaintText() {
return stringWriter.toString();
}

private static String repeatString(String string, int count) {
// Java 11: use String.repeat
return new String(new char[count]).replace("\0", string);
}

/* Future Ideas:
Calculate the most recent edit date from o.getTimestamp().
Sort by the count for presentation, so the most active editors are on top.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public boolean hasSelectedChangesets() {
* @return the list of selected changesets
*/
public List<Changeset> getSelectedChangesets() {
return Arrays.stream(TableHelper.getSelectedIndices(selectionModel))
return Arrays.stream(selectionModel.getSelectedIndices())
.map(sorter::convertRowIndexToModel).mapToObj(data::get).collect(Collectors.toList());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public interface ReorderableTableModel<T> extends ReorderableModel<T> {
* @see #selectedIndices()
*/
default int[] getSelectedIndices() {
return TableHelper.getSelectedIndices(getSelectionModel());
return getSelectionModel().getSelectedIndices();
}

/**
Expand Down
6 changes: 4 additions & 2 deletions src/org/openstreetmap/josm/gui/util/TableHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public static void computeColumnsWidth(JTable tbl) {
* Returns an array of all of the selected indices in the selection model, in increasing order.
* Unfortunately this method is not available in OpenJDK before version 11, see
* https://bugs.openjdk.java.net/browse/JDK-8199395
*
* <p>
* To be removed when we switch to Java 11 or later.
*
* @param selectionModel list selection model.
Expand All @@ -131,9 +131,11 @@ public static void computeColumnsWidth(JTable tbl) {
* or an empty array if nothing is selected
* @see #selectedIndices(ListSelectionModel)
* @since 15226
* @deprecated Use {@link ListSelectionModel#getSelectedIndices()} instead
*/
@Deprecated(since = "19120")
public static int[] getSelectedIndices(ListSelectionModel selectionModel) {
return selectedIndices(selectionModel).toArray();
return selectionModel.getSelectedIndices();
}

/**
Expand Down
6 changes: 4 additions & 2 deletions src/org/openstreetmap/josm/tools/PlatformHook.java
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ default boolean isHtmlSupportedInMenuTooltips() {

/**
* Returns extended modifier key used as the appropriate accelerator key for menu shortcuts.
* It is advised everywhere to use {@link Toolkit#getMenuShortcutKeyMask()} to get the cross-platform modifier, but:
* It was advised everywhere to use {@link Toolkit#getMenuShortcutKeyMask()} to get the cross-platform modifier, but:
* <ul>
* <li>it returns KeyEvent.CTRL_MASK instead of KeyEvent.CTRL_DOWN_MASK. We used the extended
* modifier for years, and Oracle recommends to use it instead, so it's best to keep it</li>
Expand All @@ -257,7 +257,9 @@ default boolean isHtmlSupportedInMenuTooltips() {
* @since 12748 (as a replacement to {@code GuiHelper.getMenuShortcutKeyMaskEx()})
*/
default int getMenuShortcutKeyMaskEx() {
// To remove when switching to Java 10+, and use Toolkit.getMenuShortcutKeyMaskEx instead
if (!GraphicsEnvironment.isHeadless()) {
return Toolkit.getDefaultToolkit().getMenuShortcutKeyMaskEx();
}
return InputEvent.CTRL_DOWN_MASK;
}

Expand Down
5 changes: 0 additions & 5 deletions src/org/openstreetmap/josm/tools/PlatformHookOsx.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,6 @@ public boolean isHtmlSupportedInMenuTooltips() {
&& ("Aqua".equals(laf) || laf.contains("Mac")));
}

@Override
public int getMenuShortcutKeyMaskEx() {
return InputEvent.META_DOWN_MASK;
}

/**
* Registers Apple handlers.
*
Expand Down
2 changes: 1 addition & 1 deletion src/org/openstreetmap/josm/tools/StreamUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public static <T> Stream<T> reversedStream(List<T> list) {
* @since 16436
*/
public static <T> Collector<T, ?, List<T>> toUnmodifiableList() {
// Java 10: use java.util.stream.Collectors.toUnmodifiableList
// Java 10: use java.util.stream.Collectors.toUnmodifiableList (note: does not like null checks OR null entries)
return Collectors.collectingAndThen(Collectors.toList(), Utils::toUnmodifiableList);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
package org.openstreetmap.josm.gui.conflict.pair.nodes;

import java.awt.BorderLayout;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import javax.swing.JFrame;

Expand All @@ -23,8 +21,7 @@ protected void populate() {
for (int i = 0; i < 20; i++) {
n1.put("key" + i, "value" + i);
}
// Java 11: use String.repeat
String note = IntStream.range(0, 50).mapToObj(i -> " A very long text ").collect(Collectors.joining());
String note = " A very long text ".repeat(50);
n1.put("note", note);
w1.addNode(new Node(2));
w1.addNode(new Node(3));
Expand Down
5 changes: 1 addition & 4 deletions test/unit/org/openstreetmap/josm/data/osm/ChangesetTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import org.junit.jupiter.api.Test;
import org.openstreetmap.josm.data.Bounds;
Expand Down Expand Up @@ -56,8 +54,7 @@ void testSetKeys() {

// Add a map with too long values => IllegalArgumentException
keys = new HashMap<>();
// Java 11: use String.repeat
keys.put("test", IntStream.range(0, MAX_CHANGESET_TAG_LENGTH + 1).mapToObj(i -> "x").collect(Collectors.joining()));
keys.put("test", "x".repeat(MAX_CHANGESET_TAG_LENGTH + 1));
try {
cs.setKeys(keys);
fail("Should have thrown an IllegalArgumentException as we gave a too long value.");
Expand Down

0 comments on commit 0d2e9e9

Please sign in to comment.