Collective self-trapping of atoms in a cavity