SettingsDialog.java 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. /*
  2. * Copyright (c) 2009-2012 jMonkeyEngine
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are
  7. * met:
  8. *
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * * Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  22. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  28. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. package com.jme3.app;
  33. import com.jme3.system.AppSettings;
  34. import java.awt.*;
  35. import java.awt.event.*;
  36. import java.awt.image.BufferedImage;
  37. import java.lang.reflect.Method;
  38. import java.net.MalformedURLException;
  39. import java.net.URL;
  40. import java.util.ArrayList;
  41. import java.util.Arrays;
  42. import java.util.Comparator;
  43. import java.util.List;
  44. import java.util.logging.Level;
  45. import java.util.logging.Logger;
  46. import java.util.prefs.BackingStoreException;
  47. import javax.swing.*;
  48. /**
  49. * <code>PropertiesDialog</code> provides an interface to make use of the
  50. * <code>GameSettings</code> class. The <code>GameSettings</code> object
  51. * is still created by the client application, and passed during construction.
  52. *
  53. * @see AppSettings
  54. * @author Mark Powell
  55. * @author Eric Woroshow
  56. * @author Joshua Slack - reworked for proper use of GL commands.
  57. */
  58. public final class SettingsDialog extends JDialog {
  59. public static interface SelectionListener {
  60. public void onSelection(int selection);
  61. }
  62. private static final Logger logger = Logger.getLogger(SettingsDialog.class.getName());
  63. private static final long serialVersionUID = 1L;
  64. public static final int NO_SELECTION = 0,
  65. APPROVE_SELECTION = 1,
  66. CANCEL_SELECTION = 2;
  67. // connection to properties file.
  68. private final AppSettings source;
  69. // Title Image
  70. private URL imageFile = null;
  71. // Array of supported display modes
  72. private DisplayMode[] modes = null;
  73. // Array of windowed resolutions
  74. private String[] windowedResolutions = {"320 x 240", "640 x 480", "800 x 600",
  75. "1024 x 768", "1152 x 864", "1280 x 720"};
  76. // UI components
  77. private JCheckBox vsyncBox = null;
  78. private JCheckBox fullscreenBox = null;
  79. private JComboBox displayResCombo = null;
  80. private JComboBox colorDepthCombo = null;
  81. private JComboBox displayFreqCombo = null;
  82. // private JComboBox rendererCombo = null;
  83. private JComboBox antialiasCombo = null;
  84. private JLabel icon = null;
  85. private int selection = 0;
  86. private SelectionListener selectionListener = null;
  87. /**
  88. * Constructor for the <code>PropertiesDialog</code>. Creates a
  89. * properties dialog initialized for the primary display.
  90. *
  91. * @param source
  92. * the <code>AppSettings</code> object to use for working with
  93. * the properties file.
  94. * @param imageFile
  95. * the image file to use as the title of the dialog;
  96. * <code>null</code> will result in to image being displayed
  97. * @throws NullPointerException
  98. * if the source is <code>null</code>
  99. */
  100. public SettingsDialog(AppSettings source, String imageFile, boolean loadSettings) {
  101. this(source, getURL(imageFile), loadSettings);
  102. }
  103. /**
  104. * Constructor for the <code>PropertiesDialog</code>. Creates a
  105. * properties dialog initialized for the primary display.
  106. *
  107. * @param source
  108. * the <code>GameSettings</code> object to use for working with
  109. * the properties file.
  110. * @param imageFile
  111. * the image file to use as the title of the dialog;
  112. * <code>null</code> will result in to image being displayed
  113. * @param loadSettings
  114. * @throws JmeException
  115. * if the source is <code>null</code>
  116. */
  117. public SettingsDialog(AppSettings source, URL imageFile, boolean loadSettings) {
  118. if (source == null) {
  119. throw new NullPointerException("Settings source cannot be null");
  120. }
  121. this.source = source;
  122. this.imageFile = imageFile;
  123. // setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
  124. setModal(true);
  125. AppSettings registrySettings = new AppSettings(true);
  126. String appTitle;
  127. if(source.getTitle()!=null){
  128. appTitle = source.getTitle();
  129. }else{
  130. appTitle = registrySettings.getTitle();
  131. }
  132. try {
  133. registrySettings.load(appTitle);
  134. } catch (BackingStoreException ex) {
  135. logger.log(Level.WARNING,
  136. "Failed to load settings", ex);
  137. }
  138. if (loadSettings) {
  139. source.copyFrom(registrySettings);
  140. } else if(!registrySettings.isEmpty()) {
  141. source.mergeFrom(registrySettings);
  142. }
  143. GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
  144. modes = device.getDisplayModes();
  145. Arrays.sort(modes, new DisplayModeSorter());
  146. createUI();
  147. }
  148. public void setSelectionListener(SelectionListener sl) {
  149. this.selectionListener = sl;
  150. }
  151. public int getUserSelection() {
  152. return selection;
  153. }
  154. private void setUserSelection(int selection) {
  155. this.selection = selection;
  156. selectionListener.onSelection(selection);
  157. }
  158. /**
  159. * <code>setImage</code> sets the background image of the dialog.
  160. *
  161. * @param image
  162. * <code>String</code> representing the image file.
  163. */
  164. public void setImage(String image) {
  165. try {
  166. URL file = new URL("file:" + image);
  167. setImage(file);
  168. // We can safely ignore the exception - it just means that the user
  169. // gave us a bogus file
  170. } catch (MalformedURLException e) {
  171. }
  172. }
  173. /**
  174. * <code>setImage</code> sets the background image of this dialog.
  175. *
  176. * @param image
  177. * <code>URL</code> pointing to the image file.
  178. */
  179. public void setImage(URL image) {
  180. icon.setIcon(new ImageIcon(image));
  181. pack(); // Resize to accomodate the new image
  182. setLocationRelativeTo(null); // put in center
  183. }
  184. /**
  185. * <code>showDialog</code> sets this dialog as visble, and brings it to
  186. * the front.
  187. */
  188. public void showDialog() {
  189. setLocationRelativeTo(null);
  190. setVisible(true);
  191. toFront();
  192. }
  193. /**
  194. * <code>init</code> creates the components to use the dialog.
  195. */
  196. private void createUI() {
  197. try {
  198. UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  199. } catch (Exception e) {
  200. logger.warning("Could not set native look and feel.");
  201. }
  202. addWindowListener(new WindowAdapter() {
  203. public void windowClosing(WindowEvent e) {
  204. setUserSelection(CANCEL_SELECTION);
  205. dispose();
  206. }
  207. });
  208. if (source.getIcons() != null) {
  209. safeSetIconImages( (List<BufferedImage>) Arrays.asList((BufferedImage[]) source.getIcons()) );
  210. }
  211. setTitle("Select Display Settings");
  212. // The panels...
  213. JPanel mainPanel = new JPanel();
  214. JPanel centerPanel = new JPanel();
  215. JPanel optionsPanel = new JPanel();
  216. JPanel buttonPanel = new JPanel();
  217. // The buttons...
  218. JButton ok = new JButton("Ok");
  219. JButton cancel = new JButton("Cancel");
  220. icon = new JLabel(imageFile != null ? new ImageIcon(imageFile) : null);
  221. mainPanel.setLayout(new BorderLayout());
  222. centerPanel.setLayout(new BorderLayout());
  223. KeyListener aListener = new KeyAdapter() {
  224. public void keyPressed(KeyEvent e) {
  225. if (e.getKeyCode() == KeyEvent.VK_ENTER) {
  226. if (verifyAndSaveCurrentSelection()) {
  227. setUserSelection(APPROVE_SELECTION);
  228. dispose();
  229. }
  230. }
  231. else if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
  232. setUserSelection(CANCEL_SELECTION);
  233. dispose();
  234. }
  235. }
  236. };
  237. displayResCombo = setUpResolutionChooser();
  238. displayResCombo.addKeyListener(aListener);
  239. colorDepthCombo = new JComboBox();
  240. colorDepthCombo.addKeyListener(aListener);
  241. displayFreqCombo = new JComboBox();
  242. displayFreqCombo.addKeyListener(aListener);
  243. antialiasCombo = new JComboBox();
  244. antialiasCombo.addKeyListener(aListener);
  245. fullscreenBox = new JCheckBox("Fullscreen?");
  246. fullscreenBox.setSelected(source.isFullscreen());
  247. fullscreenBox.addActionListener(new ActionListener() {
  248. public void actionPerformed(ActionEvent e) {
  249. updateResolutionChoices();
  250. }
  251. });
  252. vsyncBox = new JCheckBox("VSync?");
  253. vsyncBox.setSelected(source.isVSync());
  254. // rendererCombo = setUpRendererChooser();
  255. // rendererCombo.addKeyListener(aListener);
  256. updateResolutionChoices();
  257. updateAntialiasChoices();
  258. displayResCombo.setSelectedItem(source.getWidth() + " x " + source.getHeight());
  259. colorDepthCombo.setSelectedItem(source.getBitsPerPixel() + " bpp");
  260. optionsPanel.add(displayResCombo);
  261. optionsPanel.add(colorDepthCombo);
  262. optionsPanel.add(displayFreqCombo);
  263. optionsPanel.add(antialiasCombo);
  264. optionsPanel.add(fullscreenBox);
  265. optionsPanel.add(vsyncBox);
  266. // optionsPanel.add(rendererCombo);
  267. // Set the button action listeners. Cancel disposes without saving, OK
  268. // saves.
  269. ok.addActionListener(new ActionListener() {
  270. public void actionPerformed(ActionEvent e) {
  271. if (verifyAndSaveCurrentSelection()) {
  272. setUserSelection(APPROVE_SELECTION);
  273. dispose();
  274. }
  275. }
  276. });
  277. cancel.addActionListener(new ActionListener() {
  278. public void actionPerformed(ActionEvent e) {
  279. setUserSelection(CANCEL_SELECTION);
  280. dispose();
  281. }
  282. });
  283. buttonPanel.add(ok);
  284. buttonPanel.add(cancel);
  285. if (icon != null) {
  286. centerPanel.add(icon, BorderLayout.NORTH);
  287. }
  288. centerPanel.add(optionsPanel, BorderLayout.SOUTH);
  289. mainPanel.add(centerPanel, BorderLayout.CENTER);
  290. mainPanel.add(buttonPanel, BorderLayout.SOUTH);
  291. this.getContentPane().add(mainPanel);
  292. pack();
  293. }
  294. /* Access JDialog.setIconImages by reflection in case we're running on JRE < 1.6 */
  295. private void safeSetIconImages(List<? extends Image> icons) {
  296. try {
  297. // Due to Java bug 6445278, we try to set icon on our shared owner frame first.
  298. // Otherwise, our alt-tab icon will be the Java default under Windows.
  299. Window owner = getOwner();
  300. if (owner != null) {
  301. Method setIconImages = owner.getClass().getMethod("setIconImages", List.class);
  302. setIconImages.invoke(owner, icons);
  303. return;
  304. }
  305. Method setIconImages = getClass().getMethod("setIconImages", List.class);
  306. setIconImages.invoke(this, icons);
  307. } catch (Exception e) {
  308. return;
  309. }
  310. }
  311. /**
  312. * <code>verifyAndSaveCurrentSelection</code> first verifies that the
  313. * display mode is valid for this system, and then saves the current
  314. * selection as a properties.cfg file.
  315. *
  316. * @return if the selection is valid
  317. */
  318. private boolean verifyAndSaveCurrentSelection() {
  319. String display = (String) displayResCombo.getSelectedItem();
  320. boolean fullscreen = fullscreenBox.isSelected();
  321. boolean vsync = vsyncBox.isSelected();
  322. int width = Integer.parseInt(display.substring(0, display.indexOf(" x ")));
  323. display = display.substring(display.indexOf(" x ") + 3);
  324. int height = Integer.parseInt(display);
  325. String depthString = (String) colorDepthCombo.getSelectedItem();
  326. int depth = -1;
  327. if (depthString.equals("???")) {
  328. depth = 0;
  329. } else {
  330. depth = Integer.parseInt(depthString.substring(0, depthString.indexOf(' ')));
  331. }
  332. String freqString = (String) displayFreqCombo.getSelectedItem();
  333. int freq = -1;
  334. if (fullscreen) {
  335. if (freqString.equals("???")) {
  336. freq = 0;
  337. } else {
  338. freq = Integer.parseInt(freqString.substring(0, freqString.indexOf(' ')));
  339. }
  340. }
  341. String aaString = (String) antialiasCombo.getSelectedItem();
  342. int multisample = -1;
  343. if (aaString.equals("Disabled")) {
  344. multisample = 0;
  345. } else {
  346. multisample = Integer.parseInt(aaString.substring(0, aaString.indexOf('x')));
  347. }
  348. // FIXME: Does not work in Linux
  349. /*
  350. * if (!fullscreen) { //query the current bit depth of the desktop int
  351. * curDepth = GraphicsEnvironment.getLocalGraphicsEnvironment()
  352. * .getDefaultScreenDevice().getDisplayMode().getBitDepth(); if (depth >
  353. * curDepth) { showError(this,"Cannot choose a higher bit depth in
  354. * windowed " + "mode than your current desktop bit depth"); return
  355. * false; } }
  356. */
  357. String renderer = "LWJGL-OpenGL2";//(String) rendererCombo.getSelectedItem();
  358. boolean valid = false;
  359. // test valid display mode when going full screen
  360. if (!fullscreen) {
  361. valid = true;
  362. } else {
  363. GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
  364. valid = device.isFullScreenSupported();
  365. }
  366. if (valid) {
  367. //use the GameSettings class to save it.
  368. source.setWidth(width);
  369. source.setHeight(height);
  370. source.setBitsPerPixel(depth);
  371. source.setFrequency(freq);
  372. source.setFullscreen(fullscreen);
  373. source.setVSync(vsync);
  374. //source.setRenderer(renderer);
  375. source.setSamples(multisample);
  376. String appTitle = source.getTitle();
  377. try {
  378. source.save(appTitle);
  379. } catch (BackingStoreException ex) {
  380. logger.log(Level.WARNING,
  381. "Failed to save setting changes", ex);
  382. }
  383. } else {
  384. showError(
  385. this,
  386. "Your monitor claims to not support the display mode you've selected.\n"
  387. + "The combination of bit depth and refresh rate is not supported.");
  388. }
  389. return valid;
  390. }
  391. /**
  392. * <code>setUpChooser</code> retrieves all available display modes and
  393. * places them in a <code>JComboBox</code>. The resolution specified by
  394. * GameSettings is used as the default value.
  395. *
  396. * @return the combo box of display modes.
  397. */
  398. private JComboBox setUpResolutionChooser() {
  399. String[] res = getResolutions(modes);
  400. JComboBox resolutionBox = new JComboBox(res);
  401. resolutionBox.setSelectedItem(source.getWidth() + " x "
  402. + source.getHeight());
  403. resolutionBox.addActionListener(new ActionListener() {
  404. public void actionPerformed(ActionEvent e) {
  405. updateDisplayChoices();
  406. }
  407. });
  408. return resolutionBox;
  409. }
  410. /**
  411. * <code>setUpRendererChooser</code> sets the list of available renderers.
  412. * Data is obtained from the <code>DisplaySystem</code> class. The
  413. * renderer specified by GameSettings is used as the default value.
  414. *
  415. * @return the list of renderers.
  416. */
  417. private JComboBox setUpRendererChooser() {
  418. String modes[] = {"NULL", "JOGL-OpenGL1", "LWJGL-OpenGL2", "LWJGL-OpenGL3", "LWJGL-OpenGL3.1"};
  419. JComboBox nameBox = new JComboBox(modes);
  420. nameBox.setSelectedItem(source.getRenderer());
  421. return nameBox;
  422. }
  423. /**
  424. * <code>updateDisplayChoices</code> updates the available color depth and
  425. * display frequency options to match the currently selected resolution.
  426. */
  427. private void updateDisplayChoices() {
  428. if (!fullscreenBox.isSelected()) {
  429. // don't run this function when changing windowed settings
  430. return;
  431. }
  432. String resolution = (String) displayResCombo.getSelectedItem();
  433. String colorDepth = (String) colorDepthCombo.getSelectedItem();
  434. if (colorDepth == null) {
  435. colorDepth = source.getBitsPerPixel() + " bpp";
  436. }
  437. String displayFreq = (String) displayFreqCombo.getSelectedItem();
  438. if (displayFreq == null) {
  439. displayFreq = source.getFrequency() + " Hz";
  440. }
  441. // grab available depths
  442. String[] depths = getDepths(resolution, modes);
  443. colorDepthCombo.setModel(new DefaultComboBoxModel(depths));
  444. colorDepthCombo.setSelectedItem(colorDepth);
  445. // grab available frequencies
  446. String[] freqs = getFrequencies(resolution, modes);
  447. displayFreqCombo.setModel(new DefaultComboBoxModel(freqs));
  448. // Try to reset freq
  449. displayFreqCombo.setSelectedItem(displayFreq);
  450. }
  451. /**
  452. * <code>updateResolutionChoices</code> updates the available resolutions
  453. * list to match the currently selected window mode (fullscreen or
  454. * windowed). It then sets up a list of standard options (if windowed) or
  455. * calls <code>updateDisplayChoices</code> (if fullscreen).
  456. */
  457. private void updateResolutionChoices() {
  458. if (!fullscreenBox.isSelected()) {
  459. displayResCombo.setModel(new DefaultComboBoxModel(
  460. windowedResolutions));
  461. colorDepthCombo.setModel(new DefaultComboBoxModel(new String[]{
  462. "24 bpp", "16 bpp"}));
  463. displayFreqCombo.setModel(new DefaultComboBoxModel(
  464. new String[]{"n/a"}));
  465. displayFreqCombo.setEnabled(false);
  466. } else {
  467. displayResCombo.setModel(new DefaultComboBoxModel(
  468. getResolutions(modes)));
  469. displayFreqCombo.setEnabled(true);
  470. updateDisplayChoices();
  471. }
  472. }
  473. private void updateAntialiasChoices() {
  474. // maybe in the future will add support for determining this info
  475. // through pbuffer
  476. String[] choices = new String[]{"Disabled", "2x", "4x", "6x", "8x", "16x"};
  477. antialiasCombo.setModel(new DefaultComboBoxModel(choices));
  478. antialiasCombo.setSelectedItem(choices[Math.min(source.getSamples()/2,5)]);
  479. }
  480. //
  481. // Utility methods
  482. //
  483. /**
  484. * Utility method for converting a String denoting a file into a URL.
  485. *
  486. * @return a URL pointing to the file or null
  487. */
  488. private static URL getURL(String file) {
  489. URL url = null;
  490. try {
  491. url = new URL("file:" + file);
  492. } catch (MalformedURLException e) {
  493. }
  494. return url;
  495. }
  496. private static void showError(java.awt.Component parent, String message) {
  497. JOptionPane.showMessageDialog(parent, message, "Error",
  498. JOptionPane.ERROR_MESSAGE);
  499. }
  500. /**
  501. * Returns every unique resolution from an array of <code>DisplayMode</code>s.
  502. */
  503. private static String[] getResolutions(DisplayMode[] modes) {
  504. ArrayList<String> resolutions = new ArrayList<String>(modes.length);
  505. for (int i = 0; i < modes.length; i++) {
  506. String res = modes[i].getWidth() + " x " + modes[i].getHeight();
  507. if (!resolutions.contains(res)) {
  508. resolutions.add(res);
  509. }
  510. }
  511. String[] res = new String[resolutions.size()];
  512. resolutions.toArray(res);
  513. return res;
  514. }
  515. /**
  516. * Returns every possible bit depth for the given resolution.
  517. */
  518. private static String[] getDepths(String resolution, DisplayMode[] modes) {
  519. ArrayList<String> depths = new ArrayList<String>(4);
  520. for (int i = 0; i < modes.length; i++) {
  521. // Filter out all bit depths lower than 16 - Java incorrectly
  522. // reports
  523. // them as valid depths though the monitor does not support them
  524. if (modes[i].getBitDepth() < 16 && modes[i].getBitDepth() > 0) {
  525. continue;
  526. }
  527. String res = modes[i].getWidth() + " x " + modes[i].getHeight();
  528. String depth = modes[i].getBitDepth() + " bpp";
  529. if (res.equals(resolution) && !depths.contains(depth)) {
  530. depths.add(depth);
  531. }
  532. }
  533. if (depths.size() == 1 && depths.contains("-1 bpp")) {
  534. // add some default depths, possible system is multi-depth supporting
  535. depths.clear();
  536. depths.add("24 bpp");
  537. }
  538. String[] res = new String[depths.size()];
  539. depths.toArray(res);
  540. return res;
  541. }
  542. /**
  543. * Returns every possible refresh rate for the given resolution.
  544. */
  545. private static String[] getFrequencies(String resolution,
  546. DisplayMode[] modes) {
  547. ArrayList<String> freqs = new ArrayList<String>(4);
  548. for (int i = 0; i < modes.length; i++) {
  549. String res = modes[i].getWidth() + " x " + modes[i].getHeight();
  550. String freq;
  551. if (modes[i].getRefreshRate() == DisplayMode.REFRESH_RATE_UNKNOWN) {
  552. freq = "???";
  553. } else {
  554. freq = modes[i].getRefreshRate() + " Hz";
  555. }
  556. if (res.equals(resolution) && !freqs.contains(freq)) {
  557. freqs.add(freq);
  558. }
  559. }
  560. String[] res = new String[freqs.size()];
  561. freqs.toArray(res);
  562. return res;
  563. }
  564. /**
  565. * Utility class for sorting <code>DisplayMode</code>s. Sorts by
  566. * resolution, then bit depth, and then finally refresh rate.
  567. */
  568. private class DisplayModeSorter implements Comparator<DisplayMode> {
  569. /**
  570. * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
  571. */
  572. public int compare(DisplayMode a, DisplayMode b) {
  573. // Width
  574. if (a.getWidth() != b.getWidth()) {
  575. return (a.getWidth() > b.getWidth()) ? 1 : -1;
  576. }
  577. // Height
  578. if (a.getHeight() != b.getHeight()) {
  579. return (a.getHeight() > b.getHeight()) ? 1 : -1;
  580. }
  581. // Bit depth
  582. if (a.getBitDepth() != b.getBitDepth()) {
  583. return (a.getBitDepth() > b.getBitDepth()) ? 1 : -1;
  584. }
  585. // Refresh rate
  586. if (a.getRefreshRate() != b.getRefreshRate()) {
  587. return (a.getRefreshRate() > b.getRefreshRate()) ? 1 : -1;
  588. }
  589. // All fields are equal
  590. return 0;
  591. }
  592. }
  593. }