1 /*
2  * aTunes
3  * Copyright (C) Alex Aranda, Sylvain Gaudard and contributors
4  *
5  * See http://www.atunes.org/wiki/index.php?title=Contributing for information about contributors
6  *
7  * http://www.atunes.org
8  * http://sourceforge.net/projects/atunes
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  */
20 
21 package net.sourceforge.atunes.gui.frame;
22 
23 import java.awt.Dimension;
24 import java.awt.Frame;
25 
26 import net.sourceforge.atunes.model.IFrameSize;
27 import net.sourceforge.atunes.model.IStateUI;
28 
29 /**
30  * Calculates window size and applies to a frame
31  * @author alex
32  *
33  */
34 public class WindowSizeCalculator {
35 
36 	private Dimension screenSize;
37 
38 	private Dimension margin;
39 
40 	/**
41 	 * @param screenSize
42 	 */
setScreenSize(Dimension screenSize)43 	public void setScreenSize(Dimension screenSize) {
44 		this.screenSize = screenSize;
45 	}
46 
47 	/**
48 	 * @param margin
49 	 */
setMargin(Dimension margin)50 	public void setMargin(Dimension margin) {
51 		this.margin = margin;
52 	}
53 
54 	/**
55      * Sets the window size.
56      * @param frame
57      * @param stateUI
58      */
setWindowSize(AbstractSingleFrame frame, IStateUI stateUI)59     public void setWindowSize(AbstractSingleFrame frame, IStateUI stateUI) {
60     	IFrameSize frameSize = stateUI.getFrameSize();
61         frame.setMinimumSize(frame.getWindowMinimumSize());
62         if (frameSize.isMaximized()) {
63             setWindowSizeMaximized(frame);
64         } else {
65             Dimension dimension = null;
66             if (frameSize.getWindowWidth() != 0 && frameSize.getWindowHeight() != 0) {
67                 dimension = new Dimension(frameSize.getWindowWidth(), frameSize.getWindowHeight());
68             }
69             if (dimension == null) {
70             	dimension = getDefaultWindowSize();
71             }
72             if (dimension != null) {
73                 frame.setSize(dimension);
74             }
75         }
76     }
77 
78     /**
79      * @param frame
80      */
setWindowSizeMaximized(AbstractSingleFrame frame)81     private final void setWindowSizeMaximized(AbstractSingleFrame frame) {
82         Dimension screen = frame.getToolkit().getScreenSize();
83         frame.setSize(screen.width - margin.width, screen.height - margin.height);
84         frame.setExtendedState(Frame.MAXIMIZED_BOTH);
85     }
86 
87     /**
88      * Calculates default window size
89      * @return
90      */
getDefaultWindowSize()91     public Dimension getDefaultWindowSize() {
92         // Set size always according to main device dimension
93     	return new Dimension(screenSize.width - margin.width, screenSize.height - margin.height);
94     }
95 }
96