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-2023 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  switch (name) {
68  case CENTER:
69  center = comp;
70  break;
71 
72  case RIGHT:
73  right = comp;
74  break;
75 
76  case BOTTOM:
77  bottom = comp;
78  break;
79 
80  case LEFT_OF_SCROLL_BAR:
81  leftOfScrollBar.add(comp);
82  break;
83  }
84  }
85 
86  @Override
87  public void removeLayoutComponent(@NotNull final Component comp) {
88  if (center == comp) {
89  center = null;
90  }
91 
92  if (right == comp) {
93  right = null;
94  }
95 
96  if (bottom == comp) {
97  bottom = null;
98  } else {
99  leftOfScrollBar.remove(comp);
100  }
101  }
102 
103  @NotNull
104  @Override
105  public Dimension preferredLayoutSize(@NotNull final Container parent) {
106  final Dimension dim = new Dimension();
107  final Insets insets = textArea.getInsets();
108  dim.width = insets.left + insets.right;
109  dim.height = insets.top + insets.bottom;
110 
111  if (center != null) {
112  final Dimension centerPref = center.getPreferredSize();
113  dim.width += centerPref.width;
114  dim.height += centerPref.height;
115  }
116  if (right != null) {
117  final Dimension rightPref = right.getPreferredSize();
118  dim.width += rightPref.width;
119  }
120  if (bottom != null) {
121  final Dimension bottomPref = bottom.getPreferredSize();
122  dim.height += bottomPref.height;
123  }
124 
125  return dim;
126  }
127 
128  @NotNull
129  @Override
130  public Dimension minimumLayoutSize(@NotNull final Container parent) {
131  final Dimension dim = new Dimension();
132  final Insets insets = textArea.getInsets();
133  dim.width = insets.left + insets.right;
134  dim.height = insets.top + insets.bottom;
135 
136  if (center != null) {
137  final Dimension centerPref = center.getMinimumSize();
138  dim.width += centerPref.width;
139  dim.height += centerPref.height;
140  }
141  if (right != null) {
142  final Dimension rightPref = right.getMinimumSize();
143  dim.width += rightPref.width;
144  }
145  if (bottom != null) {
146  final Dimension bottomPref = bottom.getMinimumSize();
147  dim.height += bottomPref.height;
148  }
149 
150  return dim;
151  }
152 
153  @Override
154  public void layoutContainer(@NotNull final Container parent) {
155  final Dimension size = parent.getSize();
156  final Insets insets = parent.getInsets();
157  final int insetsTop = insets.top;
158  int insetsLeft = insets.left;
159  final int insetsBottom = insets.bottom;
160  final int insetsRight = insets.right;
161 
162  final int rightWidth = right == null ? 0 : right.getPreferredSize().width;
163  final int bottomHeight = bottom == null ? 0 : bottom.getPreferredSize().height;
164  final int centerWidth = size.width - rightWidth - insetsLeft - insetsRight;
165  final int centerHeight = size.height - bottomHeight - insetsTop - insetsBottom;
166 
167  if (center != null) {
168  center.setBounds(insetsLeft, insetsTop, centerWidth, centerHeight);
169  }
170 
171  if (right != null) {
172  right.setBounds(insetsLeft + centerWidth, insetsTop, rightWidth, centerHeight);
173  }
174 
175  // Lay out all status components, in order
176  for (final Component comp : leftOfScrollBar) {
177  final Dimension dim = comp.getPreferredSize();
178  comp.setBounds(insetsLeft, insetsTop + centerHeight, dim.width, bottomHeight);
179  insetsLeft += dim.width;
180  }
181 
182  if (bottom != null) {
183  bottom.setBounds(insetsLeft, insetsTop + centerHeight, size.width - rightWidth - insetsLeft - insetsRight, bottomHeight);
184  }
185  }
186 
187 }
name
name
Definition: ArchetypeTypeSetParserTest-ignoreDefaultAttribute1-result.txt:2
net.sf.gridarta.textedit.textarea.ScrollLayout.minimumLayoutSize
Dimension minimumLayoutSize(@NotNull final Container parent)
Definition: ScrollLayout.java:130
net.sf.gridarta.textedit.textarea.ScrollLayout.textArea
final Container textArea
Definition: ScrollLayout.java:59
net.sf.gridarta.textedit.textarea.ScrollLayout.removeLayoutComponent
void removeLayoutComponent(@NotNull final Component comp)
Definition: ScrollLayout.java:87
net.sf.gridarta.textedit.textarea.ScrollLayout.addLayoutComponent
void addLayoutComponent(@NotNull final String name, @NotNull final Component comp)
Definition: ScrollLayout.java:66
net.sf.gridarta.textedit.textarea.ScrollLayout
Definition: ScrollLayout.java:32
net.sf.gridarta.textedit.textarea.ScrollLayout.center
Component center
Definition: ScrollLayout.java:47
net.sf.gridarta.textedit.textarea.ScrollLayout.LEFT_OF_SCROLL_BAR
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.
Definition: ScrollLayout.java:44
net.sf.gridarta.textedit.textarea.ScrollLayout.layoutContainer
void layoutContainer(@NotNull final Container parent)
Definition: ScrollLayout.java:154
net.sf.gridarta.textedit.textarea.ScrollLayout.bottom
Component bottom
Definition: ScrollLayout.java:53
net.sf.gridarta.textedit.textarea.ScrollLayout.leftOfScrollBar
final Collection< Component > leftOfScrollBar
Definition: ScrollLayout.java:56
net.sf.gridarta.textedit.textarea.ScrollLayout.BOTTOM
static final String BOTTOM
Definition: ScrollLayout.java:38
net.sf.gridarta.textedit.textarea.ScrollLayout.right
Component right
Definition: ScrollLayout.java:50
net.sf.gridarta.textedit.textarea.ScrollLayout.ScrollLayout
ScrollLayout(@NotNull final Container textArea)
Definition: ScrollLayout.java:61
net.sf.gridarta.textedit.textarea.ScrollLayout.preferredLayoutSize
Dimension preferredLayoutSize(@NotNull final Container parent)
Definition: ScrollLayout.java:105
net.sf.gridarta.textedit.textarea.ScrollLayout.CENTER
static final String CENTER
Definition: ScrollLayout.java:34
net.sf.gridarta.textedit.textarea.ScrollLayout.RIGHT
static final String RIGHT
Definition: ScrollLayout.java:36