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 ? p1.getName().compareToIgnoreCase(p2.getName()) : 1);
078                for (CustomizedMenu.Item item : menu) {
079                        String description = item.getDescription();
080                        JParsedown jParsedown = new JParsedown();
081                        String text = jParsedown.text(description);
082                        content.append(text);
083
084                        String name = item.getName();
085                        if (name != null) {
086                                String filePath = frame.getWorkspace().getTasksMap().getProperty(name);
087                                File checkPath;
088                                if (StringUtils.startsWith(filePath, "jar:file:") && StringUtils.contains(filePath, '!')) {
089                                        checkPath = new File(StringUtils.substringBetween(filePath, "jar:file:", "!"));
090                                } else {
091                                        checkPath = new File(filePath);
092                                }
093                                try {
094                                        if (checkPath != null && checkPath.exists()) {
095                                                Node object = new EasyParser().load(filePath);
096
097                                                Node[] descriptions;
098                                                descriptions = object.getNodes("About/description");
099                                                content.append("<h2><a href=\"" + HyperlinkAdapter.TASK_PROTOCOL + item.getName() + "\">" + name
100                                                                + "</a></h2>");
101                                                for (Node aboutDescription : descriptions) {
102                                                        String innerXMLText = aboutDescription.getInnerXMLText();
103                                                        JParsedown pd = new JParsedown();
104                                                        innerXMLText = pd.text(innerXMLText);
105                                                        if (StringUtils.startsWith(innerXMLText, "<p>")) {
106                                                                innerXMLText = StringUtils.substringAfter(innerXMLText, "<p>");
107                                                        }
108                                                        content.append(innerXMLText);
109                                                }
110                                                // content.append("<hr/>");
111                                        } else {
112                                                log.error("Recipe: \"" + name + "\" is not found.");
113                                        }
114                                } catch (Exception e1) {
115                                        e1.printStackTrace();
116                                }
117                        }
118                }
119
120                content.append("</body><html>");
121
122                JEditorPane menuBox = new EditorPane();
123                menuBox.setText(content.toString());
124
125                HyperlinkAdapter hAdapter = new HyperlinkAdapter(frame.getWorkspace());
126                hAdapter.setWindow(this);
127                menuBox.addHyperlinkListener(hAdapter);
128
129                return new JScrollPane(menuBox);
130        }
131
132        protected void createToolBar() {
133                fCustomizedMenu.setTabPlacement(SwingConstants.TOP);
134                for (CustomizedMenu cmenu : configurableMenu) {
135                        JScrollPane createMenuTab = createMenuTab(cmenu);
136                        String title = cmenu.getTitle();
137                        fCustomizedMenu.addTab(title, createMenuTab);
138                }
139        }
140
141        protected void createToolBar(Node[] aToolBarNodes) {
142                List<CustomizedMenu> menuList = new ArrayList<>();
143
144                for (int k = 0; k < aToolBarNodes.length; k++) {
145                        CustomizedMenu menu = new CustomizedMenu();
146
147                        Node theNode = aToolBarNodes[k];
148                        String name = theNode.getAttribute("name");
149                        if (name != null) {
150                                menu.setTitle(name);
151                        }
152                        String colorStr = theNode.getAttribute("color");
153                        if (colorStr != null) {
154                                Color color;
155                                try {
156                                        color = (Color) Color.class.getDeclaredField(colorStr.toLowerCase()).get(null);
157                                        menu.setColor(color);
158                                } catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException
159                                                | SecurityException e) {
160                                        e.printStackTrace();
161                                }
162                        }
163
164                        for (Node node : theNode) {
165                                String tagName = node.getTag();
166                                if ("item".equals(tagName)) {
167                                        menu.addItem(node);
168                                }
169                        }
170
171                        menuList.add(menu);
172                }
173
174                configurableMenu = menuList;
175                createToolBar();
176        }
177
178}