Geertjan is a DZone Zone Leader and has posted 468 posts at DZone. You can read more from them at their website. View Full User Profile

GUI Tool for Patient/Doctor Scheduling

04.16.2013
| 3842 views |
  • submit to reddit

Let's create a GUI tool for scheduling doctors to patients:

The most important part of this tool is the ability to schedule doctors to time slots for patients. The workflow is that the user drags a doctor and drops it over a morning slot or afternoon slot for a specific patient. On the drop, the patient table is updated to show the newly assigned doctor, replacing the doctor that had been assigned prior to the drop.

All the rest of the above is standard NetBeans Platform stuff. A first attempt to drag from the BeanTreeView onto a Node in the OutlineView failed to achieve the desired result because the Node doesn't know about the view and hence doesn't know the location in the view where the drop occurs.

Therefore, the drop handling needs to be set on... the view:

private class PatientOutlineView extends OutlineView {

    Outline outline;

    public PatientOutlineView() {
        super("Patients");
        outline = getOutline();
        DropTarget dt = new DropTarget(outline, new PatientOutlineViewDropTargetListener());
        dt.setDefaultActions(DnDConstants.ACTION_COPY_OR_MOVE);
        dt.setActive(true);
        outline.setColumnHidingAllowed(false);
    }

    public class PatientOutlineViewDropTargetListener implements DropTargetListener {

        @Override
        public void drop(DropTargetDropEvent dtde) {
            Point locationOfDrop = dtde.getLocation();
            Node doctorNode = NodeTransfer.node(
                    dtde.getTransferable(),
                    NodeTransfer.DND_COPY_OR_MOVE);
            Doctor doctor = doctorNode.getLookup().lookup(Doctor.class);
            if (doctor != null) {
                int columnAtPoint = outline.columnAtPoint(locationOfDrop);
                int rowAtPoint = outline.rowAtPoint(locationOfDrop);
                if (columnAtPoint != 0) {
                    outline.setValueAt(doctor.getName(), rowAtPoint, columnAtPoint);
                    outline.setRowSelectionInterval(rowAtPoint, rowAtPoint);
                    Node currentPatientNode = getExplorerManager().getSelectedNodes()[0];
                    Patient currentPatient = currentPatientNode.getLookup().lookup(Patient.class);
                    StringBuilder messageBuilder = new StringBuilder();
                    messageBuilder.append("Assigned ").append(doctor.getName()).append(" to ").append(
                            currentPatient.getName());
                    if (columnAtPoint == 1) {
                        currentPatient.setMorningdoctor(doctor);
                        messageBuilder.append(" for the morning slot");
                    } else if (columnAtPoint == 2) {
                        currentPatient.setAfternoondoctor(doctor);
                        messageBuilder.append(" for the afternoon slot");
                    }
                    StatusDisplayer.getDefault().setStatusText(messageBuilder.toString());
                } else {
                    StatusDisplayer.getDefault().setStatusText(
                            "Cannot drop " + doctor.getName() + " at "
                            + rowAtPoint + " / " + columnAtPoint
                            + " because that's the Patients column");
                }
            }
        }

        @Override
        public void dragEnter(DropTargetDragEvent dtde) {
        }

        @Override
        public void dragExit(DropTargetEvent dtde) {
        }

        @Override
        public void dragOver(DropTargetDragEvent dtde) {
        }

        @Override
        public void dropActionChanged(DropTargetDragEvent dtde) {
        }

    }

}

There's at least one problem with this solution. When the user changes the position of the columns, the 0/0 row/column could be in a different place and then the doctor could be dropped on top of the patient, instead of on top of the currently assigned doctor. Aside from that, which can still be resolved somehow, the above is workable solution.

The complete source code of this example can be downloaded here:

http://java.net/projects/nb-api-samples/sources/api-samples/show/versions/7.3/misc/Scheduler

Published at DZone with permission of its author, Geertjan Wielenga.