1 | package fr.mael.jiwigo.ui.mainframe; |
---|
2 | |
---|
3 | import java.awt.BorderLayout; |
---|
4 | import java.awt.Dimension; |
---|
5 | import java.awt.FlowLayout; |
---|
6 | import java.awt.event.ActionEvent; |
---|
7 | import java.awt.event.ActionListener; |
---|
8 | import java.awt.event.KeyEvent; |
---|
9 | import java.awt.event.KeyListener; |
---|
10 | import java.util.HashMap; |
---|
11 | |
---|
12 | import javax.swing.ImageIcon; |
---|
13 | import javax.swing.JComponent; |
---|
14 | import javax.swing.JFrame; |
---|
15 | import javax.swing.JLabel; |
---|
16 | import javax.swing.JMenu; |
---|
17 | import javax.swing.JMenuBar; |
---|
18 | import javax.swing.JMenuItem; |
---|
19 | import javax.swing.JPanel; |
---|
20 | import javax.swing.JProgressBar; |
---|
21 | import javax.swing.JScrollPane; |
---|
22 | import javax.swing.JSplitPane; |
---|
23 | |
---|
24 | import fr.mael.jiwigo.transverse.util.Messages; |
---|
25 | import fr.mael.jiwigo.transverse.util.Outil; |
---|
26 | import fr.mael.jiwigo.ui.MyCollapsiblePanel; |
---|
27 | import fr.mael.jiwigo.ui.browser.BrowserPanel; |
---|
28 | import fr.mael.jiwigo.ui.field.HintTextField; |
---|
29 | import fr.mael.jiwigo.ui.mainframe.tab.JTabbedPaneWithCloseIcons; |
---|
30 | import fr.mael.jiwigo.ui.search.ImageSearchPanel; |
---|
31 | |
---|
32 | /** |
---|
33 | Copyright (c) 2010, Mael |
---|
34 | All rights reserved. |
---|
35 | |
---|
36 | Redistribution and use in source and binary forms, with or without |
---|
37 | modification, are permitted provided that the following conditions are met: |
---|
38 | * Redistributions of source code must retain the above copyright |
---|
39 | notice, this list of conditions and the following disclaimer. |
---|
40 | * Redistributions in binary form must reproduce the above copyright |
---|
41 | notice, this list of conditions and the following disclaimer in the |
---|
42 | documentation and/or other materials provided with the distribution. |
---|
43 | * Neither the name of jiwigo nor the |
---|
44 | names of its contributors may be used to endorse or promote products |
---|
45 | derived from this software without specific prior written permission. |
---|
46 | |
---|
47 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
---|
48 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
---|
49 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
---|
50 | DISCLAIMED. IN NO EVENT SHALL Mael BE LIABLE FOR ANY |
---|
51 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
---|
52 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
---|
53 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
---|
54 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
55 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
---|
56 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
57 | |
---|
58 | * @author mael |
---|
59 | * Frame principale |
---|
60 | */ |
---|
61 | public class MainFrame extends JFrame implements ActionListener, KeyListener { |
---|
62 | |
---|
63 | /** |
---|
64 | * Logger |
---|
65 | */ |
---|
66 | public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory |
---|
67 | .getLog(MainFrame.class); |
---|
68 | /** |
---|
69 | * the categories tree |
---|
70 | */ |
---|
71 | private CategoriesTree categoriesTree; |
---|
72 | /** |
---|
73 | * Splitpane. On the left : the categories tree and on the right the thumbnails |
---|
74 | * for the current category |
---|
75 | */ |
---|
76 | private JSplitPane splitPane; |
---|
77 | /** |
---|
78 | * Panel that contains the thumbnail of the current category |
---|
79 | */ |
---|
80 | public static ThumbnailCategoryPanel imagesPanel; |
---|
81 | /** |
---|
82 | * Scrollpane that contains the panel above |
---|
83 | */ |
---|
84 | private JScrollPane scrollPaneImagesPanel; |
---|
85 | /** |
---|
86 | * label that displays messages |
---|
87 | */ |
---|
88 | private JLabel labelMessage; |
---|
89 | |
---|
90 | /** |
---|
91 | * label that displays additional messages |
---|
92 | */ |
---|
93 | private JLabel labelAdditionalMessage; |
---|
94 | |
---|
95 | /** |
---|
96 | * menu bar |
---|
97 | */ |
---|
98 | private JMenuBar jMenuBar; |
---|
99 | |
---|
100 | /** |
---|
101 | * edition menu |
---|
102 | */ |
---|
103 | private JMenu jMenuEdition; |
---|
104 | |
---|
105 | /** |
---|
106 | * preferences menu |
---|
107 | */ |
---|
108 | private JMenuItem jMenuItemPreferences; |
---|
109 | /** |
---|
110 | * menu to search images |
---|
111 | */ |
---|
112 | private JMenuItem jMenuItemSearch; |
---|
113 | |
---|
114 | /** |
---|
115 | * singleton |
---|
116 | */ |
---|
117 | private static MainFrame instance; |
---|
118 | |
---|
119 | private JProgressBar progressBar; |
---|
120 | |
---|
121 | private JTabbedPaneWithCloseIcons tabbedPane; |
---|
122 | |
---|
123 | private HashMap<Integer, Integer> mapsIdPos = new HashMap<Integer, Integer>(); |
---|
124 | |
---|
125 | private HintTextField fieldSearch; |
---|
126 | |
---|
127 | private BrowserPanel browserPanel; |
---|
128 | |
---|
129 | private MyCollapsiblePanel collapsiblePanel; |
---|
130 | |
---|
131 | private ImageSearchPanel imageSearchPanel; |
---|
132 | |
---|
133 | private boolean imageSearchPanelVisible = false; |
---|
134 | |
---|
135 | private boolean browserVisible = false; |
---|
136 | |
---|
137 | /** |
---|
138 | * @return the singleton |
---|
139 | */ |
---|
140 | public static MainFrame getInstance() { |
---|
141 | if (instance == null) { |
---|
142 | instance = new MainFrame(); |
---|
143 | } |
---|
144 | return instance; |
---|
145 | } |
---|
146 | |
---|
147 | /** |
---|
148 | * private constructor to use a singleton |
---|
149 | */ |
---|
150 | private MainFrame() { |
---|
151 | this.setTitle("Jiwigo v" + Messages.getMessage("version")); |
---|
152 | this.setIconImage(java.awt.Toolkit.getDefaultToolkit().getImage(Outil.getURL("fr/mael/jiwigo/img/icon.png"))); |
---|
153 | this.setLayout(new BorderLayout()); |
---|
154 | jMenuBar = new JMenuBar(); |
---|
155 | labelMessage = new JLabel(Messages.getMessage("welcomeMessage")); |
---|
156 | labelAdditionalMessage = new JLabel(); |
---|
157 | splitPane = new JSplitPane(); |
---|
158 | categoriesTree = new CategoriesTree(); |
---|
159 | splitPane.setLeftComponent(categoriesTree); |
---|
160 | |
---|
161 | imageSearchPanel = new ImageSearchPanel(); |
---|
162 | |
---|
163 | tabbedPane = new JTabbedPaneWithCloseIcons(); |
---|
164 | collapsiblePanel = new MyCollapsiblePanel(tabbedPane); |
---|
165 | splitPane.setRightComponent(collapsiblePanel); |
---|
166 | |
---|
167 | this.add(splitPane, BorderLayout.CENTER); |
---|
168 | JPanel panelBas = new JPanel(new BorderLayout()); |
---|
169 | JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT)); |
---|
170 | progressBar = new JProgressBar(0, 100); |
---|
171 | panel.add(progressBar); |
---|
172 | panel.add(labelAdditionalMessage); |
---|
173 | panel.add(labelMessage); |
---|
174 | panelBas.add(panel, BorderLayout.WEST); |
---|
175 | |
---|
176 | fieldSearch = new HintTextField(Messages.getMessage("mainFrame_recherche")); |
---|
177 | fieldSearch.setPreferredSize(new Dimension(150, 25)); |
---|
178 | fieldSearch.addKeyListener(this); |
---|
179 | panelBas.add(fieldSearch, BorderLayout.EAST); |
---|
180 | |
---|
181 | jMenuEdition = new JMenu(Messages.getMessage("mainFrame_editionMenu")); |
---|
182 | jMenuBar.add(jMenuEdition); |
---|
183 | jMenuItemPreferences = new JMenuItem(Messages.getMessage("mainFrame_preferencesMenu")); |
---|
184 | jMenuItemPreferences.addActionListener(this); |
---|
185 | jMenuEdition.add(jMenuItemPreferences); |
---|
186 | jMenuItemSearch = new JMenuItem("Search"); |
---|
187 | jMenuItemSearch.addActionListener(this); |
---|
188 | jMenuEdition.add(jMenuItemSearch); |
---|
189 | |
---|
190 | this.setJMenuBar(jMenuBar); |
---|
191 | this.add(panelBas, BorderLayout.SOUTH); |
---|
192 | this.setSize(900, 600); |
---|
193 | this.setLocationRelativeTo(null); |
---|
194 | this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
---|
195 | this.setResizable(true); |
---|
196 | } |
---|
197 | |
---|
198 | public void setBrowserVisible() { |
---|
199 | splitPane.setRightComponent(collapsiblePanel); |
---|
200 | splitPane.revalidate(); |
---|
201 | splitPane.repaint(); |
---|
202 | imageSearchPanelVisible = false; |
---|
203 | browserVisible = true; |
---|
204 | } |
---|
205 | |
---|
206 | public void setImageSearchPanelVisible() { |
---|
207 | splitPane.setRightComponent(imageSearchPanel); |
---|
208 | splitPane.revalidate(); |
---|
209 | splitPane.repaint(); |
---|
210 | imageSearchPanelVisible = true; |
---|
211 | browserVisible = false; |
---|
212 | } |
---|
213 | |
---|
214 | public void addTabb(IThumbnailPanel panel) { |
---|
215 | JScrollPane scrollPaneImagesPanel = new JScrollPane((JPanel) panel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, |
---|
216 | JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); |
---|
217 | scrollPaneImagesPanel.setPreferredSize(new Dimension(900, 600)); |
---|
218 | boolean found = false; |
---|
219 | boolean isSearch = false; |
---|
220 | if (panel instanceof ThumbnailSearchPanel) { |
---|
221 | isSearch = true; |
---|
222 | } |
---|
223 | |
---|
224 | for (int i = 0; i < tabbedPane.getTabCount(); i++) { |
---|
225 | JScrollPane scroll = (JScrollPane) tabbedPane.getComponentAt(i); |
---|
226 | IThumbnailPanel thumbPan = (IThumbnailPanel) scroll.getViewport().getComponents()[0]; |
---|
227 | if (thumbPan.getCategory().getIdentifiant().equals(panel.getCategory().getIdentifiant())) { |
---|
228 | //only if it's not for a re |
---|
229 | if (!(panel instanceof ThumbnailSearchPanel)) { |
---|
230 | tabbedPane.setSelectedIndex(i); |
---|
231 | found = true; |
---|
232 | break; |
---|
233 | } |
---|
234 | } |
---|
235 | } |
---|
236 | //if it's not for a research, the title of the tab |
---|
237 | //is the name of the category |
---|
238 | if (!found && !isSearch) { |
---|
239 | tabbedPane.addTab(panel.getCategory().getNom(), scrollPaneImagesPanel, new ImageIcon(Outil |
---|
240 | .getURL("fr/mael/jiwigo/img/closetab.png"))); |
---|
241 | //if it's for a research, the title of the tab |
---|
242 | //if the query string |
---|
243 | } else if (!found && isSearch) { |
---|
244 | String queryString = ((ThumbnailSearchPanel) panel).getQueryString(); |
---|
245 | tabbedPane.addTab(Messages.getMessage("mainFrame_search") + queryString, scrollPaneImagesPanel, |
---|
246 | new ImageIcon(Outil.getURL("fr/mael/jiwigo/img/closetab.png"))); |
---|
247 | } |
---|
248 | |
---|
249 | } |
---|
250 | |
---|
251 | public void revalidate() { |
---|
252 | |
---|
253 | } |
---|
254 | |
---|
255 | /** |
---|
256 | * displays the frame |
---|
257 | */ |
---|
258 | public void showUi() { |
---|
259 | this.setVisible(true); |
---|
260 | } |
---|
261 | |
---|
262 | /** |
---|
263 | * Affichage d'un message de réussite |
---|
264 | * @param message |
---|
265 | */ |
---|
266 | public void setMessage(String message) { |
---|
267 | this.labelMessage.setText(message); |
---|
268 | } |
---|
269 | |
---|
270 | public void setAdditionalMessage(String additionalMessage) { |
---|
271 | this.labelAdditionalMessage.setText(additionalMessage); |
---|
272 | } |
---|
273 | |
---|
274 | @Override |
---|
275 | public void actionPerformed(ActionEvent arg0) { |
---|
276 | if (arg0.getSource().equals(jMenuItemPreferences)) { |
---|
277 | new PreferencesDialog(this); |
---|
278 | } else if (arg0.getSource().equals(jMenuItemSearch)) { |
---|
279 | setImageSearchPanelVisible(); |
---|
280 | } |
---|
281 | |
---|
282 | } |
---|
283 | |
---|
284 | public static void reduceSizeOfComponent(JComponent comp) { |
---|
285 | comp.setMaximumSize(comp.getPreferredSize()); |
---|
286 | } |
---|
287 | |
---|
288 | /** |
---|
289 | * @return the progressBar |
---|
290 | */ |
---|
291 | public JProgressBar getProgressBar() { |
---|
292 | return progressBar; |
---|
293 | } |
---|
294 | |
---|
295 | /** |
---|
296 | * @return the mapsIdPos |
---|
297 | */ |
---|
298 | public HashMap<Integer, Integer> getMapsIdPos() { |
---|
299 | return mapsIdPos; |
---|
300 | } |
---|
301 | |
---|
302 | @Override |
---|
303 | public void keyPressed(KeyEvent paramKeyEvent) { |
---|
304 | if (paramKeyEvent.getKeyCode() == KeyEvent.VK_ENTER) { |
---|
305 | String queryString = fieldSearch.getText(); |
---|
306 | ThumbnailSearchPanel searchPanel = new ThumbnailSearchPanel(queryString); |
---|
307 | addTabb(searchPanel); |
---|
308 | } |
---|
309 | } |
---|
310 | |
---|
311 | @Override |
---|
312 | public void keyReleased(KeyEvent paramKeyEvent) { |
---|
313 | } |
---|
314 | |
---|
315 | @Override |
---|
316 | public void keyTyped(KeyEvent paramKeyEvent) { |
---|
317 | } |
---|
318 | |
---|
319 | /** |
---|
320 | * @return the collapsiblePanel |
---|
321 | */ |
---|
322 | public MyCollapsiblePanel getCollapsiblePanel() { |
---|
323 | return collapsiblePanel; |
---|
324 | } |
---|
325 | |
---|
326 | /** |
---|
327 | * @return the imageSearchPanelVisible |
---|
328 | */ |
---|
329 | public boolean isImageSearchPanelVisible() { |
---|
330 | return imageSearchPanelVisible; |
---|
331 | } |
---|
332 | |
---|
333 | /** |
---|
334 | * @return the browserVisible |
---|
335 | */ |
---|
336 | public boolean isBrowserVisible() { |
---|
337 | return browserVisible; |
---|
338 | } |
---|
339 | |
---|
340 | } |
---|