View Javadoc
1   /*******************************************************************************
2    * Copyhacked (H) 2012-2025.
3    * This program and the accompanying materials
4    * are made available under no term at all, use it like
5    * you want, but share and discuss it
6    * every time possible with every body.
7    * 
8    * Contributors:
9    *      ron190 at ymail dot com - initial implementation
10   ******************************************************************************/
11  package com.jsql.view.swing.manager;
12  
13  import com.jsql.util.I18nUtil;
14  import com.jsql.util.LogLevelUtil;
15  import com.jsql.view.swing.list.ItemList;
16  import com.jsql.view.swing.manager.util.StateButton;
17  import com.jsql.view.swing.util.I18nViewUtil;
18  import com.jsql.view.swing.util.MediatorHelper;
19  import org.apache.commons.lang3.StringUtils;
20  import org.apache.logging.log4j.LogManager;
21  import org.apache.logging.log4j.Logger;
22  
23  import java.awt.*;
24  
25  /**
26   * Manager to display webpages frequently used as backoffice administration.
27   */
28  public class ManagerAdminPage extends AbstractManagerList {
29  
30      private static final Logger LOGGER = LogManager.getRootLogger();
31  
32      public static final String LIST_MANAGER_ADMIN_PAGE = "listManagerAdminPage";
33  
34      /**
35       * Create admin page finder.
36       */
37      public ManagerAdminPage() {
38          super("swing/list/admin-page.txt");
39  
40          this.buildRunButton("ADMIN_PAGE_RUN_BUTTON_LABEL", "ADMIN_PAGE_RUN_BUTTON_TOOLTIP");
41          this.run.setName("runManagerAdminPage");
42          this.run.addActionListener(actionEvent -> this.runSearch());
43          this.listPaths.setName(ManagerAdminPage.LIST_MANAGER_ADMIN_PAGE);  // no tooltip, too annoying
44  
45          this.lastLine.add(this.horizontalGlue);
46          this.lastLine.add(this.progressBar);
47          this.lastLine.add(this.run);
48          this.add(this.lastLine, BorderLayout.SOUTH);
49      }
50  
51      private void runSearch() {
52          if (this.listPaths.getSelectedValuesList().isEmpty()) {
53              LOGGER.log(LogLevelUtil.CONSOLE_ERROR, "Select at least one admin page in the list");
54              return;
55          }
56          
57          String urlAddressBar = MediatorHelper.panelAddressBar().getTextFieldAddress().getText();
58          if (!urlAddressBar.isEmpty() && !urlAddressBar.matches("(?i)^https?://.*")) {
59              if (!urlAddressBar.matches("(?i)^\\w+://.*")) {
60                  LOGGER.log(LogLevelUtil.CONSOLE_INFORM, () -> I18nUtil.valueByKey("LOG_ADMIN_NO_PROTOCOL"));
61                  urlAddressBar = "http://"+ urlAddressBar;
62              } else {
63                  LOGGER.log(LogLevelUtil.CONSOLE_INFORM, () -> I18nUtil.valueByKey("LOG_ADMIN_UNKNOWN_PROTOCOL"));
64                  return;
65              }
66          }
67          
68          String urlFinal = urlAddressBar;
69          new Thread(() -> this.searchAdminPages(urlFinal), "ThreadAdminPage").start();
70      }
71  
72      private void searchAdminPages(String urlAddressBar) {
73          if (this.run.getState() == StateButton.STARTABLE) {
74              if (StringUtils.isEmpty(urlAddressBar)) {
75                  LOGGER.log(LogLevelUtil.CONSOLE_ERROR, "Missing URL in address bar");
76              } else {
77                  LOGGER.log(LogLevelUtil.CONSOLE_DEFAULT, "{} admin pages...", () -> I18nUtil.valueByKey("LOG_CHECKING"));
78                  this.run.setText(I18nViewUtil.valueByKey("ADMIN_PAGE_RUN_BUTTON_STOP"));
79                  this.run.setState(StateButton.STOPPABLE);
80                  this.progressBar.setVisible(true);
81                  this.horizontalGlue.setVisible(false);
82                  MediatorHelper.model().getResourceAccess().createAdminPages(
83                      urlAddressBar,
84                      this.listPaths.getSelectedValuesList().stream().map(ItemList::toString).toList()
85                  );
86                  this.endProcess();
87              }
88          } else if (this.run.getState() == StateButton.STOPPABLE) {
89              MediatorHelper.model().getResourceAccess().stopSearchAdmin();
90              this.run.setEnabled(false);
91              this.run.setState(StateButton.STOPPING);
92          }
93      }
94  }