2 * Copyright 2000-2010 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.classpath;
18 import com.intellij.ide.projectView.PresentationData;
19 import com.intellij.ide.util.treeView.NodeDescriptor;
20 import com.intellij.openapi.project.Project;
21 import com.intellij.openapi.roots.libraries.Library;
22 import com.intellij.openapi.roots.libraries.LibraryTable;
23 import com.intellij.openapi.roots.ui.configuration.projectRoot.LibrariesModifiableModel;
24 import com.intellij.openapi.roots.ui.configuration.projectRoot.StructureConfigurableContext;
25 import com.intellij.openapi.util.Condition;
26 import com.intellij.ui.SimpleTextAttributes;
27 import com.intellij.util.Icons;
28 import com.intellij.util.ui.classpath.ChooseLibrariesFromTablesDialog;
29 import org.jetbrains.annotations.NotNull;
30 import org.jetbrains.annotations.Nullable;
33 import java.awt.event.ActionEvent;
34 import java.awt.event.KeyEvent;
39 public class ProjectStructureChooseLibrariesDialog extends ChooseLibrariesFromTablesDialog {
40 private StructureConfigurableContext myContext;
41 private Condition<Library> myAcceptedLibraries;
42 private AddNewLibraryItemAction myNewLibraryAction;
44 public ProjectStructureChooseLibrariesDialog(JComponent parentComponent,
45 @Nullable Project project,
46 StructureConfigurableContext context,
47 Condition<Library> acceptedLibraries, AddNewLibraryItemAction newLibraryAction) {
48 super(parentComponent, "Choose Libraries", project, true);
50 myAcceptedLibraries = acceptedLibraries;
51 myNewLibraryAction = newLibraryAction;
52 setOKButtonText("Add Selected");
58 protected Library[] getLibraries(@NotNull LibraryTable table) {
59 final LibrariesModifiableModel model = getLibrariesModifiableModel(table);
60 if (model == null) return Library.EMPTY_ARRAY;
61 return model.getLibraries();
64 private LibrariesModifiableModel getLibrariesModifiableModel(LibraryTable table) {
65 return myContext.myLevel2Providers.get(table.getTableLevel());
69 protected boolean acceptsElement(Object element) {
70 if (element instanceof Library) {
71 final Library library = (Library)element;
72 return myAcceptedLibraries.value(library);
78 private String getLibraryName(@NotNull Library library) {
79 final LibrariesModifiableModel model = getLibrariesModifiableModel(library.getTable());
81 if (model.hasLibraryEditor(library)) {
82 return model.getLibraryEditor(library).getName();
85 return library.getName();
89 protected Action[] createActions() {
90 return new Action[]{getCancelAction()};
94 protected Action[] createLeftSideActions() {
95 return new Action[]{getOKAction(), new CreateNewLibraryAction()};
99 protected LibrariesTreeNodeBase<Library> createLibraryDescriptor(NodeDescriptor parentDescriptor,
101 final String libraryName = getLibraryName(library);
102 return new LibraryEditorDescriptor(getProject(), parentDescriptor, library, libraryName);
105 private static class LibraryEditorDescriptor extends LibrariesTreeNodeBase<Library> {
106 protected LibraryEditorDescriptor(final Project project, final NodeDescriptor parentDescriptor, final Library element,
107 String libraryName) {
108 super(project, parentDescriptor, element);
109 final PresentationData templatePresentation = getTemplatePresentation();
110 templatePresentation.setIcons(Icons.LIBRARY_ICON);
111 templatePresentation.addText(libraryName, SimpleTextAttributes.REGULAR_ATTRIBUTES);
115 private class CreateNewLibraryAction extends DialogWrapperAction {
116 private CreateNewLibraryAction() {
117 super("New Library...");
118 putValue(MNEMONIC_KEY, KeyEvent.VK_N);
122 protected void doAction(ActionEvent e) {
123 close(CANCEL_EXIT_CODE);
124 myNewLibraryAction.execute();