Updated code style schemes UI (with a separate scheme management dialog)

This commit is contained in:
Rustam.Vishnyakov
2011-12-27 19:48:36 +04:00
parent 5a5b03985d
commit 4be3293aaa
5 changed files with 540 additions and 288 deletions
@@ -26,6 +26,7 @@ import com.intellij.psi.impl.source.codeStyle.CodeStyleSchemeImpl;
import com.intellij.psi.impl.source.codeStyle.CodeStyleSchemesImpl;
import com.intellij.util.EventDispatcher;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.Nullable;
import java.util.*;
@@ -41,15 +42,17 @@ public class CodeStyleSchemesModel {
private final EventDispatcher<CodeStyleSettingsListener> myDispatcher = EventDispatcher.create(CodeStyleSettingsListener.class);
private final Project myProject;
private boolean myUsePerProjectSettings;
public final static String PROJECT_SCHEME_NAME = "Project";
public CodeStyleSchemesModel(Project project) {
myProject = project;
myProjectScheme = new CodeStyleSchemeImpl("Project Scheme", false, CodeStyleSchemes.getInstance().getDefaultScheme());
myProjectScheme = new CodeStyleSchemeImpl(PROJECT_SCHEME_NAME, false, CodeStyleSchemes.getInstance().getDefaultScheme());
reset();
myDefault = CodeStyleSchemes.getInstance().getDefaultScheme();
}
public void selectScheme(final CodeStyleScheme selected, Object source) {
public void selectScheme(final CodeStyleScheme selected, @Nullable Object source) {
if (myGlobalSelected != selected && selected != myProjectScheme) {
myGlobalSelected = selected;
myDispatcher.getMulticaster().currentSchemeChanged(source);
@@ -264,4 +267,29 @@ public class CodeStyleSchemesModel {
}
return null;
}
public CodeStyleScheme getProjectScheme() {
return myProjectScheme;
}
public boolean isProjectScheme(CodeStyleScheme scheme) {
return myProjectScheme == scheme;
}
public List<CodeStyleScheme> getAllSortedSchemes() {
List<CodeStyleScheme> schemes = new ArrayList<CodeStyleScheme>();
schemes.addAll(getSchemes());
schemes.add(myProjectScheme);
Collections.sort(schemes, new Comparator<CodeStyleScheme>() {
@Override
public int compare(CodeStyleScheme s1, CodeStyleScheme s2) {
if (isProjectScheme(s1)) return -1;
if (isProjectScheme(s2)) return 1;
if (s1.isDefault()) return -1;
if (s2.isDefault()) return 1;
return s1.getName().compareToIgnoreCase(s2.getName());
}
});
return schemes;
}
}
@@ -20,16 +20,16 @@
<color color="-16777216"/>
</border>
<children>
<grid id="3fa8b" layout-manager="GridLayoutManager" row-count="1" column-count="6" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="3fa8b" layout-manager="GridLayoutManager" row-count="1" column-count="4" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints/>
<properties/>
<border type="none"/>
<children>
<grid id="93903" binding="myGlobalPanel" layout-manager="GridLayoutManager" row-count="1" column-count="7" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="93903" layout-manager="GridLayoutManager" row-count="1" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="3" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
<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"/>
</constraints>
<properties/>
<border type="none"/>
@@ -44,92 +44,26 @@
</component>
<component id="c234b" class="javax.swing.JComboBox" binding="myCombo">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="100" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
<component id="56195" class="javax.swing.JButton" binding="mySaveAsButton">
<component id="dc89" class="javax.swing.JButton" binding="myManageButton">
<constraints>
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text resource-bundle="messages/ApplicationBundle" key="button.save.as"/>
</properties>
</component>
<component id="c23b" class="javax.swing.JButton" binding="myDeleteButton">
<constraints>
<grid row="0" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text resource-bundle="messages/ApplicationBundle" key="button.delete"/>
</properties>
</component>
<component id="8c10c" class="javax.swing.JButton" binding="myExportButton">
<constraints>
<grid row="0" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="&amp;Share..."/>
</properties>
</component>
<component id="7a162" class="javax.swing.JButton" binding="myCopyToProjectButton" default-binding="true">
<constraints>
<grid row="0" column="5" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Copy to Project"/>
</properties>
</component>
<component id="b2f25" class="javax.swing.JButton" binding="myImportButton">
<constraints>
<grid row="0" column="6" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="&amp;Import Shared..."/>
<text value="&amp;Manage..."/>
</properties>
</component>
</children>
</grid>
<grid id="db159" binding="myProjectPanel" 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>
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="230d4" class="javax.swing.JButton" binding="myExportAsGlobalButton">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="100" height="-1"/>
</grid>
</constraints>
<properties>
<text value="Export..."/>
</properties>
</component>
</children>
</grid>
<component id="40179" class="com.intellij.ui.components.JBLabel">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Use:"/>
</properties>
</component>
<component id="f1d09" class="javax.swing.JComboBox" binding="mySettingsType">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="120" height="-1"/>
</grid>
</constraints>
<properties/>
</component>
<grid id="e086a" 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>
<grid row="0" column="4" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
@@ -146,7 +80,7 @@
</grid>
<hspacer id="fd090">
<constraints>
<grid row="0" column="5" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="0" column="3" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
</children>
@@ -17,109 +17,38 @@
package com.intellij.application.options.codeStyle;
import com.intellij.application.options.CodeStyleAbstractPanel;
import com.intellij.application.options.ExportSchemeAction;
import com.intellij.application.options.SaveSchemeDialog;
import com.intellij.application.options.SchemesToImportPopup;
import com.intellij.openapi.application.ApplicationBundle;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.options.SchemesManager;
import com.intellij.openapi.ui.Messages;
import com.intellij.psi.codeStyle.*;
import com.intellij.psi.impl.source.codeStyle.CodeStyleSchemeImpl;
import com.intellij.ui.components.JBScrollPane;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Vector;
import java.util.*;
import java.util.List;
public class CodeStyleSchemesPanel{
private JButton myExportButton;
private JComboBox myCombo;
private JButton mySaveAsButton;
private JButton myDeleteButton;
private final CodeStyleSchemesModel myModel;
private JButton myImportButton;
private JPanel myPanel;
private JButton myExportAsGlobalButton;
private JButton myCopyToProjectButton;
private JBScrollPane myJBScrollPane;
private JPanel myGlobalPanel;
private JPanel myProjectPanel;
private JComboBox mySettingsType;
private JButton myCopyFromButton;
private JButton myManageButton;
private PopupMenu myCopyFromMenu;
private boolean myIsReset = false;
private NewCodeStyleSettingsPanel mySettingsPanel;
private final static String GLOBAL_SETTINGS_ITEM = "Global settings";
private final static String PROJECT_SETTINGS_ITEM = "Per project settings";
public Collection<CodeStyleScheme> getSchemes() {
ArrayList<CodeStyleScheme> result = new ArrayList<CodeStyleScheme>();
for (int i = 0; i < myCombo.getItemCount(); i++) {
Object item = myCombo.getItemAt(i);
if (item instanceof CodeStyleScheme) {
result.add((CodeStyleScheme)item);
}
}
return result;
}
private final Font myDefaultComboFont;
private final Font myBoldComboFont;
public CodeStyleSchemesPanel(CodeStyleSchemesModel model) {
myModel = model;
ComboBoxModel settingsTypeModel = new DefaultComboBoxModel(new String[] {GLOBAL_SETTINGS_ITEM, PROJECT_SETTINGS_ITEM});
mySettingsType.setModel(settingsTypeModel);
mySettingsType.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
onSettingsTypeChange();
}
});
final SchemesManager<CodeStyleScheme, CodeStyleSchemeImpl> schemesManager = CodeStyleSchemesModel.getSchemesManager();
if (schemesManager.isExportAvailable()) {
myExportButton.setVisible(true);
myExportButton.addActionListener(new ActionListener(){
public void actionPerformed(final ActionEvent e) {
CodeStyleScheme selected = getSelectedScheme();
ExportSchemeAction.doExport((CodeStyleSchemeImpl)selected, schemesManager);
}
});
myExportButton.setMnemonic('S');
}
else {
myExportButton.setVisible(false);
}
if (schemesManager.isImportAvailable()) {
myImportButton.setVisible(true);
myImportButton.addActionListener(new ActionListener(){
public void actionPerformed(final ActionEvent e) {
SchemesToImportPopup<CodeStyleScheme, CodeStyleSchemeImpl> popup = new SchemesToImportPopup<CodeStyleScheme, CodeStyleSchemeImpl>(myPanel){
protected void onSchemeSelected(final CodeStyleSchemeImpl scheme) {
if (scheme != null) {
myModel.addScheme(scheme, true);
}
}
};
popup.show(schemesManager, getSchemes());
}
});
}
else {
myImportButton.setVisible(false);
}
myDefaultComboFont = myCombo.getFont();
myBoldComboFont = myDefaultComboFont.deriveFont(Font.BOLD);
myCombo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (!myIsReset) {
@@ -131,26 +60,19 @@ public class CodeStyleSchemesPanel{
}
}
});
mySaveAsButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
onSaveAs();
}
});
myDeleteButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
onDelete();
}
});
myCopyToProjectButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
onCopyToProject();
}
});
myExportAsGlobalButton.addActionListener(new ActionListener(){
public void actionPerformed(final ActionEvent e) {
onExportProjectScheme();
myCombo.setRenderer(new DefaultListCellRenderer() {
@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
Component component = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
Font font = myDefaultComboFont;
if (value instanceof CodeStyleScheme) {
CodeStyleScheme scheme = (CodeStyleScheme)value;
if (scheme.isDefault() || myModel.isProjectScheme(scheme)) {
font = myBoldComboFont;
}
}
component.setFont(font);
return component;
}
});
@@ -162,99 +84,30 @@ public class CodeStyleSchemesPanel{
});
myCopyFromButton.setEnabled(false);
myJBScrollPane.setBorder(null);
}
private void onExportProjectScheme() {
String name = Messages.showInputDialog("Enter new scheme name:", "Copy Project Scheme to Global List", Messages.getQuestionIcon());
if (name != null) {
CodeStyleScheme scheme = myModel.exportProjectScheme(name);
int switchToGlobal = Messages
.showYesNoDialog("Project scheme was copied to global scheme list as '" + scheme.getName() + ".\n" +
"Switch to this created scheme?",
"Copy Project Scheme to Global List", Messages.getQuestionIcon());
if (switchToGlobal == 0) {
myModel.setUsePerProjectSettings(false);
myModel.selectScheme(scheme, null);
myManageButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
showManageSchemesDialog();
}
}
}
private void onDelete() {
myModel.removeScheme(getSelectedScheme());
}
private void onCopyToProject() {
myModel.copyToProject(getSelectedScheme());
int switchToProject = Messages
.showYesNoDialog("Scheme '" + getSelectedScheme().getName() + "' was copied to be used as the project scheme.\n" +
"Switch to this created scheme?",
"Copy Scheme to Project", Messages.getQuestionIcon());
if (switchToProject == 0) {
myModel.setUsePerProjectSettings(true, true);
}
}
private void onSaveAs() {
CodeStyleScheme[] schemes = CodeStyleSchemes.getInstance().getSchemes();
ArrayList<String> names = new ArrayList<String>();
for (int i = 0; i < schemes.length; i++) {
CodeStyleScheme scheme = schemes[i];
names.add(scheme.getName());
}
SaveSchemeDialog saveDialog = new SaveSchemeDialog(myPanel, ApplicationBundle.message("title.save.code.style.scheme.as"), names);
saveDialog.show();
if (saveDialog.isOK()) {
CodeStyleScheme selectedScheme = getSelectedScheme();
CodeStyleScheme newScheme = myModel.createNewScheme(saveDialog.getSchemeName(),
selectedScheme);
myModel.addScheme(newScheme, true);
}
});
myJBScrollPane.setBorder(null);
}
private void onCombo() {
CodeStyleScheme selected = getSelectedScheme();
if (selected != null) {
myModel.selectScheme(selected, this);
updateButtons();
}
}
private void updateButtons() {
if (!myModel.isUsePerProjectSettings()) {
boolean deleteEnabled = false;
boolean saveAsEnabled = true;
CodeStyleScheme selected = getSelectedScheme();
if (selected != null) {
deleteEnabled = !CodeStyleSchemesModel.cannotBeDeleted(selected);
if (myModel.isProjectScheme(selected)) {
myModel.setUsePerProjectSettings(true);
}
myDeleteButton.setEnabled(deleteEnabled);
if (myExportButton != null) {
myExportButton.setEnabled(!CodeStyleSchemesModel.cannotBeModified(selected));
else {
myModel.setUsePerProjectSettings(false);
myModel.selectScheme(selected, this);
}
mySaveAsButton.setEnabled(saveAsEnabled);
myCopyToProjectButton.setEnabled(true);
myExportAsGlobalButton.setEnabled(false);
}
else {
mySaveAsButton.setEnabled(false);
myDeleteButton.setEnabled(false);
myExportButton.setEnabled(false);
myImportButton.setEnabled(false);
myCopyToProjectButton.setEnabled(false);
myExportAsGlobalButton.setEnabled(true);
}
}
@Nullable
private CodeStyleScheme getSelectedScheme() {
Object selected = myCombo.getSelectedItem();
if (selected instanceof CodeStyleScheme) {
@@ -263,7 +116,6 @@ public class CodeStyleSchemesPanel{
return null;
}
public void disposeUIResources() {
myPanel.removeAll();
}
@@ -271,13 +123,11 @@ public class CodeStyleSchemesPanel{
public void resetSchemesCombo() {
myIsReset = true;
try {
Vector schemesVector = new Vector();
schemesVector.addAll(myModel.getSchemes());
DefaultComboBoxModel model = new DefaultComboBoxModel(schemesVector);
List<CodeStyleScheme> schemes = new ArrayList<CodeStyleScheme>();
schemes.addAll(myModel.getAllSortedSchemes());
DefaultComboBoxModel model = new DefaultComboBoxModel(schemes.toArray());
myCombo.setModel(model);
myCombo.setSelectedItem(myModel.getSelectedGlobalScheme());
updateProjectSchemesRelatedUI();
}
finally {
myIsReset = false;
@@ -286,28 +136,20 @@ public class CodeStyleSchemesPanel{
}
private void updateProjectSchemesRelatedUI() {
boolean useProjectSettings = myModel.isUsePerProjectSettings();
mySettingsType.setSelectedItem(useProjectSettings ? PROJECT_SETTINGS_ITEM : GLOBAL_SETTINGS_ITEM);
myGlobalPanel.setVisible(!useProjectSettings);
myProjectPanel.setVisible(useProjectSettings);
updateButtons();
}
public void onSelectedSchemeChanged() {
myIsReset = true;
try {
myCombo.setSelectedItem(myModel.getSelectedGlobalScheme());
if (myModel.isUsePerProjectSettings()) {
myCombo.setSelectedItem(myModel.getProjectScheme());
}
else {
myCombo.setSelectedItem(myModel.getSelectedGlobalScheme());
}
}
finally {
myIsReset = false;
}
updateButtons();
}
public void usePerProjectSettingsOptionChanged() {
updateProjectSchemesRelatedUI();
}
public JComponent getPanel() {
@@ -320,14 +162,7 @@ public class CodeStyleSchemesPanel{
myCopyFromButton.setEnabled(selectedPanel != null && selectedPanel.isCopyFromMenuAvailable());
}
private void onSettingsTypeChange() {
Object selectedItem = mySettingsType.getSelectedItem();
boolean useProjectSettings = PROJECT_SETTINGS_ITEM.equals(selectedItem);
myGlobalPanel.setVisible(!useProjectSettings);
myProjectPanel.setVisible(useProjectSettings);
myModel.setUsePerProjectSettings(useProjectSettings);
}
private void showCopyFromMenu() {
initCopyFromMenu();
if (myCopyFromMenu.getItemCount() > 0) {
@@ -349,5 +184,18 @@ public class CodeStyleSchemesPanel{
myCopyFromButton.setEnabled(false);
}
}
private void showManageSchemesDialog() {
ManageCodeStyleSchemesDialog manageSchemesDialog = new ManageCodeStyleSchemesDialog(myPanel, myModel);
manageSchemesDialog.show();
}
public void usePerProjectSettingsOptionChanged() {
if (myModel.isProjectScheme(myModel.getSelectedScheme())) {
myCombo.setSelectedItem(myModel.getProjectScheme());
}
else {
myCombo.setSelectedItem(myModel.getSelectedScheme());
}
}
}
@@ -0,0 +1,93 @@
<?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.ManageCodeStyleSchemesDialog">
<grid id="27dc6" binding="myContentPane" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="500" height="400"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<scrollpane id="fb787" class="com.intellij.ui.components.JBScrollPane">
<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="200" height="200"/>
</grid>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="578bf" class="com.intellij.ui.table.JBTable" binding="mySchemesTable" custom-create="true">
<constraints/>
<properties>
<striped value="true"/>
</properties>
</component>
</children>
</scrollpane>
<grid id="8885" layout-manager="GridLayoutManager" row-count="7" 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>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="27e35" class="javax.swing.JButton" binding="myDeleteButton">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="&amp;Delete"/>
</properties>
</component>
<vspacer id="e34fc">
<constraints>
<grid row="6" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<component id="2095b" class="javax.swing.JButton" binding="mySaveAsButton">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="&amp;Save As..."/>
</properties>
</component>
<component id="dbfe3" class="javax.swing.JButton" binding="myCopyToProjectButton" default-binding="true">
<constraints>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Copy to &amp;Project"/>
</properties>
</component>
<component id="9a9cc" class="javax.swing.JButton" binding="myCloseButton">
<constraints>
<grid row="5" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="&amp;Close"/>
</properties>
</component>
<component id="2e22f" class="javax.swing.JButton" binding="myExportButton" default-binding="true">
<constraints>
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Export..."/>
</properties>
</component>
<component id="816ef" class="javax.swing.JButton" binding="myImportButton" default-binding="true">
<constraints>
<grid row="4" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Import..."/>
</properties>
</component>
</children>
</grid>
</children>
</grid>
</form>
@@ -0,0 +1,349 @@
/*
* 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.ExportSchemeAction;
import com.intellij.application.options.SaveSchemeDialog;
import com.intellij.application.options.SchemesToImportPopup;
import com.intellij.openapi.application.ApplicationBundle;
import com.intellij.openapi.options.SchemesManager;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.Messages;
import com.intellij.psi.codeStyle.CodeStyleScheme;
import com.intellij.psi.codeStyle.CodeStyleSchemes;
import com.intellij.psi.impl.source.codeStyle.CodeStyleSchemeImpl;
import com.intellij.ui.table.JBTable;
import org.jetbrains.annotations.NotNull;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import java.util.List;
/**
* @author: rvishnyakov
*/
public class ManageCodeStyleSchemesDialog extends DialogWrapper {
private JPanel myContentPane;
private JBTable mySchemesTable;
private JButton myDeleteButton;
private JButton mySaveAsButton;
private JButton myCopyToProjectButton;
private JButton myCloseButton;
private JButton myExportButton;
private JButton myImportButton;
private final MySchemesTableModel mySchemesTableModel;
private final CodeStyleSchemesModel myModel;
private final Component myParent;
protected ManageCodeStyleSchemesDialog(final Component parent, CodeStyleSchemesModel schemesModel) {
super(parent, true);
myParent = parent;
myModel = schemesModel;
setTitle("Code Style Schemes");
mySchemesTableModel = new MySchemesTableModel(schemesModel);
mySchemesTable.setModel(mySchemesTableModel);
mySchemesTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
mySchemesTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
updateActions();
}
});
myDeleteButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
deleteSelected();
}
});
mySaveAsButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
onSaveAs();
}
});
myCopyToProjectButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
onCopyToProject();
}
});
myCloseButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
doCancelAction();
}
});
final SchemesManager<CodeStyleScheme, CodeStyleSchemeImpl> schemesManager = CodeStyleSchemesModel.getSchemesManager();
if (schemesManager.isExportAvailable()) {
myExportButton.setVisible(true);
myExportButton.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
CodeStyleScheme selected = getSelectedScheme();
ExportSchemeAction.doExport((CodeStyleSchemeImpl)selected, schemesManager);
}
});
myExportButton.setMnemonic('S');
}
else {
myExportButton.setVisible(false);
}
if (schemesManager.isImportAvailable()) {
myImportButton.setVisible(true);
myImportButton.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
SchemesToImportPopup<CodeStyleScheme, CodeStyleSchemeImpl> popup =
new SchemesToImportPopup<CodeStyleScheme, CodeStyleSchemeImpl>(parent) {
protected void onSchemeSelected(final CodeStyleSchemeImpl scheme) {
if (scheme != null) {
myModel.addScheme(scheme, true);
}
}
};
popup.show(schemesManager, myModel.getSchemes());
}
});
}
else {
myImportButton.setVisible(false);
}
init();
}
private void updateActions() {
CodeStyleScheme selectedScheme = getSelectedScheme();
myDeleteButton.setEnabled(!(selectedScheme.isDefault() || mySchemesTableModel.isProjectScheme(selectedScheme)));
myCopyToProjectButton.setEnabled(!mySchemesTableModel.isProjectScheme(selectedScheme));
}
@NotNull
private CodeStyleScheme getSelectedScheme() {
int row = mySchemesTable.getSelectedRow();
assert row >= 0;
return mySchemesTableModel.getSchemeAt(row);
}
@Override
protected Action[] createActions() {
return new Action[]{};
}
private void deleteSelected() {
int row = mySchemesTable.getSelectedRow();
if (row >= 0) {
int rowToSelect = row + 1;
if (rowToSelect >= mySchemesTableModel.getRowCount()) {
rowToSelect = mySchemesTableModel.getDefaultRow();
}
mySchemesTable.getSelectionModel().setSelectionInterval(rowToSelect, rowToSelect);
mySchemesTableModel.deleteAt(row);
}
}
@Override
protected JComponent createCenterPanel() {
return myContentPane;
}
private void createUIComponents() {
mySchemesTable = new MySchemesTable();
}
private class MySchemesTable extends JBTable {
private final TableCellRenderer myFixedItemsRenderer;
private MySchemesTable() {
myFixedItemsRenderer = new DefaultTableCellRenderer() {
@Override
public Component getTableCellRendererComponent(JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row,
int column) {
Component defaultComponent = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if (value instanceof CodeStyleScheme) {
CodeStyleScheme scheme = (CodeStyleScheme)value;
if (scheme.isDefault() || myModel.isProjectScheme(scheme)) {
defaultComponent.setFont(defaultComponent.getFont().deriveFont(Font.BOLD));
}
}
return defaultComponent;
}
};
}
@Override
public TableCellRenderer getCellRenderer(int row, int column) {
return myFixedItemsRenderer;
}
}
private static class MySchemesTableModel extends AbstractTableModel {
private final CodeStyleSchemesModel mySchemesModel;
private final List<CodeStyleScheme> mySchemes;
public MySchemesTableModel(CodeStyleSchemesModel schemesModel) {
mySchemesModel = schemesModel;
mySchemes = new ArrayList<CodeStyleScheme>();
updateSchemes();
}
@Override
public String getColumnName(int column) {
assert column == 0;
return "Name";
}
@Override
public int getRowCount() {
return mySchemes.size();
}
@Override
public int getColumnCount() {
return 1;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
assert columnIndex == 0;
return mySchemes.get(rowIndex);
}
public CodeStyleScheme getSchemeAt(int row) {
return mySchemes.get(row);
}
public void deleteAt(int row) {
CodeStyleScheme scheme = mySchemes.get(row);
mySchemesModel.removeScheme(scheme);
updateSchemes();
fireTableRowsDeleted(row, row);
}
public int createNewScheme(CodeStyleScheme selectedScheme, String schemeName) {
CodeStyleScheme newScheme = mySchemesModel.createNewScheme(schemeName, selectedScheme);
mySchemesModel.addScheme(newScheme, true);
updateSchemes();
int row = 0;
for (CodeStyleScheme scheme : mySchemes) {
if (scheme == newScheme) {
fireTableRowsInserted(row, row);
break;
}
row ++;
}
return row;
}
public int getDefaultRow() {
int row = 0;
for (CodeStyleScheme scheme : mySchemes) {
if (scheme.isDefault()) return row;
row ++;
}
return 0;
}
public void copyToProject(CodeStyleScheme scheme) {
mySchemesModel.copyToProject(scheme);
int switchToProject = Messages
.showYesNoDialog("Scheme '" + scheme.getName() + "' was copied to be used as the project scheme.\n" +
"Switch to this created scheme?",
"Copy Scheme to Project", Messages.getQuestionIcon());
if (switchToProject == 0) {
mySchemesModel.setUsePerProjectSettings(true, true);
}
}
public int exportProjectScheme() {
String name = Messages.showInputDialog("Enter new scheme name:", "Copy Project Scheme to Global List", Messages.getQuestionIcon());
if (name != null && !CodeStyleSchemesModel.PROJECT_SCHEME_NAME.equals(name)) {
CodeStyleScheme newScheme = mySchemesModel.exportProjectScheme(name);
updateSchemes();
int switchToGlobal = Messages
.showYesNoDialog("Project scheme was copied to global scheme list as '" + newScheme.getName() + ".\n" +
"Switch to this created scheme?",
"Copy Project Scheme to Global List", Messages.getQuestionIcon());
if (switchToGlobal == 0) {
mySchemesModel.setUsePerProjectSettings(false);
mySchemesModel.selectScheme(newScheme, null);
}
int row = 0;
for (CodeStyleScheme scheme : mySchemes) {
if (scheme == newScheme) {
fireTableRowsInserted(row, row);
return switchToGlobal == 0 ? row : -1;
}
row++;
}
}
return -1;
}
private void updateSchemes() {
mySchemes.clear();
mySchemes.addAll(mySchemesModel.getAllSortedSchemes());
}
public boolean isProjectScheme(CodeStyleScheme scheme) {
return mySchemesModel.isProjectScheme(scheme);
}
}
private void onSaveAs() {
if (mySchemesTableModel.isProjectScheme(getSelectedScheme())) {
int rowToSelect = mySchemesTableModel.exportProjectScheme();
if (rowToSelect > 0) {
mySchemesTable.getSelectionModel().setSelectionInterval(rowToSelect, rowToSelect);
}
}
else {
CodeStyleScheme[] schemes = CodeStyleSchemes.getInstance().getSchemes();
ArrayList<String> names = new ArrayList<String>();
for (CodeStyleScheme scheme : schemes) {
names.add(scheme.getName());
}
SaveSchemeDialog saveDialog = new SaveSchemeDialog(myParent, ApplicationBundle.message("title.save.code.style.scheme.as"), names);
saveDialog.show();
if (saveDialog.isOK()) {
int row = mySchemesTableModel.createNewScheme(getSelectedScheme(), saveDialog.getSchemeName());
mySchemesTable.getSelectionModel().setSelectionInterval(row, row);
}
}
}
private void onCopyToProject() {
mySchemesTableModel.copyToProject(getSelectedScheme());
}
}