001    package net.sourceforge.mp3dings;
002    
003    /* Mp3dings - manage mp3 meta-information
004     * Copyright (C) 2003-2004 Moritz Ringler
005     * $Id: MainFrame.java 68 2007-10-24 21:27:46Z ringler $
006     *
007     * This program is free software; you can redistribute it and/or
008     * modify it under the terms of the GNU General Public License
009     * as published by the Free Software Foundation; either version 2
010     * of the License, or (at your option) any later version.
011     *
012     * This program is distributed in the hope that it will be useful,
013     * but WITHOUT ANY WARRANTY; without even the implied warranty of
014     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
015     * GNU General Public License for more details.
016     *
017     * You should have received a copy of the GNU General Public License
018     * along with this program; if not, write to the Free Software
019     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
020     */
021    
022    import java.awt.AWTEvent;
023    import java.awt.BorderLayout;
024    import java.awt.Component;
025    import java.awt.Dimension;
026    import java.awt.GridLayout;
027    import java.awt.Image;
028    import java.awt.Point;
029    import java.awt.Rectangle;
030    import java.awt.Toolkit;
031    import java.awt.event.MouseAdapter;
032    import java.awt.event.MouseEvent;
033    import java.awt.event.WindowEvent;
034    import java.net.URL;
035    import java.util.prefs.Preferences;
036    import javax.swing.ImageIcon;
037    import javax.swing.JFrame;
038    import javax.swing.JLabel;
039    import javax.swing.JOptionPane;
040    import javax.swing.JPanel;
041    import javax.swing.JScrollPane;
042    import javax.swing.JViewport;
043    import javax.swing.SwingUtilities;
044    import de.mospace.swing.JBook;
045    import de.mospace.swing.ListeningStatusBar;
046    import de.mospace.swing.MiniShell;
047    import de.mospace.swing.StatusBar;
048    
049    /**
050     * Main frame of the mp3dings application.
051     * @author Moritz Ringler
052     * @version $Revision: 68 $ ($Date: 2007-10-24 23:27:46 +0200 (Mi, 24 Okt 2007) $)
053     */
054    
055    public final class MainFrame extends JFrame{
056        protected Image logo = Toolkit.getDefaultToolkit().
057                getImage(Application.class.getResource("mp3dings_logo.gif"));
058        protected ListeningStatusBar statusBar = new ListeningStatusBar(10000);
059        private static Preferences pref;
060    
061        private MiniShell minishell;
062        private Mp3dingsTab dataTab;
063        private Mp3dingsTab fileTab;
064        private Mp3dingsTab id3v1Tab;
065        private Mp3dingsTab id3v2Tab;
066        private JPanel statusPanel;
067        protected Application app;
068    
069        /* CONSTRUCT THE FRAME */
070        public MainFrame(Application app) {
071            this.app = app;
072            pref = Globals.getPrefNode();
073            enableEvents(AWTEvent.WINDOW_EVENT_MASK);
074            init();
075        }
076    
077        /* COMPONENT INITIALIZATION*/
078        private void init(){
079    
080            /* Main frame */
081            setTitle("mp3dings");
082            setIconImage(logo);
083    
084            /* Menu */
085            setJMenuBar(new MainMenu(this));
086    
087            /* Tabs */
088            constructTabs();
089    
090            /* main pane = headline + notebook/tabs + status bars  */
091            JPanel pane = new JPanel();
092            pane.setOpaque(true);
093            pane.setLayout(new BorderLayout());
094            pane.setBackground(Globals.getColor(Globals.COLOR_MAIN_BG));
095            pane.add(constructHeadline(), BorderLayout.NORTH);
096            pane.add(constructNotebook(), BorderLayout.CENTER);
097    
098            /* - Status bars */
099            statusPanel = new JPanel(new GridLayout(0,1,0,0));
100            statusPanel.add(statusBar);
101            pane.add(statusPanel,BorderLayout.SOUTH);
102    
103            setContentPane(pane);
104            pack();
105    
106            /* Center window on screen. **/
107            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
108            Dimension size = getSize();
109            if (size.height > screenSize.height) {
110                size.height = screenSize.height;
111            }
112            if (size.width > screenSize.width) {
113                size.width = screenSize.width;
114            }
115            setSize(size);
116            setLocation((screenSize.width - size.width) / 2,
117                (screenSize.height - size.height) / 2);
118    
119        }
120    
121        private JLabel constructHeadline(){
122            final JLabel self = new JLabel();
123            self.setAlignmentX((float) 0.5);
124            self.setHorizontalAlignment(JLabel.CENTER);
125            ImageIcon ic = new ImageIcon((URL) MainFrame.class.getResource("mp3dings_title.png"));
126            self.setIcon(ic);
127            self.setToolTipText(Globals.getString("HEADLINE_HIDE_TOOLTIP"));
128            self.addMouseListener(new MouseAdapter(){
129                public void mouseClicked(MouseEvent e){
130                    self.setVisible(false);
131                }
132            });
133            return self;
134        }
135    
136        private JBook constructNotebook(){
137            JBook self = new JBook();
138            self.setBackground(Globals.getColor(Globals.COLOR_TAB_BG));
139            self.setFont(Globals.MONOSPACE_0_12);
140            self.add(dataTab.getComponent(), Globals.getString("DATA_TAB_TEXT"),
141                      (Integer) Globals.getObject("DATA_TAB_MNEMONICS"));
142            self.add(fileTab.getComponent(),Globals.getString("NAMES_TAB_TEXT"),
143                      (Integer) Globals.getObject("NAMES_TAB_MNEMONICS"));
144            self.add(id3v1Tab.getComponent(),Globals.getString("ID3V1_TAB_TEXT"),
145                      (Integer) Globals.getObject("ID3V1_TAB_MNEMONICS"));
146            self.add(id3v2Tab.getComponent(),Globals.getString("ID3V2_TAB_TEXT"),
147                      (Integer) Globals.getObject("ID3V2_TAB_MNEMONICS"));
148            self.setTransferHandler(dataTab.getTable().getTransferHandler());
149            self.registerCtrlT();
150            return self;
151        }
152    
153        private void constructTabs(){
154            TableContextMenu tablePopup;
155            //ID3TagTablePopup id3HeaderPopup = new ID3TagTablePopup();
156            dataTab = new DataTab();
157            DataTableModel dataTableModel = (DataTableModel) dataTab.getModel();
158    
159            /* Context Menu */
160            tablePopup = new TableContextMenu(dataTableModel);
161            dataTab.addTablePopup(tablePopup);
162            dataTab.getModel().addMessageListener(statusBar);
163    
164    
165            /* Name/files Spreadsheet */
166            fileTab = new FileTab(dataTableModel);
167            fileTab.addTablePopup(tablePopup);
168            fileTab.getModel().addMessageListener(statusBar);
169    
170            /* ID3 TABS */
171            id3v1Tab = new ID3Tab(dataTableModel, 1);
172            id3v1Tab.addTablePopup(tablePopup);
173            id3v1Tab.getModel().addMessageListener(statusBar);
174    
175            id3v2Tab = new ID3Tab(dataTableModel, 2);
176            id3v2Tab.addTablePopup(tablePopup);
177            id3v2Tab.getModel().addMessageListener(statusBar);
178        }
179    
180        public Mp3dingsTableModel[] getModels(){
181            Mp3dingsTableModel[] models = {
182                dataTab.getModel(),
183                fileTab.getModel(),
184                id3v1Tab.getModel(),
185                id3v2Tab.getModel()
186            };
187            return models;
188        }
189    
190        public Mp3dingsTableModel getModel(int i){
191            Mp3dingsTableModel self;
192            switch (i){
193                case 0: self = dataTab.getModel(); break;
194                case 1: self = fileTab.getModel(); break;
195                case 2: self = id3v1Tab.getModel(); break;
196                case 3: self = id3v2Tab.getModel(); break;
197                default: throw new IllegalArgumentException("i must be between 0 and 3");
198            }
199            return self;
200        }
201    
202        public void goingToExit(){
203        }
204    
205    
206        /**Overridden so we can exit when window is closed*/
207        protected void processWindowEvent(WindowEvent e) {
208            super.processWindowEvent(e);
209            if (e.getID() == WindowEvent.WINDOW_CLOSING) {
210                app.exit(0);
211            }
212        }
213    
214        protected JViewport getViewport(Component c){
215            return (JViewport) SwingUtilities.getAncestorOfClass(JViewport.class, c);
216        }
217    
218        protected void scrollTo(Point p, Component source){
219            JScrollPane spane = (JScrollPane)
220                ((source instanceof JScrollPane)
221                    ? source
222                    : SwingUtilities.getAncestorOfClass(JScrollPane.class, source));
223            for(
224                Point p1;
225                spane != null;
226                spane = (JScrollPane)
227                        SwingUtilities.getAncestorOfClass(JScrollPane.class, spane)
228            ){
229                spane.validate();
230                p1 = SwingUtilities.convertPoint(source, p, spane.getViewport().getView());
231                scrollViewportTo(p1, spane.getViewport());
232            }
233        }
234    
235        protected void scrollTo(Rectangle r, Component source){
236            /* lower right */
237            Point p = new Point(r.x + r.width, r.y + r.height);
238            scrollTo(p, source);
239            /* upper left */
240            p = r.getLocation();
241            scrollTo(p, source);
242        }
243    
244        private void scrollViewportTo(Point p, JViewport v){
245            if (v.getViewRect().contains(p)){
246                return;
247            }
248            Rectangle r = new Rectangle(v.getViewRect());
249            Point q = new Point();
250            if (p.x < r.x){
251                q.x = p.x;
252            } else if (p.x > r.x + r.width){
253                q.x = p.x - r.width;
254            } else {
255                q.x = r.x;
256            }
257            if (p.y < r.y){
258                q.y = p.y;
259            } else if (p.y > r.y + r.height){
260                q.y = p.y - r.height;
261            } else {
262                q.y = r.y;
263            }
264            v.setViewPosition(q);
265        }
266    
267        public synchronized MiniShell getMiniShell(){
268            if (minishell == null){
269                minishell = new MiniShell(this,400,150,"Mp3dings Console", true){
270                    protected Preferences getPref(){
271                        return Globals.getPrefNode().node("Console");
272                    }
273                };
274            }
275            return minishell;
276        }
277    
278        protected void setStatus(String status, int timeout){
279            statusBar.setStatus(status, timeout);
280        }
281        protected void setStatus(String status){
282            statusBar.setStatus(status);
283        }
284    
285        protected void addStatusBar(StatusBar sb){
286            statusPanel.add(sb);
287        }
288    
289        protected void removeStatusBar(StatusBar sb){
290            statusPanel.remove(sb);
291        }
292    
293        public void refreshTabs(){
294            Mp3dingsTableModel model[] = getModels();
295            for(int i=0; i < model.length; i++){
296                ((AbstractTableModel) model[i]).fireTableStructureChanged();
297            }
298        }
299    }