| 1 | // Jomic - a viewer for comic book archives. |
| 2 | // Copyright (C) 2004-2011 Thomas Aglassinger |
| 3 | // |
| 4 | // This program is free software: you can redistribute it and/or modify |
| 5 | // it under the terms of the GNU General Public License as published by |
| 6 | // the Free Software Foundation, either version 3 of the License, or |
| 7 | // (at your option) any later version. |
| 8 | // |
| 9 | // This program is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | // |
| 14 | // You should have received a copy of the GNU General Public License |
| 15 | // along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | package net.sf.jomic.ui; |
| 17 | |
| 18 | import java.awt.GridBagConstraints; |
| 19 | import java.awt.GridBagLayout; |
| 20 | import java.awt.Insets; |
| 21 | import java.beans.PropertyChangeEvent; |
| 22 | import java.beans.PropertyChangeListener; |
| 23 | |
| 24 | import javax.swing.JDialog; |
| 25 | import javax.swing.JFrame; |
| 26 | import javax.swing.JLabel; |
| 27 | import javax.swing.JOptionPane; |
| 28 | import javax.swing.JPanel; |
| 29 | |
| 30 | import net.sf.jomic.common.JomicTools; |
| 31 | import net.sf.jomic.common.PropertyConstants; |
| 32 | import net.sf.jomic.common.Settings; |
| 33 | import net.sf.jomic.tools.ImageTools; |
| 34 | import net.sf.jomic.tools.LocaleTools; |
| 35 | |
| 36 | import org.apache.commons.logging.Log; |
| 37 | import org.apache.commons.logging.LogFactory; |
| 38 | |
| 39 | /** |
| 40 | * Dialog to change blur related settings. |
| 41 | * |
| 42 | * @author Thomas Aglassinger |
| 43 | */ |
| 44 | public class ChangeBlurSettingsDialog extends JDialog implements PropertyChangeListener |
| 45 | { |
| 46 | private static final int INSET = 4; |
| 47 | private static final int RADIUS_MAJOR_TICK_SPACING = 3; |
| 48 | private static final int RADIUS_MINOR_TICK_SPACING = 1; |
| 49 | private static final int THRESHOLD_MAJOR_TICK_SPACING = 50; |
| 50 | private static final int THRESHOLD_MINOR_TICK_SPACING = 25; |
| 51 | private ChoiceSettingButtonGroup blurModeButtonGroup; |
| 52 | private int blurRadiusWhenBecomingVisible; |
| 53 | private int blurThresholdWhenBecomingVisible; |
| 54 | private JPanel controlPane; |
| 55 | private ImageTools imageTools; |
| 56 | private JomicTools jomicTools; |
| 57 | private LocaleTools localeTools; |
| 58 | private Log logger; |
| 59 | private LimitedIntSettingSlider radiusSlider; |
| 60 | private Settings settings; |
| 61 | private LimitedIntSettingSlider thresholdSlider; |
| 62 | |
| 63 | public ChangeBlurSettingsDialog(JFrame owner) { |
| 64 | super(owner); |
| 65 | logger = LogFactory.getLog(ChangeBlurSettingsDialog.class); |
| 66 | imageTools = ImageTools.instance(); |
| 67 | jomicTools = JomicTools.instance(); |
| 68 | localeTools = LocaleTools.instance(); |
| 69 | settings = Settings.instance(); |
| 70 | |
| 71 | setTitle(localeTools.getMessage("dialogs.setBlur.title")); |
| 72 | setModal(true); |
| 73 | setResizable(true); |
| 74 | |
| 75 | controlPane = new JPanel(new GridBagLayout()); |
| 76 | |
| 77 | GridBagConstraints constraints = new GridBagConstraints(); |
| 78 | String modeText = localeTools.getMessage("dialogs.setBlur.mode"); |
| 79 | String radiusText = localeTools.getMessage("dialogs.setBlur.radius"); |
| 80 | String thresholdText = localeTools.getMessage("dialogs.setBlur.threshold"); |
| 81 | |
| 82 | radiusSlider = new LimitedIntSettingSlider(PropertyConstants.BLUR_RADIUS, |
| 83 | Settings.MIN_BLUR_RADIUS, Settings.MAX_BLUR_RADIUS); |
| 84 | radiusSlider.setMinorTickSpacing(RADIUS_MINOR_TICK_SPACING); |
| 85 | radiusSlider.setMajorTickSpacing(RADIUS_MAJOR_TICK_SPACING); |
| 86 | radiusSlider.setPaintTicks(true); |
| 87 | radiusSlider.setPaintLabels(true); |
| 88 | thresholdSlider = new LimitedIntSettingSlider(PropertyConstants.BLUR_THRESHOLD, |
| 89 | Settings.MIN_BLUR_THRESHOLD, Settings.MAX_BLUR_THRESHOLD); |
| 90 | thresholdSlider.setMinorTickSpacing(THRESHOLD_MINOR_TICK_SPACING); |
| 91 | thresholdSlider.setMajorTickSpacing(THRESHOLD_MAJOR_TICK_SPACING); |
| 92 | thresholdSlider.setPaintTicks(true); |
| 93 | thresholdSlider.setPaintLabels(true); |
| 94 | blurModeButtonGroup = new ChoiceSettingButtonGroup( |
| 95 | PropertyConstants.BLUR_MODE, imageTools.getPossibleBlurModes()); |
| 96 | constraints.anchor = GridBagConstraints.LINE_END; |
| 97 | constraints.insets = new Insets(INSET, INSET, INSET / 2, INSET / 2); |
| 98 | constraints.gridx = 0; |
| 99 | constraints.gridy = 0; |
| 100 | controlPane.add(new JLabel(radiusText), constraints); |
| 101 | constraints.anchor = GridBagConstraints.LINE_START; |
| 102 | constraints.gridx += 1; |
| 103 | controlPane.add(radiusSlider, constraints); |
| 104 | constraints.anchor = GridBagConstraints.LINE_END; |
| 105 | constraints.gridx = 0; |
| 106 | constraints.gridy += 1; |
| 107 | controlPane.add(new JLabel(thresholdText), constraints); |
| 108 | constraints.anchor = GridBagConstraints.LINE_START; |
| 109 | constraints.gridx += 1; |
| 110 | controlPane.add(thresholdSlider, constraints); |
| 111 | constraints.anchor = GridBagConstraints.LINE_END; |
| 112 | constraints.gridx = 0; |
| 113 | constraints.gridy += 1; |
| 114 | controlPane.add(new JLabel(modeText), constraints); |
| 115 | constraints.anchor = GridBagConstraints.LINE_START; |
| 116 | constraints.gridx += 1; |
| 117 | controlPane.add(blurModeButtonGroup.asJPanel(), constraints); |
| 118 | } |
| 119 | |
| 120 | public void setVisible(boolean newVisible) { |
| 121 | if (isVisible()) { |
| 122 | if (newVisible) { |
| 123 | logger.warn("ignoring attempt to set already visible dialog to visible"); |
| 124 | } else { |
| 125 | getContentPane().removePropertyChangeListener(this); |
| 126 | super.setVisible(false); |
| 127 | } |
| 128 | } else { |
| 129 | if (newVisible) { |
| 130 | JOptionPane optionPane = new JOptionPane(controlPane, |
| 131 | JOptionPane.QUESTION_MESSAGE, |
| 132 | JOptionPane.OK_CANCEL_OPTION, |
| 133 | null); |
| 134 | |
| 135 | setContentPane(optionPane); |
| 136 | blurRadiusWhenBecomingVisible = settings.getBlurRadius(); |
| 137 | blurThresholdWhenBecomingVisible = settings.getBlurThreshold(); |
| 138 | getContentPane().addPropertyChangeListener(this); |
| 139 | |
| 140 | // Ensure that the dialog has a sensible size even if it never has been opened before. |
| 141 | if (settings.getProperty(PropertyConstants.SET_BLUR_DIALOG_WINDOW) == null) { |
| 142 | pack(); |
| 143 | } |
| 144 | |
| 145 | super.setVisible(true); |
| 146 | } else { |
| 147 | logger.warn("ignoring attempt to set already invisible dialog to invisible"); |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | public void dispose() { |
| 153 | if (radiusSlider != null) { |
| 154 | radiusSlider.dispose(); |
| 155 | } |
| 156 | if (thresholdSlider != null) { |
| 157 | thresholdSlider.dispose(); |
| 158 | } |
| 159 | super.dispose(); |
| 160 | } |
| 161 | |
| 162 | public void propertyChange(PropertyChangeEvent event) { |
| 163 | try { |
| 164 | assert event != null; |
| 165 | assert event.getSource() == getContentPane(); |
| 166 | String propertyName = event.getPropertyName(); |
| 167 | |
| 168 | assert propertyName != null; |
| 169 | if (propertyName.equals(JOptionPane.VALUE_PROPERTY)) { |
| 170 | if (isVisible()) { |
| 171 | settings.setComponentAreaProperty(PropertyConstants.SET_BLUR_DIALOG_WINDOW, this); |
| 172 | setVisible(false); |
| 173 | } |
| 174 | |
| 175 | Integer choiceValue = (Integer) event.getNewValue(); |
| 176 | |
| 177 | if (choiceValue != null) { |
| 178 | int choice = choiceValue.intValue(); |
| 179 | |
| 180 | // If canceled, restore settings to the ones used when opening the dialog. |
| 181 | if (choice == JOptionPane.CANCEL_OPTION) { |
| 182 | settings.setBlurRadius(blurRadiusWhenBecomingVisible); |
| 183 | settings.setBlurThreshold(blurThresholdWhenBecomingVisible); |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | } catch (Throwable error) { |
| 188 | jomicTools.showError(event, error); |
| 189 | } |
| 190 | } |
| 191 | } |