update copyrights
[fedora-idea.git] / platform / forms_rt / src / com / intellij / uiDesigner / core / Util.java
blobedeb753c7788d1a9bcbc33a097ce66fe5605ea4e
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.uiDesigner.core;
18 import java.awt.*;
19 import java.util.ArrayList;
21 public final class Util {
22 private static final Dimension MAX_SIZE = new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
23 public static final int DEFAULT_INDENT = 10;
25 public static Dimension getMinimumSize(final Component component, final GridConstraints constraints, final boolean addIndent){
26 final Dimension size = getSize(constraints.myMinimumSize, component.getMinimumSize());
27 if (addIndent) {
28 size.width += DEFAULT_INDENT * constraints.getIndent();
30 return size;
33 public static Dimension getMaximumSize(final Component component, final GridConstraints constraints, final boolean addIndent){
34 //[anton] we use only our property for maximum size.
35 // JButton reports that its max size = pref size, so it is impossible to make a column of same sized buttons.
36 // Probably there are other bad cases...
37 final Dimension size = getSize(constraints.myMaximumSize, MAX_SIZE);
38 if (addIndent && size.width < MAX_SIZE.width) {
39 size.width += DEFAULT_INDENT * constraints.getIndent();
41 return size;
44 public static Dimension getPreferredSize(final Component component, final GridConstraints constraints, final boolean addIndent) {
45 final Dimension size = getSize(constraints.myPreferredSize, component.getPreferredSize());
46 if (addIndent) {
47 size.width += DEFAULT_INDENT * constraints.getIndent();
49 return size;
52 private static Dimension getSize(final Dimension overridenSize, final Dimension ownSize){
53 final int overridenWidth = overridenSize.width >= 0 ? overridenSize.width : ownSize.width;
54 final int overridenHeight = overridenSize.height >= 0 ? overridenSize.height : ownSize.height;
55 return new Dimension(overridenWidth, overridenHeight);
58 public static void adjustSize(final Component component, final GridConstraints constraints, final Dimension size) {
59 final Dimension minimumSize = getMinimumSize(component, constraints, false);
60 final Dimension maximumSize = getMaximumSize(component, constraints, false);
62 size.width = Math.max(size.width, minimumSize.width);
63 size.height = Math.max(size.height, minimumSize.height);
65 size.width = Math.min(size.width, maximumSize.width);
66 size.height = Math.min(size.height, maximumSize.height);
69 /**
71 * @param elimitated output parameter; will be filled indices (Integers) of eliminated cells. May be null.
72 * @return
73 */
74 public static int eliminate(final int[] cellIndices, final int[] spans, final ArrayList elimitated) {
75 final int size = cellIndices.length;
76 if (size != spans.length){
77 throw new IllegalArgumentException("size mismatch: " + size + ", " + spans.length);
79 if (elimitated != null && elimitated.size() != 0) {
80 throw new IllegalArgumentException("eliminated must be empty");
83 int cellCount = 0;
84 for (int i = 0; i < size; i++) {
85 cellCount = Math.max(cellCount, cellIndices[i] + spans[i]);
88 outer: for (int cell=cellCount - 1; cell >= 0; cell--) {
89 // check if we should eliminate cell
91 boolean starts = false;
92 boolean ends = false;
94 for (int i = 0; i < size; i++) {
95 if (cellIndices[i] == cell) {
96 starts = true;
98 if (cellIndices[i] + spans[i] - 1 == cell) {
99 ends = true;
103 if (starts && ends) {
104 continue;
107 if (elimitated != null){
108 elimitated.add(new Integer(cell));
111 // eliminate cell
112 for (int i = 0; i < size; i++) {
113 final boolean decreaseSpan = cellIndices[i] <= cell && cell < cellIndices[i] + spans[i];
114 final boolean decreaseIndex = cellIndices[i] > cell;
116 if (decreaseSpan) {
117 spans[i]--;
120 if (decreaseIndex) {
121 cellIndices[i]--;
125 cellCount--;
128 return cellCount;