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.BorderLayout; |
19 | import java.awt.Dimension; |
20 | import java.awt.event.ActionEvent; |
21 | import java.awt.event.ActionListener; |
22 | |
23 | import javax.swing.JButton; |
24 | import javax.swing.JFrame; |
25 | import javax.swing.JPanel; |
26 | import javax.swing.JProgressBar; |
27 | import javax.swing.JScrollPane; |
28 | import javax.swing.JTable; |
29 | |
30 | import net.sf.jomic.comic.ConversionReport; |
31 | import net.sf.jomic.common.JomicTools; |
32 | import net.sf.jomic.common.PropertyConstants; |
33 | import net.sf.jomic.common.Settings; |
34 | import net.sf.jomic.tools.ActionDelegate; |
35 | import net.sf.jomic.tools.LocaleTools; |
36 | import net.sf.jomic.tools.UiTools; |
37 | |
38 | import org.apache.commons.logging.Log; |
39 | import org.apache.commons.logging.LogFactory; |
40 | |
41 | /** |
42 | * JFrame to show progress and result of a conversion. |
43 | * |
44 | * @see net.sf.jomic.comic.ConversionReport |
45 | * @author Thomas Aglassinger |
46 | */ |
47 | class ConversionReportFrame extends JFrame implements ActionListener |
48 | { |
49 | public static final String COMMAND_CANCEL = "cancel"; |
50 | public static final String COMMAND_SKIP = "skip"; |
51 | private static final int PREFERRED_REPORT_TABLE_HEIGHT = 200; |
52 | private static final int PREFERRED_REPORT_TABLE_WIDTH = 500; |
53 | private ActionDelegate actionDelegate; |
54 | private JButton cancelButton; |
55 | private JomicTools jomicTools; |
56 | private LocaleTools localeTools; |
57 | private Log logger; |
58 | private JProgressBar progressBar; |
59 | private ConversionReport report; |
60 | private JTable reportTable; |
61 | private Settings settings; |
62 | private UiTools uiTools; |
63 | |
64 | public ConversionReportFrame() { |
65 | logger = LogFactory.getLog(ConversionReportFrame.class); |
66 | jomicTools = JomicTools.instance(); |
67 | localeTools = LocaleTools.instance(); |
68 | uiTools = UiTools.instance(); |
69 | settings = Settings.instance(); |
70 | progressBar = new JProgressBar(); |
71 | getContentPane().add(createMainPanel()); |
72 | jomicTools.setIconToJomicLogo(this); |
73 | actionDelegate = new ActionDelegate(logger); |
74 | |
75 | String frameTitle = localeTools.getMessage("dialogs.conversionReport.title"); |
76 | |
77 | setTitle(frameTitle); |
78 | pack(); |
79 | settings.applyComponentAreaProperty(PropertyConstants.CONVERSION_REPORT_WINDOW, this); |
80 | } |
81 | |
82 | public void setReport(ConversionReport newReport) { |
83 | assert newReport != null; |
84 | report = newReport; |
85 | reportTable.setModel(report); |
86 | |
87 | } |
88 | |
89 | public JProgressBar getProgressBar() { |
90 | return progressBar; |
91 | } |
92 | |
93 | public void actionPerformed(ActionEvent event) { |
94 | try { |
95 | assert event != null; |
96 | String command = event.getActionCommand(); |
97 | |
98 | assert command != null; |
99 | if (command.equals(COMMAND_CANCEL)) { |
100 | cancelButton.setEnabled(false); |
101 | actionDelegate.actionPerformed(event); |
102 | } else { |
103 | assert false : "command=" + command; |
104 | } |
105 | } catch (Exception error) { |
106 | jomicTools.showError(event, error); |
107 | } |
108 | } |
109 | |
110 | /** |
111 | * Add listener to be notified when the "Cancel" button is pressed. |
112 | */ |
113 | public void addActionListener(ActionListener listener) { |
114 | actionDelegate.addActionListener(listener); |
115 | } |
116 | |
117 | public void dispose() { |
118 | uiTools.attemptToRemoveActionListener(cancelButton, this); |
119 | report = null; |
120 | super.dispose(); |
121 | } |
122 | |
123 | public void done() { |
124 | cancelButton.setEnabled(false); |
125 | progressBar.setValue(progressBar.getMaximum()); |
126 | progressBar.setVisible(false); |
127 | } |
128 | |
129 | /** |
130 | * Remove listener to be notified when the "Cancel" button is pressed. |
131 | */ |
132 | public void removeActionListener(ActionListener listener) { |
133 | actionDelegate.removeActionListener(listener); |
134 | } |
135 | |
136 | private JPanel createButtonPanel() { |
137 | JPanel result = new JPanel(); |
138 | |
139 | cancelButton = localeTools.createCancelButton(); |
140 | cancelButton.setActionCommand(COMMAND_CANCEL); |
141 | cancelButton.addActionListener(this); |
142 | result.add(cancelButton); |
143 | return result; |
144 | } |
145 | |
146 | private JPanel createMainPanel() { |
147 | assert progressBar != null; |
148 | JPanel result = new JPanel(); |
149 | JPanel buttonPanel = createButtonPanel(); |
150 | JPanel progressAndButtonPanel = new JPanel(); |
151 | |
152 | progressBar.setIndeterminate(true); |
153 | reportTable = new JTable(); |
154 | |
155 | Dimension preferredTableSize = |
156 | new Dimension(PREFERRED_REPORT_TABLE_WIDTH, PREFERRED_REPORT_TABLE_HEIGHT); |
157 | |
158 | reportTable.setPreferredScrollableViewportSize(preferredTableSize); |
159 | |
160 | JScrollPane reportPane = new JScrollPane(reportTable); |
161 | |
162 | progressAndButtonPanel.setLayout(new BorderLayout()); |
163 | progressAndButtonPanel.add(progressBar, BorderLayout.CENTER); |
164 | progressAndButtonPanel.add(buttonPanel, BorderLayout.LINE_END); |
165 | |
166 | result.setLayout(new BorderLayout()); |
167 | result.add(progressAndButtonPanel, BorderLayout.PAGE_START); |
168 | result.add(reportPane, BorderLayout.CENTER); |
169 | |
170 | return result; |
171 | } |
172 | } |