remove incorrect dependency
[fedora-idea.git] / platform / platform-api / src / com / intellij / ui / PathsChooserComponent.java
blob73f18084953d4dfa1809a4c493af36acc20ccd48
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.ui;
18 import com.intellij.openapi.fileChooser.FileChooserDescriptor;
19 import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory;
20 import com.intellij.openapi.fileChooser.FileChooserDialog;
21 import com.intellij.openapi.fileChooser.FileChooserFactory;
22 import com.intellij.openapi.vfs.VirtualFile;
23 import org.jetbrains.annotations.NotNull;
25 import javax.swing.*;
26 import java.awt.event.ActionEvent;
27 import java.awt.event.ActionListener;
28 import java.util.ArrayList;
29 import java.util.List;
31 /**
32 * @author oleg
33 * This component is used to configure list of folders with add/remove buttons.
35 public class PathsChooserComponent {
36 private JPanel myContentPane;
37 private JList myList;
38 private JButton myAddButton;
39 private JButton myRemoveButton;
40 private DefaultListModel myListModel;
42 private List<String> myWorkingCollection;
43 private List<String> myInitialCollection;
45 public PathsChooserComponent(@NotNull final List<String> collection, @NotNull final PathProcessor processor) {
46 myInitialCollection = collection;
47 myWorkingCollection = new ArrayList<String>(myInitialCollection);
48 myListModel = new DefaultListModel();
49 myList.setModel(myListModel);
51 // fill list
52 reset();
54 // listeners
55 myAddButton.addActionListener(new ActionListener() {
56 public void actionPerformed(ActionEvent e) {
57 final FileChooserDescriptor dirChooser = FileChooserDescriptorFactory.createSingleFolderDescriptor();
58 dirChooser.setShowFileSystemRoots(true);
59 dirChooser.setHideIgnored(true);
60 dirChooser.setTitle(UIBundle.message("file.chooser.default.title"));
61 FileChooserDialog chooser = FileChooserFactory.getInstance().createFileChooser(dirChooser, myContentPane);
62 VirtualFile[] files = chooser.choose(null, null);
63 for (VirtualFile file : files) {
64 // adding to the end
65 final String path = file.getPath();
66 if (processor.addPath(myWorkingCollection, path)){
67 myListModel.addElement(path);
71 });
73 myRemoveButton.addActionListener(new ActionListener() {
74 public void actionPerformed(ActionEvent e) {
75 int selected = myList.getSelectedIndex();
76 if (selected != -1) {
77 // removing index
78 final String path = (String) myListModel.get(selected);
79 if (processor.removePath(myWorkingCollection, path)){
80 myListModel.remove(selected);
84 });
87 public JPanel getContentPane() {
88 return myContentPane;
91 public List<String> getValues(){
92 return myWorkingCollection;
95 public void reset() {
96 myListModel.clear();
97 myWorkingCollection = new ArrayList<String>(myInitialCollection);
98 for (String path : myWorkingCollection) {
99 myListModel.addElement(path);
103 public boolean isModified() {
104 return !myWorkingCollection.equals(myInitialCollection);
107 public void apply() {
108 myInitialCollection.clear();
109 myInitialCollection.addAll(myWorkingCollection);
112 public interface PathProcessor {
113 boolean addPath(List<String> paths, String path);
114 boolean removePath(List<String> paths, String path);