Browse Source

组件复用设计器部分,几个用到的控件

master
xiaoxia 8 years ago
parent
commit
57cf66276b
  1. 192
      designer_form/src/com/fr/design/mainframe/ShareWidgetButton.java
  2. 29
      designer_form/src/com/fr/design/mainframe/ShareWidgetPane.java
  3. 312
      designer_form/src/com/fr/design/mainframe/widget/UITreeComboBox.java

192
designer_form/src/com/fr/design/mainframe/ShareWidgetButton.java

@ -0,0 +1,192 @@
package com.fr.design.mainframe;
import com.fr.base.BaseUtils;
import com.fr.design.designer.creator.XCreator;
import com.fr.design.designer.creator.XCreatorUtils;
import com.fr.design.gui.ilable.UILabel;
import com.fr.design.layout.FRGUIPaneFactory;
import com.fr.form.share.ShareLoader;
import com.fr.form.ui.ElCaseBindInfo;
import com.fr.form.ui.Widget;
import com.fr.general.ComparatorUtils;
import com.fr.stable.StringUtils;
import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.Serializable;
/**
* Coder: zack
* Date: 2016/10/9
* Time: 16:14
*/
public class ShareWidgetButton extends JPanel implements MouseListener, MouseMotionListener, Serializable {
private ElCaseBindInfo bindInfo;
private MouseEvent lastPressEvent;
public ShareWidgetButton(ElCaseBindInfo bindInfo) {
this.bindInfo = bindInfo;
initUI();
this.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
this.addMouseListener(this);
this.addMouseMotionListener(this);
new DragAndDropDragGestureListener(this, DnDConstants.ACTION_COPY_OR_MOVE);
}
private void initUI() {
this.setBackground(Color.WHITE);
this.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
setPreferredSize(new Dimension(110, 70));
setLayout(FRGUIPaneFactory.createBorderLayout());
ImagePanel imagePanel = new ImagePanel((BufferedImage) bindInfo.getCover());
this.add(imagePanel, BorderLayout.NORTH);
JPanel panel = new JPanel();
UILabel label = new UILabel(bindInfo.getName(), SwingConstants.HORIZONTAL);
panel.setBackground(new Color(184, 220, 242));
panel.add(label);
this.add(panel, BorderLayout.SOUTH);
}
private class ImagePanel extends JPanel {
private BufferedImage image;
public ImagePanel(BufferedImage image) {
this.image = image;
this.setPreferredSize(new Dimension(110, 50));
}
@Override
public void paintComponent(Graphics g) {
g.drawImage(image, 0, 0, 110, 70, null);
}
}
public ElCaseBindInfo getBindInfo() {
return bindInfo;
}
public void setBindInfo(ElCaseBindInfo bindInfo) {
this.bindInfo = bindInfo;
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
lastPressEvent = e;
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void mouseDragged(MouseEvent e) {
if (BaseUtils.isAuthorityEditing()) {
return;
}
if (lastPressEvent == null) {
return;
}
Object source = e.getSource();
Widget creatorSource = null;
String shareId = StringUtils.EMPTY;
if (source instanceof ShareWidgetButton) {
ShareWidgetButton no = (ShareWidgetButton) e.getSource();
if (no == null) {
return;
}
shareId = no.getBindInfo().getId();
creatorSource = ShareLoader.getLoader().getElCaseEditorById(shareId);
}
if (creatorSource != null) {
XCreator xCreator = XCreatorUtils.createXCreator(creatorSource);
xCreator.setShareId(shareId);
WidgetToolBarPane.getTarget().startDraggingBean(xCreator);
lastPressEvent = null;
this.setBorder(null);
}
}
@Override
public void mouseMoved(MouseEvent e) {
}
public class DragAndDropDragGestureListener extends DragSourceAdapter implements DragGestureListener {
private DragSource source;
public DragAndDropDragGestureListener(ShareWidgetButton tt, int actions) {
source = new DragSource();
source.createDefaultDragGestureRecognizer(tt, actions, this);
}
public void dragGestureRecognized(DragGestureEvent dge) {
ShareWidgetButton shareWidgetButton = (ShareWidgetButton) dge.getComponent();
if (shareWidgetButton != null) {
Widget widget = ShareLoader.getLoader().getElCaseEditorById(shareWidgetButton.getBindInfo().getId());
DragAndDropTransferable dragAndDropTransferable = new DragAndDropTransferable(widget);
dge.startDrag(DragSource.DefaultCopyDrop, dragAndDropTransferable, this);
}
}
@Override
public void dragEnter(DragSourceDragEvent dragSourceDragEvent) {
}
}
public class DragAndDropTransferable implements Transferable {
private Widget widget;
public DragAndDropTransferable(Widget widget) {
this.widget = widget;
}
DataFlavor[] flavors = {new DataFlavor(Widget.class, "Widget")};
public DataFlavor[] getTransferDataFlavors() {
return flavors;
}
public boolean isDataFlavorSupported(DataFlavor flavor) {
for (DataFlavor df : flavors) {
if (ComparatorUtils.equals(df, flavor)) {
return true;
}
}
return false;
}
public Object getTransferData(DataFlavor df) throws UnsupportedFlavorException, IOException {
return widget;
}
}
}

29
designer_form/src/com/fr/design/mainframe/ShareWidgetPane.java

@ -0,0 +1,29 @@
package com.fr.design.mainframe;
import com.fr.form.ui.ElCaseBindInfo;
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
/**
* Created by xiaxiang on 2016/10/10.
*/
public class ShareWidgetPane extends JPanel {
public ShareWidgetPane(ArrayList<ElCaseBindInfo> elCaseBindInfos) {
this.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));// 设置面板的边框 ,距离上、左、下、右 的距离
int rowCount = (elCaseBindInfos.size() + 1)/2;
this.setLayout(new GridLayout(rowCount, 2, 10, 10));
for (ElCaseBindInfo rbModuleInfo : elCaseBindInfos) {
ShareWidgetButton widgetButton = new ShareWidgetButton(rbModuleInfo);
this.add(widgetButton);
}
if (elCaseBindInfos.size() == 1) {
this.add(new JPanel());
}
}
}

312
designer_form/src/com/fr/design/mainframe/widget/UITreeComboBox.java

@ -0,0 +1,312 @@
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.XCreator;
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; i<tree.getRowCount(); i++)
{
tree.expandRow(i);
}
setPreferredSize(new Dimension(30, 20));
}
/**
* 设置树
* @param tree ComponentTree
*/
public void setTree(ComponentTree tree){
this.tree = tree;
if(tree != null){
this.setSelectedItem(tree.getSelectionPath());
this.setRenderer(new UITreeComboBoxRenderer());
}
this.updateUI();
}
/**
* 取得树
* @return JTree
*/
public ComponentTree getTree(){
return tree;
}
/**
* 设置当前选择的树路径
* @param o Object
*/
public void setSelectedItem(Object o){
tree.setSelectionPath((TreePath)o);
getModel().setSelectedItem(o);
}
public void updateUI(){
ComboBoxUI cui = (ComboBoxUI)UIManager.getUI(this);
if(cui instanceof MetalComboBoxUI){
cui = new MetalJTreeComboBoxUI();
} else if(cui instanceof MotifComboBoxUI){
cui = new MotifJTreeComboBoxUI();
} else {
cui = new WindowsJTreeComboBoxUI();
}
setUI(cui);
}
// UI Inner classes -- one for each supported Look and Feel
class MetalJTreeComboBoxUI extends MetalComboBoxUI{
protected ComboPopup createPopup() {
return new TreePopup(comboBox);
}
}
class WindowsJTreeComboBoxUI extends WindowsComboBoxUI{
protected ComboPopup createPopup() {
return new TreePopup(comboBox);
}
}
class MotifJTreeComboBoxUI extends MotifComboBoxUI{
protected ComboPopup createPopup() {
return new TreePopup(comboBox);
}
}
/**
* <p>Title: UITreeComboBoxRenderer</p>
* <p>Description: 树形结构而来的DefaultListCellRenderer </p>
*/
class UITreeComboBoxRenderer extends DefaultListCellRenderer {
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus){
if(value != null){
TreePath path = (TreePath)value;
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);
}
}
public TreePath[] getSelectedTreePath() {
XCreator[] creators = tree.getDesigner().getSelectionModel().getSelection().getSelectedCreators();
TreePath[] paths = new TreePath[creators.length];
for (int i = 0; i < paths.length; i++) {
paths[i] = tree.buildTreePath(creators[i]);
}
return paths;
}
private class TreeComboBoxDesignerEditAdapter implements DesignerEditListener {
@Override
public void fireCreatorModified(DesignerEvent evt) {
if (evt.getCreatorEventID() == DesignerEvent.CREATOR_SELECTED) {
TreePath[] paths = getSelectedTreePath();
if (paths.length == 1) {
tree.setAndScrollSelectionPath(paths[0]);
} else {
tree.setSelectionPaths(paths);
}
setSelectedItem(paths[0]);
MenuSelectionManager.defaultManager().clearSelectedPath();
} else if(evt.getCreatorEventID() == DesignerEvent.CREATOR_PASTED) {
TreePath[] paths = getSelectedTreePath();
if (paths.length == 1) {
tree.setAndScrollSelectionPath(paths[0]);
} else {
tree.setSelectionPaths(paths);
}
setSelectedItem(paths[0]);
MenuSelectionManager.defaultManager().clearSelectedPath();
} else {
return;
}
}
@Override
public boolean equals(Object o) {
return o.getClass() == this.getClass();
}
}
/**
* 测试
*/
// 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);
// }
}
/**
* <p>Title: UITreeComboBox</p>
* <p>Description: TreePopup</p>
*/
class TreePopup extends JPopupMenu implements ComboPopup{
protected UITreeComboBox comboBox;
protected JScrollPane scrollPane;
protected MouseMotionListener mouseMotionListener;
protected MouseListener mouseListener;
private MouseListener treeSelectListener = new MouseAdapter(){
public void mouseClicked (MouseEvent e){
if (e.isMetaDown()) {
popupMenu(e);
} else {
return;
}
}
};
public void popupMenu(MouseEvent e) {
TreePath path = comboBox.getTree().getSelectionPath();
if (path == null) {
return;
}
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;
setBorder(BorderFactory.createLineBorder(Color.black));
setLayout(new BorderLayout());
setLightWeightPopupEnabled(comboBox.isLightWeightPopupEnabled());
JTree tree = this.comboBox.getTree();
if(tree != null){
scrollPane = new JScrollPane(tree);
scrollPane.setBorder(null);
add(scrollPane, BorderLayout.CENTER);
this.comboBox.addMouseListener(treeSelectListener);
}
}
public void show(){
updatePopup();
show(comboBox, 0, comboBox.getHeight());
comboBox.getTree().requestFocus();
}
public void hide(){
setVisible(false);
comboBox.firePropertyChange("popupVisible", true, false);
}
protected JList list = new JList();
public JList getList(){
return list;
}
public MouseMotionListener getMouseMotionListener(){
if(mouseMotionListener == null){
mouseMotionListener = new MouseMotionAdapter(){};
}
return mouseMotionListener;
}
public KeyListener getKeyListener(){
return null;
}
public void uninstallingUI(){}
/**
* Implementation of ComboPopup.getMouseListener().
*
* @return a <code>MouseListener</code> or null
* @see ComboPopup#getMouseListener
*/
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;
}
if(comboBox.isEditable()){
Component comp = comboBox.getEditor().getEditorComponent();
if((!(comp instanceof JComponent)) ||
((JComponent)comp).isRequestFocusEnabled()){
comp.requestFocus();
}
} else if(comboBox.isRequestFocusEnabled()){
comboBox.requestFocus();
}
togglePopup();
}
}
}
Loading…
Cancel
Save