UI for copying code style settings from other languages and predefined styles

This commit is contained in:
Rustam.Vishnyakov
2011-10-14 18:39:51 +04:00
parent 1c1f27e775
commit accc3fd276
7 changed files with 166 additions and 107 deletions
@@ -203,4 +203,15 @@ public class CommonCodeStyleSettingsManager implements JDOMExternalizable {
}
}
}
public static void copy(CommonCodeStyleSettings source, CommonCodeStyleSettings target) {
CommonCodeStyleSettings.copyPublicFields(source, target);
CommonCodeStyleSettings.IndentOptions targetIndentOptions = target.getIndentOptions();
if (targetIndentOptions != null) {
CommonCodeStyleSettings.IndentOptions sourceIndentOptions = source.getIndentOptions();
if (sourceIndentOptions != null) {
CommonCodeStyleSettings.copyPublicFields(sourceIndentOptions, targetIndentOptions);
}
}
}
}
@@ -31,12 +31,10 @@ import com.intellij.psi.codeStyle.CodeStyleSettingsProvider;
import com.intellij.psi.codeStyle.CommonCodeStyleSettings;
import com.intellij.psi.codeStyle.LanguageCodeStyleSettingsProvider;
import com.intellij.ui.components.JBTabbedPane;
import com.intellij.util.ui.UIUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
@@ -51,6 +49,7 @@ public abstract class TabbedLanguageCodeStylePanel extends CodeStyleAbstractPane
private List<CodeStyleAbstractPanel> myTabs;
private JPanel myPanel;
private JTabbedPane myTabbedPane;
private AdditionalCodeStylePanel myAdditionalPanel;
protected TabbedLanguageCodeStylePanel(@Nullable Language language, CodeStyleSettings currentSettings, CodeStyleSettings settings) {
super(language, currentSettings, settings);
@@ -113,6 +112,8 @@ public abstract class TabbedLanguageCodeStylePanel extends CodeStyleAbstractPane
myTabbedPane = new JBTabbedPane();
myTabs = new ArrayList<CodeStyleAbstractPanel>();
myPanel.add(myTabbedPane);
myAdditionalPanel = new AdditionalCodeStylePanel(this, getSettings());
myPanel.add(myAdditionalPanel.getContentPane(), BorderLayout.SOUTH);
initTabs(getSettings());
}
assert !myTabs.isEmpty();
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.intellij.application.options.codeStyle.AdditionalCodeStylePanel">
<grid id="27dc6" binding="myContentPane" layout-manager="FlowLayout" hgap="5" vgap="5" flow-align="0">
<constraints>
<xy x="20" y="20" width="533" height="400"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="284f9" class="javax.swing.JLabel">
<constraints/>
<properties>
<text value="&amp;Copy settings from"/>
</properties>
</component>
<component id="bd128" class="javax.swing.JButton" binding="myLanguageButton">
<constraints/>
<properties>
<text value="..."/>
</properties>
</component>
</children>
</grid>
</form>
@@ -0,0 +1,128 @@
/*
* Copyright 2000-2011 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.application.options.codeStyle;
import com.intellij.application.options.TabbedLanguageCodeStylePanel;
import com.intellij.lang.Language;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectUtil;
import com.intellij.psi.codeStyle.*;
import com.intellij.ui.components.JBRadioButton;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
/**
* Contains additional settings used in TabbedLanguageCodeStylePanel.
* @author Rustam Vishnyakov
*/
public class AdditionalCodeStylePanel {
private JPanel myContentPane;
private JButton myLanguageButton;
private TabbedLanguageCodeStylePanel myParent;
private PredefinedCodeStyle[] myPredefinedCodeStyles;
private CodeStyleSettings mySettings;
private final PopupMenu myLangMenu;
public AdditionalCodeStylePanel(TabbedLanguageCodeStylePanel parent, CodeStyleSettings settings) {
myParent = parent;
mySettings = settings;
myPredefinedCodeStyles = getPredefinedStyles();
myLangMenu = new PopupMenu();
if (myPredefinedCodeStyles.length > 0) {
Menu langs = new Menu("Language"); //TODO<rv>: Move to resource bundle
myLangMenu.add(langs);
fillLanguages(langs);
Menu predefined = new Menu("Predefined Style"); //TODO<rv>: Move to resource bundle
myLangMenu.add(predefined);
fillPredefined(predefined);
}
else {
fillLanguages(myLangMenu);
}
myLanguageButton.setPreferredSize(new Dimension(20, 20));
myLanguageButton.add(myLangMenu);
myLanguageButton.setMnemonic(KeyEvent.VK_C);
myLanguageButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
myLangMenu.show(myLanguageButton, 0, 0);
}
});
}
public JPanel getContentPane() {
return myContentPane;
}
private void fillLanguages(Menu parentMenu) {
for (final Language lang : LanguageCodeStyleSettingsProvider.getLanguagesWithCodeStyleSettings()) {
if (!lang.equals(myParent.getDefaultLanguage())) {
MenuItem langItem = new MenuItem(lang.getDisplayName());
parentMenu.add(langItem);
langItem.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
applyLanguageSettings(lang.getDisplayName());
}
});
}
}
}
private void fillPredefined(Menu parentMenu) {
for (final PredefinedCodeStyle predefinedCodeStyle : myPredefinedCodeStyles) {
MenuItem predefinedItem = new MenuItem(predefinedCodeStyle.getName());
parentMenu.add(predefinedItem);
predefinedItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
applyPredefinedStyle(predefinedCodeStyle.getName());
}
});
}
}
private PredefinedCodeStyle[] getPredefinedStyles() {
LanguageCodeStyleSettingsProvider provider = LanguageCodeStyleSettingsProvider.forLanguage(myParent.getDefaultLanguage());
if (provider == null) return new PredefinedCodeStyle[0];
return provider.getPredefinedCodeStyles();
}
private void applyLanguageSettings(String langName) {
final Project currProject = ProjectUtil.guessCurrentProject(myParent.getPanel());
CodeStyleSettings rootSettings = CodeStyleSettingsManager.getSettings(currProject);
CommonCodeStyleSettings sourceSettings = rootSettings.getCommonSettings(langName);
CommonCodeStyleSettings targetSettings = mySettings.getCommonSettings(myParent.getDefaultLanguage());
if (sourceSettings == null || targetSettings == null) return;
CommonCodeStyleSettingsManager.copy(sourceSettings, targetSettings);
myParent.reset(mySettings);
myParent.onSomethingChanged();
}
private void applyPredefinedStyle(String styleName) {
for (PredefinedCodeStyle style : myPredefinedCodeStyles) {
if (style.getName().equals(styleName)) {
myParent.applyPredefinedSettings(style);
}
}
}
}
@@ -201,8 +201,6 @@ public class CodeStyleMainPanel extends JPanel implements LanguageSelectorListen
mySettingsPanel.add(scheme.getName(), panel);
mySchemesPanel.setCodeStyleSettingsPanel(panel);
panel.setLanguage(myLangSelector.getLanguage());
Language panelLanguage = panel.getSelectedLanguage();
mySchemesPanel.setPredefinedEnabled(panelLanguage != null && hasPredefinedStyles(panelLanguage));
}
return mySettingsPanels.get(name);
@@ -230,16 +228,9 @@ public class CodeStyleMainPanel extends JPanel implements LanguageSelectorListen
public void languageChanged(Language lang) {
for (NewCodeStyleSettingsPanel panel : mySettingsPanels.values()) {
panel.setLanguage(lang);
Language resultingLang = panel.getSelectedLanguage();
mySchemesPanel.setPredefinedEnabled(hasPredefinedStyles(resultingLang));
}
}
private static boolean hasPredefinedStyles(Language language) {
LanguageCodeStyleSettingsProvider provider = LanguageCodeStyleSettingsProvider.forLanguage(language);
return provider != null && provider.getPredefinedCodeStyles().length > 0;
}
public Set<String> processListOptions() {
final CodeStyleScheme defaultScheme = CodeStyleSchemes.getInstance().getDefaultScheme();
final NewCodeStyleSettingsPanel panel = ensurePanel(defaultScheme);
@@ -1,23 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.intellij.application.options.codeStyle.PredefinedCodeStylesDialog">
<grid id="cbd77" binding="contentPane" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="48" y="54" width="436" height="297"/>
</constraints>
<properties/>
<border type="etched"/>
<children>
<component id="8997f" class="com.intellij.ui.components.JBList" binding="myPredefinedCodeStyleList">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false">
<preferred-size width="180" height="100"/>
</grid>
</constraints>
<properties>
<selectionMode value="0"/>
</properties>
</component>
</children>
</grid>
</form>
@@ -1,73 +0,0 @@
/*
* Copyright 2000-2011 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.application.options.codeStyle;
import com.intellij.lang.Language;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.psi.codeStyle.LanguageCodeStyleSettingsProvider;
import com.intellij.psi.codeStyle.PredefinedCodeStyle;
import com.intellij.ui.components.JBList;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import java.awt.*;
public class PredefinedCodeStylesDialog extends DialogWrapper {
private JPanel contentPane;
private JBList myPredefinedCodeStyleList;
private LanguageCodeStyleSettingsProvider myProvider;
protected PredefinedCodeStylesDialog(Component parent, Language language) {
super(parent, false);
myProvider = LanguageCodeStyleSettingsProvider.forLanguage(language);
MyListModel model = new MyListModel();
myPredefinedCodeStyleList.setModel(model);
setTitle("Select Predefined Style"); //TODO<rv> Move to resource bundle
init();
}
@Nullable
public PredefinedCodeStyle getSelectedStyle() {
Object selection = myPredefinedCodeStyleList.getSelectedValue();
return selection instanceof PredefinedCodeStyle ? (PredefinedCodeStyle)selection : null;
}
@Override
protected JComponent createCenterPanel() {
return contentPane;
}
private class MyListModel extends AbstractListModel {
private PredefinedCodeStyle[] myCodeStyles;
public MyListModel() {
myCodeStyles = myProvider.getPredefinedCodeStyles();
}
@Override
public int getSize() {
return myCodeStyles.length;
}
@Override
public Object getElementAt(int index) {
return myCodeStyles[index];
}
}
}