Untitled
unknown
java
a year ago
4.5 kB
4
Indexable
Never
/** * Updates the version combo box in the specified {@link VersionGroupBox}. * * <p>It populates the {@link javax.swing.JComboBox} with available versions retrieved from a JSON * resource file and sets the currently selected version from the configuration file as the * selected item. * * @param vgb the {@link VersionGroupBox} that contains the {@link javax.swing.JComboBox} to be * updated */ public static void updateVersionComboBox(VersionGroupBox vgb) { versionMap = new HashMap<>(); DefaultComboBoxModel<String> defaultComboBoxModel = new DefaultComboBoxModel<>(); String fileName = "assets/versions.json"; URL fileUrl = LauncherOptionsUtils.class.getClassLoader().getResource(fileName); InputStream is = Optional.ofNullable( LauncherOptionsUtils.class.getClassLoader().getResourceAsStream(fileName)) .orElseThrow(() -> new NullPointerException("is cannot be null")); try (BufferedReader br = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))) { JSONObject json = new JSONObject(br.lines().collect(Collectors.joining())); List<String> oldLegacy = Collections.unmodifiableList( Arrays.asList("legacy_beta", "legacy_alpha", "legacy_infdev")); List<String> legacy = Collections.singletonList("legacy_release"); legacy.forEach( version -> { JSONArray versionArray = json.getJSONArray(version); IntStream.range(0, versionArray.length()) .mapToObj(versionArray::getJSONObject) .sorted( (o1, o2) -> { String v1 = o1.getString("versionId"); String v2 = o2.getString("versionId"); return -compareVersionIds(v1, v2); }) .collect(Collectors.toList()) .forEach( o -> { String versionId = o.getString("versionId"); String versionName = o.getString("versionName"); versionMap.put(versionName, versionId); defaultComboBoxModel.addElement(versionName); }); }); oldLegacy.forEach( version -> { JSONArray versionArray = json.getJSONArray(version); IntStream.range(0, versionArray.length()) .mapToObj(versionArray::getJSONObject) .sorted( (o1, o2) -> { String v1 = o1.getString("versionId"); String v2 = o2.getString("versionId"); return -compareVersionIds(v1, v2); }) .collect(Collectors.toList()) .forEach( o -> { boolean showBetaVersionsSelected = Boolean.parseBoolean(LauncherConfig.get(1).toString()) && Objects.equals(version, oldLegacy.get(0)); boolean showAlphaVersionsSelected = Boolean.parseBoolean(LauncherConfig.get(2).toString()) && Objects.equals(version, oldLegacy.get(1)); boolean showInfdevVersionsSelected = Boolean.parseBoolean(LauncherConfig.get(3).toString()) && Objects.equals(version, oldLegacy.get(2)); if (showBetaVersionsSelected || showAlphaVersionsSelected || showInfdevVersionsSelected) { String versionId = o.getString("versionId"); String versionName = o.getString("versionName"); versionMap.put(versionName, versionId); defaultComboBoxModel.addElement(versionName); } }); }); } catch (IOException ioe) { LOGGER.error("Cannot read {}", fileUrl, ioe); } Object selectedVersion = LauncherConfig.get(4); versionMap.entrySet().stream() .filter(entry -> entry.getValue().equals(selectedVersion)) .findFirst() .ifPresent(entry -> defaultComboBoxModel.setSelectedItem(entry.getKey())); vgb.getVersionComboBox().setModel(defaultComboBoxModel); }