| 1 | // Copyright 2004-2007 Jean-Francois Poilpret |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package net.sourceforge.hiveboard.dialog; |
| 16 | |
| 17 | import java.awt.BasicStroke; |
| 18 | import java.awt.Dimension; |
| 19 | |
| 20 | import javax.swing.JLabel; |
| 21 | import javax.swing.JSpinner; |
| 22 | import javax.swing.SpinnerNumberModel; |
| 23 | |
| 24 | import net.sourceforge.hiveboard.drawing.HiliterPrefs; |
| 25 | import net.sourceforge.hivegui.component.AlphaColorChooser; |
| 26 | import net.sourceforge.hivegui.component.ColorButton; |
| 27 | import net.sourceforge.hivegui.dialog.AbstractTabPanel; |
| 28 | import net.sourceforge.hiveutils.service.PreferencesManager; |
| 29 | |
| 30 | import zappini.designgridlayout.DesignGridLayout; |
| 31 | import zappini.designgridlayout.Row; |
| 32 | |
| 33 | public class HiliterPrefsPanel extends AbstractTabPanel |
| 34 | { |
| 35 | static private final String NAME = "preferences-hilite"; |
| 36 | |
| 37 | public HiliterPrefsPanel(PreferencesManager manager, AlphaColorChooser chooser) |
| 38 | { |
| 39 | super(NAME); |
| 40 | |
| 41 | _manager = manager; |
| 42 | _hiliterPrefs = (HiliterPrefs) _manager.read(HiliterPrefs.HILITER_PREFS_NAME); |
| 43 | _btnColor = new ColorButton(chooser); |
| 44 | |
| 45 | // Set component names (for i18n & GUI testing references) |
| 46 | _lblColor.setName(NAME + "-color-label"); |
| 47 | _btnColor.setName(NAME + "-color"); |
| 48 | _lblSize.setName(NAME + "-size-label"); |
| 49 | _txfSize.setName(NAME + "-size"); |
| 50 | |
| 51 | initComponents(); |
| 52 | layoutComponents(); |
| 53 | reset(); |
| 54 | } |
| 55 | |
| 56 | private void initComponents() |
| 57 | { |
| 58 | _lblSize.setLabelFor(_txfSize); |
| 59 | |
| 60 | Dimension size = new Dimension( _btnColor.getPreferredSize().width, |
| 61 | _txfSize.getPreferredSize().height); |
| 62 | if (size.width == 0) |
| 63 | { |
| 64 | size.width = size.height; |
| 65 | } |
| 66 | _btnColor.setPreferredSize(size); |
| 67 | _btnColor.setMaximumSize(size); |
| 68 | } |
| 69 | |
| 70 | // CSOFF: MagicNumberCheck |
| 71 | public void layoutComponents() |
| 72 | { |
| 73 | DesignGridLayout layout = new DesignGridLayout(this); |
| 74 | setLayout(layout); |
| 75 | |
| 76 | layout.row().label(_lblColor).add(_btnColor).add(Row.EMPTY, 3); |
| 77 | layout.row().label(_lblSize).add(_txfSize).add(Row.EMPTY, 3); |
| 78 | } |
| 79 | // CSON: MagicNumberCheck |
| 80 | |
| 81 | @Override public void reset() |
| 82 | { |
| 83 | // Copy all prefs content to the matching fields in the panel |
| 84 | _btnColor.setApplication(_application); |
| 85 | _btnColor.setColor(_hiliterPrefs.getColor()); |
| 86 | _txfSize.setValue(new Double(_hiliterPrefs.getStroke().getLineWidth())); |
| 87 | } |
| 88 | |
| 89 | public void accept() |
| 90 | { |
| 91 | // Copy all fields in the panel to the matching prefs content |
| 92 | _hiliterPrefs.setColor(_btnColor.getColor()); |
| 93 | BasicStroke stroke = _hiliterPrefs.getStroke(); |
| 94 | stroke = new BasicStroke( ((Number) _txfSize.getValue()).floatValue(), |
| 95 | stroke.getEndCap(), |
| 96 | stroke.getLineJoin(), |
| 97 | stroke.getMiterLimit(), |
| 98 | stroke.getDashArray(), |
| 99 | stroke.getDashPhase()); |
| 100 | _hiliterPrefs.setStroke(stroke); |
| 101 | |
| 102 | // Write the prefs |
| 103 | _manager.write(HiliterPrefs.HILITER_PREFS_NAME); |
| 104 | } |
| 105 | |
| 106 | public void setColorChooserTitle(String title) |
| 107 | { |
| 108 | _btnColor.setTitle(title); |
| 109 | } |
| 110 | |
| 111 | static final private double DEFAULT_STROKE_SIZE = 8.0; |
| 112 | static final private double STROKE_MIN_SIZE = 2.0; |
| 113 | static final private double STROKE_MAX_SIZE = 20.0; |
| 114 | |
| 115 | final private PreferencesManager _manager; |
| 116 | final private HiliterPrefs _hiliterPrefs; |
| 117 | |
| 118 | final private JLabel _lblColor = new JLabel(); |
| 119 | final private ColorButton _btnColor; |
| 120 | final private JLabel _lblSize = new JLabel(); |
| 121 | final private JSpinner _txfSize = new JSpinner( |
| 122 | new SpinnerNumberModel(DEFAULT_STROKE_SIZE, STROKE_MIN_SIZE, STROKE_MAX_SIZE, 1.0)); |
| 123 | } |