| 1 | // Jomic - a viewer for comic book archives. |
| 2 | // Copyright (C) 2004-2008 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 | import net.sf.wraplog.Logger; |
| 36 | |
| 37 | /** |
| 38 | * Dialog to change blur related settings. |
| 39 | * |
| 40 | * @author Thomas Aglassinger |
| 41 | */ |
| 42 | public class ChangeBlurSettingsDialog extends JDialog implements PropertyChangeListener |
| 43 | { |
| 44 | private static final int INSET = 4; |
| 45 | private ChoiceSettingButtonGroup blurModeButtonGroup; |
| 46 | private int blurRadiusWhenBecomingVisible; |
| 47 | private int blurThresholdWhenBecomingVisible; |
| 48 | private JPanel controlPane; |
| 49 | private ImageTools imageTools; |
| 50 | private JomicTools jomicTools; |
| 51 | private LocaleTools localeTools; |
| 52 | private Logger logger; |
| 53 | private LimitedIntSettingSlider radiusSlider; |
| 54 | private Settings settings; |
| 55 | private LimitedIntSettingSlider thresholdSlider; |
| 56 | |
| 57 | public ChangeBlurSettingsDialog(JFrame owner) { |
| 58 | super(owner); |
| 59 | logger = Logger.getLogger(ChangeBlurSettingsDialog.class); |
| 60 | imageTools = ImageTools.instance(); |
| 61 | jomicTools = JomicTools.instance(); |
| 62 | localeTools = LocaleTools.instance(); |
| 63 | settings = Settings.instance(); |
| 64 | |
| 65 | setTitle(localeTools.getMessage("dialogs.setBlur.title")); |
| 66 | setModal(true); |
| 67 | setResizable(true); |
| 68 | |
| 69 | controlPane = new JPanel(new GridBagLayout()); |
| 70 | |
| 71 | GridBagConstraints constraints = new GridBagConstraints(); |
| 72 | String modeText = localeTools.getMessage("dialogs.setBlur.mode"); |
| 73 | String radiusText = localeTools.getMessage("dialogs.setBlur.radius"); |
| 74 | String thresholdText = localeTools.getMessage("dialogs.setBlur.threshold"); |
| 75 | |
| 76 | radiusSlider = new LimitedIntSettingSlider(PropertyConstants.BLUR_RADIUS, |
| 77 | Settings.MIN_BLUR_RADIUS, Settings.MAX_BLUR_RADIUS); |
| 78 | radiusSlider.setMinorTickSpacing(1); |
| 79 | radiusSlider.setMajorTickSpacing(3); |
| 80 | radiusSlider.setPaintTicks(true); |
| 81 | radiusSlider.setPaintLabels(true); |
| 82 | thresholdSlider = new LimitedIntSettingSlider(PropertyConstants.BLUR_THRESHOLD, |
| 83 | Settings.MIN_BLUR_THRESHOLD, Settings.MAX_BLUR_THRESHOLD); |
| 84 | thresholdSlider.setMinorTickSpacing(25); |
| 85 | thresholdSlider.setMajorTickSpacing(50); |
| 86 | thresholdSlider.setPaintTicks(true); |
| 87 | thresholdSlider.setPaintLabels(true); |
| 88 | blurModeButtonGroup = new ChoiceSettingButtonGroup( |
| 89 | PropertyConstants.BLUR_MODE, imageTools.getPossibleBlurModes()); |
| 90 | constraints.anchor = GridBagConstraints.LINE_END; |
| 91 | constraints.insets = new Insets(INSET, INSET, INSET / 2, INSET / 2); |
| 92 | constraints.gridx = 0; |
| 93 | constraints.gridy = 0; |
| 94 | controlPane.add(new JLabel(radiusText), constraints); |
| 95 | constraints.anchor = GridBagConstraints.LINE_START; |
| 96 | constraints.gridx += 1; |
| 97 | controlPane.add(radiusSlider, constraints); |
| 98 | constraints.anchor = GridBagConstraints.LINE_END; |
| 99 | constraints.gridx = 0; |
| 100 | constraints.gridy += 1; |
| 101 | controlPane.add(new JLabel(thresholdText), constraints); |
| 102 | constraints.anchor = GridBagConstraints.LINE_START; |
| 103 | constraints.gridx += 1; |
| 104 | controlPane.add(thresholdSlider, constraints); |
| 105 | constraints.anchor = GridBagConstraints.LINE_END; |
| 106 | constraints.gridx = 0; |
| 107 | constraints.gridy += 1; |
| 108 | controlPane.add(new JLabel(modeText), constraints); |
| 109 | constraints.anchor = GridBagConstraints.LINE_START; |
| 110 | constraints.gridx += 1; |
| 111 | controlPane.add(blurModeButtonGroup.asJPanel(), constraints); |
| 112 | } |
| 113 | |
| 114 | public void setVisible(boolean newVisible) { |
| 115 | if (isVisible()) { |
| 116 | if (newVisible) { |
| 117 | logger.warn("ignoring attempt to set already visible dialog to visible"); |
| 118 | } else { |
| 119 | getContentPane().removePropertyChangeListener(this); |
| 120 | super.setVisible(false); |
| 121 | } |
| 122 | } else { |
| 123 | if (newVisible) { |
| 124 | JOptionPane optionPane = new JOptionPane(controlPane, |
| 125 | JOptionPane.QUESTION_MESSAGE, |
| 126 | JOptionPane.OK_CANCEL_OPTION, |
| 127 | null); |
| 128 | |
| 129 | setContentPane(optionPane); |
| 130 | blurRadiusWhenBecomingVisible = settings.getBlurRadius(); |
| 131 | blurThresholdWhenBecomingVisible = settings.getBlurThreshold(); |
| 132 | getContentPane().addPropertyChangeListener(this); |
| 133 | super.setVisible(true); |
| 134 | } else { |
| 135 | logger.warn("ignoring attempt to set already invisible dialog to invisible"); |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | public void dispose() { |
| 141 | if (radiusSlider != null) { |
| 142 | radiusSlider.dispose(); |
| 143 | } |
| 144 | if (thresholdSlider != null) { |
| 145 | thresholdSlider.dispose(); |
| 146 | } |
| 147 | super.dispose(); |
| 148 | } |
| 149 | |
| 150 | public void propertyChange(PropertyChangeEvent event) { |
| 151 | try { |
| 152 | assert event != null; |
| 153 | assert event.getSource() == getContentPane(); |
| 154 | String propertyName = event.getPropertyName(); |
| 155 | |
| 156 | assert propertyName != null; |
| 157 | if (propertyName.equals(JOptionPane.VALUE_PROPERTY)) { |
| 158 | if (isVisible()) { |
| 159 | settings.setComponentAreaProperty(PropertyConstants.GO_TO_PAGE_DIALOG_WINDOW, this); |
| 160 | setVisible(false); |
| 161 | } |
| 162 | |
| 163 | Integer choiceValue = (Integer) event.getNewValue(); |
| 164 | |
| 165 | if (choiceValue != null) { |
| 166 | int choice = choiceValue.intValue(); |
| 167 | |
| 168 | // If canceled, restore settings to the ones used when opening the dialog. |
| 169 | if (choice == JOptionPane.CANCEL_OPTION) { |
| 170 | settings.setBlurRadius(blurRadiusWhenBecomingVisible); |
| 171 | settings.setBlurThreshold(blurThresholdWhenBecomingVisible); |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | } catch (Throwable error) { |
| 176 | jomicTools.showError(event, error); |
| 177 | } |
| 178 | } |
| 179 | } |