Browse Source

REPORT-10045 重构 UIListControlPane 和 JListControlPane

final/10.0
plough 6 years ago
parent
commit
3b4dad290e
  1. 212
      designer-base/src/main/java/com/fr/design/gui/controlpane/CommonShortCutHandlers.java
  2. 44
      designer-base/src/main/java/com/fr/design/gui/controlpane/JControlPane.java
  3. 2
      designer-base/src/main/java/com/fr/design/gui/controlpane/JControlUpdatePane.java
  4. 284
      designer-base/src/main/java/com/fr/design/gui/controlpane/JListControlPane.java
  5. 71
      designer-base/src/main/java/com/fr/design/gui/controlpane/ListControlPaneHandlers.java
  6. 112
      designer-base/src/main/java/com/fr/design/gui/controlpane/ListControlPaneHelper.java
  7. 25
      designer-base/src/main/java/com/fr/design/gui/controlpane/ListControlPaneProvider.java
  8. 4
      designer-base/src/main/java/com/fr/design/gui/controlpane/ObjectUIControlPane.java
  9. 15
      designer-base/src/main/java/com/fr/design/gui/controlpane/UIControlPane.java
  10. 300
      designer-base/src/main/java/com/fr/design/gui/controlpane/UIListControlPane.java
  11. 1
      designer-base/src/main/java/com/fr/design/gui/controlpane/shortcutfactory/NewShortCutFactory.java
  12. 61
      designer-base/src/main/java/com/fr/design/gui/ilist/JNameEdList.java
  13. 362
      designer-base/src/main/java/com/fr/design/gui/ilist/UINameEdList.java
  14. 68
      designer-base/src/test/java/com/fr/design/gui/ilist/JNameEdListTest.java
  15. 82
      designer-base/src/test/java/com/fr/design/gui/ilist/UINameEdListTest.java
  16. 2
      designer-chart/src/main/java/com/fr/van/chart/custom/component/VanChartShortCutFactory.java

212
designer-base/src/main/java/com/fr/design/gui/controlpane/CommonShortCutHandlers.java

@ -0,0 +1,212 @@
package com.fr.design.gui.controlpane;
import com.fr.base.BaseUtils;
import com.fr.base.FRContext;
import com.fr.design.gui.ilist.JNameEdList;
import com.fr.design.gui.ilist.ListModelElement;
import com.fr.design.utils.gui.GUICoreUtils;
import com.fr.general.ComparatorUtils;
import com.fr.stable.ArrayUtils;
import com.fr.stable.Nameable;
import javax.swing.DefaultListModel;
import javax.swing.SwingUtilities;
import java.awt.Component;
import java.util.Arrays;
import java.util.Comparator;
/**
* 存放一些通用的事件处理方法
* Created by plough on 2018/8/13.
*/
public class CommonShortCutHandlers {
ListControlPaneProvider listControlPane;
JNameEdList nameableList;
private CommonShortCutHandlers(ListControlPaneProvider listControlPane) {
this.listControlPane = listControlPane;
this.nameableList = listControlPane.getNameableList();
}
public static CommonShortCutHandlers newInstance(ListControlPaneProvider listControlPane) {
return new CommonShortCutHandlers(listControlPane);
}
public void onAddItem(NameableCreator creator) {
if (listControlPane.hasInvalid(true)) {
return;
}
Nameable nameable = creator.createNameable(listControlPane);
listControlPane.addNameable(nameable, listControlPane.getModel().getSize());
}
public void onRemoveItem() {
try {
nameableList.getCellEditor()
.stopCellEditing();
} catch (Exception ignored) {
}
if (GUICoreUtils.removeJListSelectedNodes(SwingUtilities
.getWindowAncestor((Component) listControlPane), nameableList)) {
listControlPane.checkButtonEnabled();
}
}
public void onCopyItem() {
// p:选中的值.
ListModelElement selectedValue = (ListModelElement) nameableList.getSelectedValue();
if (selectedValue == null) {
return;
}
listControlPane.getControlUpdatePane().update();
Nameable selectedNameable = selectedValue.wrapper;
// p: 用反射机制实现
try {
Nameable newNameable = (Nameable) BaseUtils.cloneObject(selectedNameable);
newNameable.setName(createUnrepeatedCopyName(selectedNameable.getName()));
listControlPane.addNameable(newNameable, listControlPane.getSelectedIndex() + 1);
} catch (Exception e) {
FRContext.getLogger().error(e.getMessage(), e);
}
}
public void onMoveUpItem() {
int selectedIndex = nameableList.getSelectedIndex();
if (selectedIndex == -1) {
return;
}
// 上移
if (selectedIndex > 0) {
DefaultListModel listModel = (DefaultListModel) nameableList.getModel();
Object selecteObj1 = listModel.get(selectedIndex - 1);
listModel.set(selectedIndex - 1, listModel.get(selectedIndex));
listModel.set(selectedIndex, selecteObj1);
nameableList.setSelectedIndex(selectedIndex - 1);
nameableList.ensureIndexIsVisible(selectedIndex - 1);
}
}
public void onMoveDownItem() {
int selectedIndex = nameableList.getSelectedIndex();
if (selectedIndex == -1) {
return;
}
if (selectedIndex < nameableList.getModel().getSize() - 1) {
DefaultListModel listModel = (DefaultListModel) nameableList
.getModel();
Object selecteObj1 = listModel.get(selectedIndex + 1);
listModel.set(selectedIndex + 1, listModel.get(selectedIndex));
listModel.set(selectedIndex, selecteObj1);
nameableList.setSelectedIndex(selectedIndex + 1);
nameableList.ensureIndexIsVisible(selectedIndex + 1);
}
}
public void onSortItem(boolean isAtoZ) {
// p:选中的值.
Object selectedValue = nameableList.getSelectedValue();
DefaultListModel listModel = (DefaultListModel) nameableList
.getModel();
Nameable[] nameableArray = new Nameable[listModel.getSize()];
if (nameableArray.length <= 0) {
return;
}
for (int i = 0; i < listModel.getSize(); i++) {
nameableArray[i] = ((ListModelElement) listModel.getElementAt(i)).wrapper;
}
// p:排序.
if (isAtoZ) {
Comparator<Nameable> nameableComparator = new Comparator<Nameable>() {
@Override
public int compare(Nameable o1, Nameable o2) {
return -ComparatorUtils.compare(o1.getName(), o2
.getName());
}
};
isAtoZ = !isAtoZ;
Arrays.sort(nameableArray, nameableComparator);
} else {
Comparator<Nameable> nameableComparator = new Comparator<Nameable>() {
@Override
public int compare(Nameable o1, Nameable o2) {
return ComparatorUtils.compare(o1.getName(), o2
.getName());
}
};
isAtoZ = !isAtoZ;
Arrays.sort(nameableArray, nameableComparator);
}
for (int i = 0; i < nameableArray.length; i++) {
listModel.set(i, new ListModelElement(nameableArray[i]));
}
// p:需要选中以前的那个值.
if (selectedValue != null) {
nameableList.setSelectedValue(selectedValue, true);
}
listControlPane.checkButtonEnabled();
// p:需要repaint.
nameableList.repaint();
}
private String createUnrepeatedCopyName(String suffix) {
DefaultListModel model = listControlPane.getModel();
String[] names = new String[model.getSize()];
for (int i = 0; i < model.size(); i++) {
names[i] = ((ListModelElement) model.get(i)).wrapper.getName();
}
String lastName = "CopyOf" + suffix;
while (ArrayUtils.contains(names, lastName)) {
lastName = "CopyOf" + lastName;
}
return lastName;
}
/**
* 生成不重复的名字
*
* @param prefix 名字前缀
* @return 名字
*/
public String createUnrepeatedName(String prefix) {
DefaultListModel model = listControlPane.getModel();
Nameable[] all = new Nameable[model.getSize()];
for (int i = 0; i < model.size(); i++) {
all[i] = ((ListModelElement) model.get(i)).wrapper;
}
// richer:生成的名字从1开始. kunsnat: 添加属性从0开始.
int count = all.length + 1;
while (true) {
String name_test = prefix + count;
boolean repeated = false;
for (int i = 0, len = model.size(); i < len; i++) {
Nameable nameable = all[i];
if (ComparatorUtils.equals(nameable.getName(), name_test)) {
repeated = true;
break;
}
}
if (!repeated) {
return name_test;
}
count++;
}
}
}

