many template folders per module in django (PY-595) pycharm/96.681 pycharm/96.682
authorDmitry Trofimov <dmitry.trofimov@jetbrains.com>
Sun, 18 Jul 2010 09:34:44 +0000 (13:34 +0400)
committerDmitry Trofimov <dmitry.trofimov@jetbrains.com>
Sun, 18 Jul 2010 09:39:50 +0000 (13:39 +0400)
platform/lang-impl/src/com/intellij/openapi/projectRoots/ui/PathEditor.java

index d57d4838b94c7d57bc557defcb0d472776245f21..f3ebd49a4ff0456f1a0e0ab6ced5a297eed6ec58 100644 (file)
@@ -38,6 +38,7 @@ import com.intellij.util.Icons;
 import com.intellij.util.containers.HashSet;
 import com.intellij.util.ui.UIUtil;
 import gnu.trove.TIntArrayList;
+import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
 
 import javax.swing.*;
@@ -92,18 +93,18 @@ public class PathEditor {
     return myDisplayName;
   }
 
-  protected void setModified(boolean modified){
+  protected void setModified(boolean modified) {
     myModified = modified;
   }
 
-  public boolean isModified(){
+  public boolean isModified() {
     return myModified;
   }
 
   public void apply(SdkModificator sdkModificator) {
     sdkModificator.removeRoots(myOrderRootType);
     // add all items
-    for (int i = 0; i < getRowCount(); i++){
+    for (int i = 0; i < getRowCount(); i++) {
       sdkModificator.addRoot(getValueAt(i), myOrderRootType);
     }
     setModified(false);
@@ -116,17 +117,30 @@ public class PathEditor {
       return VirtualFile.EMPTY_ARRAY;
     }
     final VirtualFile[] roots = new VirtualFile[count];
-    for (int i = 0; i < count; i++){
+    for (int i = 0; i < count; i++) {
       roots[i] = getValueAt(i);
     }
     return roots;
   }
 
+  public void setPaths(@NotNull List<VirtualFile> paths) {
+    keepSelectionState();
+    clearList();
+    myEnabled = true;
+    if (myEnabled) {
+      for (VirtualFile file : paths) {
+        addElement(file);
+      }
+    }
+    setModified(false);
+    updateButtons();
+  }
+
   public void reset(@Nullable SdkModificator modificator) {
     keepSelectionState();
     clearList();
     myEnabled = modificator != null;
-    if(myEnabled){
+    if (myEnabled) {
       VirtualFile[] files = modificator.getRoots(myOrderRootType);
       for (VirtualFile file : files) {
         addElement(file);
@@ -136,14 +150,14 @@ public class PathEditor {
     updateButtons();
   }
 
-  public JComponent createComponent(){
+  public JComponent createComponent() {
     myPanel = new JPanel(new GridBagLayout());
     Insets anInsets = new Insets(2, 2, 2, 2);
 
     myModel = new DefaultListModel();
     myList = new JBList(myModel);
-    myList.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
-      public void valueChanged(ListSelectionEvent e){
+    myList.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
+      public void valueChanged(ListSelectionEvent e) {
         updateButtons();
       }
     });
@@ -155,10 +169,10 @@ public class PathEditor {
 
     mySpecifyUrlButton.setVisible(isShowUrlButton());
 
-    myAddButton.addActionListener(new ActionListener(){
-      public void actionPerformed(ActionEvent e){
+    myAddButton.addActionListener(new ActionListener() {
+      public void actionPerformed(ActionEvent e) {
         final VirtualFile[] added = doAdd();
-        if (added.length > 0){
+        if (added.length > 0) {
           setModified(true);
         }
         updateButtons();
@@ -166,38 +180,43 @@ public class PathEditor {
         setSelectedRoots(added);
       }
     });
-    myRemoveButton.addActionListener(new ActionListener(){
-      public void actionPerformed(ActionEvent e){
+    myRemoveButton.addActionListener(new ActionListener() {
+      public void actionPerformed(ActionEvent e) {
         List removedItems = ListUtil.removeSelectedItems(myList);
         itemsRemoved(removedItems);
       }
     });
-    mySpecifyUrlButton.addActionListener(new ActionListener(){
-      public void actionPerformed(ActionEvent e){
+    mySpecifyUrlButton.addActionListener(new ActionListener() {
+      public void actionPerformed(ActionEvent e) {
         onSpecifyUrlButtonClicked();
       }
     });
 
     JScrollPane scrollPane = ScrollPaneFactory.createScrollPane(myList);
     scrollPane.setPreferredSize(new Dimension(500, 500));
-    myPanel.add(scrollPane, new GridBagConstraints(0, 0, 1, 8, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, anInsets, 0, 0));
-    myPanel.add(myAddButton, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, anInsets, 0, 0));
-    myPanel.add(myRemoveButton, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, anInsets, 0, 0));
-    myPanel.add(mySpecifyUrlButton, new GridBagConstraints(1, 4, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, anInsets, 0, 0));
-    myPanel.add(Box.createRigidArea(new Dimension(mySpecifyUrlButton.getPreferredSize().width, 4)), new GridBagConstraints(1, 5, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, anInsets, 0, 0));
+    myPanel
+      .add(scrollPane, new GridBagConstraints(0, 0, 1, 8, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, anInsets, 0, 0));
+    myPanel.add(myAddButton,
+                new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, anInsets, 0, 0));
+    myPanel.add(myRemoveButton,
+                new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, anInsets, 0, 0));
+    myPanel.add(mySpecifyUrlButton,
+                new GridBagConstraints(1, 4, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, anInsets, 0, 0));
+    myPanel.add(Box.createRigidArea(new Dimension(mySpecifyUrlButton.getPreferredSize().width, 4)),
+                new GridBagConstraints(1, 5, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, anInsets, 0, 0));
     return myPanel;
   }
 
   private void itemsRemoved(List removedItems) {
     myAllFiles.removeAll(removedItems);
-    if (removedItems.size() > 0){
+    if (removedItems.size() > 0) {
       setModified(true);
     }
     updateButtons();
     requestDefaultFocus();
   }
 
-  private VirtualFile[] doAdd(){
+  private VirtualFile[] doAdd() {
     VirtualFile[] files = FileChooser.chooseFiles(myPanel, myDescriptor);
     files = adjustAddedFileSet(myPanel, files);
     List<VirtualFile> added = new ArrayList<VirtualFile>(files.length);
@@ -212,18 +231,18 @@ public class PathEditor {
   /**
    * Implement this method to adjust adding behavior, this method is called right after the files
    * or directories are selected for added. This method allows adding UI that modify file set.
-   *
+   * <p/>
    * The default implementation returns a value passed the parameter files and does nothing.
    *
    * @param component a component that could be used as a parent.
-   * @param files a selected file set
+   * @param files     a selected file set
    * @return adjusted file set
    */
   protected VirtualFile[] adjustAddedFileSet(final Component component, final VirtualFile[] files) {
     return files;
   }
 
-  protected void updateButtons(){
+  protected void updateButtons() {
     Object[] values = getSelectedRoots();
     myRemoveButton.setEnabled((values.length > 0) && myEnabled);
     myAddButton.setEnabled(myEnabled);
@@ -231,20 +250,20 @@ public class PathEditor {
     mySpecifyUrlButton.setVisible(isShowUrlButton());
   }
 
-  private boolean isUrlInserted(){
-    if(getRowCount() > 0){
+  private boolean isUrlInserted() {
+    if (getRowCount() > 0) {
       return ((VirtualFile)myModel.lastElement()).getFileSystem() instanceof HttpFileSystem;
     }
     return false;
   }
 
-  protected void requestDefaultFocus(){
-    if (myList != null){
+  protected void requestDefaultFocus() {
+    if (myList != null) {
       myList.requestFocus();
     }
   }
 
-  public void addPaths(VirtualFile... paths){
+  public void addPaths(VirtualFile... paths) {
     boolean added = false;
     keepSelectionState();
     for (final VirtualFile path : paths) {
@@ -252,7 +271,7 @@ public class PathEditor {
         added = true;
       }
     }
-    if (added){
+    if (added) {
       setModified(true);
       updateButtons();
     }
@@ -272,25 +291,27 @@ public class PathEditor {
     itemsRemoved(list);
   }
 
-  /** Method adds element only if it is not added yet. */
-  protected boolean addElement(VirtualFile item){
-    if(item == null){
+  /**
+   * Method adds element only if it is not added yet.
+   */
+  protected boolean addElement(VirtualFile item) {
+    if (item == null) {
       return false;
     }
-    if (myAllFiles.contains(item)){
+    if (myAllFiles.contains(item)) {
       return false;
     }
-    if(isUrlInserted()){
+    if (isUrlInserted()) {
       myModel.insertElementAt(item, myModel.size() - 1);
     }
-    else{
+    else {
       myModel.addElement(item);
     }
     myAllFiles.add(item);
     return true;
   }
 
-  protected void setSelectedRoots(Object[] roots){
+  protected void setSelectedRoots(Object[] roots) {
     ArrayList<Object> rootsList = new ArrayList<Object>(roots.length);
     for (Object root : roots) {
       if (root != null) {
@@ -299,110 +320,110 @@ public class PathEditor {
     }
     myList.getSelectionModel().clearSelection();
     int rowCount = myModel.getSize();
-    for (int i = 0; i < rowCount; i++){
+    for (int i = 0; i < rowCount; i++) {
       Object currObject = myModel.get(i);
       LOG.assertTrue(currObject != null);
-      if (rootsList.contains(currObject)){
+      if (rootsList.contains(currObject)) {
         myList.getSelectionModel().addSelectionInterval(i, i);
       }
     }
   }
 
-  private void keepSelectionState(){
+  private void keepSelectionState() {
     final Object[] selectedItems = getSelectedRoots();
 
-    SwingUtilities.invokeLater(new Runnable(){
-      public void run(){
-        if (selectedItems != null){
+    SwingUtilities.invokeLater(new Runnable() {
+      public void run() {
+        if (selectedItems != null) {
           setSelectedRoots(selectedItems);
         }
       }
     });
   }
 
-  protected Object[] getSelectedRoots(){
+  protected Object[] getSelectedRoots() {
     return myList.getSelectedValues();
   }
 
-  private int getRowCount(){
+  private int getRowCount() {
     return myModel.getSize();
   }
 
-  private VirtualFile getValueAt(int row){
+  private VirtualFile getValueAt(int row) {
     return (VirtualFile)myModel.get(row);
   }
 
-  public void clearList(){
+  public void clearList() {
     myModel.clear();
     myAllFiles.clear();
     setModified(true);
   }
 
-  private static boolean isJarFile(final VirtualFile file){
+  private static boolean isJarFile(final VirtualFile file) {
     return ApplicationManager.getApplication().runReadAction(new Computable<Boolean>() {
       public Boolean compute() {
         VirtualFile tempFile = file;
-        if ((file.getFileSystem() instanceof JarFileSystem) && file.getParent() == null){
+        if ((file.getFileSystem() instanceof JarFileSystem) && file.getParent() == null) {
           //[myakovlev] It was bug - directories with *.jar extensions was saved as files of JarFileSystem.
           //    so we can not just return true, we should filter such directories.
           String path = file.getPath().substring(0, file.getPath().length() - JarFileSystem.JAR_SEPARATOR.length());
           tempFile = LocalFileSystem.getInstance().findFileByPath(path);
         }
-        if (tempFile != null && !tempFile.isDirectory()){
+        if (tempFile != null && !tempFile.isDirectory()) {
           return Boolean.valueOf(FileTypeManager.getInstance().getFileTypeByFile(tempFile).equals(FileTypes.ARCHIVE));
         }
         return Boolean.FALSE;
 
       }
-    } ).booleanValue();
+    }).booleanValue();
   }
 
   /**
    * @return icon for displaying parameter (ProjectRoot or VirtualFile)
-   * If parameter is not ProjectRoot or VirtualFile, returns empty icon "/nodes/emptyNode.png"
+   *         If parameter is not ProjectRoot or VirtualFile, returns empty icon "/nodes/emptyNode.png"
    */
-  private static Icon getIconForRoot(Object projectRoot){
-    if (projectRoot instanceof VirtualFile){
+  private static Icon getIconForRoot(Object projectRoot) {
+    if (projectRoot instanceof VirtualFile) {
       final VirtualFile file = (VirtualFile)projectRoot;
-      if (!file.isValid()){
+      if (!file.isValid()) {
         return ICON_INVALID;
       }
-      else if (isHttpRoot(file)){
+      else if (isHttpRoot(file)) {
         return Icons.WEB_ICON;
       }
-      else{
+      else {
         return isJarFile(file) ? Icons.JAR_ICON : Icons.FILE_ICON;
       }
     }
     return ICON_EMPTY;
   }
 
-  private static boolean isHttpRoot(VirtualFile virtualFileOrProjectRoot){
-    if(virtualFileOrProjectRoot != null){
+  private static boolean isHttpRoot(VirtualFile virtualFileOrProjectRoot) {
+    if (virtualFileOrProjectRoot != null) {
       return (virtualFileOrProjectRoot.getFileSystem() instanceof HttpFileSystem);
     }
     return false;
   }
 
-  private final class MyCellRenderer extends DefaultListCellRenderer{
-    private String getPresentableString(final Object value){
+  private final class MyCellRenderer extends DefaultListCellRenderer {
+    private String getPresentableString(final Object value) {
       return ApplicationManager.getApplication().runReadAction(new Computable<String>() {
         public String compute() {
           //noinspection HardCodedStringLiteral
-          return (value instanceof VirtualFile)? ((VirtualFile)value).getPresentableUrl() : "UNKNOWN OBJECT";
+          return (value instanceof VirtualFile) ? ((VirtualFile)value).getPresentableUrl() : "UNKNOWN OBJECT";
         }
       });
     }
 
-    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus){
+    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
       super.getListCellRendererComponent(list, getPresentableString(value), index, isSelected, cellHasFocus);
-      if (isSelected){
+      if (isSelected) {
         setForeground(UIUtil.getListSelectionForeground());
       }
-      else{
-        if (value instanceof VirtualFile){
+      else {
+        if (value instanceof VirtualFile) {
           VirtualFile file = (VirtualFile)value;
-          if (!file.isValid()){
+          if (!file.isValid()) {
             setForeground(INVALID_COLOR);
           }
         }