📄 componentmenu.java
字号:
} } /** * Class to handle special changes to changes to node fields. */ private class NodeFieldDocumentListener implements DocumentListener { public void changedUpdate(DocumentEvent e) { nodeInfoChanged(); } public void insertUpdate(DocumentEvent e) { nodeInfoChanged(); } public void removeUpdate(DocumentEvent e) { nodeInfoChanged(); } } /** * Method called with one of the lists on the right is clicked. * Remembers the last list clicked (node, arc, or special) so that * the "add" button will take from the right list. * @param list the list selected (0=node, 1=arc, 2=special). */ private void selectList(int list) { lastListSelected = list; switch (list) { case 0: // nodes list listArcs.clearSelection(); listCells.clearSelection(); listSpecials.clearSelection(); break; case 1: // arcs list listNodes.clearSelection(); listCells.clearSelection(); listSpecials.clearSelection(); break; case 2: // cells list listNodes.clearSelection(); listArcs.clearSelection(); listSpecials.clearSelection(); break; case 3: // specials list listNodes.clearSelection(); listArcs.clearSelection(); listCells.clearSelection(); break; } } private void showMenuSize() { menuSize.setText(techName + " Component menu (" + menuWid + " by " + menuHei + ")"); } /** * Method to show details about the selected menu entry. */ private void showSelected() { Object item = (menuArray[menuSelectedY] == null) ? null : menuArray[menuSelectedY][menuSelectedX]; showSelectedObject(item, false); } /** * Method to show details about an object in a menu entry. * @param item the object to show in detail. * @param fromPopup true if this is from a popup list. */ private void showSelectedObject(Object item, boolean fromPopup) { showThisNode(false, null, null); if (!fromPopup) { popupListPane.setViewportView(null); modelPopup.clear(); } if (item instanceof Xml.PrimitiveNode) { Xml.PrimitiveNode np = (Xml.PrimitiveNode)item; if (!fromPopup) selectedMenuName.setText("Node entry: " + np.name); showThisNode(true, null, np); } else if (item instanceof Xml.MenuNodeInst) { Xml.MenuNodeInst ni = (Xml.MenuNodeInst)item; String name = "Node entry: " + ni.protoName; if (!fromPopup) selectedMenuName.setText(name); showThisNode(true, ni, null); } else if (item instanceof Xml.ArcProto) { Xml.ArcProto ap = (Xml.ArcProto)item; if (!fromPopup) selectedMenuName.setText("Arc entry: " + ap.name); } else if (item instanceof JSeparator) { if (!fromPopup) selectedMenuName.setText("Separator"); } else if (item instanceof List && !fromPopup) { selectedMenuName.setText("Popup menu entry:"); popupListPane.setViewportView(listPopup); List nodes = (List)item; for(Object obj : nodes) { if (obj instanceof Xml.PrimitiveNode) modelPopup.addElement(((Xml.PrimitiveNode)obj).name); else if (obj instanceof Xml.MenuNodeInst) modelPopup.addElement(getNodeName((Xml.MenuNodeInst)obj)); else if (obj instanceof Xml.ArcProto) modelPopup.addElement(((Xml.ArcProto)obj).name); else if (obj instanceof JSeparator) modelPopup.addElement("----------"); else modelPopup.addElement(obj); } } else if (item instanceof String) { String s = (String)item; if (s.startsWith("LOADCELL ")) { if (!fromPopup) selectedMenuName.setText("Cell entry: " + s.substring(9)); } else { if (!fromPopup) selectedMenuName.setText("Special entry: " + s); } } else { if (!fromPopup) selectedMenuName.setText("Empty entry"); } } /** * Method called when the user clicks on an entry in a Popup list. */ private void showSelectedPopup() { Object item = (menuArray[menuSelectedY] == null) ? null : menuArray[menuSelectedY][menuSelectedX]; if (item == null) return; if (item instanceof List) { List nodes = (List)item; int index = listPopup.getSelectedIndex(); if (index < 0 || index >= nodes.size()) return; Object obj = nodes.get(index); showSelectedObject(obj, true); } } /** * Method to determine the name of a node (may use its displayed name). * @param ni the node to describe. * @return the name to use for that node. */ private String getNodeName(Xml.MenuNodeInst ni) { return ni.protoName; } private void nodeInfoChanged() { if (changingNodeFields) return; Object item = (menuArray[menuSelectedY] == null) ? null : menuArray[menuSelectedY][menuSelectedX]; if (item == null) return; int index = -1; List nodes = null; if (item instanceof List) { nodes = (List)item; index = listPopup.getSelectedIndex(); if (index < 0 || index >= nodes.size()) return; item = nodes.get(index); } String protoName; if (item instanceof Xml.MenuNodeInst) protoName = ((Xml.MenuNodeInst)item).protoName; else if (item instanceof Xml.PrimitiveNode) protoName = ((Xml.PrimitiveNode)item).name; else return; Xml.MenuNodeInst newItem = new Xml.MenuNodeInst(); newItem.protoName = protoName; newItem.function = PrimitiveNode.Function.findName((String)nodeFunction.getSelectedItem()); newItem.rotation = TextUtils.atoi(nodeAngle.getText()) * 10; newItem.text = nodeName.getText().trim(); String ts = nodeTextSize.getText().trim(); if (ts.length() == 0 && newItem.text.length() > 0) { // force a default text size if none given ts = "6"; } newItem.fontSize = TextUtils.atof(ts); if (index < 0) { menuArray[menuSelectedY][menuSelectedX] = newItem; } else { nodes.set(index, newItem); } menuView.repaint(); changed = true; } /** * Method called when the library popup above the cells list changes. */ private void libraryChanged() { modelCells.clear(); String libName = (String)libraryName.getSelectedItem(); if (libName == null) return; Library lib = Library.findLibrary(libName); if (lib == null) return; for(Iterator<Cell> it = lib.getCells(); it.hasNext(); ) { Cell cell = it.next(); modelCells.addElement(cell.noLibDescribe()); } } /** * Method to show details about the selected node. * @param valid true if this is a valid node (false to dim all detail fields). * @param ni the NodeInst (may be null but still have a valid prototype). * @param np the NodeProto. */ private void showThisNode(boolean valid, Xml.MenuNodeInst ni, Xml.PrimitiveNode np) { changingNodeFields = true; nodeName.setText(""); nodeTextSize.setText(""); nodeAngle.setText(""); nodeFunction.setSelectedIndex(0); if (valid) { nodeAngle.setEnabled(true); nodeAngleLabel.setEnabled(true); nodeFunction.setEnabled(true); nodeFunctionLabel.setEnabled(true); nodeName.setEnabled(true); nodeNameLabel.setEnabled(true); nodeTextSize.setEnabled(true); nodeTextSizeLabel.setEnabled(true); if (ni == null) { nodeAngle.setText("0"); nodeTextSize.setText("4"); if (np != null) nodeFunction.setSelectedItem(np.function.getName()); } else { nodeAngle.setText((ni.rotation / 10) + ""); nodeFunction.setSelectedItem(ni.function.getName()); if (ni.text != null) { nodeName.setText(ni.text); nodeTextSize.setText(TextUtils.formatDouble(ni.fontSize)); } } } else { nodeAngle.setEnabled(false); nodeAngleLabel.setEnabled(false); nodeFunction.setEnabled(false); nodeFunctionLabel.setEnabled(false); nodeName.setEnabled(false); nodeNameLabel.setEnabled(false); nodeTextSize.setEnabled(false); nodeTextSizeLabel.setEnabled(false); } changingNodeFields = false; } /** * Method called when the "add" button is clicked. * Adds an entry to the menu or popup. * @param obj the object to add to the menu entry. */ private void addToMenu(Object obj) { if (menuArray[menuSelectedY] == null) menuArray[menuSelectedY] = new Object[menuWid]; Object item = menuArray[menuSelectedY][menuSelectedX]; if (item == null) { menuArray[menuSelectedY][menuSelectedX] = obj; } else if (item instanceof List) { List popupItems = (List)item; if (!isUniformType(popupItems, obj)) return; popupItems.add(obj); } else { List<Object> newList = new ArrayList<Object>(); newList.add(item); if (!isUniformType(newList, obj)) return; newList.add(obj); menuArray[menuSelectedY][menuSelectedX] = newList; } menuView.repaint(); showSelected(); changed = true; } /** * Method to tell whether a proposed new object fits with an existing list (all nodes, all arcs, etc). * Arcs cannot be added to node lists, nodes cannot be added to arc lists, and special text cannot * be added to any list. * @param list the list to test. * @param newOne the object being added to the list. * @return true if the object fits in the list. */ private boolean isUniformType(List list, Object newOne) { if (newOne instanceof String) { Job.getUserInterface().showErrorMessage("Must remove everything in the menu before adding 'special text'", "Cannot Add"); return false; } for(Object oldOne : list) { if (oldOne instanceof Xml.ArcProto) { if (!(newOne instanceof Xml.ArcProto)) { Job.getUserInterface().showErrorMessage("Existing Arc menu can only have other arcs added to it", "Cannot Add"); return false; } } else if (oldOne instanceof Xml.PrimitiveNode) { if (!(newOne instanceof Xml.PrimitiveNode)) { Job.getUserInterface().showErrorMessage("Existing Primitive Node menu can only have other primitive nodes added to it", "Cannot Add"); return false; } } else if (oldOne instanceof String && ((String)oldOne).startsWith("LOADCELL ")) { if (!(newOne instanceof String && ((String)newOne).startsWith("LOADCELL "))) { Job.getUserInterface().showErrorMessage("Existing Cell menu can only have other cells added to it", "Cannot Add"); return false; } } } return true; } /** * Class to draw the menu.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -