2 * Copyright 2004-2005 Alexey Efimov
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
.ProjectTopics
;
19 import com
.intellij
.openapi
.Disposable
;
20 import com
.intellij
.openapi
.options
.ConfigurationException
;
21 import com
.intellij
.openapi
.project
.ProjectBundle
;
22 import com
.intellij
.openapi
.projectRoots
.Sdk
;
23 import com
.intellij
.openapi
.roots
.*;
24 import com
.intellij
.openapi
.roots
.impl
.storage
.ClasspathStorage
;
25 import com
.intellij
.openapi
.roots
.impl
.storage
.ClasspathStorageProvider
;
26 import com
.intellij
.openapi
.util
.IconLoader
;
27 import com
.intellij
.ui
.OrderPanelListener
;
28 import org
.jetbrains
.annotations
.NotNull
;
32 import java
.util
.HashMap
;
33 import java
.util
.List
;
37 * @author Eugene Zhuravlev
41 public class ClasspathEditor
extends ModuleElementsEditor
implements ModuleRootListener
{
42 public static final String NAME
= ProjectBundle
.message("modules.classpath.title");
43 public static final Icon ICON
= IconLoader
.getIcon("/modules/classpath.png");
45 private ClasspathPanel myPanel
;
47 private ClasspathFormatPanel myClasspathFormatPanel
;
49 public ClasspathEditor(final ModuleConfigurationState state
) {
52 final Disposable disposable
= new Disposable() {
53 public void dispose() {}
56 state
.getProject().getMessageBus().connect(disposable
).subscribe(ProjectTopics
.PROJECT_ROOTS
, this);
57 registerDisposable(disposable
);
60 public boolean isModified() {
61 return super.isModified() || ( myClasspathFormatPanel
!= null && myClasspathFormatPanel
.isModified());
64 public String
getHelpTopic() {
65 return "projectStructure.modules.dependencies";
68 public String
getDisplayName() {
72 public Icon
getIcon() {
76 public void saveData() {
77 myPanel
.stopEditing();
78 flushChangesToModel();
81 public void apply () throws ConfigurationException
{
82 if(myClasspathFormatPanel
!=null) {
83 myClasspathFormatPanel
.apply();
88 public void canApply() throws ConfigurationException
{
90 if (myClasspathFormatPanel
!= null) {
91 final String storageID
= myClasspathFormatPanel
.getSelectedClasspathFormat();
92 ClasspathStorage
.getProvider(storageID
).assertCompatible(getModel());
96 public JComponent
createComponentImpl() {
97 myPanel
= new ClasspathPanel(getState());
99 myPanel
.addListener(new OrderPanelListener() {
100 public void entryMoved() {
101 flushChangesToModel();
105 final JPanel panel
= new JPanel(new BorderLayout());
106 panel
.setBorder(BorderFactory
.createEmptyBorder(6, 6, 6, 6));
107 panel
.add(myPanel
, BorderLayout
.CENTER
);
109 final ModuleJdkConfigurable jdkConfigurable
=
110 new ModuleJdkConfigurable(this, ProjectStructureConfigurable
.getInstance(myProject
).getProjectJdksModel()) {
112 protected ModifiableRootModel
getRootModel() {
113 return getState().getRootModel();
116 panel
.add(jdkConfigurable
.createComponent(), BorderLayout
.NORTH
);
117 jdkConfigurable
.reset();
118 registerDisposable(jdkConfigurable
);
120 List
<ClasspathStorageProvider
> providers
= ClasspathStorage
.getProviders();
121 if(providers
.size()>1){
122 myClasspathFormatPanel
= new ClasspathFormatPanel(providers
);
123 panel
.add(myClasspathFormatPanel
, BorderLayout
.SOUTH
);
129 public void flushChangesToModel() {
130 List
<OrderEntry
> entries
= myPanel
.getEntries();
131 getModel().rearrangeOrderEntries(entries
.toArray(new OrderEntry
[entries
.size()]));
134 public void selectOrderEntry(@NotNull final OrderEntry entry
) {
135 myPanel
.selectOrderEntry(entry
);
138 public void moduleStateChanged() {
139 if (myPanel
!= null) {
140 myPanel
.initFromModel();
144 public void beforeRootsChange(ModuleRootEvent event
) {
147 public void rootsChanged(ModuleRootEvent event
) {
148 myPanel
.rootsChanged();
151 public Sdk
setSdk(final Sdk newJDK
) {
152 final ModifiableRootModel model
= getModel();
153 final Sdk oldSdk
= model
.getSdk();
155 if (newJDK
!= null) {
156 model
.setSdk(newJDK
);
162 if (myPanel
!= null) {
163 myPanel
.forceInitFromModel();
166 flushChangesToModel();
171 private class ClasspathFormatPanel
extends JPanel
{
173 private final JComboBox cbClasspathFormat
;
175 private final Map
<String
,String
> formatIdToDescr
= new HashMap
<String
, String
>();
177 private ClasspathFormatPanel(final List
<ClasspathStorageProvider
> providers
) {
178 super(new GridBagLayout());
179 add(new JLabel(ProjectBundle
.message("project.roots.classpath.format.label")),
180 new GridBagConstraints(0,0,1,1,0.0, 0.0, GridBagConstraints
.NORTHWEST
, GridBagConstraints
.NONE
, new Insets(10, 6, 6, 0), 0, 0));
182 for (ClasspathStorageProvider provider
: providers
){
183 formatIdToDescr
.put ( provider
.getID(), provider
.getDescription());
186 final Object
[] items
= formatIdToDescr
.values().toArray();
187 cbClasspathFormat
= new JComboBox(items
);
188 updateClasspathFormat();
189 add(cbClasspathFormat
,
190 new GridBagConstraints(1,0,1,1,1.0, 0.0, GridBagConstraints
.NORTHWEST
, GridBagConstraints
.NONE
, new Insets(6, 6, 6, 0), 0, 0));
193 private void updateClasspathFormat() {
194 cbClasspathFormat
.setSelectedItem(formatIdToDescr
.get(getModuleClasspathFormat()));
197 private String
getSelectedClasspathFormat() {
198 final String selected
= (String
)cbClasspathFormat
.getSelectedItem();
199 for ( Map
.Entry
<String
,String
> entry
: formatIdToDescr
.entrySet() ) {
200 if ( entry
.getValue().equals(selected
)) {
201 return entry
.getKey();
204 throw new IllegalStateException(selected
);
208 private String
getModuleClasspathFormat() {
209 return ClasspathStorage
.getStorageType(getModel().getModule());
212 boolean isModified () {
213 return cbClasspathFormat
!= null && !getSelectedClasspathFormat().equals(getModuleClasspathFormat());
216 void apply () throws ConfigurationException
{
217 final String storageID
= getSelectedClasspathFormat();
218 ClasspathStorage
.getProvider(storageID
).assertCompatible(getModel());
219 ClasspathStorage
.setStorageType(getModel(), storageID
);