📄 allocatableselection.java
字号:
}
private List getAllocatableList(Allocatable[] allocatables) throws RaplaException {
List result = Arrays.asList( allocatables );
return result;
}
private List getAllAllocatables() throws RaplaException {
Allocatable[] allocatables = getQuery().getAllocatables( calendarModel.getFilter() );
return getAllocatableList( allocatables);
}
private List getAllocated() throws RaplaException {
return getAllocatableList( mutableReservation.getAllocatables() );
}
private boolean bWorkaround = false; // Workaround for Bug ID 4480264 on developer.java.sun.com
public void setReservation(Reservation mutableReservation) throws RaplaException {
this.mutableReservation = mutableReservation;
this.user = getUser();
//filter = getQuery().getAllocatableFilter("*");
setAppointments(mutableReservation.getAppointments());
List allocatableList = getAllAllocatables();
completeModel.setAllocatables( allocatableList );
// Expand allocatableTree if only one DynamicType
final CalendarModel calendarModel = (CalendarModel)getService( CalendarModel.ROLE);
completeModel.expandObjects( calendarModel.getSelectedObjectsAndChildren() ,completeTable.getTree() );
selectedModel.setAllocatables( getAllocated(), selectedTable.getTree());
updateBindings(appointments);
updateButtons();
JTree tree = selectedTable.getTree();
for (int i=0;i<tree.getRowCount();i++)
tree.expandRow(i);
// Workaround for Bug ID 4480264 on developer.java.sun.com
bWorkaround = true;
if (selectedTable.getRowCount() > 0 ) {
selectedTable.editCellAt(1, 1);
selectedTable.editCellAt(1, 0);
}
bWorkaround = false;
//filterAction.removePropertyChangeListener(listener);
filterAction.addPropertyChangeListener(listener);
btnFilter.setAction(filterAction);
// We have to add this after processing, because the Adapter in the JTreeTable does the same
SwingUtilities.invokeLater( new Runnable() {
public void run() {
completeModel.selectObjects( calendarModel.getSelectedObjects(), completeTable.getTree());
}
});
}
private void setAppointments(Appointment[] appointments) {
this.appointments = appointments;
this.appointmentStrings = new String[appointments.length];
this.appointmentIndexStrings = new String[appointments.length];
for (int i=0;i<appointments.length;i++) {
this.appointmentStrings[i] = getAppointmentFormater().getVeryShortSummary(appointments[i]);
this.appointmentIndexStrings[i] = getRaplaLocale().formatNumber(i + 1);
}
}
private boolean isAllocatableSelected(JTreeTable table) {
return isElementSelected( table, true );
}
private boolean isElementSelected(JTreeTable table, boolean allocatablesOnly) {
int start = table.getSelectionModel().getMinSelectionIndex();
int end = table.getSelectionModel().getMaxSelectionIndex();
if (start>=0) {
for (int i=start;i<=end;i++) {
TreePath path = table.getTree().getPathForRow(i);
if (path != null
&& ( !allocatablesOnly ||
((DefaultMutableTreeNode)
path.getLastPathComponent()).getUserObject() instanceof Allocatable) )
return true;
}
}
return false;
}
public Set getMarkedAllocatables() {
return new HashSet(getSelectedAllocatables(completeTable.getTree()));
}
protected Collection getSelectedAllocatables(JTree tree) {
return getSelectedElements( tree, true );
}
protected Collection getSelectedElements(JTree tree,boolean allocatablesOnly) {
TreePath[] path = tree.getSelectionPaths();
int size = 0;
if (path != null)
size = path.length;
Collection list = new ArrayList();
for (int i=0;i < size;i++) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) path[i].getLastPathComponent();
Object obj = node.getUserObject();
if (obj != null && (!allocatablesOnly || obj instanceof Allocatable))
list.add(obj);
}
return list;
}
protected void remove(Collection elements) throws RaplaException {
Iterator it = elements.iterator();
boolean bChanged = false;
while (it.hasNext()) {
Allocatable a = (Allocatable)it.next();
if (mutableReservation.hasAllocated(a)) {
mutableReservation.removeAllocatable(a);
bChanged = true;
}
}
if (bChanged) {
selectedModel.setAllocatables(getAllocated(), selectedTable.getTree());
}
fireAllocationsChanged();
}
protected void add(Collection elements) throws RaplaException {
Iterator it = elements.iterator();
boolean bChanged = false;
while (it.hasNext()) {
Allocatable a = (Allocatable) it.next();
if (!mutableReservation.hasAllocated(a)) {
mutableReservation.addAllocatable(a);
bChanged = true;
}
}
if (bChanged) {
selectedModel.setAllocatables( getAllocated(), selectedTable.getTree());
selectedModel.expandObjects( elements, selectedTable.getTree());
}
fireAllocationsChanged();
}
private Date findFirstStart() {
Date firstStart = null;
for (int i=0;i<appointments.length;i++)
if (firstStart == null || appointments[i].getStart().before(firstStart))
firstStart = appointments[i].getStart();
return firstStart;
}
private void updateButtons() {
{
boolean enable = isElementSelected( completeTable, false );
calendarAction1.setEnabled( enable );
enable = enable && isAllocatableSelected( completeTable );
addAction.setEnabled( enable );
}
{
boolean enable = isElementSelected( selectedTable, false );
calendarAction2.setEnabled( enable );
enable = enable && isAllocatableSelected( selectedTable );
removeAction.setEnabled( enable );
}
}
class Listener extends MouseAdapter implements ListSelectionListener, MouseListener ,PropertyChangeListener {
public void valueChanged(ListSelectionEvent e) {
updateButtons();
}
public void mousePressed(MouseEvent me) {
if (me.isPopupTrigger())
firePopup(me);
}
public void mouseReleased(MouseEvent me) {
if (me.isPopupTrigger())
firePopup(me);
}
public void mouseClicked(MouseEvent evt) {
if (evt.getClickCount() < 2) return;
JTreeTable table = (JTreeTable) evt.getSource();
int row = table.rowAtPoint(new Point(evt.getX(),evt.getY()));
if (row < 0)
return;
Object obj = table.getValueAt(row,0);
if (!(obj instanceof Allocatable))
return;
try {
if (table == completeTable)
add(getSelectedAllocatables(completeTable.getTree()));
else
remove(getSelectedAllocatables(selectedTable.getTree()));
} catch (RaplaException ex) {
showException(ex,content);
}
}
/** listener for the filter Dialog */
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getPropertyName().equals("filter")) {
try {
//filter = filterAction.getFilter();
completeModel.setAllocatables(
getAllAllocatables()
,completeTable.getTree());
} catch (Exception ex) {
showException(ex,getComponent());
}
}
}
}
protected void firePopup(MouseEvent me) {
Point p = new Point(me.getX(), me.getY());
JTreeTable table = ((JTreeTable)me.getSource());
int row = table.rowAtPoint(p);
int column = table.columnAtPoint(p);
Object selectedObject = null;
if ( row >= 0 && column >= 0 )
selectedObject = table.getValueAt(row, column);
//System.out.println("row " + row + " column " + column + " selected " + selectedObject);
showPopup(new PopupEvent(table, selectedObject, p));
}
public void showPopup(PopupEvent evt) {
try {
Point p = evt.getPoint();
JTreeTable table = ((JTreeTable)evt.getSource());
RaplaPopupMenu menu= new RaplaPopupMenu();
if (table == completeTable) {
menu.add(new JMenuItem(addAction));
menu.add(new JMenuItem(calendarAction1));
} else {
menu.add(new JMenuItem(removeAction));
menu.add(new JMenuItem(calendarAction2));
}
getQuery().getUsers();
menu.show(table, p.x, p.y);
} catch (RaplaException ex) {
showException (ex,getComponent());
}
}
class CompleteModel extends AllocatablesModel {
public int getColumnCount() { return 2;}
public boolean isCellEditable(Object node, int column) {
return false;
}
public Object getValueAt(Object node, int column) {
return ((DefaultMutableTreeNode) node).getUserObject();
}
public String getColumnName(int column) {
switch( column ) {
case 0:
return getString("selectable");
case 1:
return getString("selectable_on");
}
throw new IndexOutOfBoundsException();
}
public Class getColumnClass(int column) {
switch( column ) {
case 0:
return TreeTableModel.class;
case 1:
return Allocatable.class;
}
throw new IndexOutOfBoundsException();
}
}
class SelectedModel extends AllocatablesModel {
public int getColumnCount() { return 2;}
public boolean isCellEditable(Object node, int column) {
if (column == 1 && bWorkaround)
return true;
Object o = ((DefaultMutableTreeNode) node).getUserObject();
if (column == 1 && o instanceof Allocatable)
return true;
else
return false;
}
public Object getValueAt(Object node, int column) {
Object o = ((DefaultMutableTreeNode) node).getUserObject();
if (o instanceof Allocatable) {
switch( column ) {
case 0:
return o;
case 1:
return mutableReservation.getRestriction((Allocatable)o);
}
}
if (o instanceof DynamicType) {
return o;
}
return o;
//throw new IndexOutOfBoundsException();
}
public void setValueAt(Object value,Object node, int column) {
Object o = ((DefaultMutableTreeNode) node).getUserObject();
if (column == 1 && o instanceof Allocatable && value instanceof Appointment[]) {
if (mutableReservation.getRestriction((Allocatable) o) != value) {
mutableReservation.setRestriction((Allocatable) o ,(Appointment[]) value);
fireAllocationsChanged();
}
}
fireTreeNodesChanged(node
,((DefaultMutableTreeNode)node).getPath()
,new int[] {}
,new Object[] {});
}
public String getColumnName(int column) {
switch( column ) {
case 0:
return getString("selected");
case 1:
return getString("selected_on");
}
throw new IndexOutOfBoundsException();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -