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
;
26 import java
.awt
.event
.ActionEvent
;
27 import java
.awt
.event
.ActionListener
;
28 import java
.util
.ArrayList
;
29 import java
.util
.List
;
33 * This component is used to configure list of folders with add/remove buttons.
35 public class PathsChooserComponent
{
36 private JPanel myContentPane
;
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
);
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
) {
65 final String path
= file
.getPath();
66 if (processor
.addPath(myWorkingCollection
, path
)){
67 myListModel
.addElement(path
);
73 myRemoveButton
.addActionListener(new ActionListener() {
74 public void actionPerformed(ActionEvent e
) {
75 int selected
= myList
.getSelectedIndex();
78 final String path
= (String
) myListModel
.get(selected
);
79 if (processor
.removePath(myWorkingCollection
, path
)){
80 myListModel
.remove(selected
);
87 public JPanel
getContentPane() {
91 public List
<String
> getValues(){
92 return myWorkingCollection
;
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
);