SettingsDialog.java 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  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.text.MessageFormat;
  41. import java.util.ArrayList;
  42. import java.util.Arrays;
  43. import java.util.Comparator;
  44. import java.util.List;
  45. import java.util.ResourceBundle;
  46. import java.util.logging.Level;
  47. import java.util.logging.Logger;
  48. import java.util.prefs.BackingStoreException;
  49. import javax.swing.*;
  50. /**
  51. * <code>PropertiesDialog</code> provides an interface to make use of the
  52. * <code>GameSettings</code> class. The <code>GameSettings</code> object
  53. * is still created by the client application, and passed during construction.
  54. *
  55. * @see AppSettings
  56. * @author Mark Powell
  57. * @author Eric Woroshow
  58. * @author Joshua Slack - reworked for proper use of GL commands.
  59. */
  60. public final class SettingsDialog extends JDialog {
  61. public static interface SelectionListener {
  62. public void onSelection(int selection);
  63. }
  64. private static final Logger logger = Logger.getLogger(SettingsDialog.class.getName());
  65. private static final long serialVersionUID = 1L;
  66. public static final int NO_SELECTION = 0,
  67. APPROVE_SELECTION = 1,
  68. CANCEL_SELECTION = 2;
  69. // Resource bundle for i18n.
  70. ResourceBundle resourceBundle = ResourceBundle.getBundle("com.jme3.app/SettingsDialog");
  71. // connection to properties file.
  72. private final AppSettings source;
  73. // Title Image
  74. private URL imageFile = null;
  75. // Array of supported display modes
  76. private DisplayMode[] modes = null;
  77. // UI components
  78. private JCheckBox vsyncBox = null;
  79. private JCheckBox fullscreenBox = null;
  80. private JComboBox displayResCombo = null;
  81. private JComboBox colorDepthCombo = null;
  82. private JComboBox displayFreqCombo = null;
  83. private JComboBox antialiasCombo = null;
  84. private JLabel icon = null;
  85. private int selection = 0;
  86. private SelectionListener selectionListener = null;
  87. private int minWidth = 0;
  88. private int minHeight = 0;
  89. /**
  90. * Constructor for the <code>PropertiesDialog</code>. Creates a
  91. * properties dialog initialized for the primary display.
  92. *
  93. * @param source
  94. * the <code>AppSettings</code> object to use for working with
  95. * the properties file.
  96. * @param imageFile
  97. * the image file to use as the title of the dialog;
  98. * <code>null</code> will result in to image being displayed
  99. * @throws NullPointerException
  100. * if the source is <code>null</code>
  101. */
  102. public SettingsDialog(AppSettings source, String imageFile, boolean loadSettings) {
  103. this(source, getURL(imageFile), loadSettings);
  104. }
  105. /**
  106. * Constructor for the <code>PropertiesDialog</code>. Creates a
  107. * properties dialog initialized for the primary display.
  108. *
  109. * @param source
  110. * the <code>GameSettings</code> object to use for working with
  111. * the properties file.
  112. * @param imageFile
  113. * the image file to use as the title of the dialog;
  114. * <code>null</code> will result in to image being displayed
  115. * @param loadSettings
  116. * @throws JmeException
  117. * if the source is <code>null</code>
  118. */
  119. public SettingsDialog(AppSettings source, URL imageFile, boolean loadSettings) {
  120. if (source == null) {
  121. throw new NullPointerException("Settings source cannot be null");
  122. }
  123. this.source = source;
  124. this.imageFile = imageFile;
  125. setModal(true);
  126. setAlwaysOnTop(true);
  127. setResizable(false);
  128. AppSettings registrySettings = new AppSettings(true);
  129. String appTitle;
  130. if(source.getTitle()!=null){
  131. appTitle = source.getTitle();
  132. }else{
  133. appTitle = registrySettings.getTitle();
  134. }
  135. minWidth = source.getMinWidth();
  136. minHeight = source.getMinHeight();
  137. try {
  138. registrySettings.load(appTitle);
  139. } catch (BackingStoreException ex) {
  140. logger.log(Level.WARNING,
  141. "Failed to load settings", ex);
  142. }
  143. if (loadSettings) {
  144. source.copyFrom(registrySettings);
  145. } else if(!registrySettings.isEmpty()) {
  146. source.mergeFrom(registrySettings);
  147. }
  148. GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
  149. modes = device.getDisplayModes();
  150. Arrays.sort(modes, new DisplayModeSorter());
  151. createUI();
  152. }
  153. public void setSelectionListener(SelectionListener sl) {
  154. this.selectionListener = sl;
  155. }
  156. public int getUserSelection() {
  157. return selection;
  158. }
  159. private void setUserSelection(int selection) {
  160. this.selection = selection;
  161. selectionListener.onSelection(selection);
  162. }
  163. public int getMinWidth() {
  164. return minWidth;
  165. }
  166. public void setMinWidth(int minWidth) {
  167. this.minWidth = minWidth;
  168. }
  169. public int getMinHeight() {
  170. return minHeight;
  171. }
  172. public void setMinHeight(int minHeight) {
  173. this.minHeight = minHeight;
  174. }
  175. /**
  176. * <code>setImage</code> sets the background image of the dialog.
  177. *
  178. * @param image
  179. * <code>String</code> representing the image file.
  180. */
  181. public void setImage(String image) {
  182. try {
  183. URL file = new URL("file:" + image);
  184. setImage(file);
  185. // We can safely ignore the exception - it just means that the user
  186. // gave us a bogus file
  187. } catch (MalformedURLException e) {
  188. }
  189. }
  190. /**
  191. * <code>setImage</code> sets the background image of this dialog.
  192. *
  193. * @param image
  194. * <code>URL</code> pointing to the image file.
  195. */
  196. public void setImage(URL image) {
  197. icon.setIcon(new ImageIcon(image));
  198. pack(); // Resize to accomodate the new image
  199. setLocationRelativeTo(null); // put in center
  200. }
  201. /**
  202. * <code>showDialog</code> sets this dialog as visble, and brings it to
  203. * the front.
  204. */
  205. public void showDialog() {
  206. setLocationRelativeTo(null);
  207. setVisible(true);
  208. toFront();
  209. }
  210. /**
  211. * <code>init</code> creates the components to use the dialog.
  212. */
  213. private void createUI() {
  214. GridBagConstraints gbc;
  215. JPanel mainPanel = new JPanel(new GridBagLayout());
  216. try {
  217. UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  218. } catch (Exception e) {
  219. logger.warning("Could not set native look and feel.");
  220. }
  221. addWindowListener(new WindowAdapter() {
  222. @Override
  223. public void windowClosing(WindowEvent e) {
  224. setUserSelection(CANCEL_SELECTION);
  225. dispose();
  226. }
  227. });
  228. if (source.getIcons() != null) {
  229. safeSetIconImages( (List<BufferedImage>) Arrays.asList((BufferedImage[]) source.getIcons()) );
  230. }
  231. setTitle(MessageFormat.format(resourceBundle.getString("frame.title"), source.getTitle()));
  232. // The buttons...
  233. JButton ok = new JButton(resourceBundle.getString("button.ok"));
  234. JButton cancel = new JButton(resourceBundle.getString("button.cancel"));
  235. icon = new JLabel(imageFile != null ? new ImageIcon(imageFile) : null);
  236. KeyListener aListener = new KeyAdapter() {
  237. @Override
  238. public void keyPressed(KeyEvent e) {
  239. if (e.getKeyCode() == KeyEvent.VK_ENTER) {
  240. if (verifyAndSaveCurrentSelection()) {
  241. setUserSelection(APPROVE_SELECTION);
  242. dispose();
  243. }
  244. }
  245. else if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
  246. setUserSelection(CANCEL_SELECTION);
  247. dispose();
  248. }
  249. }
  250. };
  251. displayResCombo = setUpResolutionChooser();
  252. displayResCombo.addKeyListener(aListener);
  253. colorDepthCombo = new JComboBox();
  254. colorDepthCombo.addKeyListener(aListener);
  255. displayFreqCombo = new JComboBox();
  256. displayFreqCombo.addKeyListener(aListener);
  257. antialiasCombo = new JComboBox();
  258. antialiasCombo.addKeyListener(aListener);
  259. fullscreenBox = new JCheckBox(resourceBundle.getString("checkbox.fullscreen"));
  260. fullscreenBox.setSelected(source.isFullscreen());
  261. fullscreenBox.addActionListener(new ActionListener() {
  262. public void actionPerformed(ActionEvent e) {
  263. updateResolutionChoices();
  264. }
  265. });
  266. vsyncBox = new JCheckBox(resourceBundle.getString("checkbox.vsync"));
  267. vsyncBox.setSelected(source.isVSync());
  268. gbc = new GridBagConstraints();
  269. gbc.weightx = 0.5;
  270. gbc.gridx = 0;
  271. gbc.gridwidth = 2;
  272. gbc.gridy = 1;
  273. gbc.anchor = GridBagConstraints.EAST;
  274. mainPanel.add(fullscreenBox, gbc);
  275. gbc = new GridBagConstraints();
  276. gbc.weightx = 0.5;
  277. gbc.insets = new Insets(4, 16, 0, 4);
  278. gbc.gridx = 2;
  279. gbc.gridwidth = 2;
  280. gbc.gridy = 1;
  281. gbc.anchor = GridBagConstraints.WEST;
  282. mainPanel.add(vsyncBox, gbc);
  283. gbc = new GridBagConstraints();
  284. gbc.insets = new Insets(4, 4, 4, 4);
  285. gbc.gridx = 0;
  286. gbc.gridy = 2;
  287. gbc.anchor = GridBagConstraints.EAST;
  288. gbc.weightx = 0.5;
  289. mainPanel.add(new JLabel(resourceBundle.getString("label.resolutions")), gbc);
  290. gbc = new GridBagConstraints();
  291. gbc.gridx = 1;
  292. gbc.gridy = 2;
  293. gbc.anchor = GridBagConstraints.WEST;
  294. mainPanel.add(displayResCombo, gbc);
  295. gbc = new GridBagConstraints();
  296. gbc.insets = new Insets(4, 16, 4, 4);
  297. gbc.gridx = 2;
  298. gbc.gridy = 2;
  299. gbc.anchor = GridBagConstraints.EAST;
  300. mainPanel.add(new JLabel(resourceBundle.getString("label.colordepth")), gbc);
  301. gbc = new GridBagConstraints();
  302. gbc.weightx = 0.5;
  303. gbc.gridx = 3;
  304. gbc.gridy = 2;
  305. gbc.anchor = GridBagConstraints.WEST;
  306. mainPanel.add(colorDepthCombo, gbc);
  307. gbc = new GridBagConstraints();
  308. gbc.insets = new Insets(4, 4, 4, 4);
  309. gbc.weightx = 0.5;
  310. gbc.gridx = 0;
  311. gbc.gridy = 3;
  312. gbc.anchor = GridBagConstraints.EAST;
  313. mainPanel.add(new JLabel(resourceBundle.getString("label.refresh")), gbc);
  314. gbc = new GridBagConstraints();
  315. gbc.gridx = 1;
  316. gbc.gridy = 3;
  317. gbc.anchor = GridBagConstraints.WEST;
  318. mainPanel.add(displayFreqCombo, gbc);
  319. gbc = new GridBagConstraints();
  320. gbc.insets = new Insets(4, 16, 4, 4);
  321. gbc.gridx = 2;
  322. gbc.gridy = 3;
  323. gbc.anchor = GridBagConstraints.EAST;
  324. mainPanel.add(new JLabel(resourceBundle.getString("label.antialias")), gbc);
  325. gbc = new GridBagConstraints();
  326. gbc.weightx = 0.5;
  327. gbc.gridx = 3;
  328. gbc.gridy = 3;
  329. gbc.anchor = GridBagConstraints.WEST;
  330. mainPanel.add(antialiasCombo, gbc);
  331. // Set the button action listeners. Cancel disposes without saving, OK
  332. // saves.
  333. ok.addActionListener(new ActionListener() {
  334. public void actionPerformed(ActionEvent e) {
  335. if (verifyAndSaveCurrentSelection()) {
  336. setUserSelection(APPROVE_SELECTION);
  337. dispose();
  338. }
  339. }
  340. });
  341. cancel.addActionListener(new ActionListener() {
  342. public void actionPerformed(ActionEvent e) {
  343. setUserSelection(CANCEL_SELECTION);
  344. dispose();
  345. }
  346. });
  347. gbc = new GridBagConstraints();
  348. gbc.gridx = 0;
  349. gbc.gridwidth = 2;
  350. gbc.gridy = 4;
  351. gbc.anchor = GridBagConstraints.EAST;
  352. mainPanel.add(ok, gbc);
  353. gbc = new GridBagConstraints();
  354. gbc.insets = new Insets(4, 16, 4, 4);
  355. gbc.gridx = 2;
  356. gbc.gridwidth = 2;
  357. gbc.gridy = 4;
  358. gbc.anchor = GridBagConstraints.WEST;
  359. mainPanel.add(cancel, gbc);
  360. if (icon != null) {
  361. gbc = new GridBagConstraints();
  362. gbc.gridwidth = 4;
  363. mainPanel.add(icon, gbc);
  364. }
  365. this.getContentPane().add(mainPanel);
  366. pack();
  367. SwingUtilities.invokeLater(new Runnable() {
  368. public void run() {
  369. // Fill in the combos once the window has opened so that the insets can be read.
  370. // The assumption is made that the settings window and the display window will have the
  371. // same insets as that is used to resize the "full screen windowed" mode appropriately.
  372. updateResolutionChoices();
  373. if (source.getWidth() != 0 && source.getHeight() != 0) {
  374. displayResCombo.setSelectedItem(source.getWidth() + " x "
  375. + source.getHeight());
  376. } else {
  377. displayResCombo.setSelectedIndex(displayResCombo.getItemCount()-1);
  378. }
  379. updateAntialiasChoices();
  380. colorDepthCombo.setSelectedItem(source.getBitsPerPixel() + " bpp");
  381. }
  382. });
  383. }
  384. /* Access JDialog.setIconImages by reflection in case we're running on JRE < 1.6 */
  385. private void safeSetIconImages(List<? extends Image> icons) {
  386. try {
  387. // Due to Java bug 6445278, we try to set icon on our shared owner frame first.
  388. // Otherwise, our alt-tab icon will be the Java default under Windows.
  389. Window owner = getOwner();
  390. if (owner != null) {
  391. Method setIconImages = owner.getClass().getMethod("setIconImages", List.class);
  392. setIconImages.invoke(owner, icons);
  393. return;
  394. }
  395. Method setIconImages = getClass().getMethod("setIconImages", List.class);
  396. setIconImages.invoke(this, icons);
  397. } catch (Exception e) {
  398. return;
  399. }
  400. }
  401. /**
  402. * <code>verifyAndSaveCurrentSelection</code> first verifies that the
  403. * display mode is valid for this system, and then saves the current
  404. * selection as a properties.cfg file.
  405. *
  406. * @return if the selection is valid
  407. */
  408. private boolean verifyAndSaveCurrentSelection() {
  409. String display = (String) displayResCombo.getSelectedItem();
  410. boolean fullscreen = fullscreenBox.isSelected();
  411. boolean vsync = vsyncBox.isSelected();
  412. int width = Integer.parseInt(display.substring(0, display.indexOf(" x ")));
  413. display = display.substring(display.indexOf(" x ") + 3);
  414. int height = Integer.parseInt(display);
  415. String depthString = (String) colorDepthCombo.getSelectedItem();
  416. int depth = -1;
  417. if (depthString.equals("???")) {
  418. depth = 0;
  419. } else {
  420. depth = Integer.parseInt(depthString.substring(0, depthString.indexOf(' ')));
  421. }
  422. String freqString = (String) displayFreqCombo.getSelectedItem();
  423. int freq = -1;
  424. if (fullscreen) {
  425. if (freqString.equals("???")) {
  426. freq = 0;
  427. } else {
  428. freq = Integer.parseInt(freqString.substring(0, freqString.indexOf(' ')));
  429. }
  430. }
  431. String aaString = (String) antialiasCombo.getSelectedItem();
  432. int multisample = -1;
  433. if (aaString.equals(resourceBundle.getString("antialias.disabled"))) {
  434. multisample = 0;
  435. } else {
  436. multisample = Integer.parseInt(aaString.substring(0, aaString.indexOf('x')));
  437. }
  438. // FIXME: Does not work in Linux
  439. /*
  440. * if (!fullscreen) { //query the current bit depth of the desktop int
  441. * curDepth = GraphicsEnvironment.getLocalGraphicsEnvironment()
  442. * .getDefaultScreenDevice().getDisplayMode().getBitDepth(); if (depth >
  443. * curDepth) { showError(this,"Cannot choose a higher bit depth in
  444. * windowed " + "mode than your current desktop bit depth"); return
  445. * false; } }
  446. */
  447. boolean valid = false;
  448. // test valid display mode when going full screen
  449. if (!fullscreen) {
  450. valid = true;
  451. } else {
  452. GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
  453. valid = device.isFullScreenSupported();
  454. }
  455. if (valid) {
  456. //use the GameSettings class to save it.
  457. source.setWidth(width);
  458. source.setHeight(height);
  459. source.setBitsPerPixel(depth);
  460. source.setFrequency(freq);
  461. source.setFullscreen(fullscreen);
  462. source.setVSync(vsync);
  463. //source.setRenderer(renderer);
  464. source.setSamples(multisample);
  465. String appTitle = source.getTitle();
  466. try {
  467. source.save(appTitle);
  468. } catch (BackingStoreException ex) {
  469. logger.log(Level.WARNING,
  470. "Failed to save setting changes", ex);
  471. }
  472. } else {
  473. showError(
  474. this,
  475. resourceBundle.getString("error.unsupportedmode"));
  476. }
  477. return valid;
  478. }
  479. /**
  480. * <code>setUpChooser</code> retrieves all available display modes and
  481. * places them in a <code>JComboBox</code>. The resolution specified by
  482. * GameSettings is used as the default value.
  483. *
  484. * @return the combo box of display modes.
  485. */
  486. private JComboBox setUpResolutionChooser() {
  487. JComboBox resolutionBox = new JComboBox();
  488. resolutionBox.addActionListener(new ActionListener() {
  489. public void actionPerformed(ActionEvent e) {
  490. updateDisplayChoices();
  491. }
  492. });
  493. return resolutionBox;
  494. }
  495. /**
  496. * <code>updateDisplayChoices</code> updates the available color depth and
  497. * display frequency options to match the currently selected resolution.
  498. */
  499. private void updateDisplayChoices() {
  500. if (!fullscreenBox.isSelected()) {
  501. // don't run this function when changing windowed settings
  502. return;
  503. }
  504. String resolution = (String) displayResCombo.getSelectedItem();
  505. String colorDepth = (String) colorDepthCombo.getSelectedItem();
  506. if (colorDepth == null) {
  507. colorDepth = source.getBitsPerPixel() + " bpp";
  508. }
  509. String displayFreq = (String) displayFreqCombo.getSelectedItem();
  510. if (displayFreq == null) {
  511. displayFreq = source.getFrequency() + " Hz";
  512. }
  513. // grab available depths
  514. String[] depths = getDepths(resolution, modes);
  515. colorDepthCombo.setModel(new DefaultComboBoxModel(depths));
  516. colorDepthCombo.setSelectedItem(colorDepth);
  517. // grab available frequencies
  518. String[] freqs = getFrequencies(resolution, modes);
  519. displayFreqCombo.setModel(new DefaultComboBoxModel(freqs));
  520. // Try to reset freq
  521. displayFreqCombo.setSelectedItem(displayFreq);
  522. }
  523. /**
  524. * <code>updateResolutionChoices</code> updates the available resolutions
  525. * list to match the currently selected window mode (fullscreen or
  526. * windowed). It then sets up a list of standard options (if windowed) or
  527. * calls <code>updateDisplayChoices</code> (if fullscreen).
  528. */
  529. private void updateResolutionChoices() {
  530. if (!fullscreenBox.isSelected()) {
  531. displayResCombo.setModel(new DefaultComboBoxModel(
  532. getWindowedResolutions(modes)));
  533. colorDepthCombo.setModel(new DefaultComboBoxModel(new String[]{
  534. "24 bpp", "16 bpp"}));
  535. displayFreqCombo.setModel(new DefaultComboBoxModel(
  536. new String[]{resourceBundle.getString("refresh.na")}));
  537. displayFreqCombo.setEnabled(false);
  538. } else {
  539. displayResCombo.setModel(new DefaultComboBoxModel(
  540. getResolutions(modes, Integer.MAX_VALUE)));
  541. displayFreqCombo.setEnabled(true);
  542. updateDisplayChoices();
  543. }
  544. }
  545. private void updateAntialiasChoices() {
  546. // maybe in the future will add support for determining this info
  547. // through pbuffer
  548. String[] choices = new String[]{resourceBundle.getString("antialias.disabled"), "2x", "4x", "6x", "8x", "16x"};
  549. antialiasCombo.setModel(new DefaultComboBoxModel(choices));
  550. antialiasCombo.setSelectedItem(choices[Math.min(source.getSamples()/2,5)]);
  551. }
  552. //
  553. // Utility methods
  554. //
  555. /**
  556. * Utility method for converting a String denoting a file into a URL.
  557. *
  558. * @return a URL pointing to the file or null
  559. */
  560. private static URL getURL(String file) {
  561. URL url = null;
  562. try {
  563. url = new URL("file:" + file);
  564. } catch (MalformedURLException e) {
  565. }
  566. return url;
  567. }
  568. private static void showError(java.awt.Component parent, String message) {
  569. JOptionPane.showMessageDialog(parent, message, "Error",
  570. JOptionPane.ERROR_MESSAGE);
  571. }
  572. /**
  573. * Returns every unique resolution from an array of <code>DisplayMode</code>s
  574. * where the resolution is greater than the configured minimums.
  575. */
  576. private String[] getResolutions(DisplayMode[] modes, int heightLimit) {
  577. ArrayList<String> resolutions = new ArrayList<String>(modes.length);
  578. for (int i = 0; i < modes.length; i++) {
  579. int height = modes[i].getHeight();
  580. int width = modes[i].getWidth();
  581. if (width >= minWidth && height >= minHeight && height <= heightLimit) {
  582. if (height == heightLimit) {
  583. // If height is equal to height limit then subtract the size of the window frame and use that
  584. Insets insets = getInsets();
  585. height -= insets.top + insets.bottom;
  586. width -= insets.left + insets.right;
  587. }
  588. String res = width + " x " + height;
  589. if (!resolutions.contains(res)) {
  590. resolutions.add(res);
  591. }
  592. }
  593. }
  594. String[] res = new String[resolutions.size()];
  595. resolutions.toArray(res);
  596. return res;
  597. }
  598. /**
  599. * Returns every unique resolution from an array of <code>DisplayMode</code>s
  600. * where the resolution is greater than the configured minimums and the height
  601. * is less than the current screen resolution.
  602. */
  603. private String[] getWindowedResolutions(DisplayMode[] modes) {
  604. int maxHeight = 0;
  605. for (int i = 0; i < modes.length; i++) {
  606. if (maxHeight < modes[i].getHeight()) {
  607. maxHeight = modes[i].getHeight();
  608. }
  609. }
  610. return getResolutions(modes, maxHeight);
  611. }
  612. /**
  613. * Returns every possible bit depth for the given resolution.
  614. */
  615. private static String[] getDepths(String resolution, DisplayMode[] modes) {
  616. ArrayList<String> depths = new ArrayList<String>(4);
  617. for (int i = 0; i < modes.length; i++) {
  618. // Filter out all bit depths lower than 16 - Java incorrectly
  619. // reports
  620. // them as valid depths though the monitor does not support them
  621. if (modes[i].getBitDepth() < 16 && modes[i].getBitDepth() > 0) {
  622. continue;
  623. }
  624. String res = modes[i].getWidth() + " x " + modes[i].getHeight();
  625. String depth = modes[i].getBitDepth() + " bpp";
  626. if (res.equals(resolution) && !depths.contains(depth)) {
  627. depths.add(depth);
  628. }
  629. }
  630. if (depths.size() == 1 && depths.contains("-1 bpp")) {
  631. // add some default depths, possible system is multi-depth supporting
  632. depths.clear();
  633. depths.add("24 bpp");
  634. }
  635. String[] res = new String[depths.size()];
  636. depths.toArray(res);
  637. return res;
  638. }
  639. /**
  640. * Returns every possible refresh rate for the given resolution.
  641. */
  642. private static String[] getFrequencies(String resolution,
  643. DisplayMode[] modes) {
  644. ArrayList<String> freqs = new ArrayList<String>(4);
  645. for (int i = 0; i < modes.length; i++) {
  646. String res = modes[i].getWidth() + " x " + modes[i].getHeight();
  647. String freq;
  648. if (modes[i].getRefreshRate() == DisplayMode.REFRESH_RATE_UNKNOWN) {
  649. freq = "???";
  650. } else {
  651. freq = modes[i].getRefreshRate() + " Hz";
  652. }
  653. if (res.equals(resolution) && !freqs.contains(freq)) {
  654. freqs.add(freq);
  655. }
  656. }
  657. String[] res = new String[freqs.size()];
  658. freqs.toArray(res);
  659. return res;
  660. }
  661. /**
  662. * Utility class for sorting <code>DisplayMode</code>s. Sorts by
  663. * resolution, then bit depth, and then finally refresh rate.
  664. */
  665. private class DisplayModeSorter implements Comparator<DisplayMode> {
  666. /**
  667. * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
  668. */
  669. public int compare(DisplayMode a, DisplayMode b) {
  670. // Width
  671. if (a.getWidth() != b.getWidth()) {
  672. return (a.getWidth() > b.getWidth()) ? 1 : -1;
  673. }
  674. // Height
  675. if (a.getHeight() != b.getHeight()) {
  676. return (a.getHeight() > b.getHeight()) ? 1 : -1;
  677. }
  678. // Bit depth
  679. if (a.getBitDepth() != b.getBitDepth()) {
  680. return (a.getBitDepth() > b.getBitDepth()) ? 1 : -1;
  681. }
  682. // Refresh rate
  683. if (a.getRefreshRate() != b.getRefreshRate()) {
  684. return (a.getRefreshRate() > b.getRefreshRate()) ? 1 : -1;
  685. }
  686. // All fields are equal
  687. return 0;
  688. }
  689. }
  690. }