| 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.Dimension; |
| 19 | import java.awt.event.ActionEvent; |
| 20 | import java.awt.event.ActionListener; |
| 21 | import java.net.URL; |
| 22 | |
| 23 | import javax.swing.ImageIcon; |
| 24 | import javax.swing.JButton; |
| 25 | import javax.swing.JLabel; |
| 26 | import javax.swing.JToolBar; |
| 27 | |
| 28 | import net.sf.jomic.common.Settings; |
| 29 | import net.sf.jomic.tools.ActionDelegate; |
| 30 | import net.sf.jomic.tools.LocaleTools; |
| 31 | import net.sf.wraplog.Logger; |
| 32 | |
| 33 | /** |
| 34 | * Toolbar for essential comic operations. |
| 35 | * |
| 36 | * @author Thomas Aglassinger |
| 37 | */ |
| 38 | public class JomicToolbar extends JToolBar implements ActionListener |
| 39 | { |
| 40 | private static Logger logger = Logger.getLogger(JomicToolbar.class); |
| 41 | private ActionDelegate delegate; |
| 42 | private JButton exportButton; |
| 43 | private JButton firstButton; |
| 44 | private LocaleTools localeTools; |
| 45 | private JButton nextButton; |
| 46 | private JButton nextButtonFew; |
| 47 | private JLabel pageLabel; |
| 48 | private JButton previousButton; |
| 49 | private JButton previousButtonFew; |
| 50 | private Settings settings; |
| 51 | private JButton zoomToActualButton; |
| 52 | private JButton zoomToFitButton; |
| 53 | |
| 54 | public JomicToolbar() { |
| 55 | settings = Settings.instance(); |
| 56 | localeTools = LocaleTools.instance(); |
| 57 | firstButton = createButton("goFirst", Commands.FIRST_PAGE, "goToFront"); |
| 58 | previousButtonFew = createButton("goPreviousFew", Commands.GO_PREVIOUS_FEW, null); |
| 59 | previousButton = createButton("goPrevious", Commands.GO_PREVIOUS, null); |
| 60 | nextButton = createButton("goNext", Commands.GO_NEXT, null); |
| 61 | nextButtonFew = createButton("goNextFew", Commands.GO_NEXT_FEW, null); |
| 62 | exportButton = createButton("export", Commands.EXPORT, "exportCurrentImage"); |
| 63 | zoomToActualButton = createButton("zoomToActual", Commands.FIT_ACTUAL_SIZE, null); |
| 64 | zoomToFitButton = createButton("zoomToFit", Commands.FIT_WIDTH_AND_HEIGHT, null); |
| 65 | pageLabel = new JLabel(); |
| 66 | |
| 67 | add(firstButton); |
| 68 | add(previousButtonFew); |
| 69 | add(previousButton); |
| 70 | add(nextButton); |
| 71 | add(nextButtonFew); |
| 72 | addSeparator(); |
| 73 | add(exportButton); |
| 74 | addSeparator(); |
| 75 | add(zoomToActualButton); |
| 76 | add(zoomToFitButton); |
| 77 | addSeparator(); |
| 78 | add(pageLabel); |
| 79 | setUIState(UIStates.EMPTY); |
| 80 | setFloatable(false); |
| 81 | |
| 82 | delegate = new ActionDelegate(logger); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Enables/disables buttons to change page. |
| 87 | */ |
| 88 | public final void setEnabledPageButtons(boolean isFirstPage, boolean isLastPage) { |
| 89 | firstButton.setEnabled(!isFirstPage); |
| 90 | previousButtonFew.setEnabled(!isFirstPage); |
| 91 | previousButton.setEnabled(!isFirstPage); |
| 92 | nextButton.setEnabled(!isLastPage); |
| 93 | nextButtonFew.setEnabled(!isLastPage); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * @see #setEnabledPageButtons(boolean, boolean) |
| 98 | */ |
| 99 | void setUIState(String state) { |
| 100 | assert UIStates.isValidState(state); |
| 101 | if (state.equals(UIStates.EMPTY)) { |
| 102 | firstButton.setEnabled(false); |
| 103 | previousButtonFew.setEnabled(false); |
| 104 | previousButton.setEnabled(false); |
| 105 | nextButton.setEnabled(false); |
| 106 | nextButtonFew.setEnabled(false); |
| 107 | exportButton.setEnabled(false); |
| 108 | } else if (state.equals(UIStates.OPENING)) { |
| 109 | if (logger.isDebugEnabled()) { |
| 110 | logger.debug("ignoring state=" + state); |
| 111 | } |
| 112 | } else if (state.equals(UIStates.VIEWING)) { |
| 113 | exportButton.setEnabled(true); |
| 114 | } else { |
| 115 | assert false : "state = " + state; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Depending on <code>Settings.getShowToolbar()</code>, either get the preferred size of the |
| 121 | * super class, or a (0, 0). |
| 122 | */ |
| 123 | public Dimension getPreferredSize() { |
| 124 | Dimension result; |
| 125 | |
| 126 | if (settings.getShowToolbar()) { |
| 127 | result = super.getPreferredSize(); |
| 128 | } else { |
| 129 | result = new Dimension(0, 0); |
| 130 | } |
| 131 | return result; |
| 132 | } |
| 133 | |
| 134 | public final void actionPerformed(ActionEvent event) { |
| 135 | delegate.actionPerformed(event); |
| 136 | } |
| 137 | |
| 138 | public final void addActionListener(ActionListener listener) { |
| 139 | delegate.addActionListener(listener); |
| 140 | } |
| 141 | |
| 142 | public final void removeActionListener(ActionListener listener) { |
| 143 | delegate.removeActionListener(listener); |
| 144 | } |
| 145 | |
| 146 | public void dispose() { |
| 147 | // Do nothing for now. |
| 148 | } |
| 149 | |
| 150 | private JButton createButton( |
| 151 | String imageName, |
| 152 | String command, |
| 153 | String toolTipTextKey) { |
| 154 | assert imageName != null; |
| 155 | assert command != null; |
| 156 | |
| 157 | String imgLocation = "/net/sf/jomic/images/" + imageName + ".png"; |
| 158 | URL imageURL = JomicToolbar.class.getResource(imgLocation); |
| 159 | JButton button = new JButton(); |
| 160 | String toolTipText = null; |
| 161 | |
| 162 | if (toolTipTextKey != null) { |
| 163 | toolTipText = localeTools.getMessage("toolbars.viewer.tooltips." + toolTipTextKey); |
| 164 | button.setToolTipText(toolTipText); |
| 165 | } |
| 166 | |
| 167 | if (imageURL != null) { |
| 168 | // Image found |
| 169 | button.setIcon(new ImageIcon(imageURL)); |
| 170 | } else { |
| 171 | // No image found |
| 172 | String message = localeTools.getMessage("errors.cannotFindToolbarImage", imgLocation); |
| 173 | |
| 174 | throw new IllegalArgumentException(message); |
| 175 | } |
| 176 | button.setActionCommand(command); |
| 177 | button.addActionListener(this); |
| 178 | return button; |
| 179 | } |
| 180 | } |