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
.openapi
.roots
.ui
.configuration
;
18 import com
.intellij
.ide
.util
.ElementsChooser
;
19 import com
.intellij
.openapi
.project
.ProjectBundle
;
20 import com
.intellij
.openapi
.roots
.LibraryOrderEntry
;
21 import com
.intellij
.openapi
.roots
.ModifiableRootModel
;
22 import com
.intellij
.openapi
.roots
.OrderEntry
;
23 import com
.intellij
.openapi
.roots
.libraries
.Library
;
24 import com
.intellij
.openapi
.roots
.libraries
.LibraryTable
;
25 import com
.intellij
.openapi
.roots
.ui
.configuration
.libraryEditor
.LibraryTableEditor
;
29 import java
.awt
.event
.ActionEvent
;
30 import java
.awt
.event
.ActionListener
;
32 import java
.util
.List
;
35 * @author Eugene Zhuravlev
39 public class NamedLibrariesPanel
extends JPanel
{
40 private final ModifiableRootModel myRootModel
;
41 private final LibraryTable myLibraryTable
;
42 private final ElementsChooser
<LibraryChooserElement
> myLibrariesChooser
;
43 private final ElementsChooser
.ElementsMarkListener
<LibraryChooserElement
> myMarkListener
;
44 private final JButton myIncludeAllButton
;
45 private final JButton myExcludeAllButton
;
46 private final LibraryTable
.Listener myLibrariesUpdateListener
;
48 public NamedLibrariesPanel(ModifiableRootModel rootModel
, LibraryTable libraryTable
) {
49 super(new BorderLayout());
51 myRootModel
= rootModel
;
52 myLibraryTable
= libraryTable
;
53 myLibrariesUpdateListener
= new LibraryTable
.Listener() {
54 public void afterLibraryAdded(Library newLibrary
) {
55 updateChooser(null, true, null);
58 public void afterLibraryRenamed(Library library
) {
59 updateChooser(null, true, null);
62 public void beforeLibraryRemoved(Library library
) {
65 public void afterLibraryRemoved(Library library
) {
66 updateChooser(null, true, null);
69 myLibrariesChooser
= new ElementsChooser
<LibraryChooserElement
>(true);
70 myLibrariesChooser
.setColorUnmarkedElements(false);
72 myMarkListener
= new ElementsChooser
.ElementsMarkListener
<LibraryChooserElement
>() {
73 public void elementMarkChanged(LibraryChooserElement libraryElement
, boolean isMarked
) {
74 setChooserElementMarked(libraryElement
, isMarked
);
77 myLibrariesChooser
.addElementsMarkListener(myMarkListener
);
79 final JButton editButton
= new JButton(ProjectBundle
.message("button.edit"));
80 myIncludeAllButton
= new JButton(ProjectBundle
.message("module.libraries.include.all.button"));
81 myExcludeAllButton
= new JButton(ProjectBundle
.message("module.libraries.exclude.all.button"));
82 editButton
.addActionListener(new ActionListener() {
83 public void actionPerformed(ActionEvent e
) {
84 removeLibraryTableListener();
86 final HashSet
<Library
> librariesBeforeEdit
= new HashSet
<Library
>(Arrays
.asList(myLibraryTable
.getLibraries()));
87 final List
<Library
> selection
= new ArrayList
<Library
>(Arrays
.asList(convertToLibraries(myLibrariesChooser
.getSelectedElements())));
88 final boolean isOk
= LibraryTableEditor
.showEditDialog(editButton
, myLibraryTable
, selection
);
89 final Set
<Library
> librariesAfterEdit
= new HashSet
<Library
>(Arrays
.asList(myLibraryTable
.getLibraries()));
90 librariesAfterEdit
.removeAll(librariesBeforeEdit
);
92 updateChooser(librariesAfterEdit
, !isOk
, selection
);
95 updateChooser(librariesAfterEdit
, !isOk
, null);
99 attachLibraryTableListener();
103 myIncludeAllButton
.addActionListener(new ActionListener() {
104 public void actionPerformed(ActionEvent e
) {
105 myLibrariesChooser
.setAllElementsMarked(true);
108 myExcludeAllButton
.addActionListener(new ActionListener() {
109 public void actionPerformed(ActionEvent e
) {
110 myLibrariesChooser
.setAllElementsMarked(false);
114 final JPanel buttonsPanel
= new JPanel(new GridBagLayout());
115 buttonsPanel
.add(editButton
, new GridBagConstraints(0, GridBagConstraints
.RELATIVE
, 1, 1, 1.0, 0.0, GridBagConstraints
.NORTHWEST
, GridBagConstraints
.HORIZONTAL
, new Insets(0, 2, 5, 0), 0, 0));
116 buttonsPanel
.add(myIncludeAllButton
, new GridBagConstraints(0, GridBagConstraints
.RELATIVE
, 1, 1, 1.0, 0.0, GridBagConstraints
.NORTHWEST
, GridBagConstraints
.HORIZONTAL
, new Insets(0, 2, 5, 0), 0, 0));
117 buttonsPanel
.add(myExcludeAllButton
, new GridBagConstraints(0, GridBagConstraints
.RELATIVE
, 1, 1, 1.0, 1.0, GridBagConstraints
.NORTHWEST
, GridBagConstraints
.HORIZONTAL
, new Insets(0, 2, 0, 0), 0, 0));
119 add(myLibrariesChooser
, BorderLayout
.CENTER
);
120 add(buttonsPanel
, BorderLayout
.EAST
);
122 updateChooser(null, false, null);
123 attachLibraryTableListener();
126 private Library
[] convertToLibraries(final List
<LibraryChooserElement
> selectedElements
) {
127 List
<Library
> libs
= new ArrayList
<Library
>();
128 for (final LibraryChooserElement selectedElement
: selectedElements
) {
129 final LibraryChooserElement chooserElement
= (LibraryChooserElement
)selectedElement
;
130 final Library library
= chooserElement
.getLibrary();
131 if (library
!= null) {
135 return libs
.toArray(new Library
[libs
.size()]);
138 private boolean myListenerAdded
= false;
139 private void attachLibraryTableListener() {
140 if (!myListenerAdded
) {
141 myLibraryTable
.addListener(myLibrariesUpdateListener
);
142 myListenerAdded
= true;
146 private void removeLibraryTableListener() {
147 if (myListenerAdded
) {
148 myLibraryTable
.removeListener(myLibrariesUpdateListener
);
149 myListenerAdded
= false;
153 private void updateChooser(Set
<Library
> librariesToMark
, boolean keepUnmarkedInvalidEntries
, Collection
<Library
> librariesToSelect
) {
154 myLibrariesChooser
.saveSelection();
156 myLibrariesChooser
.removeElementsMarkListener(myMarkListener
);
157 final List
<LibraryChooserElement
> unmarkedInvalidElems
= new ArrayList
<LibraryChooserElement
>();
158 if (keepUnmarkedInvalidEntries
) {
159 final int count
= myLibrariesChooser
.getElementCount();
160 for (int idx
= 0; idx
< count
; idx
++) {
161 final LibraryChooserElement chooserElement
= myLibrariesChooser
.getElementAt(idx
);
162 if (!chooserElement
.isValid() && !chooserElement
.isAttachedToProject()) {
163 unmarkedInvalidElems
.add(chooserElement
);
167 myLibrariesChooser
.clear();
169 final List
<LibraryChooserElement
> elements
= new ArrayList
<LibraryChooserElement
>();
170 final Library
[] libraries
= myLibraryTable
.getLibraries();
171 for (final Library library
: libraries
) {
172 elements
.add(new LibraryChooserElement(library
, myRootModel
.findLibraryOrderEntry(library
)));
174 final LibraryOrderEntry
[] invalidLibraryOrderEntries
= getInvalidLibraryOrderEntries();
175 for (LibraryOrderEntry entry
: invalidLibraryOrderEntries
) {
176 elements
.add(new LibraryChooserElement(null, entry
));
178 elements
.addAll(unmarkedInvalidElems
);
179 Collections
.sort(elements
, new Comparator
<LibraryChooserElement
>() {
180 public int compare(LibraryChooserElement elem1
, LibraryChooserElement elem2
) {
181 return elem1
.getName().compareToIgnoreCase(elem2
.getName());
184 List
<LibraryChooserElement
> elementsToSelect
= new ArrayList
<LibraryChooserElement
>();
185 for (final LibraryChooserElement element
: elements
) {
186 LibraryChooserElement chooserElement
= (LibraryChooserElement
)element
;
187 if (librariesToMark
!= null && chooserElement
.isValid()) {
188 if (librariesToMark
.contains(chooserElement
.getLibrary())) {
189 setChooserElementMarked(chooserElement
, true);
192 final ElementsChooser
.ElementProperties elementProperties
= chooserElement
.isValid()
193 ? LibraryChooserElement
.VALID_LIBRARY_ELEMENT_PROPERTIES
194 : LibraryChooserElement
.INVALID_LIBRARY_ELEMENT_PROPERTIES
;
195 myLibrariesChooser
.addElement(chooserElement
, chooserElement
.isAttachedToProject(), elementProperties
);
196 if (librariesToSelect
!= null && librariesToSelect
.contains(chooserElement
.getLibrary())) {
197 elementsToSelect
.add(chooserElement
);
200 if (elementsToSelect
.size() > 0) {
201 myLibrariesChooser
.selectElements(elementsToSelect
);
204 final int elementCount
= myLibrariesChooser
.getElementCount();
205 myIncludeAllButton
.setEnabled(elementCount
> 0);
206 myExcludeAllButton
.setEnabled(elementCount
> 0);
208 myLibrariesChooser
.addElementsMarkListener(myMarkListener
);
211 if (librariesToSelect
== null) { // do not restore previous selection if there is already stuff to select
212 myLibrariesChooser
.restoreSelection();
217 protected void setChooserElementMarked(LibraryChooserElement chooserElement
, boolean isMarked
) {
219 if (!chooserElement
.isAttachedToProject()) {
220 final LibraryOrderEntry orderEntry
= chooserElement
.isValid()? myRootModel
.addLibraryEntry(chooserElement
.getLibrary()) : myRootModel
.addInvalidLibrary(chooserElement
.getName(), myLibraryTable
.getTableLevel());
221 chooserElement
.setOrderEntry(orderEntry
);
225 if (chooserElement
.isAttachedToProject()) {
226 myRootModel
.removeOrderEntry(chooserElement
.getOrderEntry());
227 chooserElement
.setOrderEntry(null);
232 private LibraryOrderEntry
[] getInvalidLibraryOrderEntries() {
233 final OrderEntry
[] orderEntries
= myRootModel
.getOrderEntries();
234 ArrayList
<LibraryOrderEntry
> entries
= new ArrayList
<LibraryOrderEntry
>();
235 for (OrderEntry orderEntry
: orderEntries
) {
236 if (orderEntry
instanceof LibraryOrderEntry
) {
237 final LibraryOrderEntry libraryOrderEntry
= (LibraryOrderEntry
)orderEntry
;
238 if (!libraryOrderEntry
.isValid() && libraryOrderEntry
.getLibraryLevel().equals(myLibraryTable
.getTableLevel())) {
239 entries
.add(libraryOrderEntry
);
243 return entries
.toArray(new LibraryOrderEntry
[entries
.size()]);
246 public void disposeUIResources() {
247 removeLibraryTableListener();