ShowArticlesController.java 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package com.example.mos.container;
  2. import com.example.mos.common.model.vo.ArticleDataVO;
  3. import com.example.mos.service.ArticleInfoService;
  4. import com.example.mos.service.WeChatAccountInfoService;
  5. import javax.swing.*;
  6. import javax.swing.table.DefaultTableModel;
  7. import java.util.List;
  8. /**
  9. * @Author xiexiang
  10. * @Date 2024/8/1
  11. */
  12. public class ShowArticlesController {
  13. private ShowArticlesModel articlesModel;
  14. private ShowArticlesUI articlesUI;
  15. public ShowArticlesController(ShowArticlesModel model, ShowArticlesUI ui){
  16. this.articlesModel = model;
  17. this.articlesUI = ui;
  18. }
  19. public void getArticles() {
  20. List<ArticleDataVO> articles = getArticlesFromAPI();
  21. // 转换文章数据为二维数组,用于填充表格模型
  22. Object[][] data = convertArticleListToData(articles);
  23. // 定义表头和初始数据模型
  24. String[] columnNames = {"标题", "文章链接", "文章来源", "发布时间","摘要" , "操作"};
  25. DefaultTableModel tableModel = new DefaultTableModel(data, columnNames) {
  26. @Override
  27. public Class<?> getColumnClass(int columnIndex) {
  28. if (columnIndex == 3) {
  29. return JButton.class;
  30. } else {
  31. return super.getColumnClass(columnIndex);
  32. }
  33. }
  34. @Override
  35. public boolean isCellEditable(int row, int column) {
  36. return column == 4;
  37. }
  38. };
  39. articlesModel.tableModel = tableModel;
  40. articlesUI.displayVOS(tableModel, articles);
  41. }
  42. public List<ArticleDataVO> getArticlesFromAPI() {
  43. ArticleInfoService articleInfoService = new ArticleInfoService();
  44. List<ArticleDataVO> articleDataVOS = articleInfoService.getArticles();
  45. for (ArticleDataVO articleDataVO : articleDataVOS) {
  46. if (articleDataVO.getSource().equals(0)) {
  47. ArticleInfoService articleInfoService1 = new ArticleInfoService();
  48. WeChatAccountInfoService weChatAccountInfoService = new WeChatAccountInfoService(articleInfoService1);
  49. String accountName = weChatAccountInfoService.getNameByFakeId(articleDataVO.getCameFrom());
  50. if (accountName != null) {
  51. articleDataVO.setCameFrom(accountName);
  52. }
  53. }
  54. }
  55. return articleDataVOS;
  56. }
  57. // 辅助方法:将文章数据转换为二维数组
  58. private Object[][] convertArticleListToData(List<ArticleDataVO> articles) {
  59. Object[][] data = new Object[articles.size()][3]; // 仅保留标题、来源、发布时间三个字段
  60. for (int i = 0; i < articles.size(); i++) {
  61. ArticleDataVO article = articles.get(i);
  62. data[i] = new Object[] {article.getTitle(), article.getLink(), article.getCameFrom(), article.getTime(), article.getSource()};
  63. }
  64. return data;
  65. }
  66. }