/** * @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); }
/** * @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); } } }
private static void calcSelectedComponentsImpl(final ArrayList<RadComponent> result, final RadContainer container) { if (container.isSelected()) { if (container.getParent() != null) { // ignore RadRootContainer result.add(container); return; } } for (int i = 0; i < container.getComponentCount(); i++) { final RadComponent component = container.getComponent(i); if (component instanceof RadContainer) { calcSelectedComponentsImpl(result, (RadContainer)component); } else { if (component.isSelected()) { result.add(component); } } } }
@Nullable public static RadContainer getRadContainerAt(final RadRootContainer rootContainer, final int x, final int y, int epsilon) { RadComponent component = getRadComponentAt(rootContainer, x, y); if (isNullOrRoot(component) && epsilon > 0) { // try to find component near specified location component = getRadComponentAt(rootContainer, x - epsilon, y - epsilon); if (isNullOrRoot(component)) component = getRadComponentAt(rootContainer, x - epsilon, y + epsilon); if (isNullOrRoot(component)) component = getRadComponentAt(rootContainer, x + epsilon, y - epsilon); if (isNullOrRoot(component)) component = getRadComponentAt(rootContainer, x + epsilon, y + epsilon); } if (component != null) { return component instanceof RadContainer ? (RadContainer)component : component.getParent(); } return null; }
public static void selectSingleComponent(final GuiEditor editor, final RadComponent component) { final RadContainer root = (RadContainer)getRoot(component); if (root == null) return; ComponentTreeBuilder builder = DesignerToolWindowManager.getInstance(editor).getComponentTreeBuilder(); // this can return null if the click to select the control also requested to grab the focus - // the component tree will be instantiated after the event has been processed completely if (builder != null) { builder.beginUpdateSelection(); } try { clearSelection(root); selectComponent(editor, component); editor.setSelectionAnchor(component); editor.scrollComponentInView(component); } finally { if (builder != null) { builder.endUpdateSelection(); } } }
public static void selectComponents(final GuiEditor editor, List<RadComponent> components) { if (components.size() > 0) { RadComponent component = components.get(0); ComponentTreeBuilder builder = DesignerToolWindowManager.getInstance(editor).getComponentTreeBuilder(); if (builder == null) { // race condition when handling event? return; } builder.beginUpdateSelection(); try { clearSelection((RadContainer)getRoot(component)); for (RadComponent aComponent : components) { selectComponent(editor, aComponent); } } finally { builder.endUpdateSelection(); } } }
public int[] getSelectedCells(@Nullable final Point dragOrigin) { ArrayList<Integer> selection = new ArrayList<Integer>(); RadContainer container = getSelectedGridContainer(); if (container == null) { return ArrayUtil.EMPTY_INT_ARRAY; } int size = getCellCount(); for(int i=0; i<size; i++) { if (mySelectionModel.isSelectedIndex(i)) { selection.add(i); } } if (selection.size() == 0 && dragOrigin != null) { int cell = getCellAt(dragOrigin); if (cell >= 0) { return new int[] { cell }; } } int[] result = new int[selection.size()]; for(int i=0; i<selection.size(); i++) { result [i] = selection.get(i).intValue(); } return result; }
private void collectUnknownLayoutManagerClasses(final Project project, final SnapShotRemoteComponent rc, final Set<String> layoutManagerClasses) throws IOException { RadComponentFactory factory = InsertComponentProcessor.getRadComponentFactory(project, rc.getClassName()); if (factory instanceof RadContainer.Factory && rc.getLayoutManager().length() > 0 && !LayoutManagerRegistry.isKnownLayoutClass(rc.getLayoutManager())) { layoutManagerClasses.add(rc.getLayoutManager()); } SnapShotRemoteComponent[] children = rc.getChildren(); if (children == null) { children = myClient.listChildren(rc.getId()); rc.setChildren(children); } for(SnapShotRemoteComponent child: children) { collectUnknownLayoutManagerClasses(project, child, layoutManagerClasses); } }
@Override public boolean appliesTo(final RadComponent component) { @NonNls String name = getName(); //noinspection SimplifiableIfStatement if (name.equals(SwingProperties.PREFERRED_SIZE) || name.equals(SwingProperties.MINIMUM_SIZE) || name.equals(SwingProperties.MAXIMUM_SIZE)) { // our own properties must be used instead final RadContainer parent = component.getParent(); return parent != null && !(parent.getLayoutManager() instanceof RadGridLayoutManager); } // check if property is available in the JDK used by the module containing the component final PsiManager psiManager = PsiManager.getInstance(component.getProject()); final GlobalSearchScope scope = component.getModule().getModuleWithDependenciesAndLibrariesScope(true); PsiClass componentClass = JavaPsiFacade.getInstance(psiManager.getProject()).findClass(component.getComponentClassName(), scope); if (componentClass == null) return true; final PsiMethod[] psiMethods = componentClass.findMethodsByName(myReadMethod.getName(), true); for(PsiMethod method: psiMethods) { if (!method.hasModifierProperty(PsiModifier.STATIC) && method.getParameterList().getParametersCount() == 0) { return true; } } return false; }
@NotNull public Property[] getChildren(final RadComponent component) { if (!(component instanceof RadContainer)) return Property.EMPTY_ARRAY; BorderType borderType = ((RadContainer)component).getBorderType(); if (borderType.equals(BorderType.EMPTY)) { return new Property[]{new MyTypeProperty(), new MySizeProperty(this), new MyTitleProperty(), new MyTitleIntEnumProperty(this, "title justification", true), new MyTitleIntEnumProperty(this, "title position", false), new MyTitleFontProperty(this), new MyBorderColorProperty(this, true)}; } else if (borderType.equals(BorderType.LINE)) { return new Property[]{new MyTypeProperty(), new MyBorderColorProperty(this, false), new MyTitleProperty(), new MyTitleIntEnumProperty(this, "title justification", true), new MyTitleIntEnumProperty(this, "title position", false), new MyTitleFontProperty(this), new MyBorderColorProperty(this, true)}; } return myChildren; }
public boolean canStartDragging(DnDAction action, Point dragOrigin) { LOG.debug("canStartDragging(): dragOrigin=" + dragOrigin); if (myResizeLine != -1) { LOG.debug("canStartDragging(): have resize line"); return false; } RadContainer container = getSelectedGridContainer(); if (container != null && container.getGridLayoutManager().getGridLineNear(mySelectedContainer, myIsRow, dragOrigin, 4) != -1) { LOG.debug("canStartDragging(): have gridline near"); return false; } int[] selectedCells = getSelectedCells(dragOrigin); for(int cell: selectedCells) { if (!canDragCell(cell)) { LOG.debug("canStartDragging(): cannot drag cell"); return false; } } LOG.debug("canStartDragging(): starting drag"); return true; }
private static void refreshImpl(final RadComponent component) { if (component.getParent() != null) { final Dimension size = component.getSize(); final int oldWidth = size.width; final int oldHeight = size.height; Util.adjustSize(component.getDelegee(), component.getConstraints(), size); if (oldWidth != size.width || oldHeight != size.height) { if (component.getParent().isXY()) { component.setSize(size); } component.getDelegee().invalidate(); } } if (component instanceof RadContainer) { component.refresh(); final RadContainer container = (RadContainer)component; for (int i = container.getComponentCount() - 1; i >= 0; i--) { refreshImpl(container.getComponent(i)); } } }
private static void flattenSimple(final RadContainer container) { RadContainer parent = container.getParent(); RadComponent child = null; Object childLayoutConstraints = null; if (container.getComponentCount() == 1) { child = container.getComponent(0); childLayoutConstraints = container.getCustomLayoutConstraints(); child.getConstraints().restore(container.getConstraints()); container.removeComponent(child); } int childIndex = parent.indexOfComponent(container); FormEditingUtil.deleteComponents(Collections.singletonList(container), false); if (child != null) { if (childLayoutConstraints != null) { child.setCustomLayoutConstraints(childLayoutConstraints); } parent.addComponent(child, childIndex); child.revalidate(); } }
private static boolean canFlatten(final RadComponent c) { if (!(c instanceof RadContainer)) { return false; } if (c.getParent() instanceof RadRootContainer) { return false; } RadContainer container = (RadContainer) c; if (container.getLayoutManager().isGrid() && container.getParent().getLayoutManager().isGrid()) { return true; } if (container.getComponentCount() <= 1) { return true; } return false; }
public void propertyChange(final PropertyChangeEvent e) { if(RadComponent.PROP_SELECTED.equals(e.getPropertyName())){ final Boolean selected = (Boolean)e.getNewValue(); selectionChanged((RadComponent)e.getSource(), selected.booleanValue()); } else if(RadContainer.PROP_CHILDREN.equals(e.getPropertyName())){ final RadComponent[] oldChildren = (RadComponent[])e.getOldValue(); for(int i = oldChildren.length - 1; i >= 0; i--){ deinstall(oldChildren[i]); } final RadComponent[] newChildren = (RadComponent[])e.getNewValue(); for(int i = newChildren.length - 1; i >= 0; i--){ install(newChildren[i]); } } }
public int compare(final NodeDescriptor descriptor1, final NodeDescriptor descriptor2) { if (descriptor1 instanceof ComponentPtrDescriptor && descriptor2 instanceof ComponentPtrDescriptor) { final RadComponent component1 = ((ComponentPtrDescriptor)descriptor1).getComponent(); final RadComponent component2 = ((ComponentPtrDescriptor)descriptor2).getComponent(); if (component1 == null || component2 == null) { return 0; } final RadContainer container1 = component1.getParent(); final RadContainer container2 = component2.getParent(); if(Comparing.equal(container1, container2)){ return indexOf(container1, component1) - indexOf(container2, component2); } else{ return 0; } }else{ return 0; } }
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; }
/** * @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); }
/** * 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 void checkSelectionChanged() { RadContainer container = getSelectedGridContainer(); if (container != mySelectedContainer) { mySelectedContainer = container; mySelectionModel.clearSelection(); repaint(); } }
private static void deleteEmptyGridCells(final RadContainer parent, final GridConstraints delConstraints, final boolean isRow) { final RadAbstractGridLayoutManager layoutManager = parent.getGridLayoutManager(); for (int cell = delConstraints.getCell(isRow) + delConstraints.getSpan(isRow) - 1; cell >= delConstraints.getCell(isRow); cell--) { if (cell < parent.getGridCellCount(isRow) && GridChangeUtil.canDeleteCell(parent, cell, isRow) == GridChangeUtil.CellStatus.Empty && !layoutManager.isGapCell(parent, isRow, cell)) { layoutManager.deleteGridCells(parent, cell, isRow); } } }
@Nullable public static RadContainer getSelectionParent(final List<RadComponent> selection) { RadContainer parent = null; for (RadComponent c : selection) { if (parent == null) { parent = c.getParent(); } else if (parent != c.getParent()) { parent = null; break; } } return parent; }
private static boolean isChild(RadContainer maybeChild, RadComponent maybeParent) { while (maybeChild != null) { if (maybeParent == maybeChild) { return true; } maybeChild = maybeChild.getParent(); } return false; }
public void testMoveRowUp() throws Exception { final RadContainer grid = SampleGrid.create(); GridChangeUtil.moveCells(grid, true, new int[] { 5 }, 3); assertEquals(4, grid.getComponent(3).getConstraints().getRow()); assertEquals(5, grid.getComponent(4).getConstraints().getRow()); assertEquals(3, grid.getComponent(5).getConstraints().getRow()); }
public RadContainer getSelectedContainer() { // when the selected component changes, and we have focus, PropertyInspector asks us about our container and selection. // PropertyInspector's selection changed listener can be called before our own listener, so we need to update ourselves // so that we don't return stale and invalid data. checkSelectionChanged(); return mySelectedContainer; }
private static int insertGridCells(RadContainer container, int cell, int cellsToInsert, boolean canGrow, boolean isRow, boolean isBefore, GridConstraints[] constraintsToAdjust) { int insertedCells = 1; for(int i=0; i<cellsToInsert; i++) { insertedCells = container.getGridLayoutManager().insertGridCells(container, cell, isRow, isBefore, canGrow); } // for insert after, shift only by one cell + possibly one gap cell, not by entire number of insertions if (!isBefore) { cell += insertedCells; } checkAdjustConstraints(constraintsToAdjust, isRow, cell, insertedCells); return cell; }
public Insets getValue(final RadContainer component) { if (component.getLayout() instanceof AbstractLayout) { final AbstractLayout layoutManager=(AbstractLayout) component.getLayout(); return layoutManager.getMargin(); } return DEFAULT_INSETS; }
public void testMoveRowDown() throws Exception { final RadContainer grid = SampleGrid.create(); GridChangeUtil.moveCells(grid, true, new int[] { 3 }, 6); assertEquals(5, grid.getComponent(3).getConstraints().getRow()); assertEquals(3, grid.getComponent(4).getConstraints().getRow()); assertEquals(4, grid.getComponent(5).getConstraints().getRow()); }
private void install(@NotNull final RadComponent component){ component.addPropertyChangeListener(myChangeListener); if(component instanceof RadContainer){ final RadContainer container = (RadContainer)component; for(int i = container.getComponentCount() - 1; i>= 0; i--){ install(container.getComponent(i)); } } }
private void deinstall(@NotNull final RadComponent component){ component.removePropertyChangeListener(myChangeListener); if(component instanceof RadContainer){ final RadContainer container = (RadContainer)component; for(int i = container.getComponentCount() - 1; i>= 0; i--){ deinstall(container.getComponent(i)); } } }
protected void setValueImpl(final RadContainer component, final Integer value) throws Exception { if (myJustification) { component.setBorderTitleJustification(value.intValue()); } else { component.setBorderTitlePosition(value.intValue()); } }
public void test_insert_after_and_before() throws Exception{ for (int i=0; i < SampleGrid.ORIGINAL_COLUMNS-1; i++){ final RadContainer afterCurrent = SampleGrid.create(); GridChangeUtil.insertRowOrColumn(afterCurrent, i, false, false); assertGridDimensions(afterCurrent, SampleGrid.ORIGINAL_ROWS, SampleGrid.ORIGINAL_COLUMNS + 1); final RadContainer beforeNext = SampleGrid.create(); GridChangeUtil.insertRowOrColumn(beforeNext, i+1, false, true); assertGridDimensions(beforeNext, SampleGrid.ORIGINAL_ROWS, SampleGrid.ORIGINAL_COLUMNS + 1); // afterCurrent and beforeNext grids should be same assertGridsEqual(afterCurrent, beforeNext); } }
public FirstComponentInsertLocation(@NotNull final RadContainer container, final Rectangle cellRect, final int xPart, final int yPart) { super(container, 0, 0); myCellRect = cellRect; myXPart = xPart; myYPart = yPart; }
protected void setValueImpl(final RadContainer component, final ColorDescriptor value) throws Exception { if (myTitleColor) { component.setBorderTitleColor(value); } else { component.setBorderColor(value); } }
private void fillOriginalConstraints() { // Store original constraints and parents. This information is required // to restore initial state if drag is canceled. myOriginalConstraints = new GridConstraints[mySelection.size()]; myOriginalBounds = new Rectangle[mySelection.size()]; myOriginalParents = new RadContainer[mySelection.size()]; for (int i1 = 0; i1 < mySelection.size(); i1++) { final RadComponent component = mySelection.get(i1); myOriginalConstraints[i1] = component.getConstraints().store(); myOriginalBounds[i1] = component.getBounds(); myOriginalParents[i1] = component.getParent(); } }
/** * This method synchronizes selection in the tree with the selected * RadComponent in the component hierarchy */ private void syncSelection() { // Found selected components final RadContainer rootContainer=myEditor.getRootContainer(); final ArrayList<RadComponent> selection = new ArrayList<RadComponent>(); FormEditingUtil.iterate( rootContainer, new FormEditingUtil.ComponentVisitor<RadComponent>() { public boolean visit(final RadComponent component) { if(component.isSelected()){ selection.add(component); } return true; } } ); if(selection.size() == 0){ // If there is no selected component in the hierarchy, then // we have to select RadRootContainer selection.add(rootContainer); } final ComponentPtr[] componentPtrs = new ComponentPtr[selection.size()]; for (int i = 0; i < selection.size(); i++) { componentPtrs [i] = new ComponentPtr(myEditor, selection.get(i)); } // Set selection in the tree select(componentPtrs, null); // Notify the ComponentTree that selected component changed myEditor.fireSelectedComponentChanged(); }
public String getValue(RadContainer component) { RadContainer container = component; while(container != null) { final RadLayoutManager layoutManager = container.getLayoutManager(); if (layoutManager != null) { return layoutManager.getName(); } container = container.getParent(); } return UIFormXmlConstants.LAYOUT_INTELLIJ; }
public void test_split() throws Exception{ final RadContainer grid = SampleGrid.create(); GridChangeUtil.splitColumn(grid, 1); assertGridDimensions(grid, SampleGrid.ORIGINAL_ROWS, SampleGrid.ORIGINAL_COLUMNS + 1); assertComponentCellAndSpan(grid, 0, 0, 4); assertComponentCellAndSpan(grid, 1, 1, 4); assertComponentCellAndSpan(grid, 2, 0, 5); assertComponentCellAndSpan(grid, 3, 1, 2); assertComponentCellAndSpan(grid, 4, 3, 3); assertComponentCellAndSpan(grid, 5, 0, 1); }