forked from fanruan/design
mengao
8 years ago
44 changed files with 3636 additions and 3075 deletions
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,21 @@ |
|||||||
|
package com.fr.common.inputevent; |
||||||
|
|
||||||
|
import com.fr.stable.OperatingSystem; |
||||||
|
|
||||||
|
import java.awt.event.KeyEvent; |
||||||
|
import java.awt.event.MouseEvent; |
||||||
|
|
||||||
|
/** |
||||||
|
* Created by hzzz on 2017/5/26. |
||||||
|
*/ |
||||||
|
public class InputEventBaseOnOS { |
||||||
|
private static final boolean IS_MACOS = OperatingSystem.isMacOS(); |
||||||
|
|
||||||
|
public static boolean isControlDown(MouseEvent e) { |
||||||
|
return IS_MACOS ? e.isMetaDown() : e.isControlDown(); |
||||||
|
} |
||||||
|
|
||||||
|
public static boolean isControlDown(KeyEvent e) { |
||||||
|
return IS_MACOS ? e.isMetaDown() : e.isControlDown(); |
||||||
|
} |
||||||
|
} |
File diff suppressed because it is too large
Load Diff
@ -1,311 +1,300 @@ |
|||||||
package com.fr.design.gui.ilist; |
package com.fr.design.gui.ilist; |
||||||
|
|
||||||
import java.awt.BorderLayout; |
import com.fr.design.gui.icheckbox.UICheckBox; |
||||||
import java.awt.Component; |
import com.fr.stable.StringUtils; |
||||||
import java.awt.event.ActionEvent; |
|
||||||
import java.awt.event.ActionListener; |
import javax.swing.*; |
||||||
import java.awt.event.KeyAdapter; |
import javax.swing.border.Border; |
||||||
import java.awt.event.KeyEvent; |
import javax.swing.border.EmptyBorder; |
||||||
import java.awt.event.MouseAdapter; |
import java.awt.*; |
||||||
import java.awt.event.MouseEvent; |
import java.awt.event.*; |
||||||
import java.util.ArrayList; |
import java.util.ArrayList; |
||||||
import java.util.Arrays; |
import java.util.Arrays; |
||||||
import java.util.EventListener; |
import java.util.EventListener; |
||||||
import java.util.List; |
import java.util.List; |
||||||
|
|
||||||
import javax.swing.JComponent; |
|
||||||
import javax.swing.JList; |
|
||||||
import javax.swing.ListCellRenderer; |
|
||||||
import javax.swing.ListModel; |
|
||||||
import javax.swing.ListSelectionModel; |
|
||||||
import javax.swing.UIManager; |
|
||||||
import javax.swing.border.Border; |
|
||||||
import javax.swing.border.EmptyBorder; |
|
||||||
|
|
||||||
import com.fr.design.gui.icheckbox.UICheckBox; |
|
||||||
import com.fr.stable.StringUtils; |
|
||||||
|
|
||||||
/** |
/** |
||||||
* CheckBoxs + JList. |
* CheckBoxs + JList. |
||||||
*/ |
*/ |
||||||
public class CheckBoxList extends JComponent { |
public class CheckBoxList extends JComponent { |
||||||
/** |
private final static int X_COORDINATE = 20; |
||||||
* 选择状态----全选和全不选 |
|
||||||
* |
/** |
||||||
* @editor zhou |
* 选择状态----全选和全不选 |
||||||
* @since 2012-4-1下午2:39:10 |
* |
||||||
*/ |
* @editor zhou |
||||||
public static enum SelectedState { |
* @since 2012-4-1下午2:39:10 |
||||||
ALL, NONE |
*/ |
||||||
} |
public static enum SelectedState { |
||||||
|
ALL, NONE |
||||||
private boolean[] selects; |
} |
||||||
private JList jlist; |
|
||||||
private UICheckBox chooseAll; |
private boolean[] selects; |
||||||
|
private JList jlist; |
||||||
public CheckBoxList(Object[] items) { |
private UICheckBox chooseAll; |
||||||
this(items, SelectedState.NONE, StringUtils.EMPTY); |
|
||||||
} |
public CheckBoxList(Object[] items) { |
||||||
|
this(items, SelectedState.NONE, StringUtils.EMPTY); |
||||||
public CheckBoxList(Object[] items, String name) { |
} |
||||||
this(items, SelectedState.NONE, name); |
|
||||||
} |
public CheckBoxList(Object[] items, String name) { |
||||||
|
this(items, SelectedState.NONE, name); |
||||||
/** |
} |
||||||
* Class constructor. |
|
||||||
* |
/** |
||||||
* @param items |
* Class constructor. |
||||||
* Items with which to populate the list. |
* |
||||||
* @param default_state |
* @param items Items with which to populate the list. |
||||||
* default state, true or false |
* @param state default state, true or false |
||||||
*/ |
*/ |
||||||
public CheckBoxList(Object[] items, SelectedState state, String name) { |
public CheckBoxList(Object[] items, SelectedState state, String name) { |
||||||
jlist = new BOXLIST(items); |
jlist = new BOXLIST(items); |
||||||
jlist.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); |
jlist.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); |
||||||
this.selects = new boolean[items.length]; |
this.selects = new boolean[items.length]; |
||||||
boolean default_state = (state == SelectedState.ALL); |
boolean default_state = (state == SelectedState.ALL); |
||||||
Arrays.fill(this.selects, default_state); |
Arrays.fill(this.selects, default_state); |
||||||
|
|
||||||
jlist.setCellRenderer(new CheckListCellRenderer()); |
jlist.setCellRenderer(new CheckListCellRenderer()); |
||||||
jlist.addMouseListener(new MouseAdapter() { |
jlist.addMouseListener(new MouseAdapter() { |
||||||
public void mousePressed(MouseEvent e) { |
public void mousePressed(MouseEvent e) { |
||||||
doCheck(); |
doCheck(); |
||||||
} |
} |
||||||
|
|
||||||
public void mouseReleased(MouseEvent e) { |
public void mouseReleased(MouseEvent e) { |
||||||
doCheck(); |
doCheck(); |
||||||
} |
} |
||||||
}); |
}); |
||||||
jlist.addKeyListener(new KeyAdapter() { |
jlist.addKeyListener(new KeyAdapter() { |
||||||
|
|
||||||
@Override |
@Override |
||||||
public void keyTyped(KeyEvent e) { |
public void keyTyped(KeyEvent e) { |
||||||
if (e.getKeyChar() == ' ') { |
if (e.getKeyChar() == ' ') { |
||||||
doCheck(); |
doCheck(); |
||||||
} |
} |
||||||
} |
} |
||||||
|
|
||||||
}); |
}); |
||||||
this.setLayout(new BorderLayout()); |
this.setLayout(new BorderLayout()); |
||||||
chooseAll = new UICheckBox(name, default_state); |
chooseAll = new UICheckBox(name, default_state); |
||||||
chooseAll.addActionListener(new ActionListener() { |
chooseAll.addActionListener(new ActionListener() { |
||||||
|
|
||||||
@Override |
@Override |
||||||
public void actionPerformed(ActionEvent e) { |
public void actionPerformed(ActionEvent e) { |
||||||
if (chooseAll.isSelected()) { |
if (chooseAll.isSelected()) { |
||||||
setSelected(true); |
setSelected(true); |
||||||
} else { |
} else { |
||||||
setSelected(false); |
setSelected(false); |
||||||
} |
} |
||||||
} |
} |
||||||
}); |
}); |
||||||
this.add(chooseAll, BorderLayout.NORTH); |
this.add(chooseAll, BorderLayout.NORTH); |
||||||
this.add(jlist, BorderLayout.CENTER); |
this.add(jlist, BorderLayout.CENTER); |
||||||
} |
} |
||||||
|
|
||||||
/* |
/* |
||||||
* 用于CellRenderer显示value为text |
* 用于CellRenderer显示value为text |
||||||
*/ |
*/ |
||||||
protected String value2Text(Object value) { |
protected String value2Text(Object value) { |
||||||
return value != null ? value.toString() : StringUtils.EMPTY; |
return value != null ? value.toString() : StringUtils.EMPTY; |
||||||
} |
} |
||||||
|
|
||||||
public void setItems(Object[] os) { |
public void setItems(Object[] os) { |
||||||
if (os == null) { |
if (os == null) { |
||||||
this.setSelected(false); |
this.setSelected(false); |
||||||
} else { |
} else { |
||||||
for (int i = 0, len = os.length; i < len; i++) { |
for (int i = 0, len = os.length; i < len; i++) { |
||||||
Object o = os[i]; |
Object o = os[i]; |
||||||
for (int j = 0, jen = jlist.getModel().getSize(); j < jen; j++) { |
for (int j = 0, jen = jlist.getModel().getSize(); j < jen; j++) { |
||||||
if (o.equals(jlist.getModel().getElementAt(j))) { |
if (o.equals(jlist.getModel().getElementAt(j))) { |
||||||
this.setSelected(j, true); |
this.setSelected(j, true); |
||||||
} |
} |
||||||
} |
} |
||||||
} |
} |
||||||
} |
} |
||||||
this.repaint(); |
this.repaint(); |
||||||
} |
} |
||||||
|
|
||||||
/** |
/** |
||||||
* Is selected |
* Is selected |
||||||
*/ |
*/ |
||||||
public boolean isSelected(int index) { |
public boolean isSelected(int index) { |
||||||
if (selects == null || index >= selects.length) { |
if (selects == null || index >= selects.length) { |
||||||
return false; |
return false; |
||||||
} |
} |
||||||
|
|
||||||
return selects[index]; |
return selects[index]; |
||||||
} |
} |
||||||
|
|
||||||
public void setSelected(int index, boolean isSelected) { |
public void setSelected(int index, boolean isSelected) { |
||||||
if (selects == null || index >= selects.length) { |
if (selects == null || index >= selects.length) { |
||||||
return; |
return; |
||||||
} |
} |
||||||
|
|
||||||
selects[index] = isSelected; |
selects[index] = isSelected; |
||||||
this.repaint(this.getBounds()); |
this.repaint(this.getBounds()); |
||||||
|
|
||||||
this.fireCheckBoxListSelectionChangeListener(); |
this.fireCheckBoxListSelectionChangeListener(); |
||||||
} |
} |
||||||
|
|
||||||
private void setSelected(boolean isSelected) { |
private void setSelected(boolean isSelected) { |
||||||
if (selects == null) { |
if (selects == null) { |
||||||
return; |
return; |
||||||
} |
} |
||||||
for (int i = 0; i < selects.length; i++) { |
for (int i = 0; i < selects.length; i++) { |
||||||
selects[i] = isSelected; |
selects[i] = isSelected; |
||||||
} |
} |
||||||
this.repaint(this.getBounds()); |
this.repaint(this.getBounds()); |
||||||
|
|
||||||
this.fireCheckBoxListSelectionChangeListener(); |
this.fireCheckBoxListSelectionChangeListener(); |
||||||
} |
} |
||||||
|
|
||||||
/** |
/** |
||||||
* Returns an array of the objects that have been selected. Overrides the |
* Returns an array of the objects that have been selected. Overrides the |
||||||
* JList method. |
* JList method. |
||||||
*/ |
*/ |
||||||
public Object[] getSelectedValues() { |
public Object[] getSelectedValues() { |
||||||
return this.jlist.getSelectedValues(); |
return this.jlist.getSelectedValues(); |
||||||
} |
} |
||||||
|
|
||||||
private class BOXLIST extends JList { |
private class BOXLIST extends JList { |
||||||
public BOXLIST(Object[] items) { |
public BOXLIST(Object[] items) { |
||||||
super(items); |
super(items); |
||||||
} |
} |
||||||
|
|
||||||
@Override |
@Override |
||||||
protected void processMouseEvent(MouseEvent e) { |
protected void processMouseEvent(MouseEvent e) { |
||||||
if (e.getX() < 20) { |
if (e.getX() < X_COORDINATE) { |
||||||
if (e.isControlDown() || e.isAltDown() || e.isShiftDown() || e.isMetaDown()) { |
boolean anyMaskDown = e.isControlDown() || e.isAltDown() || e.isShiftDown() || e.isMetaDown(); |
||||||
int[] indices = getSelectedIndices(); |
if (anyMaskDown) { |
||||||
if (indices.length == 0) { |
int[] indices = getSelectedIndices(); |
||||||
super.processMouseEvent(e); |
if (indices.length == 0) { |
||||||
} |
super.processMouseEvent(e); |
||||||
} else { |
} |
||||||
super.processMouseEvent(e); |
} else { |
||||||
} |
super.processMouseEvent(e); |
||||||
} else { |
} |
||||||
super.processMouseEvent(e); |
} else { |
||||||
} |
super.processMouseEvent(e); |
||||||
int id = e.getID(); |
} |
||||||
switch (id) { |
int id = e.getID(); |
||||||
case MouseEvent.MOUSE_PRESSED: |
switch (id) { |
||||||
break; |
case MouseEvent.MOUSE_PRESSED: |
||||||
case MouseEvent.MOUSE_RELEASED: |
break; |
||||||
break; |
case MouseEvent.MOUSE_RELEASED: |
||||||
case MouseEvent.MOUSE_CLICKED: |
break; |
||||||
doCheck(); |
case MouseEvent.MOUSE_CLICKED: |
||||||
break; |
doCheck(); |
||||||
case MouseEvent.MOUSE_EXITED: |
break; |
||||||
break; |
case MouseEvent.MOUSE_EXITED: |
||||||
case MouseEvent.MOUSE_ENTERED: |
break; |
||||||
break; |
case MouseEvent.MOUSE_ENTERED: |
||||||
} |
break; |
||||||
} |
} |
||||||
|
} |
||||||
@Override |
|
||||||
protected void processMouseMotionEvent(MouseEvent e) { |
@Override |
||||||
if (e.getX() < 20) { |
protected void processMouseMotionEvent(MouseEvent e) { |
||||||
return; |
if (e.getX() < X_COORDINATE) { |
||||||
} |
return; |
||||||
|
} |
||||||
super.processMouseEvent(e); |
|
||||||
} |
super.processMouseEvent(e); |
||||||
|
} |
||||||
@Override |
|
||||||
public Object[] getSelectedValues() { |
@Override |
||||||
List<Object> list = new ArrayList<Object>(selects.length); |
public Object[] getSelectedValues() { |
||||||
for (int i = 0; i < selects.length; i++) { |
List<Object> list = new ArrayList<Object>(selects.length); |
||||||
if (selects[i]) { |
for (int i = 0; i < selects.length; i++) { |
||||||
list.add(this.getModel().getElementAt(i)); |
if (selects[i]) { |
||||||
} |
list.add(this.getModel().getElementAt(i)); |
||||||
} |
} |
||||||
|
} |
||||||
return list.toArray(); |
|
||||||
} |
return list.toArray(); |
||||||
|
} |
||||||
} |
|
||||||
|
} |
||||||
private void doCheck() { |
|
||||||
// p:这里必须改变所有选择checkbox.
|
private void doCheck() { |
||||||
int index = jlist.getSelectedIndex(); |
// p:这里必须改变所有选择checkbox.
|
||||||
boolean sValue = !selects[index]; |
int index = jlist.getSelectedIndex(); |
||||||
|
boolean sValue = !selects[index]; |
||||||
// p:开始设置所有选择的checkbox.
|
|
||||||
int[] indices = jlist.getSelectedIndices(); |
// p:开始设置所有选择的checkbox.
|
||||||
for (int i = 0; i < indices.length; i++) { |
int[] indices = jlist.getSelectedIndices(); |
||||||
setSelected(indices[i], sValue); |
for (int i = 0; i < indices.length; i++) { |
||||||
} |
setSelected(indices[i], sValue); |
||||||
for (boolean selected : selects) { |
} |
||||||
if (!selected) { |
for (boolean selected : selects) { |
||||||
chooseAll.setSelected(false); |
if (!selected) { |
||||||
return; |
chooseAll.setSelected(false); |
||||||
} |
return; |
||||||
} |
} |
||||||
chooseAll.setSelected(true); |
} |
||||||
repaint(); |
chooseAll.setSelected(true); |
||||||
} |
repaint(); |
||||||
|
} |
||||||
private static final Border noFocusBorder = new EmptyBorder(1, 1, 1, 1); |
|
||||||
|
private static final Border NO_FOCUS_BORDER = new EmptyBorder(1, 1, 1, 1); |
||||||
private class CheckListCellRenderer extends UICheckBox implements ListCellRenderer { |
|
||||||
|
private class CheckListCellRenderer extends UICheckBox implements ListCellRenderer { |
||||||
public CheckListCellRenderer() { |
|
||||||
this.setOpaque(true); |
public CheckListCellRenderer() { |
||||||
this.setBorder(noFocusBorder); |
this.setOpaque(true); |
||||||
} |
this.setBorder(NO_FOCUS_BORDER); |
||||||
|
} |
||||||
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { |
|
||||||
this.setText(value2Text(value)); |
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { |
||||||
this.setSelected(selects[index]); |
this.setText(value2Text(value)); |
||||||
this.setFont(list.getFont()); |
this.setSelected(selects[index]); |
||||||
|
this.setFont(list.getFont()); |
||||||
if (isSelected) { |
|
||||||
this.setBackground(list.getSelectionBackground()); |
if (isSelected) { |
||||||
this.setForeground(list.getSelectionForeground()); |
this.setBackground(list.getSelectionBackground()); |
||||||
} else { |
this.setForeground(list.getSelectionForeground()); |
||||||
this.setBackground(list.getBackground()); |
} else { |
||||||
this.setForeground(list.getForeground()); |
this.setBackground(list.getBackground()); |
||||||
} |
this.setForeground(list.getForeground()); |
||||||
|
} |
||||||
if (cellHasFocus) { |
|
||||||
this.setBorder(UIManager.getBorder("List.focusCellHighlightBorder")); |
if (cellHasFocus) { |
||||||
} else { |
this.setBorder(UIManager.getBorder("List.focusCellHighlightBorder")); |
||||||
this.setBorder(noFocusBorder); |
} else { |
||||||
} |
this.setBorder(NO_FOCUS_BORDER); |
||||||
|
} |
||||||
return this; |
|
||||||
} |
return this; |
||||||
} |
} |
||||||
|
} |
||||||
public void addCheckBoxListSelectionChangeListener(CheckBoxListSelectionChangeListener l) { |
|
||||||
this.listenerList.add(CheckBoxListSelectionChangeListener.class, l); |
public void addCheckBoxListSelectionChangeListener(CheckBoxListSelectionChangeListener l) { |
||||||
} |
this.listenerList.add(CheckBoxListSelectionChangeListener.class, l); |
||||||
|
} |
||||||
public void removeCheckBoxListSelectionChangeListener(CheckBoxListSelectionChangeListener l) { |
|
||||||
this.listenerList.remove(CheckBoxListSelectionChangeListener.class, l); |
public void removeCheckBoxListSelectionChangeListener(CheckBoxListSelectionChangeListener l) { |
||||||
} |
this.listenerList.remove(CheckBoxListSelectionChangeListener.class, l); |
||||||
|
} |
||||||
public void fireCheckBoxListSelectionChangeListener() { |
|
||||||
// Guaranteed to return a non-null array
|
public void fireCheckBoxListSelectionChangeListener() { |
||||||
Object[] listeners = listenerList.getListenerList(); |
// Guaranteed to return a non-null array
|
||||||
|
Object[] listeners = listenerList.getListenerList(); |
||||||
// Process the listeners last to first, notifying
|
|
||||||
// those that are interested in this event
|
// Process the listeners last to first, notifying
|
||||||
for (int i = listeners.length - 2; i >= 0; i -= 2) { |
// those that are interested in this event
|
||||||
if (listeners[i] == CheckBoxListSelectionChangeListener.class) { |
for (int i = listeners.length - 2; i >= 0; i -= 2) { |
||||||
((CheckBoxListSelectionChangeListener)listeners[i + 1]).selectionChanged(this); |
if (listeners[i] == CheckBoxListSelectionChangeListener.class) { |
||||||
} |
((CheckBoxListSelectionChangeListener) listeners[i + 1]).selectionChanged(this); |
||||||
} |
} |
||||||
|
} |
||||||
} |
|
||||||
|
} |
||||||
public static interface CheckBoxListSelectionChangeListener extends EventListener { |
|
||||||
public void selectionChanged(CheckBoxList target); |
public static interface CheckBoxListSelectionChangeListener extends EventListener { |
||||||
} |
public void selectionChanged(CheckBoxList target); |
||||||
|
} |
||||||
public ListModel getModel() { |
|
||||||
return jlist.getModel(); |
public ListModel getModel() { |
||||||
} |
return jlist.getModel(); |
||||||
|
} |
||||||
|
|
||||||
} |
} |
Loading…
Reference in new issue