public static void updateToolWindows() {
   for (Project project : ProjectManager.getInstance().getOpenProjects()) {
     final ToolWindowManager toolWindowManager = ToolWindowManager.getInstance(project);
     for (String id : toolWindowManager.getToolWindowIds()) {
       final ToolWindow toolWindow = toolWindowManager.getToolWindow(id);
       for (Content content : toolWindow.getContentManager().getContents()) {
         final JComponent component = content.getComponent();
         if (component != null) {
           IJSwingUtilities.updateComponentTreeUI(component);
         }
       }
       final JComponent c = toolWindow.getComponent();
       if (c != null) {
         IJSwingUtilities.updateComponentTreeUI(c);
       }
     }
   }
 }
 private static void updateUI(Window window) {
   if (!window.isDisplayable()) {
     return;
   }
   IJSwingUtilities.updateComponentTreeUI(window);
   Window[] children = window.getOwnedWindows();
   for (Window aChildren : children) {
     updateUI(aChildren);
   }
 }
Esempio n. 3
0
    public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
      if (!myField.isValid()) return; // weird. should not get here when field becomes invalid

      final Collection<PsiReference> refs = ReferencesSearch.search(myField).findAll();
      if (refs.isEmpty()) return;
      Set<PsiReference> refsSet = new HashSet<PsiReference>(refs);
      PsiCodeBlock anchorBlock = findAnchorBlock(refs);
      if (anchorBlock == null)
        return; // was assert, but need to fix the case when obsolete inspection highlighting is
                // left
      if (!CodeInsightUtil.preparePsiElementsForWrite(anchorBlock)) return;
      final PsiElementFactory elementFactory =
          JavaPsiFacade.getInstance(project).getElementFactory();
      final JavaCodeStyleManager styleManager = JavaCodeStyleManager.getInstance(project);
      final String propertyName =
          styleManager.variableNameToPropertyName(myField.getName(), VariableKind.FIELD);
      String localName =
          styleManager.propertyNameToVariableName(propertyName, VariableKind.LOCAL_VARIABLE);
      localName = RefactoringUtil.suggestUniqueVariableName(localName, anchorBlock, myField);
      PsiElement firstElement = getFirstElement(refs);
      boolean mayBeFinal = mayBeFinal(refsSet, firstElement);
      PsiElement newDeclaration = null;
      try {
        final PsiElement anchor = getAnchorElement(anchorBlock, firstElement);
        if (anchor instanceof PsiExpressionStatement
            && ((PsiExpressionStatement) anchor).getExpression()
                instanceof PsiAssignmentExpression) {
          final PsiAssignmentExpression expression =
              (PsiAssignmentExpression) ((PsiExpressionStatement) anchor).getExpression();
          if (expression.getOperationTokenType() == JavaTokenType.EQ
              && expression.getLExpression() instanceof PsiReferenceExpression
              && ((PsiReference) expression.getLExpression()).isReferenceTo(myField)) {
            final PsiExpression initializer = expression.getRExpression();
            final PsiDeclarationStatement decl =
                elementFactory.createVariableDeclarationStatement(
                    localName, myField.getType(), initializer);
            if (!mayBeFinal) {
              PsiUtil.setModifierProperty(
                  ((PsiModifierListOwner) decl.getDeclaredElements()[0]), PsiModifier.FINAL, false);
            }
            newDeclaration = anchor.replace(decl);
            refsSet.remove(expression.getLExpression());
            retargetReferences(elementFactory, localName, refsSet);
          } else {
            newDeclaration =
                addDeclarationWithFieldInitializerAndRetargetReferences(
                    elementFactory, localName, anchorBlock, anchor, refsSet);
          }
        } else {
          newDeclaration =
              addDeclarationWithFieldInitializerAndRetargetReferences(
                  elementFactory, localName, anchorBlock, anchor, refsSet);
        }
      } catch (IncorrectOperationException e) {
        LOG.error(e);
      }

      if (newDeclaration != null) {
        final PsiFile psiFile = myField.getContainingFile();
        final Editor editor = FileEditorManager.getInstance(project).getSelectedTextEditor();
        if (editor != null && IJSwingUtilities.hasFocus(editor.getComponent())) {
          final PsiFile file =
              PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
          if (file == psiFile) {
            editor.getCaretModel().moveToOffset(newDeclaration.getTextOffset());
            editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
          }
        }
      }

      try {
        myField.normalizeDeclaration();
        myField.delete();
      } catch (IncorrectOperationException e) {
        LOG.error(e);
      }
    }
  public ProjectNameWithTypeStep(
      final WizardContext wizardContext, StepSequence sequence, final WizardMode mode) {
    super(wizardContext, mode);
    mySequence = sequence;
    myAdditionalContentPanel.add(
        myModulePanel,
        new GridBagConstraints(
            0,
            GridBagConstraints.RELATIVE,
            1,
            1,
            1,
            1,
            GridBagConstraints.NORTHWEST,
            GridBagConstraints.BOTH,
            new Insets(0, 0, 0, 0),
            0,
            0));
    myHeader.setVisible(myWizardContext.isCreatingNewProject() && !isCreateFromTemplateMode());
    myCreateModuleCb.addActionListener(
        new ActionListener() {
          public void actionPerformed(final ActionEvent e) {
            UIUtil.setEnabled(myInternalPanel, myCreateModuleCb.isSelected(), true);
            fireStateChanged();
          }
        });
    myCreateModuleCb.setSelected(true);
    if (!myWizardContext.isCreatingNewProject()) {
      myInternalPanel.setBorder(null);
    }
    myModuleDescriptionPane.setContentType(UIUtil.HTML_MIME);
    myModuleDescriptionPane.addHyperlinkListener(
        new HyperlinkListener() {
          public void hyperlinkUpdate(HyperlinkEvent e) {
            if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
              try {
                BrowserUtil.launchBrowser(e.getURL().toString());
              } catch (IllegalThreadStateException ex) {
                // it's nnot a problem
              }
            }
          }
        });
    myModuleDescriptionPane.setEditable(false);

    final DefaultListModel defaultListModel = new DefaultListModel();
    for (ModuleBuilder builder : ModuleBuilder.getAllBuilders()) {
      defaultListModel.addElement(builder);
    }
    myTypesList.setModel(defaultListModel);
    myTypesList.setSelectionModel(new PermanentSingleSelectionModel());
    myTypesList.setCellRenderer(
        new DefaultListCellRenderer() {
          public Component getListCellRendererComponent(
              final JList list,
              final Object value,
              final int index,
              final boolean isSelected,
              final boolean cellHasFocus) {
            final Component rendererComponent =
                super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            final ModuleBuilder builder = (ModuleBuilder) value;
            setIcon(builder.getBigIcon());
            setDisabledIcon(builder.getBigIcon());
            setText(builder.getPresentableName());
            return rendererComponent;
          }
        });
    myTypesList.addListSelectionListener(
        new ListSelectionListener() {
          @SuppressWarnings({"HardCodedStringLiteral"})
          public void valueChanged(ListSelectionEvent e) {
            if (e.getValueIsAdjusting()) {
              return;
            }

            final ModuleBuilder typeSelected = (ModuleBuilder) myTypesList.getSelectedValue();

            final StringBuilder sb = new StringBuilder("<html><body><font face=\"Verdana\" ");
            sb.append(SystemInfo.isMac ? "" : "size=\"-1\"").append('>');
            sb.append(typeSelected.getDescription()).append("</font></body></html>");

            myModuleDescriptionPane.setText(sb.toString());

            boolean focusOwner = myTypesList.isFocusOwner();
            fireStateChanged();
            if (focusOwner) {
              SwingUtilities.invokeLater(
                  new Runnable() {
                    public void run() {
                      myTypesList.requestFocusInWindow();
                    }
                  });
            }
          }
        });
    myTypesList.setSelectedIndex(0);
    new DoubleClickListener() {
      @Override
      protected boolean onDoubleClick(MouseEvent e) {
        myWizardContext.requestNextStep();
        return true;
      }
    }.installOn(myTypesList);

    final Dimension preferredSize = calcTypeListPreferredSize(ModuleBuilder.getAllBuilders());
    final JBScrollPane pane = IJSwingUtilities.findParentOfType(myTypesList, JBScrollPane.class);
    pane.setPreferredSize(preferredSize);
    pane.setMinimumSize(preferredSize);

    myNamePathComponent
        .getNameComponent()
        .getDocument()
        .addDocumentListener(
            new DocumentAdapter() {
              protected void textChanged(final DocumentEvent e) {
                if (!myModuleNameChangedByUser) {
                  setModuleName(myNamePathComponent.getNameValue());
                }
              }
            });

    myModuleContentRoot.addBrowseFolderListener(
        ProjectBundle.message("project.new.wizard.module.content.root.chooser.title"),
        ProjectBundle.message("project.new.wizard.module.content.root.chooser.description"),
        myWizardContext.getProject(),
        BrowseFilesListener.SINGLE_DIRECTORY_DESCRIPTOR);

    myNamePathComponent
        .getPathComponent()
        .getDocument()
        .addDocumentListener(
            new DocumentAdapter() {
              protected void textChanged(final DocumentEvent e) {
                if (!myContentRootChangedByUser) {
                  setModuleContentRoot(myNamePathComponent.getPath());
                }
              }
            });
    myModuleName
        .getDocument()
        .addDocumentListener(
            new DocumentAdapter() {
              protected void textChanged(final DocumentEvent e) {
                if (myModuleNameDocListenerEnabled) {
                  myModuleNameChangedByUser = true;
                }
                String path = getDefaultBaseDir(wizardContext);
                final String moduleName = getModuleName();
                if (path.length() > 0
                    && !Comparing.strEqual(moduleName, myNamePathComponent.getNameValue())) {
                  path += "/" + moduleName;
                }
                if (!myContentRootChangedByUser) {
                  final boolean f = myModuleNameChangedByUser;
                  myModuleNameChangedByUser = true;
                  setModuleContentRoot(path);
                  myModuleNameChangedByUser = f;
                }
                if (!myImlLocationChangedByUser) {
                  setImlFileLocation(path);
                }
              }
            });
    myModuleContentRoot
        .getTextField()
        .getDocument()
        .addDocumentListener(
            new DocumentAdapter() {
              protected void textChanged(final DocumentEvent e) {
                if (myContentRootDocListenerEnabled) {
                  myContentRootChangedByUser = true;
                }
                if (!myImlLocationChangedByUser) {
                  setImlFileLocation(myModuleContentRoot.getText());
                }
                if (!myModuleNameChangedByUser) {
                  final String path =
                      FileUtil.toSystemIndependentName(myModuleContentRoot.getText());
                  final int idx = path.lastIndexOf("/");

                  boolean f = myContentRootChangedByUser;
                  myContentRootChangedByUser = true;

                  boolean i = myImlLocationChangedByUser;
                  myImlLocationChangedByUser = true;

                  setModuleName(idx >= 0 ? path.substring(idx + 1) : "");

                  myContentRootChangedByUser = f;
                  myImlLocationChangedByUser = i;
                }
              }
            });

    myModuleFileLocation.addBrowseFolderListener(
        ProjectBundle.message("project.new.wizard.module.file.chooser.title"),
        ProjectBundle.message("project.new.wizard.module.file.description"),
        myWizardContext.getProject(),
        BrowseFilesListener.SINGLE_DIRECTORY_DESCRIPTOR);
    myModuleFileLocation
        .getTextField()
        .getDocument()
        .addDocumentListener(
            new DocumentAdapter() {
              protected void textChanged(final DocumentEvent e) {
                if (myImlLocationDocListenerEnabled) {
                  myImlLocationChangedByUser = true;
                }
              }
            });
    myNamePathComponent
        .getPathComponent()
        .getDocument()
        .addDocumentListener(
            new DocumentAdapter() {
              protected void textChanged(final DocumentEvent e) {
                if (!myImlLocationChangedByUser) {
                  setImlFileLocation(myNamePathComponent.getPath());
                }
              }
            });
    if (wizardContext.isCreatingNewProject()) {
      setModuleName(myNamePathComponent.getNameValue());
      setModuleContentRoot(myNamePathComponent.getPath());
      setImlFileLocation(myNamePathComponent.getPath());
    } else {
      final Project project = wizardContext.getProject();
      assert project != null;
      VirtualFile baseDir = project.getBaseDir();
      if (baseDir != null) { // e.g. was deleted
        final String baseDirPath = baseDir.getPath();
        String moduleName = ProjectWizardUtil.findNonExistingFileName(baseDirPath, "untitled", "");
        String contentRoot = baseDirPath + "/" + moduleName;
        if (!Comparing.strEqual(project.getName(), wizardContext.getProjectName())
            && !wizardContext.isCreatingNewProject()
            && wizardContext.getProjectName() != null) {
          moduleName =
              ProjectWizardUtil.findNonExistingFileName(
                  wizardContext.getProjectFileDirectory(), wizardContext.getProjectName(), "");
          contentRoot = wizardContext.getProjectFileDirectory();
        }
        setModuleName(moduleName);
        setModuleContentRoot(contentRoot);
        setImlFileLocation(contentRoot);
        myModuleName.select(0, moduleName.length());
      }
    }

    if (isCreateFromTemplateMode()) {
      replaceModuleTypeOptions(new JPanel());
    } else {
      final AnAction arrow =
          new AnAction() {
            @Override
            public void actionPerformed(AnActionEvent e) {
              if (e.getInputEvent() instanceof KeyEvent) {
                final int code = ((KeyEvent) e.getInputEvent()).getKeyCode();
                if (!myCreateModuleCb.isSelected()) return;
                int i = myTypesList.getSelectedIndex();
                if (code == KeyEvent.VK_DOWN) {
                  if (++i == myTypesList.getModel().getSize()) return;
                } else if (code == KeyEvent.VK_UP) {
                  if (--i == -1) return;
                }
                myTypesList.setSelectedIndex(i);
              }
            }
          };
      CustomShortcutSet shortcutSet = new CustomShortcutSet(KeyEvent.VK_UP, KeyEvent.VK_DOWN);
      arrow.registerCustomShortcutSet(shortcutSet, myNamePathComponent.getNameComponent());
      arrow.registerCustomShortcutSet(shortcutSet, myModuleName);
    }
  }