From 5a77fe8ee6c1ade8cc1e0a4e44b2bd02fe3d4214 Mon Sep 17 00:00:00 2001 From: xiaoxia Date: Wed, 16 Nov 2016 15:58:43 +0800 Subject: [PATCH] rt --- .../fr/design/mainframe/ComponentTree.java | 450 ++++++++++-------- 1 file changed, 249 insertions(+), 201 deletions(-) diff --git a/designer_form/src/com/fr/design/mainframe/ComponentTree.java b/designer_form/src/com/fr/design/mainframe/ComponentTree.java index a3452f602f..ab1f2a980f 100644 --- a/designer_form/src/com/fr/design/mainframe/ComponentTree.java +++ b/designer_form/src/com/fr/design/mainframe/ComponentTree.java @@ -1,255 +1,303 @@ -package com.fr.design.mainframe; - -import java.awt.Component; -import java.awt.event.MouseAdapter; -import java.awt.event.MouseEvent; -import java.util.ArrayList; - -import javax.swing.DropMode; -import javax.swing.JPopupMenu; -import javax.swing.JTree; -import javax.swing.tree.TreePath; -import javax.swing.tree.TreeSelectionModel; - -import com.fr.design.constants.UIConstants; -import com.fr.design.designer.beans.AdapterBus; -import com.fr.design.designer.beans.ComponentAdapter; +package com.fr.design.mainframe.widget; + +/** + * Created by xiaxiang on 2016/9/30. + */ +import java.awt.*; +import java.awt.event.*; +import javax.swing.*; +import javax.swing.plaf.*; +import javax.swing.plaf.basic.*; +import javax.swing.plaf.metal.*; +import javax.swing.tree.*; + +import com.fr.design.designer.beans.*; import com.fr.design.designer.beans.events.DesignerEditListener; import com.fr.design.designer.beans.events.DesignerEvent; -import com.fr.design.designer.creator.*; -import com.fr.design.designer.treeview.ComponentTreeCellRenderer; -import com.fr.design.designer.treeview.ComponentTreeModel; -import com.fr.stable.StringUtils; - -public class ComponentTree extends JTree { - - private FormDesigner designer; - private ComponentTreeModel model; - - public ComponentTree(FormDesigner designer) { - this.designer = designer; - this.setBackground(UIConstants.NORMAL_BACKGROUND); - setRootVisible(true); - setCellRenderer(new ComponentTreeCellRenderer()); - getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION); - this.setDragEnabled(false); - this.setDropMode(DropMode.ON_OR_INSERT); - this.setTransferHandler(new TreeTransferHandler()); - this.refreshTreeRoot(); - addTreeSelectionListener(designer); - -// this.addMouseListener(new MouseAdapter() { -// -// @Override -// public void mouseClicked(MouseEvent e) { -// if (e.isPopupTrigger()) { -// popupMenu(e); -// } -// } -// -// @Override -// public void mousePressed(MouseEvent e) { -// if (e.isPopupTrigger()) { -// popupMenu(e); -// } -// } -// -// @Override -// public void mouseReleased(MouseEvent e) { -// if (e.isPopupTrigger()) { -// popupMenu(e); -// } -// } -// }); - setEditable(true); - } - - public FormDesigner getDesigner() { - return designer; +import com.fr.design.designer.creator.XCreator; +import com.fr.design.gui.icontainer.UIScrollPane; +import com.fr.design.mainframe.ComponentTree; +import com.sun.java.swing.plaf.motif.*; +import com.sun.java.swing.plaf.windows.*; + +/** + * 控件树下拉列表框 + */ +public class UITreeComboBox extends JComboBox{ + /** + * 显示用的树 + */ + private ComponentTree tree; + + public UITreeComboBox(ComponentTree componentTree){ + this.setTree(componentTree); + tree.getDesigner().addDesignerEditListener(new TreeComboBoxDesignerEditAdapter()); +// for(int i=0; iTitle: UITreeComboBoxRenderer

+ *

Description: 树形结构而来的DefaultListCellRenderer

+ */ + class UITreeComboBoxRenderer extends DefaultListCellRenderer { + public Component getListCellRendererComponent(JList list, Object value, + int index, boolean isSelected, boolean cellHasFocus){ + if(tree != null && tree.getSelectedTreePath().length > 0){ + TreePath path = tree.getSelectedTreePath()[0]; + tree.setAndScrollSelectionPath(path); + Object node = path.getLastPathComponent(); + value = node; + TreeCellRenderer r = tree.getCellRenderer(); + JLabel lb = (JLabel)r.getTreeCellRendererComponent( + tree, value, isSelected, false, false, index, + cellHasFocus); + return lb; + } + return super.getListCellRendererComponent(list, value, index, + isSelected, cellHasFocus); + } + } + private class TreeComboBoxDesignerEditAdapter implements DesignerEditListener { + + @Override + public void fireCreatorModified(DesignerEvent evt) { + if (evt.getCreatorEventID() == DesignerEvent.CREATOR_SELECTED) { + TreePath[] paths = tree.getSelectedTreePath(); + if (tree != null && paths.length > 0) { + tree.setAndScrollSelectionPath(paths[0]); + setSelectedItem(paths[0]); + MenuSelectionManager.defaultManager().clearSelectedPath(); + } + + } else if (evt.getCreatorEventID() == DesignerEvent.CREATOR_PASTED) { + tree.refreshUI(); + TreePath[] paths = tree.getSelectedTreePath(); + if (tree != null && paths.length > 0) { + tree.setAndScrollSelectionPath(paths[0]); + setSelectedItem(paths[0]); + MenuSelectionManager.defaultManager().clearSelectedPath(); + } + } else if (evt.getCreatorEventID() == DesignerEvent.CREATOR_CUTED) { + tree.refreshUI(); + setSelectedItem(null); + MenuSelectionManager.defaultManager().clearSelectedPath(); + } else { + tree.refreshUI(); + repaint(); + } - public TreePath[] getSelectedTreePath() { - XCreator[] creators = designer.getSelectionModel().getSelection().getSelectedCreators(); - TreePath[] paths = new TreePath[creators.length]; + } - for (int i = 0; i < paths.length; i++) { - paths[i] = buildTreePath(creators[i]); + @Override + public boolean equals(Object o) { + return o.getClass() == this.getClass(); } - return paths; } + /** + * 测试 + */ +// public static void main(String args[]){ +// JFrame frame = new JFrame("UITreeComboBox"); +// final UITreeComboBox box = new UITreeComboBox(new ComponentTree(new FormDesigner())); +// box.setPreferredSize(new Dimension(300, 28)); +// frame.getContentPane().add(box); +// frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); +// frame.pack(); +// frame.setVisible(true); +// } +} +/** + *

Title: UITreeComboBox

+ *

Description: TreePopup

+ */ +class TreePopup extends JPopupMenu implements ComboPopup{ + protected UITreeComboBox comboBox; + protected JScrollPane scrollPane; + protected MouseMotionListener mouseMotionListener; + protected MouseListener mouseListener; - /** - *搜索指定名称的路径 - * - * @param text 名称 - * @return 树路径 - */ - public TreePath[] search(String text) { - if (StringUtils.isNotEmpty(text)) { - text = text.toLowerCase(); - } - ArrayList searchList = new ArrayList(); - XLayoutContainer root = designer.getRootComponent(); - searchFormRoot(root, searchList, text); - TreePath[] paths = new TreePath[searchList.size()]; - for (int i = 0; i < paths.length; i++) { - paths[i] = buildTreePath(searchList.get(i)); + public void popupMenu(MouseEvent e) { + TreePath path = comboBox.getTree().getSelectionPath(); + if (path == null) { + return; } - if(paths.length > 0) { - setAndScrollSelectionPath(paths[0]); - } else { - setSelectionPath(); + Component component = (Component) path.getLastPathComponent(); + if (!(component instanceof XCreator)) { + return; + } + com.fr.design.designer.beans.ComponentAdapter adapter = AdapterBus.getComponentAdapter(comboBox.getTree().getDesigner(), (XCreator) component); + JPopupMenu menu = adapter.getContextPopupMenu(e); + menu.show(comboBox, e.getX(), e.getY()); + } + + public TreePopup(JComboBox comboBox){ + this.comboBox = (UITreeComboBox)comboBox; + setLayout(new BorderLayout()); + setLightWeightPopupEnabled(comboBox.isLightWeightPopupEnabled()); + JTree tree = this.comboBox.getTree(); + if(tree != null){ + scrollPane = new UIScrollPane(tree); + scrollPane.setBorder(BorderFactory.createEmptyBorder(0,2,0,0)); + add(scrollPane, BorderLayout.CENTER); } - return paths; } + public void show() { + updatePopup(); + show(comboBox, 0, comboBox.getHeight()); + comboBox.getTree().requestFocus(); + } - private void setSelectionPath(){ + public void hide(){ + setVisible(false); + comboBox.firePropertyChange("popupVisible", true, false); + } - /** - * 不让传null参数,所以只有自己定义 - */ - super.setSelectionPath(null); - clearSelection(); + protected JList list = new JList(); + public JList getList(){ + return list; } - private void searchFormRoot(XLayoutContainer container, ArrayList searchList, String text) { - if (StringUtils.isEmpty(text)) { - return; - } - for (int i = 0, size = container.getXCreatorCount(); i < size; i++) { - XCreator creator = container.getXCreator(i); - String xName = creator.toData().getWidgetName(); - if (xName.toLowerCase().contains(text)) { - searchList.add(creator); - } - if (creator instanceof XLayoutContainer) { - searchFormRoot((XLayoutContainer) creator, searchList, text); - } + public MouseMotionListener getMouseMotionListener(){ + if(mouseMotionListener == null){ + mouseMotionListener = new MouseMotionAdapter(){}; } + return mouseMotionListener; } - /** - * 触发 - */ - public void fireTreeChanged() { - designer.refreshDesignerUI(); + public KeyListener getKeyListener(){ + return null; } + public void uninstallingUI(){} + /** - * 刷新 + * Implementation of ComboPopup.getMouseListener(). + * + * @return a MouseListener or null + * @see ComboPopup#getMouseListener */ - public void refreshTreeRoot() { - model = new ComponentTreeModel(designer, designer.getTopContainer()); - setModel(model); - setDragEnabled(false); - setDropMode(DropMode.ON_OR_INSERT); - setTransferHandler(new TreeTransferHandler()); - repaint(); - } - - private TreePath buildTreePath(Component comp) { - ArrayList path = new ArrayList(); - Component parent = comp; - - while (parent != null) { - XCreator creator = (XCreator) parent; - path.add(0, parent); - if (creator != comp ) { - creator.notShowInComponentTree(path); + public MouseListener getMouseListener(){ + if(mouseListener == null){ + mouseListener = new InvocationMouseHandler(); + } + return mouseListener; + } + + protected void togglePopup(){ + if(isVisible()){ + hide(); + } else{ + show(); + } + } + protected void updatePopup(){ + setPreferredSize(new Dimension(comboBox.getSize().width, 200)); + Object selectedObj = comboBox.getSelectedItem(); + if(selectedObj != null){ + TreePath tp = (TreePath)selectedObj; + ((UITreeComboBox)comboBox).getTree().setSelectionPath(tp); + } + } + + protected class InvocationMouseHandler extends MouseAdapter{ + public void mousePressed(MouseEvent e){ + if(!SwingUtilities.isLeftMouseButton(e) || !comboBox.isEnabled()){ + return; } - //绝对布局作为body的时候不显示自适应布局父层 - if (((XCreator) parent).acceptType(XWAbsoluteBodyLayout.class) - && (parent.getParent() != null) - && ((XCreator)parent.getParent()).acceptType(XWFitLayout.class)){ - parent = parent.getParent().getParent(); - continue; + if(comboBox.isEditable()){ + Component comp = comboBox.getEditor().getEditorComponent(); + if((!(comp instanceof JComponent)) || + ((JComponent)comp).isRequestFocusEnabled()){ + comp.requestFocus(); + } + } else if(comboBox.isRequestFocusEnabled()){ + comboBox.requestFocus(); + } + togglePopup(); + } + + public void mouseClicked (MouseEvent e){ + if (e.isMetaDown()) { + popupMenu(e); + } else { + return; } - parent = parent.getParent(); } - Object[] components = path.toArray(); - return new TreePath(components); } -} \ No newline at end of file + + + +}