You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
153 lines
5.4 KiB
153 lines
5.4 KiB
package com.fr.plugin.db.redis.ui; |
|
|
|
import com.fanruan.api.design.ui.component.UIIntNumberField; |
|
import com.fanruan.api.design.ui.component.UILabel; |
|
import com.fanruan.api.design.work.ConnectionComboBoxPanel; |
|
import com.fr.data.impl.Connection; |
|
import com.fr.data.operator.DataOperator; |
|
import com.fanruan.api.design.ui.component.UIButton; |
|
import com.fanruan.api.design.ui.component.UIPlaceholderTextField; |
|
import com.fanruan.api.design.DesignKit; |
|
import com.fanruan.api.data.ConnectionKit; |
|
import com.fr.plugin.db.redis.core.RedisDatabaseConnection; |
|
import com.fr.plugin.db.redis.ui.event.DataLoadedListener; |
|
import com.fanruan.api.util.ArrayKit; |
|
import com.fanruan.api.util.StringKit; |
|
import com.fanruan.api.log.LogKit; |
|
import com.fanruan.api.design.ui.container.BasicPane; |
|
import com.fanruan.api.design.util.GUICoreKit; |
|
|
|
import javax.swing.*; |
|
import java.awt.*; |
|
import java.awt.event.ActionEvent; |
|
import java.awt.event.ActionListener; |
|
import java.util.ArrayList; |
|
import java.util.List; |
|
|
|
|
|
public class RedisDBConnectionChosePane extends BasicPane { |
|
|
|
private ConnectionComboBoxPanel connectionComboBoxPanel; |
|
private DefaultListModel listModel = new DefaultListModel(); |
|
private List<DataLoadedListener> listeners = new ArrayList<DataLoadedListener>(); |
|
private UIPlaceholderTextField keysPatternTextField; |
|
private UIIntNumberField dbIndexNumberField; |
|
|
|
public RedisDBConnectionChosePane() { |
|
setLayout(new BorderLayout(4, 4)); |
|
connectionComboBoxPanel = new ConnectionComboBoxPanel(Connection.class) { |
|
|
|
protected void filterConnection(Connection connection, String conName, List<String> nameList) { |
|
connection.addConnection(nameList, conName, new Class[]{RedisDatabaseConnection.class}); |
|
} |
|
}; |
|
|
|
add(connectionComboBoxPanel, BorderLayout.NORTH); |
|
connectionComboBoxPanel.addComboBoxActionListener(new ActionListener() { |
|
@Override |
|
public void actionPerformed(ActionEvent e) { |
|
loadKeys(); |
|
} |
|
}); |
|
JList list = new JList(listModel); |
|
|
|
keysPatternTextField = new UIPlaceholderTextField(10); |
|
keysPatternTextField.setPlaceholder(DesignKit.i18nText("Plugin-Redis_Keys_Pattern")); |
|
|
|
dbIndexNumberField = new UIIntNumberField(); |
|
dbIndexNumberField.setColumns(2); |
|
dbIndexNumberField.setInt(0); |
|
|
|
JPanel centerPane = new JPanel(new BorderLayout()); |
|
|
|
UIButton searchButton = new UIButton(DesignKit.i18nText("Plugin-Redis_Keys_Pattern_Search")); |
|
searchButton.addActionListener(new ActionListener() { |
|
@Override |
|
public void actionPerformed(ActionEvent e) { |
|
loadKeys(); |
|
} |
|
}); |
|
|
|
centerPane.add(GUICoreKit.createBorderLayoutPane( |
|
keysPatternTextField, BorderLayout.CENTER, |
|
GUICoreKit.createBorderLayoutPane( |
|
new UILabel(DesignKit.i18nText("Plugin-Redis_DB_Simple_Index")), BorderLayout.WEST, |
|
dbIndexNumberField, BorderLayout.CENTER |
|
), BorderLayout.WEST, |
|
searchButton, BorderLayout.EAST), BorderLayout.NORTH); |
|
|
|
centerPane.add(list, BorderLayout.CENTER); |
|
|
|
add(centerPane, BorderLayout.CENTER); |
|
} |
|
|
|
private void loadKeys() { |
|
String name = getSelectRedisConnectionName(); |
|
if (StringKit.isEmpty(name)) { |
|
clearList(); |
|
fireDataLoaded(ArrayKit.EMPTY_STRING_ARRAY); |
|
return; |
|
} |
|
RedisDatabaseConnection connection = ConnectionKit.getConnection(name, RedisDatabaseConnection.class); |
|
if (connection != null) { |
|
listMatchedPatternKeys(connection); |
|
} |
|
} |
|
|
|
private void clearList() { |
|
listModel.clear(); |
|
} |
|
|
|
private void listMatchedPatternKeys(final RedisDatabaseConnection connection) { |
|
clearList(); |
|
new SwingWorker<String[], Void>() { |
|
|
|
@Override |
|
protected String[] doInBackground() throws Exception { |
|
String keysPattern = keysPatternTextField.getText(); |
|
if (StringKit.isEmpty(keysPattern)) { |
|
return ArrayKit.EMPTY_STRING_ARRAY; |
|
} else { |
|
String dbIndex = String.valueOf(dbIndexNumberField.getInt()); |
|
return DataOperator.getInstance().getTableSummary(connection, keysPattern, dbIndex); |
|
} |
|
} |
|
|
|
@Override |
|
protected void done() { |
|
try { |
|
String[] keys = get(); |
|
for (String name : keys) { |
|
listModel.addElement(name); |
|
} |
|
fireDataLoaded(keys); |
|
} catch (Exception e) { |
|
LogKit.error(e.getMessage(), e); |
|
} |
|
} |
|
}.execute(); |
|
} |
|
|
|
public String getSelectRedisConnectionName() { |
|
return connectionComboBoxPanel.getSelectedItem(); |
|
} |
|
|
|
public void populateConnection(Connection connection) { |
|
connectionComboBoxPanel.populate(connection); |
|
} |
|
|
|
public void addDataLoadedListener(DataLoadedListener listener) { |
|
listeners.add(listener); |
|
} |
|
|
|
private void fireDataLoaded(String[] data) { |
|
for (DataLoadedListener listener : listeners) { |
|
listener.fireEvent(data); |
|
} |
|
} |
|
|
|
@Override |
|
protected String title4PopupWindow() { |
|
return "Choose"; |
|
} |
|
} |