You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
372 lines
12 KiB
372 lines
12 KiB
package com.fr.design.widget; |
|
|
|
import com.formdev.flatlaf.util.ScaledEmptyBorder; |
|
import com.fr.design.ExtraDesignClassManager; |
|
import com.fr.design.fun.WidgetDesignHandler; |
|
import com.fr.design.gui.core.WidgetOption; |
|
import com.fr.design.gui.frpane.AbstractAttrNoScrollPane; |
|
import com.fr.design.gui.frpane.AttributeChangeListener; |
|
import com.fr.design.gui.icombobox.UIComboBox; |
|
import com.fr.design.gui.icombobox.UIComboBoxRenderer; |
|
import com.fr.design.gui.ilable.UILabel; |
|
import com.fr.design.i18n.Toolkit; |
|
import com.fr.design.mainframe.CellWidgetPropertyPane; |
|
import com.fr.design.mainframe.ElementCasePane; |
|
import com.fr.design.widget.btn.ButtonConstants; |
|
import com.fr.form.ui.Button; |
|
import com.fr.form.ui.NameWidget; |
|
import com.fr.form.ui.Widget; |
|
import com.fr.form.ui.WidgetConfig; |
|
import com.fr.form.ui.WidgetInfoConfig; |
|
import com.fr.general.ComparatorUtils; |
|
import com.fr.stable.ArrayUtils; |
|
import com.fr.stable.AssistUtils; |
|
|
|
import javax.swing.DefaultComboBoxModel; |
|
import javax.swing.JComponent; |
|
import javax.swing.JList; |
|
import javax.swing.JPanel; |
|
import javax.swing.event.PopupMenuEvent; |
|
import javax.swing.event.PopupMenuListener; |
|
import java.awt.Component; |
|
import java.awt.Dimension; |
|
import java.awt.event.ItemEvent; |
|
import java.awt.event.ItemListener; |
|
import java.util.Vector; |
|
|
|
import static com.fine.swing.ui.layout.Layouts.cell; |
|
import static com.fine.swing.ui.layout.Layouts.column; |
|
import static com.fine.swing.ui.layout.Layouts.row; |
|
import static com.fr.design.constants.LayoutConstants.LEFT_WEIGHT; |
|
import static com.fr.design.constants.LayoutConstants.RIGHT_WEIGHT; |
|
|
|
/** |
|
* CellEditorDef Pane. |
|
*/ |
|
public class WidgetPane extends AbstractAttrNoScrollPane implements ItemListener { |
|
|
|
private EditorTypeComboBox editorTypeComboBox; |
|
private CellWidgetCardPane cellEditorCardPane; |
|
private boolean shouldFireSelectedEvent = true; |
|
private JComponent northPane; |
|
|
|
private boolean eastRegion = false; |
|
|
|
public WidgetPane() { |
|
this(null); |
|
} |
|
|
|
/** |
|
* Constructor |
|
*/ |
|
public WidgetPane(ElementCasePane pane) { |
|
this.initComponents(pane); |
|
} |
|
|
|
public WidgetPane(ElementCasePane pane, boolean eastRegion) { |
|
this.eastRegion = eastRegion; |
|
this.initComponents(pane); |
|
} |
|
|
|
|
|
public boolean isShouldFireSelectedEvent(){ |
|
return shouldFireSelectedEvent; |
|
} |
|
|
|
|
|
protected void initComponents(ElementCasePane pane) { |
|
|
|
editorTypeComboBox = new EditorTypeComboBox(pane != null); |
|
editorTypeComboBox.setMaximumRowCount(16); |
|
northPane = initNorthPane(); |
|
northPane.setBorder(new ScaledEmptyBorder(10, 10, 0, 10)); |
|
|
|
editorTypeComboBox.addItemListener(this); |
|
cellEditorCardPane = initWidgetCardPane(pane); |
|
this.addAttributeChangeListener(listener); |
|
|
|
this.add(column( |
|
10, |
|
cell(northPane), |
|
cell(cellEditorCardPane).weight(1) |
|
).getComponent() |
|
); |
|
} |
|
|
|
public JPanel initNorthPane() { |
|
|
|
return column( |
|
row( |
|
cell(new UILabel(com.fr.design.i18n.Toolkit.i18nText("Fine-Design_Report_Select_Widget"))).weight(LEFT_WEIGHT), |
|
cell(editorTypeComboBox).weight(RIGHT_WEIGHT) |
|
) |
|
).getComponent(); |
|
} |
|
|
|
protected CellWidgetCardPane initWidgetCardPane(ElementCasePane pane) { |
|
return eastRegion ? new EastCellWidgetCardPane(pane) : new CellWidgetCardPane(pane); |
|
} |
|
|
|
protected JPanel createContentPane() { |
|
return new JPanel(); |
|
} |
|
|
|
|
|
AttributeChangeListener listener = new AttributeChangeListener() { |
|
@Override |
|
public void attributeChange() { |
|
if(shouldFireSelectedEvent){ |
|
CellWidgetPropertyPane.getInstance().update(); |
|
} |
|
} |
|
}; |
|
|
|
/** |
|
* 状态改变 |
|
* |
|
* @param e 事件对象 |
|
*/ |
|
public void itemStateChanged(ItemEvent e) { |
|
if (e.getStateChange() == ItemEvent.SELECTED) { |
|
Widget oldWidget = update(); |
|
Widget selectedItem = editorTypeComboBox.getCellWidget(); |
|
WidgetDesignHandler handler = ExtraDesignClassManager.getInstance().getSingle(WidgetDesignHandler.XML_TAG); |
|
if (handler != null) { |
|
handler.transferWidgetProperties(oldWidget, selectedItem); |
|
} |
|
if (e.getItem() instanceof Item && ((Item) e.getItem()).getValue() instanceof WidgetConfig) { |
|
populate(selectedItem); |
|
return; |
|
} |
|
if (shouldFireSelectedEvent) { |
|
populateWidgetConfig(selectedItem); |
|
} |
|
} |
|
} |
|
|
|
@Override |
|
public String title4PopupWindow() { |
|
return com.fr.design.i18n.Toolkit.i18nText("Fine-Design_Report_Widget"); |
|
} |
|
|
|
public void populate(Widget widget) { |
|
if (widget == null) { |
|
editorTypeComboBox.setSelectedIndex(-1); |
|
return; |
|
} |
|
// 预定义组件 |
|
if (widget instanceof NameWidget) { |
|
String name = ((NameWidget) widget).getName(); |
|
shouldFireSelectedEvent = false; |
|
editorTypeComboBox.setSelectedItem(new Item(name, name)); |
|
cellEditorCardPane.populate(widget); |
|
shouldFireSelectedEvent = true; |
|
} |
|
// 内置组件 |
|
else { |
|
Class clazz = widget.getClass(); |
|
if (ArrayUtils.contains(ButtonConstants.CLASSES4BUTTON, clazz)) { |
|
clazz = Button.class; |
|
} |
|
shouldFireSelectedEvent = false; |
|
editorTypeComboBox.setSelectedItemByWidgetClass(clazz); |
|
cellEditorCardPane.populate(widget); |
|
shouldFireSelectedEvent = true; |
|
} |
|
removeAttributeChangeListener(); |
|
initAllListeners(); |
|
this.addAttributeChangeListener(listener); |
|
} |
|
|
|
public Widget update() { |
|
return cellEditorCardPane.update(); |
|
} |
|
|
|
protected void populateWidgetConfig(Widget widget) { |
|
this.populate(widget); |
|
} |
|
|
|
|
|
public void registerListener(){ |
|
initAllListeners(); |
|
} |
|
|
|
private static class EditorTypeComboBox extends UIComboBox { |
|
|
|
private Item item = new Item(com.fr.design.i18n.Toolkit.i18nText("Fine-Design_Report_Widget_User_Defined"), |
|
com.fr.design.i18n.Toolkit.i18nText("Fine-Design_Report_Widget_User_Defined")); |
|
|
|
public EditorTypeComboBox(boolean userDefined) { |
|
this.setEditable(false); |
|
this.setModel(new DefaultComboBoxModel(getWidgetsName(userDefined))); |
|
this.setRenderer(new UIComboBoxRenderer() { |
|
public Component getListCellRendererComponent(JList list, |
|
Object value, int index, boolean isSelected, |
|
boolean cellHasFocus) { |
|
if (value == item) { |
|
UILabel label = new UILabel(Toolkit.i18nText("Fine-Design_Report_Widget_User_Defined") |
|
+ " ————"); |
|
label.setEnabled(false); |
|
return label; |
|
} |
|
return super.getListCellRendererComponent(list, value, |
|
index, isSelected, cellHasFocus); |
|
} |
|
}); |
|
this.setPreferredSize(new Dimension(100, 20)); |
|
this.initPopupMenuListener(userDefined); |
|
} |
|
|
|
public void setSelectedItemByWidgetClass(Class clazz) { |
|
WidgetOption[] options = getWidgetOptions(); |
|
for (int i = 0, l = this.getModel().getSize(); i < l; i++) { |
|
Item item = (Item) this.getModel().getElementAt(i); |
|
if (item.getValue() instanceof Integer |
|
&& options[(Integer) item.getValue()].widgetClass() == clazz) { |
|
this.setSelectedItem(item); |
|
} |
|
} |
|
} |
|
|
|
public void setSelectedItem(Object anObject) { |
|
if (anObject == item) { |
|
return; |
|
} |
|
super.setSelectedItem(anObject); |
|
} |
|
|
|
private Vector<Item> getWidgetsName(boolean userDefined) { |
|
|
|
WidgetOption[] reportWidgetInstance = getWidgetOptions(); |
|
Vector<Item> items = new Vector<Item>(); |
|
for (int i = 0, l = reportWidgetInstance.length; i < l; i++) { |
|
items.add(new Item(reportWidgetInstance[i].optionName(), i)); |
|
} |
|
WidgetInfoConfig manager = WidgetInfoConfig.getInstance(); |
|
java.util.Iterator<String> nameIt = manager.getWidgetConfigNameIterator(); |
|
if (userDefined && nameIt.hasNext()) { |
|
items.add(item); |
|
while (nameIt.hasNext()) { |
|
String name = nameIt.next(); |
|
items.add(new Item(name, name)); |
|
} |
|
} |
|
|
|
return items; |
|
} |
|
|
|
public Widget getCellWidget() { |
|
Item item = (Item) this.getSelectedItem(); |
|
if (item.getValue() instanceof Integer) { |
|
return getWidgetOptions()[(Integer) item.getValue()].createWidget(); |
|
} else if (item.getValue() instanceof String) { |
|
return getPredefinedWidget((String) item.getValue()); |
|
} |
|
return null; |
|
} |
|
|
|
//为了保持预定义控件的配置界面不变,返回类型必须为NameWidget |
|
private NameWidget getPredefinedWidget(String name) { |
|
NameWidget nameWidget = new NameWidget(name); |
|
WidgetInfoConfig manager = WidgetInfoConfig.getInstance(); |
|
if (manager.getWidgetConfig(name) != null) { |
|
Widget widget = manager.getWidgetConfig(name).toWidget(); |
|
nameWidget.setWidgetName(widget.getWidgetName()); |
|
nameWidget.setEnabled(widget.isEnabled()); |
|
nameWidget.setVisible(widget.isVisible()); |
|
} |
|
return nameWidget; |
|
} |
|
|
|
private WidgetOption[] getWidgetOptions() { |
|
return ArrayUtils.addAll(WidgetOption.getReportWidgetInstance(), ExtraDesignClassManager.getInstance().getCellWidgetOptions()); |
|
} |
|
|
|
private void initPopupMenuListener(boolean userDefined) { |
|
this.addPopupMenuListener(new PopupMenuListener() { |
|
|
|
@Override |
|
public void popupMenuWillBecomeVisible(PopupMenuEvent e) { |
|
// 下拉刷新下列表 |
|
Vector<Item> widgetsName = getWidgetsName(userDefined); |
|
if (modelChanged(widgetsName)) { |
|
refreshBoxItems(widgetsName); |
|
} |
|
} |
|
|
|
@Override |
|
public void popupMenuWillBecomeInvisible(PopupMenuEvent e) { |
|
|
|
} |
|
|
|
@Override |
|
public void popupMenuCanceled(PopupMenuEvent e) { |
|
|
|
} |
|
}); |
|
} |
|
|
|
private boolean modelChanged(Vector<Item> widgetsName) { |
|
DefaultComboBoxModel model = (DefaultComboBoxModel) getModel(); |
|
int modelSize = model.getSize(); |
|
if (modelSize != widgetsName.size()) { |
|
return true; |
|
} |
|
for (int i = 0; i < modelSize; i++) { |
|
if (!AssistUtils.equals(model.getElementAt(i), widgetsName.elementAt(i))) { |
|
return true; |
|
} |
|
} |
|
return false; |
|
} |
|
} |
|
|
|
/** |
|
* 校验 |
|
* |
|
* @throws Exception 抛出异常 |
|
*/ |
|
public void checkValid() throws Exception { |
|
this.cellEditorCardPane.checkValid(); |
|
} |
|
|
|
public static class Item { |
|
private String name; |
|
private Object value; |
|
|
|
|
|
public Item(String name, Object value) { |
|
this.name = name; |
|
this.value = value; |
|
} |
|
|
|
public Object getValue() { |
|
return value; |
|
} |
|
|
|
public String getName() { |
|
return name; |
|
} |
|
|
|
/** |
|
* 转化成字符串形式 |
|
* |
|
* @return 返回字符串 |
|
*/ |
|
public String toString() { |
|
return name; |
|
} |
|
|
|
@Override |
|
public boolean equals(Object o) { |
|
return o instanceof Item |
|
&& ComparatorUtils.equals(((Item) o).value, value) |
|
&& ComparatorUtils.equals(((Item) o).name, name); |
|
} |
|
} |
|
|
|
public String getIconPath() { |
|
return ""; |
|
} |
|
|
|
}
|
|
|