plough
6 years ago
16 changed files with 671 additions and 974 deletions
@ -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++; |
||||
} |
||||
} |
||||
} |
@ -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++; |
||||
} |
||||
} |
||||
} |
@ -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(); |
||||
} |
||||
} |
||||
|
||||
|
||||
} |
@ -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(); |
||||
} |
||||
|
@ -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; |
||||
} |
||||
} |
||||
} |
@ -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; |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue