summaryrefslogtreecommitdiff
path: root/plugins/ui-designer/src/com/intellij/uiDesigner/GridChangeUtil.java
blob: b1aab40ef5ba6998af3696eb93e2772a0ba5e17b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
 * Copyright 2000-2009 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.uiDesigner;

import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.radComponents.RadAbstractGridLayoutManager;
import com.intellij.uiDesigner.radComponents.RadComponent;
import com.intellij.uiDesigner.radComponents.RadContainer;
import org.jetbrains.annotations.NotNull;

import java.awt.*;

/**
 * @author Anton Katilin
 * @author Vladimir Kondratyev
 */
public final class GridChangeUtil {
  private GridChangeUtil() {
  }

  public static void splitColumn(final RadContainer grid, final int columnIndex) {
    splitCell(grid, columnIndex, false);
  }

  public static void splitRow(final RadContainer grid, final int rowIndex) {
    splitCell(grid, rowIndex, true);
  }

  public static boolean isColumnEmpty(final RadContainer grid, final int columnIndex) {
    return canDeleteCell(grid, columnIndex, false) == CellStatus.Empty;
  }

  public static boolean isRowEmpty(final RadContainer grid, final int rowIndex) {
    return canDeleteCell(grid, rowIndex, true) == CellStatus.Empty;
  }

  /**
   * @param cellIndex column or row index, depending on isRow parameter; must be in the range 0..grid.get{Row|Column}Count()-1
   * @param isRow if true, row inserted, otherwise column
   * @param isBefore if true, row/column will be inserted before row/column with given index, otherwise after
   */
  public static void insertRowOrColumn(final RadContainer grid, final int cellIndex, final boolean isRow, final boolean isBefore) {
    check(grid, isRow, cellIndex);

    final RadAbstractGridLayoutManager oldLayout = grid.getGridLayoutManager();

    int beforeIndex = cellIndex;
    if (!isBefore) {
      // beforeIndex can actually be equal to get{Row|Column}Count an case we add row after the last row/column
      beforeIndex++;
    }

    final LayoutManager newLayout = oldLayout.copyLayout(grid.getLayout(), isRow ? 1 : 0, isRow ? 0 : 1);
    GridConstraints[] oldConstraints = copyConstraints(grid);

    for (int i=grid.getComponentCount() - 1; i >= 0; i--){
      final GridConstraints constraints = grid.getComponent(i).getConstraints();
      adjustConstraintsOnInsert(constraints, isRow, beforeIndex, 1);
    }

    grid.setLayout(newLayout);
    fireAllConstraintsChanged(grid, oldConstraints);
  }

  private static GridConstraints[] copyConstraints(RadContainer grid) {
    final GridConstraints[] gridConstraints = new GridConstraints[grid.getComponentCount()];
    for (int i = 0; i < grid.getComponentCount(); i++) {
      gridConstraints [i] = (GridConstraints) grid.getComponent(i).getConstraints().clone();
    }
    return gridConstraints;
  }

  private static void fireAllConstraintsChanged(RadContainer grid, GridConstraints[] oldConstraints) {
    for(int i=grid.getComponentCount()-1; i >= 0; i--) {
      grid.getComponent(i).fireConstraintsChanged(oldConstraints [i]);
    }
  }

  public static void adjustConstraintsOnInsert(final GridConstraints constraints, final boolean isRow, final int beforeIndex,
                                               final int count) {
    if (constraints.getCell(isRow) >= beforeIndex) {
      addToCell(constraints, isRow, count);
    }
    else if (isCellInsideComponent(constraints, isRow, beforeIndex)) {
      // component belongs to the cell being resized - increment component's span
      addToSpan(constraints, isRow, count);
    }
  }

  /**
   * @param cellIndex column or row index, depending on isRow parameter; must be in the range 0..grid.get{Row|Column}Count()-1
   * @param isRow if true, row is splitted, otherwise column
   */
  public static void splitCell(final RadContainer grid, final int cellIndex, final boolean isRow) {
    check(grid, isRow, cellIndex);

    int insertedCells = grid.getGridLayoutManager().insertGridCells(grid, cellIndex, isRow, false, false);

    for (int i=grid.getComponentCount() - 1; i >= 0; i--){
      final RadComponent component = grid.getComponent(i);
      final GridConstraints constraints = component.getConstraints();

      if (constraints.getCell(isRow) + constraints.getSpan(isRow) - 1 == cellIndex) {
        // component belongs to the cell being resized - increment component's span
        GridConstraints oldConstraints = (GridConstraints)constraints.clone();
        constraints.setSpan(isRow, constraints.getSpan(isRow) + insertedCells);
        component.fireConstraintsChanged(oldConstraints);
      }
    }
  }

  public enum CellStatus {
    Empty, Redundant, CanShift, Required
  }

  /**
   * @param cellIndex column or row index, depending on isRow parameter; must be in the range 0..grid.get{Row|Column}Count()-1
   * @param isRow if true, row is deleted, otherwise column
   * @return whether the specified column can be deleted
   */
 public static CellStatus canDeleteCell(@NotNull final RadContainer grid, final int cellIndex, final boolean isRow) {
    check(grid, isRow, cellIndex);

    // Do not allow to delete the single row/column
    if(isRow && grid.getGridRowCount() <= grid.getGridLayoutManager().getMinCellCount()) {
      return CellStatus.Required;
    }
    else if(!isRow && grid.getGridColumnCount() <= grid.getGridLayoutManager().getMinCellCount()) {
      return CellStatus.Required;
    }

    boolean haveComponents = false;
    boolean haveOrigins = false;
    boolean haveSingleSpan = false;
    for (int i = 0; i < grid.getComponentCount(); i++) {
      final GridConstraints constraints = grid.getComponent(i).getConstraints();
      final int cell = constraints.getCell(isRow);
      final int span = constraints.getSpan(isRow);

      if (cellIndex >= cell && cellIndex < cell+span) {
        haveComponents = true;
        if (cellIndex == cell) {
          haveOrigins = true;
          if (span == 1) {
            haveSingleSpan = true;
          }
        }
      }
    }
    if (haveSingleSpan)
      return CellStatus.Required;
    if (haveOrigins)
      return CellStatus.CanShift;
    if (haveComponents)
      return CellStatus.Redundant;
    return CellStatus.Empty;
  }

  public static boolean canDeleteCells(final RadContainer grid, final int[] cells, final boolean row) {
    // for multiple cells, we can't determine if deleting all cells will have a correct result
    for(int cell: cells) {
      CellStatus status = canDeleteCell(grid, cell, row);
      if (status != CellStatus.Empty) {
        if (cells.length == 1 && status == CellStatus.Redundant) {
          return true;
        }
        return false;
      }
    }
    return true;    
  }

  /**
   * @param cellIndex column or row index, depending on isRow parameter; must be in the range 0..grid.get{Row|Column}Count()-1
   * @param isRow if true, row is deleted, otherwise column
   */
  public static void deleteCell(final RadContainer grid, final int cellIndex, final boolean isRow) {
    check(grid, isRow, cellIndex);
    if (canDeleteCell(grid, cellIndex, isRow) == CellStatus.Required) {
      throw new IllegalArgumentException("cell cannot be deleted");
    }

    final RadAbstractGridLayoutManager oldLayout = grid.getGridLayoutManager();

    final LayoutManager newLayout = oldLayout.copyLayout(grid.getLayout(), isRow ? -1 : 0, isRow ? 0 : -1);
    GridConstraints[] oldConstraints = copyConstraints(grid);

    for (int i=grid.getComponentCount() - 1; i >= 0; i--){
      final GridConstraints constraints = grid.getComponent(i).getConstraints();

      if (constraints.getCell(isRow) > cellIndex) {
        // component starts after the cell being deleted - move it
        addToCell(constraints, isRow, -1);
      }
      else if (isCellInsideComponent(constraints, isRow, cellIndex)) {
        // component belongs to the cell being deleted - decrement component's span
        addToSpan(constraints, isRow, -1);
      }
    }

    grid.setLayout(newLayout);
    fireAllConstraintsChanged(grid, oldConstraints);
  }


  private static boolean isCellInsideComponent(final GridConstraints constraints, final boolean isRow, final int cellIndex) {
    final int cell = constraints.getCell(isRow);
    final int span = constraints.getSpan(isRow);
    return cell <= cellIndex && cellIndex <= cell + span - 1;
  }

  /**
   * check whether passed container is grid and cellIndex is in proper range
   */
  private static void check(@NotNull RadContainer grid, final boolean isRow, final int cellIndex){
    if (!grid.getLayoutManager().isGrid()){
      throw new IllegalArgumentException("container must be grid");
    }

    final int cellCount = isRow ? grid.getGridRowCount() : grid.getGridColumnCount();
    if (cellIndex == 0 && cellCount == 0) return;
    if (cellIndex < 0 || cellIndex >= cellCount) {
      throw new IllegalArgumentException("invalid index: " + cellIndex);
    }
  }

  private static void addToCell(final GridConstraints constraints, final boolean isRow, final int delta){
    if (isRow) {
      constraints.setRow(constraints.getRow() + delta);
    }
    else {
      constraints.setColumn(constraints.getColumn() + delta);
    }
  }

  private static void addToSpan(final GridConstraints constraints, final boolean isRow, final int delta){
    if (isRow) {
      constraints.setRowSpan(constraints.getRowSpan() + delta);
    }
    else {
      constraints.setColSpan(constraints.getColSpan() + delta);
    }
  }

  public static void moveCells(final RadContainer container, final boolean isRow, final int[] cellsToMove, int targetCell) {
    for(int i=0; i<cellsToMove.length; i++) {
      final int sourceCell = cellsToMove[i];
      moveCell(container, isRow, sourceCell, targetCell);
      if (sourceCell < targetCell) {
        for(int j=i+1; j<cellsToMove.length; j++) {
          cellsToMove [j]--;
        }
      }
      else {
        targetCell++;
      }
    }
  }

  public static void moveCell(final RadContainer container, final boolean isRow, final int sourceCell, int targetCell) {
    if (targetCell == sourceCell || targetCell == sourceCell+1) return;
    // if column moved to left - components inbetween move to right, and vice versa
    int delta = (sourceCell > targetCell) ? 1 : -1;
    int startCell = Math.min(sourceCell, targetCell);
    int endCell = Math.max(sourceCell, targetCell);
    if (sourceCell < targetCell) targetCell--;
    for(RadComponent c: container.getComponents()) {
      GridConstraints constraints = c.getConstraints();
      GridConstraints oldConstraints = (GridConstraints) constraints.clone();
      final int aCell = constraints.getCell(isRow);
      if (aCell == sourceCell) {
        constraints.setCell(isRow, targetCell);
      }
      else if (aCell >= startCell && aCell < endCell) {
        constraints.setCell(isRow, aCell + delta);
      }
      c.fireConstraintsChanged(oldConstraints);
    }
  }
}