|
|
@@ -0,0 +1,167 @@
|
|
|
+package com.leanwo.management;
|
|
|
+
|
|
|
+import java.awt.AWTEvent;
|
|
|
+import java.awt.BorderLayout;
|
|
|
+import java.awt.Color;
|
|
|
+import java.awt.Dimension;
|
|
|
+import java.awt.Image;
|
|
|
+import java.awt.Toolkit;
|
|
|
+import java.awt.event.ActionEvent;
|
|
|
+import java.awt.event.ActionListener;
|
|
|
+import java.io.IOException;
|
|
|
+import java.net.URL;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Vector;
|
|
|
+
|
|
|
+import javax.imageio.ImageIO;
|
|
|
+import javax.swing.JButton;
|
|
|
+import javax.swing.JFrame;
|
|
|
+import javax.swing.JScrollPane;
|
|
|
+import javax.swing.JTable;
|
|
|
+import javax.swing.JToolBar;
|
|
|
+import javax.swing.ListSelectionModel;
|
|
|
+import javax.swing.table.DefaultTableModel;
|
|
|
+import javax.swing.table.TableColumnModel;
|
|
|
+
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+
|
|
|
+import com.leanwo.management.config.AppConfig;
|
|
|
+import com.leanwo.management.config.DataSourceConfig;
|
|
|
+import com.leanwo.management.config.DataSourceConfigList;
|
|
|
+import com.leanwo.management.util.SpringUtil;
|
|
|
+import com.leanwo.management.widget.SelectEventListener;
|
|
|
+import com.leanwo.management.widget.SelectObject;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 数据源设置
|
|
|
+ *
|
|
|
+ * @author YangZhiJie
|
|
|
+ *
|
|
|
+ */
|
|
|
+public class DataSourceSettingFrame extends JFrame {
|
|
|
+
|
|
|
+ private static Logger logger = LoggerFactory.getLogger(DataSourceSettingFrame.class);
|
|
|
+
|
|
|
+ private JButton saveButton;
|
|
|
+
|
|
|
+ // 得到显示器屏幕的宽高
|
|
|
+ private int width = Toolkit.getDefaultToolkit().getScreenSize().width;
|
|
|
+ private int height = Toolkit.getDefaultToolkit().getScreenSize().height;
|
|
|
+
|
|
|
+ // 定义窗体的宽高
|
|
|
+ private int windowsWidth = 800;
|
|
|
+ private int windowsHeight = 400;
|
|
|
+
|
|
|
+ private DefaultTableModel tableModel; //表格模型对象
|
|
|
+
|
|
|
+ public DataSourceSettingFrame() {
|
|
|
+ super();
|
|
|
+ this.initView();
|
|
|
+ setTitle("数据源配置");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private void initView() {
|
|
|
+ this.setLayout(new BorderLayout(5,5));
|
|
|
+
|
|
|
+ JToolBar toolbar = new JToolBar();
|
|
|
+ toolbar.setFloatable(false);
|
|
|
+ toolbar.setBorderPainted(false);
|
|
|
+
|
|
|
+ add(toolbar, BorderLayout.NORTH);
|
|
|
+
|
|
|
+ saveButton = new JButton("保存");
|
|
|
+ toolbar.add(saveButton);
|
|
|
+
|
|
|
+
|
|
|
+ JScrollPane scrollPane = new JScrollPane(); // 支持滚动
|
|
|
+ getContentPane().add(scrollPane, BorderLayout.CENTER);
|
|
|
+ String[] columnNames = { "数据源beanId", "链接字符串", "用户名", "密码" };
|
|
|
+
|
|
|
+ DataSourceConfigList dataSourceConfigList = (DataSourceConfigList)SpringUtil.getSingleBean(DataSourceConfigList.class);
|
|
|
+ String [][] tableValues= null;
|
|
|
+ if(dataSourceConfigList != null) {
|
|
|
+ List<DataSourceConfig> items = dataSourceConfigList.getItems();
|
|
|
+ tableValues= new String[items.size()][4];
|
|
|
+ if(items != null) {
|
|
|
+ for (int row = 0; row < items.size(); row++) { // 获得数据
|
|
|
+ DataSourceConfig item = items.get(row);
|
|
|
+ String[] rowValue = { item.getBeanId(), item.getUrl(), item.getUsername(), item.getPassword() };
|
|
|
+ tableValues[row] = rowValue;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ tableModel = new DefaultTableModel(tableValues,columnNames);
|
|
|
+
|
|
|
+ JTable table = new JTable(tableModel); // 自定义的表格
|
|
|
+ table.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN); // 关闭表格列的自动调整功能。
|
|
|
+ table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); // 单选
|
|
|
+// table.setSelectionBackground(Color.YELLOW);
|
|
|
+// table.setSelectionForeground(Color.RED);
|
|
|
+ table.setRowHeight(30);
|
|
|
+
|
|
|
+ TableColumnModel columnModel = table.getColumnModel();
|
|
|
+ columnModel.getColumn(0).setPreferredWidth(100);
|
|
|
+ columnModel.getColumn(1).setPreferredWidth(400);
|
|
|
+ columnModel.getColumn(2).setPreferredWidth(100);
|
|
|
+ columnModel.getColumn(3).setPreferredWidth(100);
|
|
|
+ table.setColumnModel(columnModel);
|
|
|
+
|
|
|
+ scrollPane.setViewportView(table); // 支持滚动
|
|
|
+
|
|
|
+ add(scrollPane, BorderLayout.CENTER);
|
|
|
+
|
|
|
+ // 设置图标
|
|
|
+ URL path = DataSourceSettingFrame.class.getResource("/prodog_16.png");
|
|
|
+ Image image;
|
|
|
+ try {
|
|
|
+ image = ImageIO.read(path);
|
|
|
+ this.setIconImage(image);
|
|
|
+ } catch (IOException e) {
|
|
|
+ logger.error("读取图片异常。", e);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 激活窗口事件
|
|
|
+ this.enableEvents(AWTEvent.WINDOW_EVENT_MASK);
|
|
|
+
|
|
|
+ // 设置窗体位置和大小
|
|
|
+ this.setBounds((width - windowsWidth) / 2, (height - windowsHeight) / 2, windowsWidth, windowsHeight);
|
|
|
+
|
|
|
+ // 保存按钮
|
|
|
+ this.saveButton.addActionListener(new ActionListener() {
|
|
|
+ @Override
|
|
|
+ public void actionPerformed(ActionEvent e) {
|
|
|
+ save();
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 保存数据
|
|
|
+ * 首先更新yml里面的内容,然后更新各个服务中applicationContext.xml里面的数据
|
|
|
+ */
|
|
|
+ private void save() {
|
|
|
+ Vector vector = tableModel.getDataVector();
|
|
|
+ List<DataSourceConfig> dataSourceConfigs = new ArrayList<DataSourceConfig>();
|
|
|
+ for(int i = 0; i < vector.size(); i ++) {
|
|
|
+ DataSourceConfig dataSourceConfig = new DataSourceConfig();
|
|
|
+ Vector rowData = (Vector)vector.elementAt(i);
|
|
|
+ dataSourceConfig.setBeanId((String)rowData.elementAt(0));
|
|
|
+ dataSourceConfig.setUrl((String)rowData.elementAt(1));
|
|
|
+ dataSourceConfig.setUsername((String)rowData.elementAt(2));
|
|
|
+ dataSourceConfig.setPassword((String)rowData.elementAt(3));
|
|
|
+ dataSourceConfigs.add(dataSourceConfig);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 根据dataSourceConfigs修改yml
|
|
|
+
|
|
|
+ // 根据dataSourceConfigs修改各个服务器中applicationContext.xml里面的数据
|
|
|
+
|
|
|
+ // 可以参考XmlConfigPathService中的方法
|
|
|
+ }
|
|
|
+
|
|
|
+}
|