From 5f427ec585951ff27142a4733efeca3b3216ffdc Mon Sep 17 00:00:00 2001 From: Vladimir Krivosheev Date: Thu, 18 Apr 2019 12:59:03 +0200 Subject: [PATCH] Ant TargetFilter - do not throw WriteExternalException --- .../ExternalizablePropertyContainer.java | 22 +--- .../intellij/util/config/Externalizer.java | 11 +- .../intellij/util/config/ListProperty.java | 21 +--- .../lang/ant/config/explorer/AntExplorer.java | 2 +- .../ant/config/impl/AntBuildFileImpl.java | 19 +-- .../lang/ant/config/impl/TargetFilter.java | 29 ++--- .../BuildFilePropertiesPanel.java | 41 ++----- .../impl/configuration/UIPropertyBinding.java | 112 ++---------------- 8 files changed, 47 insertions(+), 210 deletions(-) diff --git a/platform/platform-api/src/com/intellij/util/config/ExternalizablePropertyContainer.java b/platform/platform-api/src/com/intellij/util/config/ExternalizablePropertyContainer.java index 30647d7f5d35..65b4d8319701 100644 --- a/platform/platform-api/src/com/intellij/util/config/ExternalizablePropertyContainer.java +++ b/platform/platform-api/src/com/intellij/util/config/ExternalizablePropertyContainer.java @@ -1,18 +1,4 @@ -/* - * Copyright 2000-2016 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. - */ +// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.util.config; import com.intellij.openapi.diagnostic.Logger; @@ -76,7 +62,7 @@ public class ExternalizablePropertyContainer extends AbstractProperty.AbstractPr } private static Externalizer> createListExternalizer(final Externalizer itemExternalizer, final String itemTagName) { - return new ListExternalizer(itemExternalizer, itemTagName); + return new ListExternalizer<>(itemExternalizer, itemTagName); } public void readExternal(@NotNull Element element) { @@ -180,7 +166,9 @@ public class ExternalizablePropertyContainer extends AbstractProperty.AbstractPr else { Element element = new Element(myItemTagName); myItemExternalizer.writeValue(element, item); - dataElement.addContent(element); + if (!(item instanceof SkippableValue) || !JDOMUtil.isEmpty(element)) { + dataElement.addContent(element); + } } } } diff --git a/platform/platform-api/src/com/intellij/util/config/Externalizer.java b/platform/platform-api/src/com/intellij/util/config/Externalizer.java index a1e53c1e4735..c5426e5835a6 100644 --- a/platform/platform-api/src/com/intellij/util/config/Externalizer.java +++ b/platform/platform-api/src/com/intellij/util/config/Externalizer.java @@ -1,4 +1,4 @@ -// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.util.config; @@ -6,6 +6,7 @@ import com.intellij.openapi.util.Factory; import com.intellij.openapi.util.JDOMExternalizable; import org.jdom.Element; import org.jetbrains.annotations.NonNls; +import org.jetbrains.annotations.NotNull; import java.util.Iterator; @@ -30,7 +31,6 @@ public interface Externalizer { Externalizer STORAGE = new StorageExternalizer(); abstract class BaseExternalizer implements Externalizer { - @Override public void writeValue(Element dataElement, T value) { dataElement.setAttribute(VALUE_ATTRIBUTE, value.toString()); @@ -47,10 +47,13 @@ public interface Externalizer { void writeValue(Element dataElement, T value); + interface SkippableValue { + } + class FactoryBased implements Externalizer { private final Factory myFactory; - public FactoryBased(Factory factory) { + public FactoryBased(@NotNull Factory factory) { myFactory = factory; } @@ -66,7 +69,7 @@ public interface Externalizer { value.writeExternal(dataElement); } - static FactoryBased create(Factory factory) { + static FactoryBased create(@NotNull Factory factory) { return new FactoryBased<>(factory); } } diff --git a/platform/platform-api/src/com/intellij/util/config/ListProperty.java b/platform/platform-api/src/com/intellij/util/config/ListProperty.java index 4e773072dee1..36b97a370a92 100644 --- a/platform/platform-api/src/com/intellij/util/config/ListProperty.java +++ b/platform/platform-api/src/com/intellij/util/config/ListProperty.java @@ -1,19 +1,4 @@ -/* - * Copyright 2000-2009 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. - */ - +// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.util.config; import org.jetbrains.annotations.NonNls; @@ -68,10 +53,6 @@ public class ListProperty extends AbstractProperty> { return modifiableList; } - public void clearList(AbstractPropertyContainer container) { - getModifiableList(container).clear(); - } - public Iterator getIterator(AbstractPropertyContainer container) { return get(container).iterator(); } diff --git a/plugins/ant/src/com/intellij/lang/ant/config/explorer/AntExplorer.java b/plugins/ant/src/com/intellij/lang/ant/config/explorer/AntExplorer.java index c6513e1babac..57f91a320724 100644 --- a/plugins/ant/src/com/intellij/lang/ant/config/explorer/AntExplorer.java +++ b/plugins/ant/src/com/intellij/lang/ant/config/explorer/AntExplorer.java @@ -302,7 +302,7 @@ public class AntExplorer extends SimpleToolWindowPanel implements DataProvider, public void setBuildFileProperties() { final AntBuildFileBase buildFile = getCurrentBuildFile(); - if (buildFile != null && BuildFilePropertiesPanel.editBuildFile(buildFile, myProject)) { + if (buildFile != null && BuildFilePropertiesPanel.editBuildFile(buildFile)) { myConfig.updateBuildFile(buildFile); } } diff --git a/plugins/ant/src/com/intellij/lang/ant/config/impl/AntBuildFileImpl.java b/plugins/ant/src/com/intellij/lang/ant/config/impl/AntBuildFileImpl.java index 2baafa3b536b..6ad383ba7826 100644 --- a/plugins/ant/src/com/intellij/lang/ant/config/impl/AntBuildFileImpl.java +++ b/plugins/ant/src/com/intellij/lang/ant/config/impl/AntBuildFileImpl.java @@ -1,18 +1,4 @@ -/* - * Copyright 2000-2016 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. - */ +// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.lang.ant.config.impl; import com.intellij.codeInsight.daemon.DaemonCodeAnalyzer; @@ -44,8 +30,7 @@ import java.io.File; import java.util.*; public class AntBuildFileImpl implements AntBuildFileBase { - - private static final Logger LOG = Logger.getInstance("#com.intellij.lang.ant.config.impl.AntBuildFileImpl"); + private static final Logger LOG = Logger.getInstance(AntBuildFileImpl.class); @NonNls private static final String ANT_LIB = "/.ant/lib"; private volatile Map myCachedExternalProperties; private final Object myOptionsLock = new Object(); diff --git a/plugins/ant/src/com/intellij/lang/ant/config/impl/TargetFilter.java b/plugins/ant/src/com/intellij/lang/ant/config/impl/TargetFilter.java index 7c6556fbd1cf..bfe4accb1d2c 100644 --- a/plugins/ant/src/com/intellij/lang/ant/config/impl/TargetFilter.java +++ b/plugins/ant/src/com/intellij/lang/ant/config/impl/TargetFilter.java @@ -1,35 +1,20 @@ -/* - * Copyright 2000-2009 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. - */ +// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.lang.ant.config.impl; import com.intellij.lang.ant.config.AntBuildTarget; import com.intellij.openapi.util.JDOMExternalizable; -import com.intellij.openapi.util.WriteExternalException; +import com.intellij.util.config.Externalizer; import org.jdom.Element; import org.jetbrains.annotations.NonNls; +import org.jetbrains.annotations.NotNull; -public final class TargetFilter implements JDOMExternalizable { +public final class TargetFilter implements JDOMExternalizable, Externalizer.SkippableValue { @NonNls private static final String FILTER_TARGET_NAME = "targetName"; @NonNls private static final String FILTER_IS_VISIBLE = "isVisible"; private String myTargetName; private boolean myVisible; private String myDescription = ""; - public TargetFilter() {} - public TargetFilter(String targetName, boolean isVisible) { myTargetName = targetName; myVisible = isVisible; @@ -54,11 +39,10 @@ public final class TargetFilter implements JDOMExternalizable { } @Override - public void writeExternal(Element element) throws WriteExternalException { + public void writeExternal(@NotNull Element element) { final String targetName = getTargetName(); if (targetName == null) { - // incomplete tag - throw new WriteExternalException(); + return; } element.setAttribute(FILTER_TARGET_NAME, targetName); element.setAttribute(FILTER_IS_VISIBLE, Boolean.valueOf(isVisible()).toString()); @@ -73,6 +57,7 @@ public final class TargetFilter implements JDOMExternalizable { myDescription = target.getNotEmptyDescription(); } + @NotNull public static TargetFilter fromTarget(AntBuildTarget target) { TargetFilter filter = new TargetFilter(target.getName(), target.isDefault()); filter.myDescription = target.getNotEmptyDescription(); diff --git a/plugins/ant/src/com/intellij/lang/ant/config/impl/configuration/BuildFilePropertiesPanel.java b/plugins/ant/src/com/intellij/lang/ant/config/impl/configuration/BuildFilePropertiesPanel.java index 606dfa386c4a..d015cc84cedb 100644 --- a/plugins/ant/src/com/intellij/lang/ant/config/impl/configuration/BuildFilePropertiesPanel.java +++ b/plugins/ant/src/com/intellij/lang/ant/config/impl/configuration/BuildFilePropertiesPanel.java @@ -1,18 +1,4 @@ -/* - * Copyright 2000-2013 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. - */ +// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.lang.ant.config.impl.configuration; import com.intellij.lang.ant.AntBundle; @@ -21,7 +7,6 @@ import com.intellij.lang.ant.config.impl.*; import com.intellij.openapi.Disposable; import com.intellij.openapi.application.Application; import com.intellij.openapi.application.ApplicationManager; -import com.intellij.openapi.project.Project; import com.intellij.openapi.projectRoots.ProjectJdkTable; import com.intellij.openapi.projectRoots.Sdk; import com.intellij.openapi.projectRoots.ui.ProjectJdksEditor; @@ -35,7 +20,6 @@ import com.intellij.util.config.AbstractProperty; import com.intellij.util.ui.ColumnInfo; import com.intellij.util.ui.ListTableModel; import org.jetbrains.annotations.NonNls; -import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import javax.swing.*; @@ -51,8 +35,8 @@ public class BuildFilePropertiesPanel { private final Form myForm; private AntBuildFileBase myBuildFile; - private BuildFilePropertiesPanel(@NotNull final Project project) { - myForm = new Form(project); + private BuildFilePropertiesPanel() { + myForm = new Form(); } private void reset(final AntBuildFileBase buildFile) { @@ -90,8 +74,8 @@ public class BuildFilePropertiesPanel { Disposer.dispose(myForm); } - public static boolean editBuildFile(AntBuildFileBase buildFile, @NotNull final Project project) { - BuildFilePropertiesPanel panel = new BuildFilePropertiesPanel(project); + public static boolean editBuildFile(AntBuildFileBase buildFile) { + BuildFilePropertiesPanel panel = new BuildFilePropertiesPanel(); panel.reset(buildFile); return panel.showDialog(); } @@ -135,20 +119,19 @@ public class BuildFilePropertiesPanel { private JCheckBox myCollapseFinishedTargets; private final Tab[] myTabs; private final UIPropertyBinding.Composite myBinding = new UIPropertyBinding.Composite(); - private final TabbedPaneWrapper myWrapper; - private Form(@NotNull final Project project) { + private Form() { myTabs = new Tab[]{ new PropertiesTab(), - new ExecutionTab(GlobalAntConfiguration.getInstance(), project), + new ExecutionTab(GlobalAntConfiguration.getInstance()), new AdditionalClasspathTab(), new FiltersTab() }; myHeapSizeLabel.setLabelFor(myXmx); - myWrapper = new TabbedPaneWrapper(this); + TabbedPaneWrapper wrapper = new TabbedPaneWrapper(this); myTabsPlace.setLayout(new BorderLayout()); - myTabsPlace.add(myWrapper.getComponent(), BorderLayout.CENTER); + myTabsPlace.add(wrapper.getComponent(), BorderLayout.CENTER); myBinding.bindBoolean(myRunInBackground, AntBuildFileImpl.RUN_IN_BACKGROUND); myBinding.bindBoolean(myCloseOnNoError, AntBuildFileImpl.CLOSE_ON_NO_ERRORS); @@ -158,7 +141,7 @@ public class BuildFilePropertiesPanel { myBinding.bindInt(myXss, AntBuildFileImpl.MAX_STACK_SIZE); for (Tab tab : myTabs) { - myWrapper.addTab(tab.getDisplayName(), tab.getComponent()); + wrapper.addTab(tab.getDisplayName(), tab.getComponent()); } } @@ -414,11 +397,9 @@ public class BuildFilePropertiesPanel { private AntReference myProjectDefaultAnt = null; private final GlobalAntConfiguration myAntGlobalConfiguration; - private final Project myProject; - ExecutionTab(final GlobalAntConfiguration antConfiguration, @NotNull final Project project) { + ExecutionTab(final GlobalAntConfiguration antConfiguration) { myAntGlobalConfiguration = antConfiguration; - myProject = project; myAntCommandLine.attachLabel(myAntCmdLineLabel); myAntCommandLine.setDialogCaption(AntBundle.message("run.execution.tab.ant.command.line.dialog.title")); setLabelFor(myJDKLabel, myJDKs); diff --git a/plugins/ant/src/com/intellij/lang/ant/config/impl/configuration/UIPropertyBinding.java b/plugins/ant/src/com/intellij/lang/ant/config/impl/configuration/UIPropertyBinding.java index 806a6b9e31c5..963b4c7f600f 100644 --- a/plugins/ant/src/com/intellij/lang/ant/config/impl/configuration/UIPropertyBinding.java +++ b/plugins/ant/src/com/intellij/lang/ant/config/impl/configuration/UIPropertyBinding.java @@ -1,12 +1,10 @@ -// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.lang.ant.config.impl.configuration; -import com.intellij.openapi.util.Condition; import com.intellij.openapi.util.Factory; import com.intellij.openapi.util.JDOMExternalizable; import com.intellij.ui.DocumentAdapter; import com.intellij.ui.ScrollingUtil; -import com.intellij.ui.SortedListModel; import com.intellij.ui.TableUtil; import com.intellij.ui.table.BaseTableView; import com.intellij.util.config.AbstractProperty; @@ -17,7 +15,10 @@ import com.intellij.util.ui.ListTableModel; import org.jetbrains.annotations.NotNull; import javax.swing.*; -import javax.swing.event.*; +import javax.swing.event.DocumentEvent; +import javax.swing.event.DocumentListener; +import javax.swing.event.TableModelEvent; +import javax.swing.event.TableModelListener; import javax.swing.table.JTableHeader; import javax.swing.table.TableCellEditor; import javax.swing.table.TableColumn; @@ -29,7 +30,10 @@ import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.beans.PropertyChangeListener; import java.beans.PropertyChangeSupport; -import java.util.*; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Iterator; +import java.util.List; public abstract class UIPropertyBinding { public abstract void loadValues(AbstractProperty.AbstractPropertyContainer container); @@ -48,10 +52,9 @@ public abstract class UIPropertyBinding { public static class Composite extends UIPropertyBinding { private final ArrayList myBindings = new ArrayList<>(); - public ToggleButtonBinding bindBoolean(JToggleButton toggleButton, AbstractProperty property) { + public void bindBoolean(JToggleButton toggleButton, AbstractProperty property) { ToggleButtonBinding binding = new ToggleButtonBinding(toggleButton, property); myBindings.add(binding); - return binding; } public void bindInt(JTextComponent textComponent, AbstractProperty property) { @@ -193,10 +196,6 @@ public abstract class UIPropertyBinding { getProperty().set(container, getComponent().isSelected()); } - public void addChangeListener(PropertyChangeListener listener) { - myChangeSupport.addListener(listener); - } - @Override public void beforeClose(AbstractProperty.AbstractPropertyContainer container) { myChangeSupport.stop(); @@ -274,7 +273,7 @@ public abstract class UIPropertyBinding { public static ChangeValueSupport create(Comp component, ListenerInstaller installer, String propertyName) { - return new ChangeValueSupport(component, installer, propertyName); + return new ChangeValueSupport<>(component, installer, propertyName); } public void stop() { @@ -408,11 +407,8 @@ public abstract class UIPropertyBinding { myModel.setItems(myProperty.getModifiableList(container)); if (myModel.isSortable()) { final ColumnInfo[] columnInfos = myModel.getColumnInfos(); - int sortByColumn = -1; - for (int idx = 0; idx < columnInfos.length; idx++) { - ColumnInfo columnInfo = columnInfos[idx]; + for (ColumnInfo columnInfo : columnInfos) { if (columnInfo.isSortable()) { - sortByColumn = idx; break; } } @@ -451,74 +447,9 @@ public abstract class UIPropertyBinding { getComponent().getTableHeader().setResizingAllowed(true); } - public void addAddFacility(JButton addButton, final Factory factory) { - myComponents.add(addButton); - addButton.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - JTable table = getComponent(); - if (table.isEditing() && !table.getCellEditor().stopCellEditing()) { - return; - } - T item = factory.create(); - if (item == null) { - return; - } - ArrayList items = new ArrayList<>(myModel.getItems()); - items.add(item); - myModel.setItems(items); - int newIndex = myModel.indexOf(item); - ListSelectionModel selectionModel = table.getSelectionModel(); - selectionModel.clearSelection(); - selectionModel.setSelectionInterval(newIndex, newIndex); - ColumnInfo[] columns = myModel.getColumnInfos(); - for (int i = 0; i < columns.length; i++) { - ColumnInfo column = columns[i]; - if (column.isCellEditable(item)) { - table.requestFocusInWindow(); - table.editCellAt(newIndex, i); - break; - } - } - } - }); - } - public void setSortable(boolean isSortable) { myModel.setSortable(isSortable); } - - public void addRemoveFacility(final JButton button, final Condition removable) { - myComponents.add(button); - button.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - TableUtil.removeSelectedItems(getComponent()); - } - }); - getComponent().getSelectionModel().addListSelectionListener(new ListSelectionListener() { - @Override - public void valueChanged(ListSelectionEvent e) { - updateRemoveButton(button, removable); - } - }); - } - - public void updateRemoveButton(JButton button, Condition removable) { - final JTable table = getComponent(); - final ListSelectionModel selectionModel = table.getSelectionModel(); - boolean enable = false; - if (!selectionModel.isSelectionEmpty()) { - enable = true; - for (int i : table.getSelectedRows()) { - if (!removable.value(myModel.getItems().get(i))) { - enable = false; - break; - } - } - } - button.setEnabled(enable); - } } private static abstract class BaseListBinding extends UIPropertyBinding { @@ -574,23 +505,6 @@ public abstract class UIPropertyBinding { } } - public static class SortedListBinding extends BaseListBinding { - public SortedListBinding(JList list, ListProperty property, Comparator comparator) { - super(property, list); - list.setModel(new SortedListModel<>(comparator)); - } - - @Override - public void loadValues(AbstractProperty.AbstractPropertyContainer container) { - getModel().setAll(getProperty().get(container)); - ScrollingUtil.ensureSelectionExists(getList()); - } - - private SortedListModel getModel() { - return ((SortedListModel)getList().getModel()); - } - } - public static class OrderListBinding extends BaseListBinding { public OrderListBinding(JList list, ListProperty property) { super(property, list); @@ -613,7 +527,7 @@ public abstract class UIPropertyBinding { return ((DefaultListModel)getList().getModel()); } - public void addAddManyFacility(JButton button, final Factory> factory) { + public void addAddManyFacility(JButton button, final Factory> factory) { button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) {