PageRenderTime 26ms CodeModel.GetById 12ms RepoModel.GetById 0ms 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
Possible License(s): LGPL-3.0, Apache-2.0, GPL-3.0, GPL-2.0, AGPL-3.0, JSON, BSD-3-Clause
  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. */
  17. package mpv5.ui.misc;
  18. import java.awt.Component;
  19. import java.awt.Dimension;
  20. import java.awt.GraphicsDevice;
  21. import java.awt.GraphicsEnvironment;
  22. import java.awt.HeadlessException;
  23. import java.awt.Rectangle;
  24. import java.awt.Toolkit;
  25. import mpv5.logging.Log;
  26. /**
  27. * This is a helper class for visual component positioning
  28. *
  29. */
  30. public class Position {
  31. private Component comp;
  32. /**
  33. * Centers the given Component
  34. * @param comp
  35. */
  36. public Position(Component comp) {
  37. this.comp = comp;
  38. center();
  39. }
  40. /**
  41. *
  42. * @param comp
  43. * @param doNotCenter
  44. */
  45. public Position(Component comp, boolean doNotCenter) {
  46. this.comp = comp;
  47. if (!doNotCenter) {
  48. center();
  49. }
  50. }
  51. /**
  52. * Moves the component to the bottom left corner
  53. */
  54. public void bottomLeft() {
  55. Dimension frameSize = new Dimension(comp.getSize());
  56. Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
  57. comp.setLocation(0, (int) (screenSize.getHeight() - frameSize.getHeight()));
  58. }
  59. /**
  60. * Centers the given Component
  61. */
  62. public void center() {
  63. // Dimension frameSize = new Dimension(comp.getSize());
  64. // Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
  65. //
  66. // int top = (screenSize.height - frameSize.height) / 2;
  67. // int left = (screenSize.width - frameSize.width) / 2;
  68. //
  69. // comp.setSize(frameSize);
  70. // comp.setLocation(left, top);
  71. try {
  72. Dimension w = comp.getSize();
  73. GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
  74. Rectangle r = ge.getDefaultScreenDevice().getDefaultConfiguration().getBounds();
  75. Dimension d = new Dimension(r.width, r.height);
  76. comp.setLocation((d.width - w.width) / 2, (d.height - w.height) / 2);
  77. } catch (Exception e) {
  78. Log.Debug(e);
  79. }
  80. }
  81. /**
  82. * Returns TRUE if the component is not as big as the screen
  83. * @return
  84. */
  85. public boolean isNotMaximized() {
  86. Dimension frameSize = new Dimension(comp.getSize());
  87. Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
  88. if (frameSize.width < screenSize.width) {
  89. return true;
  90. } else {
  91. return false;
  92. }
  93. }
  94. /**
  95. * Moves the component to the upper left corner
  96. */
  97. public void topLeft() {
  98. comp.setLocation(0, 0);
  99. }
  100. public void bottomRight() {
  101. Dimension frameSize = new Dimension(comp.getSize());
  102. Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
  103. comp.setLocation((int) (screenSize.getWidth() - frameSize.getWidth()), (int) (screenSize.getHeight() - frameSize.getHeight()));
  104. }
  105. }