Browse Source

REPORT-817 条件属性字体颜色没法选择设置

master
vito 8 years ago
parent
commit
1d3dfbebed
  1. 335
      designer_base/src/com/fr/design/style/AbstractPopBox.java

335
designer_base/src/com/fr/design/style/AbstractPopBox.java

@ -9,6 +9,7 @@ import javax.swing.*;
import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener; import javax.swing.event.ChangeListener;
import java.awt.*; import java.awt.*;
import java.awt.event.AWTEventListener;
import java.awt.event.MouseAdapter; import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;
import java.util.ArrayList; import java.util.ArrayList;
@ -21,212 +22,210 @@ import java.util.List;
*/ */
public abstract class AbstractPopBox extends JPanel { public abstract class AbstractPopBox extends JPanel {
protected BackgroundJComponent displayComponent; protected BackgroundJComponent displayComponent;
private JWindow selectPopupWindow; private JWindow selectPopupWindow;
private boolean isWindowEventInit = false; private boolean isWindowEventInit = false;
private static int GAP = 2; private static int GAP = 2;
private static int GAP2 = 20; private static int GAP2 = 20;
private List<ChangeListener> changeListenerList = new ArrayList<ChangeListener>(); private List<ChangeListener> changeListenerList = new ArrayList<ChangeListener>();
MouseAdapter mouseListener = new MouseAdapter() { MouseAdapter mouseListener = new MouseAdapter() {
public void mousePressed(MouseEvent evt) { public void mousePressed(MouseEvent evt) {
showPopupMenu(); showPopupMenu();
} }
public void mouseExited(MouseEvent evt) {
int x= evt.getXOnScreen();//mac和widows的计算结果还不一样 };
int y = evt.getYOnScreen();
if (selectPopupWindow != null) { AWTEventListener awt = new AWTEventListener() {
Rectangle rectangle = selectPopupWindow.getBounds(); public void eventDispatched(AWTEvent event) {
boolean b1 = x < rectangle.x - GAP || x > rectangle.x + rectangle.width + GAP; if (event instanceof MouseEvent) {
boolean b2 = y < rectangle.y - GAP || y >rectangle.y + rectangle.height + GAP; MouseEvent evt = (MouseEvent) event;
if(b1 || b2) { if (evt.getClickCount() > 0) {
hidePopupMenu(); mouseClick(evt);
} }
// if (OperatingSystem.isWindows()) { }
// }
// }else{ };
// Point pp = SwingUtilities.convertPoint(evt.getComponent(), evt.getPoint(), selectPopupWindow.getParent());
// Rectangle rectangle = selectPopupWindow.getBounds(); private void showPopupMenu() {
// if (pp.getY() < rectangle.getY() || pp.getY() > rectangle.getY() + rectangle.getHeight()) { if (selectPopupWindow != null && selectPopupWindow.isVisible()) {
// //hidePopupMenu(); hidePopupMenu();
// } return;
// } }
}
} if (!this.isEnabled()) {
}; return;
}
private void showPopupMenu() { Toolkit.getDefaultToolkit().addAWTEventListener(awt, AWTEvent.MOUSE_EVENT_MASK);
if (selectPopupWindow != null && selectPopupWindow.isVisible()) {
hidePopupMenu(); selectPopupWindow = this.getControlWindow();
return;
} Point convertPoint = new Point(0, 0);
if (!this.isEnabled()) { // e: 将点(0,0)从ColorSelectionPane的坐标系统转换到屏幕坐标.
return; SwingUtilities.convertPointToScreen(convertPoint, this);
} int y = convertPoint.y + this.getSize().height;
int x = convertPoint.x;
selectPopupWindow = this.getControlWindow(); int h = y + selectPopupWindow.getHeight();
int width = x + selectPopupWindow.getWidth();
Point convertPoint = new Point(0, 0); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
if (h > screenSize.height) {
// e: 将点(0,0)从ColorSelectionPane的坐标系统转换到屏幕坐标. y = y - selectPopupWindow.getHeight() - GAP2;// 超过屏幕高度了
SwingUtilities.convertPointToScreen(convertPoint, this); }
int y = convertPoint.y + this.getSize().height;
int x = convertPoint.x; if (width > screenSize.width) {
int h = y + selectPopupWindow.getHeight(); x = screenSize.width - selectPopupWindow.getWidth();
int width = x + selectPopupWindow.getWidth(); }
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); selectPopupWindow.setLocation(x, y);
if (h > screenSize.height) {
y = y - selectPopupWindow.getHeight() - GAP2;// 超过屏幕高度了 selectPopupWindow.setVisible(true);
}
//wei : 为了点击别的地方下拉颜色窗口消失
if(width > screenSize.width) { MouseAdapter parentMouseListener = new MouseAdapter() {
x = screenSize.width - selectPopupWindow.getWidth(); public void mousePressed(MouseEvent evt) {
} mouseClick(evt);
selectPopupWindow.setLocation(x, y); }
};
selectPopupWindow.setVisible(true); if (!this.isWindowEventInit && SwingUtilities.getAncestorOfClass(GradientBackgroundPane.class, this) != null) {
SwingUtilities.getAncestorOfClass(GradientBackgroundPane.class, this).addMouseListener(parentMouseListener);
//wei : 为了点击别的地方下拉颜色窗口消失 this.isWindowEventInit = true;
MouseAdapter parentMouseListener = new MouseAdapter() { }
public void mousePressed(MouseEvent evt) { }
mouseClick(evt);
} private void mouseClick(MouseEvent evt) {
}; int x = evt.getLocationOnScreen().x;
if (!this.isWindowEventInit && SwingUtilities.getAncestorOfClass(GradientBackgroundPane.class, this) != null) { int y = evt.getLocationOnScreen().y;
SwingUtilities.getAncestorOfClass(GradientBackgroundPane.class, this).addMouseListener(parentMouseListener); Rectangle popBoxRect = AbstractPopBox.this.getBounds();
this.isWindowEventInit = true; Rectangle rectangle = this.getControlWindow().getBounds();
}
}
private void mouseClick(MouseEvent evt) {
int x = evt.getLocationOnScreen().x;
int y = evt.getLocationOnScreen().y;
Rectangle rectangle = this.getControlWindow().getBounds();
boolean b1 = x < rectangle.x || x > rectangle.x + rectangle.width; boolean b1 = x < rectangle.x || x > rectangle.x + rectangle.width;
boolean b2 = y < rectangle.y || y > rectangle.y + rectangle.height; boolean b2 = y < rectangle.y - popBoxRect.height || y > rectangle.y + rectangle.height;
if (b1 || b2) { if (b1 || b2) {
this.hidePopupMenu(); this.hidePopupMenu();
} }
} }
protected void hidePopupMenu() { protected void hidePopupMenu() {
if (selectPopupWindow != null) { if (selectPopupWindow != null) {
selectPopupWindow.setVisible(false); selectPopupWindow.setVisible(false);
} }
selectPopupWindow = null; selectPopupWindow = null;
} Toolkit.getDefaultToolkit().removeAWTEventListener(awt);
}
protected JWindow getControlWindow() {
//find parent. protected JWindow getControlWindow() {
if (this.selectPopupWindow == null) { //find parent.
Window parentWindow = SwingUtilities.windowForComponent(this); if (this.selectPopupWindow == null) {
if (parentWindow != null) { Window parentWindow = SwingUtilities.windowForComponent(this);
this.selectPopupWindow = new SelectControlWindow(parentWindow); if (parentWindow != null) {
} this.selectPopupWindow = new SelectControlWindow(parentWindow);
}
selectPopupWindow.addMouseListener(new MouseAdapter() {
public void mouseExited(MouseEvent evt) { selectPopupWindow.addMouseListener(new MouseAdapter() {
int x = evt.getLocationOnScreen().x; public void mouseExited(MouseEvent evt) {
int y = evt.getLocationOnScreen().y; int x = evt.getLocationOnScreen().x;
int y = evt.getLocationOnScreen().y;
if (selectPopupWindow != null) {
double desValue = 2; if (selectPopupWindow != null) {
Rectangle rectangle = selectPopupWindow.getBounds(); double desValue = 2;
Rectangle rectangle = selectPopupWindow.getBounds();
boolean b1 = x < rectangle.x + desValue || x >= rectangle.x + rectangle.width - desValue; boolean b1 = x < rectangle.x + desValue || x >= rectangle.x + rectangle.width - desValue;
boolean b2 = y > rectangle.y + rectangle.height - desValue;//避免了鼠标下移刚进入selectPopupWindow的过程中弹出框隐藏,上移移出后由AbstractPopBox的mouseListener处理 boolean b2 = y > rectangle.y + rectangle.height - desValue;//避免了鼠标下移刚进入selectPopupWindow的过程中弹出框隐藏,上移移出后由AbstractPopBox的mouseListener处理
if (b1 || b2) { if (b1 || b2) {
hidePopupMenu(); hidePopupMenu();
} }
} }
} }
}); });
} }
return selectPopupWindow; return selectPopupWindow;
} }
/** /**
* 添加事件 * 添加事件
*
* @param changeListener 事件 * @param changeListener 事件
*/ */
public void addSelectChangeListener(ChangeListener changeListener) { public void addSelectChangeListener(ChangeListener changeListener) {
this.changeListenerList.add(changeListener); this.changeListenerList.add(changeListener);
} }
/** /**
* 删除事件 * 删除事件
*
* @param changeListener 事件 * @param changeListener 事件
*/ */
public void removeSelectChangeListener(ChangeListener changeListener) { public void removeSelectChangeListener(ChangeListener changeListener) {
this.changeListenerList.remove(changeListener); this.changeListenerList.remove(changeListener);
} }
/** /**
* 响应事件 * 响应事件
*/ */
public void fireChangeListener() { public void fireChangeListener() {
if (!changeListenerList.isEmpty()) { if (!changeListenerList.isEmpty()) {
ChangeEvent evt = new ChangeEvent(this); ChangeEvent evt = new ChangeEvent(this);
for (int i = 0; i < changeListenerList.size(); i++) { for (int i = 0; i < changeListenerList.size(); i++) {
this.changeListenerList.get(i).stateChanged(evt); this.changeListenerList.get(i).stateChanged(evt);
} }
} }
} }
/** /**
* 待说明 * 待说明
*
* @param background 背景 * @param background 背景
*/ */
public void fireDisplayComponent(Background background) { public void fireDisplayComponent(Background background) {
if (displayComponent != null) { if (displayComponent != null) {
displayComponent.setSelfBackground(background); displayComponent.setSelfBackground(background);
} }
fireChangeListener(); fireChangeListener();
this.repaint(); this.repaint();
} }
/** /**
* 初始化弹出框的面板 * 初始化弹出框的面板
*
* @param preWidth 宽度 * @param preWidth 宽度
* @return 弹出面板 * @return 弹出面板
*/ */
public abstract JPanel initWindowPane(double preWidth); public abstract JPanel initWindowPane(double preWidth);
private class SelectControlWindow extends JWindow { private class SelectControlWindow extends JWindow {
private static final long serialVersionUID = -5776589767069105911L; private static final long serialVersionUID = -5776589767069105911L;
public SelectControlWindow(Window paranet) { public SelectControlWindow(Window paranet) {
super(paranet); super(paranet);
this.initComponents(); this.initComponents();
} }
public void initComponents() { public void initComponents() {
JPanel defaultPane = FRGUIPaneFactory.createBorderLayout_S_Pane(); JPanel defaultPane = FRGUIPaneFactory.createBorderLayout_S_Pane();
this.setContentPane(defaultPane); this.setContentPane(defaultPane);
// defaultPane.setBorder(UIManager.getBorder("PopupMenu.border")); // defaultPane.setBorder(UIManager.getBorder("PopupMenu.border"));
if (displayComponent != null) { if (displayComponent != null) {
defaultPane.add(initWindowPane(displayComponent.getPreferredSize().getWidth())); defaultPane.add(initWindowPane(displayComponent.getPreferredSize().getWidth()));
} else { } else {
defaultPane.add(initWindowPane(20)); defaultPane.add(initWindowPane(20));
} }
this.pack(); this.pack();
} }
@Override @Override
public void setVisible(boolean b) { public void setVisible(boolean b) {
super.setVisible(b); super.setVisible(b);
AbstractPopBox.this.repaint(); AbstractPopBox.this.repaint();
} }
} }
protected boolean isPopupVisible() { protected boolean isPopupVisible() {
return selectPopupWindow == null ? false : selectPopupWindow.isVisible(); return selectPopupWindow == null ? false : selectPopupWindow.isVisible();
} }
} }
Loading…
Cancel
Save