PageRenderTime 24ms CodeModel.GetById 131ms app.highlight 8ms RepoModel.GetById 33ms app.codeStats 0ms

/src/mpv5/ui/misc/Position.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 119 lines | 55 code | 14 blank | 50 comment | 3 complexity | 26a42dd59272477b88d7b698c28dea96 MD5 | raw file
  1/*
  2 *  This file is part of YaBS.
  3 *
  4 *      YaBS is free software: you can redistribute it and/or modify
  5 *      it under the terms of the GNU General Public License as published by
  6 *      the Free Software Foundation, either version 3 of the License, or
  7 *      (at your option) any later version.
  8 *
  9 *      YaBS is distributed in the hope that it will be useful,
 10 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 11 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 12 *      GNU General Public License for more details.
 13 *
 14 *      You should have received a copy of the GNU General Public License
 15 *      along with YaBS.  If not, see <http://www.gnu.org/licenses/>.
 16 */
 17package mpv5.ui.misc;
 18
 19import java.awt.Component;
 20import java.awt.Dimension;
 21import java.awt.GraphicsDevice;
 22import java.awt.GraphicsEnvironment;
 23import java.awt.HeadlessException;
 24import java.awt.Rectangle;
 25import java.awt.Toolkit;
 26import mpv5.logging.Log;
 27
 28/**
 29 * This is a helper class for visual component positioning
 30 * 
 31 */
 32public class Position {
 33
 34    private Component comp;
 35
 36    /**
 37     * Centers the given Component
 38     * @param comp
 39     */
 40    public Position(Component comp) {
 41        this.comp = comp;
 42        center();
 43    }
 44
 45    /**
 46     *
 47     * @param comp
 48     * @param doNotCenter
 49     */
 50    public Position(Component comp, boolean doNotCenter) {
 51        this.comp = comp;
 52        if (!doNotCenter) {
 53            center();
 54        }
 55    }
 56
 57    /**
 58     * Moves the component to the bottom left corner
 59     */
 60    public void bottomLeft() {
 61        Dimension frameSize = new Dimension(comp.getSize());
 62        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
 63        comp.setLocation(0, (int) (screenSize.getHeight() - frameSize.getHeight()));
 64    }
 65
 66    /**
 67     * Centers the given Component
 68     */
 69    public void center() {
 70
 71//        Dimension frameSize = new Dimension(comp.getSize());
 72//        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
 73//
 74//        int top = (screenSize.height - frameSize.height) / 2;
 75//        int left = (screenSize.width - frameSize.width) / 2;
 76//
 77//        comp.setSize(frameSize);
 78//        comp.setLocation(left, top);
 79
 80        try {
 81            Dimension w = comp.getSize();
 82            GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
 83            Rectangle r = ge.getDefaultScreenDevice().getDefaultConfiguration().getBounds();
 84            Dimension d = new Dimension(r.width, r.height);
 85            comp.setLocation((d.width - w.width) / 2, (d.height - w.height) / 2);
 86        } catch (Exception e) {
 87            Log.Debug(e);
 88        }
 89    }
 90
 91    /**
 92     * Returns TRUE if the component is not as big as the screen
 93     * @return
 94     */
 95    public boolean isNotMaximized() {
 96        Dimension frameSize = new Dimension(comp.getSize());
 97        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
 98
 99        if (frameSize.width < screenSize.width) {
100            return true;
101        } else {
102            return false;
103        }
104    }
105
106    /**
107     * Moves the component to the upper left corner
108     */
109    public void topLeft() {
110        comp.setLocation(0, 0);
111    }
112
113    public void bottomRight() {
114        Dimension frameSize = new Dimension(comp.getSize());
115        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
116        comp.setLocation((int) (screenSize.getWidth() - frameSize.getWidth()), (int) (screenSize.getHeight() - frameSize.getHeight()));
117    }
118}
119