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
.ModifiableRootModel
;
24 import com
.intellij
.openapi
.roots
.ModuleRootEvent
;
25 import com
.intellij
.openapi
.roots
.ModuleRootListener
;
26 import com
.intellij
.openapi
.roots
.OrderEntry
;
27 import com
.intellij
.openapi
.roots
.impl
.storage
.ClasspathStorage
;
28 import com
.intellij
.openapi
.roots
.impl
.storage
.ClasspathStorageProvider
;
29 import com
.intellij
.openapi
.util
.Disposer
;
30 import com
.intellij
.openapi
.util
.IconLoader
;
31 import com
.intellij
.ui
.OrderPanelListener
;
32 import org
.jetbrains
.annotations
.NotNull
;
36 import java
.util
.HashMap
;
37 import java
.util
.List
;
41 * @author Eugene Zhuravlev
45 public class ClasspathEditor
extends ModuleElementsEditor
implements ModuleRootListener
{
46 public static final String NAME
= ProjectBundle
.message("modules.classpath.title");
47 public static final Icon ICON
= IconLoader
.getIcon("/modules/classpath.png");
49 private ClasspathPanel myPanel
;
51 private ClasspathFormatPanel myClasspathFormatPanel
;
53 public ClasspathEditor(final ModuleConfigurationState state
) {
56 final Disposable disposable
= Disposer
.newDisposable();
58 state
.getProject().getMessageBus().connect(disposable
).subscribe(ProjectTopics
.PROJECT_ROOTS
, this);
59 registerDisposable(disposable
);
62 public boolean isModified() {
63 return super.isModified() || ( myClasspathFormatPanel
!= null && myClasspathFormatPanel
.isModified());
66 public String
getHelpTopic() {
67 return "projectStructure.modules.dependencies";
70 public String
getDisplayName() {
74 public Icon
getIcon() {
78 public void saveData() {
79 myPanel
.stopEditing();
80 flushChangesToModel();
83 public void apply () throws ConfigurationException
{
84 if(myClasspathFormatPanel
!=null) {
85 myClasspathFormatPanel
.apply();
90 public void canApply() throws ConfigurationException
{
92 if (myClasspathFormatPanel
!= null) {
93 final String storageID
= myClasspathFormatPanel
.getSelectedClasspathFormat();
94 ClasspathStorage
.getProvider(storageID
).assertCompatible(getModel());
98 public JComponent
createComponentImpl() {
99 myPanel
= new ClasspathPanel(getState());
101 myPanel
.addListener(new OrderPanelListener() {
102 public void entryMoved() {
103 flushChangesToModel();
107 final JPanel panel
= new JPanel(new BorderLayout());
108 panel
.setBorder(BorderFactory
.createEmptyBorder(6, 6, 6, 6));
109 panel
.add(myPanel
, BorderLayout
.CENTER
);
111 final ModuleJdkConfigurable jdkConfigurable
=
112 new ModuleJdkConfigurable(this, ProjectStructureConfigurable
.getInstance(myProject
).getProjectJdksModel()) {
114 protected ModifiableRootModel
getRootModel() {
115 return getState().getRootModel();
118 panel
.add(jdkConfigurable
.createComponent(), BorderLayout
.NORTH
);
119 jdkConfigurable
.reset();
120 registerDisposable(jdkConfigurable
);
122 List
<ClasspathStorageProvider
> providers
= ClasspathStorage
.getProviders();
123 if(providers
.size()>1){
124 myClasspathFormatPanel
= new ClasspathFormatPanel(providers
);
125 panel
.add(myClasspathFormatPanel
, BorderLayout
.SOUTH
);
131 public void flushChangesToModel() {
132 List
<OrderEntry
> entries
= myPanel
.getEntries();
133 getModel().rearrangeOrderEntries(entries
.toArray(new OrderEntry
[entries
.size()]));
136 public void selectOrderEntry(@NotNull final OrderEntry entry
) {
137 myPanel
.selectOrderEntry(entry
);
140 public void moduleStateChanged() {
141 if (myPanel
!= null) {
142 myPanel
.initFromModel();
146 public void beforeRootsChange(ModuleRootEvent event
) {
149 public void rootsChanged(ModuleRootEvent event
) {
150 if (myPanel
!= null) {
151 myPanel
.rootsChanged();
155 public Sdk
setSdk(final Sdk newJDK
) {
156 final ModifiableRootModel model
= getModel();
157 final Sdk oldSdk
= model
.getSdk();
159 if (newJDK
!= null) {
160 model
.setSdk(newJDK
);
166 if (myPanel
!= null) {
167 myPanel
.forceInitFromModel();
170 flushChangesToModel();
175 private class ClasspathFormatPanel
extends JPanel
{
177 private final JComboBox cbClasspathFormat
;
179 private final Map
<String
,String
> formatIdToDescr
= new HashMap
<String
, String
>();
181 private ClasspathFormatPanel(final List
<ClasspathStorageProvider
> providers
) {
182 super(new GridBagLayout());
183 add(new JLabel(ProjectBundle
.message("project.roots.classpath.format.label")),
184 new GridBagConstraints(0,0,1,1,0.0, 0.0, GridBagConstraints
.NORTHWEST
, GridBagConstraints
.NONE
, new Insets(10, 6, 6, 0), 0, 0));
186 for (ClasspathStorageProvider provider
: providers
){
187 formatIdToDescr
.put ( provider
.getID(), provider
.getDescription());
190 final Object
[] items
= formatIdToDescr
.values().toArray();
191 cbClasspathFormat
= new JComboBox(items
);
192 updateClasspathFormat();
193 add(cbClasspathFormat
,
194 new GridBagConstraints(1,0,1,1,1.0, 0.0, GridBagConstraints
.NORTHWEST
, GridBagConstraints
.NONE
, new Insets(6, 6, 6, 0), 0, 0));
197 private void updateClasspathFormat() {
198 cbClasspathFormat
.setSelectedItem(formatIdToDescr
.get(getModuleClasspathFormat()));
201 private String
getSelectedClasspathFormat() {
202 final String selected
= (String
)cbClasspathFormat
.getSelectedItem();
203 for ( Map
.Entry
<String
,String
> entry
: formatIdToDescr
.entrySet() ) {
204 if ( entry
.getValue().equals(selected
)) {
205 return entry
.getKey();
208 throw new IllegalStateException(selected
);
212 private String
getModuleClasspathFormat() {
213 return ClasspathStorage
.getStorageType(getModel().getModule());
216 boolean isModified() {
217 return cbClasspathFormat
!= null && !getSelectedClasspathFormat().equals(getModuleClasspathFormat());
220 void apply() throws ConfigurationException
{
221 final String storageID
= getSelectedClasspathFormat();
222 ClasspathStorage
.getProvider(storageID
).assertCompatible(getModel());
223 ClasspathStorage
.setStorageType(getModel(), storageID
);