44
designer-base/src/main/java/com/fr/design/gui/controlpane/JControlPane.java

@ -1,6 +1,8 @@
package com.fr.design.gui.controlpane;
import com.fr.design.dialog.BasicPane;
import com.fr.design.gui.controlpane.shortcutfactory.AbstractShortCutFactory;
import com.fr.design.gui.controlpane.shortcutfactory.OldShortCutFactory;
import com.fr.design.gui.ilable.UILabel;
import com.fr.design.gui.itoolbar.UIToolbar;
import com.fr.design.layout.FRGUIPaneFactory;
@ -19,6 +21,8 @@ import java.awt.*;
*/
abstract class JControlPane extends BasicPane implements UnrepeatedNameHelper, ShortCutListenerProvider {
private static final int SHORT_WIDTH = 30; //每加一个short Divider位置加30
private static final String SELECT = "SELECT";
private static final String EDIT = "EDIT";
JPanel controlUpdatePane;
ShortCut4JControlPane[] shorts;
@ -48,7 +52,7 @@ abstract class JControlPane extends BasicPane implements UnrepeatedNameHelper, S
*/
public abstract NameableCreator[] createNameableCreators();
ShortCut4JControlPane[] getShorts() {
public ShortCut4JControlPane[] getShorts() {
return shorts;
}
@ -91,6 +95,18 @@ abstract class JControlPane extends BasicPane implements UnrepeatedNameHelper, S
protected void initComponentPane() {
this.setLayout(FRGUIPaneFactory.createBorderLayout());
this.creators = this.createNameableCreators();
initCardPane();
// SplitPane
JSplitPane mainSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true, getLeftPane(), cardPane);
mainSplitPane.setBorder(BorderFactory.createLineBorder(GUICoreUtils.getTitleLineBorderColor()));
mainSplitPane.setOneTouchExpandable(true);
this.add(mainSplitPane, BorderLayout.CENTER);
mainSplitPane.setDividerLocation(getLeftPreferredSize());
this.checkButtonEnabled();
}
protected void initCardPane() {
this.controlUpdatePane = createControlUpdatePane();
// p: edit card layout
@ -99,16 +115,16 @@ abstract class JControlPane extends BasicPane implements UnrepeatedNameHelper, S
cardPane.setLayout(this.cardLayout);
// p:选择的Label
UILabel selectLabel = new UILabel();
cardPane.add(selectLabel, "SELECT");
cardPane.add(controlUpdatePane, "EDIT");
// SplitPane
JSplitPane mainSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true, getLeftPane(), cardPane);
mainSplitPane.setBorder(BorderFactory.createLineBorder(GUICoreUtils.getTitleLineBorderColor()));
mainSplitPane.setOneTouchExpandable(true);
cardPane.add(selectLabel, SELECT);
cardPane.add(controlUpdatePane, EDIT);
}
this.add(mainSplitPane, BorderLayout.CENTER);
mainSplitPane.setDividerLocation(getLeftPreferredSize());
this.checkButtonEnabled();
public void showEditPane() {
this.cardLayout.show(cardPane, EDIT);
}
public void showSelectPane() {
this.cardLayout.show(cardPane, SELECT);
}
protected abstract JPanel createControlUpdatePane();
@ -166,18 +182,10 @@ abstract class JControlPane extends BasicPane implements UnrepeatedNameHelper, S
public void checkButtonEnabled() {
}
void doBeforeRemove() {
}
void doAfterRemove() {
}
public NameableCreator[] creators() {
return creators == null ? new NameableCreator[0] : creators;
}
protected abstract boolean hasInvalid(boolean isAdd);
/**
* 刷新 NameableCreator
*

2
designer-base/src/main/java/com/fr/design/gui/controlpane/JControlUpdatePane.java

@ -51,7 +51,7 @@ class JControlUpdatePane extends JPanel {
}
public void populate() {
ListModelElement el = listControlPane.getSelectedElement();
ListModelElement el = listControlPane.getSelectedValue();
if (el == null) {
return;
}

284
designer-base/src/main/java/com/fr/design/gui/controlpane/JListControlPane.java

@ -1,7 +1,5 @@
package com.fr.design.gui.controlpane;
import com.fr.base.BaseUtils;
import com.fr.base.FRContext;
import com.fr.design.beans.BasicBeanPane;
import com.fr.design.gui.icontainer.UIScrollPane;
import com.fr.design.gui.ilist.JNameEdList;
@ -18,7 +16,6 @@ import com.fr.stable.core.PropertyChangeAdapter;
import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.ListSelectionModel;
@ -30,8 +27,6 @@ import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Arrays;
import java.util.Comparator;
public abstract class JListControlPane extends JControlPane implements ListControlPaneProvider {
private static final String LIST_NAME = "JControl_List";
@ -40,6 +35,8 @@ public abstract class JListControlPane extends JControlPane implements ListContr
protected int editingIndex;
protected String selectedName;
private boolean isNameRepeated = false;
private CommonShortCutHandlers commonHandlers;
private ListControlPaneHelper helper;
public JListControlPane() {
this.initComponentPane();
@ -49,6 +46,20 @@ public abstract class JListControlPane extends JControlPane implements ListContr
protected JPanel createControlUpdatePane() {
return JControlUpdatePane.newInstance(this);
}
private ListControlPaneHelper getHelper() {
if (helper == null) {
helper = ListControlPaneHelper.newInstance(this);
}
return helper;
}
private CommonShortCutHandlers getCommonHandlers() {
if (commonHandlers == null) {
commonHandlers = CommonShortCutHandlers.newInstance(this);
}
return commonHandlers;
}
/**
* 生成添加按钮的NameableCreator
@ -65,7 +76,6 @@ public abstract class JListControlPane extends JControlPane implements ListContr
nameableList.setName(LIST_NAME);
leftPane.add(new UIScrollPane(nameableList), BorderLayout.CENTER);
nameableList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
nameableList.addMouseListener(listMouseListener);
nameableList.addListSelectionListener(new ListSelectionListener() {
@ -101,14 +111,7 @@ public abstract class JListControlPane extends JControlPane implements ListContr
@Override
public Nameable[] update() {
java.util.List<Nameable> res = new java.util.ArrayList<Nameable>();
((JControlUpdatePane) this.controlUpdatePane).update();
DefaultListModel listModel = (DefaultListModel) this.nameableList.getModel();
for (int i = 0, len = listModel.getSize(); i < len; i++) {
res.add(((ListModelElement) listModel.getElementAt(i)).wrapper);
}
return res.toArray(new Nameable[res.size()]);
return getHelper().update();
}
@Override
@ -184,9 +187,7 @@ public abstract class JListControlPane extends JControlPane implements ListContr
* 获取选中的名字
*/
public String getSelectedName() {
ListModelElement el = (ListModelElement) this.nameableList.getSelectedValue();
return el == null ? null : el.wrapper.getName();
return getHelper().getSelectedName();
}
protected boolean isNameRepeated(java.util.List[] list, String name) {
@ -215,16 +216,8 @@ public abstract class JListControlPane extends JControlPane implements ListContr
* @param nameable 添加的Nameable
* @param index 序号
*/
private void addNameable(Nameable nameable, int index) {
JNameEdList nameEdList = JListControlPane.this.nameableList;
DefaultListModel model = (DefaultListModel) nameEdList.getModel();
ListModelElement el = new ListModelElement(nameable);
model.add(index, el);
nameableList.setSelectedIndex(index);
nameableList.ensureIndexIsVisible(index);
nameEdList.repaint();
public void addNameable(Nameable nameable, int index) {
getHelper().addNameable(nameable, index);
}
/**
@ -243,199 +236,49 @@ public abstract class JListControlPane extends JControlPane implements ListContr
return false;
}
protected DefaultListModel getModel() {
public DefaultListModel getModel() {
return (DefaultListModel) JListControlPane.this.nameableList.getModel();
}
private String createUnrepeatedCopyName(String suffix) {
DefaultListModel model = this.getModel();
String[] names = new String[model.getSize()];
for (int i = 0; i < model.size(); i++) {
names[i] = ((ListModelElement) model.get(i)).wrapper.getName();
}
String lastName = "CopyOf" + suffix;
while (ArrayUtils.contains(names, lastName)) {
lastName = "CopyOf" + lastName;
}
return lastName;
}
/**
* 生成不重复的名字
*
* @param prefix 名字前缀
* @return 名字
*/
* 生成不重复的名字
*
* @param prefix 名字前缀
* @return 名字
*/
@Override
public String createUnrepeatedName(String prefix) {
DefaultListModel model = this.getModel();
Nameable[] all = new Nameable[model.getSize()];
for (int i = 0; i < model.size(); i++) {
all[i] = ((ListModelElement) model.get(i)).wrapper;
}
// richer:生成的名字从1开始. kunsnat: 添加属性从0开始.
int count = all.length + 1;
while (true) {
String name_test = prefix + count;
boolean repeated = false;
for (int i = 0, len = model.size(); i < len; i++) {
Nameable nameable = all[i];
if (ComparatorUtils.equals(nameable.getName(), name_test)) {
repeated = true;
break;
}
}
if (!repeated) {
return name_test;
}
count++;
}
return getCommonHandlers().createUnrepeatedName(prefix);
}
@Override
public void onAddItem(NameableCreator creator) {
if (hasInvalid(true)) {
return;
}
Nameable nameable = creator.createNameable(JListControlPane.this);
JListControlPane.this.addNameable(nameable, getModel().getSize());
getCommonHandlers().onAddItem(creator);
}
@Override
public void onRemoveItem() {
try {
JListControlPane.this.nameableList.getCellEditor()
.stopCellEditing();
} catch (Exception ignored) {
}
// bug:在选中一个NameObject并删除,会遗留下Name.
doBeforeRemove();
if (GUICoreUtils.removeJListSelectedNodes(SwingUtilities
.getWindowAncestor(JListControlPane.this), nameableList)) {
checkButtonEnabled();
doAfterRemove();
}
getCommonHandlers().onRemoveItem();
}
@Override
public void onCopyItem() {
// p:选中的值.
ListModelElement selectedValue = (ListModelElement) nameableList.getSelectedValue();
if (selectedValue == null) {
return;
}
((JControlUpdatePane) controlUpdatePane).update();
Nameable selectedNameable = selectedValue.wrapper;
// p: 用反射机制实现
try {
Nameable newNameable = (Nameable) BaseUtils.cloneObject(selectedNameable);
newNameable.setName(createUnrepeatedCopyName(selectedNameable.getName()));
JListControlPane.this.addNameable(newNameable, nameableList.getSelectedIndex() + 1);
} catch (Exception e) {
FRContext.getLogger().error(e.getMessage(), e);
}
getCommonHandlers().onCopyItem();
}
@Override
public void onMoveUpItem() {
int selectedIndex = nameableList.getSelectedIndex();
if (selectedIndex == -1) {
return;
}
// 上移
if (selectedIndex > 0) {
DefaultListModel listModel = (DefaultListModel) nameableList
.getModel();
Object selecteObj1 = listModel.get(selectedIndex - 1);
listModel.set(selectedIndex - 1, listModel.get(selectedIndex));
listModel.set(selectedIndex, selecteObj1);
nameableList.setSelectedIndex(selectedIndex - 1);
nameableList.ensureIndexIsVisible(selectedIndex - 1);
}
getCommonHandlers().onMoveUpItem();
}
@Override
public void onMoveDownItem() {
int selectedIndex = nameableList.getSelectedIndex();
if (selectedIndex == -1) {
return;
}
if (selectedIndex < nameableList.getModel().getSize() - 1) {
DefaultListModel listModel = (DefaultListModel) nameableList
.getModel();
Object selecteObj1 = listModel.get(selectedIndex + 1);
listModel.set(selectedIndex + 1, listModel.get(selectedIndex));
listModel.set(selectedIndex, selecteObj1);
nameableList.setSelectedIndex(selectedIndex + 1);
nameableList.ensureIndexIsVisible(selectedIndex + 1);
}
getCommonHandlers().onMoveDownItem();
}
@Override
public void onSortItem(boolean isAtoZ) {
// p:选中的值.
Object selectedValue = nameableList.getSelectedValue();
DefaultListModel listModel = (DefaultListModel) nameableList
.getModel();
Nameable[] nameableArray = new Nameable[listModel.getSize()];
if (nameableArray.length <= 0) {
return;
}
for (int i = 0; i < listModel.getSize(); i++) {
nameableArray[i] = ((ListModelElement) listModel.getElementAt(i)).wrapper;
}
// p:排序.
if (isAtoZ) {
Comparator<Nameable> nameableComparator = new Comparator<Nameable>() {
@Override
public int compare(Nameable o1, Nameable o2) {
return -ComparatorUtils.compare(o1.getName(), o2
.getName());
}
};
isAtoZ = !isAtoZ;
Arrays.sort(nameableArray, nameableComparator);
} else {
Comparator<Nameable> nameableComparator = new Comparator<Nameable>() {
@Override
public int compare(Nameable o1, Nameable o2) {
return ComparatorUtils.compare(o1.getName(), o2
.getName());
}
};
isAtoZ = !isAtoZ;
Arrays.sort(nameableArray, nameableComparator);
}
for (int i = 0; i < nameableArray.length; i++) {
listModel.set(i, new ListModelElement(nameableArray[i]));
}
// p:需要选中以前的那个值.
if (selectedValue != null) {
nameableList.setSelectedValue(selectedValue, true);
}
checkButtonEnabled();
// p:需要repaint.
nameableList.repaint();
getCommonHandlers().onSortItem(isAtoZ);
}
@Override
@ -487,16 +330,7 @@ public abstract class JListControlPane extends JControlPane implements ListContr
*/
@Override
public void checkButtonEnabled() {
int selectedIndex = nameableList.getSelectedIndex();
if (selectedIndex == -1) {
this.cardLayout.show(cardPane, "SELECT");
} else {
this.cardLayout.show(cardPane, "EDIT");
}
for (ShortCut4JControlPane sj : getShorts()) {
sj.checkEnable();
}
getHelper().checkButtonEnabled();
}
private class NameableListCellRenderer extends
@ -548,35 +382,9 @@ public abstract class JListControlPane extends JControlPane implements ListContr
((JControlUpdatePane) this.controlUpdatePane).checkValid();
}
private int getInValidIndex() {
BasicBeanPane[] p = ((JControlUpdatePane) controlUpdatePane).getUpdatePanes();
if (p != null) {
for (int i = 0; i < p.length; i++) {
if (p[i] != null) {
try {
p[i].checkValid();
} catch (Exception e) {
return i;
}
}
}
}
return -1;
}
@Override
protected boolean hasInvalid(boolean isAdd) {
int idx = JListControlPane.this.getInValidIndex();
if (isAdd || nameableList.getSelectedIndex() != idx) {
try {
checkValid();
} catch (Exception exp) {
JOptionPane.showMessageDialog(JListControlPane.this, exp.getMessage());
nameableList.setSelectedIndex(idx);
return true;
}
}
return false;
public boolean hasInvalid(boolean isAdd) {
return getHelper().hasInvalid(isAdd);
}
/**
@ -584,12 +392,28 @@ public abstract class JListControlPane extends JControlPane implements ListContr
*
* @param index 选中项的序列号
*/
@Override
public void setSelectedIndex(int index) {
nameableList.setSelectedIndex(index);
}
@Override
public ListModelElement getSelectedElement() {
return (ListModelElement) JListControlPane.this.nameableList.getSelectedValue();
public int getSelectedIndex() {
return nameableList.getSelectedIndex();
}
@Override
public JNameEdList getNameableList() {
return nameableList;
}
@Override
public ListModelElement getSelectedValue() {
return (ListModelElement) this.nameableList.getSelectedValue();
}
@Override
public JControlUpdatePane getControlUpdatePane() {
return (JControlUpdatePane) controlUpdatePane;
}
}

71
designer-base/src/main/java/com/fr/design/gui/controlpane/ListControlPaneHandlers.java

@ -1,71 +0,0 @@
package com.fr.design.gui.controlpane;
import com.fr.design.gui.ilist.ListModelElement;
import com.fr.general.ComparatorUtils;
import com.fr.stable.ArrayUtils;
import com.fr.stable.Nameable;
import javax.swing.DefaultListModel;
/**
* Created by plough on 2018/8/13.
*/
public class ListControlPaneHandlers {
ListControlPaneProvider listControlPane;
private ListControlPaneHandlers(ListControlPaneProvider listControlPane) {
this.listControlPane = listControlPane;
}
public ListControlPaneHandlers newInstance(ListControlPaneProvider listControlPane) {
return new ListControlPaneHandlers(listControlPane);
}
private String createUnrepeatedCopyName(String suffix) {
DefaultListModel model = listControlPane.getModel();
String[] names = new String[model.getSize()];
for (int i = 0; i < model.size(); i++) {
names[i] = ((ListModelElement) model.get(i)).wrapper.getName();
}
String lastName = "CopyOf" + suffix;
while (ArrayUtils.contains(names, lastName)) {
lastName = "CopyOf" + lastName;
}
return lastName;
}
/**
* 生成不重复的名字
*
* @param prefix 名字前缀
* @return 名字
*/
public String createUnrepeatedName(String prefix) {
DefaultListModel model = listControlPane.getModel();
Nameable[] all = new Nameable[model.getSize()];
for (int i = 0; i < model.size(); i++) {
all[i] = ((ListModelElement) model.get(i)).wrapper;
}
// richer:生成的名字从1开始. kunsnat: 添加属性从0开始.
int count = all.length + 1;
while (true) {
String name_test = prefix + count;
boolean repeated = false;
for (int i = 0, len = model.size(); i < len; i++) {
Nameable nameable = all[i];
if (ComparatorUtils.equals(nameable.getName(), name_test)) {
repeated = true;
break;
}
}
if (!repeated) {
return name_test;
}
count++;
}
}
}

112
designer-base/src/main/java/com/fr/design/gui/controlpane/ListControlPaneHelper.java

@ -0,0 +1,112 @@
package com.fr.design.gui.controlpane;
import com.fr.design.beans.BasicBeanPane;
import com.fr.design.gui.ilist.JNameEdList;
import com.fr.design.gui.ilist.ListModelElement;
import com.fr.stable.Nameable;
import com.fr.stable.StringUtils;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import java.awt.Component;
/**
* 存放一些公用的方法
* Created by plough on 2018/8/13.
*/
class ListControlPaneHelper {
private ListControlPaneProvider listControlPane;
private ListControlPaneHelper(ListControlPaneProvider listControlPane) {
this.listControlPane = listControlPane;
}
public static ListControlPaneHelper newInstance(ListControlPaneProvider listControlPane) {
return new ListControlPaneHelper(listControlPane);
}
public boolean hasInvalid(boolean isAdd) {
int idx = getInValidIndex();
if (isAdd || listControlPane.getSelectedIndex() != idx) {
try {
listControlPane.checkValid();
} catch (Exception exp) {
JOptionPane.showMessageDialog((Component) listControlPane, exp.getMessage());
listControlPane.setSelectedIndex(idx);
return true;
}
}
return false;
}
private int getInValidIndex() {
BasicBeanPane[] p = listControlPane.getControlUpdatePane().getUpdatePanes();
if (p != null) {
for (int i = 0; i < p.length; i++) {
if (p[i] != null) {
try {
p[i].checkValid();
} catch (Exception e) {
return i;
}
}
}
}
return -1;
}
public Nameable[] update() {
java.util.List<Nameable> res = new java.util.ArrayList<Nameable>();
listControlPane.getControlUpdatePane().update();
DefaultListModel listModel = listControlPane.getModel();
for (int i = 0, len = listModel.getSize(); i < len; i++) {
res.add(((ListModelElement) listModel.getElementAt(i)).wrapper);
}
return res.toArray(new Nameable[res.size()]);
}
/**
* 获取选中的名字
*/
public String getSelectedName() {
ListModelElement el = listControlPane.getSelectedValue();
return el == null ? StringUtils.EMPTY : el.wrapper.getName();
}
/**
* 添加 Nameable
*
* @param nameable 添加的Nameable
* @param index 序号
*/
public void addNameable(Nameable nameable, int index) {
JNameEdList nameableList = listControlPane.getNameableList();
DefaultListModel model = listControlPane.getModel();
ListModelElement el = new ListModelElement(nameable);
model.add(index, el);
nameableList.setSelectedIndex(index);
nameableList.ensureIndexIsVisible(index);
nameableList.repaint();
}
/**
* 检查按钮可用状态 Check button enabled.
*/
public void checkButtonEnabled() {
int selectedIndex = listControlPane.getSelectedIndex();
if (selectedIndex == -1) {
listControlPane.showSelectPane();
} else {
listControlPane.showEditPane();
}
for (ShortCut4JControlPane sj : listControlPane.getShorts()) {
sj.checkEnable();
}
}
}

25
designer-base/src/main/java/com/fr/design/gui/controlpane/ListControlPaneProvider.java

@ -1,14 +1,35 @@
package com.fr.design.gui.controlpane;
import com.fr.design.beans.BasicBeanPane;
import com.fr.design.gui.ilist.JNameEdList;
import com.fr.design.gui.ilist.ListModelElement;
import com.fr.stable.Nameable;
import javax.swing.DefaultListModel;
/**
* Created by plough on 2018/8/13.
*/
public interface ListControlPaneProvider {
public interface ListControlPaneProvider extends UnrepeatedNameHelper {
NameableCreator[] creators();
ListModelElement getSelectedElement();
BasicBeanPane createPaneByCreators(NameableCreator creator);
BasicBeanPane createPaneByCreators(NameableCreator creator, String string);
DefaultListModel getModel();
boolean hasInvalid(boolean isAdd);
void addNameable(Nameable nameable, int index);
JNameEdList getNameableList();
int getSelectedIndex();
void setSelectedIndex(int idx);
ListModelElement getSelectedValue();
void checkButtonEnabled();
JControlUpdatePane getControlUpdatePane();
// BasicBeanPane[] getUpdatePanes();
/**
* 检查是否符合规范
* @throws Exception
*/
void checkValid() throws Exception;
void showSelectPane();
void showEditPane();
ShortCut4JControlPane[] getShorts();
}

4
designer-base/src/main/java/com/fr/design/gui/controlpane/ObjectUIControlPane.java

@ -10,10 +10,6 @@ import com.fr.design.beans.BasicBeanPane;
public abstract class ObjectUIControlPane extends UIListControlPane {
private Object object;
public ObjectUIControlPane() {
this(null);
}
public ObjectUIControlPane(Object object) {
super();
this.object = object;

15
designer-base/src/main/java/com/fr/design/gui/controlpane/UIControlPane.java

@ -2,6 +2,7 @@ package com.fr.design.gui.controlpane;
import com.fr.base.chart.BasePlot;
import com.fr.design.constants.UIConstants;
import com.fr.design.gui.controlpane.shortcutfactory.NewShortCutFactory;
import com.fr.design.gui.ilable.UILabel;
import com.fr.design.gui.itoolbar.UIToolBarUI;
import com.fr.design.gui.itoolbar.UIToolbar;
@ -22,7 +23,6 @@ import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.border.EmptyBorder;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Cursor;
@ -78,16 +78,9 @@ abstract class UIControlPane extends JControlPane {
protected void initComponentPane() {
this.setLayout(FRGUIPaneFactory.createBorderLayout());
this.creators = this.createNameableCreators();
this.controlUpdatePane = createControlUpdatePane();
// p: edit card layout
this.cardLayout = new CardLayout();
cardPane = FRGUIPaneFactory.createCardLayout_S_Pane();
cardPane.setLayout(this.cardLayout);
// p:选择的Label
UILabel selectLabel = new UILabel();
cardPane.add(selectLabel, "SELECT");
cardPane.add(controlUpdatePane, "EDIT");
initCardPane();
if (isNewStyle()) {
getPopupEditDialog(cardPane);
this.add(getLeftPane(), BorderLayout.CENTER);

300
designer-base/src/main/java/com/fr/design/gui/controlpane/UIListControlPane.java

@ -1,22 +1,19 @@
package com.fr.design.gui.controlpane;
import com.fr.base.BaseUtils;
import com.fr.base.FRContext;
import com.fr.base.chart.BasePlot;
import com.fr.design.beans.BasicBeanPane;
import com.fr.design.constants.UIConstants;
import com.fr.design.gui.icontainer.UIScrollPane;
import com.fr.design.gui.ilist.JNameEdList;
import com.fr.design.gui.ilist.ListModelElement;
import com.fr.design.gui.ilist.UINameEdList;
import com.fr.design.mainframe.DesignerContext;
import com.fr.design.utils.gui.GUICoreUtils;
import com.fr.general.ComparatorUtils;
import com.fr.stable.ArrayUtils;
import com.fr.stable.Nameable;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.ListSelectionModel;
@ -36,32 +33,46 @@ import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.Comparator;
/**
* Created by plough on 2017/7/19.
*/
public abstract class UIListControlPane extends UIControlPane implements ListControlPaneProvider {
public static final String LIST_NAME = "UIControl_List";
private static final String LIST_NAME = "UIControl_List";
private static final int EDIT_RANGE = 25; // 编辑按钮的x坐标范围
protected UINameEdList nameableList;
protected int editingIndex;
private int editingIndex;
protected String selectedName;
private boolean isNameRepeated = false;
protected boolean isPopulating = false;
private CommonShortCutHandlers commonHandlers;
private ListControlPaneHelper helper;
public UIListControlPane() {
this.initComponentPane();
super();
}
public UIListControlPane(BasePlot plot) {
super(plot);
}
private ListControlPaneHelper getHelper() {
if (helper == null) {
helper = ListControlPaneHelper.newInstance(this);
}
return helper;
}
private CommonShortCutHandlers getCommonHandlers() {
if (commonHandlers == null) {
commonHandlers = CommonShortCutHandlers.newInstance(this);
}
return commonHandlers;
}
@Override
protected JPanel createControlUpdatePane() {
return JControlUpdatePane.newInstance(this);
@ -120,7 +131,7 @@ public abstract class UIListControlPane extends UIControlPane implements ListCon
}
}
public UINameEdList createJNameList() {
private UINameEdList createJNameList() {
UINameEdList nameEdList = new UINameEdList(new DefaultListModel()) {
@Override
protected void doAfterLostFocus() {
@ -135,24 +146,17 @@ public abstract class UIListControlPane extends UIControlPane implements ListCon
return nameEdList;
}
public void updateControlUpdatePane() {
private void updateControlUpdatePane() {
((JControlUpdatePane) controlUpdatePane).update();
}
public void setNameListEditable(boolean editable) {
protected void setNameListEditable(boolean editable) {
this.nameableList.setEditable(editable);
}
@Override
public Nameable[] update() {
java.util.List<Nameable> res = new java.util.ArrayList<Nameable>();
((JControlUpdatePane) this.controlUpdatePane).update();
DefaultListModel listModel = (DefaultListModel) this.nameableList.getModel();
for (int i = 0, len = listModel.getSize(); i < len; i++) {
res.add(((ListModelElement) listModel.getElementAt(i)).wrapper);
}
return res.toArray(new Nameable[res.size()]);
return getHelper().update();
}
@Override
@ -184,9 +188,7 @@ public abstract class UIListControlPane extends UIControlPane implements ListCon
* 获取选中的名字
*/
public String getSelectedName() {
ListModelElement el = (ListModelElement) this.nameableList.getSelectedValue();
return el == null ? null : el.wrapper.getName();
return getHelper().getSelectedName();
}
/**
@ -196,70 +198,14 @@ public abstract class UIListControlPane extends UIControlPane implements ListCon
* @param index 序号
*/
public void addNameable(Nameable nameable, int index) {
UINameEdList nameEdList = UIListControlPane.this.nameableList;
DefaultListModel model = (DefaultListModel) nameEdList.getModel();
ListModelElement el = new ListModelElement(nameable);
model.add(index, el);
nameableList.setSelectedIndex(index);
nameableList.ensureIndexIsVisible(index);
nameEdList.repaint();
getHelper().addNameable(nameable, index);
popupEditDialog();
}
protected DefaultListModel getModel() {
public DefaultListModel getModel() {
return (DefaultListModel) UIListControlPane.this.nameableList.getModel();
}
private String createUnrepeatedCopyName(String suffix) {
DefaultListModel model = this.getModel();
String[] names = new String[model.getSize()];
for (int i = 0; i < model.size(); i++) {
names[i] = ((ListModelElement) model.get(i)).wrapper.getName();
}
String lastName = "CopyOf" + suffix;
while (ArrayUtils.contains(names, lastName)) {
lastName = "CopyOf" + lastName;
}
return lastName;
}
/**
* 生成不重复的名字
*
* @param prefix 名字前缀
* @return 名字
*/
@Override
public String createUnrepeatedName(String prefix) {
DefaultListModel model = this.getModel();
Nameable[] all = new Nameable[model.getSize()];
for (int i = 0; i < model.size(); i++) {
all[i] = ((ListModelElement) model.get(i)).wrapper;
}
// richer:生成的名字从1开始. kunsnat: 添加属性从0开始.
int count = all.length + 1;
while (true) {
String name_test = prefix + count;
boolean repeated = false;
for (int i = 0, len = model.size(); i < len; i++) {
Nameable nameable = all[i];
if (ComparatorUtils.equals(nameable.getName(), name_test)) {
repeated = true;
break;
}
}
if (!repeated) {
return name_test;
}
count++;
}
}
private void popupEditDialog() {
popupEditDialog(null);
}
@ -310,143 +256,45 @@ public abstract class UIListControlPane extends UIControlPane implements ListCon
return resultPos;
}
/**
* 生成不重复的名字
*
* @param prefix 名字前缀
* @return 名字
*/
@Override
public String createUnrepeatedName(String prefix) {
return getCommonHandlers().createUnrepeatedName(prefix);
}
@Override
public void onAddItem(NameableCreator creator) {
Nameable nameable = creator.createNameable(this);
this.addNameable(nameable, getModel().getSize());
getCommonHandlers().onAddItem(creator);
}
@Override
public void onRemoveItem() {
try {
this.nameableList.getCellEditor().stopCellEditing();
} catch (Exception ignored) {
// do nothing
}
// bug:在选中一个NameObject并删除,会遗留下Name.
doBeforeRemove();
if (GUICoreUtils.removeJListSelectedNodes(SwingUtilities
.getWindowAncestor(this), nameableList)) {
checkButtonEnabled();
doAfterRemove();
}
getCommonHandlers().onRemoveItem();
}
@Override
public void onCopyItem() {
// p:选中的值.
ListModelElement selectedValue = (ListModelElement) nameableList.getSelectedValue();
if (selectedValue == null) {
return;
}
((JControlUpdatePane) controlUpdatePane).update();
Nameable selectedNameable = selectedValue.wrapper;
// p: 用反射机制实现
try {
Nameable newNameable = (Nameable) BaseUtils.cloneObject(selectedNameable);
newNameable.setName(createUnrepeatedCopyName(selectedNameable.getName()));
UIListControlPane.this.addNameable(newNameable, nameableList.getSelectedIndex() + 1);
} catch (Exception e) {
FRContext.getLogger().error(e.getMessage(), e);
}
getCommonHandlers().onCopyItem();
}
@Override
public void onMoveUpItem() {
int selectedIndex = nameableList.getSelectedIndex();
if (selectedIndex == -1) {
return;
}
// 上移
if (selectedIndex > 0) {
DefaultListModel listModel = (DefaultListModel) nameableList
.getModel();
Object selecteObj1 = listModel.get(selectedIndex - 1);
listModel.set(selectedIndex - 1, listModel.get(selectedIndex));
listModel.set(selectedIndex, selecteObj1);
nameableList.setSelectedIndex(selectedIndex - 1);
nameableList.ensureIndexIsVisible(selectedIndex - 1);
}
getCommonHandlers().onMoveUpItem();
}
@Override
public void onMoveDownItem() {
int selectedIndex = nameableList.getSelectedIndex();
if (selectedIndex == -1) {
return;
}
if (selectedIndex < nameableList.getModel().getSize() - 1) {
DefaultListModel listModel = (DefaultListModel) nameableList
.getModel();
Object selecteObj1 = listModel.get(selectedIndex + 1);
listModel.set(selectedIndex + 1, listModel.get(selectedIndex));
listModel.set(selectedIndex, selecteObj1);
nameableList.setSelectedIndex(selectedIndex + 1);
nameableList.ensureIndexIsVisible(selectedIndex + 1);
}
getCommonHandlers().onMoveDownItem();
}
@Override
public void onSortItem(boolean isAtoZ) {
// p:选中的值.
Object selectedValue = nameableList.getSelectedValue();
DefaultListModel listModel = (DefaultListModel) nameableList
.getModel();
Nameable[] nameableArray = new Nameable[listModel.getSize()];
if (nameableArray.length <= 0) {
return;
}
for (int i = 0; i < listModel.getSize(); i++) {
nameableArray[i] = ((ListModelElement) listModel.getElementAt(i)).wrapper;
}
// p:排序.
if (isAtoZ) {
Comparator<Nameable> nameableComparator = new Comparator<Nameable>() {
@Override
public int compare(Nameable o1, Nameable o2) {
return -ComparatorUtils.compare(o1.getName(), o2
.getName());
}
};
isAtoZ = !isAtoZ;
Arrays.sort(nameableArray, nameableComparator);
} else {
Comparator<Nameable> nameableComparator = new Comparator<Nameable>() {
@Override
public int compare(Nameable o1, Nameable o2) {
return ComparatorUtils.compare(o1.getName(), o2
.getName());
}
};
isAtoZ = !isAtoZ;
Arrays.sort(nameableArray, nameableComparator);
}
for (int i = 0; i < nameableArray.length; i++) {
listModel.set(i, new ListModelElement(nameableArray[i]));
}
// p:需要选中以前的那个值.
if (selectedValue != null) {
nameableList.setSelectedValue(selectedValue, true);
}
checkButtonEnabled();
// p:需要repaint.
nameableList.repaint();
getCommonHandlers().onSortItem(isAtoZ);
}
@Override
@ -517,16 +365,7 @@ public abstract class UIListControlPane extends UIControlPane implements ListCon
*/
@Override
public void checkButtonEnabled() {
int selectedIndex = nameableList.getSelectedIndex();
if (selectedIndex == -1) {
this.cardLayout.show(cardPane, "SELECT");
} else {
this.cardLayout.show(cardPane, "EDIT");
}
for (ShortCut4JControlPane sj : getShorts()) {
sj.checkEnable();
}
getHelper().checkButtonEnabled();
}
public BasicBeanPane createPaneByCreators(NameableCreator creator) {
@ -566,35 +405,9 @@ public abstract class UIListControlPane extends UIControlPane implements ListCon
((JControlUpdatePane) this.controlUpdatePane).checkValid();
}
private int getInValidIndex() {
BasicBeanPane[] p = ((JControlUpdatePane) controlUpdatePane).getUpdatePanes();
if (p != null) {
for (int i = 0; i < p.length; i++) {
if (p[i] != null) {
try {
p[i].checkValid();
} catch (Exception e) {
return i;
}
}
}
}
return -1;
}
@Override
protected boolean hasInvalid(boolean isAdd) {
int idx = UIListControlPane.this.getInValidIndex();
if (isAdd || nameableList.getSelectedIndex() != idx) {
try {
checkValid();
} catch (Exception exp) {
JOptionPane.showMessageDialog(UIListControlPane.this, exp.getMessage());
nameableList.setSelectedIndex(idx);
return true;
}
}
return false;
public boolean hasInvalid(boolean isAdd) {
return getHelper().hasInvalid(isAdd);
}
/**
* 设置选中项
@ -606,7 +419,22 @@ public abstract class UIListControlPane extends UIControlPane implements ListCon
}
@Override
public ListModelElement getSelectedElement() {
public int getSelectedIndex() {
return nameableList.getSelectedIndex();
}
@Override
public ListModelElement getSelectedValue() {
return (ListModelElement) this.nameableList.getSelectedValue();
}
@Override
public JControlUpdatePane getControlUpdatePane() {
return (JControlUpdatePane) controlUpdatePane;
}
@Override
public JNameEdList getNameableList() {
return nameableList;
}
}

1
designer-base/src/main/java/com/fr/design/gui/controlpane/shortcutfactory/NewShortCutFactory.java

@ -4,7 +4,6 @@ import com.fr.base.BaseUtils;
import com.fr.design.actions.UpdateAction;
import com.fr.design.actions.core.ActionFactory;
import com.fr.design.gui.HyperlinkFilterHelper;
import com.fr.design.gui.controlpane.AbstractShortCutFactory;
import com.fr.design.gui.controlpane.NameableCreator;
import com.fr.design.gui.controlpane.ShortCut4JControlPane;
import com.fr.design.gui.controlpane.ShortCutListenerProvider;

61
designer-base/src/main/java/com/fr/design/gui/ilist/JNameEdList.java

@ -22,7 +22,6 @@ import java.util.ArrayList;
import java.util.Vector;
public class JNameEdList extends UIList implements CellEditorListener {
private static final int TEST_LIST_LENTH = 20;
private static final int ICON_WIDTH = 20;
private boolean editable = true;
@ -232,7 +231,7 @@ public class JNameEdList extends UIList implements CellEditorListener {
}
Rectangle rect = this.getCellBounds(index, index);
// alex:所有的JNameEdList都有Icon,空出前面20 * 20的位置就是放的Icon
rect.setRect(createRect(rect, ICON_WIDTH));
rect.setRect(createRect(rect, getIconWidth()));
editorComp.setBounds(rect);
add(editorComp);
@ -247,6 +246,10 @@ public class JNameEdList extends UIList implements CellEditorListener {
return true;
}
public int getIconWidth() {
return ICON_WIDTH;
}
public Rectangle createRect(Rectangle rect, int iconWidth) {
return new Rectangle(rect.x + iconWidth, rect.y, rect.width - iconWidth, rect.height);
}
@ -301,9 +304,14 @@ public class JNameEdList extends UIList implements CellEditorListener {
String name = StringUtils.isBlank(value.toString()) ? oldName : value.toString();
setNameAt(name, editingIndex);
removeComp();
doAfterStopEditing();
}
}
protected void doAfterStopEditing() {
// default: do nothing
}
public String[] getAllNames() {
int length = this.getModel().getSize();
String[] names = new String[length];
@ -335,53 +343,4 @@ public class JNameEdList extends UIList implements CellEditorListener {
editorComp = null;
repaint(cellRect);
}
/**
* 主函数
*
* @param args 参数
*/
public static void main(String... args) {
JFrame f = new JFrame();
JPanel c = (JPanel) f.getContentPane();
c.setLayout(new BorderLayout());
ListModelElement[] data = new ListModelElement[TEST_LIST_LENTH];
for (int i = 0; i < TEST_LIST_LENTH; i++) {
data[i] = new ListModelElement(new NameObject(i + 1 + "", i));
}
final JNameEdList list = new JNameEdList(data);
list.setEditable(true);
list.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent evt) {
list.stopEditing();
if (evt.getClickCount() >= 2
&& SwingUtilities.isLeftMouseButton(evt)) {
list.editItemAt(list.getSelectedIndex());
}
}
})
;
list.setCellEditor(new DefaultListCellEditor(new UITextField()));
list.setCellRenderer(new NameableListCellRenderer());
c.add(list, BorderLayout.CENTER);
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.setSize(400, 600);
f.setVisible(true);
}
private static class NameableListCellRenderer extends DefaultListCellRenderer {
@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
boolean cellHasFocus) {
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
if (value instanceof Nameable) {
Nameable wrappee = (Nameable) value;
this.setText(wrappee.getName());
}
return this;
}
}
}

362
designer-base/src/main/java/com/fr/design/gui/ilist/UINameEdList.java

@ -19,19 +19,19 @@ import java.util.Vector;
/**
* Created by plough on 2017/7/23.
*/
public class UINameEdList extends UIList implements CellEditorListener {
private static final int TEST_LIST_LENTH = 20;
public class UINameEdList extends JNameEdList {
// private static final int TEST_LIST_LENTH = 20;
private static final int BUTTON_WIDTH = 25;
private boolean editable = true;
// kunsnat: 是否强制ListName是数字 (int型)
private boolean isNameShouldNumber = false;
transient protected ListCellEditor cellEditor;
transient protected Component editorComp;
transient protected int editingIndex;
private PropertyChangeAdapter editingListner;
private java.util.List<ModNameActionListener> ll = new ArrayList<ModNameActionListener>();
// private boolean editable = true;
//
// // kunsnat: 是否强制ListName是数字 (int型)
// private boolean isNameShouldNumber = false;
//
// transient protected ListCellEditor cellEditor;
// transient protected Component editorComp;
// transient protected int editingIndex;
// private PropertyChangeAdapter editingListner;
// private java.util.List<ModNameActionListener> ll = new ArrayList<ModNameActionListener>();
public UINameEdList(ListModel dataModel) {
super(dataModel);
@ -49,115 +49,6 @@ public class UINameEdList extends UIList implements CellEditorListener {
super();
}
/*
* Sets是否可编辑
*/
public void setEditable(boolean editable) {
this.editable = editable;
}
/**
* 是否可编辑
*
* @return 是则返回true
*/
public boolean isEditable() {
return this.editable;
}
public void setNameShouldNumber(boolean isNameShouldNumber) {
this.isNameShouldNumber = isNameShouldNumber;
}
/**
* 是否强制ListName是数字 (int型)
*
* @return 是则返回true
*/
public boolean isNameShouldNumber() {
return isNameShouldNumber;
}
/**
* 添加名字改变时的listener
*
* @param l 监听器
*/
public void addModNameActionListener(ModNameActionListener l) {
ll.add(l);
}
/**
* 编辑时的监听器
*
* @param l 监听器
*/
public void addEditingListner(PropertyChangeAdapter l) {
this.editingListner = l;
}
/**
* 移除某名字改变时的listener
*
* @param l 监听器
*/
public void removeModNameActionListener(ModNameActionListener l) {
ll.remove(l);
}
public ListCellEditor getCellEditor() {
if (cellEditor == null) {
UITextField editField = new UITextField();
if (editingListner != null) {
editField.addFocusListener(new FocusListener() {
@Override
public void focusLost(FocusEvent e) {
editingListner.propertyChange();
}
@Override
public void focusGained(FocusEvent e) {
// TODO Auto-generated method stub
}
});
}
cellEditor = new DefaultListCellEditor(editField) {
public boolean stopCellEditing() {
boolean isTrue = super.stopCellEditing();
stopEditing();
return isTrue;
}
};
cellEditor.addCellEditorListener(this);
}
return cellEditor;
}
protected void doAfterLostFocus() {
}
public void setCellEditor(ListCellEditor editor) {
this.cellEditor = editor;
}
/*
* 取得index节点的名字
*/
public String getNameAt(int index) {
Nameable nameable = ((ListModelElement) getModel().getElementAt(index)).wrapper;
if (nameable != null) {
return nameable.getName();
}
return null;
}
public Object getType(int index) {
Nameable nameable = ((ListModelElement) getModel().getElementAt(index)).wrapper;
if (nameable != null && nameable instanceof NameObject) {
@ -166,177 +57,8 @@ public class UINameEdList extends UIList implements CellEditorListener {
return null;
}
/*
* 设置index节点的名字
*/
// b:edit改变name的时候怎么办?
public void setNameAt(String name, int index) {
Nameable nameable = ((ListModelElement) getModel().getElementAt(index)).wrapper;
if (nameable != null) {
String oldName = nameable.getName();
if (isNameShouldNumber()) {
// kunsnat: 限制只能是数字(int型)
Number number = Utils.string2Number(name);
if (number == null) {
nameable.setName(oldName);
} else {
int newName = number.intValue();
nameable.setName(String.valueOf(newName));
}
} else {
nameable.setName(name);
}
for (int i = 0, len = ll.size(); i < len; i++) {
ll.get(i).nameModed(index, oldName, name);
}
}
}
/*
* 编辑第index个item
*/
private String oldName;
/**
* 编辑第index项
*
* @param index 序号
* @return 成功返回true
*/
public boolean editItemAt(int index) {
// 如果不可编辑,返回
if (!this.editable) {
return false;
}
if (cellEditor != null && !cellEditor.stopCellEditing()) {
return false;
}
if (index < 0 || index >= this.getModel().getSize()) {
return false;
}
ListCellEditor editor = getCellEditor();
Object value = editor.getCellEditorValue();
if (!StringUtils.isBlank(value.toString())) {
oldName = value.toString();
}
editorComp = prepareEditor(editor, index);
if (editorComp == null) {
return false;
}
Rectangle rect = this.getCellBounds(index, index);
// alex:所有的UINameEdList都有Icon,空出前面20 * 20的位置就是放的Icon
rect.setRect(createRect(rect, BUTTON_WIDTH));
editorComp.setBounds(rect);
add(editorComp);
editorComp.validate();
editorComp.requestFocus();
if (editorComp instanceof UITextField) {
((UITextField) editorComp).selectAll();
}
setEditingIndex(index);
return true;
}
public Rectangle createRect(Rectangle rect, int iconWidth) {
return new Rectangle(rect.x + iconWidth, rect.y, rect.width - iconWidth, rect.height);
}
public String getEditingName() {
return (String) getCellEditor().getCellEditorValue();
}
/*
* 根据ListCellEditor取得编辑器的Component
*/
private Component prepareEditor(ListCellEditor cellEditor, int index) {
String name = getNameAt(index);
boolean isSelected = this.isSelectedIndex(index);
Component comp = cellEditor.getListCellEditorComponent(this, name, isSelected, index);
return comp;
}
/*
* 记录正在编辑的index
*/
private void setEditingIndex(int idx) {
editingIndex = idx;
}
/**
* 编辑取消
*
* @param e 事件
*/
public void editingCanceled(ChangeEvent e) {
removeComp();
}
/**
* 编辑结束
*
* @param e 事件
*/
public void editingStopped(ChangeEvent e) {
doAfterLostFocus();
stopEditing();
}
/**
* 停止编辑事件
*/
public void stopEditing() {
ListCellEditor editor = getCellEditor();
if (editor != null && editorComp != null) {
Object value = editor.getCellEditorValue();
String name = StringUtils.isBlank(value.toString()) ? oldName : value.toString();
setNameAt(name, editingIndex);
removeComp();
doAfterStopEditing();
}
}
protected void doAfterStopEditing() {
// default: do nothing
}
public String[] getAllNames() {
int length = this.getModel().getSize();
String[] names = new String[length];
for (int i = 0; i < length; i++) {
names[i] = getNameAt(i);
}
return names;
}
public Object[] getAllTypes() {
int length = this.getModel().getSize();
Object[] types = new Object[length];
for (int i = 0; i < length; i++) {
types[i] = getType(i);
}
return types;
}
/*
* 移除编辑器的Component
*/
private void removeComp() {
if (editorComp != null) {
remove(editorComp);
}
Rectangle cellRect = this.getCellBounds(editingIndex, editingIndex);
setEditingIndex(-1);
editorComp = null;
repaint(cellRect);
public int getIconWidth() {
return BUTTON_WIDTH;
}
@Override
@ -355,61 +77,5 @@ public class UINameEdList extends UIList implements CellEditorListener {
*
* @param args 参数
*/
public static void main(String... args) {
JFrame f = new JFrame();
JPanel c = (JPanel) f.getContentPane();
c.setLayout(new BorderLayout());
ListModelElement[] data = new ListModelElement[TEST_LIST_LENTH];
for (int i = 0; i < TEST_LIST_LENTH; i++) {
data[i] = new ListModelElement(new NameObject(i + 1 + "", i));
}
final UINameEdList list = new UINameEdList(data);
list.setEditable(true);
list.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent evt) {
list.stopEditing();
if (evt.getClickCount() >= 2
&& SwingUtilities.isLeftMouseButton(evt)) {
list.editItemAt(list.getSelectedIndex());
}
}
@Override
public void mouseClicked(MouseEvent e) {
JList list = (JList) e.getSource();
if (list.locationToIndex(e.getPoint()) == -1 && !e.isShiftDown()
&& !isMenuShortcutKeyDown(e)) {
list.clearSelection();
}
}
private boolean isMenuShortcutKeyDown(InputEvent event) {
return (event.getModifiers() & Toolkit.getDefaultToolkit()
.getMenuShortcutKeyMask()) != 0;
}
})
;
list.setCellEditor(new DefaultListCellEditor(new UITextField()));
list.setCellRenderer(new NameableListCellRenderer());
c.add(list, BorderLayout.CENTER);
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.setSize(400, 600);
f.setVisible(true);
}
private static class NameableListCellRenderer extends DefaultListCellRenderer {
@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
boolean cellHasFocus) {
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
if (value instanceof Nameable) {
Nameable wrappee = (Nameable) value;
this.setText(wrappee.getName());
}
return this;
}
}
}

68
designer-base/src/test/java/com/fr/design/gui/ilist/JNameEdListTest.java

@ -0,0 +1,68 @@
package com.fr.design.gui.ilist;
import com.fr.design.gui.itextfield.UITextField;
import com.fr.general.NameObject;
import com.fr.stable.Nameable;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Toolkit;
import java.awt.event.InputEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
/**
* Created by plough on 2018/8/13.
*/
public class JNameEdListTest {
private static final int TEST_LIST_LENTH = 20;
public static void main(String... args) {
JFrame f = new JFrame();
JPanel c = (JPanel) f.getContentPane();
c.setLayout(new BorderLayout());
ListModelElement[] data = new ListModelElement[TEST_LIST_LENTH];
for (int i = 0; i < TEST_LIST_LENTH; i++) {
data[i] = new ListModelElement(new NameObject(i + 1 + "", i));
}
final JNameEdList list = new JNameEdList(data);
list.setEditable(true);
list.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent evt) {
list.stopEditing();
if (evt.getClickCount() >= 2
&& SwingUtilities.isLeftMouseButton(evt)) {
list.editItemAt(list.getSelectedIndex());
}
}
})
;
list.setCellEditor(new DefaultListCellEditor(new UITextField()));
list.setCellRenderer(new NameableListCellRenderer());
c.add(list, BorderLayout.CENTER);
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.setSize(400, 600);
f.setVisible(true);
}
private static class NameableListCellRenderer extends DefaultListCellRenderer {
@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
boolean cellHasFocus) {
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
if (value instanceof Nameable) {
Nameable wrappee = (Nameable) value;
this.setText(wrappee.getName());
}
return this;
}
}
}

82
designer-base/src/test/java/com/fr/design/gui/ilist/UINameEdListTest.java

@ -0,0 +1,82 @@
package com.fr.design.gui.ilist;
import com.fr.design.gui.itextfield.UITextField;
import com.fr.general.NameObject;
import com.fr.stable.Nameable;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Toolkit;
import java.awt.event.InputEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
/**
* Created by plough on 2018/8/13.
*/
public class UINameEdListTest {
private static final int TEST_LIST_LENTH = 20;
public static void main(String... args) {
JFrame f = new JFrame();
JPanel c = (JPanel) f.getContentPane();
c.setLayout(new BorderLayout());
ListModelElement[] data = new ListModelElement[TEST_LIST_LENTH];
for (int i = 0; i < TEST_LIST_LENTH; i++) {
data[i] = new ListModelElement(new NameObject(i + 1 + "", i));
}
final UINameEdList list = new UINameEdList(data);
list.setEditable(true);
list.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent evt) {
list.stopEditing();
if (evt.getClickCount() >= 2
&& SwingUtilities.isLeftMouseButton(evt)) {
list.editItemAt(list.getSelectedIndex());
}
}
@Override
public void mouseClicked(MouseEvent e) {
JList list = (JList) e.getSource();
if (list.locationToIndex(e.getPoint()) == -1 && !e.isShiftDown()
&& !isMenuShortcutKeyDown(e)) {
list.clearSelection();
}
}
private boolean isMenuShortcutKeyDown(InputEvent event) {
return (event.getModifiers() & Toolkit.getDefaultToolkit()
.getMenuShortcutKeyMask()) != 0;
}
})
;
list.setCellEditor(new DefaultListCellEditor(new UITextField()));
list.setCellRenderer(new NameableListCellRenderer());
c.add(list, BorderLayout.CENTER);
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.setSize(400, 600);
f.setVisible(true);
}
private static class NameableListCellRenderer extends DefaultListCellRenderer {
@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
boolean cellHasFocus) {
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
if (value instanceof Nameable) {
Nameable wrappee = (Nameable) value;
this.setText(wrappee.getName());
}
return this;
}
}
}

2
designer-chart/src/main/java/com/fr/van/chart/custom/component/VanChartShortCutFactory.java

@ -2,7 +2,7 @@ package com.fr.van.chart.custom.component;
import com.fr.design.gui.HyperlinkFilterHelper;
import com.fr.design.gui.controlpane.NameableCreator;
import com.fr.design.gui.controlpane.NewShortCutFactory;
import com.fr.design.gui.controlpane.shortcutfactory.NewShortCutFactory;
import com.fr.design.gui.controlpane.ShortCutListenerProvider;
import com.fr.design.menu.ShortCut;

Loading…
Cancel
Save