private void resetFromFile(@NotNull VirtualFile file, @NotNull Project project) {
    final Module moduleForFile = ModuleUtilCore.findModuleForFile(file, project);
    if (moduleForFile == null) {
      return;
    }

    final VirtualFile parent = file.getParent();
    if (parent == null) {
      return;
    }

    if (myModule == null) {
      final Object prev = myModuleCombo.getSelectedItem();
      myModuleCombo.setSelectedItem(moduleForFile);

      if (!moduleForFile.equals(myModuleCombo.getSelectedItem())) {
        myModuleCombo.setSelectedItem(prev);
        return;
      }
    } else if (!myModule.equals(moduleForFile)) {
      return;
    }

    final JCheckBox checkBox = myCheckBoxes.get(parent.getName());
    if (checkBox == null) {
      return;
    }

    for (JCheckBox checkBox1 : myCheckBoxes.values()) {
      checkBox1.setSelected(false);
    }
    checkBox.setSelected(true);
    myFileNameCombo.getEditor().setItem(file.getName());
  }
Beispiel #2
0
 private static void setupSelectionOnPreferredComponent(final JComponent component) {
   if (component instanceof JTextField) {
     JTextField field = (JTextField) component;
     String text = field.getText();
     if (text != null && field.getClientProperty(HAVE_INITIAL_SELECTION) == null) {
       field.setSelectionStart(0);
       field.setSelectionEnd(text.length());
     }
   } else if (component instanceof JComboBox) {
     JComboBox combobox = (JComboBox) component;
     combobox.getEditor().selectAll();
   }
 }
  public CreateXmlResourceDialog(
      @NotNull Module module,
      @NotNull ResourceType resourceType,
      @Nullable String predefinedName,
      @Nullable String predefinedValue,
      boolean chooseName,
      @Nullable VirtualFile defaultFile) {
    super(module.getProject());
    myResourceType = resourceType;

    if (predefinedName != null && predefinedName.length() > 0) {
      if (!chooseName) {
        myNameLabel.setVisible(false);
        myNameField.setVisible(false);
      }
      myNameField.setText(predefinedName);
    }

    if (predefinedValue != null && predefinedValue.length() > 0) {
      myValueLabel.setVisible(false);
      myValueField.setVisible(false);
      myValueField.setText(predefinedValue);
    }
    final Set<Module> modulesSet = new HashSet<Module>();
    modulesSet.add(module);

    for (AndroidFacet depFacet : AndroidUtils.getAllAndroidDependencies(module, true)) {
      modulesSet.add(depFacet.getModule());
    }

    assert modulesSet.size() > 0;

    if (modulesSet.size() == 1) {
      myModule = module;
      myModuleLabel.setVisible(false);
      myModuleCombo.setVisible(false);
    } else {
      myModule = null;

      final Module[] modules = modulesSet.toArray(new Module[modulesSet.size()]);
      Arrays.sort(
          modules,
          new Comparator<Module>() {
            @Override
            public int compare(Module m1, Module m2) {
              return m1.getName().compareTo(m2.getName());
            }
          });

      myModuleCombo.setModel(new DefaultComboBoxModel(modules));
      myModuleCombo.setSelectedItem(module);
      myModuleCombo.setRenderer(new ModuleListCellRendererWrapper(myModuleCombo.getRenderer()));
    }

    if (defaultFile == null) {
      final String defaultFileName = AndroidResourceUtil.getDefaultResourceFileName(resourceType);

      if (defaultFileName != null) {
        myFileNameCombo.getEditor().setItem(defaultFileName);
      }
    }
    myDirectoriesList = new CheckBoxList();
    myDirectoriesLabel.setLabelFor(myDirectoriesList);
    final ToolbarDecorator decorator = ToolbarDecorator.createDecorator(myDirectoriesList);

    decorator.setEditAction(null);
    decorator.disableUpDownActions();

    decorator.setAddAction(
        new AnActionButtonRunnable() {
          @Override
          public void run(AnActionButton button) {
            doAddNewDirectory();
          }
        });

    decorator.setRemoveAction(
        new AnActionButtonRunnable() {
          @Override
          public void run(AnActionButton button) {
            doDeleteDirectory();
          }
        });

    final AnActionButton selectAll =
        new AnActionButton("Select All", null, PlatformIcons.SELECT_ALL_ICON) {
          @Override
          public void actionPerformed(AnActionEvent e) {
            doSelectAllDirs();
          }
        };
    decorator.addExtraAction(selectAll);

    final AnActionButton unselectAll =
        new AnActionButton("Unselect All", null, PlatformIcons.UNSELECT_ALL_ICON) {
          @Override
          public void actionPerformed(AnActionEvent e) {
            doUnselectAllDirs();
          }
        };
    decorator.addExtraAction(unselectAll);

    myDirectoriesPanel.add(decorator.createPanel());

    updateDirectories(true);

    myModuleCombo.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            updateDirectories(true);
          }
        });
    final JCheckBox valuesCheckBox = myCheckBoxes.get(SdkConstants.FD_RES_VALUES);
    if (valuesCheckBox != null) {
      valuesCheckBox.setSelected(true);
    }

    if (defaultFile != null) {
      resetFromFile(defaultFile, module.getProject());
    }
    init();
  }
 @NotNull
 public String getFileName() {
   return ((String) myFileNameCombo.getEditor().getItem()).trim();
 }
  private void updateDirectories(boolean updateFileCombo) {
    final Module module = getModule();
    List<VirtualFile> valuesDirs = Collections.emptyList();

    if (module != null) {
      final AndroidFacet facet = AndroidFacet.getInstance(module);

      if (facet != null) {
        myResourceDir = AndroidRootUtil.getResourceDir(facet);

        if (myResourceDir != null) {
          valuesDirs =
              AndroidResourceUtil.getResourceSubdirs(
                  ResourceFolderType.VALUES.getName(), new VirtualFile[] {myResourceDir});
        }
      }
    }

    Collections.sort(
        valuesDirs,
        new Comparator<VirtualFile>() {
          @Override
          public int compare(VirtualFile f1, VirtualFile f2) {
            return f1.getName().compareTo(f2.getName());
          }
        });

    final Map<String, JCheckBox> oldCheckBoxes = myCheckBoxes;
    final int selectedIndex = myDirectoriesList.getSelectedIndex();
    final String selectedDirName = selectedIndex >= 0 ? myDirNames[selectedIndex] : null;

    final List<JCheckBox> checkBoxList = new ArrayList<JCheckBox>();
    myCheckBoxes = new HashMap<String, JCheckBox>();
    myDirNames = new String[valuesDirs.size()];

    int newSelectedIndex = -1;

    int i = 0;

    for (VirtualFile dir : valuesDirs) {
      final String dirName = dir.getName();
      final JCheckBox oldCheckBox = oldCheckBoxes.get(dirName);
      final boolean selected = oldCheckBox != null && oldCheckBox.isSelected();
      final JCheckBox checkBox = new JCheckBox(dirName, selected);
      checkBoxList.add(checkBox);
      myCheckBoxes.put(dirName, checkBox);
      myDirNames[i] = dirName;

      if (dirName.equals(selectedDirName)) {
        newSelectedIndex = i;
      }
      i++;
    }
    myDirectoriesList.setModel(new CollectionListModel<JCheckBox>(checkBoxList));

    if (newSelectedIndex >= 0) {
      myDirectoriesList.setSelectedIndex(newSelectedIndex);
    }

    if (checkBoxList.size() == 1) {
      checkBoxList.get(0).setSelected(true);
    }

    if (updateFileCombo) {
      final Object oldItem = myFileNameCombo.getEditor().getItem();
      final Set<String> fileNameSet = new HashSet<String>();

      for (VirtualFile valuesDir : valuesDirs) {
        for (VirtualFile file : valuesDir.getChildren()) {
          fileNameSet.add(file.getName());
        }
      }
      final List<String> fileNames = new ArrayList<String>(fileNameSet);
      Collections.sort(fileNames);
      myFileNameCombo.setModel(new DefaultComboBoxModel(fileNames.toArray()));
      myFileNameCombo.getEditor().setItem(oldItem);
    }
  }