| 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.event.ActionEvent; |
| 20 | import java.awt.event.ActionListener; |
| 21 | import java.io.File; |
| 22 | |
| 23 | import javax.swing.JButton; |
| 24 | import javax.swing.JFileChooser; |
| 25 | import javax.swing.JLabel; |
| 26 | import javax.swing.JPanel; |
| 27 | import javax.swing.JTextField; |
| 28 | |
| 29 | import net.sf.jomic.common.JomicTools; |
| 30 | import net.sf.jomic.common.PropertyConstants; |
| 31 | import net.sf.jomic.common.Settings; |
| 32 | import net.sf.jomic.tools.LocaleTools; |
| 33 | import net.sf.jomic.tools.UiTools; |
| 34 | |
| 35 | /** |
| 36 | * Utility class to add components to an existing JPanel to set a file path. The user can choose to |
| 37 | * either type it in a text field or select it in a JFileChooser. |
| 38 | * |
| 39 | * @author Thomas Aglassinger |
| 40 | */ |
| 41 | public class FileFieldAdder implements ActionListener |
| 42 | { |
| 43 | private static final String COMMAND_CHOOSE = "choose"; |
| 44 | private static final int DEFAULT_FILE_FIELD_COLUMN_COUNT = 20; |
| 45 | |
| 46 | private JFileChooser chooser; |
| 47 | private JButton chooserButton; |
| 48 | private JPanel container; |
| 49 | private JTextField field; |
| 50 | private JomicTools jomicTools; |
| 51 | private JLabel label; |
| 52 | private LocaleTools localeTools; |
| 53 | private Settings settings; |
| 54 | private UiTools uiTools; |
| 55 | |
| 56 | /** |
| 57 | * Create a new FileFieldAdder which immediately adds the relevent components to the specified |
| 58 | * JPanel. |
| 59 | * |
| 60 | * @param newContainer the JPanel to which the file field component should be added; it must |
| 61 | * be using a GridBadLayout |
| 62 | * @param constraints the GridBagConstraints of <code>newContainer</code> applied to |
| 63 | * components to be added |
| 64 | * @param newChooser the JFileChooser to use when the "..." button was pressed; if this is |
| 65 | * <code>null</code>, a generic default chooser is used |
| 66 | */ |
| 67 | public FileFieldAdder(JPanel newContainer, GridBagConstraints constraints, JFileChooser newChooser) { |
| 68 | super(); |
| 69 | assert newContainer != null; |
| 70 | assert constraints != null; |
| 71 | settings = Settings.instance(); |
| 72 | jomicTools = JomicTools.instance(); |
| 73 | localeTools = LocaleTools.instance(); |
| 74 | uiTools = UiTools.instance(); |
| 75 | |
| 76 | container = newContainer; |
| 77 | label = new JLabel("File:"); |
| 78 | field = new JTextField(DEFAULT_FILE_FIELD_COLUMN_COUNT); |
| 79 | chooserButton = new JButton(localeTools.getMessage("buttons.chooseFile")); |
| 80 | chooserButton.setActionCommand(COMMAND_CHOOSE); |
| 81 | chooserButton.addActionListener(this); |
| 82 | if (newChooser == null) { |
| 83 | chooser = new SnapableJFileChooser(settings, PropertyConstants.GENERIC_FILE_CHOOSER_WINDOW); |
| 84 | } else { |
| 85 | chooser = newChooser; |
| 86 | } |
| 87 | |
| 88 | int oldAnchor = constraints.anchor; |
| 89 | int oldFill = constraints.fill; |
| 90 | double oldWeightX = constraints.weightx; |
| 91 | |
| 92 | constraints.anchor = GridBagConstraints.LINE_END; |
| 93 | constraints.fill = GridBagConstraints.NONE; |
| 94 | constraints.weightx = 0; |
| 95 | container.add(label, constraints); |
| 96 | constraints.anchor = GridBagConstraints.CENTER; |
| 97 | constraints.fill = GridBagConstraints.HORIZONTAL; |
| 98 | constraints.gridx += 1; |
| 99 | constraints.weightx = 1; |
| 100 | container.add(field, constraints); |
| 101 | constraints.fill = GridBagConstraints.NONE; |
| 102 | constraints.gridx += 1; |
| 103 | constraints.weightx = 0; |
| 104 | container.add(chooserButton, constraints); |
| 105 | constraints.anchor = oldAnchor; |
| 106 | constraints.fill = oldFill; |
| 107 | constraints.weightx = oldWeightX; |
| 108 | } |
| 109 | |
| 110 | public JFileChooser getChooser() { |
| 111 | return chooser; |
| 112 | } |
| 113 | |
| 114 | public JTextField getField() { |
| 115 | return field; |
| 116 | } |
| 117 | |
| 118 | public JLabel getLabel() { |
| 119 | return label; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Process event after the "..." button was pressed and open a JFileChooser to select a file. |
| 124 | */ |
| 125 | public void actionPerformed(ActionEvent event) { |
| 126 | try { |
| 127 | assert event != null; |
| 128 | String command = event.getActionCommand(); |
| 129 | |
| 130 | assert command != null; |
| 131 | if (command.equals(COMMAND_CHOOSE)) { |
| 132 | int set = chooser.showDialog(container, null); |
| 133 | |
| 134 | if (set == JFileChooser.APPROVE_OPTION) { |
| 135 | File selectedFile = chooser.getSelectedFile(); |
| 136 | |
| 137 | field.setText(selectedFile.getAbsolutePath()); |
| 138 | } |
| 139 | } else { |
| 140 | assert false : "command=" + command; |
| 141 | } |
| 142 | |
| 143 | } catch (Exception error) { |
| 144 | jomicTools.showError(event, error); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | public void dispose() { |
| 149 | uiTools.attemptToRemoveActionListener(chooserButton, this); |
| 150 | } |
| 151 | } |