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