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