annotate at/ofai/music/util/FrameMargins.java @ 5:bcb4c9697967 tip

Add README and CITATION files
author Chris Cannam
date Tue, 03 Dec 2013 12:58:05 +0000
parents 4c3f5bc01c97
children
rev   line source
Chris@2 1 /*
Chris@2 2 Copyright (C) 2001, 2006 by Simon Dixon
Chris@2 3
Chris@2 4 This program is free software; you can redistribute it and/or modify
Chris@2 5 it under the terms of the GNU General Public License as published by
Chris@2 6 the Free Software Foundation; either version 2 of the License, or
Chris@2 7 (at your option) any later version.
Chris@2 8
Chris@2 9 This program is distributed in the hope that it will be useful,
Chris@2 10 but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@2 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@2 12 GNU General Public License for more details.
Chris@2 13
Chris@2 14 You should have received a copy of the GNU General Public License along
Chris@2 15 with this program (the file gpl.txt); if not, download it from
Chris@2 16 http://www.gnu.org/licenses/gpl.txt or write to the
Chris@2 17 Free Software Foundation, Inc.,
Chris@2 18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Chris@2 19 */
Chris@2 20
Chris@2 21 package at.ofai.music.util;
Chris@2 22
Chris@2 23 import java.awt.*;
Chris@2 24 import javax.swing.*;
Chris@2 25
Chris@2 26 /** Java has problems communicating with window managers like fvwm2.
Chris@2 27 * This class is a workaround to find the size of borders of JFrames in
Chris@2 28 * the current environment, so that programs can size their JFrames correctly.
Chris@2 29 * Since we don't know how big borders are until after the window is created,
Chris@2 30 * we create a dummy JFrame, query the size of its borders, and destroy it.
Chris@2 31 * The values are saved for later calls to this class, so that the dummy JFrame
Chris@2 32 * is only created once.
Chris@2 33 */
Chris@2 34 public class FrameMargins {
Chris@2 35
Chris@2 36 protected static Insets i = null;
Chris@2 37 protected static Dimension insetsWithMenu = null;
Chris@2 38 protected static Dimension insetsWithoutMenu = null;
Chris@2 39 protected static Dimension topLeftWithMenu = null;
Chris@2 40 protected static Dimension topLeftWithoutMenu = null;
Chris@2 41
Chris@2 42 /** Returns the total size of the insets of a JFrame, that is the size of
Chris@2 43 * the title bar, menu bar (if requested) and the borders of the JFrame.
Chris@2 44 * In other words, the return value is the difference in size between the
Chris@2 45 * JFrame itself and its content pane.
Chris@2 46 * @param withMenuFlag indicates whether a menu bar should be included in
Chris@2 47 * the calculations
Chris@2 48 * @return the height and width of the insets of a JFrame, unless
Chris@2 49 * <code>getInsets()</code> returns a ridiculously large value, in which
Chris@2 50 * case we gracefully return a guess of (30,20).
Chris@2 51 */
Chris@2 52 public static Dimension get(boolean withMenuFlag) {
Chris@2 53 if (i == null) {
Chris@2 54 JFrame f = new JFrame("Get size of window borders");
Chris@2 55 JMenuBar mb = new JMenuBar();
Chris@2 56 f.setJMenuBar(mb);
Chris@2 57 mb.add(new JMenu("OK"));
Chris@2 58 f.setVisible(true);
Chris@2 59 i = f.getInsets();
Chris@2 60 f.dispose();
Chris@2 61 if ((i.left>100) || (i.right>100) || (i.top>100) || (i.bottom>100)){
Chris@2 62 i.left = 10; // Code around a bug in getInsets()
Chris@2 63 i.right = 10; // - don't believe ridiculously high values
Chris@2 64 i.top = 20;
Chris@2 65 i.bottom = 10;
Chris@2 66 }
Chris@2 67 insetsWithMenu = new Dimension(i.left + i.right,
Chris@2 68 i.top + i.bottom + mb.getHeight());
Chris@2 69 insetsWithoutMenu = new Dimension(i.left + i.right, i.top+i.bottom);
Chris@2 70 topLeftWithoutMenu = new Dimension(i.left, i.top);
Chris@2 71 topLeftWithMenu = new Dimension(i.left, i.top + mb.getHeight());
Chris@2 72 }
Chris@2 73 return withMenuFlag? insetsWithMenu: insetsWithoutMenu;
Chris@2 74 } // get()
Chris@2 75
Chris@2 76 /** Returns the location of the content pane with respect to its JFrame.
Chris@2 77 * @param withMenuFlag indicates whether a menu bar should be included in
Chris@2 78 * the calculations
Chris@2 79 * @return the x and y offsets of the top left corner of the content pane
Chris@2 80 * from the top left corner of the JFrame
Chris@2 81 */
Chris@2 82 public static Dimension getOrigin(boolean withMenuFlag) {
Chris@2 83 if (i == null)
Chris@2 84 get(withMenuFlag);
Chris@2 85 return withMenuFlag? topLeftWithMenu: topLeftWithoutMenu;
Chris@2 86 } // getOrigin()
Chris@2 87
Chris@2 88 /** Returns the Insets object for a JFrame.
Chris@2 89 * @return the Insets object measuring the size of the borders of a JFrame
Chris@2 90 */
Chris@2 91 public static Insets getFrameInsets() {
Chris@2 92 if (i == null)
Chris@2 93 get(false);
Chris@2 94 return i;
Chris@2 95 } // getFrameInsets()
Chris@2 96
Chris@2 97 } // class FrameMargins