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.
20 package com.intellij.codeInsight.intention.impl.config;
22 import com.intellij.codeInsight.CodeInsightBundle;
23 import com.intellij.ide.plugins.IdeaPluginDescriptor;
24 import com.intellij.ide.plugins.PluginManager;
25 import com.intellij.ide.plugins.PluginManagerConfigurable;
26 import com.intellij.ide.plugins.PluginManagerUISettings;
27 import com.intellij.ide.ui.search.SearchUtil;
28 import com.intellij.openapi.application.ApplicationNamesInfo;
29 import com.intellij.openapi.diagnostic.Logger;
30 import com.intellij.openapi.extensions.PluginId;
31 import com.intellij.openapi.fileTypes.FileType;
32 import com.intellij.openapi.fileTypes.ex.FileTypeManagerEx;
33 import com.intellij.openapi.options.ShowSettingsUtil;
34 import com.intellij.openapi.project.Project;
35 import com.intellij.openapi.project.ProjectManager;
36 import com.intellij.ui.HyperlinkLabel;
37 import com.intellij.ui.TitledSeparator;
38 import org.jetbrains.annotations.NonNls;
39 import org.jetbrains.annotations.Nullable;
42 import javax.swing.event.HyperlinkEvent;
43 import javax.swing.event.HyperlinkListener;
45 import java.io.IOException;
47 import java.util.ArrayList;
48 import java.util.List;
50 public class IntentionDescriptionPanel {
51 private static final Logger LOG = Logger.getInstance("#com.intellij.codeInsight.intention.impl.config.IntentionDescriptionPanel");
52 private JPanel myPanel;
54 private JPanel myAfterPanel;
55 private JPanel myBeforePanel;
56 private JEditorPane myDescriptionBrowser;
57 private TitledSeparator myBeforeSeparator;
58 private TitledSeparator myAfterSeparator;
59 private JPanel myPoweredByPanel;
60 private final List<IntentionUsagePanel> myBeforeUsagePanels = new ArrayList<IntentionUsagePanel>();
61 private final List<IntentionUsagePanel> myAfterUsagePanels = new ArrayList<IntentionUsagePanel>();
62 @NonNls private static final String BEFORE_TEMPLATE = "before.java.template";
63 @NonNls private static final String AFTER_TEMPLATE = "after.java.template";
65 public void reset(IntentionActionMetaData actionMetaData, String filter) {
67 final TextDescriptor url = actionMetaData.getDescription();
68 final String description = url == null ?
69 CodeInsightBundle.message("under.construction.string") :
70 SearchUtil.markup(url.getText(), filter);
71 myDescriptionBrowser.setText(description);
72 setupPoweredByPanel(actionMetaData);
74 showUsages(myBeforePanel, myBeforeSeparator, myBeforeUsagePanels, actionMetaData.getExampleUsagesBefore());
75 showUsages(myAfterPanel, myAfterSeparator, myAfterUsagePanels, actionMetaData.getExampleUsagesAfter());
77 SwingUtilities.invokeLater(new Runnable() {
84 catch (IOException e) {
89 private void setupPoweredByPanel(final IntentionActionMetaData actionMetaData) {
90 PluginId pluginId = actionMetaData == null ? null : actionMetaData.getPluginId();
92 if (pluginId == null) {
93 @NonNls String label = "<html><body><b>" + ApplicationNamesInfo.getInstance().getFullProductName() + "</b></body></html>";
94 owner = new JLabel(label);
97 final IdeaPluginDescriptor pluginDescriptor = PluginManager.getPlugin(pluginId);
98 HyperlinkLabel label = new HyperlinkLabel(CodeInsightBundle.message("powered.by.plugin", pluginDescriptor.getName()));
99 label.addHyperlinkListener(new HyperlinkListener() {
100 public void hyperlinkUpdate(HyperlinkEvent e) {
101 final ShowSettingsUtil util = ShowSettingsUtil.getInstance();
102 final PluginManagerConfigurable pluginConfigurable = new PluginManagerConfigurable(PluginManagerUISettings.getInstance());
103 final Project project = ProjectManager.getInstance().getDefaultProject();
104 util.editConfigurable(project, pluginConfigurable, new Runnable(){
106 pluginConfigurable.select(pluginDescriptor);
113 //myPoweredByContainer.setVisible(true);
114 myPoweredByPanel.removeAll();
115 myPoweredByPanel.add(owner, BorderLayout.CENTER);
119 public void reset(String intentionCategory) {
121 String text = CodeInsightBundle.message("intention.settings.category.text", intentionCategory);
123 myDescriptionBrowser.setText(text);
124 setupPoweredByPanel(null);
126 URL beforeURL = getClass().getClassLoader().getResource(getClass().getPackage().getName().replace('.','/') + "/" + BEFORE_TEMPLATE);
127 showUsages(myBeforePanel, myBeforeSeparator, myBeforeUsagePanels, new ResourceTextDescriptor[]{new ResourceTextDescriptor(beforeURL)});
128 URL afterURL = getClass().getClassLoader().getResource(getClass().getPackage().getName().replace('.','/') + "/" + AFTER_TEMPLATE);
129 showUsages(myAfterPanel, myAfterSeparator, myAfterUsagePanels, new ResourceTextDescriptor[]{new ResourceTextDescriptor(afterURL)});
131 SwingUtilities.invokeLater(new Runnable() {
133 myPanel.revalidate();
137 catch (IOException e) {
142 private void createUIComponents() {
143 // TODO: place custom component creation code here
146 private static void showUsages(final JPanel panel,
147 final TitledSeparator separator,
148 final List<IntentionUsagePanel> usagePanels,
149 @Nullable final TextDescriptor[] exampleUsages) throws IOException {
150 GridBagConstraints gb = null;
151 boolean reuse = exampleUsages != null && panel.getComponents().length == exampleUsages.length;
153 disposeUsagePanels(usagePanels);
154 panel.setLayout(new GridBagLayout());
156 gb = new GridBagConstraints();
157 gb.anchor = GridBagConstraints.NORTHWEST;
158 gb.fill = GridBagConstraints.BOTH;
159 gb.gridheight = GridBagConstraints.REMAINDER;
163 gb.insets = new Insets(0,0,0,0);
170 if (exampleUsages != null) {
171 for (int i = 0; i < exampleUsages.length; i++) {
172 final TextDescriptor exampleUsage = exampleUsages[i];
173 final String name = exampleUsage.getFileName();
174 final FileTypeManagerEx fileTypeManager = FileTypeManagerEx.getInstanceEx();
175 final String extension = fileTypeManager.getExtension(name);
176 final FileType fileType = fileTypeManager.getFileTypeByExtension(extension);
178 IntentionUsagePanel usagePanel;
180 usagePanel = (IntentionUsagePanel)panel.getComponent(i);
183 usagePanel = new IntentionUsagePanel();
184 usagePanels.add(usagePanel);
186 usagePanel.reset(exampleUsage.getText(), fileType);
189 if (i == exampleUsages.length) {
190 gb.gridwidth = GridBagConstraints.REMAINDER;
192 panel.add(usagePanel, gb);
201 public JPanel getComponent() {
205 public void dispose() {
206 disposeUsagePanels(myBeforeUsagePanels);
207 disposeUsagePanels(myAfterUsagePanels);
210 private static void disposeUsagePanels(List<IntentionUsagePanel> usagePanels) {
211 for (final IntentionUsagePanel usagePanel : usagePanels) {
212 usagePanel.dispose();
217 public void init(final int preferredWidth) {
218 //adjust vertical dimension to be equal for all three panels
219 double height = (myDescriptionBrowser.getSize().getHeight() + myBeforePanel.getSize().getHeight() + myAfterPanel.getSize().getHeight()) / 3;
220 final Dimension newd = new Dimension(preferredWidth, (int)height);
221 myDescriptionBrowser.setSize(newd);
222 myDescriptionBrowser.setPreferredSize(newd);
223 myDescriptionBrowser.setMaximumSize(newd);
224 myDescriptionBrowser.setMinimumSize(newd);
226 myBeforePanel.setSize(newd);
227 myBeforePanel.setPreferredSize(newd);
228 myBeforePanel.setMaximumSize(newd);
229 myBeforePanel.setMinimumSize(newd);
231 myAfterPanel.setSize(newd);
232 myAfterPanel.setPreferredSize(newd);
233 myAfterPanel.setMaximumSize(newd);
234 myAfterPanel.setMinimumSize(newd);