package com.example.mos.container; import com.example.mos.common.model.vo.ArticleDataVO; import com.example.mos.service.ArticleInfoService; import com.example.mos.service.WeChatAccountInfoService; import javax.swing.*; import javax.swing.table.DefaultTableModel; import java.util.List; /** * @Author xiexiang * @Date 2024/8/1 */ public class ShowArticlesController { private ShowArticlesModel articlesModel; private ShowArticlesUI articlesUI; public ShowArticlesController(ShowArticlesModel model, ShowArticlesUI ui){ this.articlesModel = model; this.articlesUI = ui; } public void getArticles() { List articles = getArticlesFromAPI(); // 转换文章数据为二维数组,用于填充表格模型 Object[][] data = convertArticleListToData(articles); // 定义表头和初始数据模型 String[] columnNames = {"标题", "文章链接", "文章来源", "发布时间","摘要" , "操作"}; DefaultTableModel tableModel = new DefaultTableModel(data, columnNames) { @Override public Class getColumnClass(int columnIndex) { if (columnIndex == 3) { return JButton.class; } else { return super.getColumnClass(columnIndex); } } @Override public boolean isCellEditable(int row, int column) { return column == 4; } }; articlesModel.tableModel = tableModel; articlesUI.displayVOS(tableModel, articles); } public List getArticlesFromAPI() { ArticleInfoService articleInfoService = new ArticleInfoService(); List articleDataVOS = articleInfoService.getArticles(); for (ArticleDataVO articleDataVO : articleDataVOS) { if (articleDataVO.getSource().equals(0)) { ArticleInfoService articleInfoService1 = new ArticleInfoService(); WeChatAccountInfoService weChatAccountInfoService = new WeChatAccountInfoService(articleInfoService1); String accountName = weChatAccountInfoService.getNameByFakeId(articleDataVO.getCameFrom()); if (accountName != null) { articleDataVO.setCameFrom(accountName); } } } return articleDataVOS; } // 辅助方法:将文章数据转换为二维数组 private Object[][] convertArticleListToData(List articles) { Object[][] data = new Object[articles.size()][3]; // 仅保留标题、来源、发布时间三个字段 for (int i = 0; i < articles.size(); i++) { ArticleDataVO article = articles.get(i); data[i] = new Object[] {article.getTitle(), article.getLink(), article.getCameFrom(), article.getTime(), article.getSource()}; } return data; } }