001    /* $Id: MiniShell.java 12 2007-07-27 09:48:53Z ringler $
002     * This class is part of the de.mospace.swing library.
003     * Copyright (C) 2005-2006 Moritz Ringler
004     *
005     * This program is free software; you can redistribute it and/or
006     * modify it under the terms of the GNU General Public License
007     * as published by the Free Software Foundation; either version 2
008     * of the License, or (at your option) any later version.
009     *
010     * This program is distributed in the hope that it will be useful,
011     * but WITHOUT ANY WARRANTY; without even the implied warranty of
012     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013     * GNU General Public License for more details.
014     *
015     * You should have received a copy of the GNU General Public License
016     * along with this program; if not, write to the Free Software
017     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
018     */
019    package de.mospace.swing;
020    
021    import java.awt.BorderLayout;
022    import java.awt.Container;
023    import java.awt.Dimension;
024    import java.awt.event.ActionEvent;
025    import java.awt.event.MouseAdapter;
026    import java.awt.event.MouseEvent;
027    import java.util.prefs.Preferences;
028    import javax.swing.AbstractAction;
029    import javax.swing.Action;
030    import javax.swing.JButton;
031    import javax.swing.JComponent;
032    import javax.swing.JDialog;
033    import javax.swing.JFrame;
034    import javax.swing.JPanel;
035    import javax.swing.JSplitPane;
036    import javax.swing.JToolBar;
037    
038    /** This class associates a ProcessIOPane with an instance of JFrame.
039     * The ProcessIOPane can either be docked in
040     * the upper part of the JFrame or float in a separate window owned by the
041     * JFrame. It can be switched between the two states by means of 'float' and
042     * 'dock' buttons. Additionally buttons are provided for the control actions of
043     * the ProcessIOPane.
044     *
045     *  @version $Revision: 12 $ ($Date: 2007-07-27 11:48:53 +0200 (Fr, 27 Jul 2007) $)
046     *  @author Moritz Ringler
047     **/
048    public class MiniShell extends JPanel {
049    
050        private JDialog dialg;
051        private final Action dockAction = new AbstractAction(GLOBALS.getString("Dock")) {
052            public void actionPerformed(ActionEvent e) {
053                dock();
054            }
055        };
056    
057        private JButton dockButton;
058        private final Action floatAction = new AbstractAction(GLOBALS.getString("Float")) {
059            public void actionPerformed(ActionEvent e) {
060                undock();
061            }
062        };
063    
064        private JButton floatButton;
065        private boolean floating= false;
066        private final JSplitPane jsp = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
067        private final JToolBar jtb = new JToolBar();
068        private String name;
069        private JFrame parent;
070        private ProcessIOPane piop;
071        /* must not fail when the ActionEvent is null */
072        private final Action showAction = new AbstractAction(GLOBALS.getString("Console")){
073            public void actionPerformed(ActionEvent e) {
074                if (isFloating()) {
075                    showDialog();
076                } else {
077                    showDocked();
078                }
079            }
080        };
081    
082        private final Action closeAction = new AbstractAction(GLOBALS.getString("Close")){
083            public void actionPerformed(ActionEvent e) {
084                close();
085            }
086        };
087    
088        private boolean close(){
089            if (isFloating()) {
090                dialg.setVisible(false);
091            } else {
092                hideDocked();
093            }
094            return true;
095        }
096    
097        /**
098         * Creates a new MiniShell object.
099         *
100         * @param f the JFrame this MiniShell depends on, must be non-null
101         * @param w the preferred width of this component
102         * @param h the preferred height of this component
103         * @param title the title of this MiniShell when floating
104         */
105        public MiniShell(JFrame f, int w, int h, String title) {
106            this(f, w, h, title, false);
107        }
108    
109        /**
110         * Creates a new MiniShell object.
111         *
112         * @param f the JFrame this MiniShell depends on, must be non-null
113         * @param w the preferred width of this component
114         * @param h the preferred height of this component
115         * @param title the title of this MiniShell when floating
116         */
117        public MiniShell(JFrame f, int w, int h, String title, boolean floating) {
118            init(f, w, h, title, new ProcessIOPane(5, 50){
119                protected boolean builtin(String s){
120                    return  super.builtin(s) || ("exit".equals(s) && close());
121                }
122    
123                protected Preferences getPref(){
124                    return MiniShell.this.getPref().node("ProcessIOPane");
125                }
126            }, floating);
127        }
128    
129        /**
130         * Creates a new MiniShell object from the specified ProcessIOPane.
131         *
132         * @param f the JFrame this MiniShell depends on, must be non-null
133         * @param w the preferred width of this component
134         * @param h the preferred height of this component
135         * @param title the title of this MiniShell when floating
136         * @param pip the ProcessIOPane to wrap
137         */
138        public MiniShell(JFrame f, int w, int h, String title, ProcessIOPane pip) {
139            init(f,w,h,title,pip, false);
140        }
141    
142        private void init(JFrame f, int w, int h, String title, ProcessIOPane pip, boolean floating){
143            this.floating= floating;
144            piop = pip;
145            jtb.setRollover(true);
146            setLayout(new BorderLayout());
147            add(piop, BorderLayout.CENTER);
148            add(jtb, BorderLayout.PAGE_START);
149            Action ashell = piop.getSystemShellAction();
150            JButton jb = addButton(ashell);
151            jb.addMouseListener(new MouseAdapter(){
152                public void mousePressed(MouseEvent e){
153                    if(e.isPopupTrigger()){ //getButton() != MouseEvent.BUTTON1){
154                        piop.setSystemShell(piop.querySystemShell());
155                    }
156                }
157                public void mouseReleased(MouseEvent e){
158                    if(e.isPopupTrigger()){ //getButton() != MouseEvent.BUTTON1){
159                        piop.setSystemShell(piop.querySystemShell());
160                    }
161                }
162            });
163            Action[] piopa = piop.getControlActions();
164            for (int i = 0; i < piopa.length; i++) {
165                addButton(piopa[i]);
166            }
167            parent = f;
168            addButton(closeAction);
169            floatButton = addButton(floatAction);
170            dockButton = addButton(dockAction);
171            dockButton.setVisible(floating);
172            floatButton.setVisible(!floating);
173            setPreferredSize(new Dimension(w, h));
174            jsp.setResizeWeight(0.2);
175            name = title;
176        }
177    
178        protected Preferences getPref(){
179            return Preferences.userNodeForPackage(MiniShell.class);
180        }
181    
182        /**
183         * Returns the action that hides this MiniShell.
184         *
185         * @return the action that hides this MiniShell
186         */
187        public Action getCloseAction() {
188            return closeAction;
189        }
190    
191        /**
192         * Returns whether this MiniShell is currently floating.
193         *
194         * @return <code>true</code> when floating, <code>false</code> when docked
195         */
196        public boolean isFloating() {
197            return floating;//(dialg != null && dialg.getContentPane().isAncestorOf(this));
198        }
199    
200        /**
201         * Returns the ProcessIOPane wrapped by this MiniShell.
202         *
203         * @return the ProcessIOPane wrapped by this MiniShell
204         */
205        public ProcessIOPane getProcessIOPane() {
206            return piop;
207        }
208    
209        /**
210         * Returns the action that makes this MiniShell visible.
211         *
212         * @return the action that makes this MiniShell visible
213         */
214        public Action getShowAction() {
215            return showAction;
216        }
217    
218        /**
219         * Adds a new button to this MiniShell.
220         *
221         * @param a the action to configure the button from
222         * @return the JButton that has been created and added
223         */
224        private JButton addButton(Action a) {
225    
226            JButton jb = new JButton(a);
227    
228            //jb.setBorderPainted(false);
229            jb.setOpaque(false);
230            jb.setBackground(jtb.getBackground());
231            jtb.add(jb);
232            return jb;
233        }
234    
235        /**
236         * Docks this MiniShell into its JFrame.
237         */
238        private void dock() {
239            if (!isFloating()) {
240                return;
241            }
242            hideDialog();
243            showDocked();
244        }
245    
246        /**
247         * Hides this MiniShell when floating.
248         */
249        private void hideDialog() {
250            ((JComponent) dialg.getContentPane()).setPreferredSize(dialg.getSize());
251            dialg.getContentPane().remove(this);
252            dialg.setVisible(false);
253        }
254    
255        /**
256         * Hides this MiniShell when docked.
257         */
258        private void hideDocked() {
259    
260            Dimension parentsize = parent.getContentPane().getSize();
261            parent.setContentPane((Container) jsp.getBottomComponent());
262            ((JComponent) parent.getContentPane()).setPreferredSize(parentsize);
263            jsp.remove(this);
264            jsp.remove(parent.getContentPane());
265            parent.pack();
266        }
267    
268        /**
269         * Shows this MiniShell floating.
270         */
271        private void showDialog() {
272            floating = true;
273            if(dialg == null || ! dialg.isVisible()){
274                Dimension parentsize = parent.getContentPane().getSize();
275                if (dialg == null) {
276                    dialg = new JDialog(parent, name);
277                }
278                dockButton.setVisible(true);
279                floatButton.setVisible(false);
280                dialg.getContentPane().add(this);
281                ((JComponent) parent.getContentPane()).setPreferredSize(parentsize);
282                dialg.pack();
283                dialg.setVisible(true);
284                piop.requestFocus();
285            }
286        }
287    
288        /**
289         * Shows this MiniShell docked.
290         */
291        private void showDocked() {
292            floating = false;
293            if(jsp.getTopComponent() != this){
294            Dimension parentsize = parent.getContentPane().getSize();
295            dockButton.setVisible(false);
296            floatButton.setVisible(true);
297    
298            //Container c = parent.getContentPane();
299            //c.setPreferredSize(c.getSize());
300            jsp.setTopComponent(this);
301            jsp.setBottomComponent(parent.getContentPane());
302            parent.setContentPane(jsp);
303            ((JComponent) parent.getContentPane()).setPreferredSize(parentsize);
304            jsp.resetToPreferredSizes();
305            parent.pack();
306            piop.requestFocus();
307            }
308        }
309    
310        /**
311         * Makes this MiniShell float.
312         */
313        private void undock() {
314            if (isFloating()) {
315                return;
316            }
317            hideDocked();
318            showDialog();
319        }
320    }