/** * Copyright 1999-2009 The Pegadi Team * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.pegadi.games.tetris; import org.pegadi.swing.AbstractPreferencesDialog; import org.pegadi.util.GridBagConstraints2; import javax.swing.*; import javax.swing.border.Border; import java.awt.*; import java.util.Properties; import java.util.ResourceBundle; class PrefsDialog extends AbstractPreferencesDialog { /** UI strings. */ protected ResourceBundle str; protected JCheckBox rotateLeftCheck; protected JCheckBox previewCheck; protected JCheckBox scoreCheck; protected JCheckBox showOnlyActiveUsers; protected JTextField dayScoreField; protected JTextField topScoreField; protected JTextField personalScoreField; /** * Create a new dialog with parent and preferences set. */ public PrefsDialog(JFrame parent, Properties preferences) { super(parent, preferences); Dimension size = Toolkit.getDefaultToolkit().getScreenSize(); int x = Math.min(((size.width / 5) *4), 810); int y = Math.min(((size.height / 5) *4), 600); this.setLocation((size.width - x) / 2, (size.height - y) / 2); } /** * Creates the main pane of the dialog. */ protected Component createPreferencesPane() { str = ResourceBundle.getBundle("org.pegadi.games.tetris.PrefsDialogStrings"); Box pane = new Box(BoxLayout.Y_AXIS); Border b; JPanel previewPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); b = BorderFactory.createTitledBorder(str.getString("game_prefs_title")); previewPanel.setBorder(b); previewCheck = new JCheckBox(str.getString("preview_label")); previewPanel.add(previewCheck); rotateLeftCheck = new JCheckBox(str.getString("rotateLeft_label")); previewPanel.add(rotateLeftCheck); pane.add(previewPanel); JPanel scorePanel = new JPanel(new GridBagLayout()); b = BorderFactory.createTitledBorder(str.getString("score_title")); scorePanel.setBorder(b); scoreCheck = new JCheckBox(str.getString("score_label")); scorePanel.add(scoreCheck, new GridBagConstraints2(0, 0, 2, 1, 1.0, 0.0 ,GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0)); showOnlyActiveUsers = new JCheckBox(str.getString("active_users_label")); scorePanel.add(showOnlyActiveUsers, new GridBagConstraints2(0, 1, 2, 1, 1.0, 0.0 ,GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0)); scorePanel.add(new JLabel(str.getString("score_hi_label")), new GridBagConstraints2(0, 2, 1, 1, 0.0, 0.0 ,GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0)); topScoreField = new JTextField(4); scorePanel.add(topScoreField, new GridBagConstraints2(1, 2, 1, 1, 0.0, 0.0 ,GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0)); scorePanel.add(new JLabel(str.getString("score_day_label")), new GridBagConstraints2(0, 3, 1, 1, 0.0, 0.0 ,GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0)); dayScoreField = new JTextField(4); scorePanel.add(dayScoreField, new GridBagConstraints2(1, 3, 1, 1, 0.0, 0.0 ,GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0)); scorePanel.add(new JLabel(str.getString("score_pers_label")), new GridBagConstraints2(0, 4, 1, 1, 0.0, 0.0 ,GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0)); personalScoreField = new JTextField(4); scorePanel.add(personalScoreField, new GridBagConstraints2(1, 4, 1, 1, 0.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0)); pane.add(scorePanel); return pane; } /** * Set preferences controls according to this <code>Properties</code>. */ public void setPreferences(Properties preferences) { prefs = preferences; //to be set soon... don't delete! nils rotateLeftCheck.setSelected(Boolean.valueOf(prefs.getProperty(Tetris.PREFS_ROTATELEFT))); previewCheck.setSelected(Boolean.valueOf(prefs.getProperty(Tetris.PREFS_PREVIEW))); scoreCheck.setSelected(Boolean.valueOf(prefs.getProperty(Tetris.PREFS_SCORES))); showOnlyActiveUsers.setSelected(Boolean.valueOf(prefs.getProperty(Tetris.PREFS_ACTIVE_ONLY))); topScoreField.setText(prefs.getProperty(Tetris.PREFS_HIGH)); dayScoreField.setText(prefs.getProperty(Tetris.PREFS_TODAY)); personalScoreField.setText(prefs.getProperty(Tetris.PREFS_PERS)); } /** * Called when "OK" is pressed to determine which preferences are changed. * All changed preferences will be placed in <code>changedPrefs</code>, which * prior to this call will be <code>null</code>. */ protected void findChangedPreferences() { changedPrefs = new Properties(); String preview = String.valueOf(previewCheck.isSelected()); if (!preview.equals(prefs.getProperty(Tetris.PREFS_PREVIEW))) { changedPrefs.put(Tetris.PREFS_PREVIEW, preview); } String scores = String.valueOf(scoreCheck.isSelected()); if (!scores.equals(prefs.getProperty(Tetris.PREFS_SCORES))) { changedPrefs.put(Tetris.PREFS_SCORES, scores); } String activesOnly = String.valueOf(showOnlyActiveUsers.isSelected()); if(!activesOnly.equals(prefs.getProperty(Tetris.PREFS_ACTIVE_ONLY))) { changedPrefs.put(Tetris.PREFS_ACTIVE_ONLY, activesOnly); } String rotate_left = String.valueOf(rotateLeftCheck.isSelected()); if (!rotate_left.equals(prefs.getProperty(Tetris.PREFS_ROTATELEFT))) { changedPrefs.put(Tetris.PREFS_ROTATELEFT, rotate_left); } String hi = topScoreField.getText(); if (!hi.equals(prefs.getProperty(Tetris.PREFS_HIGH))) { changedPrefs.put(Tetris.PREFS_HIGH, hi); } String today = dayScoreField.getText(); if (!today.equals(prefs.getProperty(Tetris.PREFS_TODAY))) { changedPrefs.put(Tetris.PREFS_TODAY, today); } String pers = personalScoreField.getText(); if (!pers.equals(prefs.getProperty(Tetris.PREFS_PERS))) { changedPrefs.put(Tetris.PREFS_PERS, pers); } } }