#RUBY-17510 provide list reloadable panel

This commit is contained in:
Dmitry.Krasilschikov
2016-01-28 17:06:03 +03:00
parent 4cd38d818d
commit f86f1f23f8
6 changed files with 298 additions and 155 deletions
@@ -26,6 +26,7 @@ import com.intellij.ui.ListCellRendererWrapper;
import com.intellij.util.ObjectUtils;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.ui.ReloadableComboBoxPanel;
import com.intellij.util.ui.ReloadablePanel;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -116,7 +117,7 @@ public class GithubProjectGeneratorPeer implements WebProjectGenerator.Generator
private final GithubTagListProvider myTagListProvider;
private JComponent myComponent;
private JPanel myVersionPanel;
private ReloadableComboBoxPanel<GithubTagInfo> myReloadableComboBoxPanel;
private ReloadablePanel<GithubTagInfo> myReloadableComboBoxPanel;
public GithubProjectGeneratorPeer(@NotNull AbstractGithubTagDownloadedProjectGenerator generator) {
String ghUserName = generator.getGithubUserName();
@@ -21,7 +21,7 @@
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<grid id="c8f68" binding="myActionPanel" layout-manager="CardLayout" hgap="0" vgap="0">
<grid id="c8f68" binding="myActionPanel" custom-create="true" layout-manager="CardLayout" hgap="0" vgap="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>
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
* 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.
@@ -15,176 +15,34 @@
*/
package com.intellij.util.ui;
import com.intellij.icons.AllIcons;
import com.intellij.openapi.actionSystem.ActionToolbar;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.impl.ActionButton;
import com.intellij.openapi.ui.ComboBox;
import com.intellij.ui.JBColor;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import java.awt.*;
import java.util.Set;
public abstract class ReloadableComboBoxPanel<T> {
public interface DataProvider<T> {
/**
* returns init cached values
*/
@Nullable
Set<T> getCachedValues();
/**
* After getting values method must call #onUpdateValues or #onValuesUpdateError
*/
void updateValuesAsynchronously();
}
private static final String CONTROL_PLACE = "UI.Configuration.Component.Reload.Panel";
private final AsyncProcessIcon myLoadingVersionIcon = new AsyncProcessIcon("Getting possible values");
private final JLabel myErrorMessage = new JLabel();
@Nullable
private volatile DataProvider<T> myDataProvider;
@Nullable
private volatile UpdateStatus myUpdateStatus;
public abstract class ReloadableComboBoxPanel<T> extends ReloadablePanel<T> {
protected JComboBox myComboBox;
protected JPanel myActionPanel;
protected JPanel myMainPanel;
public ReloadableComboBoxPanel() {
myErrorMessage.setForeground(JBColor.RED);
fillActionPanel();
}
public final void setDataProvider(@NotNull DataProvider<T> dataProvider) {
myDataProvider = dataProvider;
Set<T> cachedValues = dataProvider.getCachedValues();
if (cachedValues != null) {
onUpdateValues(cachedValues);
}
}
protected abstract void doUpdateValues(@NotNull Set<T> values);
private JPanel myActionPanel;
private JPanel myMainPanel;
@SuppressWarnings("unchecked")
public T getSelectedValue() {
return (T)myComboBox.getSelectedItem();
}
@NotNull
public final JLabel getErrorComponent() {
return myErrorMessage;
}
public final boolean isBackgroundJobRunning() {
return myUpdateStatus == UpdateStatus.UPDATING;
}
public final void onUpdateValues(@NotNull Set<T> values) {
changeUpdateStatus(UpdateStatus.IDLE);
doUpdateValues(values);
}
public final void reloadValuesInBackground() {
if (myUpdateStatus == UpdateStatus.UPDATING) return;
changeUpdateStatus(UpdateStatus.UPDATING);
myErrorMessage.setText(null);
DataProvider<T> provider = myDataProvider;
assert provider != null;
provider.updateValuesAsynchronously();
}
private void changeUpdateStatus(@NotNull UpdateStatus status) {
CardLayout cardLayout = (CardLayout)myActionPanel.getLayout();
cardLayout.show(myActionPanel, status.name());
if (status == UpdateStatus.UPDATING) {
myLoadingVersionIcon.resume();
}
else {
myLoadingVersionIcon.suspend();
}
myUpdateStatus = status;
}
private void fillActionPanel() {
myActionPanel.add(createReloadButtonPanel(), UpdateStatus.IDLE.name());
myActionPanel.add(createReloadInProgressPanel(), UpdateStatus.UPDATING.name());
changeUpdateStatus(UpdateStatus.IDLE);
}
public final void onValuesUpdateError(@NotNull final String errorMessage) {
UIUtil.invokeLaterIfNeeded(new Runnable() {
@Override
public void run() {
if (getSelectedValue() == null) {
myErrorMessage.setText(errorMessage);
}
changeUpdateStatus(UpdateStatus.IDLE);
}
});
}
@NotNull
private JPanel createReloadButtonPanel() {
ReloadAction reloadAction = new ReloadAction();
ActionButton reloadButton = new ActionButton(
reloadAction,
reloadAction.getTemplatePresentation().clone(),
CONTROL_PLACE,
ActionToolbar.DEFAULT_MINIMUM_BUTTON_SIZE
);
JPanel panel = new JPanel(new BorderLayout(0, 0));
panel.add(reloadButton, BorderLayout.WEST);
return panel;
}
@NotNull
public final JPanel getMainPanel() {
return myMainPanel;
}
@NotNull
private JPanel createReloadInProgressPanel() {
JPanel panel = new JPanel();
panel.add(myLoadingVersionIcon);
return panel;
}
private final class ReloadAction extends AnAction {
private ReloadAction() {
super("Reload list", null, AllIcons.Actions.Refresh);
}
@Override
public void actionPerformed(AnActionEvent e) {
reloadValuesInBackground();
}
@Override
public void update(AnActionEvent e) {
e.getPresentation().setEnabled(true);
}
}
private enum UpdateStatus {
UPDATING, IDLE
}
@NotNull
protected JComboBox createValuesComboBox() {
return new ComboBox();
}
@NotNull
@Override
public JPanel getMainPanel() {
return myMainPanel;
}
private void createUIComponents() {
myComboBox = createValuesComboBox();
myActionPanel = getActionPanel();
}
}
@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.intellij.util.ui.ReloadableListPanel">
<grid id="27dc6" binding="myMainPanel" layout-manager="GridLayoutManager" row-count="2" 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>
<grid id="c5722" 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>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="1" fill="1" indent="0" use-parent-layout="true"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="58690" class="com.intellij.ui.components.JBList" binding="myList" custom-create="true">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="1" fill="0" indent="0" use-parent-layout="false">
<maximum-size width="-1" height="200"/>
</grid>
</constraints>
<properties>
<inheritsPopupMenu value="false"/>
<selectionMode value="0"/>
</properties>
</component>
<grid id="259ea" binding="myActionPanel" custom-create="true" 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="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="1" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children/>
</grid>
</children>
</grid>
<hspacer id="5dcae">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<vspacer id="bb939">
<constraints>
<grid row="1" 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>
</children>
</grid>
</form>
@@ -0,0 +1,47 @@
/*
* 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.
*/
package com.intellij.util.ui;
import com.intellij.ui.components.JBList;
import org.jetbrains.annotations.NotNull;
import javax.swing.*;
public abstract class ReloadableListPanel<T> extends ReloadablePanel<T> {
protected JList myList;
private JPanel myMainPanel;
private JPanel myActionPanel;
@SuppressWarnings("unchecked")
public T getSelectedValue() {
return (T)myList.getSelectedValue();
}
protected void createList() {
myList = new JBList();
}
@NotNull
@Override
public JPanel getMainPanel() {
return myMainPanel;
}
private void createUIComponents() {
myActionPanel = getActionPanel();
createList();
}
}
@@ -0,0 +1,184 @@
/*
* Copyright 2000-2014 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.util.ui;
import com.intellij.icons.AllIcons;
import com.intellij.openapi.actionSystem.ActionToolbar;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.impl.ActionButton;
import com.intellij.ui.JBColor;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import java.awt.*;
import java.util.Set;
public abstract class ReloadablePanel<T> {
public interface DataProvider<T> {
/**
* returns init cached values
*/
@Nullable
Set<T> getCachedValues();
/**
* After getting values method must call #onUpdateValues or #onValuesUpdateError
*/
void updateValuesAsynchronously();
}
private static final String CONTROL_PLACE = "UI.Configuration.Component.Reload.Panel";
private final AsyncProcessIcon myLoadingVersionIcon = new AsyncProcessIcon("Getting possible values");
private final JLabel myErrorMessage = new JLabel();
@Nullable
private volatile DataProvider<T> myDataProvider;
@Nullable
private volatile UpdateStatus myUpdateStatus;
protected JPanel myActionPanel;
public ReloadablePanel() {
myErrorMessage.setForeground(JBColor.RED);
createActionPanel();
fillActionPanel();
}
private void createActionPanel() {
myActionPanel = new JPanel(new CardLayout());
}
public final void setDataProvider(@NotNull DataProvider<T> dataProvider) {
myDataProvider = dataProvider;
Set<T> cachedValues = dataProvider.getCachedValues();
if (cachedValues != null) {
onUpdateValues(cachedValues);
}
}
protected abstract void doUpdateValues(@NotNull Set<T> values);
public abstract T getSelectedValue();
@NotNull
public final JLabel getErrorComponent() {
return myErrorMessage;
}
public final boolean isBackgroundJobRunning() {
return myUpdateStatus == UpdateStatus.UPDATING;
}
public final void onUpdateValues(@NotNull Set<T> values) {
changeUpdateStatus(UpdateStatus.IDLE);
doUpdateValues(values);
}
public final void reloadValuesInBackground() {
if (myUpdateStatus == UpdateStatus.UPDATING) return;
changeUpdateStatus(UpdateStatus.UPDATING);
myErrorMessage.setText(null);
DataProvider<T> provider = myDataProvider;
assert provider != null;
provider.updateValuesAsynchronously();
}
private void changeUpdateStatus(@NotNull UpdateStatus status) {
CardLayout cardLayout = (CardLayout)myActionPanel.getLayout();
cardLayout.show(myActionPanel, status.name());
if (status == UpdateStatus.UPDATING) {
myLoadingVersionIcon.resume();
}
else {
myLoadingVersionIcon.suspend();
}
myUpdateStatus = status;
}
protected void fillActionPanel() {
myActionPanel.add(createReloadButtonPanel(), UpdateStatus.IDLE.name());
myActionPanel.add(createReloadInProgressPanel(), UpdateStatus.UPDATING.name());
changeUpdateStatus(UpdateStatus.IDLE);
}
public final void onValuesUpdateError(@NotNull final String errorMessage) {
UIUtil.invokeLaterIfNeeded(new Runnable() {
@Override
public void run() {
if (getSelectedValue() == null) {
myErrorMessage.setText(errorMessage);
}
changeUpdateStatus(UpdateStatus.IDLE);
}
});
}
@NotNull
private JPanel createReloadButtonPanel() {
ReloadAction reloadAction = new ReloadAction();
ActionButton reloadButton = new ActionButton(
reloadAction,
reloadAction.getTemplatePresentation().clone(),
CONTROL_PLACE,
ActionToolbar.DEFAULT_MINIMUM_BUTTON_SIZE
);
JPanel panel = new JPanel(new BorderLayout(0, 0));
panel.add(reloadButton, BorderLayout.WEST);
return panel;
}
@NotNull
public abstract JPanel getMainPanel();
@NotNull
JPanel getActionPanel(){
return myActionPanel;
}
@NotNull
private JPanel createReloadInProgressPanel() {
JPanel panel = new JPanel();
panel.add(myLoadingVersionIcon);
return panel;
}
private final class ReloadAction extends AnAction {
private ReloadAction() {
super("Reload list", null, AllIcons.Actions.Refresh);
}
@Override
public void actionPerformed(AnActionEvent e) {
reloadValuesInBackground();
}
@Override
public void update(AnActionEvent e) {
e.getPresentation().setEnabled(true);
}
}
private enum UpdateStatus {
UPDATING, IDLE
}
}