📄 allocatableselection.java
字号:
public Class getColumnClass(int column) {
switch( column ) {
case 0:
return TreeTableModel.class;
case 1:
return Appointment[].class;
}
throw new IndexOutOfBoundsException();
}
}
abstract class AllocatablesModel extends AbstractTreeTableModel implements TreeTableModel {
TreeModel treeModel;
public AllocatablesModel() {
super(new DefaultMutableTreeNode());
treeModel = new DefaultTreeModel((DefaultMutableTreeNode)super.getRoot());
}
// Types of the columns.
Collection allocatables;
public void setAllocatables(Collection allocatables) throws RaplaException {
this.allocatables = allocatables;
treeModel =
getTreeFactory().createClassifiableModel((org.rapla.entities.domain.Allocatable[])
allocatables.toArray(Allocatable.ALLOCATABLE_ARRAY));
DefaultMutableTreeNode root = (DefaultMutableTreeNode)getRoot();
int childCount = root.getChildCount();
int[] childIndices = new int[childCount];
Object[] children = new Object[childCount];
for (int i=0;i<childCount;i++) {
childIndices[i]=i;
children[i] = root.getChildAt(i);
}
fireTreeStructureChanged(root,root.getPath(),childIndices,children);
}
public void setAllocatables(Collection allocatables,JTree tree) throws RaplaException {
this.allocatables = allocatables;
Collection expanded = new HashSet();
for (int i=0;i<tree.getRowCount();i++) {
if (tree.isExpanded(i)) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode)tree.getPathForRow(i).getLastPathComponent();
expanded.add(node.getUserObject());
}
}
setAllocatables(allocatables);
expandNodes( expanded, tree);
}
void expandNodes(Collection expanded, JTree tree) {
if (expanded.size() ==0)
return;
for (int i=0;i<tree.getRowCount();i++) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode)tree.getPathForRow(i).getLastPathComponent();
if (expanded.contains(node.getUserObject())) {
tree.expandRow(i);
}
}
}
public Collection getAllocatables() {
return allocatables;
}
public void expandObjects(Collection expandedNodes,JTree tree) {
// we need an enumeration, because we modife the set
Enumeration enumaration = ((DefaultMutableTreeNode)getRoot()).preorderEnumeration();
while (enumaration.hasMoreElements()) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode)enumaration.nextElement();
Iterator it = expandedNodes.iterator();
while (it.hasNext()) {
if (it.next().equals(node.getUserObject())) {
DefaultMutableTreeNode parent = (DefaultMutableTreeNode) node.getParent();
int row = tree.getRowForPath(new TreePath(parent.getPath()));
tree.expandRow(row);
}
}
}
}
public void selectObjects(Collection expandedNodes,JTree tree) {
Enumeration enumaration = ((DefaultMutableTreeNode)getRoot()).preorderEnumeration();
List selectionPaths= new ArrayList();
while (enumaration.hasMoreElements()) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode)enumaration.nextElement();
Iterator it = expandedNodes.iterator();
while (it.hasNext()) {
if (it.next().equals(node.getUserObject())) {
selectionPaths.add( new TreePath(node.getPath() ));
}
}
}
tree.setSelectionPaths( (TreePath[]) selectionPaths.toArray( new TreePath[]{}) );
}
public void treeDidChange() {
DefaultMutableTreeNode root = (DefaultMutableTreeNode)getRoot();
int childCount = root.getChildCount();
int[] childIndices = new int[childCount];
Object[] children = new Object[childCount];
for (int i=0;i<childCount;i++) {
childIndices[i]=i;
children[i] = root.getChildAt(i);
}
fireTreeNodesChanged(root,root.getPath(),childIndices,children);
}
public Object getRoot() {
return treeModel.getRoot();
}
public int getChildCount(Object node) {
return treeModel.getChildCount(node);
}
public Object getChild(Object node, int i) {
return treeModel.getChild(node,i);
}
}
class RestrictionCellRenderer extends DefaultTableCellRenderer {
private static final long serialVersionUID = 1L;
Object newValue;
JButton button = new JButton();
public void setValue(Object value) {
newValue = value;
super.setValue("");
}
public void setBounds(int x,int y, int width, int heigth) {
super.setBounds(x,y,width,heigth);
button.setBounds(x,y,width,heigth);
}
public void paint(Graphics g) {
Object value = newValue;
if (value instanceof Appointment[]) {
super.paint(g);
java.awt.Font f = g.getFont();
button.paint(g);
g.setFont(f);
paintRestriction(g,(Appointment[])value,this);
}
}
}
class AllocationCellRenderer extends DefaultTableCellRenderer {
private static final long serialVersionUID = 1L;
Object newValue;
public void setValue(Object value) {
newValue = value;
super.setValue("");
}
public void paint(Graphics g) {
Object value = newValue;
super.paint(g);
if (value instanceof Allocatable) {
paintAllocation(g,(Allocatable)value,this);
}
}
}
class RaplaToolTipRenderer implements TableToolTipRenderer {
public String getToolTipText(JTable table,int row,int column) {
Object value = table.getValueAt(row,column);
return getInfoFactory().getToolTip(value);
}
}
private int indexOf(Appointment appointment) {
for (int i=0;i<appointments.length;i++)
if (appointments[i].equals(appointment))
return i;
return -1;
}
private boolean conflictingAppointments[]; // stores the temp conflicting appointments
private int conflictCount; // temp value for conflicts
private int permissionConflictCount; // temp value for conflicts that are the result of denied permissions
// returns if the user is allowed to allocate the passed allocatable
private boolean isAllowed( Allocatable allocatable,Appointment appointment ) {
Date start = appointment.getStart();
Date end = appointment.getMaxEnd();
Date today = getQuery().today();
return allocatable.canAllocate(user, start, end, today );
}
// calculates the number of conflicting appointments for this allocatable
private void calcConflictingAppointments(Allocatable allocatable) {
if (conflictingAppointments == null || conflictingAppointments.length!=appointments.length)
conflictingAppointments = new boolean[appointments.length];
conflictCount = 0;
permissionConflictCount = 0;
for (int i=0;i<appointments.length;i++) {
Set allocatables = (Set) appointmentMap.get(appointments[i]);
if (allocatables != null && allocatables.contains(allocatable))
{
conflictingAppointments[i] = true;
conflictCount ++;
} else if (!isAllowed( allocatable, appointments[i] ) ) {
conflictingAppointments[i] = true;
conflictCount ++;
permissionConflictCount ++;
} else {
conflictingAppointments[i] = false;
}
}
}
private void paintAllocation(Graphics g,Allocatable allocatable,JComponent c) {
calcConflictingAppointments( allocatable );
if (appointments.length == 0) {
} else if (conflictCount == 0) {
g.setColor(Color.green);
g.drawString(getString("every_appointment"),2,c.getHeight()-4);
return;
} /*else if (conflictCount == appointments.length) {
g.setColor(Color.red);
g.drawString(getString("zero_appointment"),2,c.getHeight()-4);
return;
}*/
int x = 2;
Insets insets = c.getInsets();
FontMetrics fm = g.getFontMetrics();
for (int i=0;i<appointments.length;i++) {
if (conflictingAppointments[i])
continue;
x = paintApp(c,g,fm,i,insets,x);
}
}
private void paintRestriction(Graphics g,Appointment[] restriction,JComponent c) {
if (restriction.length == 0) {
g.drawString(getString("every_appointment"),2,c.getHeight()-4);
return;
}
int x = 0;
Insets insets = c.getInsets();
FontMetrics fm = g.getFontMetrics();
for ( int i = 0; i < appointments.length; i++ ) {
for ( int j = 0; j < restriction.length; j++ ) {
if ( restriction[j].equals( appointments[i] ) )
x = paintApp(c,g,fm,i,insets,x);
}
}
}
private int paintApp(Component c,Graphics g,FontMetrics fm,int index,Insets insets,int x) {
int xborder = 4;
int yborder = 1;
int width = fm.stringWidth(appointmentIndexStrings[index]);
x += xborder;
g.setColor(RaplaColorList.getAppointmentColor(index));
g.fillRoundRect(x
,insets.top
,width
,c.getHeight()-insets.top - insets.bottom - yborder * 2,4,4);
g.setColor(c.getForeground());
g.drawRoundRect(x-1
,insets.top
,width + 1
,c.getHeight()-insets.top - insets.bottom - yborder * 2,4,4);
g.drawString(appointmentIndexStrings[index]
,x
,c.getHeight() - yborder - fm.getDescent());
x += width;
x += 2;
int textWidth = fm.stringWidth(appointmentStrings[index]);
g.drawString(appointmentStrings[index]
,x
,c.getHeight()-fm.getDescent());
x += textWidth;
x += xborder;
return x;
}
class RestrictionTextField extends JTextField {
private static final long serialVersionUID = 1L;
Object newValue;
public void setValue(Object value) {
newValue = value;
}
public void paint(Graphics g) {
Object value = newValue;
super.paint(g);
if (value instanceof Appointment[]) {
paintRestriction(g,(Appointment[]) value,this);
}
}
}
class AppointmentCellEditor extends DefaultCellEditor
implements TableCellEditor,MouseListener,KeyListener,PopupMenuListener,ActionListener
{
private static final long serialVersionUID = 1L;
JPopupMenu menu = new JPopupMenu();
RestrictionTextField editingComponent;
boolean bStopEditingCalled = false; /* We need this variable to check if stopCellEditing
* was already called. */
DefaultMutableTreeNode selectedNode;
int selectedColumn = 0;
Appointment[] restriction;
public AppointmentCellEditor(RestrictionTextField textField) {
super(textField);
editingComponent = (RestrictionTextField)this.getComponent();
editingComponent.setEditable(false);
editingComponent.addMouseListener(this);
editingComponent.addKeyListener(this);
menu.addPopupMenuListener(this);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -