update copyright
[fedora-idea.git] / java / idea-ui / src / com / intellij / facet / impl / ui / libraries / versions / VersionsComponent.java
blob1b7cb8cc0c21ea426b435d1d3e414487628411a4
1 /*
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.facet.impl.ui.libraries.versions;
18 import com.intellij.facet.ui.libraries.FacetLibrariesValidator;
19 import com.intellij.facet.ui.libraries.FacetLibrariesValidatorDescription;
20 import com.intellij.facet.ui.libraries.LibraryInfo;
21 import com.intellij.openapi.module.Module;
22 import com.intellij.openapi.roots.libraries.JarVersionDetectionUtil;
23 import com.intellij.openapi.util.Pair;
24 import com.intellij.openapi.util.text.StringUtil;
25 import com.intellij.ui.CollectionComboBoxModel;
26 import com.intellij.util.containers.HashSet;
27 import com.intellij.util.containers.hash.HashMap;
28 import org.jetbrains.annotations.NonNls;
29 import org.jetbrains.annotations.NotNull;
30 import org.jetbrains.annotations.Nullable;
32 import javax.swing.*;
33 import java.awt.*;
34 import java.awt.event.ActionEvent;
35 import java.awt.event.ActionListener;
36 import java.util.ArrayList;
37 import java.util.List;
38 import java.util.Map;
39 import java.util.Set;
41 public abstract class VersionsComponent {
42 private JPanel myMainPanel;
43 private static String UNKNOWN_RI_NAME = "Unknown";
45 private final @NotNull Module myModule;
46 private FacetLibrariesValidator myValidator;
48 private ButtonGroup myButtonGroup = new ButtonGroup();
50 private Map<String, Pair<JRadioButton, JComboBox>> myButtons = new HashMap<String, Pair<JRadioButton, JComboBox>>();
52 private LibraryVersionInfo myCurrentVersion = null;
54 public VersionsComponent(@NotNull final Module module, FacetLibrariesValidator validator) {
55 myModule = module;
56 myValidator = validator;
59 public JPanel getJComponent() {
60 if (myMainPanel == null) {
61 init();
63 return myMainPanel;
66 @Nullable
67 public LibraryVersionInfo getCurrentLibraryVersionInfo() {
68 return myCurrentVersion;
71 private void init() {
72 myMainPanel = new JPanel(new GridBagLayout());
74 Set<String> referenceImplementations = getRIs();
76 if (referenceImplementations.size() == 1) {
77 String ri = referenceImplementations.iterator().next();
78 addSingletonReferenceImplementationUI(ri);
80 else {
81 for (String ri : referenceImplementations) {
82 addMultipleReferenceImplementationUI(ri);
84 if (myCurrentVersion == null) {
85 myCurrentVersion = getCurrentVersion(ri);
89 if (myCurrentVersion != null) {
90 Pair<JRadioButton, JComboBox> currentPair = myButtons.get(myCurrentVersion.getRI());
91 if (currentPair != null) {
92 currentPair.first.setSelected(true);
93 currentPair.second.setSelectedItem(myCurrentVersion);
94 for (Pair<JRadioButton, JComboBox> buttonsPair : myButtons.values()) {
95 buttonsPair.second.setEnabled(buttonsPair == currentPair);
102 @Nullable
103 protected String getFacetDetectionClass(@NotNull String currentRI) {
104 return null;
107 @NotNull
108 protected abstract Map<LibraryVersionInfo, List<LibraryInfo>> getLibraries();
110 @Nullable
111 private LibraryVersionInfo getCurrentVersion(@NotNull String currentRI) {
112 String detectionClass = getFacetDetectionClass(currentRI);
113 if (detectionClass != null) {
114 final String version = JarVersionDetectionUtil.detectJarVersion(detectionClass, myModule);
115 if (version != null) {
116 LibraryVersionInfo approximatedVersion = null;
117 for (LibraryVersionInfo info : getLibraries().keySet()) {
118 if (version.equals(info.getVersion())) {
119 return info;
121 if (version.startsWith(info.getVersion())) {
122 approximatedVersion = info;
125 return approximatedVersion;
129 return null;
132 private List<LibraryVersionInfo> getSupportedVersions(@NotNull String ri) {
133 List<LibraryVersionInfo> versions = new ArrayList<LibraryVersionInfo>();
134 for (Map.Entry<LibraryVersionInfo, List<LibraryInfo>> entry : getLibraries().entrySet()) {
135 if (ri.equals(entry.getKey().getRI())) {
136 versions.add(entry.getKey());
140 return versions;
143 private void addSingletonReferenceImplementationUI(@NotNull final String ri) {
144 JComboBox comboBox = createComboBox(ri);
145 addToPanel(new JLabel(ri), comboBox);
146 LibraryVersionInfo version = getCurrentVersion(ri);
147 if (version != null) {
148 comboBox.setSelectedItem(version);
152 private void addMultipleReferenceImplementationUI(@NotNull final String ri) {
153 final JRadioButton radioButton = createRadioButton(ri);
154 final JComboBox comboBox = createComboBox(ri);
156 comboBox.setEnabled(false);
158 addToPanel(radioButton, comboBox);
160 myButtons.put(ri, new Pair<JRadioButton, JComboBox>(radioButton, comboBox));
161 myButtonGroup.add(radioButton);
164 private void addToPanel(@NotNull JComponent first, @NotNull JComponent second) {
165 myMainPanel.add(first, new GridBagConstraints(0, GridBagConstraints.RELATIVE, 1, 1, 1, 0, GridBagConstraints.LINE_START,
166 GridBagConstraints.BOTH, new Insets(2, 2, 2, 2), 0, 0));
167 myMainPanel.add(second,
168 new GridBagConstraints(1, GridBagConstraints.RELATIVE, 1, 1, 1, 0, GridBagConstraints.LINE_END, GridBagConstraints.BOTH,
169 new Insets(2, 2, 2, 2), 0, 0));
172 private JRadioButton createRadioButton(final String ri) {
173 final JRadioButton radioButton = new JRadioButton(ri);
174 radioButton.addActionListener(new ActionListener() {
175 public void actionPerformed(ActionEvent actionEvent) {
176 for (Pair<JRadioButton, JComboBox> pair : myButtons.values()) {
177 if (pair.getFirst().equals(radioButton)) {
178 JComboBox comboBox = pair.second;
179 comboBox.setEnabled(true);
181 LibraryVersionInfo currentVersion = getCurrentVersion(ri);
182 if (currentVersion != null) {
183 comboBox.setSelectedItem(currentVersion);
185 else {
186 if (comboBox.getSelectedIndex() < 0) {
187 comboBox.setSelectedItem(getAppropriateVersion(getSupportedVersions(ri)));
189 else {
190 updateCurrentVersion(comboBox); // activate already selected
194 else {
195 pair.second.setEnabled(false);
200 return radioButton;
203 private JComboBox createComboBox(String ri) {
204 final JComboBox comboBox = new JComboBox();
206 List<LibraryVersionInfo> versions = getSupportedVersions(ri);
207 comboBox.setModel(new CollectionComboBoxModel(versions, null));
209 comboBox.addActionListener(new ActionListener() {
210 public void actionPerformed(ActionEvent e) {
211 updateCurrentVersion(comboBox);
215 return comboBox;
218 private void updateCurrentVersion(JComboBox comboBox) {
219 final LibraryVersionInfo versionInfo = getSelectedVersion(comboBox);
221 if (versionInfo != null) {
222 myCurrentVersion = versionInfo;
223 myValidator.setDescription(getFacetLibrariesValidatorDescription(versionInfo));
224 myValidator.setRequiredLibraries(getRequiredLibraries(versionInfo));
228 protected FacetLibrariesValidatorDescription getFacetLibrariesValidatorDescription(LibraryVersionInfo versionInfo) {
229 return new FacetLibrariesValidatorDescription(versionInfo.getVersion()) {
230 @NonNls
231 public String getDefaultLibraryName() {
232 if (myCurrentVersion != null) {
233 String ri = myCurrentVersion.getRI();
234 String version = myCurrentVersion.getVersion();
236 return StringUtil.isEmptyOrSpaces(ri) ? version : ri + "." + version;
239 return super.getDefaultLibraryName();
244 @Nullable
245 private static LibraryVersionInfo getAppropriateVersion(List<LibraryVersionInfo> versions) {
246 return versions.size() > 0 ? versions.get(0) : null;
249 private LibraryInfo[] getRequiredLibraries(LibraryVersionInfo versionInfo) {
250 List<LibraryInfo> libraryInfos = getLibraries().get(versionInfo);
251 return libraryInfos.toArray(new LibraryInfo[libraryInfos.size()]);
254 @Nullable
255 private static LibraryVersionInfo getSelectedVersion(@NotNull JComboBox comboBox) {
256 final Object version = comboBox.getModel().getSelectedItem();
257 return version instanceof LibraryVersionInfo ? (LibraryVersionInfo)version : null;
261 public FacetLibrariesValidator getValidator() {
262 return myValidator;
265 @NotNull
266 public Module getModule() {
267 return myModule;
270 public Set<String> getRIs() {
271 Set<String> ris = new HashSet<String>();
272 for (LibraryVersionInfo info : getLibraries().keySet()) {
273 String ri = info.getRI();
274 if (!StringUtil.isEmptyOrSpaces(ri)) {
275 ris.add(ri);
277 else {
278 ris.add(UNKNOWN_RI_NAME);
281 return ris;