diff --git a/platform/lang-impl/src/com/intellij/application/options/codeStyle/CodeStyleSchemesModel.java b/platform/lang-impl/src/com/intellij/application/options/codeStyle/CodeStyleSchemesModel.java index 07a97e91f162..94b1d7cd2881 100644 --- a/platform/lang-impl/src/com/intellij/application/options/codeStyle/CodeStyleSchemesModel.java +++ b/platform/lang-impl/src/com/intellij/application/options/codeStyle/CodeStyleSchemesModel.java @@ -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 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 getAllSortedSchemes() { + List schemes = new ArrayList(); + schemes.addAll(getSchemes()); + schemes.add(myProjectScheme); + Collections.sort(schemes, new Comparator() { + @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; + } } diff --git a/platform/lang-impl/src/com/intellij/application/options/codeStyle/CodeStyleSchemesPanel.form b/platform/lang-impl/src/com/intellij/application/options/codeStyle/CodeStyleSchemesPanel.form index d1f280abb23a..036fa4dbc389 100644 --- a/platform/lang-impl/src/com/intellij/application/options/codeStyle/CodeStyleSchemesPanel.form +++ b/platform/lang-impl/src/com/intellij/application/options/codeStyle/CodeStyleSchemesPanel.form @@ -20,16 +20,16 @@ - + - + - + @@ -44,92 +44,26 @@ - + + + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + @@ -146,7 +80,7 @@ - + diff --git a/platform/lang-impl/src/com/intellij/application/options/codeStyle/CodeStyleSchemesPanel.java b/platform/lang-impl/src/com/intellij/application/options/codeStyle/CodeStyleSchemesPanel.java index 821eff38cc88..7f7507795456 100644 --- a/platform/lang-impl/src/com/intellij/application/options/codeStyle/CodeStyleSchemesPanel.java +++ b/platform/lang-impl/src/com/intellij/application/options/codeStyle/CodeStyleSchemesPanel.java @@ -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 getSchemes() { - ArrayList result = new ArrayList(); - 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 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 popup = new SchemesToImportPopup(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 names = new ArrayList(); - 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 schemes = new ArrayList(); + 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()); + } + } } diff --git a/platform/lang-impl/src/com/intellij/application/options/codeStyle/ManageCodeStyleSchemesDialog.form b/platform/lang-impl/src/com/intellij/application/options/codeStyle/ManageCodeStyleSchemesDialog.form new file mode 100644 index 000000000000..01eb0fb7de87 --- /dev/null +++ b/platform/lang-impl/src/com/intellij/application/options/codeStyle/ManageCodeStyleSchemesDialog.form @@ -0,0 +1,93 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/platform/lang-impl/src/com/intellij/application/options/codeStyle/ManageCodeStyleSchemesDialog.java b/platform/lang-impl/src/com/intellij/application/options/codeStyle/ManageCodeStyleSchemesDialog.java new file mode 100644 index 000000000000..fa93e04f6d81 --- /dev/null +++ b/platform/lang-impl/src/com/intellij/application/options/codeStyle/ManageCodeStyleSchemesDialog.java @@ -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 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 popup = + new SchemesToImportPopup(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 mySchemes; + + public MySchemesTableModel(CodeStyleSchemesModel schemesModel) { + mySchemesModel = schemesModel; + mySchemes = new ArrayList(); + 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 names = new ArrayList(); + 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()); + } + + +}