001package com.ganteater.ae.desktop.ui; 002 003import java.awt.Color; 004import java.io.File; 005import java.util.ArrayList; 006import java.util.List; 007import java.util.Set; 008 009import javax.swing.JComponent; 010import javax.swing.JEditorPane; 011import javax.swing.JScrollPane; 012import javax.swing.JTabbedPane; 013import javax.swing.SwingConstants; 014 015import org.apache.commons.io.FilenameUtils; 016import org.apache.commons.lang.StringUtils; 017 018import com.ganteater.ae.CustomizedMenu; 019import com.ganteater.ae.ILogger; 020import com.ganteater.ae.Logger; 021import com.ganteater.ae.desktop.editor.EditorPane; 022import com.ganteater.ae.util.xml.easyparser.EasyParser; 023import com.ganteater.ae.util.xml.easyparser.Node; 024 025public class MenuDialogFrame extends DialogFrame { 026 027 private static final long serialVersionUID = 1L; 028 private static ILogger log = new Logger("Menu"); 029 private static final String MENU_TITLE = "Menu"; 030 031 private JTabbedPane fCustomizedMenu; 032 private JComponent preferedMenu; 033 private List<CustomizedMenu> configurableMenu; 034 private AEFrame frame; 035 036 public MenuDialogFrame(AEFrame frame) { 037 super(frame, MENU_TITLE); 038 this.frame = frame; 039 fCustomizedMenu = new JTabbedPane(); 040 getContentPane().add(fCustomizedMenu); 041 } 042 043 public void showDialogTestSelect(Set<String> tasks) { 044 045 int selectedTab = fCustomizedMenu.getSelectedIndex(); 046 if (selectedTab < 0) { 047 selectedTab = 0; 048 } 049 if (preferedMenu != null) { 050 fCustomizedMenu.remove(preferedMenu); 051 } 052 053 Object[] taskArray = tasks.toArray(); 054 055 String title = "Selected"; 056 if (taskArray.length == 0) { 057 taskArray = frame.getWorkspace().getTestsList(); 058 title = "All Recipes"; 059 } 060 061 if (taskArray.length > 0) { 062 CustomizedMenu menu = new CustomizedMenu(); 063 for (Object recipeName : taskArray) { 064 Node itemNode = new Node("item"); 065 itemNode.setAttribute("task", (String) recipeName); 066 menu.addItem(itemNode); 067 } 068 preferedMenu = createMenuTab(menu); 069 fCustomizedMenu.addTab(title, preferedMenu); 070 fCustomizedMenu.setSelectedIndex(selectedTab); 071 } 072 073 showFrame(); 074 } 075 076 private JScrollPane createMenuTab(CustomizedMenu menu) { 077 StringBuilder content = new StringBuilder("<html><body>"); 078 menu.sort((p1, p2) -> p1.getName() != null && p2.getName() != null 079 ? p1.getName().compareToIgnoreCase(p2.getName()) 080 : 1); 081 for (CustomizedMenu.Item item : menu) { 082 String description = item.getDescription(); 083 JParsedown jParsedown = new JParsedown(); 084 String text = jParsedown.text(description); 085 content.append(text); 086 087 String name = item.getName(); 088 if (name != null) { 089 String filePath = frame.getWorkspace().getTasksMap().getProperty(name); 090 if (filePath != null) { 091 File checkPath; 092 if (StringUtils.startsWith(filePath, "jar:file:") && StringUtils.contains(filePath, '!')) { 093 checkPath = new File(StringUtils.substringBetween(filePath, "jar:file:", "!")); 094 } else { 095 checkPath = new File(filePath); 096 } 097 try { 098 if (checkPath != null && checkPath.exists()) { 099 String filename = FilenameUtils.getBaseName(filePath); 100 if (!StringUtils.startsWith(filename, "#")) { 101 Node object = new EasyParser().load(filePath); 102 103 Node[] descriptions; 104 descriptions = object.getNodes("About/description"); 105 content.append("<h2><a href=\"" + HyperlinkAdapter.TASK_PROTOCOL + item.getName() 106 + "\">" + name + "</a></h2>"); 107 for (Node aboutDescription : descriptions) { 108 String innerXMLText = aboutDescription.getInnerXMLText(); 109 JParsedown pd = new JParsedown(); 110 innerXMLText = pd.text(innerXMLText); 111 if (StringUtils.startsWith(innerXMLText, "<p>")) { 112 innerXMLText = StringUtils.substringAfter(innerXMLText, "<p>"); 113 } 114 content.append(innerXMLText); 115 } 116 } 117 } else { 118 log.error("Recipe: \"" + name + "\" is not found."); 119 } 120 } catch (Exception e1) { 121 e1.printStackTrace(); 122 } 123 } 124 } 125 } 126 127 content.append("</body><html>"); 128 129 JEditorPane menuBox = new EditorPane(); 130 menuBox.setText(content.toString()); 131 132 HyperlinkAdapter hAdapter = new HyperlinkAdapter(frame.getWorkspace()); 133 hAdapter.setWindow(this); 134 menuBox.addHyperlinkListener(hAdapter); 135 136 return new JScrollPane(menuBox); 137 } 138 139 protected void createToolBar() { 140 fCustomizedMenu.setTabPlacement(SwingConstants.TOP); 141 for (CustomizedMenu cmenu : configurableMenu) { 142 JScrollPane createMenuTab = createMenuTab(cmenu); 143 String title = cmenu.getTitle(); 144 fCustomizedMenu.addTab(title, createMenuTab); 145 } 146 } 147 148 protected void createToolBar(Node[] aToolBarNodes) { 149 List<CustomizedMenu> menuList = new ArrayList<>(); 150 151 for (int k = 0; k < aToolBarNodes.length; k++) { 152 CustomizedMenu menu = new CustomizedMenu(); 153 154 Node theNode = aToolBarNodes[k]; 155 String name = theNode.getAttribute("name"); 156 if (name != null) { 157 menu.setTitle(name); 158 } 159 String colorStr = theNode.getAttribute("color"); 160 if (colorStr != null) { 161 Color color; 162 try { 163 color = (Color) Color.class.getDeclaredField(colorStr.toLowerCase()).get(null); 164 menu.setColor(color); 165 } catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException 166 | SecurityException e) { 167 e.printStackTrace(); 168 } 169 } 170 171 for (Node node : theNode) { 172 String tagName = node.getTag(); 173 if ("item".equals(tagName)) { 174 menu.addItem(node); 175 } 176 } 177 178 menuList.add(menu); 179 } 180 181 configurableMenu = menuList; 182 createToolBar(); 183 } 184 185}