001    package org.shiftone.jrat.desktop;
002    
003    import org.jdesktop.swingx.JXStatusBar;
004    import org.shiftone.jrat.desktop.action.file.*;
005    import org.shiftone.jrat.desktop.action.help.AboutAction;
006    import org.shiftone.jrat.desktop.action.help.DocsAction;
007    import org.shiftone.jrat.desktop.action.help.TipsAction;
008    import org.shiftone.jrat.desktop.action.inject.InjectDirectoryAction;
009    import org.shiftone.jrat.desktop.action.inject.InjectFileAction;
010    import org.shiftone.jrat.util.log.Logger;
011    
012    import javax.swing.*;
013    import java.awt.*;
014    import java.awt.event.ComponentAdapter;
015    import java.awt.event.ComponentEvent;
016    import java.awt.event.ContainerEvent;
017    import java.awt.event.ContainerListener;
018    import java.io.InputStream;
019    
020    /**
021     * @author jeff@shiftone.org (Jeff Drost)
022     */
023    public class DesktopFrame extends JFrame {
024    
025        private static final Logger LOG = Logger.getLogger(DesktopFrame.class);
026    
027        private JXStatusBar statusBar = createStatusBar();
028        private JTabbedPane tabbedPane = new JTabbedPane();
029        private CloseAction closeAction = new CloseAction(tabbedPane);
030        private CloseAllAction closeAllAction = new CloseAllAction(tabbedPane);
031        private OpenAction openAction = new OpenAction(this);
032        private int waiters = 0;
033    
034        public DesktopFrame() {
035    
036            super("JRat Desktop");
037    
038            DesktopPreferences.setLastRunTime(System.currentTimeMillis());
039            DesktopPreferences.incrementRunCount();
040    
041            setDefaultCloseOperation(EXIT_ON_CLOSE);
042    
043            Rectangle windowBounds = DesktopPreferences.getWindowBounds();
044    
045            if (windowBounds == null) {
046                Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
047                windowBounds = new Rectangle(50, 50, (int) dimension.getWidth() - 150, (int) dimension.getHeight() - 150);
048            }
049    
050            setBounds(windowBounds);
051    
052    
053            setJMenuBar(createMenuBar());
054    
055            Container pane = getContentPane();
056            pane.setLayout(new BorderLayout());
057            pane.add(tabbedPane, BorderLayout.CENTER);
058            pane.add(statusBar, BorderLayout.SOUTH);
059    
060            tabbedPane.addMouseListener(new TabMouseListener(tabbedPane));
061            tabbedPane.addContainerListener(new TabChangeListener());
062            checkTabs();
063    
064            addComponentListener(new ComponentListener());
065    
066        }
067    
068    
069        public void waitCursor() {
070            waiters++;
071            setCursor(Cursor.WAIT_CURSOR);
072        }
073    
074        public void unwaitCursor() {
075            waiters--;
076            if (waiters == 0) {
077                setCursor(Cursor.DEFAULT_CURSOR);
078            }
079        }
080    
081        private JMenuBar createMenuBar() {
082            JMenuBar toolBar = new JMenuBar();
083            {
084                JMenu file = new JMenu("File");
085                file.setMnemonic('F');
086                file.add(openAction);
087                file.add(closeAction);
088                file.add(closeAllAction);
089                {
090                    JMenu resize = new JMenu("Window Size");
091                    resize.setMnemonic('S');
092                    resize.add(new WindowSizeAction(this, 640, 480));
093                    resize.add(new WindowSizeAction(this, 800, 600));
094                    resize.add(new WindowSizeAction(this, 1024, 768));
095                    file.add(resize);
096                }
097                file.add(new ExitAction());
098                file.add(new ClearPreferencesAction(this));
099    
100                toolBar.add(file);
101            }
102    
103            {
104                JMenu inject = new JMenu("Inject");
105                inject.setMnemonic('I');
106                inject.add(new InjectDirectoryAction(this));
107                inject.add(new InjectFileAction(this));
108    
109                toolBar.add(inject);
110            }
111    
112            {
113                JMenu help = new JMenu("Help");
114                help.setMnemonic('H');
115                help.add(new AboutAction(this));
116                help.add(new DocsAction(this));
117                help.add(new TipsAction(this));
118    
119                toolBar.add(help);
120            }
121    
122            return toolBar;
123        }
124    
125        void open(String name, InputStream inputStream) {
126            openAction.open(name, inputStream);
127        }
128    
129        public static JXStatusBar createStatusBar() {
130    
131            JXStatusBar statusBar = new JXStatusBar();
132    
133            statusBar.setLayout(new BorderLayout());
134            statusBar.add(Memory.createMemoryButton(), BorderLayout.EAST);
135    
136            return statusBar;
137        }
138    
139        public void createView(final String title, JComponent component) {
140            tabbedPane.addTab(title, component);
141            tabbedPane.setSelectedComponent(component);
142        }
143    
144        private void checkTabs() {
145            boolean enableClose = tabbedPane.getTabCount() > 0;
146            closeAction.setEnabled(enableClose);
147            closeAllAction.setEnabled(enableClose);
148        }
149    
150        private class ComponentListener extends ComponentAdapter {
151    
152            public void componentResized(ComponentEvent e) {
153                DesktopPreferences.setWindowBounds(getBounds());
154            }
155    
156            public void componentMoved(ComponentEvent e) {
157                DesktopPreferences.setWindowBounds(getBounds());
158            }
159        }
160    
161        private class TabChangeListener implements ContainerListener {
162    
163            public void componentAdded(ContainerEvent e) {
164                checkTabs();
165            }
166    
167            public void componentRemoved(ContainerEvent e) {
168                checkTabs();
169            }
170        }
171    }