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