protected void storeState() {
    if (myRootComponent != null && myExpandedState == null && mySelectionState == null) {
      myExpandedState = new int[myExpandedComponents == null ? 0 : myExpandedComponents.size()][];
      for (int i = 0; i < myExpandedState.length; i++) {
        IntArrayList path = new IntArrayList();
        componentToPath((RadComponent) myExpandedComponents.get(i), path);
        myExpandedState[i] = path.toArray();
      }

      mySelectionState = getSelectionState();

      myExpandedComponents = null;
      myToolProvider.loadDefaultTool();

      mySurfaceArea.removeSelectionListener(mySourceSelectionListener);
      mySurfaceArea.deselectAll();
      mySurfaceArea.addSelectionListener(mySourceSelectionListener);
    }
  }
  private int[][] getSelectionState() {
    List<RadComponent> selection = mySurfaceArea.getSelection();
    int[][] selectionState = new int[selection.size()][];

    for (int i = 0; i < selectionState.length; i++) {
      IntArrayList path = new IntArrayList();
      componentToPath(selection.get(i), path);
      selectionState[i] = path.toArray();
    }

    return selectionState;
  }
  protected void restoreState() {
    DesignerToolWindowManager toolManager = DesignerToolWindowManager.getInstance(getProject());

    if (myExpandedState != null) {
      List<RadComponent> expanded = new ArrayList<RadComponent>();
      for (int[] path : myExpandedState) {
        pathToComponent(expanded, myRootComponent, path, 0);
      }
      myExpandedComponents = expanded;
      toolManager.expandFromState();
      myExpandedState = null;
    }

    List<RadComponent> selection = new ArrayList<RadComponent>();

    int[][] selectionState = mySourceSelectionState.get(getEditorText());
    if (selectionState != null) {
      for (int[] path : selectionState) {
        pathToComponent(selection, myRootComponent, path, 0);
      }
    }

    if (selection.isEmpty()) {
      if (mySelectionState != null) {
        for (int[] path : mySelectionState) {
          pathToComponent(selection, myRootComponent, path, 0);
        }
      }
    }

    if (selection.isEmpty()) {
      toolManager.refresh(true);
    } else {
      mySurfaceArea.setSelection(selection);
    }

    mySelectionState = null;
  }
  private void createDesignerCard() {
    myLayeredPane = new MyLayeredPane();

    mySurfaceArea =
        new ComponentEditableArea(myLayeredPane) {
          @Override
          protected void fireSelectionChanged() {
            super.fireSelectionChanged();
            myLayeredPane.revalidate();
            myLayeredPane.repaint();
          }

          @Override
          public RadComponent findTarget(int x, int y, @Nullable ComponentTargetFilter filter) {
            if (myRootComponent != null) {
              FindComponentVisitor visitor = new FindComponentVisitor(myLayeredPane, filter, x, y);
              myRootComponent.accept(visitor, false);
              return visitor.getResult();
            }
            return null;
          }

          @Override
          public InputTool findTargetTool(int x, int y) {
            return myDecorationLayer.findTargetTool(x, y);
          }

          @Override
          public void showSelection(boolean value) {
            myDecorationLayer.showSelection(value);
          }

          @Override
          public ComponentDecorator getRootSelectionDecorator() {
            return DesignerEditorPanel.this.getRootSelectionDecorator();
          }

          @Nullable
          public EditOperation processRootOperation(OperationContext context) {
            return DesignerEditorPanel.this.processRootOperation(context);
          }

          @Override
          public FeedbackLayer getFeedbackLayer() {
            return myFeedbackLayer;
          }

          @Override
          public RadComponent getRootComponent() {
            return myRootComponent;
          }
        };

    myPaletteListener =
        new ListSelectionListener() {
          @Override
          public void valueChanged(ListSelectionEvent e) {
            if (DesignerToolWindowManager.getInstance(getProject()).getActiveDesigner()
                == DesignerEditorPanel.this) {
              Item paletteItem = (Item) PaletteManager.getInstance(getProject()).getActiveItem();
              if (paletteItem != null) {
                myToolProvider.setActiveTool(
                    new CreationTool(true, createCreationFactory(paletteItem)));
              } else if (myToolProvider.getActiveTool() instanceof CreationTool) {
                myToolProvider.loadDefaultTool();
              }
            }
          }
        };

    myToolProvider =
        new ToolProvider() {
          @Override
          public void loadDefaultTool() {
            setActiveTool(new SelectionTool());
          }

          @Override
          public void setActiveTool(InputTool tool) {
            if (getActiveTool() instanceof CreationTool && !(tool instanceof CreationTool)) {
              PaletteManager.getInstance(getProject()).clearActiveItem();
            }
            super.setActiveTool(tool);
          }

          @Override
          public boolean execute(
              final ThrowableRunnable<Exception> operation,
              String command,
              final boolean updateProperties) {
            final boolean[] is = {true};
            CommandProcessor.getInstance()
                .executeCommand(
                    getProject(),
                    new Runnable() {
                      public void run() {
                        is[0] = DesignerEditorPanel.this.execute(operation, updateProperties);
                      }
                    },
                    command,
                    null);
            return is[0];
          }

          @Override
          public void execute(final List<EditOperation> operations, String command) {
            CommandProcessor.getInstance()
                .executeCommand(
                    getProject(),
                    new Runnable() {
                      public void run() {
                        DesignerEditorPanel.this.execute(operations);
                      }
                    },
                    command,
                    null);
          }

          @Override
          public void showError(@NonNls String message, Throwable e) {
            DesignerEditorPanel.this.showError(message, e);
          }
        };

    myGlassLayer = new GlassLayer(myToolProvider, mySurfaceArea);
    myLayeredPane.add(myGlassLayer, LAYER_GLASS);

    myDecorationLayer = new DecorationLayer(mySurfaceArea);
    myLayeredPane.add(myDecorationLayer, LAYER_DECORATION);

    myFeedbackLayer = new FeedbackLayer();
    myLayeredPane.add(myFeedbackLayer, LAYER_FEEDBACK);

    JPanel content = new JPanel(new GridBagLayout());

    GridBagConstraints gbc = new GridBagConstraints();

    gbc.gridx = 0;
    gbc.gridy = 1;
    gbc.fill = GridBagConstraints.VERTICAL;

    myVerticalCaption = new CaptionPanel(this, false);
    content.add(myVerticalCaption, gbc);

    gbc.gridx = 1;
    gbc.gridy = 0;
    gbc.fill = GridBagConstraints.HORIZONTAL;

    myHorizontalCaption = new CaptionPanel(this, true);
    content.add(myHorizontalCaption, gbc);

    gbc.gridx = 1;
    gbc.gridy = 1;
    gbc.weightx = 1;
    gbc.weighty = 1;
    gbc.fill = GridBagConstraints.BOTH;

    myScrollPane = ScrollPaneFactory.createScrollPane(myLayeredPane);
    myScrollPane.setBackground(Color.WHITE);
    content.add(myScrollPane, gbc);

    myHorizontalCaption.attachToScrollPane(myScrollPane);
    myVerticalCaption.attachToScrollPane(myScrollPane);

    myActionPanel = new DesignerActionPanel(this, myGlassLayer);

    myDesignerCard = new JPanel(new FillLayout());
    myDesignerCard.add(myActionPanel.getToolbarComponent());
    myDesignerCard.add(content);
    add(myDesignerCard, DESIGNER_CARD);

    PaletteManager.getInstance(getProject()).addSelectionListener(myPaletteListener);

    mySourceSelectionListener =
        new ComponentSelectionListener() {
          @Override
          public void selectionChanged(EditableArea area) {
            storeSourceSelectionState();
          }
        };
    mySurfaceArea.addSelectionListener(mySourceSelectionListener);
  }