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

Last change on this file since 6972 was 6972, checked in by mlg, 14 years ago

New feature : research
Add a research functionnality : it's juste a field on the bottom right of the main frame. To make a research, the user have to type text in that field and press enter. It opens a new tab. A new tab is opened for each research. The tab contains the query string.

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