source: extensions/jiwigo/trunk/src/main/java/fr/mael/jiwigo/ui/mainframe/MainFrame.java @ 9880

Last change on this file since 9880 was 9880, checked in by mlg, 13 years ago

Changes splitpanes behavior
to continuousLayout = true

File size: 10.3 KB
Line 
1package fr.mael.jiwigo.ui.mainframe;
2
3import java.awt.BorderLayout;
4import java.awt.Dimension;
5import java.awt.FlowLayout;
6import java.awt.event.ActionEvent;
7import java.awt.event.ActionListener;
8import java.awt.event.KeyEvent;
9import java.awt.event.KeyListener;
10import java.util.HashMap;
11
12import javax.swing.ImageIcon;
13import javax.swing.JComponent;
14import javax.swing.JFrame;
15import javax.swing.JLabel;
16import javax.swing.JMenu;
17import javax.swing.JMenuBar;
18import javax.swing.JMenuItem;
19import javax.swing.JPanel;
20import javax.swing.JProgressBar;
21import javax.swing.JScrollPane;
22import javax.swing.JSplitPane;
23
24import fr.mael.jiwigo.transverse.util.Messages;
25import fr.mael.jiwigo.transverse.util.Tools;
26import fr.mael.jiwigo.ui.MyCollapsiblePanel;
27import fr.mael.jiwigo.ui.browser.BrowserPanel;
28import fr.mael.jiwigo.ui.field.HintTextField;
29import fr.mael.jiwigo.ui.mainframe.tab.JTabbedPaneWithCloseIcons;
30import 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 */
61public 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(Tools.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        splitPane.setContinuousLayout(true);
159        categoriesTree = new CategoriesTree();
160        splitPane.setLeftComponent(categoriesTree);
161
162        imageSearchPanel = new ImageSearchPanel();
163
164        tabbedPane = new JTabbedPaneWithCloseIcons();
165        collapsiblePanel = new MyCollapsiblePanel(tabbedPane);
166        //      splitPane.setTopComponent(browserFrame);
167        //      splitPane.setBottomComponent(tabbedPane);
168        splitPane.setRightComponent(collapsiblePanel);
169
170        this.add(splitPane, BorderLayout.CENTER);
171        JPanel panelBas = new JPanel(new BorderLayout());
172        JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
173        progressBar = new JProgressBar(0, 100);
174        panel.add(progressBar);
175        panel.add(labelAdditionalMessage);
176        panel.add(labelMessage);
177        panelBas.add(panel, BorderLayout.WEST);
178
179        fieldSearch = new HintTextField(Messages.getMessage("mainFrame_recherche"));
180        fieldSearch.setPreferredSize(new Dimension(150, 25));
181        fieldSearch.addKeyListener(this);
182        panelBas.add(fieldSearch, BorderLayout.EAST);
183
184        jMenuEdition = new JMenu(Messages.getMessage("mainFrame_editionMenu"));
185        jMenuBar.add(jMenuEdition);
186        jMenuItemPreferences = new JMenuItem(Messages.getMessage("mainFrame_preferencesMenu"));
187        jMenuItemPreferences.addActionListener(this);
188        jMenuEdition.add(jMenuItemPreferences);
189        jMenuItemSearch = new JMenuItem("Search");
190        jMenuItemSearch.addActionListener(this);
191        jMenuEdition.add(jMenuItemSearch);
192
193        this.setJMenuBar(jMenuBar);
194        this.add(panelBas, BorderLayout.SOUTH);
195        this.setSize(900, 600);
196        this.setLocationRelativeTo(null);
197        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
198        this.setResizable(true);
199    }
200
201    public void setBrowserVisible() {
202        splitPane.remove(imageSearchPanel);
203        splitPane.remove(collapsiblePanel);
204        splitPane.setRightComponent(collapsiblePanel);
205        splitPane.revalidate();
206        splitPane.repaint();
207        imageSearchPanelVisible = false;
208        browserVisible = true;
209    }
210
211    public void setImageSearchPanelVisible() {
212        splitPane.remove(imageSearchPanel);
213        splitPane.remove(collapsiblePanel);
214        splitPane.setLeftComponent(categoriesTree);
215        splitPane.setRightComponent(imageSearchPanel);
216        splitPane.revalidate();
217        splitPane.repaint();
218        imageSearchPanelVisible = true;
219        browserVisible = false;
220    }
221
222    public void addTabb(IThumbnailPanel panel) {
223        JScrollPane scrollPaneImagesPanel = new JScrollPane((JPanel) panel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
224                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
225        scrollPaneImagesPanel.setPreferredSize(new Dimension(900, 600));
226        boolean found = false;
227        boolean isSearch = false;
228        if (panel instanceof ThumbnailSearchPanel) {
229            isSearch = true;
230        }
231
232        for (int i = 0; i < tabbedPane.getTabCount(); i++) {
233            JScrollPane scroll = (JScrollPane) tabbedPane.getComponentAt(i);
234            IThumbnailPanel thumbPan = (IThumbnailPanel) scroll.getViewport().getComponents()[0];
235            if (thumbPan.getCategory().getIdentifier().equals(panel.getCategory().getIdentifier())) {
236                //only if it's not for a re
237                if (!(panel instanceof ThumbnailSearchPanel)) {
238                    tabbedPane.setSelectedIndex(i);
239                    found = true;
240                    break;
241                }
242            }
243        }
244        //if it's not for a research, the title of the tab
245        //is the name of the category
246        if (!found && !isSearch) {
247            tabbedPane.addTab(panel.getCategory().getName(), scrollPaneImagesPanel, new ImageIcon(Tools
248                    .getURL("fr/mael/jiwigo/img/closetab.png")));
249            //if it's for a research, the title of the tab
250            //if the query string
251        } else if (!found && isSearch) {
252            String queryString = ((ThumbnailSearchPanel) panel).getQueryString();
253            tabbedPane.addTab(Messages.getMessage("mainFrame_search") + queryString, scrollPaneImagesPanel,
254                    new ImageIcon(Tools.getURL("fr/mael/jiwigo/img/closetab.png")));
255        }
256
257    }
258
259    public void revalidate() {
260
261    }
262
263    /**
264     * displays the frame
265     */
266    public void showUi() {
267        this.setVisible(true);
268    }
269
270    /**
271     * Affichage d'un message de réussite
272     * @param message
273     */
274    public void setMessage(String message) {
275        this.labelMessage.setText(message);
276    }
277
278    public void setAdditionalMessage(String additionalMessage) {
279        this.labelAdditionalMessage.setText(additionalMessage);
280    }
281
282    @Override
283    public void actionPerformed(ActionEvent arg0) {
284        if (arg0.getSource().equals(jMenuItemPreferences)) {
285            new PreferencesDialog(this);
286        } else if (arg0.getSource().equals(jMenuItemSearch)) {
287            setImageSearchPanelVisible();
288        }
289
290    }
291
292    public static void reduceSizeOfComponent(JComponent comp) {
293        comp.setMaximumSize(comp.getPreferredSize());
294    }
295
296    /**
297     * @return the progressBar
298     */
299    public JProgressBar getProgressBar() {
300        return progressBar;
301    }
302
303    /**
304     * @return the mapsIdPos
305     */
306    public HashMap<Integer, Integer> getMapsIdPos() {
307        return mapsIdPos;
308    }
309
310    @Override
311    public void keyPressed(KeyEvent paramKeyEvent) {
312        if (paramKeyEvent.getKeyCode() == KeyEvent.VK_ENTER) {
313            String queryString = fieldSearch.getText();
314            ThumbnailSearchPanel searchPanel = new ThumbnailSearchPanel(queryString);
315            addTabb(searchPanel);
316        }
317    }
318
319    @Override
320    public void keyReleased(KeyEvent paramKeyEvent) {
321    }
322
323    @Override
324    public void keyTyped(KeyEvent paramKeyEvent) {
325    }
326
327    //    /**
328    //     * @return the collapsiblePanel
329    //     */
330    //    public MyCollapsiblePanel getCollapsiblePanel() {
331    //  return collapsiblePanel;
332    //    }
333
334    /**
335     * @return the imageSearchPanelVisible
336     */
337    public boolean isImageSearchPanelVisible() {
338        return imageSearchPanelVisible;
339    }
340
341    /**
342     * @return the browserVisible
343     */
344    public boolean isBrowserVisible() {
345        return browserVisible;
346    }
347
348}
Note: See TracBrowser for help on using the repository browser.