Gridarta Editor
ScrollLayout.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2015 The Gridarta Developers.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 package net.sf.gridarta.textedit.textarea;
21 
22 import java.awt.Component;
23 import java.awt.Container;
24 import java.awt.Dimension;
25 import java.awt.Insets;
26 import java.awt.LayoutManager;
27 import java.util.ArrayList;
28 import java.util.Collection;
29 import org.jetbrains.annotations.NotNull;
30 import org.jetbrains.annotations.Nullable;
31 
32 public class ScrollLayout implements LayoutManager {
33 
34  public static final String CENTER = "center";
35 
36  public static final String RIGHT = "right";
37 
38  public static final String BOTTOM = "bottom";
39 
44  private static final String LEFT_OF_SCROLL_BAR = "los";
45 
46  @Nullable
47  private Component center;
48 
49  @Nullable
50  private Component right;
51 
52  @Nullable
53  private Component bottom;
54 
55  @NotNull
56  private final Collection<Component> leftOfScrollBar = new ArrayList<>();
57 
58  @NotNull
59  private final Container textArea;
60 
61  public ScrollLayout(@NotNull final Container textArea) {
62  this.textArea = textArea;
63  }
64 
65  @Override
66  public void addLayoutComponent(@NotNull final String name, @NotNull final Component comp) {
67  if (name.equals(CENTER)) {
68  center = comp;
69  } else if (name.equals(RIGHT)) {
70  right = comp;
71  } else if (name.equals(BOTTOM)) {
72  bottom = comp;
73  } else if (name.equals(LEFT_OF_SCROLL_BAR)) {
74  leftOfScrollBar.add(comp);
75  }
76  }
77 
78  @Override
79  public void removeLayoutComponent(@NotNull final Component comp) {
80  if (center == comp) {
81  center = null;
82  }
83 
84  if (right == comp) {
85  right = null;
86  }
87 
88  if (bottom == comp) {
89  bottom = null;
90  } else {
91  leftOfScrollBar.remove(comp);
92  }
93  }
94 
95  @NotNull
96  @Override
97  public Dimension preferredLayoutSize(@NotNull final Container parent) {
98  final Dimension dim = new Dimension();
99  final Insets insets = textArea.getInsets();
100  dim.width = insets.left + insets.right;
101  dim.height = insets.top + insets.bottom;
102 
103  if (center != null) {
104  final Dimension centerPref = center.getPreferredSize();
105  dim.width += centerPref.width;
106  dim.height += centerPref.height;
107  }
108  if (right != null) {
109  final Dimension rightPref = right.getPreferredSize();
110  dim.width += rightPref.width;
111  }
112  if (bottom != null) {
113  final Dimension bottomPref = bottom.getPreferredSize();
114  dim.height += bottomPref.height;
115  }
116 
117  return dim;
118  }
119 
120  @NotNull
121  @Override
122  public Dimension minimumLayoutSize(@NotNull final Container parent) {
123  final Dimension dim = new Dimension();
124  final Insets insets = textArea.getInsets();
125  dim.width = insets.left + insets.right;
126  dim.height = insets.top + insets.bottom;
127 
128  if (center != null) {
129  final Dimension centerPref = center.getMinimumSize();
130  dim.width += centerPref.width;
131  dim.height += centerPref.height;
132  }
133  if (right != null) {
134  final Dimension rightPref = right.getMinimumSize();
135  dim.width += rightPref.width;
136  }
137  if (bottom != null) {
138  final Dimension bottomPref = bottom.getMinimumSize();
139  dim.height += bottomPref.height;
140  }
141 
142  return dim;
143  }
144 
145  @Override
146  public void layoutContainer(@NotNull final Container parent) {
147  final Dimension size = parent.getSize();
148  final Insets insets = parent.getInsets();
149  final int insetsTop = insets.top;
150  int insetsLeft = insets.left;
151  final int insetsBottom = insets.bottom;
152  final int insetsRight = insets.right;
153 
154  final int rightWidth = right != null ? right.getPreferredSize().width : 0;
155  final int bottomHeight = bottom != null ? bottom.getPreferredSize().height : 0;
156  final int centerWidth = size.width - rightWidth - insetsLeft - insetsRight;
157  final int centerHeight = size.height - bottomHeight - insetsTop - insetsBottom;
158 
159  if (center != null) {
160  center.setBounds(insetsLeft, insetsTop, centerWidth, centerHeight);
161  }
162 
163  if (right != null) {
164  right.setBounds(insetsLeft + centerWidth, insetsTop, rightWidth, centerHeight);
165  }
166 
167  // Lay out all status components, in order
168  for (final Component comp : leftOfScrollBar) {
169  final Dimension dim = comp.getPreferredSize();
170  comp.setBounds(insetsLeft, insetsTop + centerHeight, dim.width, bottomHeight);
171  insetsLeft += dim.width;
172  }
173 
174  if (bottom != null) {
175  bottom.setBounds(insetsLeft, insetsTop + centerHeight, size.width - rightWidth - insetsLeft - insetsRight, bottomHeight);
176  }
177  }
178 
179 }
void removeLayoutComponent(@NotNull final Component comp)
final Collection< Component > leftOfScrollBar
Dimension preferredLayoutSize(@NotNull final Container parent)
static final String LEFT_OF_SCROLL_BAR
Adding components with this name to the text area will place them left of the horizontal scroll bar...
ScrollLayout(@NotNull final Container textArea)
Dimension minimumLayoutSize(@NotNull final Container parent)
void addLayoutComponent(@NotNull final String name, @NotNull final Component comp)
void layoutContainer(@NotNull final Container parent)